annotate src/gui/widgets/elided_label.cc @ 253:b3549da699a6

*: ooooh! stupid big commit! oops
author Paper <paper@paper.us.eu.org>
date Tue, 06 Feb 2024 16:56:32 -0500
parents
children 862d0d8619f6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
253
b3549da699a6 *: ooooh! stupid big commit!
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1 /*
b3549da699a6 *: ooooh! stupid big commit!
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2 * Copyright (C) 2016 The Qt Company Ltd.
b3549da699a6 *: ooooh! stupid big commit!
Paper <paper@paper.us.eu.org>
parents:
diff changeset
3 * Contact: https://www.qt.io/licensing/
b3549da699a6 *: ooooh! stupid big commit!
Paper <paper@paper.us.eu.org>
parents:
diff changeset
4 *
b3549da699a6 *: ooooh! stupid big commit!
Paper <paper@paper.us.eu.org>
parents:
diff changeset
5 * This file is part of the QtCore module of the Qt Toolkit.
b3549da699a6 *: ooooh! stupid big commit!
Paper <paper@paper.us.eu.org>
parents:
diff changeset
6 *
b3549da699a6 *: ooooh! stupid big commit!
Paper <paper@paper.us.eu.org>
parents:
diff changeset
7 * "Redistribution and use in source and binary forms, with or without
b3549da699a6 *: ooooh! stupid big commit!
Paper <paper@paper.us.eu.org>
parents:
diff changeset
8 * modification, are permitted provided that the following conditions are
b3549da699a6 *: ooooh! stupid big commit!
Paper <paper@paper.us.eu.org>
parents:
diff changeset
9 * met:
b3549da699a6 *: ooooh! stupid big commit!
Paper <paper@paper.us.eu.org>
parents:
diff changeset
10 * * Redistributions of source code must retain the above copyright
b3549da699a6 *: ooooh! stupid big commit!
Paper <paper@paper.us.eu.org>
parents:
diff changeset
11 * notice, this list of conditions and the following disclaimer.
b3549da699a6 *: ooooh! stupid big commit!
Paper <paper@paper.us.eu.org>
parents:
diff changeset
12 * * Redistributions in binary form must reproduce the above copyright
b3549da699a6 *: ooooh! stupid big commit!
Paper <paper@paper.us.eu.org>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in
b3549da699a6 *: ooooh! stupid big commit!
Paper <paper@paper.us.eu.org>
parents:
diff changeset
14 * the documentation and/or other materials provided with the
b3549da699a6 *: ooooh! stupid big commit!
Paper <paper@paper.us.eu.org>
parents:
diff changeset
15 * distribution.
b3549da699a6 *: ooooh! stupid big commit!
Paper <paper@paper.us.eu.org>
parents:
diff changeset
16 * * Neither the name of The Qt Company Ltd nor the names of its
b3549da699a6 *: ooooh! stupid big commit!
Paper <paper@paper.us.eu.org>
parents:
diff changeset
17 * contributors may be used to endorse or promote products derived
b3549da699a6 *: ooooh! stupid big commit!
Paper <paper@paper.us.eu.org>
parents:
diff changeset
18 * from this software without specific prior written permission.
b3549da699a6 *: ooooh! stupid big commit!
Paper <paper@paper.us.eu.org>
parents:
diff changeset
19 *
b3549da699a6 *: ooooh! stupid big commit!
Paper <paper@paper.us.eu.org>
parents:
diff changeset
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
b3549da699a6 *: ooooh! stupid big commit!
Paper <paper@paper.us.eu.org>
parents:
diff changeset
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
b3549da699a6 *: ooooh! stupid big commit!
Paper <paper@paper.us.eu.org>
parents:
diff changeset
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
b3549da699a6 *: ooooh! stupid big commit!
Paper <paper@paper.us.eu.org>
parents:
diff changeset
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
b3549da699a6 *: ooooh! stupid big commit!
Paper <paper@paper.us.eu.org>
parents:
diff changeset
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
b3549da699a6 *: ooooh! stupid big commit!
Paper <paper@paper.us.eu.org>
parents:
diff changeset
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
b3549da699a6 *: ooooh! stupid big commit!
Paper <paper@paper.us.eu.org>
parents:
diff changeset
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
b3549da699a6 *: ooooh! stupid big commit!
Paper <paper@paper.us.eu.org>
parents:
diff changeset
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
b3549da699a6 *: ooooh! stupid big commit!
Paper <paper@paper.us.eu.org>
parents:
diff changeset
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
b3549da699a6 *: ooooh! stupid big commit!
Paper <paper@paper.us.eu.org>
parents:
diff changeset
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
b3549da699a6 *: ooooh! stupid big commit!
Paper <paper@paper.us.eu.org>
parents:
diff changeset
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
b3549da699a6 *: ooooh! stupid big commit!
Paper <paper@paper.us.eu.org>
parents:
diff changeset
31 */
b3549da699a6 *: ooooh! stupid big commit!
Paper <paper@paper.us.eu.org>
parents:
diff changeset
32
b3549da699a6 *: ooooh! stupid big commit!
Paper <paper@paper.us.eu.org>
parents:
diff changeset
33 #include "gui/widgets/elided_label.h"
b3549da699a6 *: ooooh! stupid big commit!
Paper <paper@paper.us.eu.org>
parents:
diff changeset
34
b3549da699a6 *: ooooh! stupid big commit!
Paper <paper@paper.us.eu.org>
parents:
diff changeset
35 #include <QPainter>
b3549da699a6 *: ooooh! stupid big commit!
Paper <paper@paper.us.eu.org>
parents:
diff changeset
36 #include <QSizePolicy>
b3549da699a6 *: ooooh! stupid big commit!
Paper <paper@paper.us.eu.org>
parents:
diff changeset
37 #include <QTextLayout>
b3549da699a6 *: ooooh! stupid big commit!
Paper <paper@paper.us.eu.org>
parents:
diff changeset
38
b3549da699a6 *: ooooh! stupid big commit!
Paper <paper@paper.us.eu.org>
parents:
diff changeset
39 ElidedLabel::ElidedLabel(const QString& text, QWidget* parent)
b3549da699a6 *: ooooh! stupid big commit!
Paper <paper@paper.us.eu.org>
parents:
diff changeset
40 : QFrame(parent), content(text) {
b3549da699a6 *: ooooh! stupid big commit!
Paper <paper@paper.us.eu.org>
parents:
diff changeset
41 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
b3549da699a6 *: ooooh! stupid big commit!
Paper <paper@paper.us.eu.org>
parents:
diff changeset
42 }
b3549da699a6 *: ooooh! stupid big commit!
Paper <paper@paper.us.eu.org>
parents:
diff changeset
43
b3549da699a6 *: ooooh! stupid big commit!
Paper <paper@paper.us.eu.org>
parents:
diff changeset
44 void ElidedLabel::SetText(const QString& text) {
b3549da699a6 *: ooooh! stupid big commit!
Paper <paper@paper.us.eu.org>
parents:
diff changeset
45 content = text;
b3549da699a6 *: ooooh! stupid big commit!
Paper <paper@paper.us.eu.org>
parents:
diff changeset
46 update();
b3549da699a6 *: ooooh! stupid big commit!
Paper <paper@paper.us.eu.org>
parents:
diff changeset
47 }
b3549da699a6 *: ooooh! stupid big commit!
Paper <paper@paper.us.eu.org>
parents:
diff changeset
48
b3549da699a6 *: ooooh! stupid big commit!
Paper <paper@paper.us.eu.org>
parents:
diff changeset
49 void ElidedLabel::paintEvent(QPaintEvent *event) {
b3549da699a6 *: ooooh! stupid big commit!
Paper <paper@paper.us.eu.org>
parents:
diff changeset
50 QFrame::paintEvent(event);
b3549da699a6 *: ooooh! stupid big commit!
Paper <paper@paper.us.eu.org>
parents:
diff changeset
51
b3549da699a6 *: ooooh! stupid big commit!
Paper <paper@paper.us.eu.org>
parents:
diff changeset
52 QPainter painter(this);
b3549da699a6 *: ooooh! stupid big commit!
Paper <paper@paper.us.eu.org>
parents:
diff changeset
53 QFontMetrics fontMetrics = painter.fontMetrics();
b3549da699a6 *: ooooh! stupid big commit!
Paper <paper@paper.us.eu.org>
parents:
diff changeset
54
b3549da699a6 *: ooooh! stupid big commit!
Paper <paper@paper.us.eu.org>
parents:
diff changeset
55 int line_spacing = fontMetrics.lineSpacing();
b3549da699a6 *: ooooh! stupid big commit!
Paper <paper@paper.us.eu.org>
parents:
diff changeset
56 int y = 0;
b3549da699a6 *: ooooh! stupid big commit!
Paper <paper@paper.us.eu.org>
parents:
diff changeset
57
b3549da699a6 *: ooooh! stupid big commit!
Paper <paper@paper.us.eu.org>
parents:
diff changeset
58 QTextLayout textLayout(content, painter.font());
b3549da699a6 *: ooooh! stupid big commit!
Paper <paper@paper.us.eu.org>
parents:
diff changeset
59 textLayout.beginLayout();
b3549da699a6 *: ooooh! stupid big commit!
Paper <paper@paper.us.eu.org>
parents:
diff changeset
60 for (;;) {
b3549da699a6 *: ooooh! stupid big commit!
Paper <paper@paper.us.eu.org>
parents:
diff changeset
61 QTextLine line = textLayout.createLine();
b3549da699a6 *: ooooh! stupid big commit!
Paper <paper@paper.us.eu.org>
parents:
diff changeset
62
b3549da699a6 *: ooooh! stupid big commit!
Paper <paper@paper.us.eu.org>
parents:
diff changeset
63 if (!line.isValid())
b3549da699a6 *: ooooh! stupid big commit!
Paper <paper@paper.us.eu.org>
parents:
diff changeset
64 break;
b3549da699a6 *: ooooh! stupid big commit!
Paper <paper@paper.us.eu.org>
parents:
diff changeset
65
b3549da699a6 *: ooooh! stupid big commit!
Paper <paper@paper.us.eu.org>
parents:
diff changeset
66 line.setLineWidth(width());
b3549da699a6 *: ooooh! stupid big commit!
Paper <paper@paper.us.eu.org>
parents:
diff changeset
67 int nextLineY = y + line_spacing;
b3549da699a6 *: ooooh! stupid big commit!
Paper <paper@paper.us.eu.org>
parents:
diff changeset
68
b3549da699a6 *: ooooh! stupid big commit!
Paper <paper@paper.us.eu.org>
parents:
diff changeset
69 if (height() >= nextLineY + line_spacing) {
b3549da699a6 *: ooooh! stupid big commit!
Paper <paper@paper.us.eu.org>
parents:
diff changeset
70 line.draw(&painter, QPoint(0, y));
b3549da699a6 *: ooooh! stupid big commit!
Paper <paper@paper.us.eu.org>
parents:
diff changeset
71 y = nextLineY;
b3549da699a6 *: ooooh! stupid big commit!
Paper <paper@paper.us.eu.org>
parents:
diff changeset
72 } else {
b3549da699a6 *: ooooh! stupid big commit!
Paper <paper@paper.us.eu.org>
parents:
diff changeset
73 QString last_line = content.mid(line.textStart());
b3549da699a6 *: ooooh! stupid big commit!
Paper <paper@paper.us.eu.org>
parents:
diff changeset
74 QString elided_last_line = fontMetrics.elidedText(last_line, Qt::ElideRight, width());
b3549da699a6 *: ooooh! stupid big commit!
Paper <paper@paper.us.eu.org>
parents:
diff changeset
75 painter.drawText(QPoint(0, y + fontMetrics.ascent()), elided_last_line);
b3549da699a6 *: ooooh! stupid big commit!
Paper <paper@paper.us.eu.org>
parents:
diff changeset
76 line = textLayout.createLine();
b3549da699a6 *: ooooh! stupid big commit!
Paper <paper@paper.us.eu.org>
parents:
diff changeset
77 break;
b3549da699a6 *: ooooh! stupid big commit!
Paper <paper@paper.us.eu.org>
parents:
diff changeset
78 }
b3549da699a6 *: ooooh! stupid big commit!
Paper <paper@paper.us.eu.org>
parents:
diff changeset
79 }
b3549da699a6 *: ooooh! stupid big commit!
Paper <paper@paper.us.eu.org>
parents:
diff changeset
80 textLayout.endLayout();
b3549da699a6 *: ooooh! stupid big commit!
Paper <paper@paper.us.eu.org>
parents:
diff changeset
81 }