KTutorial  0.5.1
WaitForSignal.cpp
00001 /***************************************************************************
00002  *   Copyright (C) 2008 by Daniel Calviño Sánchez <danxuliu@gmail.com>     *
00003  *   Copyright (C) 2009 by Daniel Calviño Sánchez <danxuliu@gmail.com>     *
00004  *   Copyright (C) 2010 by Daniel Calviño Sánchez <danxuliu@gmail.com>     *
00005  *   Copyright (C) 2012 by Daniel Calviño Sánchez <danxuliu@gmail.com>     *
00006  *                                                                         *
00007  *   This program is free software; you can redistribute it and/or modify  *
00008  *   it under the terms of the GNU General Public License as published by  *
00009  *   the Free Software Foundation; either version 2 of the License, or     *
00010  *   (at your option) any later version.                                   *
00011  *                                                                         *
00012  *   This program is distributed in the hope that it will be useful,       *
00013  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00014  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00015  *   GNU General Public License for more details.                          *
00016  *                                                                         *
00017  *   You should have received a copy of the GNU General Public License     *
00018  *   along with this program; If not, see <http://www.gnu.org/licenses/>.  *
00019  ***************************************************************************/
00020 
00021 #include "WaitForSignal.h"
00022 #include "WaitForSignal_p.h"
00023 
00024 #include <KDebug>
00025 
00026 namespace ktutorial {
00027 extern int debugArea();
00028 }
00029 
00030 namespace ktutorial {
00031 
00032 //public:
00033 
00034 WaitForSignal::WaitForSignal(): WaitFor(),
00035     d(new WaitForSignalPrivate()) {
00036     d->mConditionMet = false;
00037 }
00038 
00039 WaitForSignal::WaitForSignal(QObject* sender, const QString& signal): WaitFor(),
00040     d(new WaitForSignalPrivate()) {
00041     d->mConditionMet = false;
00042     setSignal(sender, signal);
00043 }
00044 
00045 WaitForSignal::~WaitForSignal() {
00046     delete d;
00047 }
00048 
00049 void WaitForSignal::setSignal(QObject* sender, const QString& signal) {
00050     if (!sender) {
00051         kWarning(debugArea()) << "The object that emits the signal" << signal
00052                               << "to wait for is null!";
00053         return;
00054     }
00055 
00056     QString signalName = signal;
00057     if (!signalName.startsWith('2')) {
00058         signalName = QString("2%1").arg(signal);
00059     }
00060 
00061     connect(sender, signalName.toLatin1(), this, SLOT(signalWaitEnd()));
00062 }
00063 
00064 bool WaitForSignal::conditionMet() const {
00065     return d->mConditionMet;
00066 }
00067 
00068 void WaitForSignal::setActive(bool active) {
00069     WaitFor::setActive(active);
00070 
00071     if (active) {
00072         d->mConditionMet = false;
00073     }
00074 }
00075 
00076 //public slots:
00077 
00078 void WaitForSignal::signalWaitEnd() {
00079     if (!isActive()) {
00080         return;
00081     }
00082 
00083     d->mConditionMet = true;
00084     emit waitEnded(this);
00085 }
00086 
00087 }