00001 /*************************************************************************** 00002 * Copyright (c) 2004 Werner Mayer <wmayer[at]users.sourceforge.net> * 00003 * * 00004 * This file is part of the FreeCAD CAx development system. * 00005 * * 00006 * This library is free software; you can redistribute it and/or * 00007 * modify it under the terms of the GNU Library General Public * 00008 * License as published by the Free Software Foundation; either * 00009 * version 2 of the License, or (at your option) any later version. * 00010 * * 00011 * This library is distributed in the hope that it will be useful, * 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00014 * GNU Library General Public License for more details. * 00015 * * 00016 * You should have received a copy of the GNU Library General Public * 00017 * License along with this library; see the file COPYING.LIB. If not, * 00018 * write to the Free Software Foundation, Inc., 59 Temple Place, * 00019 * Suite 330, Boston, MA 02111-1307, USA * 00020 * * 00021 ***************************************************************************/ 00022 00023 00024 #include "PreCompiled.h" 00025 00026 #ifndef _PreComp_ 00027 # include <QApplication> 00028 # include <QDateTime> 00029 # include <QMessageBox> 00030 # ifdef FC_OS_WIN32 00031 # include <windows.h> 00032 # endif 00033 #endif 00034 00035 #include "WaitCursor.h" 00036 00037 using namespace Gui; 00038 00039 namespace Gui { 00040 class WaitCursorP : public QObject 00041 { 00042 public: 00043 static WaitCursorP* getInstance(); 00044 void setBusy(bool); 00045 WaitCursor::FilterEventsFlags ignoreEvents() const; 00046 void setIgnoreEvents(WaitCursor::FilterEventsFlags flags); 00047 00048 protected: 00049 bool eventFilter(QObject*, QEvent*); 00050 bool isModalDialog(QObject* o) const; 00051 00052 private: 00053 WaitCursorP(); // Disable constructor 00054 static WaitCursorP* _instance; 00055 bool isOn; 00056 WaitCursor::FilterEventsFlags flags; 00057 }; 00058 } // namespace Gui 00059 00060 WaitCursorP* WaitCursorP::_instance = 0; 00061 00062 WaitCursorP::WaitCursorP() : QObject(0), isOn(false), flags(WaitCursor::AllEvents) 00063 { 00064 } 00065 00066 WaitCursorP* WaitCursorP::getInstance() 00067 { 00068 if (!_instance) 00069 _instance = new WaitCursorP(); 00070 return _instance; 00071 } 00072 00073 void WaitCursorP::setBusy(bool on) 00074 { 00075 if (on == this->isOn) 00076 return; 00077 00078 if (on) { 00079 qApp->installEventFilter(this); 00080 QApplication::setOverrideCursor(Qt::WaitCursor); 00081 } 00082 else { 00083 qApp->removeEventFilter(this); 00084 QApplication::restoreOverrideCursor(); 00085 } 00086 00087 this->isOn = on; 00088 } 00089 00090 WaitCursor::FilterEventsFlags WaitCursorP::ignoreEvents() const 00091 { 00092 return this->flags; 00093 } 00094 00095 void WaitCursorP::setIgnoreEvents(WaitCursor::FilterEventsFlags flags) 00096 { 00097 this->flags = flags; 00098 } 00099 00100 bool WaitCursorP::isModalDialog(QObject* o) const 00101 { 00102 QWidget* parent = qobject_cast<QWidget*>(o); 00103 while (parent) { 00104 QMessageBox* dlg = qobject_cast<QMessageBox*>(parent); 00105 if (dlg && dlg->isModal()) 00106 return true; 00107 parent = parent->parentWidget(); 00108 } 00109 00110 return false; 00111 } 00112 00113 bool WaitCursorP::eventFilter(QObject* o, QEvent* e) 00114 { 00115 // Note: This might cause problems when we want to open a modal dialog at the lifetime 00116 // of a WaitCursor instance because the incoming events are still filtered. 00117 if (e->type() == QEvent::KeyPress || 00118 e->type() == QEvent::KeyRelease) { 00119 if (isModalDialog(o)) 00120 return false; 00121 if (this->flags & WaitCursor::KeyEvents) 00122 return true; 00123 } 00124 if (e->type() == QEvent::MouseButtonPress || 00125 e->type() == QEvent::MouseButtonRelease || 00126 e->type() == QEvent::MouseButtonDblClick) { 00127 if (isModalDialog(o)) 00128 return false; 00129 if (this->flags & WaitCursor::MouseEvents) 00130 return true; 00131 } 00132 return false; 00133 } 00134 00135 int WaitCursor::instances = 0; 00136 00143 WaitCursor::WaitCursor() 00144 { 00145 if (instances++ == 0) 00146 setWaitCursor(); 00147 } 00148 00150 WaitCursor::~WaitCursor() 00151 { 00152 if (--instances == 0) 00153 restoreCursor(); 00154 } 00155 00159 void WaitCursor::setWaitCursor() 00160 { 00161 WaitCursorP::getInstance()->setBusy(true); 00162 } 00163 00167 void WaitCursor::restoreCursor() 00168 { 00169 WaitCursorP::getInstance()->setBusy(false); 00170 } 00171 00172 WaitCursor::FilterEventsFlags WaitCursor::ignoreEvents() const 00173 { 00174 return WaitCursorP::getInstance()->ignoreEvents(); 00175 } 00176 00177 void WaitCursor::setIgnoreEvents(FilterEventsFlags flags) 00178 { 00179 WaitCursorP::getInstance()->setIgnoreEvents(flags); 00180 }