• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

KDEUI

  • sources
  • kde-4.14
  • kdelibs
  • kdeui
  • widgets
ktitlewidget.cpp
Go to the documentation of this file.
1 /* This file is part of the KDE libraries
2  Copyright (C) 2007 Urs Wolfer <uwolfer @ kde.org>
3  Copyright (C) 2007 MichaĆ«l Larouche <larouche@kde.org>
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Library General Public
7  License version 2 as published by the Free Software Foundation.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Library General Public License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to
16  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  Boston, MA 02110-1301, USA.
18 */
19 
20 #include "ktitlewidget.h"
21 
22 #include <QtCore/QTimer>
23 #include <QtGui/QMouseEvent>
24 #include <QtGui/QFrame>
25 #include <QtGui/QLabel>
26 #include <QtGui/QLayout>
27 #include <QtGui/QTextDocument>
28 
29 #include <kicon.h>
30 #include <kiconloader.h>
31 
32 class KTitleWidget::Private
33 {
34 public:
35  Private(KTitleWidget* parent)
36  : q(parent),
37  autoHideTimeout(0),
38  messageType(InfoMessage)
39  {
40  }
41 
42  QString textStyleSheet() const
43  {
44  return QString("QLabel { font-weight: bold; color: %1}").arg(q->palette().color(QPalette::WindowText).name());
45  }
46 
47  QString commentStyleSheet() const
48  {
49  QString styleSheet;
50  switch (messageType) {
51  //FIXME: we need the usability color styles to implement different
52  // yet palette appropriate colours for the different use cases!
53  // also .. should we include an icon here,
54  // perhaps using the imageLabel?
55  case InfoMessage:
56  case WarningMessage:
57  case ErrorMessage:
58  styleSheet = QString("QLabel { color: palette(%1); background: palette(%2); }").arg(q->palette().color(QPalette::HighlightedText).name()).arg(q->palette().color(QPalette::Highlight).name());
59  break;
60  case PlainMessage:
61  default:
62  break;
63  }
64  return styleSheet;
65  }
66 
67  KTitleWidget* q;
68  QGridLayout *headerLayout;
69  QLabel *imageLabel;
70  QLabel *textLabel;
71  QLabel *commentLabel;
72  int autoHideTimeout;
73  MessageType messageType;
74 
80  QString iconTypeToIconName(KTitleWidget::MessageType type);
81 
82  void _k_timeoutFinished()
83  {
84  q->setVisible(false);
85  }
86 };
87 
88 QString KTitleWidget::Private::iconTypeToIconName(KTitleWidget::MessageType type)
89 {
90  switch (type) {
91  case KTitleWidget::InfoMessage:
92  return QLatin1String("dialog-information");
93  break;
94  case KTitleWidget::ErrorMessage:
95  return QLatin1String("dialog-error");
96  break;
97  case KTitleWidget::WarningMessage:
98  return QLatin1String("dialog-warning");
99  break;
100  case KTitleWidget::PlainMessage:
101  break;
102  }
103 
104  return QString();
105 }
106 
107 KTitleWidget::KTitleWidget(QWidget *parent)
108  : QWidget(parent),
109  d(new Private(this))
110 {
111  QFrame *titleFrame = new QFrame(this);
112  titleFrame->setAutoFillBackground(true);
113  titleFrame->setFrameShape(QFrame::StyledPanel);
114  titleFrame->setFrameShadow(QFrame::Plain);
115  titleFrame->setBackgroundRole(QPalette::Base);
116 
117  // default image / text part start
118  d->headerLayout = new QGridLayout(titleFrame);
119  d->headerLayout->setColumnStretch(0, 1);
120  d->headerLayout->setMargin(6);
121 
122  d->textLabel = new QLabel(titleFrame);
123  d->textLabel->setVisible(false);
124  d->textLabel->setTextInteractionFlags(Qt::TextSelectableByMouse | Qt::LinksAccessibleByMouse);
125 
126  d->imageLabel = new QLabel(titleFrame);
127  d->imageLabel->setVisible(false);
128 
129  d->headerLayout->addWidget(d->textLabel, 0, 0);
130  d->headerLayout->addWidget(d->imageLabel, 0, 1, 1, 2);
131 
132  d->commentLabel = new QLabel(titleFrame);
133  d->commentLabel->setVisible(false);
134  d->commentLabel->setOpenExternalLinks(true);
135  d->commentLabel->setWordWrap(true);
136  d->commentLabel->setTextInteractionFlags(Qt::TextSelectableByMouse | Qt::LinksAccessibleByMouse);
137  d->headerLayout->addWidget(d->commentLabel, 1, 0);
138 
139  // default image / text part end
140 
141  QVBoxLayout *mainLayout = new QVBoxLayout(this);
142  mainLayout->addWidget(titleFrame);
143  mainLayout->setMargin(0);
144  setLayout(mainLayout);
145 }
146 
147 KTitleWidget::~KTitleWidget()
148 {
149  delete d;
150 }
151 
152 bool KTitleWidget::eventFilter(QObject *object, QEvent *event)
153 {
154  // Hide message label on click
155  if (d->autoHideTimeout > 0 &&
156  event->type() == QEvent::MouseButtonPress) {
157  QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
158  if (mouseEvent && mouseEvent->button() == Qt::LeftButton) {
159  setVisible(false);
160  return true;
161  }
162  }
163 
164  return QWidget::eventFilter(object, event);
165 }
166 
167 void KTitleWidget::setWidget(QWidget *widget)
168 {
169  d->headerLayout->addWidget(widget, 2, 0, 1, 2);
170 }
171 
172 QString KTitleWidget::text() const
173 {
174  return d->textLabel->text();
175 }
176 
177 QString KTitleWidget::comment() const
178 {
179  return d->commentLabel->text();
180 }
181 
182 const QPixmap *KTitleWidget::pixmap() const
183 {
184  return d->imageLabel->pixmap();
185 }
186 
187 void KTitleWidget::setBuddy(QWidget *buddy)
188 {
189  d->textLabel->setBuddy(buddy);
190 }
191 
192 void KTitleWidget::changeEvent(QEvent *e)
193 {
194  QWidget::changeEvent(e);
195  if (e->type() == QEvent::PaletteChange) {
196  d->textLabel->setStyleSheet(d->textStyleSheet());
197  d->commentLabel->setStyleSheet(d->commentStyleSheet());
198  }
199 }
200 
201 void KTitleWidget::setText(const QString &text, Qt::Alignment alignment)
202 {
203  d->textLabel->setVisible(!text.isNull());
204 
205  if (!Qt::mightBeRichText(text)) {
206  d->textLabel->setStyleSheet(d->textStyleSheet());
207  }
208 
209  d->textLabel->setText(text);
210  d->textLabel->setAlignment(alignment);
211  show();
212 }
213 
214 void KTitleWidget::setText(const QString &text, MessageType type)
215 {
216  setPixmap(type);
217  setText(text);
218 }
219 
220 void KTitleWidget::setComment(const QString &comment, MessageType type)
221 {
222  d->commentLabel->setVisible(!comment.isNull());
223 
224  //TODO: should we override the current icon with the corresponding MessageType icon?
225  d->messageType = type;
226  d->commentLabel->setStyleSheet(d->commentStyleSheet());
227  d->commentLabel->setText(comment);
228  show();
229 }
230 
231 void KTitleWidget::setPixmap(const QPixmap &pixmap, ImageAlignment alignment)
232 {
233  d->imageLabel->setVisible(!pixmap.isNull());
234 
235  d->headerLayout->removeWidget(d->textLabel);
236  d->headerLayout->removeWidget(d->commentLabel);
237  d->headerLayout->removeWidget(d->imageLabel);
238 
239  if (alignment == ImageLeft) {
240  // swap the text and image labels around
241  d->headerLayout->addWidget(d->imageLabel, 0, 0, 2, 1);
242  d->headerLayout->addWidget(d->textLabel, 0, 1);
243  d->headerLayout->addWidget(d->commentLabel, 1, 1);
244  d->headerLayout->setColumnStretch(0, 0);
245  d->headerLayout->setColumnStretch(1, 1);
246  } else {
247  d->headerLayout->addWidget(d->textLabel, 0, 0);
248  d->headerLayout->addWidget(d->commentLabel, 1, 0);
249  d->headerLayout->addWidget(d->imageLabel, 0, 1, 2, 1);
250  d->headerLayout->setColumnStretch(1, 0);
251  d->headerLayout->setColumnStretch(0, 1);
252  }
253 
254  d->imageLabel->setPixmap(pixmap);
255 }
256 
257 
258 void KTitleWidget::setPixmap(const QString &icon, ImageAlignment alignment)
259 {
260  setPixmap(KIcon(icon), alignment);
261 }
262 
263 void KTitleWidget::setPixmap(const QIcon& icon, ImageAlignment alignment)
264 {
265  setPixmap(icon.pixmap(IconSize(KIconLoader::Dialog)), alignment);
266 }
267 
268 void KTitleWidget::setPixmap(MessageType type, ImageAlignment alignment)
269 {
270  setPixmap(KIcon(d->iconTypeToIconName(type)), alignment);
271 }
272 
273 int KTitleWidget::autoHideTimeout() const
274 {
275  return d->autoHideTimeout;
276 }
277 
278 void KTitleWidget::setAutoHideTimeout(int msecs)
279 {
280  d->autoHideTimeout = msecs;
281 
282  if (msecs > 0) {
283  installEventFilter(this);
284  } else {
285  removeEventFilter(this);
286  }
287 }
288 
289 void KTitleWidget::showEvent(QShowEvent *event)
290 {
291  Q_UNUSED(event)
292  if (d->autoHideTimeout > 0) {
293  QTimer::singleShot(d->autoHideTimeout, this, SLOT(_k_timeoutFinished()));
294  }
295 }
296 
297 #include "ktitlewidget.moc"
KTitleWidget::pixmap
const QPixmap * pixmap() const
KTitleWidget::InfoMessage
Information the user should be alerted to.
Definition: ktitlewidget.h:87
QWidget::styleSheet
QString styleSheet() const
QEvent
QWidget
KTitleWidget::KTitleWidget
KTitleWidget(QWidget *parent=0)
Constructs a title widget with the given.
Definition: ktitlewidget.cpp:107
QEvent::type
Type type() const
KTitleWidget::WarningMessage
A warning the user should be alerted to.
Definition: ktitlewidget.h:88
IconSize
int IconSize(KIconLoader::Group group)
Definition: kiconloader.cpp:1598
ktitlewidget.h
QFrame::setFrameShape
void setFrameShape(Shape)
QWidget::setVisible
virtual void setVisible(bool visible)
KTitleWidget::text
QString text() const
KTitleWidget::showEvent
void showEvent(QShowEvent *event)
Definition: ktitlewidget.cpp:289
kiconloader.h
QGridLayout
QMouseEvent
Qt::Alignment
typedef Alignment
KTitleWidget::MessageType
MessageType
Comment message types.
Definition: ktitlewidget.h:85
QIcon::pixmap
QPixmap pixmap(const QSize &size, Mode mode, State state) const
QString::isNull
bool isNull() const
QBoxLayout::addWidget
void addWidget(QWidget *widget, int stretch, QFlags< Qt::AlignmentFlag > alignment)
KTitleWidget::~KTitleWidget
virtual ~KTitleWidget()
Definition: ktitlewidget.cpp:147
KTitleWidget::setWidget
void setWidget(QWidget *widget)
Definition: ktitlewidget.cpp:167
QWidget::setLayout
void setLayout(QLayout *layout)
QObject::installEventFilter
void installEventFilter(QObject *filterObj)
KTitleWidget::PlainMessage
Normal comment.
Definition: ktitlewidget.h:86
QShowEvent
QObject
QMouseEvent::button
Qt::MouseButton button() const
KTitleWidget::eventFilter
bool eventFilter(QObject *object, QEvent *event)
Definition: ktitlewidget.cpp:152
KIcon
A wrapper around QIcon that provides KDE icon features.
Definition: kicon.h:40
QVBoxLayout
QObject::eventFilter
virtual bool eventFilter(QObject *watched, QEvent *event)
KIconLoader::Dialog
Icons for use in dialog titles, page lists, etc.
Definition: kiconloader.h:143
QString
QLayout::setMargin
void setMargin(int margin)
QWidget::changeEvent
virtual void changeEvent(QEvent *event)
MessageType
MessageType
KTitleWidget::ImageAlignment
ImageAlignment
Possible title pixmap alignments.
Definition: ktitlewidget.h:77
QPixmap
KTitleWidget::comment
QString comment() const
KTitleWidget::changeEvent
void changeEvent(QEvent *e)
Definition: ktitlewidget.cpp:192
QPixmap::isNull
bool isNull() const
QFrame
QFrame::setFrameShadow
void setFrameShadow(Shadow)
KTitleWidget::ImageLeft
Display the pixmap on the left.
Definition: ktitlewidget.h:78
KTitleWidget::setAutoHideTimeout
void setAutoHideTimeout(int msecs)
Set the autohide timeout of the label Set value to 0 to disable autohide, which is the default...
Definition: ktitlewidget.cpp:278
QLatin1String
KTitleWidget
Standard title widget with a white background and round border.
Definition: ktitlewidget.h:61
KTitleWidget::setBuddy
void setBuddy(QWidget *buddy)
Sets this label's buddy to buddy.
Definition: ktitlewidget.cpp:187
Qt::mightBeRichText
bool mightBeRichText(const QString &text)
KTitleWidget::setPixmap
void setPixmap(const QPixmap &pixmap, ImageAlignment alignment=ImageRight)
Definition: ktitlewidget.cpp:231
KTitleWidget::ErrorMessage
An error message.
Definition: ktitlewidget.h:89
QWidget::setAutoFillBackground
void setAutoFillBackground(bool enabled)
QWidget::show
void show()
KTitleWidget::setText
void setText(const QString &text, Qt::Alignment alignment=Qt::AlignLeft|Qt::AlignVCenter)
Definition: ktitlewidget.cpp:201
QWidget::setBackgroundRole
void setBackgroundRole(QPalette::ColorRole role)
KTitleWidget::setComment
void setComment(const QString &comment, MessageType type=PlainMessage)
Definition: ktitlewidget.cpp:220
kicon.h
QLabel
QObject::parent
QObject * parent() const
QString::arg
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
QWidget::event
virtual bool event(QEvent *event)
QObject::removeEventFilter
void removeEventFilter(QObject *obj)
KTitleWidget::autoHideTimeout
int autoHideTimeout() const
Get the current timeout value in milliseconds.
QIcon
QTimer::singleShot
singleShot
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:24:00 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDEUI

Skip menu "KDEUI"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal