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 $
12 #include <QApplication>
15 #include <QFontMetrics>
17 #include <QModelIndex>
20 #include <QPixmapCache>
21 #include <QStyleOptionProgressBar>
23 #include <libtransmission/transmission.h>
24 #include <libtransmission/utils.h>
27 #include "torrent-delegate-min.h"
28 #include "torrent-model.h"
40 **** +---------+-----------------------------------------------+
41 **** | Icon | Title shortStatusString [Progressbar] |
42 **** +-------- +-----------------------------------------------+
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
));
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
));
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
));
106 if (isPaused
|| !(option
.state
& QStyle::State_Enabled
))
107 im
= QIcon::Disabled
;
108 else if (option
.state
& QStyle::State_Selected
)
109 im
= QIcon::Selected
;
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
;
131 QStyle::State
progressBarState (option
.state
);
133 progressBarState
= QStyle::State_None
;
134 progressBarState
|= QStyle::State_Small
;
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,
144 const QRect
barArea (fillArea
.x() + fillArea
.width() - BAR_WIDTH
,
145 fillArea
.y() + (fillArea
.height() - BAR_HEIGHT
) / 2,
148 const QRect
statusArea (barArea
.x() - GUI_PAD
- statusSize
.width(),
149 fillArea
.y() + (fillArea
.height() - statusSize
.height()) / 2,
152 const QRect
nameArea (iconArea
.x() + iconArea
.width() + GUI_PAD
,
154 statusArea
.x() - (iconArea
.x() + iconArea
.width() + GUI_PAD
* 2),
159 painter
->setPen (QColor ("red"));
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
);
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
;
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
);