1 ///////////////////////////////////////////////////////////////////////////////
2 // MuldeR's Utilities for Qt
3 // Copyright (C) 2004-2014 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>
35 ///////////////////////////////////////////////////////////////////////////////
37 ///////////////////////////////////////////////////////////////////////////////
39 bool MUtils::GUI::broadcast(int eventType
, const bool &onlyToVisible
)
41 if(QApplication
*app
= dynamic_cast<QApplication
*>(QApplication::instance()))
43 qDebug("Broadcasting %d", eventType
);
46 QEvent
poEvent(static_cast<QEvent::Type
>(eventType
));
47 QWidgetList list
= app
->topLevelWidgets();
49 while(!list
.isEmpty())
51 QWidget
*widget
= list
.takeFirst();
52 if(!onlyToVisible
|| widget
->isVisible())
54 if(!app
->sendEvent(widget
, &poEvent
))
61 qDebug("Broadcast %d done (%s)", eventType
, (allOk
? "OK" : "Stopped"));
66 qWarning("Broadcast failed, could not get QApplication instance!");
71 ///////////////////////////////////////////////////////////////////////////////
73 ///////////////////////////////////////////////////////////////////////////////
81 class WindowIconHelper
: public QObject
84 WindowIconHelper(QWidget
*const parent
, const HICON hIcon
, const bool &bIsBigIcon
)
89 SendMessage(parent
->winId(), WM_SETICON
, (bIsBigIcon
? ICON_BIG
: ICON_SMALL
), LPARAM(hIcon
));
92 ~WindowIconHelper(void)
107 bool MUtils::GUI::set_window_icon(QWidget
*const window
, const QIcon
&icon
, const bool bIsBigIcon
)
109 if((!icon
.isNull()) && window
->winId())
111 const int extend
= (bIsBigIcon
? 32 : 16);
112 if(HICON hIcon
= qicon_to_hicon(icon
, extend
, extend
))
114 if(new Internal::WindowIconHelper(window
, hIcon
, bIsBigIcon
))
123 ///////////////////////////////////////////////////////////////////////////////
125 ///////////////////////////////////////////////////////////////////////////////
127 static QMutex g_blinkMutex
;
129 void MUtils::GUI::blink_window(QWidget
*const poWindow
, const unsigned int &count
, const unsigned int &delay
)
131 const double maxOpac
= 1.0;
132 const double minOpac
= 0.3;
133 const double delOpac
= 0.1;
135 if(!g_blinkMutex
.tryLock())
137 qWarning("Blinking is already in progress, skipping!");
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();
147 for(unsigned int i
= 0; i
< count
; i
++)
149 for(double x
= maxOpac
; x
>= minOpac
; x
-= delOpac
)
151 poWindow
->setWindowOpacity(x
);
152 QApplication::processEvents(QEventLoop::ExcludeUserInputEvents
);
153 MUtils::OS::sleep_ms(sleep
);
156 for(double x
= minOpac
; x
<= maxOpac
; x
+= delOpac
)
158 poWindow
->setWindowOpacity(x
);
159 QApplication::processEvents(QEventLoop::ExcludeUserInputEvents
);
160 MUtils::OS::sleep_ms(sleep
);
164 poWindow
->setWindowOpacity(opacity
);
165 QApplication::processEvents(QEventLoop::ExcludeUserInputEvents
);
169 qWarning("Exception error while blinking!");
172 g_blinkMutex
.unlock();
175 ///////////////////////////////////////////////////////////////////////////////
177 ///////////////////////////////////////////////////////////////////////////////
179 void MUtils::GUI::force_quit(void)
181 qApp
->closeAllWindows();
185 ///////////////////////////////////////////////////////////////////////////////