Enable customizing the save statistics time interval
[qBittorrent.git] / src / gui / transferlistdelegate.cpp
blobe08cb7ef81e27e46aca8eb452837ec23d7882a7c
1 /*
2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2006 Christophe Dumez <chris@qbittorrent.org>
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.
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.
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.
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.
29 #include "transferlistdelegate.h"
31 #include <QModelIndex>
33 #include "transferlistmodel.h"
35 TransferListDelegate::TransferListDelegate(QObject *parent)
36 : QStyledItemDelegate {parent}
40 QWidget *TransferListDelegate::createEditor(QWidget *, const QStyleOptionViewItem &, const QModelIndex &) const
42 // No editor here
43 return nullptr;
46 QSize TransferListDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
48 // Reimplementing sizeHint() because the 'name' column contains text+icon.
49 // When that WHOLE column goes out of view(eg user scrolls horizontally)
50 // the rows shrink if the text's height is smaller than the icon's height.
51 // This happens because icon from the 'name' column is no longer drawn.
53 if (m_nameColHeight == -1)
55 const QModelIndex nameColumn = index.sibling(index.row(), TransferListModel::TR_NAME);
56 m_nameColHeight = QStyledItemDelegate::sizeHint(option, nameColumn).height();
59 QSize size = QStyledItemDelegate::sizeHint(option, index);
60 size.setHeight(std::max(m_nameColHeight, size.height()));
61 return size;
64 void TransferListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
66 switch (index.column())
68 case TransferListModel::TR_PROGRESS:
70 using namespace BitTorrent;
72 const auto isEnableState = [](const TorrentState state) -> bool
74 switch (state)
76 case TorrentState::Error:
77 case TorrentState::StoppedDownloading:
78 case TorrentState::Unknown:
79 return false;
80 default:
81 return true;
85 const int progress = static_cast<int>(index.data(TransferListModel::UnderlyingDataRole).toReal());
87 const QModelIndex statusIndex = index.siblingAtColumn(TransferListModel::TR_STATUS);
88 const auto torrentState = statusIndex.data(TransferListModel::UnderlyingDataRole).value<TorrentState>();
90 QStyleOptionViewItem customOption {option};
91 customOption.state.setFlag(QStyle::State_Enabled, isEnableState(torrentState));
93 m_progressBarPainter.paint(painter, customOption, index.data().toString(), progress);
95 break;
96 default:
97 QStyledItemDelegate::paint(painter, option, index);
98 break;