Enable customizing the save statistics time interval
[qBittorrent.git] / src / gui / torrentcontentmodelitem.cpp
blobd226d0852cbf158d6e0291188095af8137a071a4
1 /*
2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2006-2012 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 "torrentcontentmodelitem.h"
31 #include <QVariant>
33 #include "base/unicodestrings.h"
34 #include "base/utils/misc.h"
35 #include "base/utils/string.h"
36 #include "torrentcontentmodelfolder.h"
38 TorrentContentModelItem::TorrentContentModelItem(TorrentContentModelFolder *parent)
39 : m_parentItem(parent)
43 TorrentContentModelItem::~TorrentContentModelItem() = default;
45 bool TorrentContentModelItem::isRootItem() const
47 return !m_parentItem;
50 QString TorrentContentModelItem::name() const
52 Q_ASSERT(!isRootItem());
53 return m_name;
56 void TorrentContentModelItem::setName(const QString &name)
58 Q_ASSERT(!isRootItem());
59 m_name = name;
62 qulonglong TorrentContentModelItem::size() const
64 Q_ASSERT(!isRootItem());
66 return m_size;
69 qreal TorrentContentModelItem::progress() const
71 Q_ASSERT(!isRootItem());
73 return (m_size > 0) ? m_progress : 1;
76 qulonglong TorrentContentModelItem::remaining() const
78 Q_ASSERT(!isRootItem());
79 return (m_priority == BitTorrent::DownloadPriority::Ignored) ? 0 : m_remaining;
82 qreal TorrentContentModelItem::availability() const
84 Q_ASSERT(!isRootItem());
86 return (m_size > 0) ? m_availability : 0;
89 BitTorrent::DownloadPriority TorrentContentModelItem::priority() const
91 Q_ASSERT(!isRootItem());
92 return m_priority;
95 int TorrentContentModelItem::columnCount() const
97 return NB_COL;
100 QString TorrentContentModelItem::displayData(const int column) const
102 if (isRootItem())
103 return m_itemData.value(column);
105 switch (column)
107 case COL_NAME:
108 return m_name;
109 case COL_PRIO:
110 switch (m_priority)
112 case BitTorrent::DownloadPriority::Mixed:
113 return tr("Mixed", "Mixed (priorities");
114 case BitTorrent::DownloadPriority::Ignored:
115 return tr("Not downloaded");
116 case BitTorrent::DownloadPriority::High:
117 return tr("High", "High (priority)");
118 case BitTorrent::DownloadPriority::Maximum:
119 return tr("Maximum", "Maximum (priority)");
120 default:
121 return tr("Normal", "Normal (priority)");
123 case COL_PROGRESS:
124 return (m_progress >= 1)
125 ? u"100%"_s
126 : (Utils::String::fromDouble((m_progress * 100), 1) + u'%');
127 case COL_SIZE:
128 return Utils::Misc::friendlyUnit(m_size);
129 case COL_REMAINING:
130 return Utils::Misc::friendlyUnit(remaining());
131 case COL_AVAILABILITY:
133 const qreal avail = availability();
134 if (avail < 0)
135 return tr("N/A");
137 const QString value = (avail >= 1)
138 ? u"100"_s
139 : Utils::String::fromDouble((avail * 100), 1);
140 return (value + u'%');
142 default:
143 Q_ASSERT(false);
144 return {};
148 QVariant TorrentContentModelItem::underlyingData(const int column) const
150 if (isRootItem())
151 return m_itemData.value(column);
153 switch (column)
155 case COL_NAME:
156 return m_name;
157 case COL_PRIO:
158 return static_cast<int>(m_priority);
159 case COL_PROGRESS:
160 return progress() * 100;
161 case COL_SIZE:
162 return m_size;
163 case COL_REMAINING:
164 return remaining();
165 case COL_AVAILABILITY:
166 return availability();
167 default:
168 Q_ASSERT(false);
169 return {};
173 int TorrentContentModelItem::row() const
175 if (m_parentItem)
176 return m_parentItem->children().indexOf(const_cast<TorrentContentModelItem *>(this));
177 return 0;
180 TorrentContentModelFolder *TorrentContentModelItem::parent() const
182 return m_parentItem;