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

Applets

notificationwidget.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   notificationwidget.cpp                                                *
00003  *                                                                         *
00004  *   Copyright (C) 2008 Dmitry Suzdalev <dimsuz@gmail.com>                 *
00005  *   Copyright (C) 2008 Rob Scheepmaker <r.scheepmaker@student.utwente.nl> *
00006  *   Copyright (C) 2008 Jason Stubbs <jasonbstubbs@gmail.com>              *
00007  *                                                                         *
00008  *   This program is free software; you can redistribute it and/or modify  *
00009  *   it under the terms of the GNU Library General Public License as       *
00010  *   published by the Free Software Foundation; either version 2 of the    *
00011  *   License, or (at your option) any later version.                       *
00012  *                                                                         *
00013  *   This program is distributed in the hope that it will be useful,       *
00014  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00015  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00016  *   GNU Library General Public License for more details.                  *
00017  *                                                                         *
00018  *   You should have received a copy of the GNU Library General Public     *
00019  *   License along with this library; if not, write to the                 *
00020  *   Free Software Foundation, Inc.,                                       *
00021  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA .        *
00022  ***************************************************************************/
00023 
00024 #include "notificationwidget.h"
00025 
00026 #include <QSignalMapper>
00027 
00028 #include <QtGui/QGraphicsLinearLayout>
00029 #include <QtGui/QTextDocument>
00030 #include <QtGui/QFontMetrics>
00031 #include <QtGui/QGraphicsSceneResizeEvent>
00032 #include <QtGui/QPainter>
00033 
00034 #include <KColorScheme>
00035 #include <KPushButton>
00036 #include <KGlobalSettings>
00037 #include <KIcon>
00038 #include <KLocalizedString>
00039 
00040 #include <plasma/extender.h>
00041 #include <plasma/extenderitem.h>
00042 #include <plasma/theme.h>
00043 #include <plasma/widgets/pushbutton.h>
00044 
00045 class NotificationWidgetPrivate
00046 {
00047 public:
00048     NotificationWidgetPrivate(NotificationWidget *q)
00049         : q(q),
00050           notification(0),
00051           destroyOnClose(true),
00052           body(new QGraphicsTextItem(q)),
00053           actionsWidget(0),
00054           signalMapper(new QSignalMapper(q))
00055     {
00056     }
00057 
00058     void setTextFields(const QString &applicationName, const QString &summary, const QString &message);
00059     void completeDetach();
00060     void updateActions();
00061     void updateNotification();
00062     void destroy();
00063 
00064     NotificationWidget *q;
00065 
00066     SystemTray::Notification *notification;
00067     bool destroyOnClose;
00068 
00069     QString message;
00070     QGraphicsTextItem *body;
00071     QGraphicsWidget *actionsWidget;
00072     QHash<QString, QString> actions;
00073     QStringList actionOrder;
00074 
00075     QSignalMapper *signalMapper;
00076 };
00077 
00078 NotificationWidget::NotificationWidget(SystemTray::Notification *notification, Plasma::ExtenderItem *extenderItem)
00079     : QGraphicsWidget(extenderItem),
00080       d(new NotificationWidgetPrivate(this))
00081 {
00082     setMinimumWidth(300);
00083     setPreferredWidth(400);
00084 
00085     Plasma::Theme *theme = Plasma::Theme::defaultTheme();
00086     d->body->setFont(theme->font(Plasma::Theme::DefaultFont));
00087     d->body->setDefaultTextColor(theme->color(Plasma::Theme::TextColor));
00088 
00089     QTextOption option = d->body->document()->defaultTextOption();
00090     option.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
00091     d->body->document()->setDefaultTextOption(option);
00092 
00093     if (notification) {
00094         d->notification = notification;
00095 
00096         connect(d->signalMapper, SIGNAL(mapped(const QString &)),
00097                 d->notification, SLOT(triggerAction(const QString &)));
00098         connect(notification, SIGNAL(changed()),
00099                 this, SLOT(updateNotification()));
00100         connect(notification, SIGNAL(destroyed()),
00101                 this, SLOT(destroy()));
00102 
00103         d->updateNotification();
00104     } else {
00105         d->setTextFields(extenderItem->config().readEntry("applicationName", ""),
00106                          extenderItem->config().readEntry("summary", ""),
00107                          extenderItem->config().readEntry("message", ""));
00108         qreal bodyHeight = d->body->boundingRect().height();
00109         setPreferredHeight(bodyHeight);
00110         extenderItem->showCloseButton();
00111     }
00112 }
00113 
00114 NotificationWidget::~NotificationWidget()
00115 {
00116     delete d;
00117 }
00118 
00119 void NotificationWidget::resizeEvent(QGraphicsSceneResizeEvent *event)
00120 {
00121     d->body->setTextWidth(event->newSize().width());
00122     d->body->setPos(0, 0);
00123     if (d->actionsWidget) {
00124         d->actionsWidget->setPos(event->newSize().width() - d->actionsWidget->size().width(),
00125                                  event->newSize().height() - d->actionsWidget->size().height());
00126     }
00127 }
00128 
00129 void NotificationWidgetPrivate::setTextFields(const QString &applicationName,
00130                                                 const QString &summary, const QString &message)
00131 {
00132     Plasma::ExtenderItem *extenderItem = dynamic_cast<Plasma::ExtenderItem*>(q->parentWidget());
00133 
00134     if (!summary.isEmpty()) {
00135         extenderItem->setTitle(summary);
00136     } else {
00137         extenderItem->setTitle(i18n("Notification from %1", applicationName));
00138     }
00139 
00140     body->setHtml(message);
00141 }
00142 
00143 void NotificationWidgetPrivate::completeDetach()
00144 {
00145     actions.clear();
00146     actionOrder.clear();
00147 
00148     delete actionsWidget;
00149     actionsWidget = 0;
00150 }
00151 
00152 void NotificationWidgetPrivate::updateActions()
00153 {
00154     if (actions.isEmpty() || actionsWidget) {
00155         return;
00156     }
00157 
00158     actionsWidget = new QGraphicsWidget(q);
00159     QGraphicsLinearLayout *layout = new QGraphicsLinearLayout(actionsWidget);
00160     layout->setOrientation(Qt::Horizontal);
00161     actionsWidget->setContentsMargins(0, 0, 0, 0);
00162 
00163     foreach (const QString &actionId, actionOrder) {
00164         Plasma::PushButton *button = new Plasma::PushButton(actionsWidget);
00165         QString &action = actions[actionId];
00166         button->setText(action);
00167         button->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
00168         //TODO: we need smaller buttons but I don't like this method of accomplishing it.
00169         button->setPreferredHeight(button->minimumHeight() - 6);
00170 
00171         q->connect(button, SIGNAL(clicked()), signalMapper, SLOT(map()));
00172         signalMapper->setMapping(button, actionId);
00173 
00174         layout->addItem(button);
00175     }
00176 
00177     layout->updateGeometry();
00178     actionsWidget->setPos(q->size().width() - actionsWidget->size().width(),
00179                           q->size().width() - actionsWidget->size().height());
00180 }
00181 
00182 void NotificationWidgetPrivate::updateNotification()
00183 {
00184     Plasma::ExtenderItem *extenderItem = dynamic_cast<Plasma::ExtenderItem*>(q->parentWidget());
00185 
00186     //store the notification
00187     extenderItem->config().writeEntry("applicationName", notification->applicationName());
00188     extenderItem->config().writeEntry("summary", notification->summary());
00189     extenderItem->config().writeEntry("message", notification->message());
00190 
00191     //set text fields and icon
00192     setTextFields(notification->applicationName(), notification->summary(), notification->message());
00193     extenderItem->setIcon(notification->applicationIcon());
00194 
00195     //set the actions provided
00196     actions = notification->actions();
00197     actionOrder = notification->actionOrder();
00198     updateActions();
00199 
00200     //set the correct size hint and display a close action if no actions are provided by the
00201     //notification
00202     qreal bodyHeight = body->boundingRect().height();
00203     if (actionsWidget) {
00204         extenderItem->hideCloseButton();
00205         q->setPreferredHeight(bodyHeight + actionsWidget->size().height());
00206     } else {
00207         extenderItem->showCloseButton();
00208         q->setPreferredHeight(bodyHeight);
00209     }
00210 }
00211 
00212 void NotificationWidgetPrivate::destroy()
00213 {
00214     Plasma::ExtenderItem *extenderItem = dynamic_cast<Plasma::ExtenderItem *>(q->parentItem());
00215 
00216     if (extenderItem->isDetached()) {
00217         completeDetach();
00218     } else {
00219         completeDetach();
00220         extenderItem->destroy();
00221     }
00222 
00223     notification = 0;
00224 }
00225 
00226 #include "notificationwidget.moc"

Applets

Skip menu "Applets"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members

API Reference

Skip menu "API Reference"
  • KWin
  •   KWin Libraries
  • Libraries
  •   libkworkspace
  •   libsolidcontrol
  •   libtaskmanager
  • Plasma
  •   Animators
  •   Applets
  •   Engines
  • Solid Modules
Generated for API Reference 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