Applets
notificationwidget.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
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
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
00187 extenderItem->config().writeEntry("applicationName", notification->applicationName());
00188 extenderItem->config().writeEntry("summary", notification->summary());
00189 extenderItem->config().writeEntry("message", notification->message());
00190
00191
00192 setTextFields(notification->applicationName(), notification->summary(), notification->message());
00193 extenderItem->setIcon(notification->applicationIcon());
00194
00195
00196 actions = notification->actions();
00197 actionOrder = notification->actionOrder();
00198 updateActions();
00199
00200
00201
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"