not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / plasma / applets / tasks / tasksmenu.cpp
blob21ab560613637d821db054c98e297824a7d4b7a6
1 /***************************************************************************
2 * Copyright (C) 2008 Marco Martin <notmart@gmail.com> *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . *
18 ***************************************************************************/
21 #include "tasksmenu.h"
23 //Qt
24 #include <QPainter>
25 #include <QPaintEvent>
26 #include <QStyle>
27 #include <QApplication>
28 #include <QBitmap>
29 #include <QTimer>
31 //KDE
32 #include <KIconLoader>
34 //Plasma
35 #include <Plasma/Applet>
36 #include <Plasma/FrameSvg>
37 #include <Plasma/Theme>
39 #include "tasks.h"
41 namespace TaskManager
45 TasksMenu::TasksMenu(QWidget *parent, TaskGroup *group, GroupManager *groupManager, Tasks *applet)
46 : GroupPopupMenu(parent, group, groupManager),
47 m_activateTimer(0),
48 m_lasttriggeredAction(0),
49 m_applet(applet)
51 setAttribute(Qt::WA_NoSystemBackground);
53 m_background = new Plasma::FrameSvg(this);
54 m_background->setImagePath("dialogs/background");
56 //since the thing gets destroyed on close we can set this just one time for now
57 const int topHeight = m_background->marginSize(Plasma::TopMargin);
58 const int leftWidth = m_background->marginSize(Plasma::LeftMargin);
59 const int rightWidth = m_background->marginSize(Plasma::RightMargin);
60 const int bottomHeight = m_background->marginSize(Plasma::BottomMargin);
62 setAcceptDrops(true);
64 switch (m_applet->location()) {
65 case Plasma::BottomEdge:
66 m_background->setEnabledBorders(Plasma::FrameSvg::LeftBorder | Plasma::FrameSvg::TopBorder
67 | Plasma::FrameSvg::RightBorder);
68 setContentsMargins(leftWidth, topHeight, rightWidth, 0);
69 break;
70 case Plasma::TopEdge:
71 m_background->setEnabledBorders(Plasma::FrameSvg::LeftBorder | Plasma::FrameSvg::BottomBorder
72 | Plasma::FrameSvg::RightBorder);
74 setContentsMargins(leftWidth, 0, rightWidth, bottomHeight);
75 break;
76 case Plasma::LeftEdge:
77 m_background->setEnabledBorders(Plasma::FrameSvg::TopBorder | Plasma::FrameSvg::BottomBorder
78 | Plasma::FrameSvg::RightBorder);
80 setContentsMargins(0, topHeight, rightWidth, bottomHeight);
81 break;
82 case Plasma::RightEdge:
83 m_background->setEnabledBorders(Plasma::FrameSvg::TopBorder | Plasma::FrameSvg::BottomBorder
84 | Plasma::FrameSvg::LeftBorder);
86 setContentsMargins(leftWidth, topHeight, 0, bottomHeight);
87 break;
88 default:
89 m_background->setEnabledBorders(Plasma::FrameSvg::AllBorders);
90 setContentsMargins(leftWidth, topHeight, rightWidth, bottomHeight);
94 TasksMenu::~TasksMenu()
97 void TasksMenu::paintEvent(QPaintEvent *event)
99 //Q_UNUSED(event)
101 QPainter painter(this);
102 painter.setCompositionMode(QPainter::CompositionMode_Source);
103 painter.fillRect(event->rect(), Qt::transparent);
104 m_background->paintFrame(&painter);
106 painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
108 qreal left = 0, right = 0, top = 0, bottom = 0;
109 bool first = true;
110 Plasma::FrameSvg *itemBackground = m_applet->itemBackground();
111 itemBackground->setElementPrefix("normal");
113 foreach (QAction *a, actions()) {
114 QRect actionRect(actionGeometry(a));
116 if (first) {
117 itemBackground->resizeFrame(actionRect.size());
118 itemBackground->getMargins(left, top, right, bottom);
119 first = false;
122 QRect deframedRect = actionRect.adjusted(left, 0, -right, 0);
123 QRect iconRect(QStyle::alignedRect(QApplication::layoutDirection(), Qt::AlignLeft | Qt::AlignVCenter,
124 QSize(KIconLoader::SizeSmall, KIconLoader::SizeSmall),
125 deframedRect));
126 QRect textRect(QStyle::alignedRect(QApplication::layoutDirection(), Qt::AlignRight | Qt::AlignVCenter,
127 QSize(deframedRect.width() - iconRect.width() - 3, deframedRect.height()),
128 deframedRect));
130 if (activeAction() == a && m_applet->itemBackground()) {
131 itemBackground->paintFrame(&painter, actionRect.topLeft());
134 painter.drawPixmap(iconRect, a->icon().pixmap(iconRect.size()));
135 painter.setPen(Plasma::Theme::defaultTheme()->color(Plasma::Theme::TextColor));
136 painter.drawText(textRect, Qt::AlignLeft|Qt::AlignVCenter, a->text());
141 void TasksMenu::resizeEvent(QResizeEvent *event)
143 m_background->resizeFrame(event->size());
144 setMask(m_background->mask());
148 void TasksMenu::dragEnterEvent(QDragEnterEvent *event)
150 //kDebug()<<"Drag enter";
151 event->accept();
154 void TasksMenu::dragMoveEvent(QDragMoveEvent *event)
156 if (!m_activateTimer) {
157 m_activateTimer = new QTimer(this);
158 m_activateTimer->setSingleShot(true);
159 m_activateTimer->setInterval(300);
160 connect(m_activateTimer, SIGNAL(timeout()), this, SLOT(activate()));
163 m_lastMousePos = event->pos();
164 m_activateTimer->start(300);
167 void TasksMenu::dragLeaveEvent(QDragLeaveEvent *event)
169 Q_UNUSED(event)
170 m_activateTimer->stop();
171 m_activateTimer = 0;
173 close();
176 void TasksMenu::dropEvent(QDropEvent *event)
178 Q_UNUSED(event)
179 m_activateTimer->stop();
180 m_activateTimer = 0;
182 close();
185 void TasksMenu::activate()
187 QAction *action = actionAt(m_lastMousePos);
189 if (action && action != m_lasttriggeredAction) {
190 m_lasttriggeredAction = action;
191 action->trigger();
197 #include "tasksmenu.moc"