qBittorrent
torrentcontentmodelitem.cpp
Go to the documentation of this file.
1 /*
2  * Bittorrent Client using Qt and libtorrent.
3  * Copyright (C) 2006-2012 Christophe Dumez <chris@qbittorrent.org>
4  *
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.
9  *
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.
14  *
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.
18  *
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.
27  */
28 
30 
31 #include <QVariant>
32 
33 #include "base/unicodestrings.h"
34 #include "base/utils/misc.h"
35 #include "base/utils/string.h"
37 
39  : m_parentItem(parent)
40  , m_size(0)
41  , m_remaining(0)
42  , m_priority(BitTorrent::DownloadPriority::Normal)
43  , m_progress(0)
44  , m_availability(-1.)
45 {
46 }
47 
49 
51 {
52  return !m_parentItem;
53 }
54 
56 {
57  Q_ASSERT(!isRootItem());
58  return m_name;
59 }
60 
61 void TorrentContentModelItem::setName(const QString &name)
62 {
63  Q_ASSERT(!isRootItem());
64  m_name = name;
65 }
66 
68 {
69  Q_ASSERT(!isRootItem());
70 
71  return m_size;
72 }
73 
75 {
76  Q_ASSERT(!isRootItem());
77 
78  return (m_size > 0) ? m_progress : 1;
79 }
80 
82 {
83  Q_ASSERT(!isRootItem());
85 }
86 
88 {
89  Q_ASSERT(!isRootItem());
90 
91  return (m_size > 0) ? m_availability : 0;
92 }
93 
95 {
96  Q_ASSERT(!isRootItem());
97  return m_priority;
98 }
99 
101 {
102  return NB_COL;
103 }
104 
105 QString TorrentContentModelItem::displayData(const int column) const
106 {
107  if (isRootItem())
108  return m_itemData.value(column);
109 
110  switch (column)
111  {
112  case COL_NAME:
113  return m_name;
114  case COL_PRIO:
115  switch (m_priority)
116  {
118  return tr("Mixed", "Mixed (priorities");
120  return tr("Not downloaded");
122  return tr("High", "High (priority)");
124  return tr("Maximum", "Maximum (priority)");
125  default:
126  return tr("Normal", "Normal (priority)");
127  }
128  case COL_PROGRESS:
129  return (m_progress >= 1)
130  ? QString::fromLatin1("100%")
131  : (Utils::String::fromDouble((m_progress * 100), 1) + QLatin1Char('%'));
132  case COL_SIZE:
134  case COL_REMAINING:
136  case COL_AVAILABILITY:
137  {
138  const qreal avail = availability();
139  if (avail < 0)
140  return tr("N/A");
141 
142  const QString value = (avail >= 1)
143  ? QString::fromLatin1("100")
144  : Utils::String::fromDouble((avail * 100), 1);
145  return (value + C_THIN_SPACE + QLatin1Char('%'));
146  }
147  default:
148  Q_ASSERT(false);
149  return {};
150  }
151 }
152 
153 QVariant TorrentContentModelItem::underlyingData(const int column) const
154 {
155  if (isRootItem())
156  return m_itemData.value(column);
157 
158  switch (column)
159  {
160  case COL_NAME:
161  return m_name;
162  case COL_PRIO:
163  return static_cast<int>(m_priority);
164  case COL_PROGRESS:
165  return progress() * 100;
166  case COL_SIZE:
167  return m_size;
168  case COL_REMAINING:
169  return remaining();
170  case COL_AVAILABILITY:
171  return availability();
172  default:
173  Q_ASSERT(false);
174  return {};
175  }
176 }
177 
179 {
180  if (m_parentItem)
181  return m_parentItem->children().indexOf(const_cast<TorrentContentModelItem *>(this));
182  return 0;
183 }
184 
186 {
187  return m_parentItem;
188 }
const QVector< TorrentContentModelItem * > & children() const
TorrentContentModelFolder * m_parentItem
BitTorrent::DownloadPriority priority() const
TorrentContentModelItem(TorrentContentModelFolder *parent)
QString displayData(int column) const
virtual ~TorrentContentModelItem()
QVariant underlyingData(int column) const
BitTorrent::DownloadPriority m_priority
TorrentContentModelFolder * parent() const
void setName(const QString &name)
QString friendlyUnit(qint64 bytes, bool isSpeed=false)
Definition: misc.cpp:261
QString fromDouble(double n, int precision)
Definition: string.cpp:44
T value(const QString &key, const T &defaultValue={})
Definition: preferences.cpp:64
const char C_THIN_SPACE[]