OSDN Git Service

Moved more functions into MUtils library, especially all the Qt initialization code...
[mutilities/MUtilities.git] / src / GUI.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // MuldeR's Utilities for Qt
3 // Copyright (C) 2004-2014 LoRd_MuldeR <MuldeR2@GMX.de>
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18 //
19 // http://www.gnu.org/licenses/lgpl-2.1.txt
20 //////////////////////////////////////////////////////////////////////////////////
21
22 #include <MUtils/GUI.h>
23
24 //Qt
25 #include <QApplication>
26 #include <QWidget>
27
28 ///////////////////////////////////////////////////////////////////////////////
29 // BROADCAST
30 ///////////////////////////////////////////////////////////////////////////////
31
32 bool MUtils::GUI::broadcast(int eventType, const bool &onlyToVisible)
33 {
34         if(QApplication *app = dynamic_cast<QApplication*>(QApplication::instance()))
35         {
36                 qDebug("Broadcasting %d", eventType);
37                 
38                 bool allOk = true;
39                 QEvent poEvent(static_cast<QEvent::Type>(eventType));
40                 QWidgetList list = app->topLevelWidgets();
41
42                 while(!list.isEmpty())
43                 {
44                         QWidget *widget = list.takeFirst();
45                         if(!onlyToVisible || widget->isVisible())
46                         {
47                                 if(!app->sendEvent(widget, &poEvent))
48                                 {
49                                         allOk = false;
50                                 }
51                         }
52                 }
53
54                 qDebug("Broadcast %d done (%s)", eventType, (allOk ? "OK" : "Stopped"));
55                 return allOk;
56         }
57         else
58         {
59                 qWarning("Broadcast failed, could not get QApplication instance!");
60                 return false;
61         }
62 }
63
64 ///////////////////////////////////////////////////////////////////////////////
65 // BROADCAST
66 ///////////////////////////////////////////////////////////////////////////////
67
68 void MUtils::GUI::force_quit(void)
69 {
70         qApp->closeAllWindows();
71         qApp->quit();
72 }
73
74 ///////////////////////////////////////////////////////////////////////////////