2015-06-02 12:09:15 +03:00
|
|
|
/*
|
|
|
|
* Bittorrent Client using Qt and libtorrent.
|
2017-10-26 10:10:30 +03:00
|
|
|
* Copyright (C) 2006 Christophe Dumez <chris@qbittorrent.org>
|
2015-06-02 12:09:15 +03:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
* In addition, as a special exception, the copyright holders give permission to
|
|
|
|
* link this program with the OpenSSL project's "OpenSSL" library (or with
|
|
|
|
* modified versions of it that use the same license as the "OpenSSL" library),
|
|
|
|
* and distribute the linked executables. You must obey the GNU General Public
|
|
|
|
* License in all respects for all of the code used other than "OpenSSL". If you
|
|
|
|
* modify file(s), you may extend this exception to your version of the file(s),
|
|
|
|
* but you are not obligated to do so. If you do not wish to do so, delete this
|
|
|
|
* exception statement from your version.
|
|
|
|
*/
|
|
|
|
|
2017-04-11 15:35:10 +03:00
|
|
|
#include "proplistdelegate.h"
|
|
|
|
|
|
|
|
#include <QApplication>
|
2015-06-02 12:09:15 +03:00
|
|
|
#include <QComboBox>
|
|
|
|
#include <QModelIndex>
|
|
|
|
#include <QPainter>
|
2016-12-25 01:18:37 +03:00
|
|
|
#include <QPalette>
|
2015-06-02 12:09:15 +03:00
|
|
|
#include <QProgressBar>
|
2016-12-25 01:18:37 +03:00
|
|
|
#include <QStyleOptionProgressBar>
|
2015-06-02 12:09:15 +03:00
|
|
|
|
2019-01-02 10:01:32 +03:00
|
|
|
#if defined(Q_OS_WIN) || defined(Q_OS_MACOS)
|
2015-06-02 12:09:15 +03:00
|
|
|
#include <QProxyStyle>
|
|
|
|
#endif
|
|
|
|
|
2019-03-06 08:58:07 +03:00
|
|
|
#include "base/bittorrent/downloadpriority.h"
|
2019-07-13 08:03:46 +03:00
|
|
|
#include "base/bittorrent/torrenthandle.h"
|
2020-10-24 10:27:46 +03:00
|
|
|
#include "gui/torrentcontentmodel.h"
|
2015-06-02 12:09:15 +03:00
|
|
|
#include "propertieswidget.h"
|
|
|
|
|
2017-04-05 10:21:12 +03:00
|
|
|
namespace
|
|
|
|
{
|
2016-12-25 01:18:37 +03:00
|
|
|
QPalette progressBarDisabledPalette()
|
|
|
|
{
|
2019-08-08 16:32:27 +03:00
|
|
|
static const QPalette palette = []()
|
|
|
|
{
|
|
|
|
QProgressBar bar;
|
|
|
|
bar.setEnabled(false);
|
|
|
|
QStyleOptionProgressBar opt;
|
|
|
|
opt.initFrom(&bar);
|
|
|
|
return opt.palette;
|
|
|
|
}();
|
2016-12-25 01:18:37 +03:00
|
|
|
return palette;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-08 08:01:59 +03:00
|
|
|
PropListDelegate::PropListDelegate(PropertiesWidget *properties)
|
2020-10-24 10:27:46 +03:00
|
|
|
: ProgressBarDelegate {PROGRESS, TorrentContentModel::UnderlyingDataRole, properties}
|
2015-06-02 12:09:15 +03:00
|
|
|
, m_properties(properties)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-10-24 10:27:46 +03:00
|
|
|
void PropListDelegate::initProgressStyleOption(QStyleOptionProgressBar &option, const QModelIndex &index) const
|
2015-06-02 12:09:15 +03:00
|
|
|
{
|
2020-10-24 10:27:46 +03:00
|
|
|
ProgressBarDelegate::initProgressStyleOption(option, index);
|
|
|
|
const int priority
|
|
|
|
= index.sibling(index.row(), PRIORITY).data(TorrentContentModel::UnderlyingDataRole).toInt();
|
|
|
|
if (static_cast<BitTorrent::DownloadPriority>(priority) == BitTorrent::DownloadPriority::Ignored) {
|
|
|
|
option.state &= ~QStyle::State_Enabled;
|
|
|
|
option.palette = progressBarDisabledPalette();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
option.state |= QStyle::State_Enabled;
|
2015-06-02 12:09:15 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PropListDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
|
|
|
|
{
|
2019-02-13 18:12:02 +03:00
|
|
|
auto *combobox = static_cast<QComboBox *>(editor);
|
2015-06-02 12:09:15 +03:00
|
|
|
// Set combobox index
|
2020-10-24 10:27:46 +03:00
|
|
|
const int priority = index.data(TorrentContentModel::UnderlyingDataRole).toInt();
|
|
|
|
switch (static_cast<BitTorrent::DownloadPriority>(priority)) {
|
2019-03-06 08:58:07 +03:00
|
|
|
case BitTorrent::DownloadPriority::Ignored:
|
2016-12-25 02:12:05 +03:00
|
|
|
combobox->setCurrentIndex(0);
|
|
|
|
break;
|
2019-03-06 08:58:07 +03:00
|
|
|
case BitTorrent::DownloadPriority::High:
|
2016-12-25 02:12:05 +03:00
|
|
|
combobox->setCurrentIndex(2);
|
2015-06-02 12:09:15 +03:00
|
|
|
break;
|
2019-03-06 08:58:07 +03:00
|
|
|
case BitTorrent::DownloadPriority::Maximum:
|
2016-12-25 02:12:05 +03:00
|
|
|
combobox->setCurrentIndex(3);
|
2015-06-02 12:09:15 +03:00
|
|
|
break;
|
|
|
|
default:
|
2016-12-25 02:12:05 +03:00
|
|
|
combobox->setCurrentIndex(1);
|
2015-06-02 12:09:15 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QWidget *PropListDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &, const QModelIndex &index) const
|
|
|
|
{
|
2017-10-26 10:10:30 +03:00
|
|
|
if (index.column() != PRIORITY) return nullptr;
|
2015-06-02 12:09:15 +03:00
|
|
|
|
|
|
|
if (m_properties) {
|
2019-08-08 16:32:27 +03:00
|
|
|
const BitTorrent::TorrentHandle *torrent = m_properties->getCurrentTorrent();
|
2015-06-02 12:09:15 +03:00
|
|
|
if (!torrent || !torrent->hasMetadata() || torrent->isSeed())
|
2017-10-26 10:10:30 +03:00
|
|
|
return nullptr;
|
2015-06-02 12:09:15 +03:00
|
|
|
}
|
|
|
|
|
2020-10-24 10:27:46 +03:00
|
|
|
const int priority = index.data(TorrentContentModel::UnderlyingDataRole).toInt();
|
|
|
|
if (static_cast<BitTorrent::DownloadPriority>(priority) == BitTorrent::DownloadPriority::Mixed)
|
2017-10-26 10:10:30 +03:00
|
|
|
return nullptr;
|
2015-06-02 12:09:15 +03:00
|
|
|
|
2019-02-13 18:12:02 +03:00
|
|
|
auto *editor = new QComboBox(parent);
|
2015-06-02 12:09:15 +03:00
|
|
|
editor->setFocusPolicy(Qt::StrongFocus);
|
2016-12-25 02:12:05 +03:00
|
|
|
editor->addItem(tr("Do not download", "Do not download (priority)"));
|
2015-06-02 12:09:15 +03:00
|
|
|
editor->addItem(tr("Normal", "Normal (priority)"));
|
|
|
|
editor->addItem(tr("High", "High (priority)"));
|
|
|
|
editor->addItem(tr("Maximum", "Maximum (priority)"));
|
|
|
|
return editor;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PropListDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
|
|
|
|
{
|
2019-08-08 16:32:27 +03:00
|
|
|
const auto *combobox = static_cast<QComboBox *>(editor);
|
|
|
|
const int value = combobox->currentIndex();
|
2015-06-02 12:09:15 +03:00
|
|
|
|
2019-03-06 08:58:07 +03:00
|
|
|
BitTorrent::DownloadPriority prio = BitTorrent::DownloadPriority::Normal; // NORMAL
|
2017-04-05 10:21:12 +03:00
|
|
|
switch (value) {
|
2016-12-25 02:12:05 +03:00
|
|
|
case 0:
|
2019-03-06 08:58:07 +03:00
|
|
|
prio = BitTorrent::DownloadPriority::Ignored; // IGNORED
|
2015-06-02 12:09:15 +03:00
|
|
|
break;
|
|
|
|
case 2:
|
2019-03-06 08:58:07 +03:00
|
|
|
prio = BitTorrent::DownloadPriority::High; // HIGH
|
2016-12-25 02:12:05 +03:00
|
|
|
break;
|
|
|
|
case 3:
|
2019-03-06 08:58:07 +03:00
|
|
|
prio = BitTorrent::DownloadPriority::Maximum; // MAX
|
2015-06-02 12:09:15 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-10-22 01:58:11 +03:00
|
|
|
model->setData(index, static_cast<int>(prio));
|
2015-06-02 12:09:15 +03:00
|
|
|
emit filteredFilesChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PropListDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &) const
|
|
|
|
{
|
|
|
|
editor->setGeometry(option.rect);
|
|
|
|
}
|