Eventviews

alternatelabel.cpp
1/*
2 SPDX-FileCopyrightText: 2001 Cornelius Schumacher <schumacher@kde.org>
3 SPDX-FileCopyrightText: 2003-2004 Reinhold Kainhofer <reinhold@kainhofer.com>
4
5 SPDX-License-Identifier: GPL-2.0-or-later WITH Qt-Commercial-exception-1.0
6*/
7#include "alternatelabel.h"
8
9using namespace EventViews;
10
11AlternateLabel::AlternateLabel(const QString &shortlabel, const QString &longlabel, const QString &extensivelabel, QWidget *parent)
12 : QLabel(parent)
13 , mShortText(shortlabel)
14 , mLongText(longlabel)
15 , mExtensiveText(extensivelabel)
16{
18 if (mExtensiveText.isEmpty()) {
19 mExtensiveText = mLongText;
20 }
21 const QFontMetrics &fm = fontMetrics();
22 // We use at least averageCharWidth * 2 here to avoid misalignment
23 // for single char labels.
24 setMinimumWidth(qMax(fm.averageCharWidth() * 2, fm.boundingRect(shortlabel).width()) + getIndent());
25
26 squeezeTextToLabel();
27}
28
29AlternateLabel::~AlternateLabel() = default;
30
31void AlternateLabel::useShortText()
32{
33 mTextTypeFixed = true;
34 QLabel::setText(mShortText);
35 setToolTip(mExtensiveText);
36}
37
38void AlternateLabel::useLongText()
39{
40 mTextTypeFixed = true;
41 QLabel::setText(mLongText);
42 this->setToolTip(mExtensiveText);
43}
44
45void AlternateLabel::useExtensiveText()
46{
47 mTextTypeFixed = true;
48 QLabel::setText(mExtensiveText);
49 this->setToolTip(QString());
50}
51
52void AlternateLabel::useDefaultText()
53{
54 mTextTypeFixed = false;
55 squeezeTextToLabel();
56}
57
58void AlternateLabel::squeezeTextToLabel()
59{
60 if (mTextTypeFixed) {
61 return;
62 }
63
65 int labelWidth = size().width() - getIndent();
66 int textWidth = fm.boundingRect(mLongText).width();
67 int longTextWidth = fm.boundingRect(mExtensiveText).width();
68 if (longTextWidth <= labelWidth) {
69 QLabel::setText(mExtensiveText);
70 this->setToolTip(QString());
71 } else if (textWidth <= labelWidth) {
72 QLabel::setText(mLongText);
73 this->setToolTip(mExtensiveText);
74 } else {
75 QLabel::setText(mShortText);
76 this->setToolTip(mExtensiveText);
77 }
78}
79
80void AlternateLabel::resizeEvent(QResizeEvent *)
81{
82 squeezeTextToLabel();
83}
84
85AlternateLabel::TextType AlternateLabel::largestFittingTextType() const
86{
88 const int labelWidth = size().width() - getIndent();
89 const int longTextWidth = fm.boundingRect(mLongText).width();
90 const int extensiveTextWidth = fm.boundingRect(mExtensiveText).width();
91 if (extensiveTextWidth <= labelWidth) {
92 return Extensive;
93 } else if (longTextWidth <= labelWidth) {
94 return Long;
95 } else {
96 return Short;
97 }
98}
99
100void AlternateLabel::setFixedType(TextType type)
101{
102 switch (type) {
103 case Extensive:
104 useExtensiveText();
105 break;
106 case Long:
107 useLongText();
108 break;
109 case Short:
110 useShortText();
111 break;
112 }
113}
114
115int AlternateLabel::getIndent() const
116{
117 return indent() == -1 ? fontMetrics().boundingRect(QStringLiteral("x")).width() / 2 : indent();
118}
119
120#include "moc_alternatelabel.cpp"
Namespace EventViews provides facilities for displaying incidences, including events,...
Definition agenda.h:33
int averageCharWidth() const const
QRect boundingRect(QChar ch) const const
void setText(const QString &)
int width() const const
QFontMetrics fontMetrics() const const
void setToolTip(const QString &)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:57:55 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.