annotate src/gui/widgets/elided_label.cc @ 326:10096c5489e3

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