~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to plasma/generic/applets/notifications/ui/notificationstack.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *   notificationstack.cpp                                                *
 
3
 *   Copyright (C) 2010 Marco Martin <notmart@gmail.com>                   *
 
4
 *                                                                         *
 
5
 *   This program is free software; you can redistribute it and/or modify  *
 
6
 *   it under the terms of the GNU General Public License as published by  *
 
7
 *   the Free Software Foundation; either version 2 of the License, or     *
 
8
 *   (at your option) any later version.                                   *
 
9
 *                                                                         *
 
10
 *   This program is distributed in the hope that it will be useful,       *
 
11
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 
12
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 
13
 *   GNU General Public License for more details.                          *
 
14
 *                                                                         *
 
15
 *   You should have received a copy of the GNU General Public License     *
 
16
 *   along with this program; if not, write to the                         *
 
17
 *   Free Software Foundation, Inc.,                                       *
 
18
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA .        *
 
19
 ***************************************************************************/
 
20
 
 
21
#include "notificationstack.h"
 
22
#include "../core/notification.h"
 
23
#include "notificationwidget.h"
 
24
 
 
25
#include <QGraphicsLinearLayout>
 
26
#include <QTimer>
 
27
 
 
28
#include <KDebug>
 
29
#include <KGlobalSettings>
 
30
 
 
31
#include <Plasma/FrameSvg>
 
32
#include <Plasma/Dialog>
 
33
 
 
34
NotificationStack::NotificationStack(QGraphicsItem *parent)
 
35
   : QGraphicsWidget(parent),
 
36
     m_size(4),
 
37
     m_underMouse(false)
 
38
{
 
39
    m_mainLayout = new QGraphicsLinearLayout(Qt::Vertical, this);
 
40
    m_canDismissTimer = new QTimer(this);
 
41
    m_canDismissTimer->setSingleShot(true);
 
42
 
 
43
    m_delayedRemoveTimer = new QTimer(this);
 
44
    m_delayedRemoveTimer->setSingleShot(true);
 
45
    connect(m_delayedRemoveTimer, SIGNAL(timeout()), this, SLOT(popNotification()));
 
46
 
 
47
    setAcceptsHoverEvents(true);
 
48
}
 
49
 
 
50
NotificationStack::~NotificationStack()
 
51
{
 
52
}
 
53
 
 
54
void NotificationStack::addNotification(Notification *notification)
 
55
{
 
56
    m_canDismissTimer->start(1000);
 
57
    connect(notification, SIGNAL(notificationDestroyed(Notification *)), this, SLOT(removeNotification(Notification *)), Qt::UniqueConnection);
 
58
    connect(notification, SIGNAL(expired(Notification *)), this, SLOT(delayedRemoveNotification(Notification *)),  Qt::UniqueConnection);
 
59
    connect(notification, SIGNAL(changed(Notification *)), this, SLOT(notificationChanged(Notification *)), Qt::UniqueConnection);
 
60
 
 
61
    NotificationWidget *notificationWidget = new NotificationWidget(notification, this);
 
62
    notificationWidget->installEventFilter(this);
 
63
    notificationWidget->setAcceptsHoverEvents(this);
 
64
    connect(notificationWidget, SIGNAL(actionTriggered(Notification *)), this, SLOT(removeNotification(Notification *)));
 
65
 
 
66
    m_notificationWidgets[notification] = notificationWidget;
 
67
    m_notifications.append(notification);
 
68
 
 
69
    if (m_notifications.size() > 1) {
 
70
        notificationWidget->setCollapsed(true, false);
 
71
    } else {
 
72
        m_currentNotificationWidget = notificationWidget;
 
73
    }
 
74
 
 
75
    if (m_notifications.size() > m_size) {
 
76
        bool found = false;
 
77
 
 
78
        //try to kill the oldest notification of the same app
 
79
        foreach (Notification *notif, m_notifications) {
 
80
            if (notif->applicationName() == notification->applicationName()) {
 
81
                m_notificationWidgets[notif]->deleteLater();
 
82
                m_notificationWidgets.remove(notif);
 
83
                m_notifications.removeAll(notif);
 
84
                found = true;
 
85
                break;
 
86
            }
 
87
        }
 
88
        //or kill the oldest one
 
89
        if (!found) {
 
90
            Notification *notif = m_notifications.first();
 
91
            m_notificationWidgets[notif]->deleteLater();
 
92
            m_notificationWidgets.remove(notif);
 
93
            m_notifications.pop_front();
 
94
        }
 
95
    }
 
96
 
 
97
    m_mainLayout->insertItem(0, notificationWidget);
 
98
    m_mainLayout->activate();
 
99
    updateGeometry();
 
100
    resize(size().width(), effectiveSizeHint(Qt::MinimumSize).height());
 
101
    emit updateRequested();
 
102
}
 
