File indexing completed on 2024-11-24 04:41:32

0001 /*
0002   SPDX-FileCopyrightText: 2001 Cornelius Schumacher <schumacher@kde.org>
0003   SPDX-FileCopyrightText: 2003-2004 Reinhold Kainhofer <reinhold@kainhofer.com>
0004 
0005   SPDX-License-Identifier: GPL-2.0-or-later WITH Qt-Commercial-exception-1.0
0006 */
0007 #include "alternatelabel.h"
0008 
0009 using namespace EventViews;
0010 
0011 AlternateLabel::AlternateLabel(const QString &shortlabel, const QString &longlabel, const QString &extensivelabel, QWidget *parent)
0012     : QLabel(parent)
0013     , mShortText(shortlabel)
0014     , mLongText(longlabel)
0015     , mExtensiveText(extensivelabel)
0016 {
0017     setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed));
0018     if (mExtensiveText.isEmpty()) {
0019         mExtensiveText = mLongText;
0020     }
0021     const QFontMetrics &fm = fontMetrics();
0022     // We use at least averageCharWidth * 2 here to avoid misalignment
0023     // for single char labels.
0024     setMinimumWidth(qMax(fm.averageCharWidth() * 2, fm.boundingRect(shortlabel).width()) + getIndent());
0025 
0026     squeezeTextToLabel();
0027 }
0028 
0029 AlternateLabel::~AlternateLabel() = default;
0030 
0031 void AlternateLabel::useShortText()
0032 {
0033     mTextTypeFixed = true;
0034     QLabel::setText(mShortText);
0035     setToolTip(mExtensiveText);
0036 }
0037 
0038 void AlternateLabel::useLongText()
0039 {
0040     mTextTypeFixed = true;
0041     QLabel::setText(mLongText);
0042     this->setToolTip(mExtensiveText);
0043 }
0044 
0045 void AlternateLabel::useExtensiveText()
0046 {
0047     mTextTypeFixed = true;
0048     QLabel::setText(mExtensiveText);
0049     this->setToolTip(QString());
0050 }
0051 
0052 void AlternateLabel::useDefaultText()
0053 {
0054     mTextTypeFixed = false;
0055     squeezeTextToLabel();
0056 }
0057 
0058 void AlternateLabel::squeezeTextToLabel()
0059 {
0060     if (mTextTypeFixed) {
0061         return;
0062     }
0063 
0064     QFontMetrics fm(fontMetrics());
0065     int labelWidth = size().width() - getIndent();
0066     int textWidth = fm.boundingRect(mLongText).width();
0067     int longTextWidth = fm.boundingRect(mExtensiveText).width();
0068     if (longTextWidth <= labelWidth) {
0069         QLabel::setText(mExtensiveText);
0070         this->setToolTip(QString());
0071     } else if (textWidth <= labelWidth) {
0072         QLabel::setText(mLongText);
0073         this->setToolTip(mExtensiveText);
0074     } else {
0075         QLabel::setText(mShortText);
0076         this->setToolTip(mExtensiveText);
0077     }
0078 }
0079 
0080 void AlternateLabel::resizeEvent(QResizeEvent *)
0081 {
0082     squeezeTextToLabel();
0083 }
0084 
0085 AlternateLabel::TextType AlternateLabel::largestFittingTextType() const
0086 {
0087     QFontMetrics fm(fontMetrics());
0088     const int labelWidth = size().width() - getIndent();
0089     const int longTextWidth = fm.boundingRect(mLongText).width();
0090     const int extensiveTextWidth = fm.boundingRect(mExtensiveText).width();
0091     if (extensiveTextWidth <= labelWidth) {
0092         return Extensive;
0093     } else if (longTextWidth <= labelWidth) {
0094         return Long;
0095     } else {
0096         return Short;
0097     }
0098 }
0099 
0100 void AlternateLabel::setFixedType(TextType type)
0101 {
0102     switch (type) {
0103     case Extensive:
0104         useExtensiveText();
0105         break;
0106     case Long:
0107         useLongText();
0108         break;
0109     case Short:
0110         useShortText();
0111         break;
0112     }
0113 }
0114 
0115 int AlternateLabel::getIndent() const
0116 {
0117     return indent() == -1 ? fontMetrics().boundingRect(QStringLiteral("x")).width() / 2 : indent();
0118 }
0119 
0120 #include "moc_alternatelabel.cpp"