1 /* stock_icon_tool_button.cpp
3 * Wireshark - Network traffic analyzer
4 * By Gerald Combs <gerald@wireshark.org>
5 * Copyright 1998 Gerald Combs
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
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.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "stock_icon_tool_button.h"
24 #include "stock_icon.h"
26 #include <QApplication>
29 #include <QMouseEvent>
31 // We want nice icons that render correctly, and that are responsive
32 // when the user hovers and clicks them.
33 // Using setIcon renders correctly on normal and retina displays. It is
34 // not completely responsive, particularly on OS X.
35 // Calling setStyleSheet is responsive, but does not render correctly on
36 // retina displays: https://bugreports.qt.io/browse/QTBUG-36825
37 // Subclass QToolButton, which lets us catch events and set icons as needed.
39 StockIconToolButton::StockIconToolButton(QWidget * parent, QString stock_icon_name) :
43 if (!stock_icon_name.isEmpty()) {
44 setStockIcon(stock_icon_name);
48 void StockIconToolButton::setIconMode(QIcon::Mode mode)
51 QList<QIcon::State> states = QList<QIcon::State>() << QIcon::Off << QIcon::On;
52 foreach (QIcon::State state, states) {
53 foreach (QSize size, base_icon_.availableSizes(mode, state)) {
54 mode_icon.addPixmap(base_icon_.pixmap(size, mode, state), mode, state);
60 void StockIconToolButton::setStockIcon(QString icon_name)
62 base_icon_ = StockIcon(icon_name);
66 bool StockIconToolButton::event(QEvent *event)
68 switch (event->type()) {
71 setIconMode(QIcon::Active);
72 if (leave_timer_ > 0) killTimer(leave_timer_);
73 leave_timer_ = startTimer(leave_interval_);
76 case QEvent::MouseButtonPress:
78 setIconMode(QIcon::Selected);
82 if (leave_timer_ > 0) killTimer(leave_timer_);
85 case QEvent::MouseButtonRelease:
90 // We can lose QEvent::Leave, QEvent::HoverLeave and underMouse()
91 // on OS X if a tooltip appears:
92 // https://bugreports.qt.io/browse/QTBUG-46379
93 // Work around the issue by periodically checking the mouse
94 // position and scheduling a fake leave event when the mouse
96 QTimerEvent *te = (QTimerEvent *) event;
97 bool under_mouse = rect().contains(mapFromGlobal(QCursor::pos()));
98 if (te->timerId() == leave_timer_ && !under_mouse) {
99 killTimer(leave_timer_);
101 QMouseEvent *me = new QMouseEvent(QEvent::Leave, mapFromGlobal(QCursor::pos()), Qt::NoButton, Qt::NoButton, Qt::NoModifier);
102 QApplication::postEvent(this, me);
110 return QToolButton::event(event);
119 * indent-tabs-mode: nil
122 * ex: set shiftwidth=4 tabstop=8 expandtab:
123 * :indentSize=4:tabSize=8:noTabs=true: