1 /*
2 * Cantata
3 *
4 * Copyright (c) 2011-2020 Craig Drummond <craig.p.drummond@gmail.com>
5 *
6 * ----
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; see the file COPYING. If not, write to
20 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
22 */
23
24 #include "icon.h"
25 #include "utils.h"
26 #include <QMessageBox>
27 #include <QApplication>
28 #include <QToolButton>
29
stdSize(int v)30 int Icon::stdSize(int v)
31 {
32 if (v<19) {
33 return 16;
34 } else if (v<=28) {
35 return 22;
36 } else if (v<=40) {
37 return 32;
38 } else if (v<=56) {
39 return 48;
40 } else if (v<=90) {
41 return 64;
42 }
43
44 if (Utils::isHighDpi()) {
45 if (v<=160) {
46 return 128;
47 } else {
48 return 256;
49 }
50 }
51 return 128;
52 }
53
dlgIconSize()54 int Icon::dlgIconSize()
55 {
56 static int size=-1;
57
58 if (-1==size) {
59 QMessageBox *box=new QMessageBox(nullptr);
60 box->setVisible(false);
61 box->setIcon(QMessageBox::Information);
62 box->ensurePolished();
63 QPixmap pix=box->iconPixmap();
64 if (pix.isNull() || pix.width()<16) {
65 size=stdSize(QApplication::fontMetrics().height()*3.5);
66 } else {
67 size=pix.width();
68 }
69 box->deleteLater();
70 }
71 return size;
72 }
73
init(QToolButton * btn,bool setFlat)74 void Icon::init(QToolButton *btn, bool setFlat)
75 {
76 static int size=-1;
77
78 if (-1==size) {
79 size=QApplication::fontMetrics().height();
80 if (size>22) {
81 size=stdSize(size*1.1);
82 } else {
83 size=16;
84 }
85 }
86 btn->setIconSize(QSize(size, size));
87 if (setFlat) {
88 btn->setAutoRaise(true);
89 }
90 }
91
getScaledPixmap(const QIcon & icon,int w,int h,int base)92 QPixmap Icon::getScaledPixmap(const QIcon &icon, int w, int h, int base)
93 {
94 QList<QSize> sizes=icon.availableSizes();
95 for (const QSize &s: sizes) {
96 if (s.width()==w && s.height()==h) {
97 return icon.pixmap(s);
98 }
99 }
100
101 return icon.pixmap(base, base).scaled(w, h, Qt::KeepAspectRatio, Qt::SmoothTransformation);
102 }
103