qBittorrent
proplistdelegate.cpp
Go to the documentation of this file.
1 /*
2  * Bittorrent Client using Qt and libtorrent.
3  * Copyright (C) 2006 Christophe Dumez <chris@qbittorrent.org>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  *
19  * In addition, as a special exception, the copyright holders give permission to
20  * link this program with the OpenSSL project's "OpenSSL" library (or with
21  * modified versions of it that use the same license as the "OpenSSL" library),
22  * and distribute the linked executables. You must obey the GNU General Public
23  * License in all respects for all of the code used other than "OpenSSL". If you
24  * modify file(s), you may extend this exception to your version of the file(s),
25  * but you are not obligated to do so. If you do not wish to do so, delete this
26  * exception statement from your version.
27  */
28 
29 #include "proplistdelegate.h"
30 
31 #include <QComboBox>
32 #include <QModelIndex>
33 #include <QPainter>
34 #include <QProgressBar>
35 
39 #include "propertieswidget.h"
40 
42  : QStyledItemDelegate {properties}
43  , m_properties {properties}
44 {
45 }
46 
47 void PropListDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
48 {
49  auto *combobox = static_cast<QComboBox *>(editor);
50  // Set combobox index
51  const int priority = index.data(TorrentContentModel::UnderlyingDataRole).toInt();
52  switch (static_cast<BitTorrent::DownloadPriority>(priority))
53  {
55  combobox->setCurrentIndex(0);
56  break;
58  combobox->setCurrentIndex(2);
59  break;
61  combobox->setCurrentIndex(3);
62  break;
63  default:
64  combobox->setCurrentIndex(1);
65  break;
66  }
67 }
68 
69 QWidget *PropListDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &, const QModelIndex &index) const
70 {
71  if (index.column() != PRIORITY)
72  return nullptr;
73 
74  if (m_properties)
75  {
77  if (!torrent || !torrent->hasMetadata() || torrent->isSeed())
78  return nullptr;
79  }
80 
81  const int priority = index.data(TorrentContentModel::UnderlyingDataRole).toInt();
83  return nullptr;
84 
85  auto *editor = new QComboBox(parent);
86  editor->setFocusPolicy(Qt::StrongFocus);
87  editor->addItem(tr("Do not download", "Do not download (priority)"));
88  editor->addItem(tr("Normal", "Normal (priority)"));
89  editor->addItem(tr("High", "High (priority)"));
90  editor->addItem(tr("Maximum", "Maximum (priority)"));
91 
92  connect(editor, qOverload<int>(&QComboBox::currentIndexChanged), this, [this, editor]()
93  {
94  emit const_cast<PropListDelegate *>(this)->commitData(editor);
95  });
96 
97  return editor;
98 }
99 
100 void PropListDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
101 {
102  const auto *combobox = static_cast<QComboBox *>(editor);
103 
105  switch (combobox->currentIndex())
106  {
107  case 0:
108  prio = BitTorrent::DownloadPriority::Ignored; // IGNORED
109  break;
110  case 2:
111  prio = BitTorrent::DownloadPriority::High; // HIGH
112  break;
113  case 3:
115  break;
116  }
117 
118  const int newPriority = static_cast<int>(prio);
119  const int previousPriority = index.data(TorrentContentModel::UnderlyingDataRole).toInt();
120 
121  if (newPriority != previousPriority)
122  {
123  model->setData(index, newPriority);
124  emit filteredFilesChanged();
125  }
126 }
127 
128 void PropListDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &) const
129 {
130  editor->setGeometry(option.rect);
131 }
132 
133 void PropListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
134 {
135  switch (index.column())
136  {
138  {
139  const int progress = static_cast<int>(index.data(TorrentContentModel::UnderlyingDataRole).toReal());
140  const int priority = index.sibling(index.row(), PropColumn::PRIORITY).data(TorrentContentModel::UnderlyingDataRole).toInt();
141  const bool isEnabled = static_cast<BitTorrent::DownloadPriority>(priority) != BitTorrent::DownloadPriority::Ignored;
142 
143  QStyleOptionViewItem customOption {option};
144  customOption.state.setFlag(QStyle::State_Enabled, isEnabled);
145 
146  m_progressBarPainter.paint(painter, customOption, index.data().toString(), progress);
147  }
148  break;
149  default:
150  QStyledItemDelegate::paint(painter, option, index);
151  break;
152  }
153 }
virtual bool isSeed() const =0
virtual bool hasMetadata() const =0
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QString &text, int progress) const
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const override
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override
PropListDelegate(PropertiesWidget *properties)
QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override
void setEditorData(QWidget *editor, const QModelIndex &index) const override
ProgressBarPainter m_progressBarPainter
void filteredFilesChanged() const
PropertiesWidget * m_properties
BitTorrent::Torrent * getCurrentTorrent() const
@ PRIORITY
@ PROGRESS