qBittorrent/src/gui/properties/proplistdelegate.cpp

142 lines
5.2 KiB
C++
Raw Normal View History

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.
*/
#include "proplistdelegate.h"
2015-06-02 12:09:15 +03:00
#include <QComboBox>
#include <QModelIndex>
#include <QPainter>
#include <QProgressBar>
#include "base/bittorrent/downloadpriority.h"
#include "base/bittorrent/torrent.h"
#include "gui/torrentcontentmodel.h"
2015-06-02 12:09:15 +03:00
#include "propertieswidget.h"
PropListDelegate::PropListDelegate(PropertiesWidget *properties)
2021-03-10 10:52:30 +03:00
: QStyledItemDelegate {properties}
, m_properties {properties}
2015-06-02 12:09:15 +03:00
{
}
void PropListDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
auto *combobox = static_cast<QComboBox *>(editor);
2015-06-02 12:09:15 +03:00
// Set combobox index
const int priority = index.data(TorrentContentModel::UnderlyingDataRole).toInt();
2020-11-16 10:02:11 +03:00
switch (static_cast<BitTorrent::DownloadPriority>(priority))
{
case BitTorrent::DownloadPriority::Ignored:
combobox->setCurrentIndex(0);
break;
case BitTorrent::DownloadPriority::High:
combobox->setCurrentIndex(2);
2015-06-02 12:09:15 +03:00
break;
case BitTorrent::DownloadPriority::Maximum:
combobox->setCurrentIndex(3);
2015-06-02 12:09:15 +03:00
break;
default:
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
2020-11-16 10:02:11 +03:00
if (m_properties)
{
const BitTorrent::Torrent *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
}
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
auto *editor = new QComboBox(parent);
2015-06-02 12:09:15 +03:00
editor->setFocusPolicy(Qt::StrongFocus);
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
BitTorrent::DownloadPriority prio = BitTorrent::DownloadPriority::Normal; // NORMAL
2020-11-16 10:02:11 +03:00
switch (value)
{
case 0:
prio = BitTorrent::DownloadPriority::Ignored; // IGNORED
2015-06-02 12:09:15 +03:00
break;
case 2:
prio = BitTorrent::DownloadPriority::High; // HIGH
break;
case 3:
prio = BitTorrent::DownloadPriority::Maximum; // MAX
2015-06-02 12:09:15 +03:00
break;
}
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);
}
2021-03-10 10:52:30 +03:00
void PropListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
switch (index.column())
{
case PropColumn::PROGRESS:
{
const int progress = static_cast<int>(index.data(TorrentContentModel::UnderlyingDataRole).toReal());
const int priority = index.sibling(index.row(), PropColumn::PRIORITY).data(TorrentContentModel::UnderlyingDataRole).toInt();
const bool isEnabled = static_cast<BitTorrent::DownloadPriority>(priority) != BitTorrent::DownloadPriority::Ignored;
QStyleOptionViewItem customOption {option};
customOption.state.setFlag(QStyle::State_Enabled, isEnabled);
m_progressBarPainter.paint(painter, customOption, index.data().toString(), progress);
}
break;
default:
QStyledItemDelegate::paint(painter, option, index);
break;
}
}