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 "toolbutton.h"
25 #include "support/icon.h"
26 #include "support/gtkstyle.h"
27 #include "config.h"
28 #include "support/utils.h"
29 #include <QMenu>
30 #include <QStylePainter>
31 #include <QStyleOptionToolButton>
32 #include <QApplication>
33 #include <QPainter>
34 
ToolButton(QWidget * parent)35 ToolButton::ToolButton(QWidget *parent)
36     : QToolButton(parent)
37 {
38     Icon::init(this);
39     #ifdef Q_OS_MAC
40     setStyleSheet("QToolButton {border: 0}");
41     allowMouseOver=parent && parent->objectName()!=QLatin1String("toolbar");
42     #endif
43     setFocusPolicy(Qt::NoFocus);
44 }
45 
paintEvent(QPaintEvent * e)46 void ToolButton::paintEvent(QPaintEvent *e)
47 {
48     #ifdef Q_OS_MAC
49     bool down=isDown() || isChecked();
50     bool mo=false;
51 
52     if (allowMouseOver && !down && isEnabled()) {
53         QStyleOptionToolButton opt;
54         initStyleOption(&opt);
55         mo=opt.state&QStyle::State_MouseOver && this==QApplication::widgetAt(QCursor::pos());
56     }
57     if (down || mo) {
58         QPainter p(this);
59         QColor col(palette().color(QPalette::WindowText));
60         QRect r(rect());
61         QPainterPath path=Utils::buildPath(QRectF(r.x()+1.5, r.y()+1.5, r.width()-3, r.height()-3), 2.5);
62         p.setRenderHint(QPainter::Antialiasing, true);
63         col.setAlphaF(0.4);
64         p.setPen(col);
65         p.drawPath(path);
66         if (down) {
67             col.setAlphaF(0.1);
68             p.fillPath(path, col);
69         }
70     }
71     #endif
72     Q_UNUSED(e)
73     // Hack to work-around Qt5 sometimes leaving toolbutton in 'raised' state.
74     QStylePainter p(this);
75     QStyleOptionToolButton opt;
76     initStyleOption(&opt);
77     opt.features=QStyleOptionToolButton::None;
78     if (opt.state&QStyle::State_MouseOver && this!=QApplication::widgetAt(QCursor::pos())) {
79         opt.state&=~QStyle::State_MouseOver;
80     }
81     p.drawComplexControl(QStyle::CC_ToolButton, opt);
82 }
83 
sizeHint() const84 QSize ToolButton::sizeHint() const
85 {
86     if (!sh.isValid()) {
87         ensurePolished();
88         sh = QToolButton::sizeHint();
89 
90         if (sh.width()>sh.height()) {
91             sh.setWidth(sh.height());
92         }
93 
94         sh=QSize(qMax(sh.width(), sh.height()), qMax(sh.width(), sh.height()));
95         #ifdef Q_OS_MAC
96         sh=QSize(qMax(sh.width(), 22), qMax(sh.height(), 20));
97         #endif
98     }
99     return sh;
100 }
101 
setMenu(QMenu * m)102 void ToolButton::setMenu(QMenu *m)
103 {
104     QToolButton::setMenu(m);
105     sh=QSize();
106     setPopupMode(InstantPopup);
107 }
108