Eventviews

decorationlabel.cpp
1/*
2 SPDX-FileCopyrightText: 2001 Cornelius Schumacher <schumacher@kde.org>
3 SPDX-FileCopyrightText: 2003-2004 Reinhold Kainhofer <reinhold@kainhofer.com>
4 SPDX-FileCopyrightText: 2007 Loïc Corbasson <loic.corbasson@gmail.com>
5
6 SPDX-License-Identifier: GPL-2.0-or-later WITH Qt-Commercial-exception-1.0
7*/
8#include "decorationlabel.h"
9
10#include <QDesktopServices>
11
12#include <QMouseEvent>
13#include <QResizeEvent>
14
15using namespace EventViews;
16
17DecorationLabel::DecorationLabel(CalendarDecoration::Element *e, QWidget *parent)
18 : QLabel(parent)
19 , mDecorationElement(e)
20 , mShortText(e->shortText())
21 , mLongText(e->longText())
22 , mExtensiveText(e->extensiveText())
23 , mPixmap(e->newPixmap(size()))
24 , mUrl(e->url())
25{
26 setUrl(mUrl);
27
28 connect(e, &CalendarDecoration::Element::gotNewExtensiveText, this, &DecorationLabel::setExtensiveText);
29 connect(e, &CalendarDecoration::Element::gotNewLongText, this, &DecorationLabel::setLongText);
30 connect(e, &CalendarDecoration::Element::gotNewPixmap, this, &DecorationLabel::setPixmap);
31 connect(e, &CalendarDecoration::Element::gotNewShortText, this, &DecorationLabel::setShortText);
32 connect(e, &CalendarDecoration::Element::gotNewUrl, this, &DecorationLabel::setUrl);
33 squeezeContentsToLabel();
34}
35
36DecorationLabel::DecorationLabel(const QString &shortText,
37 const QString &longText,
38 const QString &extensiveText,
39 const QPixmap &pixmap,
40 const QUrl &url,
41 QWidget *parent)
42 : QLabel(parent)
43 , mShortText(shortText)
44 , mLongText(longText)
45 , mExtensiveText(extensiveText)
46 , mPixmap(pixmap)
47{
48 setUrl(url);
49
50 squeezeContentsToLabel();
51}
52
53DecorationLabel::~DecorationLabel()
54{
55 delete mDecorationElement;
56}
57
58void DecorationLabel::mouseReleaseEvent(QMouseEvent *event)
59{
61
62 switch (event->button()) {
63 case Qt::LeftButton:
64 if (!mUrl.isEmpty()) {
67 }
68 break;
70 case Qt::RightButton:
71 default:
72 break;
73 }
74}
75
76void DecorationLabel::resizeEvent(QResizeEvent *event)
77{
78 mPixmap = mDecorationElement->newPixmap(event->size());
80 squeezeContentsToLabel();
81}
82
83void DecorationLabel::setExtensiveText(const QString &text)
84{
85 mExtensiveText = text;
86 squeezeContentsToLabel();
87}
88
89void DecorationLabel::setLongText(const QString &text)
90{
91 mLongText = text;
92 squeezeContentsToLabel();
93}
94
95void DecorationLabel::setPixmap(const QPixmap &pixmap)
96{
98 squeezeContentsToLabel();
99}
100
101void DecorationLabel::setShortText(const QString &text)
102{
103 mShortText = text;
104 squeezeContentsToLabel();
105}
106
107void DecorationLabel::setText(const QString &text)
108{
109 setLongText(text);
110}
111
112void DecorationLabel::setUrl(const QUrl &url)
113{
114 mUrl = url;
115 QFont f = font();
116 if (url.isEmpty()) {
118 f.setUnderline(false);
119#ifndef QT_NO_CURSOR
121#endif
122 } else {
124 f.setUnderline(true);
125#ifndef QT_NO_CURSOR
127#endif
128 }
129 setFont(f);
130}
131
132void DecorationLabel::squeezeContentsToLabel()
133{
134 if (!mAutomaticSqueeze) { // The content type to use has been set manually
135 return;
136 }
137
139
140 int labelWidth = size().width();
141 int longTextWidth = fm.boundingRect(mLongText).width();
142 int extensiveTextWidth = fm.boundingRect(mExtensiveText).width();
143
144 if (!mPixmap.isNull()) {
145 usePixmap(true);
146 } else if ((!mExtensiveText.isEmpty()) && (extensiveTextWidth <= labelWidth)) {
147 useExtensiveText(true);
148 } else if ((!mLongText.isEmpty()) && (longTextWidth <= labelWidth)) {
149 useLongText(true);
150 } else {
151 useShortText(true);
152 }
153
155 setWordWrap(true);
157 msh.setHeight(fontMetrics().lineSpacing());
158 msh.setWidth(0);
159 setMinimumSize(msh);
161}
162
163void DecorationLabel::useDefaultText()
164{
165 mAutomaticSqueeze = false;
166 squeezeContentsToLabel();
167}
168
169void DecorationLabel::useExtensiveText(bool allowAutomaticSqueeze)
170{
171 mAutomaticSqueeze = allowAutomaticSqueeze;
172 QLabel::setText(mExtensiveText);
174}
175
176void DecorationLabel::useLongText(bool allowAutomaticSqueeze)
177{
178 mAutomaticSqueeze = allowAutomaticSqueeze;
179 QLabel::setText(mLongText);
180 setToolTip(mExtensiveText.isEmpty() ? QString() : mExtensiveText);
181}
182
183void DecorationLabel::usePixmap(bool allowAutomaticSqueeze)
184{
185 mAutomaticSqueeze = allowAutomaticSqueeze;
186 QLabel::setPixmap(mPixmap);
187 setToolTip(mExtensiveText.isEmpty() ? mLongText : mExtensiveText);
188}
189
190void DecorationLabel::useShortText(bool allowAutomaticSqueeze)
191{
192 mAutomaticSqueeze = allowAutomaticSqueeze;
193 QLabel::setText(mShortText);
194 setToolTip(mExtensiveText.isEmpty() ? mLongText : mExtensiveText);
195}
196
197#include "moc_decorationlabel.cpp"
Class for calendar decoration elements.
virtual QPixmap newPixmap(const QSize &)
Return a pixmap for a given date and a given size.
Namespace EventViews provides facilities for displaying incidences, including events,...
Definition agenda.h:33
bool openUrl(const QUrl &url)
void setUnderline(bool enable)
void setAlignment(Qt::Alignment)
virtual bool event(QEvent *e) override
virtual QSize minimumSizeHint() const const override
virtual void mouseReleaseEvent(QMouseEvent *ev) override
void setWordWrap(bool on)
bool isNull() const const
QPixmap scaled(const QSize &size, Qt::AspectRatioMode aspectRatioMode, Qt::TransformationMode transformMode) const const
void setHeight(int height)
void setWidth(int width)
bool isEmpty() const const
AlignCenter
KeepAspectRatio
ArrowCursor
LeftButton
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
bool isEmpty() const const
void setCursor(const QCursor &)
QFontMetrics fontMetrics() const const
void setMinimumSize(const QSize &)
virtual void resizeEvent(QResizeEvent *event)
void setForegroundRole(QPalette::ColorRole role)
void setSizePolicy(QSizePolicy)
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.