• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdenetwork API Reference
  • KDE Home
  • Contact Us
 

kget

  • sources
  • kde-4.12
  • kdenetwork
  • kget
  • ui
transfersviewdelegate.cpp
Go to the documentation of this file.
1 /* This file is part of the KDE project
2 
3  Copyright (C) 2006 Dario Massarin <nekkar@libero.it>
4  Copyright (C) 2007 by Javier Goday <jgoday@gmail.com>
5  Copyright (C) 2008 Lukas Appelhans <l.appelhans@gmx.de>
6  Copyright (C) 2010 Matthias Fuchs <mat69@gmx.net>
7 
8  This program is free software; you can redistribute it and/or
9  modify it under the terms of the GNU General Public
10  License as published by the Free Software Foundation; either
11  version 2 of the License, or (at your option) any later version.
12 */
13 
14 #include "ui/transfersviewdelegate.h"
15 
16 #include "transferdetails.h"
17 #include "ui/contextmenu.h"
18 #include "core/kget.h"
19 #include "core/transferhandler.h"
20 #include "core/transfergrouphandler.h"
21 #include "core/transfertreemodel.h"
22 #include "core/transfertreeselectionmodel.h"
23 #include "settings.h"
24 
25 #include <kdebug.h>
26 #include <klocale.h>
27 #include <kmenu.h>
28 #include <kicon.h>
29 
30 #include <QApplication>
31 #include <QPainter>
32 #include <QMouseEvent>
33 #include <QModelIndex>
34 #include <QButtonGroup>
35 #include <QHBoxLayout>
36 #include <QGroupBox>
37 #include <QLabel>
38 #include <QAbstractItemView>
39 
40 GroupStatusButton::GroupStatusButton(const QModelIndex & index, QWidget * parent)
41  : QToolButton(parent),
42  m_status(None),
43  m_index(index),
44  m_timerId(-1),
45  m_iconSize(22),
46  m_gradientId(0)
47 {
48  setAttribute(Qt::WA_NoSystemBackground);
49 }
50 
51 void GroupStatusButton::checkStateSet()
52 {
53 // kDebug(5001) << "GroupStatusButton::checkStateSet";
54 
55  QToolButton::checkStateSet();
56 
57  if(isChecked())
58  {
59  if(m_status == None)
60  m_gradientId = 0.9;
61  m_status = Selecting;
62  }
63  else
64  {
65  if(m_status == None)
66  m_gradientId = 1;
67  m_status = Deselecting;
68  }
69 
70  setMouseTracking(!isChecked());
71 
72  if(m_timerId == -1)
73  m_timerId = startTimer(100);
74 }
75 
76 void GroupStatusButton::enterEvent(QEvent * event)
77 {
78  Q_UNUSED(event)
79  if(!isChecked())
80  {
81  m_status = Blinking;
82 
83  if(m_timerId == -1)
84  {
85  m_timerId = startTimer(100);
86 
87  if(m_status == !BlinkingExiting)
88  m_gradientId = 1;
89  }
90  }
91 }
92 
93 void GroupStatusButton::leaveEvent(QEvent * event)
94 {
95  Q_UNUSED(event)
96  if(m_status == Blinking)
97  m_status = BlinkingExiting;
98 }
99 
100 void GroupStatusButton::paintEvent(QPaintEvent * event)
101 {
102  Q_UNUSED(event)
103 
104  QPainter p(this);
105 
106  const int offset = (rect().width() - m_iconSize) / 2;
107 
108  if(m_gradientId == 0)
109  m_gradientId = isChecked() ? 1 : 0.7;
110 
111  QRadialGradient gradient(height() / 2.0, height() / 2.0, height() / 2);
112 
113  QPen pen;
114 
115  if(KGet::selectionModel()->isSelected(m_index))
116  {
117  gradient.setColorAt(0, palette().color(QPalette::AlternateBase));
118  gradient.setColorAt(m_gradientId, Qt::transparent);
119  gradient.setColorAt(1, Qt::transparent);
120  pen.setColor(palette().color(QPalette::AlternateBase));
121  }
122  else
123  {
124  gradient.setColorAt(0, palette().color(QPalette::Highlight));
125  gradient.setColorAt(m_gradientId, Qt::transparent);
126  gradient.setColorAt(1, Qt::transparent);
127  pen.setColor(palette().color(QPalette::Highlight));
128  }
129 
130  QRect r = rect().adjusted(0, 0, 0, 1);
131 
132  p.fillRect(r, gradient);
133 
134  p.setRenderHint(QPainter::Antialiasing);
135 
136  if(isChecked())
137  {
138  pen.setWidth(1);
139  p.setPen(pen);
140  p.drawEllipse(rect().x()+5, rect().y()+4, rect().width()-10, rect().width()-10);
141  }
142 
143  p.drawPixmap(rect().topLeft() + QPoint(offset, offset - 1),
144  icon().pixmap(m_iconSize, isChecked() || m_status == Blinking ?
145  QIcon::Normal : QIcon::Disabled));
146 }
147 
148 void GroupStatusButton::timerEvent(QTimerEvent *event)
149 {
150  Q_UNUSED(event)
151 
152  if(m_status == Selecting)
153  {
154  m_gradientId+=0.04;
155 
156  if(m_gradientId >= 1)
157  {
158  m_status = None;
159  m_gradientId = 1;
160  killTimer(m_timerId);
161  m_timerId = -1;
162  }
163  }
164  else if(m_status == Deselecting)
165  {
166  m_gradientId-=0.04;
167 
168  if(m_gradientId <= 0.7)
169  {
170  m_status = None;
171  m_gradientId = 0.7;
172  killTimer(m_timerId);
173  m_timerId = -1;
174  }
175  }
176  else if(m_status == Blinking)
177  {
178  if(isChecked())
179  {
180  m_status = Selecting;
181  m_gradientId = 0.9;
182  return;
183  }
184 
185  m_gradientId-=0.04;
186 
187  if(m_gradientId <= 0.7)
188  {
189  m_gradientId = 1;
190  }
191  }
192  else if(m_status == BlinkingExiting)
193  {
194  m_gradientId-=0.04;
195 
196  if(m_gradientId <= 0.7)
197  {
198  m_status = None;
199  m_gradientId = 0.7;
200  killTimer(m_timerId);
201  m_timerId = -1;
202  }
203  }
204 
205  update();
206 }
207 
208 GroupStatusEditor::GroupStatusEditor(const QModelIndex &index, QWidget *parent)
209  : QWidget(parent),
210  m_index(index)
211 {
212  setMinimumWidth(80);
213 
214  m_layout = new QHBoxLayout();
215  m_layout->addStretch();
216  setLayout(m_layout);
217 
218  m_btGroup = new QButtonGroup(this);
219  m_btGroup->setExclusive(true);
220 
221  m_startBt = new GroupStatusButton(m_index, this);
222  m_startBt->setCheckable(true);
223  m_startBt->setAutoRaise(true);
224  m_startBt->setIcon(KIcon("media-playback-start"));
225  m_startBt->setFixedSize(36, 36);
226  m_startBt->setIconSize(QSize(22, 22));
227  m_layout->addWidget(m_startBt);
228  m_btGroup->addButton(m_startBt);
229 
230  m_stopBt = new GroupStatusButton(m_index, this);
231  m_stopBt->setCheckable(true);
232  m_stopBt->setAutoRaise(true);
233  m_stopBt->setIcon(KIcon("media-playback-pause"));
234  m_stopBt->setFixedSize(36, 36);
235  m_stopBt->setIconSize(QSize(22, 22));
236  m_layout->addWidget(m_stopBt);
237  m_btGroup->addButton(m_stopBt);
238 
239  m_stopBt->setChecked(true);
240 
241  m_layout->addStretch();
242  m_layout->setMargin(1);
243 
244  connect(m_startBt, SIGNAL(toggled(bool)), this, SLOT(slotStatusChanged()));
245 }
246 
247 void GroupStatusEditor::setRunning(bool running)
248 {
249  if(running == m_startBt->isChecked())
250  return;
251 
252  if(running)
253  m_startBt->setChecked(true);
254  else
255  m_stopBt->setChecked(true);
256 }
257 
258 bool GroupStatusEditor::isRunning()
259 {
260  return m_startBt->isChecked();
261 }
262 
263 void GroupStatusEditor::slotStatusChanged()
264 {
265  emit changedStatus(this);
266 }
267 
268 
269 BasicTransfersViewDelegate::BasicTransfersViewDelegate(QAbstractItemView *parent)
270  : KExtendableItemDelegate(parent)
271 {
272 }
273 
274 QWidget *BasicTransfersViewDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
275 {
276  if (index.column() == TransferTreeModel::Status) {
277  GroupStatusEditor *qroupStatusEditor = new GroupStatusEditor(index, parent);
278  connect(qroupStatusEditor, SIGNAL(changedStatus(GroupStatusEditor*)), this, SLOT(slotGroupStatusChanged(GroupStatusEditor*)));
279  return qroupStatusEditor;
280  } else {
281  return KExtendableItemDelegate::createEditor(parent, option, index);
282  }
283 }
284 
285 void BasicTransfersViewDelegate::slotGroupStatusChanged(GroupStatusEditor *editor)
286 {
287  commitData(editor);
288 }
289 
290 void BasicTransfersViewDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
291 {
292  if (index.column() == TransferTreeModel::Status) {
293  GroupStatusEditor *groupEditor = static_cast<GroupStatusEditor*>(editor);
294  groupEditor->setRunning(KGet::model()->itemFromIndex(index)->asGroup()->groupHandler()->status() == JobQueue::Running);
295  } else {
296  KExtendableItemDelegate::setEditorData(editor, index);
297  }
298 }
299 
300 void BasicTransfersViewDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
301 {
302  if (index.column() == TransferTreeModel::Status) {
303  GroupStatusEditor *groupEditor = static_cast<GroupStatusEditor *>(editor);
304  TransferGroupHandler *groupHandler = KGet::model()->itemFromIndex(index)->asGroup()->groupHandler();
305 
306  if (groupEditor->isRunning()) {
307  groupHandler->start();
308  } else {
309  groupHandler->stop();
310  }
311  } else {
312  KExtendableItemDelegate::setModelData(editor, model, index);
313  }
314 }
315 
316 TransfersViewDelegate::TransfersViewDelegate(QAbstractItemView *parent)
317  : BasicTransfersViewDelegate(parent)
318 {
319  Q_ASSERT(qobject_cast<QAbstractItemView *>(parent));
320  setExtendPixmap(SmallIcon("arrow-right"));
321  setContractPixmap(SmallIcon("arrow-down"));
322 }
323 
324 void TransfersViewDelegate::paint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index) const
325 {
326  TransferTreeModel * transferTreeModel = KGet::model();
327 
328  ModelItem * item = transferTreeModel->itemFromIndex(index);
329 
330  if (item->isGroup())
331  {
332  painter->save();
333 
334  if (option.state & QStyle::State_Selected)
335  {
336  }
337  else
338  {
339  static bool backgroundInitialized = false;
340  static QPixmap groupBackground(64, 35);
341  static QPalette palette(QApplication::palette());
342 
343  if(!backgroundInitialized || palette!= QApplication::palette())
344  {
345  const QRect rect = groupBackground.rect();
346  QPainter p(&groupBackground);
347 
348  QLinearGradient gradient(rect.x(), rect.y(),
349  rect.x(), (rect.y() + rect.height()));
350 
351  gradient.setColorAt(0, QApplication::palette().color(QPalette::Base));
352  gradient.setColorAt(0.5, QApplication::palette().color(QPalette::AlternateBase).darker(110));
353  gradient.setColorAt(1, QApplication::palette().color(QPalette::Base));
354 
355  p.fillRect(rect, gradient);
356  backgroundInitialized = true;
357  }
358 
359  painter->drawTiledPixmap(option.rect, groupBackground);
360  }
361 
362  KExtendableItemDelegate::paint(painter, option, index);
363 
364  painter->restore();
365  }
366  else {
367  if (KGet::selectionModel()->isSelected(index))
368  painter->fillRect(option.rect, QApplication::palette().color(option.state & QStyle::State_Active ?
369  QPalette::Active : QPalette::Inactive,
370  QPalette::Highlight));
371 
372  KExtendableItemDelegate::paint(painter, option, index);
373 
374  if (index.column() == 3 && !isExtended(transferTreeModel->index(index.row(), 0, index.parent()))) { // the percent column
375  TransferHandler *transferHandler = item->asTransfer()->transferHandler();
376 
377  // following progressbar code has mostly been taken from Qt4 examples/network/torrent/mainview.cpp
378  // Set up a QStyleOptionProgressBar to precisely mimic the
379  // environment of a progress bar.
380  QStyleOptionProgressBar progressBarOption;
381  progressBarOption.state = QStyle::State_Enabled;
382  progressBarOption.direction = QApplication::layoutDirection();
383  progressBarOption.rect = option.rect;
384  progressBarOption.fontMetrics = QApplication::fontMetrics();
385  progressBarOption.minimum = 0;
386  progressBarOption.maximum = 100;
387  progressBarOption.textAlignment = Qt::AlignCenter;
388  progressBarOption.textVisible = true;
389 
390  // Set the progress and text values of the style option.
391  int progress = transferHandler->percent();
392  if (progress >= 0 && progress <= 100) {
393  progressBarOption.progress = progress;
394  progressBarOption.text = QString().sprintf("%d%%", progressBarOption.progress);
395  } else {
396  progressBarOption.text = i18nc("not available", "n/a");
397  }
398 
399  progressBarOption.rect.setY(progressBarOption.rect.y() +
400  (option.rect.height() - QApplication::fontMetrics().height()) / 2);
401  progressBarOption.rect.setHeight(QApplication::fontMetrics().height());
402 
403  // Draw the progress bar onto the view.
404  QApplication::style()->drawControl(QStyle::CE_ProgressBar, &progressBarOption, painter);
405  }
406  }
407 
409 // static int i=0;
410 // kDebug(5001) << "paint!!! " << i++ << " " << index.internalPointer() << " " << index.column();
411 //
412 // painter->drawRect(option.rect);
413 // painter->drawText(option.rect.topLeft(), QString::number(i));
414 }
415 
416 void TransfersViewDelegate::drawFocus(QPainter * painter, const QStyleOptionViewItem & option, const
417  QRect & rect) const
418 {
419  Q_UNUSED(painter)
420  Q_UNUSED(option)
421  Q_UNUSED(rect)
422 }
423 
424 QSize TransfersViewDelegate::sizeHint(const QStyleOptionViewItem & option, const QModelIndex & index) const
425 {
426  Q_UNUSED(option)
427 
428  TransferTreeModel *transferTreeModel = KGet::model();
429  ModelItem *item = transferTreeModel->itemFromIndex(index);
430 
431  if (!item) {
432  kWarning(5001) << "Sizehint for non-existing item.";
433  return QSize();
434  }
435 
436  if (transferTreeModel->itemFromIndex(index)->isGroup())
437  {
438  return QSize(0, 35);
439  }
440  else {
441  QSize ret(KExtendableItemDelegate::sizeHint(option, index));
442  ret.rheight() += 8;
443  return ret;
444  }
445 }
446 
447 bool TransfersViewDelegate::editorEvent(QEvent * event, QAbstractItemModel * model, const QStyleOptionViewItem & option, const QModelIndex & index)
448 {
449  Q_UNUSED(model)
450  Q_UNUSED(option)
451 
452  if (event->type() == QEvent::MouseButtonRelease)
453  {
454  QMouseEvent * mouseEvent = static_cast<QMouseEvent *>(event);
455  if (mouseEvent->button() == Qt::RightButton)
456  {
457 // kDebug(5001) << "TransfersViewDelegate::editorEvent() -> rightClick";
458 
459  KMenu *popup = 0;
460 
461  TransferTreeModel * transferTreeModel = KGet::model();
462 
463  ModelItem * item = transferTreeModel->itemFromIndex(index);
464  if (item->isGroup())
465  {
466 // kDebug(5001) << "isTransferGroup = true";
467  TransferGroupHandler * transferGroupHandler = item->asGroup()->groupHandler();
468 
469  popup = ContextMenu::createTransferGroupContextMenu(transferGroupHandler, qobject_cast<QWidget*>(this));
470  }
471  else
472  {
473 // kDebug(5001) << "isTransferGroup = false";
474 
475  TransferHandler * transferHandler = item->asTransfer()->transferHandler();
476 
477  popup = ContextMenu::createTransferContextMenu(transferHandler, qobject_cast<QWidget*>(this));
478  }
479 
480  if (popup) {
481  popup->exec(QCursor::pos());
482  popup->deleteLater();
483  }
484  }
485  }
486 
487  return false;
488 }
489 
490 #include "transfersviewdelegate.moc"
TransfersViewDelegate::paint
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
Definition: transfersviewdelegate.cpp:324
TransferTreeModel::Status
Definition: transfertreemodel.h:120
TransferHandler
Class TransferHandler:
Definition: transferhandler.h:48
TransferGroupHandler
Definition: transfergrouphandler.h:30
TransferGroupHandler::start
void start()
These are all JobQueue-related functions.
Definition: transfergrouphandler.cpp:38
transfertreemodel.h
ModelItem::asGroup
GroupModelItem * asGroup()
Definition: transfertreemodel.cpp:74
contextmenu.h
QWidget
KExtendableItemDelegate
ContextMenu::createTransferGroupContextMenu
KMenu * createTransferGroupContextMenu(TransferGroupHandler *handler, QWidget *parent)
Definition: contextmenu.cpp:124
JobQueue::Running
Definition: jobqueue.h:36
BasicTransfersViewDelegate::setEditorData
virtual void setEditorData(QWidget *editor, const QModelIndex &index) const
Definition: transfersviewdelegate.cpp:290
transferdetails.h
TransfersViewDelegate::sizeHint
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
Definition: transfersviewdelegate.cpp:424
BasicTransfersViewDelegate::createEditor
virtual QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
Definition: transfersviewdelegate.cpp:274
GroupStatusEditor::changedStatus
void changedStatus(GroupStatusEditor *editor)
TransferTreeModel
Definition: transfertreemodel.h:108
GroupStatusButton
Definition: transfersviewdelegate.h:28
TransferTreeModel::itemFromIndex
ModelItem * itemFromIndex(const QModelIndex &index) const
Definition: transfertreemodel.cpp:391
BasicTransfersViewDelegate
The BasicTransfersViewDelegate handles the setting of the status of a group.
Definition: transfersviewdelegate.h:81
GroupStatusEditor
Definition: transfersviewdelegate.h:52
GroupStatusButton::paintEvent
void paintEvent(QPaintEvent *event)
Definition: transfersviewdelegate.cpp:100
KGet::model
static TransferTreeModel * model()
Definition: kget.cpp:487
KGet::selectionModel
static TransferTreeSelectionModel * selectionModel()
Definition: kget.cpp:492
transfergrouphandler.h
GroupStatusButton::leaveEvent
void leaveEvent(QEvent *event)
Definition: transfersviewdelegate.cpp:93
transfertreeselectionmodel.h
TransfersViewDelegate::TransfersViewDelegate
TransfersViewDelegate(QAbstractItemView *parent)
Definition: transfersviewdelegate.cpp:316
GroupModelItem::groupHandler
TransferGroupHandler * groupHandler()
Definition: transfertreemodel.cpp:178
TransfersViewDelegate::editorEvent
bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
Definition: transfersviewdelegate.cpp:447
GroupStatusEditor::GroupStatusEditor
GroupStatusEditor(const QModelIndex &index, QWidget *parent)
Definition: transfersviewdelegate.cpp:208
ContextMenu::createTransferContextMenu
KMenu * createTransferContextMenu(QList< TransferHandler * > transfer, QWidget *parent)
Definition: contextmenu.cpp:32
QAbstractItemModel
GroupStatusButton::enterEvent
void enterEvent(QEvent *event)
Definition: transfersviewdelegate.cpp:76
transferhandler.h
ModelItem
Definition: transfertreemodel.h:59
TransferHandler::percent
int percent() const
Definition: transferhandler.cpp:101
BasicTransfersViewDelegate::setModelData
virtual void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
Definition: transfersviewdelegate.cpp:300
settings.h
BasicTransfersViewDelegate::BasicTransfersViewDelegate
BasicTransfersViewDelegate(QAbstractItemView *parent)
Definition: transfersviewdelegate.cpp:269
GroupStatusButton::GroupStatusButton
GroupStatusButton(const QModelIndex &index, QWidget *parent)
Definition: transfersviewdelegate.cpp:40
GroupStatusEditor::setRunning
void setRunning(bool running)
Definition: transfersviewdelegate.cpp:247
kget.h
TransferGroupHandler::stop
void stop()
Definition: transfergrouphandler.cpp:44
ModelItem::asTransfer
TransferModelItem * asTransfer()
Definition: transfertreemodel.cpp:79
transfersviewdelegate.h
GroupStatusEditor::isRunning
bool isRunning()
Definition: transfersviewdelegate.cpp:258
QToolButton
TransfersViewDelegate::drawFocus
void drawFocus(QPainter *painter, const QStyleOptionViewItem &option, const QRect &rect) const
Definition: transfersviewdelegate.cpp:416
TransferModelItem::transferHandler
TransferHandler * transferHandler()
Definition: transfertreemodel.cpp:138
GroupStatusButton::timerEvent
void timerEvent(QTimerEvent *event)
Definition: transfersviewdelegate.cpp:148
GroupStatusButton::checkStateSet
void checkStateSet()
Definition: transfersviewdelegate.cpp:51
ModelItem::isGroup
virtual bool isGroup()
Definition: transfertreemodel.cpp:69
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:53:18 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kget

Skip menu "kget"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdenetwork API Reference

Skip menu "kdenetwork API Reference"
  • kget
  • kopete
  •   kopete
  •   libkopete
  • krdc
  • krfb

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal