KTutorial  0.5.1
WaitForEvent.cpp
00001 /***************************************************************************
00002  *   Copyright (C) 2010 by Daniel Calviño Sánchez <danxuliu@gmail.com>     *
00003  *   Copyright (C) 2012 by Daniel Calviño Sánchez <danxuliu@gmail.com>     *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; If not, see <http://www.gnu.org/licenses/>.  *
00017  ***************************************************************************/
00018 
00019 #include "WaitForEvent.h"
00020 #include "WaitForEvent_p.h"
00021 
00022 #include <QMetaEnum>
00023 
00024 #include <KDebug>
00025 
00026 namespace ktutorial {
00027 extern int debugArea();
00028 }
00029 
00030 namespace ktutorial {
00031 
00032 //public:
00033 
00034 WaitForEvent::WaitForEvent():
00035     d(new WaitForEventPrivate()) {
00036     d->mObject = 0;
00037     d->mEventType = QEvent::None;
00038     d->mConditionMet = false;
00039 }
00040 
00041 WaitForEvent::WaitForEvent(QObject* object, QEvent::Type type):
00042     d(new WaitForEventPrivate()) {
00043     d->mObject = object;
00044     d->mEventType = type;
00045     d->mConditionMet = false;
00046 
00047     if (!object) {
00048         kWarning(debugArea()) << "The object that receives the event to wait"
00049                               << "for is null!";
00050         return;
00051     }
00052 
00053     object->installEventFilter(this);
00054 }
00055 
00056 WaitForEvent::~WaitForEvent() {
00057     delete d;
00058 }
00059 
00060 void WaitForEvent::setEvent(QObject* object, const QString& typeName) {
00061     if (!object) {
00062         kWarning(debugArea()) << "The object that receives the event to wait"
00063                               << "for is null!";
00064         return;
00065     }
00066 
00067     int index = QEvent::staticMetaObject.indexOfEnumerator("Type");
00068     QMetaEnum eventTypeEnumerator = QEvent::staticMetaObject.enumerator(index);
00069 
00070     int eventTypeValue = eventTypeEnumerator.keyToValue(qPrintable(typeName));
00071     if (eventTypeValue == -1) {
00072         kWarning(debugArea()) << "QEvent::Type named" << typeName << "is"
00073                               << "unknown";
00074         return;
00075     }
00076 
00077     d->mObject = object;
00078     d->mEventType = static_cast<QEvent::Type>(eventTypeValue);
00079 
00080     d->mObject->installEventFilter(this);
00081 }
00082 
00083 bool WaitForEvent::eventFilter(QObject* object, QEvent* event) {
00084     if (!isActive()) {
00085         return false;
00086     }
00087 
00088     if (object == d->mObject && event->type() == d->mEventType) {
00089         handleEvent(event);
00090     }
00091 
00092     return false;
00093 }
00094 
00095 bool WaitForEvent::conditionMet() const {
00096     return d->mConditionMet;
00097 }
00098 
00099 void WaitForEvent::setActive(bool active) {
00100     WaitFor::setActive(active);
00101 
00102     if (active) {
00103         d->mConditionMet = false;
00104     }
00105 }
00106 
00107 //protected:
00108 
00109 void WaitForEvent::handleEvent(QEvent* event) {
00110     Q_UNUSED(event);
00111 
00112     d->mConditionMet = true;
00113     emit waitEnded(this);
00114 }
00115 
00116 }