~ubuntu-branches/ubuntu/gutsy/kdebase-workspace/gutsy

« back to all changes in this revision

Viewing changes to libs/taskmanager/tasklmbmenu.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2007-10-11 14:04:48 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20071011140448-v0eb7lxbb24zagca
Tags: 3.94.0-0ubuntu1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*****************************************************************
 
2
 
 
3
Copyright (c) 2001 Matthias Elter <elter@kde.org>
 
4
Copyright (c) 2002 John Firebaugh <jfirebaugh@kde.org>
 
5
 
 
6
Permission is hereby granted, free of charge, to any person obtaining a copy
 
7
of this software and associated documentation files (the "Software"), to deal
 
8
in the Software without restriction, including without limitation the rights
 
9
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 
10
copies of the Software, and to permit persons to whom the Software is
 
11
furnished to do so, subject to the following conditions:
 
12
 
 
13
The above copyright notice and this permission notice shall be included in
 
14
all copies or substantial portions of the Software.
 
15
 
 
16
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
17
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
18
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
 
19
AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
 
20
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 
21
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
22
 
 
23
******************************************************************/
 
24
 
 
25
#include "tasklmbmenu.h"
 
26
#include "tasklmbmenu.moc"
 
27
 
 
28
#include <QPainter>
 
29
#include <QStyle>
 
30
#include <QMenuItem>
 
31
#include <QDragLeaveEvent>
 
32
#include <QDragMoveEvent>
 
33
#include <QList>
 
34
#include <QDragEnterEvent>
 
35
#include <QMouseEvent>
 
36
 
 
37
#include <kdebug.h>
 
38
#include <kglobalsettings.h>
 
39
 
 
40
#include <kmenu.h>
 
41
 
 
42
//#include "utils.h"
 
43
 
 
44
#if 0
 
45
TaskMenuItem::TaskMenuItem(const QString &text,
 
46
                           bool active, bool minimized, bool attention)
 
47
  : QCustomMenuItem(),
 
48
    m_text(text),
 
49
    m_isActive(active),
 
50
    m_isMinimized(minimized),
 
51
    m_demandsAttention(attention),
 
52
    m_attentionState(true)
 
53
{
 
54
}
 
55
 
 
56
TaskMenuItem::~TaskMenuItem()
 
57
{
 
58
}
 
59
 
 
60
void TaskMenuItem::paint(QPainter *p, const QColorGroup &cg,
 
61
                         bool highlighted, bool /*enabled*/,
 
62
                         int x, int y, int w, int h )
 
63
{
 
64
    if (m_isActive)
 
65
    {
 
66
        QFont font = p->font();
 
67
        font.setBold(true);
 
68
        p->setFont(font);
 
69
    }
 
70
 
 
71
    if (highlighted)
 
72
    {
 
73
        p->setPen(cg.highlightedText());
 
74
    }
 
75
    else if (m_isMinimized)
 
76
    {
 
77
        p->setPen(QPen(Plasma::blendColors(cg.background(), cg.text())));
 
78
    }
 
79
    else if (m_demandsAttention && !m_attentionState)
 
80
    {
 
81
        p->setPen(cg.mid());
 
82
    }
 
83
 
 
84
    p->drawText(x, y, w, h, Qt::AlignLeft|Qt::AlignVCenter|Qt::TextDontClip|Qt::TextShowMnemonic, m_text);
 
85
}
 
86
 
 
87
QSize TaskMenuItem::sizeHint()
 
88
{
 
89
    QFont font = QFont();
 
90
    if (m_isActive)
 
91
    {
 
92
        font.setBold(true);
 
93
    }
 
94
    return QFontMetrics(font).size(AlignAuto|AlignVCenter|DontClip|ShowPrefix,
 
95
                                   m_text);
 
96
}
 
97
#endif
 
98
 
 
99
/*****************************************************************************/
 
100
 
 
101
TaskLMBMenu::TaskLMBMenu(const Task::List& tasks, QWidget *parent, const char *name)
 
102
  : QMenu(parent),
 
103
    m_tasks(tasks),
 
104
    m_lastDragId(-1),
 
105
    m_attentionState(false)
 
106
{
 
107
    setObjectName(name);
 
108
    fillMenu();
 
109
 
 
110
    setAcceptDrops(true); // Always enabled to activate task during drag&drop.
 
111
 
 
112
    m_dragSwitchTimer = new QTimer(this, "DragSwitchTimer");
 
113
    m_dragSwitchTimer->setSingleShot(true);
 
114
    connect(m_dragSwitchTimer, SIGNAL(timeout()), SLOT(dragSwitch()));
 
115
}
 
116
 
 
117
void TaskLMBMenu::fillMenu()
 
118
{
 
119
 
 
120
    Task::List::iterator itEnd = m_tasks.end();
 
121
    for (Task::List::iterator it = m_tasks.begin(); it != itEnd; ++it)
 
122
    {
 
123
        Task::TaskPtr t = (*it);
 
124
 
 
125
        QString text = t->visibleName().replace("&", "&&");
 
126
 
 
127
        //### KDE4
 
128
/*        TaskMenuItem *menuItem = new TaskMenuItem(text,
 
129
                                                  t->isActive(),
 
130
                                                  t->isIconified(),
 
131
                                                  t->demandsAttention());*/
 
132
        //QAction* menuItem = 
 
133
        int id = insertItem(QIcon(t->pixmap()), text);
 
134
        connectItem(id, t.data(), SLOT(activateRaiseOrIconify()));
 
135
        setItemChecked(id, t->isActive());
 
136
 
 
137
        if (t->demandsAttention())
 
138
        {
 
139
            m_attentionState = true;
 
140
            m_attentionMap.append(actions().at(indexOf(id)));
 
141
        }
 
142
    }
 
143
 
 
144
    if (m_attentionState)
 
145
    {
 
146
        m_attentionTimer = new QTimer(this, "AttentionTimer");
 
147
        connect(m_attentionTimer, SIGNAL(timeout()), SLOT(attentionTimeout()));
 
148
        m_attentionTimer->setSingleShot(true);
 
149
        m_attentionTimer->start(500);
 
150
    }
 
151
}
 
