00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "tasksmenu.h"
00022
00023
00024 #include <QPainter>
00025 #include <QPaintEvent>
00026 #include <QStyle>
00027 #include <QApplication>
00028 #include <QBitmap>
00029 #include <QTimer>
00030
00031
00032 #include <KIconLoader>
00033
00034
00035 #include <Plasma/Applet>
00036 #include <Plasma/FrameSvg>
00037 #include <Plasma/Theme>
00038
00039 #include "tasks.h"
00040
00041 namespace TaskManager
00042 {
00043
00044
00045 TasksMenu::TasksMenu(QWidget *parent, TaskGroup *group, GroupManager *groupManager, Tasks *applet)
00046 : GroupPopupMenu(parent, group, groupManager),
00047 m_activateTimer(0),
00048 m_lasttriggeredAction(0),
00049 m_applet(applet)
00050 {
00051 setAttribute(Qt::WA_NoSystemBackground);
00052
00053 m_background = new Plasma::FrameSvg(this);
00054 m_background->setImagePath("dialogs/background");
00055
00056
00057 const int topHeight = m_background->marginSize(Plasma::TopMargin);
00058 const int leftWidth = m_background->marginSize(Plasma::LeftMargin);
00059 const int rightWidth = m_background->marginSize(Plasma::RightMargin);
00060 const int bottomHeight = m_background->marginSize(Plasma::BottomMargin);
00061
00062 setAcceptDrops(true);
00063
00064 switch (m_applet->location()) {
00065 case Plasma::BottomEdge:
00066 m_background->setEnabledBorders(Plasma::FrameSvg::LeftBorder | Plasma::FrameSvg::TopBorder
00067 | Plasma::FrameSvg::RightBorder);
00068 setContentsMargins(leftWidth, topHeight, rightWidth, 0);
00069 break;
00070 case Plasma::TopEdge:
00071 m_background->setEnabledBorders(Plasma::FrameSvg::LeftBorder | Plasma::FrameSvg::BottomBorder
00072 | Plasma::FrameSvg::RightBorder);
00073
00074 setContentsMargins(leftWidth, 0, rightWidth, bottomHeight);
00075 break;
00076 case Plasma::LeftEdge:
00077 m_background->setEnabledBorders(Plasma::FrameSvg::TopBorder | Plasma::FrameSvg::BottomBorder
00078 | Plasma::FrameSvg::RightBorder);
00079
00080 setContentsMargins(0, topHeight, rightWidth, bottomHeight);
00081 break;
00082 case Plasma::RightEdge:
00083 m_background->setEnabledBorders(Plasma::FrameSvg::TopBorder | Plasma::FrameSvg::BottomBorder
00084 | Plasma::FrameSvg::LeftBorder);
00085
00086 setContentsMargins(leftWidth, topHeight, 0, bottomHeight);
00087 break;
00088 default:
00089 m_background->setEnabledBorders(Plasma::FrameSvg::AllBorders);
00090 setContentsMargins(leftWidth, topHeight, rightWidth, bottomHeight);
00091 }
00092 }
00093
00094 TasksMenu::~TasksMenu()
00095 {}
00096
00097 void TasksMenu::paintEvent(QPaintEvent *event)
00098 {
00099
00100
00101 QPainter painter(this);
00102 painter.setCompositionMode(QPainter::CompositionMode_Source);
00103 painter.fillRect(event->rect(), Qt::transparent);
00104 m_background->paintFrame(&painter);
00105
00106 painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
00107
00108 qreal left = 0, right = 0, top = 0, bottom = 0;
00109 bool first = true;
00110 Plasma::FrameSvg *itemBackground = m_applet->itemBackground();
00111 itemBackground->setElementPrefix("normal");
00112
00113 foreach (QAction *a, actions()) {
00114 QRect actionRect(actionGeometry(a));
00115
00116 if (first) {
00117 itemBackground->resizeFrame(actionRect.size());
00118 itemBackground->getMargins(left, top, right, bottom);
00119 first = false;
00120 }
00121
00122 QRect deframedRect = actionRect.adjusted(left, 0, -right, 0);
00123 QRect iconRect(QStyle::alignedRect(QApplication::layoutDirection(), Qt::AlignLeft | Qt::AlignVCenter,
00124 QSize(KIconLoader::SizeSmall, KIconLoader::SizeSmall),
00125 deframedRect));
00126 QRect textRect(QStyle::alignedRect(QApplication::layoutDirection(), Qt::AlignRight | Qt::AlignVCenter,
00127 QSize(deframedRect.width() - iconRect.width() - 3, deframedRect.height()),
00128 deframedRect));
00129
00130 if (activeAction() == a && m_applet->itemBackground()) {
00131 itemBackground->paintFrame(&painter, actionRect.topLeft());
00132 }
00133
00134 painter.drawPixmap(iconRect, a->icon().pixmap(iconRect.size()));
00135 painter.setPen(Plasma::Theme::defaultTheme()->color(Plasma::Theme::TextColor));
00136 painter.drawText(textRect, Qt::AlignLeft|Qt::AlignVCenter, a->text());
00137 }
00138
00139 }
00140
00141 void TasksMenu::resizeEvent(QResizeEvent *event)
00142 {
00143 m_background->resizeFrame(event->size());
00144 setMask(m_background->mask());
00145 }
00146
00147
00148 void TasksMenu::dragEnterEvent(QDragEnterEvent *event)
00149 {
00150
00151 event->accept();
00152 }
00153
00154 void TasksMenu::dragMoveEvent(QDragMoveEvent *event)
00155 {
00156 if (!m_activateTimer) {
00157 m_activateTimer = new QTimer(this);
00158 m_activateTimer->setSingleShot(true);
00159 m_activateTimer->setInterval(300);
00160 connect(m_activateTimer, SIGNAL(timeout()), this, SLOT(activate()));
00161 }
00162
00163 m_lastMousePos = event->pos();
00164 m_activateTimer->start(300);
00165 }
00166
00167 void TasksMenu::dragLeaveEvent(QDragLeaveEvent *event)
00168 {
00169 Q_UNUSED(event)
00170 m_activateTimer->stop();
00171 m_activateTimer = 0;
00172
00173 close();
00174 }
00175
00176 void TasksMenu::dropEvent(QDropEvent *event)
00177 {
00178 Q_UNUSED(event)
00179 m_activateTimer->stop();
00180 m_activateTimer = 0;
00181
00182 close();
00183 }
00184
00185 void TasksMenu::activate()
00186 {
00187 QAction *action = actionAt(m_lastMousePos);
00188
00189 if (action && action != m_lasttriggeredAction) {
00190 m_lasttriggeredAction = action;
00191 action->trigger();
00192 }
00193 }
00194
00195 }
00196
00197 #include "tasksmenu.moc"
00198
00199