1 /***************************************************************************
2 * Copyright (C) 2008 Marco Martin <notmart@gmail.com> *
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. *
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. *
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"
25 #include <QPaintEvent>
27 #include <QApplication>
32 #include <KIconLoader>
35 #include <Plasma/Applet>
36 #include <Plasma/FrameSvg>
37 #include <Plasma/Theme>
45 TasksMenu::TasksMenu(QWidget
*parent
, TaskGroup
*group
, GroupManager
*groupManager
, Tasks
*applet
)
46 : GroupPopupMenu(parent
, group
, groupManager
),
48 m_lasttriggeredAction(0),
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
);
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);
71 m_background
->setEnabledBorders(Plasma::FrameSvg::LeftBorder
| Plasma::FrameSvg::BottomBorder
72 | Plasma::FrameSvg::RightBorder
);
74 setContentsMargins(leftWidth
, 0, rightWidth
, bottomHeight
);
76 case Plasma::LeftEdge
:
77 m_background
->setEnabledBorders(Plasma::FrameSvg::TopBorder
| Plasma::FrameSvg::BottomBorder
78 | Plasma::FrameSvg::RightBorder
);
80 setContentsMargins(0, topHeight
, rightWidth
, bottomHeight
);
82 case Plasma::RightEdge
:
83 m_background
->setEnabledBorders(Plasma::FrameSvg::TopBorder
| Plasma::FrameSvg::BottomBorder
84 | Plasma::FrameSvg::LeftBorder
);
86 setContentsMargins(leftWidth
, topHeight
, 0, bottomHeight
);
89 m_background
->setEnabledBorders(Plasma::FrameSvg::AllBorders
);
90 setContentsMargins(leftWidth
, topHeight
, rightWidth
, bottomHeight
);
94 TasksMenu::~TasksMenu()
97 void TasksMenu::paintEvent(QPaintEvent
*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;
110 Plasma::FrameSvg
*itemBackground
= m_applet
->itemBackground();
111 itemBackground
->setElementPrefix("normal");
113 foreach (QAction
*a
, actions()) {
114 QRect
actionRect(actionGeometry(a
));
117 itemBackground
->resizeFrame(actionRect
.size());
118 itemBackground
->getMargins(left
, top
, right
, bottom
);
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
),
126 QRect
textRect(QStyle::alignedRect(QApplication::layoutDirection(), Qt::AlignRight
| Qt::AlignVCenter
,
127 QSize(deframedRect
.width() - iconRect
.width() - 3, deframedRect
.height()),
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";
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
)
170 m_activateTimer
->stop();
176 void TasksMenu::dropEvent(QDropEvent
*event
)
179 m_activateTimer
->stop();
185 void TasksMenu::activate()
187 QAction
*action
= actionAt(m_lastMousePos
);
189 if (action
&& action
!= m_lasttriggeredAction
) {
190 m_lasttriggeredAction
= action
;
197 #include "tasksmenu.moc"