1 ///////////////////////////////////////////////////////////////////////////////
2 // MuldeR's Utilities for Qt
3 // Copyright (C) 2004-2016 LoRd_MuldeR <MuldeR2@GMX.de>
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.
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 //////////////////////////////////////////////////////////////////////////////////
23 #include <MUtils/GUI.h>
24 #include <MUtils/OSSupport.h>
27 #include "Utils_Win32.h"
31 #include <QApplication>
37 #define WIN32_LEAN_AND_MEAN 1
41 ///////////////////////////////////////////////////////////////////////////////
43 ///////////////////////////////////////////////////////////////////////////////
45 bool MUtils::GUI::broadcast(int eventType
, const bool &onlyToVisible
)
47 if(QApplication
*app
= dynamic_cast<QApplication
*>(QApplication::instance()))
49 qDebug("Broadcasting %d", eventType
);
52 QEvent
poEvent(static_cast<QEvent::Type
>(eventType
));
53 QWidgetList list
= app
->topLevelWidgets();
55 while(!list
.isEmpty())
57 QWidget
*widget
= list
.takeFirst();
58 if(!onlyToVisible
|| widget
->isVisible())
60 if(!app
->sendEvent(widget
, &poEvent
))
67 qDebug("Broadcast %d done (%s)", eventType
, (allOk
? "OK" : "Stopped"));
72 qWarning("Broadcast failed, could not get QApplication instance!");
77 ///////////////////////////////////////////////////////////////////////////////
79 ///////////////////////////////////////////////////////////////////////////////
87 class WindowIconHelper
: public QObject
90 WindowIconHelper(QWidget
*const parent
, const HICON hIcon
, const bool &bIsBigIcon
)
95 SendMessage(parent
->winId(), WM_SETICON
, (bIsBigIcon
? ICON_BIG
: ICON_SMALL
), LPARAM(hIcon
));
98 virtual ~WindowIconHelper(void)
102 DestroyIcon(m_hIcon
);
113 bool MUtils::GUI::set_window_icon(QWidget
*const window
, const QIcon
&icon
, const bool bIsBigIcon
)
115 if((!icon
.isNull()) && window
->winId())
117 const int extend
= (bIsBigIcon
? 32 : 16);
118 if(const HICON hIcon
= (HICON
) MUtils::Win32Utils::qicon_to_hicon(icon
, extend
, extend
))
120 new Internal::WindowIconHelper(window
, hIcon
, bIsBigIcon
); /*will be free'd using QObject parent mechanism*/
127 ///////////////////////////////////////////////////////////////////////////////
129 ///////////////////////////////////////////////////////////////////////////////
131 static QMutex g_blinkMutex
;
133 void MUtils::GUI::blink_window(QWidget
*const poWindow
, const unsigned int &count
, const unsigned int &delay
)
135 const double maxOpac
= 1.0;
136 const double minOpac
= 0.3;
137 const double delOpac
= 0.1;
139 if(!g_blinkMutex
.tryLock())
141 qWarning("Blinking is already in progress, skipping!");
147 const int steps
= static_cast<int>(ceil(maxOpac
- minOpac
) / delOpac
);
148 const int sleep
= static_cast<int>(floor(static_cast<double>(delay
) / static_cast<double>(steps
)));
149 const double opacity
= poWindow
->windowOpacity();
151 for(unsigned int i
= 0; i
< count
; i
++)
153 for(double x
= maxOpac
; x
>= minOpac
; x
-= delOpac
)
155 poWindow
->setWindowOpacity(x
);
156 QApplication::processEvents(QEventLoop::ExcludeUserInputEvents
);
157 MUtils::OS::sleep_ms(sleep
);
160 for(double x
= minOpac
; x
<= maxOpac
; x
+= delOpac
)
162 poWindow
->setWindowOpacity(x
);
163 QApplication::processEvents(QEventLoop::ExcludeUserInputEvents
);
164 MUtils::OS::sleep_ms(sleep
);
168 poWindow
->setWindowOpacity(opacity
);
169 QApplication::processEvents(QEventLoop::ExcludeUserInputEvents
);
173 qWarning("Exception error while blinking!");
176 g_blinkMutex
.unlock();
179 ///////////////////////////////////////////////////////////////////////////////
181 ///////////////////////////////////////////////////////////////////////////////
183 void MUtils::GUI::force_quit(void)
185 qApp
->closeAllWindows();
189 ///////////////////////////////////////////////////////////////////////////////