1 /* This file is part of Clementine.
2    Copyright 2010, David Sansome <me@davidsansome.com>
3 
4    Clementine is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8 
9    Clementine is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13 
14    You should have received a copy of the GNU General Public License
15    along with Clementine.  If not, see <http://www.gnu.org/licenses/>.
16 */
17 
18 #include "songplaystats.h"
19 
20 #include <QIcon>
21 #include <QPainter>
22 
23 const int SongPlayStats::kIconSize = 16;
24 const int SongPlayStats::kLineSpacing = 2;
25 const int SongPlayStats::kIconTextSpacing = 6;
26 const int SongPlayStats::kMargin = 4;
27 
SongPlayStats(QWidget * parent)28 SongPlayStats::SongPlayStats(QWidget* parent) : QWidget(parent) {
29   setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
30 }
31 
AddItem(const QIcon & icon,const QString & text)32 void SongPlayStats::AddItem(const QIcon& icon, const QString& text) {
33   items_ << Item(icon, text);
34   updateGeometry();
35   update();
36 }
37 
sizeHint() const38 QSize SongPlayStats::sizeHint() const {
39   return QSize(100, kMargin * 2 + items_.count() * kIconSize +
40                         (items_.count() - 1) * kLineSpacing);
41 }
42 
paintEvent(QPaintEvent *)43 void SongPlayStats::paintEvent(QPaintEvent*) {
44   QPainter p(this);
45 
46   int y = kMargin;
47   for (const Item& item : items_) {
48     const QRect line(kMargin, y, width() - kMargin * 2, kIconSize);
49     const QRect icon_rect(line.topLeft(), QSize(kIconSize, kIconSize));
50     const QRect text_rect(
51         icon_rect.topRight() + QPoint(kIconTextSpacing, 0),
52         QSize(line.width() - icon_rect.width() - kIconTextSpacing,
53               line.height()));
54 
55     p.drawPixmap(icon_rect, item.icon_.pixmap(kIconSize));
56     p.drawText(text_rect, item.text_);
57 
58     y += line.height() + kLineSpacing;
59   }
60 }
61