libtaskmanager
tasklmbmenu.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "tasklmbmenu.h"
00026 #include "tasklmbmenu.moc"
00027
00028 #include <QPainter>
00029 #include <QStyle>
00030 #include <QMenuItem>
00031 #include <QDragLeaveEvent>
00032 #include <QDragMoveEvent>
00033 #include <QList>
00034 #include <QDragEnterEvent>
00035 #include <QMouseEvent>
00036
00037 #include <kdebug.h>
00038 #include <kglobalsettings.h>
00039
00040 #include <kmenu.h>
00041
00042
00043
00044 #if 0
00045 TaskMenuItem::TaskMenuItem(const QString &text,
00046 bool active, bool minimized, bool attention)
00047 : QCustomMenuItem(),
00048 m_text(text),
00049 m_isActive(active),
00050 m_isMinimized(minimized),
00051 m_demandsAttention(attention),
00052 m_attentionState(true)
00053 {
00054 }
00055
00056 TaskMenuItem::~TaskMenuItem()
00057 {
00058 }
00059
00060 void TaskMenuItem::paint(QPainter *p, const QColorGroup &cg,
00061 bool highlighted, bool ,
00062 int x, int y, int w, int h )
00063 {
00064 if (m_isActive)
00065 {
00066 QFont font = p->font();
00067 font.setBold(true);
00068 p->setFont(font);
00069 }
00070
00071 if (highlighted)
00072 {
00073 p->setPen(cg.highlightedText());
00074 }
00075 else if (m_isMinimized)
00076 {
00077 p->setPen(QPen(Plasma::blendColors(cg.background(), cg.text())));
00078 }
00079 else if (m_demandsAttention && !m_attentionState)
00080 {
00081 p->setPen(cg.mid());
00082 }
00083
00084 p->drawText(x, y, w, h, Qt::AlignLeft|Qt::AlignVCenter|Qt::TextDontClip|Qt::TextShowMnemonic, m_text);
00085 }
00086
00087 QSize TaskMenuItem::sizeHint()
00088 {
00089 QFont font = QFont();
00090 if (m_isActive)
00091 {
00092 font.setBold(true);
00093 }
00094 return QFontMetrics(font).size(AlignAuto|AlignVCenter|DontClip|ShowPrefix,
00095 m_text);
00096 }
00097 #endif
00098
00099
00100
00101 TaskLMBMenu::TaskLMBMenu(const Task::List& tasks, QWidget *parent, const char *name)
00102 : QMenu(parent),
00103 m_tasks(tasks),
00104 m_lastDragId(-1),
00105 m_attentionState(false)
00106 {
00107 setObjectName(name);
00108 fillMenu();
00109
00110 setAcceptDrops(true);
00111
00112 m_dragSwitchTimer = new QTimer(this, "DragSwitchTimer");
00113 m_dragSwitchTimer->setSingleShot(true);
00114 connect(m_dragSwitchTimer, SIGNAL(timeout()), SLOT(dragSwitch()));
00115 }
00116
00117 void TaskLMBMenu::fillMenu()
00118 {
00119
00120 Task::List::iterator itEnd = m_tasks.end();
00121 for (Task::List::iterator it = m_tasks.begin(); it != itEnd; ++it)
00122 {
00123 Task::TaskPtr t = (*it);
00124
00125 QString text = t->visibleName().replace("&", "&&");
00126
00127
00128
00129
00130
00131
00132
00133 int id = insertItem(QIcon(t->pixmap()), text);
00134 connectItem(id, t.data(), SLOT(activateRaiseOrIconify()));
00135 setItemChecked(id, t->isActive());
00136
00137 if (t->demandsAttention())
00138 {
00139 m_attentionState = true;
00140 m_attentionMap.append(actions().at(indexOf(id)));
00141 }
00142 }
00143
00144 if (m_attentionState)
00145 {
00146 m_attentionTimer = new QTimer(this, "AttentionTimer");
00147 connect(m_attentionTimer, SIGNAL(timeout()), SLOT(attentionTimeout()));
00148 m_attentionTimer->setSingleShot(true);
00149 m_attentionTimer->start(500);
00150 }
00151 }
00152
00153 void TaskLMBMenu::attentionTimeout()
00154 {
00155 m_attentionState = !m_attentionState;
00156
00157
00158 #if 0
00159 foreach (TaskMenuItem* item, m_attentionMap)
00160 {
00161 item->setAttentionState(m_attentionState);
00162 }
00163 #endif
00164
00165 update();
00166
00167 m_attentionTimer->start(500);
00168 }
00169
00170 void TaskLMBMenu::dragEnterEvent( QDragEnterEvent* e )
00171 {
00172
00173 if (TaskDrag::canDecode(e->mimeData()))
00174 {
00175 return;
00176 }
00177
00178 int id = static_cast<QMenuItem*>(actionAt(e->pos()))->id();
00179
00180 if (id == -1)
00181 {
00182 m_dragSwitchTimer->stop();
00183 m_lastDragId = -1;
00184 }
00185 else if (id != m_lastDragId)
00186 {
00187 m_lastDragId = id;
00188 m_dragSwitchTimer->start(1000);
00189 }
00190
00191 QMenu::dragEnterEvent( e );
00192 }
00193
00194 void TaskLMBMenu::dragLeaveEvent( QDragLeaveEvent* e )
00195 {
00196 m_dragSwitchTimer->stop();
00197 m_lastDragId = -1;
00198
00199 QMenu::dragLeaveEvent(e);
00200
00201 hide();
00202 }
00203
00204 void TaskLMBMenu::dragMoveEvent( QDragMoveEvent* e )
00205 {
00206
00207 if (TaskDrag::canDecode(e->mimeData()))
00208 {
00209 return;
00210 }
00211
00212 int id = static_cast<QMenuItem*>(actionAt(e->pos()))->id();
00213
00214 if (id == -1)
00215 {
00216 m_dragSwitchTimer->stop();
00217 m_lastDragId = -1;
00218 }
00219 else if (id != m_lastDragId)
00220 {
00221 m_lastDragId = id;
00222 m_dragSwitchTimer->start(1000);
00223 }
00224
00225 QMenu::dragMoveEvent(e);
00226 }
00227
00228 void TaskLMBMenu::dragSwitch()
00229 {
00230 Task::TaskPtr t = m_tasks.at(indexOf(m_lastDragId));
00231 if (t)
00232 {
00233 t->activate();
00234
00235 for (unsigned int i = 0; i < count(); ++i)
00236 {
00237 setItemChecked(idAt(i), false );
00238 }
00239
00240 setItemChecked( m_lastDragId, true );
00241 }
00242 }
00243
00244 void TaskLMBMenu::mousePressEvent( QMouseEvent* e )
00245 {
00246 if (e->button() == Qt::LeftButton)
00247 {
00248 m_dragStartPos = e->pos();
00249 }
00250 else
00251 {
00252 m_dragStartPos = QPoint();
00253 }
00254
00255 QMenu::mousePressEvent(e);
00256 }
00257
00258 void TaskLMBMenu::mouseReleaseEvent(QMouseEvent* e)
00259 {
00260 m_dragStartPos = QPoint();
00261 QMenu::mouseReleaseEvent(e);
00262 }
00263
00264 void TaskLMBMenu::mouseMoveEvent(QMouseEvent* e)
00265 {
00266 if (m_dragStartPos.isNull())
00267 {
00268 QMenu::mouseMoveEvent(e);
00269 return;
00270 }
00271
00272 int delay = KGlobalSettings::dndEventDelay();
00273 QPoint newPos(e->pos());
00274
00275 if ((m_dragStartPos - newPos).manhattanLength() > delay)
00276 {
00277 int index = actions().indexOf(actionAt(e->pos()));
00278 if (index != -1)
00279 {
00280 Task::TaskPtr task = m_tasks.at(index);
00281 if (task)
00282 {
00283 Task::List tasks;
00284 tasks.append(task);
00285 TaskDrag* drag = new TaskDrag(tasks, this);
00286 drag->setPixmap(task->pixmap());
00287 drag->start();
00288 }
00289 }
00290 }
00291
00292 QMenu::mouseMoveEvent(e);
00293 }
00294