qt: add missing initializers (CID 1325722)
[metze/wireshark/wip.git] / ui / qt / stock_icon_tool_button.cpp
1 /* stock_icon_tool_button.cpp
2  *
3  * Wireshark - Network traffic analyzer
4  * By Gerald Combs <gerald@wireshark.org>
5  * Copyright 1998 Gerald Combs
6  *
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.
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, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #include "stock_icon_tool_button.h"
23
24 #include "stock_icon.h"
25
26 #include <QApplication>
27 #include <QEvent>
28 #include <QMenu>
29 #include <QMouseEvent>
30
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.
38
39 StockIconToolButton::StockIconToolButton(QWidget * parent, QString stock_icon_name) :
40     QToolButton(parent),
41     leave_timer_(0)
42 {
43     if (!stock_icon_name.isEmpty()) {
44         setStockIcon(stock_icon_name);
45     }
46 }
47
48 void StockIconToolButton::setIconMode(QIcon::Mode mode)
49 {
50     QIcon mode_icon;
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);
55         }
56     }
57     setIcon(mode_icon);
58 }
59
60 void StockIconToolButton::setStockIcon(QString icon_name)
61 {
62     base_icon_ = StockIcon(icon_name);
63     setIconMode();
64 }
65
66 bool StockIconToolButton::event(QEvent *event)
67 {
68     switch (event->type()) {
69         case QEvent::Enter:
70         if (isEnabled()) {
71             setIconMode(QIcon::Active);
72             if (leave_timer_ > 0) killTimer(leave_timer_);
73             leave_timer_ = startTimer(leave_interval_);
74         }
75         break;
76     case QEvent::MouseButtonPress:
77         if (isEnabled()) {
78             setIconMode(QIcon::Selected);
79         }
80         break;
81     case QEvent::Leave:
82         if (leave_timer_ > 0) killTimer(leave_timer_);
83         leave_timer_ = 0;
84         // Fall through
85     case QEvent::MouseButtonRelease:
86         setIconMode();
87         break;
88     case QEvent::Timer:
89     {
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
95         // moves away.
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_);
100             leave_timer_ = 0;
101             QMouseEvent *me = new QMouseEvent(QEvent::Leave, mapFromGlobal(QCursor::pos()), Qt::NoButton, Qt::NoButton, Qt::NoModifier);
102             QApplication::postEvent(this, me);
103         }
104         break;
105     }
106     default:
107         break;
108     }
109
110     return QToolButton::event(event);
111 }
112
113 /*
114  * Editor modelines
115  *
116  * Local Variables:
117  * c-basic-offset: 4
118  * tab-width: 8
119  * indent-tabs-mode: nil
120  * End:
121  *
122  * ex: set shiftwidth=4 tabstop=8 expandtab:
123  * :indentSize=4:tabSize=8:noTabs=true:
124  */