| 1 |
#if defined(Hiro_Label) |
| 2 |
|
| 3 |
namespace hiro { |
| 4 |
|
| 5 |
auto pLabel::construct() -> void { |
| 6 |
qtWidget = qtLabel = new QtLabel(*this); |
| 7 |
|
| 8 |
pWidget::construct(); |
| 9 |
qtLabel->update(); |
| 10 |
} |
| 11 |
|
| 12 |
auto pLabel::destruct() -> void { |
| 13 |
delete qtLabel; |
| 14 |
qtWidget = qtLabel = nullptr; |
| 15 |
} |
| 16 |
|
| 17 |
auto pLabel::minimumSize() const -> Size { |
| 18 |
auto size = pFont::size(qtWidget->font(), state().text); |
| 19 |
return {size.width(), size.height()}; |
| 20 |
} |
| 21 |
|
| 22 |
auto pLabel::setAlignment(Alignment alignment) -> void { |
| 23 |
qtLabel->update(); |
| 24 |
} |
| 25 |
|
| 26 |
auto pLabel::setBackgroundColor(Color color) -> void { |
| 27 |
qtLabel->update(); |
| 28 |
} |
| 29 |
|
| 30 |
auto pLabel::setFont(const Font& font) -> void { |
| 31 |
pWidget::setFont(font); |
| 32 |
qtLabel->update(); |
| 33 |
} |
| 34 |
|
| 35 |
auto pLabel::setForegroundColor(Color color) -> void { |
| 36 |
qtLabel->update(); |
| 37 |
} |
| 38 |
|
| 39 |
auto pLabel::setText(const string& text) -> void { |
| 40 |
qtLabel->update(); |
| 41 |
} |
| 42 |
|
| 43 |
auto QtLabel::mousePressEvent(QMouseEvent* event) -> void { |
| 44 |
switch(event->button()) { |
| 45 |
case Qt::LeftButton: p.self().doMousePress(Mouse::Button::Left); break; |
| 46 |
case Qt::MiddleButton: p.self().doMousePress(Mouse::Button::Middle); break; |
| 47 |
case Qt::RightButton: p.self().doMousePress(Mouse::Button::Right); break; |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
auto QtLabel::mouseReleaseEvent(QMouseEvent* event) -> void { |
| 52 |
switch(event->button()) { |
| 53 |
case Qt::LeftButton: p.self().doMouseRelease(Mouse::Button::Left); break; |
| 54 |
case Qt::MiddleButton: p.self().doMouseRelease(Mouse::Button::Middle); break; |
| 55 |
case Qt::RightButton: p.self().doMouseRelease(Mouse::Button::Right); break; |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
//QLabel ignores QPalette ... so we have to implement our own Label class atop QWidget ... |
| 60 |
auto QtLabel::paintEvent(QPaintEvent* event) -> void { |
| 61 |
QPainter painter(p.qtLabel); |
| 62 |
if(auto& color = p.state().backgroundColor) { |
| 63 |
painter.fillRect(event->rect(), CreateColor(color)); |
| 64 |
} |
| 65 |
if(auto& text = p.state().text) { |
| 66 |
if(auto& color = p.state().foregroundColor) { |
| 67 |
QPen pen(CreateColor(color)); |
| 68 |
painter.setPen(pen); |
| 69 |
} |
| 70 |
auto alignment = p.state().alignment ? p.state().alignment : Alignment{0.0, 0.5}; |
| 71 |
painter.drawText(event->rect(), CalculateAlignment(alignment), QString::fromUtf8(text)); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
} |
| 76 |
|
| 77 |
#endif |