miniupnpd 1.9 (20160113)
[tomato.git] / release / src / router / transmission / qt / torrent-delegate-min.cc
blobcb13c8571d637bf07cd17311f54e97af56322c62
1 /*
2 * This file Copyright (C) 2009-2014 Mnemosyne LLC
4 * It may be used under the GNU Public License v2 or v3 licenses,
5 * or any future license endorsed by Mnemosyne LLC.
7 * $Id: torrent-delegate-min.cc 14225 2014-01-19 01:09:44Z jordan $
8 */
10 #include <iostream>
12 #include <QApplication>
13 #include <QBrush>
14 #include <QFont>
15 #include <QFontMetrics>
16 #include <QIcon>
17 #include <QModelIndex>
18 #include <QPainter>
19 #include <QPixmap>
20 #include <QPixmapCache>
21 #include <QStyleOptionProgressBar>
23 #include <libtransmission/transmission.h>
24 #include <libtransmission/utils.h>
26 #include "torrent.h"
27 #include "torrent-delegate-min.h"
28 #include "torrent-model.h"
30 enum
32 GUI_PAD = 6,
33 BAR_WIDTH = 50,
34 BAR_HEIGHT = 12,
35 LINE_SPACING = 4
38 /***
39 ****
40 **** +---------+-----------------------------------------------+
41 **** | Icon | Title shortStatusString [Progressbar] |
42 **** +-------- +-----------------------------------------------+
43 ****
44 ***/
46 QSize
47 TorrentDelegateMin :: sizeHint (const QStyleOptionViewItem & option,
48 const Torrent & tor) const
50 const QStyle* style (QApplication::style());
51 static const int iconSize (style->pixelMetric (QStyle :: PM_SmallIconSize));
53 QFont nameFont (option.font);
54 const QFontMetrics nameFM (nameFont);
55 const bool isMagnet (!tor.hasMetadata());
56 const QString nameStr = (isMagnet ? progressString (tor) : tor.name());
57 const int nameWidth = nameFM.width (nameStr);
59 QFont statusFont (option.font);
60 statusFont.setPointSize (int (option.font.pointSize() * 0.85));
61 const QFontMetrics statusFM (statusFont);
62 const QString statusStr (shortStatusString (tor));
63 const int statusWidth = statusFM.width (statusStr);
65 const QSize m (margin (*style));
67 return QSize (m.width()*2 + iconSize + GUI_PAD + nameWidth
68 + GUI_PAD + statusWidth
69 + GUI_PAD + BAR_WIDTH,
70 m.height()*2 + std::max (nameFM.height(), (int)BAR_HEIGHT));
73 void
74 TorrentDelegateMin :: drawTorrent (QPainter * painter,
75 const QStyleOptionViewItem & option,
76 const Torrent & tor) const
78 const bool isPaused (tor.isPaused());
79 const QStyle * style (QApplication::style());
80 static const int iconSize (style->pixelMetric (QStyle :: PM_SmallIconSize));
82 QFont nameFont (option.font);
83 const QFontMetrics nameFM (nameFont);
84 const bool isMagnet (!tor.hasMetadata());
85 const QString nameStr = (isMagnet ? progressString (tor) : tor.name());
87 QFont statusFont (option.font);
88 statusFont.setPointSize (int (option.font.pointSize() * 0.85));
89 const QFontMetrics statusFM (statusFont);
90 const QString statusStr (shortStatusString (tor));
91 const QSize statusSize (statusFM.size (0, statusStr));
93 painter->save();
95 if (option.state & QStyle::State_Selected)
97 QPalette::ColorGroup cg = option.state & QStyle::State_Enabled
98 ? QPalette::Normal : QPalette::Disabled;
99 if (cg == QPalette::Normal && !(option.state & QStyle::State_Active))
100 cg = QPalette::Inactive;
102 painter->fillRect(option.rect, option.palette.brush(cg, QPalette::Highlight));
105 QIcon::Mode im;
106 if (isPaused || !(option.state & QStyle::State_Enabled))
107 im = QIcon::Disabled;
108 else if (option.state & QStyle::State_Selected)
109 im = QIcon::Selected;
110 else
111 im = QIcon::Normal;
113 QIcon::State qs;
114 if (isPaused)
115 qs = QIcon::Off;
116 else
117 qs = QIcon::On;
119 QPalette::ColorGroup cg = QPalette::Normal;
120 if (isPaused || !(option.state & QStyle::State_Enabled))
121 cg = QPalette::Disabled;
122 if (cg == QPalette::Normal && !(option.state & QStyle::State_Active))
123 cg = QPalette::Inactive;
125 QPalette::ColorRole cr;
126 if (option.state & QStyle::State_Selected)
127 cr = QPalette::HighlightedText;
128 else
129 cr = QPalette::Text;
131 QStyle::State progressBarState (option.state);
132 if (isPaused)
133 progressBarState = QStyle::State_None;
134 progressBarState |= QStyle::State_Small;
136 // layout
137 const QSize m (margin (*style));
138 QRect fillArea (option.rect);
139 fillArea.adjust (m.width(), m.height(), -m.width(), -m.height());
140 const QRect iconArea (fillArea.x(),
141 fillArea.y() + (fillArea.height() - iconSize) / 2,
142 iconSize,
143 iconSize);
144 const QRect barArea (fillArea.x() + fillArea.width() - BAR_WIDTH,
145 fillArea.y() + (fillArea.height() - BAR_HEIGHT) / 2,
146 BAR_WIDTH,
147 BAR_HEIGHT);
148 const QRect statusArea (barArea.x() - GUI_PAD - statusSize.width(),
149 fillArea.y() + (fillArea.height() - statusSize.height()) / 2,
150 fillArea.width(),
151 fillArea.height());
152 const QRect nameArea (iconArea.x() + iconArea.width() + GUI_PAD,
153 fillArea.y(),
154 statusArea.x() - (iconArea.x() + iconArea.width() + GUI_PAD * 2),
155 fillArea.height());
157 // render
158 if (tor.hasError())
159 painter->setPen (QColor ("red"));
160 else
161 painter->setPen (option.palette.color (cg, cr));
162 tor.getMimeTypeIcon().paint (painter, iconArea, Qt::AlignCenter, im, qs);
163 painter->setFont (nameFont);
164 painter->drawText (nameArea, 0, nameFM.elidedText (nameStr, Qt::ElideRight, nameArea.width()));
165 painter->setFont (statusFont);
166 painter->drawText (statusArea, 0, statusStr);
167 myProgressBarStyle->rect = barArea;
168 if (tor.isDownloading())
170 myProgressBarStyle->palette.setBrush (QPalette::Highlight, blueBrush);
171 myProgressBarStyle->palette.setColor (QPalette::Base, blueBack);
172 myProgressBarStyle->palette.setColor (QPalette::Window, blueBack);
174 else if (tor.isSeeding())
176 myProgressBarStyle->palette.setBrush (QPalette::Highlight, greenBrush);
177 myProgressBarStyle->palette.setColor (QPalette::Base, greenBack);
178 myProgressBarStyle->palette.setColor (QPalette::Window, greenBack);
180 else
182 myProgressBarStyle->palette.setBrush (QPalette::Highlight, silverBrush);
183 myProgressBarStyle->palette.setColor (QPalette::Base, silverBack);
184 myProgressBarStyle->palette.setColor (QPalette::Window, silverBack);
186 myProgressBarStyle->state = progressBarState;
187 char buf[32];
188 tr_snprintf (buf, sizeof (buf), "%d%%", (int)tr_truncd (100.0 * tor.percentDone(), 0));
189 myProgressBarStyle->text = buf;
190 myProgressBarStyle->textVisible = true;
191 myProgressBarStyle->textAlignment = Qt::AlignCenter;
192 setProgressBarPercentDone (option, tor);
193 style->drawControl (QStyle::CE_ProgressBar, myProgressBarStyle, painter);
195 painter->restore();