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

« back to all changes in this revision

Viewing changes to plasma/generic/applets/systemtray/core/task.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
 *   task.cpp                                                              *
 
3
 *                                                                         *
 
4
 *   Copyright (C) 2008 Jason Stubbs <jasonbstubbs@gmail.com>              *
 
5
 *                                                                         *
 
6
 *   This program is free software; you can redistribute it and/or modify  *
 
7
 *   it under the terms of the GNU General Public License as published by  *
 
8
 *   the Free Software Foundation; either version 2 of the License, or     *
 
9
 *   (at your option) any later version.                                   *
 
10
 *                                                                         *
 
11
 *   This program is distributed in the hope that it will be useful,       *
 
12
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 
13
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 
14
 *   GNU General Public License for more details.                          *
 
15
 *                                                                         *
 
16
 *   You should have received a copy of the GNU General Public License     *
 
17
 *   along with this program; if not, write to the                         *
 
18
 *   Free Software Foundation, Inc.,                                       *
 
19
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA .        *
 
20
 ***************************************************************************/
 
21
 
 
22
#include "task.h"
 
23
 
 
24
#include <QtGui/QGraphicsWidget>
 
25
 
 
26
 
 
27
namespace SystemTray
 
28
{
 
29
 
 
30
 
 
31
class Task::Private
 
32
{
 
33
public:
 
34
    Private()
 
35
        : hiddenState(Task::NotHidden),
 
36
          order(Task::Normal),
 
37
          status(Task::UnknownStatus),
 
38
          category(Task::UnknownCategory)
 
39
    {
 
40
    }
 
41
 
 
42
    QHash<Plasma::Applet *, QGraphicsWidget *> widgetsByHost;
 
43
    Task::HideStates hiddenState;
 
44
    Task::Order order;
 
45
    Task::Status status;
 
46
    Task::Category category;
 
47
};
 
48
 
 
49
 
 
50
Task::Task(QObject *parent)
 
51
    : QObject(parent),
 
52
      d(new Private)
 
53
{
 
54
}
 
55
 
 
56
Task::~Task()
 
57
{
 
58
    emit destroyed(this);
 
59
    foreach (QGraphicsWidget * widget, d->widgetsByHost) {
 
60
        disconnect(widget, 0, this, 0);
 
61
        // sometimes it appears that the widget will get scheduled for a repaint
 
62
        // then it gets deleted here and QGraphicsScene doesn't get that straight
 
63
        // in its bookkeeping and crashes occur; work around this by giving it a
 
64
        // chance to schedule after the next paintfun
 
65
        widget->deleteLater();
 
66
    }
 
67
    delete d;
 
68
}
 
69
 
 
70
QGraphicsWidget *Task::widget(Plasma::Applet *host, bool createIfNecessary)
 
71
{
 
72
    Q_ASSERT(host);
 
73
 
 
74
    QGraphicsWidget *widget = d->widgetsByHost.value(host);
 
75
 
 
76
    if (!widget && createIfNecessary) {
 
77
        widget = createWidget(host);
 
78
 
 
79
        if (widget) {
 
80
            d->widgetsByHost.insert(host, widget);
 
81
            connect(widget, SIGNAL(destroyed()), this, SLOT(widgetDeleted()));
 
82
        }
 
83
    }
 
84
 
 
85
    return widget;
 
86
}
 
87
 
 
88
bool Task::isEmbeddable(Plasma::Applet *host)
 
89
{
 
90
    if (!host) {
 
91
        return false;
 
92
    }
 
93
 
 
94
    return d->widgetsByHost.value(host) || isEmbeddable();
 
95
}
 
96
 
 
97
QHash<Plasma::Applet *, QGraphicsWidget *> Task::widgetsByHost() const
 
98
{
 
99
    return d->widgetsByHost;
 
100
}
 
101
 
 
102
void Task::abandon(Plasma::Applet *host)
 
103
{
 
104
    QGraphicsWidget *widget = d->widgetsByHost.value(host);
 
105
    if (widget) {
 
106
        widget->deleteLater();
 
107
    }
 
108
}
 
109
 
 
110
QGraphicsWidget *Task::forget(Plasma::Applet *host)
 
111
{
 
112
    return d->widgetsByHost.take(host);
 
113
}
 
114
 
 
115
void Task::widgetDeleted()
 
116
{
 
117
    bool wasEmbeddable = isEmbeddable();
 
118
 
 
119
    QGraphicsWidget * w = static_cast<QGraphicsWidget*>(sender());
 
120
    QMutableHashIterator<Plasma::Applet *, QGraphicsWidget *> it(d->widgetsByHost);
 
121
    while (it.hasNext()) {
 
122
        it.next();
 
123
        if (it.value() == w) {
 
124
            it.remove();
 
125
        }
 
126
    }
 
127
 
 
128
    if (!wasEmbeddable && isEmbeddable()) {
 
129
        emit changed(this);
 
130
    }
 
131
}
 
132
 
 
133
void Task::setHidden(HideStates state)
 
134
{
 
135
    d->hiddenState = state;
 
136
}
 
137
 
 
138
Task::HideStates Task::hidden() const
 
139
{
 
140
    return d->hiddenState;
 
141
}
 
142
 
 
143
bool Task::isUsed() const
 
144
{
 
145
    return !d->widgetsByHost.isEmpty();
 
146
}
 
147
 
 
148
Task::Order Task::order() const
 
149
{
 
150
    return d->order;
 
151
}
 
152
 
 
153
void Task::setOrder(Order order)
 
154
{
 
155
    d->order = order;
 
156
}
 
157
 
 
158
void Task::setCategory(Category category)
 
159
{
 
160
    if (d->category == category) {
 
161
        return;
 
162
    }
 
163
 
 
164
    d->category = category;
 
165
    emit changed(this);
 
166
}
 
167
 
 
168
Task::Category Task::category() const
 
169
{
 
170
    return d->category;
 
171
}
 
172
 
 
173
void Task::setStatus(Status status)
 
174
{
 
175
    if (d->status == status) {
 
176
        return;
 
177
    }
 
178
 
 
179
    d->status = status;
 
180
    resetHiddenStatus();
 
181
    emit changed(this);
 
182
}
 
183
 
 
184
Task::Status Task::status() const
 
185
{
 
186
    return d->status;
 
187
}
 
188
 
 
189
void Task::resetHiddenStatus()
 
190
{
 
191
     if (d->status == NeedsAttention) {
 
192
        //tasks don't get moved anymore
 
193
        setOrder(Normal);
 
194
        if (hidden() & AutoHidden) {
 
195
            setHidden(hidden() ^ AutoHidden);
 
196
        }
 
197
    } else {
 
198
        if (d->status == Active && (hidden() & AutoHidden)) {
 
199
            setHidden(hidden() ^ AutoHidden);
 
200
        } else if (d->status == Passive) {
 
201
            setHidden(hidden() | AutoHidden);
 
202
        }
 
203
 
 
204
        setOrder(Normal);
 
205
    }
 
206
}
 
207
 
 
208
}
 
209
 
 
210
#include "task.moc"