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 messageType(InfoMessage) 00039 { 00040 } 00041 00042 QString textStyleSheet() const 00043 { 00044 return QString("QLabel { font-weight: bold; color: %1}").arg(q->palette().color(QPalette::WindowText).name()); 00045 } 00046 00047 QString commentStyleSheet() const 00048 { 00049 QString styleSheet; 00050 switch (messageType) { 00051 //FIXME: we need the usability color styles to implement different 00052 // yet palette appropriate colours for the different use cases! 00053 // also .. should we include an icon here, 00054 // perhaps using the imageLabel? 00055 case InfoMessage: 00056 case WarningMessage: 00057 case ErrorMessage: 00058 styleSheet = QString("QLabel { color: palette(%1); background: palette(%2); }").arg(q->palette().color(QPalette::HighlightedText).name()).arg(q->palette().color(QPalette::Highlight).name()); 00059 break; 00060 case PlainMessage: 00061 default: 00062 break; 00063 } 00064 return styleSheet; 00065 } 00066 00067 KTitleWidget* q; 00068 QGridLayout *headerLayout; 00069 QLabel *imageLabel; 00070 QLabel *textLabel; 00071 QLabel *commentLabel; 00072 int autoHideTimeout; 00073 MessageType messageType; 00074 00080 QString iconTypeToIconName(KTitleWidget::MessageType type); 00081 00082 void _k_timeoutFinished() 00083 { 00084 q->setVisible(false); 00085 } 00086 }; 00087 00088 QString KTitleWidget::Private::iconTypeToIconName(KTitleWidget::MessageType type) 00089 { 00090 switch (type) { 00091 case KTitleWidget::InfoMessage: 00092 return QLatin1String("dialog-information"); 00093 break; 00094 case KTitleWidget::ErrorMessage: 00095 return QLatin1String("dialog-error"); 00096 break; 00097 case KTitleWidget::WarningMessage: 00098 return QLatin1String("dialog-warning"); 00099 break; 00100 case KTitleWidget::PlainMessage: 00101 break; 00102 } 00103 00104 return QString(); 00105 } 00106 00107 KTitleWidget::KTitleWidget(QWidget *parent) 00108 : QWidget(parent), 00109 d(new Private(this)) 00110 { 00111 QFrame *titleFrame = new QFrame(this); 00112 titleFrame->setAutoFillBackground(true); 00113 titleFrame->setFrameShape(QFrame::StyledPanel); 00114 titleFrame->setFrameShadow(QFrame::Plain); 00115 titleFrame->setBackgroundRole(QPalette::Base); 00116 00117 // default image / text part start 00118 d->headerLayout = new QGridLayout(titleFrame); 00119 d->headerLayout->setColumnStretch(0, 1); 00120 d->headerLayout->setMargin(6); 00121 00122 d->textLabel = new QLabel(titleFrame); 00123 d->textLabel->setVisible(false); 00124 d->textLabel->setTextInteractionFlags(Qt::TextSelectableByMouse | Qt::LinksAccessibleByMouse); 00125 00126 d->imageLabel = new QLabel(titleFrame); 00127 d->imageLabel->setVisible(false); 00128 00129 d->headerLayout->addWidget(d->textLabel, 0, 0); 00130 d->headerLayout->addWidget(d->imageLabel, 0, 1, 1, 2); 00131 00132 d->commentLabel = new QLabel(titleFrame); 00133 d->commentLabel->setVisible(false); 00134 d->commentLabel->setOpenExternalLinks(true); 00135 d->commentLabel->setWordWrap(true); 00136 d->commentLabel->setTextInteractionFlags(Qt::TextSelectableByMouse | Qt::LinksAccessibleByMouse); 00137 d->headerLayout->addWidget(d->commentLabel, 1, 0); 00138 00139 // default image / text part end 00140 00141 QVBoxLayout *mainLayout = new QVBoxLayout(this); 00142 mainLayout->addWidget(titleFrame); 00143 mainLayout->setMargin(0); 00144 setLayout(mainLayout); 00145 } 00146 00147 KTitleWidget::~KTitleWidget() 00148 { 00149 delete d; 00150 } 00151 00152 bool KTitleWidget::eventFilter(QObject *object, QEvent *event) 00153 { 00154 // Hide message label on click 00155 if (d->autoHideTimeout > 0 && 00156 event->type() == QEvent::MouseButtonPress) { 00157 QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event); 00158 if (mouseEvent && mouseEvent->button() == Qt::LeftButton) { 00159 setVisible(false); 00160 return true; 00161 } 00162 } 00163 00164 return QWidget::eventFilter(object, event); 00165 } 00166 00167 void KTitleWidget::setWidget(QWidget *widget) 00168 { 00169 d->headerLayout->addWidget(widget, 2, 0, 1, 2); 00170 } 00171 00172 QString KTitleWidget::text() const 00173 { 00174 return d->textLabel->text(); 00175 } 00176 00177 QString KTitleWidget::comment() const 00178 { 00179 return d->commentLabel->text(); 00180 } 00181 00182 const QPixmap *KTitleWidget::pixmap() const 00183 { 00184 return d->imageLabel->pixmap(); 00185 } 00186 00187 void KTitleWidget::setBuddy(QWidget *buddy) 00188 { 00189 d->textLabel->setBuddy(buddy); 00190 } 00191 00192 void KTitleWidget::changeEvent(QEvent *e) 00193 { 00194 QWidget::changeEvent(e); 00195 if (e->type() == QEvent::PaletteChange) { 00196 d->textLabel->setStyleSheet(d->textStyleSheet()); 00197 d->commentLabel->setStyleSheet(d->commentStyleSheet()); 00198 } 00199 } 00200 00201 void KTitleWidget::setText(const QString &text, Qt::Alignment alignment) 00202 { 00203 d->textLabel->setVisible(!text.isNull()); 00204 00205 if (!Qt::mightBeRichText(text)) { 00206 d->textLabel->setStyleSheet(d->textStyleSheet()); 00207 } 00208 00209 d->textLabel->setText(text); 00210 d->textLabel->setAlignment(alignment); 00211 show(); 00212 } 00213 00214 void KTitleWidget::setText(const QString &text, MessageType type) 00215 { 00216 setPixmap(type); 00217 setText(text); 00218 } 00219 00220 void KTitleWidget::setComment(const QString &comment, MessageType type) 00221 { 00222 d->commentLabel->setVisible(!comment.isNull()); 00223 00224 //TODO: should we override the current icon with the corresponding MessageType icon? 00225 d->messageType = type; 00226 d->commentLabel->setStyleSheet(d->commentStyleSheet()); 00227 d->commentLabel->setText(comment); 00228 show(); 00229 } 00230 00231 void KTitleWidget::setPixmap(const QPixmap &pixmap, ImageAlignment alignment) 00232 { 00233 d->imageLabel->setVisible(!pixmap.isNull()); 00234 00235 d->headerLayout->removeWidget(d->textLabel); 00236 d->headerLayout->removeWidget(d->commentLabel); 00237 d->headerLayout->removeWidget(d->imageLabel); 00238 00239 if (alignment == ImageLeft) { 00240 // swap the text and image labels around 00241 d->headerLayout->addWidget(d->imageLabel, 0, 0, 2, 1); 00242 d->headerLayout->addWidget(d->textLabel, 0, 1); 00243 d->headerLayout->addWidget(d->commentLabel, 1, 1); 00244 d->headerLayout->setColumnStretch(0, 0); 00245 d->headerLayout->setColumnStretch(1, 1); 00246 } else { 00247 d->headerLayout->addWidget(d->textLabel, 0, 0); 00248 d->headerLayout->addWidget(d->commentLabel, 1, 0); 00249 d->headerLayout->addWidget(d->imageLabel, 0, 1, 2, 1); 00250 d->headerLayout->setColumnStretch(1, 0); 00251 d->headerLayout->setColumnStretch(0, 1); 00252 } 00253 00254 d->imageLabel->setPixmap(pixmap); 00255 } 00256 00257 00258 void KTitleWidget::setPixmap(const QString &icon, ImageAlignment alignment) 00259 { 00260 setPixmap(KIcon(icon), alignment); 00261 } 00262 00263 void KTitleWidget::setPixmap(const QIcon& icon, ImageAlignment alignment) 00264 { 00265 setPixmap(icon.pixmap(IconSize(KIconLoader::Dialog)), alignment); 00266 } 00267 00268 void KTitleWidget::setPixmap(MessageType type, ImageAlignment alignment) 00269 { 00270 setPixmap(KIcon(d->iconTypeToIconName(type)), alignment); 00271 } 00272 00273 int KTitleWidget::autoHideTimeout() const 00274 { 00275 return d->autoHideTimeout; 00276 } 00277 00278 void KTitleWidget::setAutoHideTimeout(int msecs) 00279 { 00280 d->autoHideTimeout = msecs; 00281 00282 if (msecs > 0) { 00283 installEventFilter(this); 00284 } else { 00285 removeEventFilter(this); 00286 } 00287 } 00288 00289 void KTitleWidget::showEvent(QShowEvent *event) 00290 { 00291 Q_UNUSED(event) 00292 if (d->autoHideTimeout > 0) { 00293 QTimer::singleShot(d->autoHideTimeout, this, SLOT(_k_timeoutFinished())); 00294 } 00295 } 00296 00297 #include "ktitlewidget.moc"
KDE 4.6 API Reference