comparison src/gui/widgets/elided_label.cc @ 273:f31305b9f60a

*: various code safety changes this also makes the code build on Qt 5.7. I can't test it though because I don't have it working... FAIL!
author Paper <paper@paper.us.eu.org>
date Thu, 18 Apr 2024 16:53:17 -0400
parents 862d0d8619f6
children 6b0768158dcd
comparison
equal deleted inserted replaced
272:5437009cb10e 273:f31305b9f60a
47 47
48 void ElidedLabel::paintEvent(QPaintEvent* event) { 48 void ElidedLabel::paintEvent(QPaintEvent* event) {
49 QFrame::paintEvent(event); 49 QFrame::paintEvent(event);
50 50
51 QPainter painter(this); 51 QPainter painter(this);
52 QFontMetrics fontMetrics = painter.fontMetrics(); 52 QFontMetrics metrics = painter.fontMetrics();
53 53
54 int line_spacing = fontMetrics.lineSpacing(); 54 const int line_spacing = metrics.lineSpacing();
55 int y = 0; 55 int y = 0;
56 56
57 QTextLayout textLayout(content, painter.font()); 57 QTextLayout text_layout(content, painter.font());
58 textLayout.beginLayout(); 58 text_layout.beginLayout();
59 for (;;) { 59 for (;;) {
60 QTextLine line = textLayout.createLine(); 60 QTextLine line = text_layout.createLine();
61
62 if (!line.isValid()) 61 if (!line.isValid())
63 break; 62 break;
64 63
65 line.setLineWidth(width()); 64 line.setLineWidth(width());
66 int nextLineY = y + line_spacing;
67 65
68 if (height() >= nextLineY + line_spacing) { 66 if (height() >= y + (2 * line_spacing)) {
69 line.draw(&painter, QPoint(0, y)); 67 line.draw(&painter, QPoint(0, y));
70 y = nextLineY; 68 y += line_spacing;
71 } else { 69 } else {
72 QString last_line = content.mid(line.textStart()); 70 QString last_line = content.mid(line.textStart());
73 QString elided_last_line = fontMetrics.elidedText(last_line, Qt::ElideRight, width()); 71 QString elided_last_line = metrics.elidedText(last_line, Qt::ElideRight, width());
74 painter.drawText(QPoint(0, y + fontMetrics.ascent()), elided_last_line); 72 painter.drawText(QPoint(0, y + metrics.ascent()), elided_last_line);
75 line = textLayout.createLine(); 73 line = text_layout.createLine();
76 break; 74 break;
77 } 75 }
78 } 76 }
79 textLayout.endLayout(); 77 text_layout.endLayout();
80 } 78 }