File indexing completed on 2025-02-16 04:47:52
0001 /* 0002 SPDX-FileCopyrightText: 2001 Cornelius Schumacher <schumacher@kde.org> 0003 SPDX-FileCopyrightText: 2003-2004 Reinhold Kainhofer <reinhold@kainhofer.com> 0004 SPDX-FileCopyrightText: 2007 Loïc Corbasson <loic.corbasson@gmail.com> 0005 0006 SPDX-License-Identifier: GPL-2.0-or-later WITH Qt-Commercial-exception-1.0 0007 */ 0008 #include "decorationlabel.h" 0009 0010 #include <QDesktopServices> 0011 0012 #include <QMouseEvent> 0013 #include <QResizeEvent> 0014 0015 using namespace EventViews; 0016 0017 DecorationLabel::DecorationLabel(CalendarDecoration::Element *e, QWidget *parent) 0018 : QLabel(parent) 0019 , mDecorationElement(e) 0020 , mShortText(e->shortText()) 0021 , mLongText(e->longText()) 0022 , mExtensiveText(e->extensiveText()) 0023 , mPixmap(e->newPixmap(size())) 0024 , mUrl(e->url()) 0025 { 0026 setUrl(mUrl); 0027 0028 connect(e, &CalendarDecoration::Element::gotNewExtensiveText, this, &DecorationLabel::setExtensiveText); 0029 connect(e, &CalendarDecoration::Element::gotNewLongText, this, &DecorationLabel::setLongText); 0030 connect(e, &CalendarDecoration::Element::gotNewPixmap, this, &DecorationLabel::setPixmap); 0031 connect(e, &CalendarDecoration::Element::gotNewShortText, this, &DecorationLabel::setShortText); 0032 connect(e, &CalendarDecoration::Element::gotNewUrl, this, &DecorationLabel::setUrl); 0033 squeezeContentsToLabel(); 0034 } 0035 0036 DecorationLabel::DecorationLabel(const QString &shortText, 0037 const QString &longText, 0038 const QString &extensiveText, 0039 const QPixmap &pixmap, 0040 const QUrl &url, 0041 QWidget *parent) 0042 : QLabel(parent) 0043 , mShortText(shortText) 0044 , mLongText(longText) 0045 , mExtensiveText(extensiveText) 0046 , mPixmap(pixmap) 0047 { 0048 setUrl(url); 0049 0050 squeezeContentsToLabel(); 0051 } 0052 0053 DecorationLabel::~DecorationLabel() 0054 { 0055 delete mDecorationElement; 0056 } 0057 0058 void DecorationLabel::mouseReleaseEvent(QMouseEvent *event) 0059 { 0060 QLabel::mouseReleaseEvent(event); 0061 0062 switch (event->button()) { 0063 case Qt::LeftButton: 0064 if (!mUrl.isEmpty()) { 0065 QDesktopServices::openUrl(mUrl); 0066 setForegroundRole(QPalette::LinkVisited); 0067 } 0068 break; 0069 case Qt::MiddleButton: 0070 case Qt::RightButton: 0071 default: 0072 break; 0073 } 0074 } 0075 0076 void DecorationLabel::resizeEvent(QResizeEvent *event) 0077 { 0078 mPixmap = mDecorationElement->newPixmap(event->size()); 0079 QLabel::resizeEvent(event); 0080 squeezeContentsToLabel(); 0081 } 0082 0083 void DecorationLabel::setExtensiveText(const QString &text) 0084 { 0085 mExtensiveText = text; 0086 squeezeContentsToLabel(); 0087 } 0088 0089 void DecorationLabel::setLongText(const QString &text) 0090 { 0091 mLongText = text; 0092 squeezeContentsToLabel(); 0093 } 0094 0095 void DecorationLabel::setPixmap(const QPixmap &pixmap) 0096 { 0097 mPixmap = pixmap.scaled(size(), Qt::KeepAspectRatio); 0098 squeezeContentsToLabel(); 0099 } 0100 0101 void DecorationLabel::setShortText(const QString &text) 0102 { 0103 mShortText = text; 0104 squeezeContentsToLabel(); 0105 } 0106 0107 void DecorationLabel::setText(const QString &text) 0108 { 0109 setLongText(text); 0110 } 0111 0112 void DecorationLabel::setUrl(const QUrl &url) 0113 { 0114 mUrl = url; 0115 QFont f = font(); 0116 if (url.isEmpty()) { 0117 setForegroundRole(QPalette::WindowText); 0118 f.setUnderline(false); 0119 #ifndef QT_NO_CURSOR 0120 setCursor(QCursor(Qt::ArrowCursor)); 0121 #endif 0122 } else { 0123 setForegroundRole(QPalette::Link); 0124 f.setUnderline(true); 0125 #ifndef QT_NO_CURSOR 0126 setCursor(QCursor(Qt::PointingHandCursor)); 0127 #endif 0128 } 0129 setFont(f); 0130 } 0131 0132 void DecorationLabel::squeezeContentsToLabel() 0133 { 0134 if (!mAutomaticSqueeze) { // The content type to use has been set manually 0135 return; 0136 } 0137 0138 QFontMetrics fm(fontMetrics()); 0139 0140 int labelWidth = size().width(); 0141 int longTextWidth = fm.boundingRect(mLongText).width(); 0142 int extensiveTextWidth = fm.boundingRect(mExtensiveText).width(); 0143 0144 if (!mPixmap.isNull()) { 0145 usePixmap(true); 0146 } else if ((!mExtensiveText.isEmpty()) && (extensiveTextWidth <= labelWidth)) { 0147 useExtensiveText(true); 0148 } else if ((!mLongText.isEmpty()) && (longTextWidth <= labelWidth)) { 0149 useLongText(true); 0150 } else { 0151 useShortText(true); 0152 } 0153 0154 setAlignment(Qt::AlignCenter); 0155 setWordWrap(true); 0156 QSize msh = QLabel::minimumSizeHint(); 0157 msh.setHeight(fontMetrics().lineSpacing()); 0158 msh.setWidth(0); 0159 setMinimumSize(msh); 0160 setSizePolicy(sizePolicy().horizontalPolicy(), QSizePolicy::MinimumExpanding); 0161 } 0162 0163 void DecorationLabel::useDefaultText() 0164 { 0165 mAutomaticSqueeze = false; 0166 squeezeContentsToLabel(); 0167 } 0168 0169 void DecorationLabel::useExtensiveText(bool allowAutomaticSqueeze) 0170 { 0171 mAutomaticSqueeze = allowAutomaticSqueeze; 0172 QLabel::setText(mExtensiveText); 0173 setToolTip(QString()); 0174 } 0175 0176 void DecorationLabel::useLongText(bool allowAutomaticSqueeze) 0177 { 0178 mAutomaticSqueeze = allowAutomaticSqueeze; 0179 QLabel::setText(mLongText); 0180 setToolTip(mExtensiveText.isEmpty() ? QString() : mExtensiveText); 0181 } 0182 0183 void DecorationLabel::usePixmap(bool allowAutomaticSqueeze) 0184 { 0185 mAutomaticSqueeze = allowAutomaticSqueeze; 0186 QLabel::setPixmap(mPixmap); 0187 setToolTip(mExtensiveText.isEmpty() ? mLongText : mExtensiveText); 0188 } 0189 0190 void DecorationLabel::useShortText(bool allowAutomaticSqueeze) 0191 { 0192 mAutomaticSqueeze = allowAutomaticSqueeze; 0193 QLabel::setText(mShortText); 0194 setToolTip(mExtensiveText.isEmpty() ? mLongText : mExtensiveText); 0195 } 0196 0197 #include "moc_decorationlabel.cpp"