OSDN Git Service

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