File: label.cpp

package info (click to toggle)
ares 134%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 34,680 kB
  • sloc: cpp: 338,717; ansic: 89,036; sh: 52; makefile: 27
file content (77 lines) | stat: -rw-r--r-- 2,100 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#if defined(Hiro_Label)

namespace hiro {

auto pLabel::construct() -> void {
  qtWidget = qtLabel = new QtLabel(*this);

  pWidget::construct();
  qtLabel->update();
}

auto pLabel::destruct() -> void {
  delete qtLabel;
  qtWidget = qtLabel = nullptr;
}

auto pLabel::minimumSize() const -> Size {
  auto size = pFont::size(qtWidget->font(), state().text);
  return {size.width(), size.height()};
}

auto pLabel::setAlignment(Alignment alignment) -> void {
  qtLabel->update();
}

auto pLabel::setBackgroundColor(Color color) -> void {
  qtLabel->update();
}

auto pLabel::setFont(const Font& font) -> void {
  pWidget::setFont(font);
  qtLabel->update();
}

auto pLabel::setForegroundColor(Color color) -> void {
  qtLabel->update();
}

auto pLabel::setText(const string& text) -> void {
  qtLabel->update();
}

auto QtLabel::mousePressEvent(QMouseEvent* event) -> void {
  switch(event->button()) {
  case Qt::LeftButton: p.self().doMousePress(Mouse::Button::Left); break;
  case Qt::MidButton: p.self().doMousePress(Mouse::Button::Middle); break;
  case Qt::RightButton: p.self().doMousePress(Mouse::Button::Right); break;
  }
}

auto QtLabel::mouseReleaseEvent(QMouseEvent* event) -> void {
  switch(event->button()) {
  case Qt::LeftButton: p.self().doMouseRelease(Mouse::Button::Left); break;
  case Qt::MidButton: p.self().doMouseRelease(Mouse::Button::Middle); break;
  case Qt::RightButton: p.self().doMouseRelease(Mouse::Button::Right); break;
  }
}

//QLabel ignores QPalette ... so we have to implement our own Label class atop QWidget ...
auto QtLabel::paintEvent(QPaintEvent* event) -> void {
  QPainter painter(p.qtLabel);
  if(auto& color = p.state().backgroundColor) {
    painter.fillRect(event->rect(), CreateColor(color));
  }
  if(auto& text = p.state().text) {
    if(auto& color = p.state().foregroundColor) {
      QPen pen(CreateColor(color));
      painter.setPen(pen);
    }
    auto alignment = p.state().alignment ? p.state().alignment : Alignment{0.0, 0.5};
    painter.drawText(event->rect(), CalculateAlignment(alignment), QString::fromUtf8(text));
  }
}

}

#endif