OSDN Git Service

Bump version.
[mutilities/MUtilities.git] / src / GUI.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // MuldeR's Utilities for Qt
3 // Copyright (C) 2004-2019 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 #include <QDesktopWidget>
35
36 //Win32 API
37 #ifndef _INC_WINDOWS
38 #define WIN32_LEAN_AND_MEAN 1
39 #include <Windows.h>
40 #endif //_INC_WINDOWS
41
42 ///////////////////////////////////////////////////////////////////////////////
43 // BROADCAST
44 ///////////////////////////////////////////////////////////////////////////////
45
46 bool MUtils::GUI::broadcast(int eventType, const bool &onlyToVisible)
47 {
48         if(QApplication *app = dynamic_cast<QApplication*>(QApplication::instance()))
49         {
50                 qDebug("Broadcasting %d", eventType);
51                 
52                 bool allOk = true;
53                 QEvent poEvent(static_cast<QEvent::Type>(eventType));
54                 QWidgetList list = app->topLevelWidgets();
55
56                 while(!list.isEmpty())
57                 {
58                         QWidget *widget = list.takeFirst();
59                         if(!onlyToVisible || widget->isVisible())
60                         {
61                                 if(!app->sendEvent(widget, &poEvent))
62                                 {
63                                         allOk = false;
64                                 }
65                         }
66                 }
67
68                 qDebug("Broadcast %d done (%s)", eventType, (allOk ? "OK" : "Stopped"));
69                 return allOk;
70         }
71         else
72         {
73                 qWarning("Broadcast failed, could not get QApplication instance!");
74                 return false;
75         }
76 }
77
78 ///////////////////////////////////////////////////////////////////////////////
79 // WINDOW ICON
80 ///////////////////////////////////////////////////////////////////////////////
81
82 namespace MUtils
83 {
84         namespace GUI
85         {
86                 namespace Internal
87                 {
88                 class WindowIconHelper : public QObject
89                 {
90                 public:
91                         WindowIconHelper(QWidget *const parent, const HICON hIcon, const bool &bIsBigIcon)
92                                 :
93                                 QObject(parent),
94                                 m_hIcon(hIcon)
95                         {
96                                 SendMessage(reinterpret_cast<HWND>(parent->winId()), WM_SETICON, (bIsBigIcon ? ICON_BIG : ICON_SMALL), LPARAM(hIcon));
97                         }
98
99                         virtual ~WindowIconHelper(void)
100                         {
101                                 if (m_hIcon)
102                                 {
103                                         DestroyIcon(m_hIcon);
104                                 }
105                         }
106
107                 private:
108                         const HICON m_hIcon;
109                 };
110                 }
111         }
112 }
113
114 bool MUtils::GUI::set_window_icon(QWidget *const window, const QIcon &icon, const bool bIsBigIcon)
115 {
116         if ((!icon.isNull()) && window->winId())
117         {
118                 const int extend = (bIsBigIcon ? 32 : 16);
119                 if (const HICON hIcon = (HICON)MUtils::Win32Utils::qicon_to_hicon(&icon, extend, extend))
120                 {
121                         new Internal::WindowIconHelper(window, hIcon, bIsBigIcon); /*will be free'd using QObject parent mechanism*/
122                         return true;
123                 }
124         }
125         return false;
126 }
127
128 ///////////////////////////////////////////////////////////////////////////////
129 // BLINK WINDOW
130 ///////////////////////////////////////////////////////////////////////////////
131
132 static QMutex g_blinkMutex;
133
134 void MUtils::GUI::blink_window(QWidget *const poWindow, const unsigned int &count, const unsigned int &delay)
135 {
136         const double maxOpac = 1.0;
137         const double minOpac = 0.3;
138         const double delOpac = 0.1;
139
140         if (!g_blinkMutex.tryLock())
141         {
142                 qWarning("Blinking is already in progress, skipping!");
143                 return;
144         }
145
146         try
147         {
148                 const int steps = static_cast<int>(ceil(maxOpac - minOpac) / delOpac);
149                 const int sleep = static_cast<int>(floor(static_cast<double>(delay) / static_cast<double>(steps)));
150                 const double opacity = poWindow->windowOpacity();
151
152                 for (unsigned int i = 0; i < count; i++)
153                 {
154                         for (double x = maxOpac; x >= minOpac; x -= delOpac)
155                         {
156                                 poWindow->setWindowOpacity(x);
157                                 QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
158                                 MUtils::OS::sleep_ms(sleep);
159                         }
160
161                         for (double x = minOpac; x <= maxOpac; x += delOpac)
162                         {
163                                 poWindow->setWindowOpacity(x);
164                                 QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
165                                 MUtils::OS::sleep_ms(sleep);
166                         }
167                 }
168
169                 poWindow->setWindowOpacity(opacity);
170                 QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
171         }
172         catch (...)
173         {
174                 qWarning("Exception error while blinking!");
175         }
176
177         g_blinkMutex.unlock();
178 }
179
180 ///////////////////////////////////////////////////////////////////////////////
181 // DPI SCALING
182 ///////////////////////////////////////////////////////////////////////////////
183
184 double MUtils::GUI::dpi_scale(void)
185 {
186         if (const QApplication *const app = dynamic_cast<QApplication*>(QCoreApplication::instance()))
187         {
188                 const double dpi_x = static_cast<double>(app->desktop()->logicalDpiX());
189                 const double dpi_y = static_cast<double>(app->desktop()->logicalDpiY());
190                 return qBound(1.0, ((dpi_x + dpi_y) / 192.0), 2.0);
191         }
192         return -1.0;
193 }
194
195 bool MUtils::GUI::scale_widget(QWidget *const widget, const bool recenter)
196 {
197         if (widget && (widget->windowFlags().testFlag(Qt::WindowType::Window)))
198         {
199                 const double dpiScale = dpi_scale();
200                 if ((dpiScale > 0.0) && (!qFuzzyCompare(dpiScale, 1.0)))
201                 {
202                         const QSize originalSize = widget->size();
203                         widget->resize(qRound(originalSize.width() * dpiScale), qRound(originalSize.height() * dpiScale));
204                         return recenter ? center_widget(widget) : true;
205                 }
206         }
207         return false;
208 }
209
210 bool MUtils::GUI::center_widget(QWidget *const widget)
211 {
212         if (widget && (!widget->parentWidget()))
213         {
214                 const QRect desktopRect = QApplication::desktop()->screenGeometry();
215                 const QRect thisRect = widget->geometry();
216                 widget->move((desktopRect.width() - thisRect.width()) / 2, (desktopRect.height() - thisRect.height()) / 2);
217                 return true;
218         }
219         return false;
220 }
221
222 ///////////////////////////////////////////////////////////////////////////////
223 // FORCE QUIT
224 ///////////////////////////////////////////////////////////////////////////////
225
226 void MUtils::GUI::force_quit(void)
227 {
228         qApp->closeAllWindows();
229         qApp->quit();
230 }
231
232 ///////////////////////////////////////////////////////////////////////////////