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

KDEUI

ktitlewidget.cpp

Go to the documentation of this file.
00001 /* This file is part of the KDE libraries
00002    Copyright (C) 2007 Urs Wolfer <uwolfer @ kde.org>
00003    Copyright (C) 2007 Michaƫl Larouche <larouche@kde.org>
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License version 2 as published by the Free Software Foundation.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB. If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017    Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include "ktitlewidget.h"
00021 
00022 #include <QtCore/QTimer>
00023 #include <QtGui/QMouseEvent>
00024 #include <QtGui/QFrame>
00025 #include <QtGui/QLabel>
00026 #include <QtGui/QLayout>
00027 #include <QtGui/QTextDocument>
00028 
00029 #include <kicon.h>
00030 #include <kiconloader.h>
00031 
00032 class KTitleWidget::Private
00033 {
00034 public:
00035     Private(KTitleWidget* parent)
00036         : q(parent),
00037           autoHideTimeout(0)
00038     {
00039     }
00040 
00041     KTitleWidget* q;
00042     QGridLayout *headerLayout;
00043     QLabel *imageLabel;
00044     QLabel *textLabel;
00045     QLabel *commentLabel;
00046     int autoHideTimeout;
00047 
00053     QString iconTypeToIconName(KTitleWidget::MessageType type);
00054 
00055     void _k_timeoutFinished()
00056     {
00057         q->setVisible(false);
00058     }
00059 };
00060 
00061 QString KTitleWidget::Private::iconTypeToIconName(KTitleWidget::MessageType type)
00062 {
00063     switch (type) {
00064         case KTitleWidget::InfoMessage:
00065             return QLatin1String("dialog-information");
00066             break;
00067         case KTitleWidget::ErrorMessage:
00068             return QLatin1String("dialog-error");
00069             break;
00070         case KTitleWidget::WarningMessage:
00071             return QLatin1String("dialog-warning");
00072             break;
00073         case KTitleWidget::PlainMessage:
00074             break;
00075     }
00076 
00077     return QString();
00078 }
00079 
00080 KTitleWidget::KTitleWidget(QWidget *parent)
00081   : QWidget(parent),
00082     d(new Private(this))
00083 {
00084     QFrame *titleFrame = new QFrame(this);
00085     titleFrame->setAutoFillBackground(true);
00086     titleFrame->setFrameShape(QFrame::StyledPanel);
00087     titleFrame->setFrameShadow(QFrame::Plain);
00088     titleFrame->setBackgroundRole(QPalette::Base);
00089 
00090     // default image / text part start
00091     d->headerLayout = new QGridLayout(titleFrame);
00092     d->headerLayout->setColumnStretch(0, 1);
00093     d->headerLayout->setMargin(6);
00094 
00095     d->textLabel = new QLabel(titleFrame);
00096     d->textLabel->setVisible(false);
00097     d->textLabel->setTextInteractionFlags(Qt::TextBrowserInteraction);
00098 
00099     d->imageLabel = new QLabel(titleFrame);
00100     d->imageLabel->setVisible(false);
00101 
00102     d->headerLayout->addWidget(d->textLabel, 0, 0);
00103     d->headerLayout->addWidget(d->imageLabel, 0, 1, 1, 2);
00104 
00105     d->commentLabel = new QLabel(titleFrame);
00106     d->commentLabel->setVisible(false);
00107     d->commentLabel->setOpenExternalLinks(true);
00108     d->commentLabel->setWordWrap(true);
00109     d->commentLabel->setTextInteractionFlags(Qt::TextBrowserInteraction);
00110     d->headerLayout->addWidget(d->commentLabel, 1, 0);
00111 
00112     // default image / text part end
00113 
00114     QVBoxLayout *mainLayout = new QVBoxLayout(this);
00115     mainLayout->addWidget(titleFrame);
00116     mainLayout->setMargin(0);
00117     setLayout(mainLayout);
00118 }
00119 
00120 KTitleWidget::~KTitleWidget()
00121 {
00122     delete d;
00123 }
00124 
00125 bool KTitleWidget::eventFilter(QObject *object, QEvent *event)
00126 {
00127     // Hide message label on click
00128     if (d->autoHideTimeout > 0 &&
00129         event->type() == QEvent::MouseButtonPress) {
00130         QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
00131         if (mouseEvent && mouseEvent->button() == Qt::LeftButton) {
00132             setVisible(false);
00133             return true;
00134         }
00135     }
00136 
00137     return QWidget::eventFilter(object, event);
00138 }
00139 
00140 void KTitleWidget::setWidget(QWidget *widget)
00141 {
00142     d->headerLayout->addWidget(widget, 2, 0, 1, 2);
00143 }
00144 
00145 QString KTitleWidget::text() const
00146 {
00147     return d->textLabel->text();
00148 }
00149 
00150 QString KTitleWidget::comment() const
00151 {
00152     return d->commentLabel->text();
00153 }
00154 
00155 const QPixmap *KTitleWidget::pixmap() const
00156 {
00157     return d->imageLabel->pixmap();
00158 }
00159 
00160 void KTitleWidget::setBuddy(QWidget *buddy)
00161 {
00162     d->textLabel->setBuddy(buddy);
00163 }
00164 
00165 void KTitleWidget::changeEvent(QEvent *e)
00166 {
00167     QWidget::changeEvent(e);
00168     if (e->type() == QEvent::PaletteChange) {
00169         d->textLabel->setStyleSheet(d->textLabel->styleSheet());
00170         d->commentLabel->setStyleSheet(d->commentLabel->styleSheet());
00171     }
00172 }
00173 
00174 void KTitleWidget::setText(const QString &text, Qt::Alignment alignment)
00175 {
00176     d->textLabel->setVisible(!text.isNull());
00177 
00178     if (!Qt::mightBeRichText(text)) {
00179         d->textLabel->setStyleSheet("QLabel { font-weight: bold; }");
00180     }
00181 
00182     d->textLabel->setText(text);
00183     d->textLabel->setAlignment(alignment);
00184     show();
00185 }
00186 
00187 void KTitleWidget::setText(const QString &text, MessageType type)
00188 {
00189     setText(text);
00190     setPixmap(type);
00191 }
00192 
00193 void KTitleWidget::setComment(const QString &comment, MessageType type)
00194 {
00195     d->commentLabel->setVisible(!comment.isNull());
00196 
00197     QString styleSheet;
00198     switch (type) {
00199         //FIXME: we need the usability color styles to implement different
00200         //       yet palette appropriate colours for the different use cases!
00201         //       also .. should we include an icon here,
00202         //       perhaps using the imageLabel?
00203         case InfoMessage:
00204         case WarningMessage:
00205         case ErrorMessage:
00206             styleSheet = QString("QLabel { color: palette(highlighted-text); background: palette(highlight); }");
00207             break;
00208         case PlainMessage:
00209         default:
00210             break;
00211     }
00212 
00213     //TODO: should we override the current icon with the corresponding MessageType icon?
00214     d->commentLabel->setStyleSheet(styleSheet);
00215     d->commentLabel->setText(comment);
00216     show();
00217 }
00218 
00219 void KTitleWidget::setPixmap(const QPixmap &pixmap, ImageAlignment alignment)
00220 {
00221     d->imageLabel->setVisible(!pixmap.isNull());
00222 
00223     d->headerLayout->removeWidget(d->textLabel);
00224     d->headerLayout->removeWidget(d->commentLabel);
00225     d->headerLayout->removeWidget(d->imageLabel);
00226 
00227     if (alignment == ImageLeft) {
00228         // swap the text and image labels around
00229         d->headerLayout->addWidget(d->imageLabel, 0, 0, 2, 1);
00230         d->headerLayout->addWidget(d->textLabel, 0, 1);
00231         d->headerLayout->addWidget(d->commentLabel, 1, 1);
00232         d->headerLayout->setColumnStretch(0, 0);
00233         d->headerLayout->setColumnStretch(1, 1);
00234     } else {
00235         d->headerLayout->addWidget(d->textLabel, 0, 0);
00236         d->headerLayout->addWidget(d->commentLabel, 1, 0);
00237         d->headerLayout->addWidget(d->imageLabel, 0, 1, 2, 1);
00238         d->headerLayout->setColumnStretch(1, 0);
00239         d->headerLayout->setColumnStretch(0, 1);
00240     }
00241 
00242     d->imageLabel->setPixmap(pixmap);
00243 }
00244 
00245 
00246 void KTitleWidget::setPixmap(const QString &icon, ImageAlignment alignment)
00247 {
00248     setPixmap(KIcon(icon), alignment);
00249 }
00250 
00251 void KTitleWidget::setPixmap(const QIcon& icon, ImageAlignment alignment)
00252 {
00253     setPixmap(icon.pixmap(IconSize(KIconLoader::Dialog)), alignment);
00254 }
00255 
00256 void KTitleWidget::setPixmap(MessageType type, ImageAlignment alignment)
00257 {
00258     setPixmap(KIcon(d->iconTypeToIconName(type)), alignment);
00259 }
00260 
00261 int KTitleWidget::autoHideTimeout() const
00262 {
00263     return d->autoHideTimeout;
00264 }
00265 
00266 void KTitleWidget::setAutoHideTimeout(int msecs)
00267 {
00268     d->autoHideTimeout = msecs;
00269 
00270     if (msecs > 0) {
00271         installEventFilter(this);
00272     } else {
00273         removeEventFilter(this);
00274     }
00275 }
00276 
00277 void KTitleWidget::showEvent(QShowEvent *event)
00278 {
00279     Q_UNUSED(event)
00280     if (d->autoHideTimeout > 0) {
00281         QTimer::singleShot(d->autoHideTimeout, this, SLOT(_k_timeoutFinished()));
00282     }
00283 }
00284 
00285 #include "ktitlewidget.moc"

KDEUI

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

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.7
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal