task.cpp 4.88 KB
Newer Older
Aaron J. Seigo's avatar
Aaron J. Seigo committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/***************************************************************************
 *   task.cpp                                                              *
 *                                                                         *
 *   Copyright (C) 2008 Jason Stubbs <jasonbstubbs@gmail.com>              *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program 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 General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA .        *
 ***************************************************************************/

#include "task.h"

#include <QtGui/QGraphicsWidget>


namespace SystemTray
{


class Task::Private
{
public:
34
35
36
37
38
39
40
41
    Private()
        : hiddenState(Task::NotHidden),
          order(Task::Normal),
          status(Task::UnknownStatus),
          category(Task::UnknownCategory)
    {
    }

42
    QHash<Plasma::Applet *, QGraphicsWidget *> widgetsByHost;
43
    Task::HideStates hiddenState;
44
45
46
    Task::Order order;
    Task::Status status;
    Task::Category category;
Aaron J. Seigo's avatar
Aaron J. Seigo committed
47
48
49
};


50
51
52
Task::Task(QObject *parent)
    : QObject(parent),
      d(new Private)
Aaron J. Seigo's avatar
Aaron J. Seigo committed
53
54
55
56
57
58
{
}

Task::~Task()
{
    emit destroyed(this);
59
60
    foreach (QGraphicsWidget * widget, d->widgetsByHost) {
        disconnect(widget, 0, this, 0);
61
62
63
64
65
        // sometimes it appears that the widget will get scheduled for a repaint
        // then it gets deleted here and QGraphicsScene doesn't get that straight
        // in its bookkeeping and crashes occur; work around this by giving it a
        // chance to schedule after the next paintfun
        widget->deleteLater();
66
    }
Aaron J. Seigo's avatar
Aaron J. Seigo committed
67
68
69
70
    delete d;
}


71
QGraphicsWidget* Task::widget(Plasma::Applet *host, bool createIfNecessary)
Aaron J. Seigo's avatar
Aaron J. Seigo committed
72
73
74
{
    Q_ASSERT(host);

75
76
77
78
79
80
81
82
83
84
    QGraphicsWidget *widget = d->widgetsByHost.value(host);

    if (!widget && createIfNecessary) {
        widget = createWidget(host);

        if (widget) {
            d->widgetsByHost.insert(host, widget);
            connect(widget, SIGNAL(destroyed()), this, SLOT(widgetDeleted()));
        }
    }
Aaron J. Seigo's avatar
Aaron J. Seigo committed
85
86
87
88

    return widget;
}

89
90
91
92
93
94
95
96
97
bool Task::isEmbeddable(Plasma::Applet *host)
{
    if (!host) {
        return false;
    }

    return d->widgetsByHost.value(host) || isEmbeddable();
}

98
99
100
101
102
QHash<Plasma::Applet *, QGraphicsWidget *> Task::widgetsByHost() const
{
    return d->widgetsByHost;
}

Aaron J. Seigo's avatar
Aaron J. Seigo committed
103
104
105
void Task::widgetDeleted()
{
    bool wasEmbeddable = isEmbeddable();
106
107
108
109
110
111
112
113
114
115

    QGraphicsWidget * w = static_cast<QGraphicsWidget*>(sender());
    QMutableHashIterator<Plasma::Applet *, QGraphicsWidget *> it(d->widgetsByHost);
    while (it.hasNext()) {
        it.next();
        if (it.value() == w) {
            it.remove();
        }
    }

Aaron J. Seigo's avatar
Aaron J. Seigo committed
116
117
118
119
120
    if (!wasEmbeddable && isEmbeddable()) {
        emit changed(this);
    }
}

121
122
123
124
125
bool Task::isHideable() const
{
    return true;
}

126
127
128
129
130
131
132
133
134
135
void Task::setHidden(HideStates state)
{
    d->hiddenState = state;
}

Task::HideStates Task::hidden() const
{
    return d->hiddenState;
}

136
137
138
139
140
bool Task::isUsed() const
{
    return !d->widgetsByHost.isEmpty();
}

141
142
143
144
145
146
147
148
149
Task::Order Task::order() const
{
    return d->order;
}

void Task::setOrder(Order order)
{
    d->order = order;
}
Aaron J. Seigo's avatar
Aaron J. Seigo committed
150

151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
void Task::setCategory(Category category)
{
    if (d->category == category) {
        return;
    }

    d->category = category;
    emit changed(this);
}

Task::Category Task::category() const
{
    return d->category;
}

void Task::setStatus(Status status)
{
    if (d->status == status) {
        return;
    }
171

172
    d->status = status;
173
174
175
176
177
178
179
180
    resetHiddenStatus();
    emit changed(this);
}

Task::Status Task::status() const
{
    return d->status;
}
181

182
183
void Task::resetHiddenStatus()
{
184
185
186
187
188
189
190
191
192
193
194
     if (d->status == NeedsAttention) {
        setOrder(First);
        if (hidden() & AutoHidden) {
            setHidden(hidden() ^ AutoHidden);
        }
    } else {
        if (d->status == Active && (hidden() & AutoHidden)) {
            setHidden(hidden() ^ AutoHidden);
        } else if (d->status == Passive) {
            setHidden(hidden() | AutoHidden);
        }
195

196
197
        setOrder(Normal);
    }
198
199
}

Aaron J. Seigo's avatar
Aaron J. Seigo committed
200
201
202
203
}


#include "task.moc"