Enable customizing the save statistics time interval
[qBittorrent.git] / src / gui / torrentcontentitemdelegate.cpp
blob4bc015301c60b9baab49f83fdd1a7cc097e0e02f
1 /*
2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2022 Vladimir Golovnev <glassez@yandex.ru>
4 * Copyright (C) 2006 Christophe Dumez <chris@qbittorrent.org>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * In addition, as a special exception, the copyright holders give permission to
21 * link this program with the OpenSSL project's "OpenSSL" library (or with
22 * modified versions of it that use the same license as the "OpenSSL" library),
23 * and distribute the linked executables. You must obey the GNU General Public
24 * License in all respects for all of the code used other than "OpenSSL". If you
25 * modify file(s), you may extend this exception to your version of the file(s),
26 * but you are not obligated to do so. If you do not wish to do so, delete this
27 * exception statement from your version.
30 #include "torrentcontentitemdelegate.h"
32 #include <QComboBox>
33 #include <QModelIndex>
34 #include <QPainter>
35 #include <QProgressBar>
37 #include "base/bittorrent/downloadpriority.h"
38 #include "base/bittorrent/torrent.h"
39 #include "gui/torrentcontentmodel.h"
41 TorrentContentItemDelegate::TorrentContentItemDelegate(QWidget *parent)
42 : QStyledItemDelegate(parent)
46 void TorrentContentItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
48 auto *combobox = static_cast<QComboBox *>(editor);
49 // Set combobox index
50 const int priority = index.data(TorrentContentModel::UnderlyingDataRole).toInt();
51 switch (static_cast<BitTorrent::DownloadPriority>(priority))
53 case BitTorrent::DownloadPriority::Ignored:
54 combobox->setCurrentIndex(0);
55 break;
56 case BitTorrent::DownloadPriority::High:
57 combobox->setCurrentIndex(2);
58 break;
59 case BitTorrent::DownloadPriority::Maximum:
60 combobox->setCurrentIndex(3);
61 break;
62 case BitTorrent::DownloadPriority::Mixed:
63 combobox->setCurrentIndex(4);
64 break;
65 default:
66 combobox->setCurrentIndex(1);
67 break;
71 QWidget *TorrentContentItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &, const QModelIndex &index) const
73 if (index.column() != TorrentContentModelItem::COL_PRIO)
74 return nullptr;
76 auto *editor = new QComboBox(parent);
77 editor->setFocusPolicy(Qt::StrongFocus);
78 editor->addItem(tr("Do not download", "Do not download (priority)"));
79 editor->addItem(tr("Normal", "Normal (priority)"));
80 editor->addItem(tr("High", "High (priority)"));
81 editor->addItem(tr("Maximum", "Maximum (priority)"));
83 // add Mixed priority item to the new combobox only for those items with Mixed priority
84 const auto priority = static_cast<BitTorrent::DownloadPriority>(index.data(TorrentContentModel::UnderlyingDataRole).toInt());
85 if (priority == BitTorrent::DownloadPriority::Mixed)
87 editor->addItem(tr("Mixed", "Mixed (priorities)"));
90 connect(editor, qOverload<int>(&QComboBox::currentIndexChanged), this, [this, editor]()
92 emit const_cast<TorrentContentItemDelegate *>(this)->commitData(editor);
93 });
95 return editor;
98 void TorrentContentItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
100 const auto *combobox = static_cast<QComboBox *>(editor);
102 BitTorrent::DownloadPriority prio = BitTorrent::DownloadPriority::Normal; // NORMAL
103 switch (combobox->currentIndex())
105 case 0:
106 prio = BitTorrent::DownloadPriority::Ignored; // IGNORED
107 break;
108 case 2:
109 prio = BitTorrent::DownloadPriority::High; // HIGH
110 break;
111 case 3:
112 prio = BitTorrent::DownloadPriority::Maximum; // MAX
113 break;
114 case 4:
115 prio = BitTorrent::DownloadPriority::Mixed; // MIXED
116 break;
119 const int newPriority = static_cast<int>(prio);
120 const int previousPriority = index.data(TorrentContentModel::UnderlyingDataRole).toInt();
122 if (newPriority != previousPriority)
124 model->setData(index, newPriority);
128 void TorrentContentItemDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &) const
130 editor->setGeometry(option.rect);
133 void TorrentContentItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
135 switch (index.column())
137 case TorrentContentModelItem::COL_PROGRESS:
139 const int progress = static_cast<int>(index.data(TorrentContentModel::UnderlyingDataRole).toReal());
140 const int priority = index.sibling(index.row(), TorrentContentModelItem::COL_PRIO).data(TorrentContentModel::UnderlyingDataRole).toInt();
141 const bool isEnabled = static_cast<BitTorrent::DownloadPriority>(priority) != BitTorrent::DownloadPriority::Ignored;
143 QStyleOptionViewItem customOption {option};
144 customOption.state.setFlag(QStyle::State_Enabled, isEnabled);
146 m_progressBarPainter.paint(painter, customOption, index.data().toString(), progress);
148 break;
149 default:
150 QStyledItemDelegate::paint(painter, option, index);
151 break;