1 /* BottomBarButton.cpp */
2 
3 /* Copyright (C) 2011-2020 Michael Lugmair (Lucio Carreras)
4  *
5  * This file is part of sayonara player
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11 
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16 
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #include "PlaylistBottomBarButton.h"
22 #include <QPixmap>
23 #include <QPainter>
24 #include <QPaintEvent>
25 
26 using Playlist::BottomBarButton;
27 
28 struct BottomBarButton::Private
29 {
30 	QIcon icon;
31 
32 	Private(const QIcon& icon) : icon(icon) {}
33 };
34 
35 BottomBarButton::BottomBarButton(const QIcon& icon, QWidget* parent) :
36 	QPushButton(parent)
37 {
38 	m = Pimpl::make<Private>(icon);
39 }
40 
41 BottomBarButton::~BottomBarButton() = default;
42 
43 void BottomBarButton::setIcon(const QIcon& icon)
44 {
45 	m->icon = icon;
46 }
47 
48 void BottomBarButton::paintEvent(QPaintEvent* e)
49 {
50 	if(!this->isChecked())
51 	{
52 		QPushButton::paintEvent(e);
53 	}
54 
55     if (!m->icon.isNull())
56     {
57 		const int w = this->width();
58 		const int h = this->height();
59 
60 		int pm_w = (w * 800) / 1000;
61 		int pm_h = (h * 800) / 1000;
62 
63 		const int x = (w - pm_w) / 2;
64 		const int y = (h - pm_h) / 2;
65 
66 		if((w - pm_w) % 2 == 1){
67 			pm_w++;
68 		}
69 
70 		if((h - pm_h) % 2 == 1){
71 			pm_h++;
72 		}
73 
74 		QPixmap pm = m->icon.pixmap(pm_w, pm_h);;
75 
76         QPainter painter(this);
77         if(this->isChecked())
78         {
79 			QRect r = e->rect();
80 			painter.setPen(palette().window().color());
81 			painter.drawRect(r);
82 
83 			QColor c = palette().highlight().color();
84 			painter.setOpacity(0.3);
85 			painter.setPen(c);
86 			painter.setBrush(c);
87 			painter.drawRect(r);
88 			painter.fillRect(r, c);
89         }
90 
91 		painter.setOpacity(1.0);
92         painter.drawPixmap
93         (
94 			QRect(x, y, pm_w, pm_h),
95 			pm
96         );
97     }
98 }
99