KTutorial  0.5.1
WaitForProperty.cpp
00001 /***************************************************************************
00002  *   Copyright (C) 2011 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 "WaitForProperty.h"
00020 #include "WaitForProperty_p.h"
00021 
00022 #include <QMetaProperty>
00023 
00024 #include <KDebug>
00025 
00026 namespace ktutorial {
00027 extern int debugArea();
00028 }
00029 
00030 namespace ktutorial {
00031 
00032 //public:
00033 
00034 WaitForProperty::WaitForProperty(): WaitFor(),
00035     d(new WaitForPropertyPrivate()) {
00036 }
00037 
00038 WaitForProperty::WaitForProperty(QObject* object, const QString& propertyName,
00039                                  const QVariant& value): WaitFor(),
00040     d(new WaitForPropertyPrivate()) {
00041     setProperty(object, propertyName, value);
00042 }
00043 
00044 WaitForProperty::~WaitForProperty() {
00045     delete d;
00046 }
00047 
00048 void WaitForProperty::setProperty(QObject* object, const QString& propertyName,
00049                                   const QVariant& value) {
00050     d->mObject = object;
00051     d->mPropertyName = propertyName;
00052     d->mValue = value;
00053 
00054     if (!d->mObject) {
00055         kWarning(debugArea()) << "The object that contains the property"
00056                               << d->mPropertyName << "to wait for is null!";
00057         return;
00058     }
00059 
00060     const QMetaObject* metaObject = d->mObject->metaObject();
00061     int propertyIndex = metaObject->indexOfProperty(d->mPropertyName.toUtf8());
00062 
00063     if (propertyIndex == -1) {
00064         kWarning(debugArea()) << "The class" << metaObject->className()
00065                               << "does not contain a property named"
00066                               << d->mPropertyName << "!";
00067         return;
00068     }
00069 
00070     QMetaProperty metaProperty = metaObject->property(propertyIndex);
00071 
00072     if (!metaProperty.hasNotifySignal()) {
00073         kWarning(debugArea()) << "The property" << d->mPropertyName << "in the"
00074                               << "class" << metaObject->className()
00075                               << "does not have a notification signal!";
00076         return;
00077     }
00078 
00079     const char* notifySignalSignature = metaProperty.notifySignal().signature();
00080     QString notifySignal = QString("2%1").arg(notifySignalSignature);
00081 
00082     connect(object, notifySignal.toUtf8(),
00083             this, SLOT(checkPropertyValueToEndTheWait()));
00084 }
00085 
00086 void WaitForProperty::setPropertyToWaitFor(QObject* object,
00087                                            const QString& propertyName,
00088                                            const QVariant& value) {
00089     setProperty(object, propertyName, value);
00090 }
00091 
00092 bool WaitForProperty::conditionMet() const {
00093     if (!d->mObject) {
00094         return false;
00095     }
00096 
00097     if (d->mObject->property(d->mPropertyName.toUtf8()) != d->mValue) {
00098         return false;
00099     }
00100 
00101     return true;
00102 }
00103 
00104 //private slots:
00105 
00106 void WaitForProperty::checkPropertyValueToEndTheWait() {
00107     if (!isActive()) {
00108         return;
00109     }
00110 
00111     if (conditionMet()) {
00112         emit waitEnded(this);
00113     }
00114 }
00115 
00116 }