OSDN Git Service

7cb8e39ffc4f82450d14d6d935230e983b1ddd4f
[mutilities/MUtilities.git] / src / GUI.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // MuldeR's Utilities for Qt
3 // Copyright (C) 2004-2016 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 //MUtils
23 #include <MUtils/GUI.h>
24 #include <MUtils/OSSupport.h>
25
26 //Internal
27 #include "Utils_Win32.h"
28
29 //Qt
30 #include <QIcon>
31 #include <QApplication>
32 #include <QWidget>
33 #include <QMutex>
34
35 //Win32 API
36 #ifndef _INC_WINDOWS
37 #define WIN32_LEAN_AND_MEAN 1
38 #include <Windows.h>
39 #endif //_INC_WINDOWS
40
41 ///////////////////////////////////////////////////////////////////////////////
42 // BROADCAST
43 ///////////////////////////////////////////////////////////////////////////////
44
45 bool MUtils::GUI::broadcast(int eventType, const bool &onlyToVisible)
46 {
47         if(QApplication *app = dynamic_cast<QApplication*>(QApplication::instance()))
48         {
49                 qDebug("Broadcasting %d", eventType);
50                 
51                 bool allOk = true;
52                 QEvent poEvent(static_cast<QEvent::Type>(eventType));
53                 QWidgetList list = app->topLevelWidgets();
54
55                 while(!list.isEmpty())
56                 {
57                         QWidget *widget = list.takeFirst();
58                         if(!onlyToVisible || widget->isVisible())
59                         {
60                                 if(!app->sendEvent(widget, &poEvent))
61                                 {
62                                         allOk = false;
63                                 }
64                         }
65                 }
66
67                 qDebug("Broadcast %d done (%s)", eventType, (allOk ? "OK" : "Stopped"));
68                 return allOk;
69         }
70         else
71         {
72                 qWarning("Broadcast failed, could not get QApplication instance!");
73                 return false;
74         }
75 }
76
77 ///////////////////////////////////////////////////////////////////////////////
78 // WINDOW ICON
79 ///////////////////////////////////////////////////////////////////////////////
80
81 namespace MUtils
82 {
83         namespace GUI
84         {
85                 namespace Internal
86                 {
87                         class WindowIconHelper : public QObject
88                         {
89                         public:
90                                 WindowIconHelper(QWidget *const parent, const HICON hIcon, const bool &bIsBigIcon)
91                                 :
92                                         QObject(parent),
93                                         m_hIcon(hIcon)
94                                 {
95                                         SendMessage(parent->winId(), WM_SETICON, (bIsBigIcon ? ICON_BIG : ICON_SMALL), LPARAM(hIcon));
96                                 }
97
98                                 ~WindowIconHelper(void)
99                                 {
100                                         if(m_hIcon)
101                                         {
102                                                 DestroyIcon(m_hIcon);
103                                         }
104                                 }
105
106                         private:
107                                 const HICON m_hIcon;
108                         };
109                 }
110         }
111 }
112
113 bool MUtils::GUI::set_window_icon(QWidget *const window, const QIcon &icon, const bool bIsBigIcon)
114 {
115         if((!icon.isNull()) && window->winId())
116         {
117                 const int extend = (bIsBigIcon ? 32 : 16);
118                 if(HICON hIcon = (HICON) MUtils::Win32Utils::qicon_to_hicon(icon, extend, extend))
119                 {
120                         if(new Internal::WindowIconHelper(window, hIcon, bIsBigIcon))
121                         {
122                                 return true;
123                         }
124                 }
125         }
126         return false;
127 }
128
129 ///////////////////////////////////////////////////////////////////////////////
130 // BLINK WINDOW
131 ///////////////////////////////////////////////////////////////////////////////
132
133 static QMutex g_blinkMutex;
134
135 void MUtils::GUI::blink_window(QWidget *const poWindow, const unsigned int &count, const unsigned int &delay)
136 {
137         const double maxOpac = 1.0;
138         const double minOpac = 0.3;
139         const double delOpac = 0.1;
140
141         if(!g_blinkMutex.tryLock())
142         {
143                 qWarning("Blinking is already in progress, skipping!");
144                 return;
145         }
146         
147         try
148         {
149                 const int steps = static_cast<int>(ceil(maxOpac - minOpac) / delOpac);
150                 const int sleep = static_cast<int>(floor(static_cast<double>(delay) / static_cast<double>(steps)));
151                 const double opacity = poWindow->windowOpacity();
152         
153                 for(unsigned int i = 0; i < count; i++)
154                 {
155                         for(double x = maxOpac; x >= minOpac; x -= delOpac)
156                         {
157                                 poWindow->setWindowOpacity(x);
158                                 QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
159                                 MUtils::OS::sleep_ms(sleep);
160                         }
161
162                         for(double x = minOpac; x <= maxOpac; x += delOpac)
163                         {
164                                 poWindow->setWindowOpacity(x);
165                                 QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
166                                 MUtils::OS::sleep_ms(sleep);
167                         }
168                 }
169
170                 poWindow->setWindowOpacity(opacity);
171                 QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
172         }
173         catch(...)
174         {
175                 qWarning("Exception error while blinking!");
176         }
177
178         g_blinkMutex.unlock();
179 }
180
181 ///////////////////////////////////////////////////////////////////////////////
182 // FORCE QUIT
183 ///////////////////////////////////////////////////////////////////////////////
184
185 void MUtils::GUI::force_quit(void)
186 {
187         qApp->closeAllWindows();
188         qApp->quit();
189 }
190
191 ///////////////////////////////////////////////////////////////////////////////