diff --git a/src/ktitlewidget.cpp b/src/ktitlewidget.cpp index 67fd503..2cbed4c 100644 --- a/src/ktitlewidget.cpp +++ b/src/ktitlewidget.cpp @@ -1,330 +1,335 @@ /* This file is part of the KDE libraries Copyright (C) 2007 Urs Wolfer Copyright (C) 2007 Michaƫl Larouche This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License version 2 as published by the Free Software Foundation. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "ktitlewidget.h" #include #include #include #include #include #include #include #include #include class Q_DECL_HIDDEN KTitleWidget::Private { public: Private(KTitleWidget *parent) : q(parent), autoHideTimeout(0), messageType(InfoMessage) { } QString textStyleSheet() const { qreal factor; switch (level) { case 1: factor = 1.80; break; case 2: factor = 1.30; break; case 3: factor = 1.20; break; case 4: factor = 1.10; break; default: factor = 1; } const int fontSize = qRound(QApplication::font().pointSize() * factor); return QStringLiteral("QLabel { font-size: %1pt; color: %2 }").arg(QString::number(fontSize), q->palette().color(QPalette::WindowText).name()); } QString commentStyleSheet() const { QString styleSheet; switch (messageType) { //FIXME: we need the usability color styles to implement different // yet palette appropriate colours for the different use cases! // also .. should we include an icon here, // perhaps using the imageLabel? case InfoMessage: case WarningMessage: case ErrorMessage: styleSheet = QStringLiteral("QLabel { color: palette(%1); background: palette(%2); }").arg(q->palette().color(QPalette::HighlightedText).name(), q->palette().color(QPalette::Highlight).name()); break; case PlainMessage: default: break; } return styleSheet; } int level = 1; KTitleWidget *q; QGridLayout *headerLayout; QLabel *imageLabel; QLabel *textLabel; QLabel *commentLabel; int autoHideTimeout; MessageType messageType; /** * @brief Get the icon name from the icon type * @param type icon type from the enum * @return named icon as QString */ QString iconTypeToIconName(KTitleWidget::MessageType type); void _k_timeoutFinished() { q->setVisible(false); } }; QString KTitleWidget::Private::iconTypeToIconName(KTitleWidget::MessageType type) { switch (type) { case KTitleWidget::InfoMessage: return QStringLiteral("dialog-information"); case KTitleWidget::ErrorMessage: return QStringLiteral("dialog-error"); case KTitleWidget::WarningMessage: return QStringLiteral("dialog-warning"); case KTitleWidget::PlainMessage: break; } return QString(); } KTitleWidget::KTitleWidget(QWidget *parent) : QWidget(parent), d(new Private(this)) { QFrame *titleFrame = new QFrame(this); titleFrame->setAutoFillBackground(true); titleFrame->setFrameShape(QFrame::StyledPanel); titleFrame->setFrameShadow(QFrame::Plain); titleFrame->setBackgroundRole(QPalette::Base); titleFrame->setContentsMargins(0, 0, 0, 0); // default image / text part start d->headerLayout = new QGridLayout(titleFrame); d->headerLayout->setColumnStretch(0, 1); d->headerLayout->setContentsMargins(0, 0, 0, 0); d->textLabel = new QLabel(titleFrame); d->textLabel->setVisible(false); d->textLabel->setTextInteractionFlags(Qt::TextSelectableByMouse | Qt::LinksAccessibleByMouse); d->imageLabel = new QLabel(titleFrame); d->imageLabel->setVisible(false); d->headerLayout->addWidget(d->textLabel, 0, 0); d->headerLayout->addWidget(d->imageLabel, 0, 1, 1, 2); d->commentLabel = new QLabel(titleFrame); d->commentLabel->setVisible(false); d->commentLabel->setOpenExternalLinks(true); d->commentLabel->setWordWrap(true); d->commentLabel->setTextInteractionFlags(Qt::TextSelectableByMouse | Qt::LinksAccessibleByMouse); d->headerLayout->addWidget(d->commentLabel, 1, 0); // default image / text part end QVBoxLayout *mainLayout = new QVBoxLayout(this); mainLayout->addWidget(titleFrame); mainLayout->setContentsMargins(0, 0, 0, 0); setLayout(mainLayout); } KTitleWidget::~KTitleWidget() { delete d; } bool KTitleWidget::eventFilter(QObject *object, QEvent *event) { // Hide message label on click if (d->autoHideTimeout > 0 && event->type() == QEvent::MouseButtonPress) { QMouseEvent *mouseEvent = static_cast(event); if (mouseEvent && mouseEvent->button() == Qt::LeftButton) { setVisible(false); return true; } } return QWidget::eventFilter(object, event); } void KTitleWidget::setWidget(QWidget *widget) { d->headerLayout->addWidget(widget, 2, 0, 1, 2); } QString KTitleWidget::text() const { return d->textLabel->text(); } QString KTitleWidget::comment() const { return d->commentLabel->text(); } const QPixmap *KTitleWidget::pixmap() const { return d->imageLabel->pixmap(); } void KTitleWidget::setBuddy(QWidget *buddy) { d->textLabel->setBuddy(buddy); } void KTitleWidget::changeEvent(QEvent *e) { QWidget::changeEvent(e); if (e->type() == QEvent::PaletteChange || e->type() == QEvent::FontChange || e->type() == QEvent::ApplicationFontChange) { d->textLabel->setStyleSheet(d->textStyleSheet()); d->commentLabel->setStyleSheet(d->commentStyleSheet()); } } void KTitleWidget::setText(const QString &text, Qt::Alignment alignment) { d->textLabel->setVisible(!text.isNull()); if (!Qt::mightBeRichText(text)) { d->textLabel->setStyleSheet(d->textStyleSheet()); } d->textLabel->setText(text); d->textLabel->setAlignment(alignment); show(); } void KTitleWidget::setLevel(int level) { if (d->level == level) { return; } d->level = level; d->textLabel->setStyleSheet(d->textStyleSheet()); } int KTitleWidget::level() { return d->level; } void KTitleWidget::setText(const QString &text, MessageType type) { setPixmap(type); setText(text); } void KTitleWidget::setComment(const QString &comment, MessageType type) { d->commentLabel->setVisible(!comment.isNull()); //TODO: should we override the current icon with the corresponding MessageType icon? d->messageType = type; d->commentLabel->setStyleSheet(d->commentStyleSheet()); d->commentLabel->setText(comment); show(); } +void KTitleWidget::setIcon(const QIcon &icon, KTitleWidget::ImageAlignment alignment) +{ + setPixmap(icon.pixmap(style()->pixelMetric(QStyle::PM_MessageBoxIconSize)), alignment); +} + void KTitleWidget::setPixmap(const QPixmap &pixmap, ImageAlignment alignment) { d->imageLabel->setVisible(!pixmap.isNull()); d->headerLayout->removeWidget(d->textLabel); d->headerLayout->removeWidget(d->commentLabel); d->headerLayout->removeWidget(d->imageLabel); if (alignment == ImageLeft) { // swap the text and image labels around d->headerLayout->addWidget(d->imageLabel, 0, 0, 2, 1); d->headerLayout->addWidget(d->textLabel, 0, 1); d->headerLayout->addWidget(d->commentLabel, 1, 1); d->headerLayout->setColumnStretch(0, 0); d->headerLayout->setColumnStretch(1, 1); } else { d->headerLayout->addWidget(d->textLabel, 0, 0); d->headerLayout->addWidget(d->commentLabel, 1, 0); d->headerLayout->addWidget(d->imageLabel, 0, 1, 2, 1); d->headerLayout->setColumnStretch(1, 0); d->headerLayout->setColumnStretch(0, 1); } d->imageLabel->setPixmap(pixmap); } void KTitleWidget::setPixmap(const QString &icon, ImageAlignment alignment) { - setPixmap(QIcon::fromTheme(icon), alignment); + setIcon(QIcon::fromTheme(icon), alignment); } void KTitleWidget::setPixmap(const QIcon &icon, ImageAlignment alignment) { - setPixmap(icon.pixmap(style()->pixelMetric(QStyle::PM_MessageBoxIconSize)), alignment); + setIcon(icon, alignment); } void KTitleWidget::setPixmap(MessageType type, ImageAlignment alignment) { - setPixmap(QIcon::fromTheme(d->iconTypeToIconName(type)), alignment); + setIcon(QIcon::fromTheme(d->iconTypeToIconName(type)), alignment); } int KTitleWidget::autoHideTimeout() const { return d->autoHideTimeout; } void KTitleWidget::setAutoHideTimeout(int msecs) { d->autoHideTimeout = msecs; if (msecs > 0) { installEventFilter(this); } else { removeEventFilter(this); } } void KTitleWidget::showEvent(QShowEvent *event) { Q_UNUSED(event) if (d->autoHideTimeout > 0) { QTimer::singleShot(d->autoHideTimeout, this, [this] { d->_k_timeoutFinished(); }); } } #include "moc_ktitlewidget.cpp" diff --git a/src/ktitlewidget.h b/src/ktitlewidget.h index 961eadb..f5d2fa7 100644 --- a/src/ktitlewidget.h +++ b/src/ktitlewidget.h @@ -1,228 +1,238 @@ /* This file is part of the KDE libraries Copyright (C) 2007-2009 Urs Wolfer This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License version 2 as published by the Free Software Foundation. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifndef KTITLEWIDGET_H #define KTITLEWIDGET_H #include #include /** * @class KTitleWidget ktitlewidget.h KTitleWidget * * @short Standard title widget. * * This class provides a widget often used for dialog titles. * \image html ktitlewidget.png "KTitleWidget with title and icon" * * KTitleWidget uses the general application font at 1.4 times its size to * style the text. This is a visual change from 4.x. * * @section Usage * KTitleWidget is very simple to use. You can either use its default text * (and pixmap) properties or display your own widgets in the title widget. * * A title text with a right-aligned pixmap: * @code KTitleWidget *titleWidget = new KTitleWidget(this); titleWidget->setText(i18n("Title")); titleWidget->setPixmap(QIcon::fromTheme("screen").pixmap(22, 22)); * @endcode * * Use it with an own widget: * @code KTitleWidget *checkboxTitleWidget = new KTitleWidget(this); QWidget *checkBoxTitleMainWidget = new QWidget(this); QVBoxLayout *titleLayout = new QVBoxLayout(checkBoxTitleMainWidget); titleLayout->setContentsMargins(6, 6, 6, 6); QCheckBox *checkBox = new QCheckBox("Text Checkbox", checkBoxTitleMainWidget); titleLayout->addWidget(checkBox); checkboxTitleWidget->setWidget(checkBoxTitleMainWidget); * @endcode * * @see KPageView * @author Urs Wolfer \ */ class KWIDGETSADDONS_EXPORT KTitleWidget : public QWidget { Q_OBJECT Q_PROPERTY(QString text READ text WRITE setText) Q_PROPERTY(QString comment READ comment WRITE setComment) Q_PROPERTY(QPixmap pixmap READ pixmap WRITE setPixmap) Q_PROPERTY(int autoHideTimeout READ autoHideTimeout WRITE setAutoHideTimeout) public: /** * Possible title pixmap alignments. * * @li ImageLeft: Display the pixmap left * @li ImageRight: Display the pixmap right (default) */ enum ImageAlignment { ImageLeft, /**< Display the pixmap on the left */ ImageRight /**< Display the pixmap on the right */ }; Q_ENUM(ImageAlignment) /** * Comment message types */ enum MessageType { PlainMessage, /**< Normal comment */ InfoMessage, /**< Information the user should be alerted to */ WarningMessage, /**< A warning the user should be alerted to */ ErrorMessage /**< An error message */ }; /** * Constructs a title widget. */ explicit KTitleWidget(QWidget *parent = nullptr); ~KTitleWidget() override; /** * @param widget Widget displayed on the title widget. */ void setWidget(QWidget *widget); /** * @return the text displayed in the title * @see setText() */ QString text() const; /** * @return the text displayed in the comment below the title, if any * @see setComment() */ QString comment() const; /** * @return the pixmap displayed in the title * @see setPixmap() */ const QPixmap *pixmap() const; /** * Sets this label's buddy to buddy. * When the user presses the shortcut key indicated by the label in this * title widget, the keyboard focus is transferred to the label's buddy * widget. * @param buddy the widget to activate when the shortcut key is activated */ void setBuddy(QWidget *buddy); /** * Get the current timeout value in milliseconds * @return timeout value in msecs */ int autoHideTimeout() const; /** * @return The level of this title: it influences the font size following the guidelines at * https://www.my-scratch.de/HIG/style/typography.html * It also corresponds to the level api of Kirigami Heading for QML applications * @since 5.53 */ int level(); public Q_SLOTS: /** * @param text Text displayed on the label. It can either be plain text or rich text. If it * is plain text, the text is displayed as a bold title text. * @param alignment Alignment of the text. Default is left and vertical centered. * @see text() */ void setText(const QString &text, Qt::Alignment alignment = Qt::AlignLeft | Qt::AlignVCenter); /** * @param text Text displayed on the label. It can either be plain text or rich text. If it * is plain text, the text is displayed as a bold title text. * @param type The sort of message it is; will also set the icon accordingly * @see text() */ void setText(const QString &text, MessageType type); /** * @param comment Text displayed beneath the main title as a comment. * It can either be plain text or rich text. * @param type The sort of message it is. * @see comment() */ void setComment(const QString &comment, MessageType type = PlainMessage); + /** + * Set the icon to display in the header. + * @param icon the icon to display in the header. + * @param alignment alignment of the icon (default is right aligned). + * @since 5.63 + */ + void setIcon(const QIcon &icon, ImageAlignment alignment = ImageRight); + /** * @param pixmap Pixmap displayed in the header. The pixmap is by default right, but * @param alignment can be used to display it also left. * @see pixmap() */ void setPixmap(const QPixmap &pixmap, ImageAlignment alignment = ImageRight); /** * @param icon name of the icon to display in the header. The pixmap is by default right, but * @param alignment can be used to display it also left. * @see pixmap() + * @deprecated since 5.63 use setIcon() instead */ - void setPixmap(const QString &icon, ImageAlignment alignment = ImageRight); + KWIDGETSADDONS_DEPRECATED void setPixmap(const QString &icon, ImageAlignment alignment = ImageRight); /** * @param icon the icon to display in the header. The pixmap is by default right, but * @param alignment can be used to display it also left. * @see pixmap() + * @deprecated since 5.63 use setIcon() instead */ - void setPixmap(const QIcon &icon, ImageAlignment alignment = ImageRight); + KWIDGETSADDONS_DEPRECATED void setPixmap(const QIcon &icon, ImageAlignment alignment = ImageRight); /** * @param type the type of message icon to display in the header. The pixmap is by default right, but * @param alignment can be used to display it also left. * @see pixmap() */ void setPixmap(MessageType type, ImageAlignment alignment = ImageRight); /** * Set the autohide timeout of the label * Set value to 0 to disable autohide, which is the default. * @param msecs timeout value in milliseconds */ void setAutoHideTimeout(int msecs); /** * Sets the level of this title, similar to HTML's h1 h2 h3... * follows KDE HIG https://www.my-scratch.de/HIG/style/typography.html * @param level the level of the title, 1 is the biggest font and most important, descending * @since 5.53 */ void setLevel(int level); protected: void changeEvent(QEvent *e) override; void showEvent(QShowEvent *event) override; bool eventFilter(QObject *object, QEvent *event) override; private: class Private; Private *const d; Q_DISABLE_COPY(KTitleWidget) }; #endif