Some code refactoring.
[MUtilities.git] / src / GUI.cpp
bloba2ea8408b2c85dcaa718c76af8c5b29fa346aa8d
1 ///////////////////////////////////////////////////////////////////////////////
2 // MuldeR's Utilities for Qt
3 // Copyright (C) 2004-2017 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.
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
19 // http://www.gnu.org/licenses/lgpl-2.1.txt
20 //////////////////////////////////////////////////////////////////////////////////
22 //MUtils
23 #include <MUtils/GUI.h>
24 #include <MUtils/OSSupport.h>
26 //Internal
27 #include "Utils_Win32.h"
29 //Qt
30 #include <QIcon>
31 #include <QApplication>
32 #include <QWidget>
33 #include <QMutex>
34 #include <QDesktopWidget>
36 //Win32 API
37 #ifndef _INC_WINDOWS
38 #define WIN32_LEAN_AND_MEAN 1
39 #include <Windows.h>
40 #endif //_INC_WINDOWS
42 ///////////////////////////////////////////////////////////////////////////////
43 // BROADCAST
44 ///////////////////////////////////////////////////////////////////////////////
46 bool MUtils::GUI::broadcast(int eventType, const bool &onlyToVisible)
48 if(QApplication *app = dynamic_cast<QApplication*>(QApplication::instance()))
50 qDebug("Broadcasting %d", eventType);
52 bool allOk = true;
53 QEvent poEvent(static_cast<QEvent::Type>(eventType));
54 QWidgetList list = app->topLevelWidgets();
56 while(!list.isEmpty())
58 QWidget *widget = list.takeFirst();
59 if(!onlyToVisible || widget->isVisible())
61 if(!app->sendEvent(widget, &poEvent))
63 allOk = false;
68 qDebug("Broadcast %d done (%s)", eventType, (allOk ? "OK" : "Stopped"));
69 return allOk;
71 else
73 qWarning("Broadcast failed, could not get QApplication instance!");
74 return false;
78 ///////////////////////////////////////////////////////////////////////////////
79 // WINDOW ICON
80 ///////////////////////////////////////////////////////////////////////////////
82 namespace MUtils
84 namespace GUI
86 namespace Internal
88 class WindowIconHelper : public QObject
90 public:
91 WindowIconHelper(QWidget *const parent, const HICON hIcon, const bool &bIsBigIcon)
93 QObject(parent),
94 m_hIcon(hIcon)
96 SendMessage(reinterpret_cast<HWND>(parent->winId()), WM_SETICON, (bIsBigIcon ? ICON_BIG : ICON_SMALL), LPARAM(hIcon));
99 virtual ~WindowIconHelper(void)
101 if (m_hIcon)
103 DestroyIcon(m_hIcon);
107 private:
108 const HICON m_hIcon;
114 bool MUtils::GUI::set_window_icon(QWidget *const window, const QIcon &icon, const bool bIsBigIcon)
116 if ((!icon.isNull()) && window->winId())
118 const int extend = (bIsBigIcon ? 32 : 16);
119 if (const HICON hIcon = (HICON)MUtils::Win32Utils::qicon_to_hicon(&icon, extend, extend))
121 new Internal::WindowIconHelper(window, hIcon, bIsBigIcon); /*will be free'd using QObject parent mechanism*/
122 return true;
125 return false;
128 ///////////////////////////////////////////////////////////////////////////////
129 // BLINK WINDOW
130 ///////////////////////////////////////////////////////////////////////////////
132 static QMutex g_blinkMutex;
134 void MUtils::GUI::blink_window(QWidget *const poWindow, const unsigned int &count, const unsigned int &delay)
136 const double maxOpac = 1.0;
137 const double minOpac = 0.3;
138 const double delOpac = 0.1;
140 if (!g_blinkMutex.tryLock())
142 qWarning("Blinking is already in progress, skipping!");
143 return;
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();
152 for (unsigned int i = 0; i < count; i++)
154 for (double x = maxOpac; x >= minOpac; x -= delOpac)
156 poWindow->setWindowOpacity(x);
157 QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
158 MUtils::OS::sleep_ms(sleep);
161 for (double x = minOpac; x <= maxOpac; x += delOpac)
163 poWindow->setWindowOpacity(x);
164 QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
165 MUtils::OS::sleep_ms(sleep);
169 poWindow->setWindowOpacity(opacity);
170 QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
172 catch (...)
174 qWarning("Exception error while blinking!");
177 g_blinkMutex.unlock();
180 ///////////////////////////////////////////////////////////////////////////////
181 // DPI SCALING
182 ///////////////////////////////////////////////////////////////////////////////
184 double MUtils::GUI::dpi_scale(void)
186 if (const QApplication *const app = dynamic_cast<QApplication*>(QCoreApplication::instance()))
188 const double dpiX = static_cast<double>(app->desktop()->logicalDpiX());
189 const double dpiY = static_cast<double>(app->desktop()->logicalDpiY());
190 return qBound(1.0, ((dpiX + dpiY) / 192.0), 2.0);
192 return -1.0;
195 bool MUtils::GUI::scale_widget(QWidget *const widget, const bool recenter)
197 if (widget && (!widget->parentWidget()))
199 const double dpiScale = dpi_scale();
200 if ((dpiScale > 0.0) && (!qFuzzyCompare(dpiScale, 1.0)))
202 const QSize originalSize = widget->size();
203 widget->resize(qRound(originalSize.width() * dpiScale), qRound(originalSize.height() * dpiScale));
204 return recenter ? center_widget(widget) : true;
207 return false;
210 bool MUtils::GUI::center_widget(QWidget *const widget)
212 if (widget && (!widget->parentWidget()))
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;
219 return false;
222 ///////////////////////////////////////////////////////////////////////////////
223 // FORCE QUIT
224 ///////////////////////////////////////////////////////////////////////////////
226 void MUtils::GUI::force_quit(void)
228 qApp->closeAllWindows();
229 qApp->quit();
232 ///////////////////////////////////////////////////////////////////////////////