1 /*
2 * This file Copyright (C) 2009-2015 Mnemosyne LLC
3 *
4 * It may be used under the GNU GPL versions 2 or 3
5 * or any future license endorsed by Mnemosyne LLC.
6 *
7 */
8
9 #include <algorithm>
10
11 #include <QApplication>
12 #include <QBrush>
13 #include <QFont>
14 #include <QFontMetrics>
15 #include <QIcon>
16 #include <QModelIndex>
17 #include <QPainter>
18 #include <QPixmap>
19 #include <QPixmapCache>
20 #include <QStyleOptionProgressBar>
21
22 #include <libtransmission/transmission.h>
23 #include <libtransmission/utils.h>
24
25 #include "Torrent.h"
26 #include "TorrentDelegateMin.h"
27 #include "TorrentModel.h"
28 #include "Utils.h"
29
30 enum
31 {
32 GUI_PAD = 6,
33 BAR_WIDTH = 50,
34 BAR_HEIGHT = 16,
35 LINE_SPACING = 4
36 };
37
38 /***
39 ****
40 **** +---------+-----------------------------------------------+
41 **** | Icon | Title shortStatusString [Progressbar] |
42 **** +-------- +-----------------------------------------------+
43 ****
44 ***/
45
46 namespace
47 {
48
49 class ItemLayout
50 {
51 private:
52 QString myNameText;
53 QString myStatusText;
54
55 public:
56 QFont nameFont;
57 QFont statusFont;
58
59 QRect iconRect;
60 QRect emblemRect;
61 QRect nameRect;
62 QRect statusRect;
63 QRect barRect;
64
65 public:
66 ItemLayout(QString const& nameText, QString const& statusText, QIcon const& emblemIcon, QFont const& baseFont,
67 Qt::LayoutDirection direction, QPoint const& topLeft, int width);
68
size() const69 QSize size() const
70 {
71 return (iconRect | nameRect | statusRect | barRect).size();
72 }
73
nameText() const74 QString nameText() const
75 {
76 return elidedText(nameFont, myNameText, nameRect.width());
77 }
78
statusText() const79 QString statusText() const
80 {
81 return myStatusText;
82 }
83
84 private:
elidedText(QFont const & font,QString const & text,int width) const85 QString elidedText(QFont const& font, QString const& text, int width) const
86 {
87 return QFontMetrics(font).elidedText(text, Qt::ElideRight, width);
88 }
89 };
90
ItemLayout(QString const & nameText,QString const & statusText,QIcon const & emblemIcon,QFont const & baseFont,Qt::LayoutDirection direction,QPoint const & topLeft,int width)91 ItemLayout::ItemLayout(QString const& nameText, QString const& statusText, QIcon const& emblemIcon, QFont const& baseFont,
92 Qt::LayoutDirection direction, QPoint const& topLeft, int width) :
93 myNameText(nameText),
94 myStatusText(statusText),
95 nameFont(baseFont),
96 statusFont(baseFont)
97 {
98 QStyle const* style(qApp->style());
99 int const iconSize(style->pixelMetric(QStyle::PM_SmallIconSize));
100
101 QFontMetrics const nameFM(nameFont);
102 QSize const nameSize(nameFM.size(0, myNameText));
103
104 statusFont.setPointSize(static_cast<int>(statusFont.pointSize() * 0.85));
105 QFontMetrics const statusFM(statusFont);
106 QSize const statusSize(statusFM.size(0, myStatusText));
107
108 QStyleOptionProgressBar barStyle;
109 barStyle.rect = QRect(0, 0, BAR_WIDTH, BAR_HEIGHT);
110 barStyle.maximum = 100;
111 barStyle.progress = 100;
112 barStyle.textVisible = true;
113 QSize const barSize(barStyle.rect.width() * 2 - style->subElementRect(QStyle::SE_ProgressBarGroove, &barStyle).width(),
114 barStyle.rect.height());
115
116 QRect baseRect(topLeft, QSize(width, std::max({ iconSize, nameSize.height(), statusSize.height(), barSize.height() })));
117
118 iconRect = style->alignedRect(direction, Qt::AlignLeft | Qt::AlignVCenter, QSize(iconSize, iconSize), baseRect);
119 emblemRect = style->alignedRect(direction, Qt::AlignRight | Qt::AlignBottom, emblemIcon.actualSize(iconRect.size() / 2,
120 QIcon::Normal, QIcon::On), iconRect);
121 barRect = style->alignedRect(direction, Qt::AlignRight | Qt::AlignVCenter, barSize, baseRect);
122 Utils::narrowRect(baseRect, iconRect.width() + GUI_PAD, barRect.width() + GUI_PAD, direction);
123 statusRect = style->alignedRect(direction, Qt::AlignRight | Qt::AlignVCenter, QSize(statusSize.width(), baseRect.height()),
124 baseRect);
125 Utils::narrowRect(baseRect, 0, statusRect.width() + GUI_PAD, direction);
126 nameRect = baseRect;
127 }
128
129 } // namespace
130
sizeHint(QStyleOptionViewItem const & option,Torrent const & tor) const131 QSize TorrentDelegateMin::sizeHint(QStyleOptionViewItem const& option, Torrent const& tor) const
132 {
133 bool const isMagnet(!tor.hasMetadata());
134 QSize const m(margin(*qApp->style()));
135 ItemLayout const layout(isMagnet ? progressString(tor) : tor.name(), shortStatusString(tor), QIcon(), option.font,
136 option.direction, QPoint(0, 0), option.rect.width() - m.width() * 2);
137 return layout.size() + m * 2;
138 }
139
drawTorrent(QPainter * painter,QStyleOptionViewItem const & option,Torrent const & tor) const140 void TorrentDelegateMin::drawTorrent(QPainter* painter, QStyleOptionViewItem const& option, Torrent const& tor) const
141 {
142 QStyle const* style(qApp->style());
143
144 bool const isPaused(tor.isPaused());
145 bool const isMagnet(!tor.hasMetadata());
146
147 bool const isItemSelected((option.state & QStyle::State_Selected) != 0);
148 bool const isItemEnabled((option.state & QStyle::State_Enabled) != 0);
149 bool const isItemActive((option.state & QStyle::State_Active) != 0);
150
151 painter->save();
152
153 if (isItemSelected)
154 {
155 QPalette::ColorGroup cg = isItemEnabled ? QPalette::Normal : QPalette::Disabled;
156
157 if (cg == QPalette::Normal && !isItemActive)
158 {
159 cg = QPalette::Inactive;
160 }
161
162 painter->fillRect(option.rect, option.palette.brush(cg, QPalette::Highlight));
163 }
164
165 QIcon::Mode im;
166
167 if (isPaused || !isItemEnabled)
168 {
169 im = QIcon::Disabled;
170 }
171 else if (isItemSelected)
172 {
173 im = QIcon::Selected;
174 }
175 else
176 {
177 im = QIcon::Normal;
178 }
179
180 QIcon::State qs;
181
182 if (isPaused)
183 {
184 qs = QIcon::Off;
185 }
186 else
187 {
188 qs = QIcon::On;
189 }
190
191 QPalette::ColorGroup cg = QPalette::Normal;
192
193 if (isPaused || !isItemEnabled)
194 {
195 cg = QPalette::Disabled;
196 }
197
198 if (cg == QPalette::Normal && !isItemActive)
199 {
200 cg = QPalette::Inactive;
201 }
202
203 QPalette::ColorRole cr;
204
205 if (isItemSelected)
206 {
207 cr = QPalette::HighlightedText;
208 }
209 else
210 {
211 cr = QPalette::Text;
212 }
213
214 QStyle::State progressBarState(option.state);
215
216 if (isPaused)
217 {
218 progressBarState = QStyle::State_None;
219 }
220
221 progressBarState |= QStyle::State_Small;
222
223 QIcon::Mode const emblemIm = isItemSelected ? QIcon::Selected : QIcon::Normal;
224 QIcon const emblemIcon = tor.hasError() ? getWarningEmblem() : QIcon();
225
226 // layout
227 QSize const m(margin(*style));
228 QRect const contentRect(option.rect.adjusted(m.width(), m.height(), -m.width(), -m.height()));
229 ItemLayout const layout(isMagnet ? progressString(tor) : tor.name(), shortStatusString(tor), emblemIcon, option.font,
230 option.direction, contentRect.topLeft(), contentRect.width());
231
232 // render
233 if (tor.hasError() && !isItemSelected)
234 {
235 painter->setPen(QColor("red"));
236 }
237 else
238 {
239 painter->setPen(option.palette.color(cg, cr));
240 }
241
242 tor.getMimeTypeIcon().paint(painter, layout.iconRect, Qt::AlignCenter, im, qs);
243
244 if (!emblemIcon.isNull())
245 {
246 emblemIcon.paint(painter, layout.emblemRect, Qt::AlignCenter, emblemIm, qs);
247 }
248
249 painter->setFont(layout.nameFont);
250 painter->drawText(layout.nameRect, Qt::AlignLeft | Qt::AlignVCenter, layout.nameText());
251 painter->setFont(layout.statusFont);
252 painter->drawText(layout.statusRect, Qt::AlignLeft | Qt::AlignVCenter, layout.statusText());
253 myProgressBarStyle->rect = layout.barRect;
254
255 if (tor.isDownloading())
256 {
257 myProgressBarStyle->palette.setBrush(QPalette::Highlight, blueBrush);
258 myProgressBarStyle->palette.setColor(QPalette::Base, blueBack);
259 myProgressBarStyle->palette.setColor(QPalette::Window, blueBack);
260 }
261 else if (tor.isSeeding())
262 {
263 myProgressBarStyle->palette.setBrush(QPalette::Highlight, greenBrush);
264 myProgressBarStyle->palette.setColor(QPalette::Base, greenBack);
265 myProgressBarStyle->palette.setColor(QPalette::Window, greenBack);
266 }
267 else
268 {
269 myProgressBarStyle->palette.setBrush(QPalette::Highlight, silverBrush);
270 myProgressBarStyle->palette.setColor(QPalette::Base, silverBack);
271 myProgressBarStyle->palette.setColor(QPalette::Window, silverBack);
272 }
273
274 myProgressBarStyle->state = progressBarState;
275 myProgressBarStyle->text = QString::fromLatin1("%1%").arg(static_cast<int>(tr_truncd(100.0 * tor.percentDone(), 0)));
276 myProgressBarStyle->textVisible = true;
277 myProgressBarStyle->textAlignment = Qt::AlignCenter;
278 setProgressBarPercentDone(option, tor);
279 style->drawControl(QStyle::CE_ProgressBar, myProgressBarStyle, painter);
280
281 painter->restore();
282 }
283