103
 
 
104
void NotificationStack::notificationChanged(Notification *notification)
 
105
{
 
106
    //if it was gone away put in on the stack again
 
107
    if (!m_notificationWidgets.contains(notification)) {
 
108
        addNotification(notification);
 
109
    }
 
110
    emit showRequested();
 
111
}
 
112
 
 
113
void NotificationStack::removeNotification(Notification *notification)
 
114
{
 
115
    NotificationWidget *nw = m_notificationWidgets.value(notification);
 
116
    if (nw) {
 
117
        nw->deleteLater();
 
118
    }
 
119
    m_mainLayout->removeItem(nw);
 
120
    m_notificationWidgets.remove(notification);
 
121
    m_notifications.removeAll(notification);
 
122
 
 
123
    if (m_notifications.count() > 0) {
 
124
        setCurrentNotification(m_notifications.first());
 
125
    }
 
126
 
 
127
    if (m_notifications.count() == 0) {
 
128
        emit stackEmpty();
 
129
    }
 
130
 
 
131
    updateGeometry();
 
132
    resize(size().width(), sizeHint(Qt::MinimumSize, QSizeF()).height());
 
133
    emit updateRequested();
 
134
}
 
135
 
 
136
void NotificationStack::delayedRemoveNotification(Notification *notification)
 
137
{
 
138
    m_notificationsToRemove.append(notification);
 
139
    if (!m_underMouse) {
 
140
        m_delayedRemoveTimer->start(1000);
 
141
    }
 
142
}
 
143
 
 
144
void NotificationStack::setCurrentNotification(Notification *notification)
 
145
{
 
146
    if (m_notificationWidgets.contains(notification)) {
 
147
        if (m_currentNotificationWidget) {
 
148
            m_currentNotificationWidget.data()->setCollapsed(true);
 
149
        }
 
150
        m_currentNotificationWidget = m_notificationWidgets.value(notification);
 
151
        m_currentNotificationWidget.data()->setCollapsed(false);
 
152
    }
 
153
}
 
154
 
 
155
void NotificationStack::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
 
156
{
 
157
    Q_UNUSED(event)
 
158
 
 
159
    m_underMouse = true;
 
160
    m_delayedRemoveTimer->stop();
 
161
}
 
162
 
 
163
void NotificationStack::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
 
164
{
 
165
    Q_UNUSED(event)
 
166
 
 
167
    m_underMouse = false;
 
168
    m_delayedRemoveTimer->start(1000);
 
169
}
 
170
 
 
171
void NotificationStack::mousePressEvent(QGraphicsSceneMouseEvent *event)
 
172
{
 
173
    event->accept();
 
174
}
 
175
 
 
176
void NotificationStack::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
 
177
{
 
178
    Q_UNUSED(event)
 
179
 
 
180
    if (!m_canDismissTimer->isActive() &&
 
181
        QPointF(event->buttonDownScenePos(event->button()) - event->scenePos()).manhattanLength() < KGlobalSettings::dndEventDelay()) {
 
182
        emit hideRequested();
 
183
    }
 
184
}
 
185
 
 
186
NotificationWidget *NotificationStack::currentNotificationWidget() const
 
187
{
 
188
    if (m_currentNotificationWidget) {
 
189
        return m_currentNotificationWidget.data();
 
190
    } else {
 
191
        return 0;
 
192
    }
 
193
}
 
194
 
 
195
 
 
196
 
 
197
bool NotificationStack::eventFilter(QObject *watched, QEvent *event)
 
198
{
 
199
    NotificationWidget *nw = qobject_cast<NotificationWidget *>(watched);
 
200
 
 
201
    if (!nw) {
 
202
        return false;
 
203
    }
 
204
 
 
205
    if (event->type() == QEvent::GraphicsSceneHoverEnter) {
 
206
        if (m_currentNotificationWidget && m_currentNotificationWidget.data() == nw) {
 
207
            return false;
 
208
        } else if (m_currentNotificationWidget) {
 
209
            m_currentNotificationWidget.data()->setCollapsed(true);
 
210
        }
 
211
        nw->setCollapsed(false);
 
212
        m_currentNotificationWidget = nw;
 
213
        m_canDismissTimer->start(1000);
 
214
    } else if (event->type() == QEvent::GraphicsSceneMove) {
 
215
        emit updateRequested();
 
216
    }
 
217
 
 
218
 
 
219
    return false;
 
220
}
 
221
 
 
222
void NotificationStack::popNotification()
 
223
{
 
224
    if (m_notificationsToRemove.isEmpty()) {
 
225
        return;
 
226
    }
 
227
 
 
228
    Notification *notif = m_notificationsToRemove.first();
 
229
    removeNotification(notif);
 
230
    m_notificationsToRemove.pop_front();
 
231
    m_delayedRemoveTimer->start(1000);
 
232
}
 
233
 
 
234
 
 
235
#include "notificationstack.moc"