78 lines
2.4 KiB
C++
78 lines
2.4 KiB
C++
#pragma once
|
|
#include <optional>
|
|
#include <utility>
|
|
#include "SVG.h"
|
|
#include "global.h"
|
|
struct Text {
|
|
QFont font;
|
|
QPen pen;
|
|
QString text;
|
|
Text(QString text, QPen pen = QPen(), const QFont& font = QFont()) :
|
|
text(std::move(text)),
|
|
pen(std::move(pen)),
|
|
font(font) {
|
|
}
|
|
};
|
|
// 水平布局
|
|
struct Content : Paintable {
|
|
Text* text{};
|
|
SVG_Image_Render* icon{};
|
|
Layout layout;
|
|
enum Plottable {
|
|
Plottable_Icon,
|
|
Plottable_Text,
|
|
};
|
|
[[nodiscard]] QSize sizeHint() const override {
|
|
QSize ret{0, 0};
|
|
ret += QSize{layout.prefix + layout.suffix, layout.second_prefix + layout.second_suffix};
|
|
if (text) {
|
|
QFontMetrics fm(text->font);
|
|
ret += QSize{fm.horizontalAdvance(text->text), fm.height()};
|
|
}
|
|
return ret;
|
|
}
|
|
void set_icon(const QString& icon_path, const QColor& icon_color) {
|
|
icon = new SVG_Image_Render();
|
|
icon->set_path(icon_path).set_color(icon_color).create();
|
|
}
|
|
Content() {
|
|
layout.set_margin(4);
|
|
layout.main_type = Layout::Center_Space;
|
|
layout.second_type = Layout::Center_Space;
|
|
}
|
|
Event_Type resize_event(QResizeEvent* e) override {
|
|
layout.infos.clear();
|
|
int a = qMin(e->size().width(), e->size().height());
|
|
if (icon) {
|
|
Layout_Info t(Plottable_Icon, Layout_Item_Type::Fixed, Layout_Item_Type2::Fixed, a, a);
|
|
layout.infos.push_back(t);
|
|
}
|
|
if (text) {
|
|
QFontMetrics fm(text->font);
|
|
int len = fm.horizontalAdvance(text->text);
|
|
Layout_Info t(Plottable_Text, Layout_Item_Type::Fixed, Layout_Item_Type2::Expand, len, 0);
|
|
layout.infos.push_back(t);
|
|
}
|
|
layout.cacl(e->size().width(), e->size().height());
|
|
return Event_Type::refresh;
|
|
}
|
|
void paint(QPainter* painter, QRect rect) override {
|
|
painter->save();
|
|
if (icon) {
|
|
auto pos = layout.get(Plottable_Icon).value();
|
|
QRect r = pos.h_rect();
|
|
// painter->drawRect(r);
|
|
icon->render(painter, r);
|
|
}
|
|
if (text) {
|
|
painter->setPen(text->pen);
|
|
painter->setFont(text->font);
|
|
auto pos = layout.get(Plottable_Text).value();
|
|
QRect r = pos.h_rect();
|
|
// painter->drawRect(r);
|
|
painter->drawText(r, Qt::AlignCenter, text->text);
|
|
}
|
|
painter->restore();
|
|
}
|
|
};
|