Debian Codegraph @

src/Gui/WaitCursor.cpp

raw
1 /***************************************************************************
2 * Copyright (c) 2004 Werner Mayer <wmayer[at]users.sourceforge.net> *
3 * *
4 * This file is part of the FreeCAD CAx development system. *
5 * *
6 * This library is free software; you can redistribute it and/or *
7 * modify it under the terms of the GNU Library General Public *
8 * License as published by the Free Software Foundation; either *
9 * version 2 of the License, or (at your option) any later version. *
10 * *
11 * This library is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU Library General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Library General Public *
17 * License along with this library; see the file COPYING.LIB. If not, *
18 * write to the Free Software Foundation, Inc., 59 Temple Place, *
19 * Suite 330, Boston, MA 02111-1307, USA *
20 * *
21 ***************************************************************************/
22
23 #include <FCConfig.h>
24
25 #include <QApplication>
26 #include <QMessageBox>
27 #include <QProgressDialog>
28 #include <QWindow>
29 #ifdef FC_OS_WIN32
30 # include <windows.h>
31 #endif
32
33 #include "WaitCursor.h"
34
35 using namespace Gui;
36
37 namespace Gui
38 {
39 class WaitCursorP: public QObject
40 {
41 public:
42 static WaitCursorP* getInstance();
43 void setBusy(bool);
44 WaitCursor::FilterEventsFlags ignoreEvents() const;
45 void setIgnoreEvents(WaitCursor::FilterEventsFlags flags);
46
47 protected:
48 bool eventFilter(QObject*, QEvent*) override;
49 bool isModalDialog(QObject* o) const;
50
51 private:
52 WaitCursorP(); // Disable constructor
53 static WaitCursorP* _instance;
54 bool isOn {false};
55 WaitCursor::FilterEventsFlags flags {WaitCursor::AllEvents};
56 };
57 } // namespace Gui
58
59 WaitCursorP* WaitCursorP::_instance = nullptr;
60
61 WaitCursorP::WaitCursorP()
62 : QObject(nullptr)
63 {}
64
65 WaitCursorP* WaitCursorP::getInstance()
66 {
67 if (!_instance) {
68 _instance = new WaitCursorP();
69 }
70 return _instance;
71 }
72
73 void WaitCursorP::setBusy(bool on)
74 {
75 if (on == this->isOn) {
76 return;
77 }
78
79 if (on) {
80 qApp->installEventFilter(this);
81 QApplication::setOverrideCursor(Qt::WaitCursor);
82 }
83 else {
84 qApp->removeEventFilter(this);
85 QApplication::restoreOverrideCursor();
86 }
87
88 this->isOn = on;
89 }
90
91 WaitCursor::FilterEventsFlags WaitCursorP::ignoreEvents() const
92 {
93 return this->flags;
94 }
95
96 void WaitCursorP::setIgnoreEvents(WaitCursor::FilterEventsFlags flags)
97 {
98 this->flags = flags;
99 }
100
101 bool WaitCursorP::isModalDialog(QObject* o) const
102 {
103 QWidget* parent = qobject_cast<QWidget*>(o);
104 if (!parent) {
105 QWindow* window = qobject_cast<QWindow*>(o);
106 if (window) {
107 parent = QWidget::find(window->winId());
108 }
109 }
110 while (parent) {
111 auto dlg = qobject_cast<QMessageBox*>(parent);
112 if (dlg && dlg->isModal()) {
113 return true;
114 }
115 auto pd = qobject_cast<QProgressDialog*>(parent);
116 if (pd) {
117 return true;
118 }
119 parent = parent->parentWidget();
120 }
121
122 return false;
123 }
124
125 bool WaitCursorP::eventFilter(QObject* o, QEvent* e)
126 {
127 // Note: This might cause problems when we want to open a modal dialog at the lifetime
128 // of a WaitCursor instance because the incoming events are still filtered.
129 if (e->type() == QEvent::KeyPress || e->type() == QEvent::KeyRelease) {
130 if (isModalDialog(o)) {
131 return false;
132 }
133 if (this->flags & WaitCursor::KeyEvents) {
134 return true;
135 }
136 }
137 if (e->type() == QEvent::MouseButtonPress || e->type() == QEvent::MouseButtonRelease
138 || e->type() == QEvent::MouseButtonDblClick) {
139 if (isModalDialog(o)) {
140 return false;
141 }
142 if (this->flags & WaitCursor::MouseEvents) {
143 return true;
144 }
145 }
146 return false;
147 }
148
149 int WaitCursor::instances = 0;
150
151 /**
152 * Constructs this object and shows the wait cursor immediately. If you need to open a dialog as
153 * long as an instance of WaitCursor exists you must call restoreCursor() before and setWaitCursor()
154 * afterwards because all key events and mouse button events are filtered, otherwise you will run
155 * into strange behaviour.
156 */
157 WaitCursor::WaitCursor()
158 {
159 if (instances++ == 0) {
160 setWaitCursor();
161 }
162 filter = WaitCursorP::getInstance()->ignoreEvents();
163 }
164
165 /** Restores the last cursor again. */
166 WaitCursor::~WaitCursor()
167 {
168 if (--instances == 0) {
169 restoreCursor();
170 }
171 WaitCursorP::getInstance()->setIgnoreEvents(filter);
172 }
173
174 /**
175 * Sets the wait cursor if needed.
176 */
177 void WaitCursor::setWaitCursor()
178 {
179 WaitCursorP::getInstance()->setBusy(true);
180 }
181
182 /**
183 * Restores the last cursor if needed.
184 */
185 void WaitCursor::restoreCursor()
186 {
187 WaitCursorP::getInstance()->setBusy(false);
188 }
189
190 WaitCursor::FilterEventsFlags WaitCursor::ignoreEvents() const
191 {
192 return WaitCursorP::getInstance()->ignoreEvents();
193 }
194
195 void WaitCursor::setIgnoreEvents(FilterEventsFlags flags)
196 {
197 WaitCursorP::getInstance()->setIgnoreEvents(flags);
198 }
199
200 void WaitCursor::suspend()
201 {
202 // Calling setBusy(false) will restore the cursor and remove the event filter.
203 WaitCursorP::getInstance()->setBusy(false);
204 }
205
206 void WaitCursor::resume()
207 {
208 // Calling setBusy(true) will set the wait cursor and reinstall the event filter.
209 // The WaitCursorP's internal state `isOn` correctly handles this call.
210 WaitCursorP::getInstance()->setBusy(true);
211 }