KTutorial  0.5.1
EditorSupport.cpp
00001 /***************************************************************************
00002  *   Copyright (C) 2010 by Daniel Calviño Sánchez <danxuliu@gmail.com>     *
00003  *   Copyright (C) 2011 by Daniel Calviño Sánchez <danxuliu@gmail.com>     *
00004  *   Copyright (C) 2012 by Daniel Calviño Sánchez <danxuliu@gmail.com>     *
00005  *                                                                         *
00006  *   This program is free software; you can redistribute it and/or modify  *
00007  *   it under the terms of the GNU General Public License as published by  *
00008  *   the Free Software Foundation; either version 2 of the License, or     *
00009  *   (at your option) any later version.                                   *
00010  *                                                                         *
00011  *   This program 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 General Public License for more details.                          *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU General Public License     *
00017  *   along with this program; If not, see <http://www.gnu.org/licenses/>.  *
00018  ***************************************************************************/
00019 
00020 #include "EditorSupport.h"
00021 
00022 #include <QWidget>
00023 #include <QtDBus/QtDBus>
00024 
00025 #include <KDebug>
00026 
00027 #include "ClassRegisterAdaptor.h"
00028 #include "EditorSupportAdaptor.h"
00029 #include "EventSpy.h"
00030 #include "EventSpyAdaptor.h"
00031 #include "ObjectRegister.h"
00032 #include "ObjectRegisterAdaptor.h"
00033 #include "../ObjectFinder.h"
00034 #include "../extendedinformation/WidgetHighlighterManager.h"
00035 #include "../scripting/ScriptedTutorial.h"
00036 
00037 using ktutorial::extendedinformation::WidgetHighlighterManager;
00038 using ktutorial::scripting::ScriptedTutorial;
00039 
00040 namespace ktutorial {
00041 extern int debugArea();
00042 }
00043 
00044 namespace ktutorial {
00045 namespace editorsupport {
00046 
00047 //public:
00048 
00049 EditorSupport::EditorSupport(QObject* parent /*= 0*/): QObject(parent),
00050     mObjectRegister(0),
00051     mEventSpy(0),
00052     mObjectFinder(0) {
00053 }
00054 
00055 void EditorSupport::setObjectFinder(ObjectFinder* objectFinder) {
00056     mObjectFinder = objectFinder;
00057 }
00058 
00059 void EditorSupport::setup(QObject* window) {
00060     if (!QDBusConnection::sessionBus().isConnected()) {
00061         kWarning(debugArea()) << "Cannot connect to the D-Bus session bus!"
00062                               << "KTutorial editor support will not be enabled";
00063         return;
00064     }
00065 
00066     mWindow = window;
00067 
00068     new EditorSupportAdaptor(this);
00069     QDBusConnection::sessionBus().registerObject("/ktutorial", this);
00070 
00071     mObjectRegister = new ObjectRegister(this);
00072     new ClassRegisterAdaptor(mObjectRegister);
00073     new ObjectRegisterAdaptor(mObjectRegister);
00074 
00075     QDBusConnection::sessionBus().registerObject("/ktutorial/ObjectRegister",
00076                                                  mObjectRegister);
00077 }
00078 
00079 int EditorSupport::mainWindowObjectId() {
00080     return mObjectRegister->idForObject(mWindow);
00081 }
00082 
00083 int EditorSupport::findObject(const QString& name) {
00084     Q_ASSERT(mObjectFinder);
00085 
00086     QObject* object = mObjectFinder->findObject<QObject*>(name, mWindow);
00087     return mObjectRegister->idForObject(object);
00088 }
00089 
00090 void EditorSupport::highlight(int objectId) {
00091     QObject* object = mObjectRegister->objectForId(objectId);
00092     QWidget* widget = qobject_cast<QWidget*>(object);
00093     if (!widget) {
00094         return;
00095     }
00096 
00097     WidgetHighlighterManager::self()->highlight(widget);
00098 }
00099 
00100 void EditorSupport::stopHighlighting(int objectId) {
00101     QObject* object = mObjectRegister->objectForId(objectId);
00102     QWidget* widget = qobject_cast<QWidget*>(object);
00103     if (!widget) {
00104         return;
00105     }
00106 
00107     WidgetHighlighterManager::self()->stopHighlighting(widget);
00108 }
00109 
00110 void EditorSupport::enableEventSpy() {
00111     mEventSpy = new EventSpy(this);
00112     mEventSpy->addObjectToSpy(mWindow);
00113     new EventSpyAdaptor(mEventSpy, mObjectRegister);
00114 
00115     QDBusConnection::sessionBus().registerObject("/ktutorial/EventSpy",
00116                                                  mEventSpy);
00117 }
00118 
00119 void EditorSupport::disableEventSpy() {
00120     QDBusConnection::sessionBus().unregisterObject("/ktutorial/EventSpy");
00121 
00122     delete mEventSpy;
00123     mEventSpy = 0;
00124 }
00125 
00126 void EditorSupport::testScriptedTutorial(const QString& filename,
00127                                          const QString& stepId) {
00128     ScriptedTutorial* scriptedTutorial = new ScriptedTutorial(filename);
00129 
00130     if (!scriptedTutorial->isValid()) {
00131         kWarning(debugArea()) << "Cannot test the scripted tutorial stored in"
00132                               << filename << ": the script is invalid";
00133         delete scriptedTutorial;
00134         return;
00135     }
00136 
00137     connect(scriptedTutorial, SIGNAL(finished(Tutorial*)),
00138             this, SLOT(deleteFinishedTestTutorial(Tutorial*)));
00139 
00140     emit started(scriptedTutorial);
00141 
00142     scriptedTutorial->start();
00143 
00144     if (!stepId.isEmpty()) {
00145         scriptedTutorial->nextStep(stepId);
00146     }
00147 }
00148 
00149 //private:
00150 
00151 void EditorSupport::deleteFinishedTestTutorial(Tutorial* tutorial) {
00152     tutorial->deleteLater();
00153 }
00154 
00155 
00156 }
00157 }