• 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
  • history
transferhistoryitemdelegate.cpp
Go to the documentation of this file.
1 /* This file is part of the KDE project
2 
3  Based in the kcategorizeditemsviewdelegate from kdebase/workspace/libs/plasma/appletbrowser by Ivan Cukic
4  Copyright (C) 2008 Javier Goday <jgoday@gmail.com>
5 
6  This program is free software; you can redistribute it and/or
7  modify it under the terms of the GNU General Public
8  License as published by the Free Software Foundation; either
9  version 2 of the License, or (at your option) any later version.
10 */
11 
12 #include "transferhistoryitemdelegate.h"
13 #include "ui/history/transferhistorycategorizeddelegate.h"
14 #include "ui/history/transferhistorycategorizedview.h"
15 #include "ui/newtransferdialog.h"
16 
17 #include <QAbstractItemModel>
18 #include <QAction>
19 #include <QApplication>
20 #include <QDate>
21 #include <QMenu>
22 #include <QModelIndex>
23 #include <QMouseEvent>
24 #include <QPainter>
25 
26 #include <KDebug>
27 #include <kio/netaccess.h>
28 #include <kio/global.h>
29 #include <KIconLoader>
30 #include <KIcon>
31 #include <KLocale>
32 #include <KRun>
33 
34 TransferHistoryItemDelegate::TransferHistoryItemDelegate(QWidget *parent) : QStyledItemDelegate(),
35  m_selectedIndex()
36 {
37  m_view = parent;
38 
39  // Actions
40  m_actionDownload = new QAction(this);
41  m_actionDownload->setText(i18n("Download again"));
42  m_actionDownload->setIcon(KIcon("document-new"));
43  connect(m_actionDownload, SIGNAL(triggered()), SLOT(slotDownload()));
44 
45  m_actionDelete_Selected = new QAction(this);
46  m_actionDelete_Selected->setText(i18nc("Delete selected history-item", "Delete selected"));
47  m_actionDelete_Selected->setIcon(KIcon("edit-delete"));
48  connect(m_actionDelete_Selected, SIGNAL(triggered()), SLOT(slotDeleteTransfer()));
49 
50  m_openFile = new QAction(this);
51  m_openFile->setText(i18n("Open file"));
52  m_openFile->setIcon(KIcon("document-open"));
53  connect(m_openFile, SIGNAL(triggered()), SLOT(slotOpenFile()));
54 }
55 
56 TransferHistoryItemDelegate::~TransferHistoryItemDelegate()
57 {
58 }
59 
60 void TransferHistoryItemDelegate::paint(QPainter *painter,
61  const QStyleOptionViewItem &option, const QModelIndex &index) const
62 {
63  if (!option.state.testFlag(QStyle::State_Selected) && !option.state.testFlag(QStyle::State_MouseOver)) {
64  // draw a separator
65  painter->save();
66 
67  QRect roundRect(option.rect.left() + 1, option.rect.top() + 1,
68  option.rect.width() - 2, option.rect.height() - 2);
69 
70  QPainterPath path;
71  path.addRoundRect(roundRect, 2, 2);
72  QLinearGradient gradient(roundRect.left(), roundRect.top(),
73  roundRect.left(), roundRect.bottom());
74  gradient.setColorAt(0, Qt::transparent);
75  gradient.setColorAt(0.95, option.palette.color(QPalette::AlternateBase).darker(130));
76  QBrush brush(gradient);
77 
78  painter->fillPath(path, brush);
79 
80  painter->setPen(option.palette.color(QPalette::AlternateBase).darker(190));
81  painter->drawRoundRect(roundRect, 2, 2);
82  painter->restore();
83  }
84 
85  QStyleOptionViewItemV4 opt(option);
86  QStyle *style = opt.widget ? opt.widget->style() : QApplication::style();
87  style->drawPrimitive(QStyle::PE_PanelItemViewItem, &opt, painter, opt.widget);
88 
89  const QAbstractItemModel *model = static_cast <const QAbstractItemModel *> (index.model());
90  QUrl url(model->data(index, TransferHistoryCategorizedDelegate::RoleUrl).toString());
91  QString name = url.path().mid(url.path().lastIndexOf("/") + 1);
92  KIcon icon(KIO::pixmapForUrl(
93  model->data(index, TransferHistoryCategorizedDelegate::RoleDest).toString(),
94  0, KIconLoader::Panel));
95  QString size = KIO::convertSize(model->data(index, TransferHistoryCategorizedDelegate::RoleSize).toInt());
96  QString date = model->data(index, TransferHistoryCategorizedDelegate::RoleDate).toDate().toString("dd.MM.yyyy");
97  QString host = url.host();
98 
99  // draw the host
100  painter->save();
101  painter->setPen(option.palette.color(QPalette::Link));
102  // draw the host
103  painter->drawText(option.rect.left() + PADDING,
104  option.rect.top() + PADDING,
105  option.rect.width() - PADDING * 2, 15,
106  Qt::AlignTop | Qt::AlignLeft, host);
107  painter->restore();
108 
109  // draw the transfer icon
110  icon.paint(painter,
111  option.rect.left() + option.rect.width() / 2 - ICON_SIZE / 2,
112  option.rect.top() + option.rect.height() / 2 - ICON_SIZE / 2 - PADDING * 2,
113  ICON_SIZE, ICON_SIZE, Qt::AlignCenter, QIcon::Active);
114 
115  painter->save();
116  QColor subcolor = (option.state.testFlag(QStyle::State_Selected) || (option.state.testFlag(QStyle::State_MouseOver))) ?
117  option.palette.color(QPalette::Text) :
118  option.palette.color(QPalette::BrightText);
119 
120  // draw a separator line between the file name and his size and date
121  painter->setPen(option.palette.color(QPalette::AlternateBase).darker(190));
122  painter->drawLine(option.rect.left() + 2, option.rect.bottom() + PADDING - 27,
123  option.rect.right() - 2, option.rect.bottom() + PADDING - 27);
124  painter->setPen(subcolor);
125  // draw the size
126  painter->drawText(option.rect.right() - PADDING - 100,
127  option.rect.bottom() + PADDING - 25,
128  100 - PADDING * 2, 25,
129  Qt::AlignTop | Qt::AlignRight, size);
130 
131  // draw the date
132  painter->drawText(option.rect.left() + PADDING,
133  option.rect.bottom() + PADDING - 25,
134  100 - PADDING * 2, 25,
135  Qt::AlignTop | Qt::AlignLeft, date);
136 
137  painter->restore();
138 
139  // draw the filenamne
140  painter->save();
141  QColor foregroundColor = (option.state.testFlag(QStyle::State_Selected)) ?
142  option.palette.color(QPalette::HighlightedText):option.palette.color(QPalette::Text);
143  painter->setPen(foregroundColor);
144  painter->setFont(option.font);
145  painter->drawText(option.rect.left() + PADDING,
146  option.rect.bottom() - PADDING - 35,
147  200 - PADDING * 2, 15,
148  Qt::AlignBottom | Qt::AlignCenter, name);
149  painter->restore();
150 }
151 
152 QSize TransferHistoryItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
153 {
154  Q_UNUSED(option)
155  Q_UNUSED(index)
156 
157  return QSize(200, 110);
158 }
159 
160 bool TransferHistoryItemDelegate::editorEvent(QEvent * event, QAbstractItemModel * model,
161  const QStyleOptionViewItem &option, const QModelIndex & index)
162 {
163  Q_UNUSED(option)
164  Q_UNUSED(model)
165 
166  QMouseEvent * mouseEvent = dynamic_cast<QMouseEvent *>(event);
167 
168  if(mouseEvent && index.isValid()) {
169  if(mouseEvent->button() == Qt::RightButton) {
170  m_selectedIndex = index;
171 
172  QMenu *contextMenu = new QMenu();
173  contextMenu->addAction(m_actionDownload);
174  contextMenu->addAction(m_actionDelete_Selected);
175  contextMenu->addAction(m_openFile);
176 
177  contextMenu->exec(QCursor::pos());
178  }
179  }
180 
181  return false;
182 }
183 
184 
185 void TransferHistoryItemDelegate::slotOpenFile()
186 {
187  const QAbstractItemModel *model = static_cast <const QAbstractItemModel *> (m_selectedIndex.model());
188 
189  new KRun(model->data(m_selectedIndex, TransferHistoryCategorizedDelegate::RoleDest).toString(), m_view, true, false);
190 }
191 
192 void TransferHistoryItemDelegate::slotDownload()
193 {
194  const QAbstractItemModel *model = static_cast <const QAbstractItemModel *> (m_selectedIndex.model());
195 
196  NewTransferDialogHandler::showNewTransferDialog(model->data(m_selectedIndex,
197  TransferHistoryCategorizedDelegate::RoleUrl).toString());
198 }
199 
200 void TransferHistoryItemDelegate::slotDeleteTransfer()
201 {
202  const QAbstractItemModel *model = static_cast <const QAbstractItemModel *> (m_selectedIndex.model());
203 
204  emit deletedTransfer(model->data(m_selectedIndex, TransferHistoryCategorizedDelegate::RoleUrl).toString(), m_selectedIndex);
205 }
206 
207 #include "transferhistoryitemdelegate.moc"
TransferHistoryItemDelegate::editorEvent
bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
Definition: transferhistoryitemdelegate.cpp:160
PADDING
#define PADDING
Definition: transferhistoryitemdelegate.h:18
TransferHistoryItemDelegate::deletedTransfer
void deletedTransfer(const QString &url, const QModelIndex &index)
TransferHistoryCategorizedDelegate::RoleDate
Definition: transferhistorycategorizeddelegate.h:24
QWidget
TransferHistoryItemDelegate::~TransferHistoryItemDelegate
~TransferHistoryItemDelegate()
Definition: transferhistoryitemdelegate.cpp:56
TransferHistoryCategorizedDelegate::RoleDest
Definition: transferhistorycategorizeddelegate.h:26
QStyledItemDelegate
transferhistorycategorizedview.h
TransferHistoryItemDelegate::sizeHint
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
Definition: transferhistoryitemdelegate.cpp:152
TransferHistoryCategorizedDelegate::RoleSize
Definition: transferhistorycategorizeddelegate.h:23
TransferHistoryCategorizedDelegate::RoleUrl
Definition: transferhistorycategorizeddelegate.h:25
newtransferdialog.h
transferhistorycategorizeddelegate.h
QAbstractItemModel
TransferHistoryItemDelegate::TransferHistoryItemDelegate
TransferHistoryItemDelegate(QWidget *parent)
Definition: transferhistoryitemdelegate.cpp:34
NewTransferDialogHandler::showNewTransferDialog
static void showNewTransferDialog(const KUrl &url=KUrl())
Definition: newtransferdialog.cpp:493
TransferHistoryItemDelegate::paint
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
Definition: transferhistoryitemdelegate.cpp:60
ICON_SIZE
#define ICON_SIZE
Definition: transferhistoryitemdelegate.h:17
transferhistoryitemdelegate.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:53:17 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