152
 
 
153
void TaskLMBMenu::attentionTimeout()
 
154
{
 
155
    m_attentionState = !m_attentionState;
 
156
 
 
157
    //### KDE4
 
158
#if 0
 
159
    foreach (TaskMenuItem* item, m_attentionMap)
 
160
    {
 
161
        item->setAttentionState(m_attentionState);
 
162
    }
 
163
#endif
 
164
 
 
165
    update();
 
166
 
 
167
    m_attentionTimer->start(500);
 
168
}
 
169
 
 
170
void TaskLMBMenu::dragEnterEvent( QDragEnterEvent* e )
 
171
{
 
172
    // ignore task drags
 
173
    if (TaskDrag::canDecode(e->mimeData()))
 
174
    {
 
175
        return;
 
176
    }
 
177
 
 
178
    int id = static_cast<QMenuItem*>(actionAt(e->pos()))->id();
 
179
 
 
180
    if (id == -1)
 
181
    {
 
182
        m_dragSwitchTimer->stop();
 
183
        m_lastDragId = -1;
 
184
    }
 
185
    else if (id != m_lastDragId)
 
186
    {
 
187
        m_lastDragId = id;
 
188
        m_dragSwitchTimer->start(1000);
 
189
    }
 
190
 
 
191
    QMenu::dragEnterEvent( e );
 
192
}
 
193
 
 
194
void TaskLMBMenu::dragLeaveEvent( QDragLeaveEvent* e )
 
195
{
 
196
    m_dragSwitchTimer->stop();
 
197
    m_lastDragId = -1;
 
198
 
 
199
    QMenu::dragLeaveEvent(e);
 
200
 
 
201
    hide();
 
202
}
 
203
 
 
204
void TaskLMBMenu::dragMoveEvent( QDragMoveEvent* e )
 
205
{
 
206
    // ignore task drags
 
207
    if (TaskDrag::canDecode(e->mimeData()))
 
208
    {
 
209
        return;
 
210
    }
 
211
 
 
212
    int id = static_cast<QMenuItem*>(actionAt(e->pos()))->id();
 
213
 
 
214
    if (id == -1)
 
215
    {
 
216
        m_dragSwitchTimer->stop();
 
217
        m_lastDragId = -1;
 
218
    }
 
219
    else if (id != m_lastDragId)
 
220
    {
 
221
        m_lastDragId = id;
 
222
        m_dragSwitchTimer->start(1000);
 
223
    }
 
224
 
 
225
    QMenu::dragMoveEvent(e);
 
226
}
 
227
 
 
228
void TaskLMBMenu::dragSwitch()
 
229
{
 
230
    Task::TaskPtr t = m_tasks.at(indexOf(m_lastDragId));
 
231
    if (t)
 
232
    {
 
233
        t->activate();
 
234
 
 
235
        for (unsigned int i = 0; i < count(); ++i)
 
236
        {
 
237
            setItemChecked(idAt(i), false );
 
238
        }
 
239
 
 
240
        setItemChecked( m_lastDragId, true );
 
241
    }
 
242
}
 
243
 
 
244
void TaskLMBMenu::mousePressEvent( QMouseEvent* e )
 
245
{
 
246
    if (e->button() == Qt::LeftButton)
 
247
    {
 
248
        m_dragStartPos = e->pos();
 
249
    }
 
250
    else
 
251
    {
 
252
        m_dragStartPos = QPoint();
 
253
    }
 
254
 
 
255
    QMenu::mousePressEvent(e);
 
256
}
 
257
 
 
258
void TaskLMBMenu::mouseReleaseEvent(QMouseEvent* e)
 
259
{
 
260
    m_dragStartPos = QPoint();
 
261
    QMenu::mouseReleaseEvent(e);
 
262
}
 
263
 
 
264
void TaskLMBMenu::mouseMoveEvent(QMouseEvent* e)
 
265
{
 
266
    if (m_dragStartPos.isNull())
 
267
    {
 
268
        QMenu::mouseMoveEvent(e);
 
269
        return;
 
270
    }
 
271
 
 
272
    int delay = KGlobalSettings::dndEventDelay();
 
273
    QPoint newPos(e->pos());
 
274
 
 
275
    if ((m_dragStartPos - newPos).manhattanLength() > delay)
 
276
    {
 
277
        int index = actions().indexOf(actionAt(e->pos()));
 
278
        if (index != -1)
 
279
        {
 
280
            Task::TaskPtr task = m_tasks.at(index);
 
281
            if (task)
 
282
            {
 
283
                Task::List tasks;
 
284
                tasks.append(task);
 
285
                TaskDrag* drag = new TaskDrag(tasks, this);
 
286
                drag->setPixmap(task->pixmap());
 
287
                drag->start();
 
288
            }
 
289
        }
 
290
    }
 
291
 
 
292
    QMenu::mouseMoveEvent(e);
 
293
}
 
294