[Ktutorial-commits] SF.net SVN: ktutorial:[372] trunk/ktutorial/ktutorial-library
Status: Alpha
Brought to you by:
danxuliu
From: <dan...@us...> - 2012-08-20 10:59:49
|
Revision: 372 http://ktutorial.svn.sourceforge.net/ktutorial/?rev=372&view=rev Author: danxuliu Date: 2012-08-20 10:59:41 +0000 (Mon, 20 Aug 2012) Log Message: ----------- Add system for the applications using KTutorial to customize its user interface. Modified Paths: -------------- trunk/ktutorial/ktutorial-library/src/CMakeLists.txt trunk/ktutorial/ktutorial-library/src/KTutorial.cpp trunk/ktutorial/ktutorial-library/src/KTutorial.h trunk/ktutorial/ktutorial-library/src/Option.h trunk/ktutorial/ktutorial-library/src/Tutorial.h trunk/ktutorial/ktutorial-library/src/TutorialManager.h trunk/ktutorial/ktutorial-library/src/WaitForWindow.cpp trunk/ktutorial/ktutorial-library/src/scripting/ScriptingModule.cpp trunk/ktutorial/ktutorial-library/src/tutorials/UsingKTutorial.cpp trunk/ktutorial/ktutorial-library/tests/CMakeLists.txt Added Paths: ----------- trunk/ktutorial/ktutorial-library/src/KTutorialCustomization.cpp trunk/ktutorial/ktutorial-library/src/KTutorialCustomization.h trunk/ktutorial/ktutorial-library/src/customization/ trunk/ktutorial/ktutorial-library/src/customization/CMakeLists.txt trunk/ktutorial/ktutorial-library/src/customization/DefaultKdeCustomization.cpp trunk/ktutorial/ktutorial-library/src/customization/DefaultKdeCustomization.h trunk/ktutorial/ktutorial-library/tests/customization/ trunk/ktutorial/ktutorial-library/tests/customization/CMakeLists.txt trunk/ktutorial/ktutorial-library/tests/customization/DefaultKdeCustomizationTest.cpp Modified: trunk/ktutorial/ktutorial-library/src/CMakeLists.txt =================================================================== --- trunk/ktutorial/ktutorial-library/src/CMakeLists.txt 2012-08-15 02:08:03 UTC (rev 371) +++ trunk/ktutorial/ktutorial-library/src/CMakeLists.txt 2012-08-20 10:59:41 UTC (rev 372) @@ -3,6 +3,7 @@ add_definitions("-fPIC") add_subdirectory(common) +add_subdirectory(customization) add_subdirectory(extendedinformation) add_subdirectory(scripting) add_subdirectory(tutorials) @@ -20,6 +21,7 @@ set(ktutorial_LIB_SRCS KTutorial.cpp + KTutorialCustomization.cpp ObjectFinder.cpp Option.cpp Step.cpp @@ -40,7 +42,7 @@ kde4_add_library(ktutorial SHARED ${ktutorial_LIB_SRCS}) -target_link_libraries(ktutorial ktutorial_scripting ktutorial_tutorials ktutorial_view) +target_link_libraries(ktutorial ktutorial_customization ktutorial_scripting) if (QT_QTDBUS_FOUND) target_link_libraries(ktutorial ktutorial_editorsupport) Modified: trunk/ktutorial/ktutorial-library/src/KTutorial.cpp =================================================================== --- trunk/ktutorial/ktutorial-library/src/KTutorial.cpp 2012-08-15 02:08:03 UTC (rev 371) +++ trunk/ktutorial/ktutorial-library/src/KTutorial.cpp 2012-08-20 10:59:41 UTC (rev 372) @@ -16,46 +16,23 @@ * along with this program; If not, see <http://www.gnu.org/licenses/>. * ***************************************************************************/ -#include <kaction.h> -#include <kactioncollection.h> -#include <klocalizedstring.h> - #include "KTutorial.h" -#include "Tutorial.h" -#include "TutorialInformation.h" +#include "customization/DefaultKdeCustomization.h" #include "scripting/ScriptingModule.h" #include "scripting/ScriptManager.h" -#include "tutorials/UsingKTutorial.h" -#include "view/StepWidget.h" -#include "view/TutorialManagerDialog.h" #ifdef QT_QTDBUS_FOUND #include "editorsupport/EditorSupport.h" #endif +using ktutorial::customization::DefaultKdeCustomization; using ktutorial::scripting::ScriptingModule; using ktutorial::scripting::ScriptManager; -using ktutorial::view::StepWidget; -using ktutorial::view::TutorialManagerDialog; namespace ktutorial { //public: -//It inherits from QObject to be automatically destroyed when the parent -//KXmlGuiWindow is destroyed. It uses the same strategy as -//KDEPrivate::ToolBarHandler. -//Usually I do not like multiple inheritance, but it comes very handy here :) -class TutorialKXmlGuiClient: public QObject, public KXMLGUIClient { -Q_OBJECT -public: - TutorialKXmlGuiClient(KXmlGuiWindow* parent): - QObject(parent), KXMLGUIClient(parent) { - setComponentData(KComponentData("ktutorial")); - setXMLFile("ktutorialui.rc"); - } -}; - bool KTutorial::registerWaitForMetaObject(const QMetaObject& waitForMetaObject, const QString& typeName /*= QString()*/) { return ScriptingModule::self()->registerWaitForMetaObject(waitForMetaObject, @@ -63,79 +40,37 @@ } void KTutorial::setup(KXmlGuiWindow* window) { - TutorialKXmlGuiClient* tutorialClient= new TutorialKXmlGuiClient(window); + DefaultKdeCustomization* defaultKdeCustomization = + new DefaultKdeCustomization(window); + setup(defaultKdeCustomization); +} - mTutorialsAction = new KAction(window); - mTutorialsAction->setText(i18nc("@action:inmenu", "Tutorials...")); - tutorialClient->actionCollection()->addAction("tutorials", mTutorialsAction); - connect(mTutorialsAction, SIGNAL(triggered(bool)), - this, SLOT(showTutorialManagerDialog())); +void KTutorial::setup(KTutorialCustomization* ktutorialCustomization) { + mCustomization = ktutorialCustomization; + mCustomization->setParent(this); - connect(mTutorialmanager, SIGNAL(started(Tutorial*)), - this, SLOT(showStepWidget(Tutorial*))); + mCustomization->setup(mTutorialmanager); - connect(mTutorialmanager, SIGNAL(started(Tutorial*)), - this, SLOT(disableTutorialsAction())); - connect(mTutorialmanager, SIGNAL(finished()), - this, SLOT(enableTutorialsAction())); - - mParent = window; - - registerTutorial(new UsingKTutorial()); - ScriptManager().loadTutorials(mTutorialmanager); #ifdef QT_QTDBUS_FOUND editorsupport::EditorSupport* editorSupport = new editorsupport::EditorSupport(this); editorSupport->setObjectFinder(mObjectFinder); - editorSupport->setup(window); + editorSupport->setup(mainApplicationWindow()); connect(editorSupport, SIGNAL(started(Tutorial*)), - this, SLOT(showStepWidget(Tutorial*))); + ktutorialCustomization, SLOT(showTutorialUI(Tutorial*))); #endif } +QWidget* KTutorial::mainApplicationWindow() const { + return mCustomization->mainApplicationWindow(); +} + //private: KTutorial* KTutorial::sSelf = new KTutorial(); -//private slots: - -void KTutorial::showTutorialManagerDialog() const { - QDialog* dialog = new TutorialManagerDialog(mTutorialmanager, mParent); - dialog->setAttribute(Qt::WA_DeleteOnClose); - dialog->setModal(true); - dialog->setObjectName("ktutorial_TutorialManagerDialog"); - - dialog->show(); } -void KTutorial::showStepWidget(Tutorial* tutorial) const { - QString tutorialName = tutorial->tutorialInformation()->name(); - - StepWidget* stepWidget = new StepWidget(tutorialName, mParent); - stepWidget->setMainApplicationWindow(mParent); - stepWidget->setObjectName("ktutorial_StepWidget"); - connect(tutorial, SIGNAL(stepActivated(Step*)), - stepWidget, SLOT(setStep(Step*))); - connect(stepWidget, SIGNAL(finished()), tutorial, SLOT(finish())); - //Invalid tutorials finish just after being started. Deleting the StepWidget - //when the tutorial finishes ensures that it is deleted in those cases and, - //as deleteLater() is used, it does not interfere with the deletion when the - //StepWidget is closed by the user. - connect(tutorial, SIGNAL(finished(Tutorial*)), - stepWidget, SLOT(deleteLater())); -} - -void KTutorial::disableTutorialsAction() { - mTutorialsAction->setEnabled(false); -} - -void KTutorial::enableTutorialsAction() { - mTutorialsAction->setEnabled(true); -} - -} - -#include "moc_KTutorial.cpp" #include "KTutorial.moc" Modified: trunk/ktutorial/ktutorial-library/src/KTutorial.h =================================================================== --- trunk/ktutorial/ktutorial-library/src/KTutorial.h 2012-08-15 02:08:03 UTC (rev 371) +++ trunk/ktutorial/ktutorial-library/src/KTutorial.h 2012-08-20 10:59:41 UTC (rev 372) @@ -19,27 +19,35 @@ #ifndef KTUTORIAL_KTUTORIAL_H #define KTUTORIAL_KTUTORIAL_H -#include <kxmlguiwindow.h> +#include <QWidget> #include "ktutorial_export.h" #include "ObjectFinder.h" #include "TutorialManager.h" -class KAction; +class KXmlGuiWindow; namespace ktutorial { +class KTutorialCustomization; +} +namespace ktutorial { + /** * Main class of KTutorial. * This class is used to setup KTutorial, register WaitFor classes to be created * from scripts, register new Tutorials embedded in the application and find * objects in the Tutorials. It uses a Singleton design pattern. * - * When using KTutorial, KTutorial::setup(KxmlGuiWindow*) must be called to set - * up everything needed for KTutorial to work. It needs to know the main window - * of the application. + * When using KTutorial and no customization is needed, + * KTutorial::setup(KxmlGuiWindow*) must be called to set up everything needed + * for KTutorial to work. It needs to know the main window of the application. * + * If a KTutorial customization is needed, + * KTutorial::setup(KTutorialCustomization*) must be called instead providing + * the desired customization. + * * The setup method will be the first one to be called, but when custom WaitFor * objects are used in scripts. In that case, all the needed WaitFor classes * must be registered using registerWaitForMetaObject(const QMetaObject&) before @@ -50,14 +58,18 @@ * Just use KTutorial::registerTutorial(Tutorial*) so it is registered in the * system. Only registered tutorial are seen by the user. * - * Finally, any of the children objects of the main window of the application - * (which will usually be at least all widgets) can be got with - * KTutorial::findObject(const QString&), provided the looked for object has its - * object name set. + * Note, however, that when a customization is used, the WaitFor classes and the + * tutorials embedded in the application can be registered during the + * customization setup itself. * - * The TutorialManagerDialog is shown as modal through the "tutorials" action in - * Help menu. This action will be automatically disabled whenever a tutorial is - * being executed. + * When no customization is used, the TutorialManagerDialog is shown as modal + * through the "tutorials" action in the "Help" menu. This action will be + * automatically disabled whenever a tutorial is being executed. + * + * Finally, no matter whether a customization is used or not, any of the + * children objects of the main window of the application (which will usually be + * at least all widgets) can be got with KTutorial::findObject(const QString&), + * provided the looked for object has its object name set. */ class KTUTORIAL_EXPORT KTutorial: public QObject { Q_OBJECT @@ -86,10 +98,10 @@ const QString& typeName = QString()); /** - * Sets up everything for KTutorial to work. - * This is usually the first method that must be called when using KTutorial - * in an application. However, if any WaitFor class has to be registered, it - * must be done before calling this method. + * Sets up everything for KTutorial to work with the default customization. + * If no customization is needed, this is usually the first method that must + * be called when using KTutorial in an application. However, if any WaitFor + * class has to be registered, it must be done before calling this method. * * It adds the Tutorials action and the menu entry for it in Help menu and * loads the scripted tutorials from the application standard directories. @@ -99,6 +111,15 @@ void setup(KXmlGuiWindow* window); /** + * Sets up everything for KTutorial to work with the given customization. + * After setting up the customization, it loads the scripted tutorials from + * the application standard directories. + * + * @param ktutorialCustomization The customization to use. + */ + void setup(KTutorialCustomization* ktutorialCustomization); + + /** * Registers the Tutorial. * Only Tutorials with a identifier not added yet can be added. If the * Tutorial couldn't be added, false is returned. @@ -113,14 +134,11 @@ } /** - * Returns the parent widget. - * It is the main window of the application. + * Returns the main window of the application. * - * @return The parent widget. + * @return The main window of the application. */ - KXmlGuiWindow* parentWidget() const { - return mParent; - } + QWidget* mainApplicationWindow() const; /** * Returns the object with the specified name, if any. @@ -174,7 +192,7 @@ */ template <typename T> T findObject(const QString& name) const { - return mObjectFinder->findObject<T>(name, mParent); + return mObjectFinder->findObject<T>(name, mainApplicationWindow()); } private: @@ -190,62 +208,26 @@ TutorialManager* mTutorialmanager; /** - * The parent widget of the TutorialManagerDialog to show. + * The helper used to find objects. */ - KXmlGuiWindow* mParent; + ObjectFinder* mObjectFinder; /** - * The KAction to show the TutorialManagerDialog. + * The KTutorialCustomization used. */ - KAction* mTutorialsAction; + KTutorialCustomization* mCustomization; /** - * The helper used to find objects. - */ - ObjectFinder* mObjectFinder; - - /** * Creates a new KTutorial. * Private to avoid classes other than self to create instances. */ - KTutorial(): QObject(), + KTutorial(): mTutorialmanager(new TutorialManager()) { mTutorialmanager->setParent(this); - mTutorialsAction = 0; - mParent = 0; mObjectFinder = new ObjectFinder(this); + mCustomization = 0; } -private slots: - - /** - * Shows a modal TutorialManagerDialog. - * Called when the tutorials action is triggered. - */ - void showTutorialManagerDialog() const; - - /** - * Shows the GUI for the given Tutorial. - * Called when the tutorial is about to be started. - * - * @param tutorial The tutorial to show its StepWidget. - */ - void showStepWidget(Tutorial* tutorial) const; - - /** - * Disables mTutorialsAction. - * Just a wrapper to be connected with signals that can't pass false to the - * setEnabled(bool) slot. - */ - void disableTutorialsAction(); - - /** - * Enables mTutorialsAction. - * Just a wrapper to be connected with signals that can't pass true to the - * setEnabled(bool) slot. - */ - void enableTutorialsAction(); - }; } Added: trunk/ktutorial/ktutorial-library/src/KTutorialCustomization.cpp =================================================================== --- trunk/ktutorial/ktutorial-library/src/KTutorialCustomization.cpp (rev 0) +++ trunk/ktutorial/ktutorial-library/src/KTutorialCustomization.cpp 2012-08-20 10:59:41 UTC (rev 372) @@ -0,0 +1,21 @@ +/*************************************************************************** + * Copyright (C) 2012 by Daniel Calviño Sánchez * + * dan...@gm... * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 3 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; If not, see <http://www.gnu.org/licenses/>. * + ***************************************************************************/ + +#include "KTutorialCustomization.h" + +#include "KTutorialCustomization.moc" Property changes on: trunk/ktutorial/ktutorial-library/src/KTutorialCustomization.cpp ___________________________________________________________________ Added: svn:eol-style + native Added: trunk/ktutorial/ktutorial-library/src/KTutorialCustomization.h =================================================================== --- trunk/ktutorial/ktutorial-library/src/KTutorialCustomization.h (rev 0) +++ trunk/ktutorial/ktutorial-library/src/KTutorialCustomization.h 2012-08-20 10:59:41 UTC (rev 372) @@ -0,0 +1,110 @@ +/*************************************************************************** + * Copyright (C) 2012 by Daniel Calviño Sánchez * + * dan...@gm... * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 3 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; If not, see <http://www.gnu.org/licenses/>. * + ***************************************************************************/ + +#ifndef KTUTORIAL_KTUTORIALCUSTOMIZATION_H +#define KTUTORIAL_KTUTORIALCUSTOMIZATION_H + +#include <QObject> + +#include "ktutorial_export.h" + +namespace ktutorial { +class Tutorial; +class TutorialManager; +} + +namespace ktutorial { + +/** + * Interface to customize the UI of KTutorial. + * The classes that implement this interface are used by KTutorial to set up its + * user interface. You can provide your own class to KTutorial through + * KTutorial::setup(KTutorialCustomization*) to control the user interface for + * the tutorial manager and the tutorials. + * + * As this is an interface, all its methods must be implemented. + * + * Besides implementing the methods, you must provide your own classes for the + * tutorial manager and tutorial UIs. The tutorial manager UI must call the + * TutorialManager::start(QString) with the id of the tutorial to be started. If + * you need to know when the tutorial finishes (for example, to show again the + * tutorial manager UI, or to know when the tutorial UI is no longer needed), + * you can connect to the TutorialManager::finished() or the + * Tutorial::finished(Tutorial*) signals. Anyway, the tutorial UI must call + * Tutorial::finish() when the user finishes the tutorial from the UI. The + * tutorial UI must also emit the appropriate Option::selected() (connecting it + * to another signal) when the user selects an option in the UI. Finally, the + * tutorial UI must connect to the Tutorial::stepActivated(Step*) to know when + * a new step has to be shown. + * + * If you do not want to implement your own KTutorial user interface just use + * the default one for KDE through KTutorial::setup(KXmlGuiWindow*). + */ +class KTUTORIAL_EXPORT KTutorialCustomization: public QObject { +Q_OBJECT +public: + + /** + * Sets up the KTutorial user interface. + * This method will be called before loading the scripted tutorials, and + * before setting up the editor support (if available). + * + * Two things must be done in this method: preparing the user interface for + * the tutorial manager, and preparing the user interface for the tutorials. + * Note that preparing the UI for the tutorial manager also implies setting + * up a method to show the UI itself, for example, through a menu item. + * Also, to show the tutorial UI when a tutorial is started you can just + * connect the TutorialManager::started(Tutorial*) signal with the + * showTutorialUI(Tutorial*) slot in the KTutorialCustomization implementing + * class. + * + * Besides setting up the user interface, you may also register meta objects + * and tutorials here if needed. + * + * @param tutorialManager The TutorialManager used by KTutorial. + */ + virtual void setup(TutorialManager* tutorialManager) = 0; + + /** + * Returns the main window of the application. + * + * @return The main window of the application. + */ + virtual QWidget* mainApplicationWindow() = 0; + +public slots: + + /** + * Shows the tutorial UI. + * This method is used by KTutorial to show the UI of a tutorial when tested + * from the editor. + * + * Of course, it can be used when showing regular tutorials too, but it is + * the KTutorialCustomization implementing class the one which must call + * this method when needed, for example, connecting the slot to the + * started(Tutorial*) signal of the TutorialManager. + * + * @param tutorial The tutorial to show its UI. + */ + virtual void showTutorialUI(Tutorial* tutorial) = 0; + +}; + +} + +#endif Property changes on: trunk/ktutorial/ktutorial-library/src/KTutorialCustomization.h ___________________________________________________________________ Added: svn:eol-style + native Modified: trunk/ktutorial/ktutorial-library/src/Option.h =================================================================== --- trunk/ktutorial/ktutorial-library/src/Option.h 2012-08-15 02:08:03 UTC (rev 371) +++ trunk/ktutorial/ktutorial-library/src/Option.h 2012-08-20 10:59:41 UTC (rev 372) @@ -74,8 +74,10 @@ /** * This signal is emitted when this Option is selected by the user. - * Don't connect nor emit this signal yourself. It is connected - * automatically by KTutorial. + * + * If a KTutorial customization is used, you must emit this signal + * (connecting it to another signal) when the user selects the option in the + * UI. */ void selected(); Modified: trunk/ktutorial/ktutorial-library/src/Tutorial.h =================================================================== --- trunk/ktutorial/ktutorial-library/src/Tutorial.h 2012-08-15 02:08:03 UTC (rev 371) +++ trunk/ktutorial/ktutorial-library/src/Tutorial.h 2012-08-20 10:59:41 UTC (rev 372) @@ -151,8 +151,8 @@ * The current step is deactivated, this tutorial is cleaned, and finished * signal is emitted. * - * This slot is used internally. Do not call or connect to this slot - * yourself. + * If a KTutorial customization is used, this method must be called when the + * user finished the tutorial from the UI. */ void finish(); @@ -160,18 +160,20 @@ /** * This signal is emitted when this Tutorial finishes. - * Don't connect nor emit this signal yourself. It is connected - * automatically by KTutorial. * + * If a KTutorial customization is used, you may connect to this signal to + * know when the tutorial UI is no longer needed. + * * @param tutorial This tutorial. */ void finished(Tutorial* tutorial); /** * This signal is emitted when a Step is activated. - * Don't connect nor emit this signal yourself. It is connected - * automatically by KTutorial. * + * If a KTutorial customization is used, you must connect to this signal to + * know when the tutorial UI has to show a new step. + * * @param step The activated Step. */ void stepActivated(Step* step); Modified: trunk/ktutorial/ktutorial-library/src/TutorialManager.h =================================================================== --- trunk/ktutorial/ktutorial-library/src/TutorialManager.h 2012-08-15 02:08:03 UTC (rev 371) +++ trunk/ktutorial/ktutorial-library/src/TutorialManager.h 2012-08-20 10:59:41 UTC (rev 372) @@ -84,6 +84,9 @@ * emitted if there is no tutorial with the specified id. In this case, no * tutorial is started. * + * If a KTutorial customization is used, this method must be called when the + * tutorial has to be started. + * * @param id The id of the tutorial. */ void start(const QString& id); Modified: trunk/ktutorial/ktutorial-library/src/WaitForWindow.cpp =================================================================== --- trunk/ktutorial/ktutorial-library/src/WaitForWindow.cpp 2012-08-15 02:08:03 UTC (rev 371) +++ trunk/ktutorial/ktutorial-library/src/WaitForWindow.cpp 2012-08-20 10:59:41 UTC (rev 372) @@ -30,7 +30,7 @@ mConditionMet(false) { WindowVisibilitySpy* spy = new WindowVisibilitySpy(this); - spy->addWidgetToSpy(KTutorial::self()->parentWidget()); + spy->addWidgetToSpy(KTutorial::self()->mainApplicationWindow()); connect(spy, SIGNAL(windowShown(QWidget*)), this, SLOT(checkWindowShown(QWidget*))); } @@ -39,7 +39,7 @@ mConditionMet(false) { WindowVisibilitySpy* spy = new WindowVisibilitySpy(this); - spy->addWidgetToSpy(KTutorial::self()->parentWidget()); + spy->addWidgetToSpy(KTutorial::self()->mainApplicationWindow()); connect(spy, SIGNAL(windowShown(QWidget*)), this, SLOT(checkWindowShown(QWidget*))); Added: trunk/ktutorial/ktutorial-library/src/customization/CMakeLists.txt =================================================================== --- trunk/ktutorial/ktutorial-library/src/customization/CMakeLists.txt (rev 0) +++ trunk/ktutorial/ktutorial-library/src/customization/CMakeLists.txt 2012-08-20 10:59:41 UTC (rev 372) @@ -0,0 +1,9 @@ +include_directories(${CMAKE_CURRENT_BINARY_DIR} ${KDE4_INCLUDES}) + +set(ktutorial_customization_SRCS + DefaultKdeCustomization.cpp +) + +kde4_add_library(ktutorial_customization ${ktutorial_customization_SRCS}) + +target_link_libraries(ktutorial_customization ktutorial_tutorials ktutorial_view) Property changes on: trunk/ktutorial/ktutorial-library/src/customization/CMakeLists.txt ___________________________________________________________________ Added: svn:eol-style + native Added: trunk/ktutorial/ktutorial-library/src/customization/DefaultKdeCustomization.cpp =================================================================== --- trunk/ktutorial/ktutorial-library/src/customization/DefaultKdeCustomization.cpp (rev 0) +++ trunk/ktutorial/ktutorial-library/src/customization/DefaultKdeCustomization.cpp 2012-08-20 10:59:41 UTC (rev 372) @@ -0,0 +1,125 @@ +/*************************************************************************** + * Copyright (C) 2012 by Daniel Calviño Sánchez * + * dan...@gm... * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 3 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; If not, see <http://www.gnu.org/licenses/>. * + ***************************************************************************/ + +#include <KAction> +#include <KActionCollection> +#include <KLocalizedString> + +#include "DefaultKdeCustomization.h" +#include "../Tutorial.h" +#include "../TutorialInformation.h" +#include "../TutorialManager.h" +#include "../tutorials/UsingKTutorial.h" +#include "../view/StepWidget.h" +#include "../view/TutorialManagerDialog.h" + +using ktutorial::view::StepWidget; +using ktutorial::view::TutorialManagerDialog; + +namespace ktutorial { +namespace customization { + +//public: + +//It inherits from QObject to be automatically destroyed when the parent +//KXmlGuiWindow is destroyed. It uses the same strategy as +//KDEPrivate::ToolBarHandler. +//Usually I do not like multiple inheritance, but it comes very handy here :) +class TutorialKXmlGuiClient: public QObject, public KXMLGUIClient { +Q_OBJECT +public: + TutorialKXmlGuiClient(KXmlGuiWindow* parent): + QObject(parent), KXMLGUIClient(parent) { + setComponentData(KComponentData("ktutorial")); + setXMLFile("ktutorialui.rc"); + } +}; + +DefaultKdeCustomization::DefaultKdeCustomization(KXmlGuiWindow* window): + mWindow(window) { +} + +void DefaultKdeCustomization::setup(TutorialManager* tutorialManager) { + mTutorialManager = tutorialManager; + + TutorialKXmlGuiClient* tutorialClient= new TutorialKXmlGuiClient(mWindow); + + mTutorialsAction = new KAction(mWindow); + mTutorialsAction->setText(i18nc("@action:inmenu", "Tutorials...")); + tutorialClient->actionCollection()->addAction("tutorials", mTutorialsAction); + connect(mTutorialsAction, SIGNAL(triggered(bool)), + this, SLOT(showTutorialManagerDialog())); + + connect(mTutorialManager, SIGNAL(started(Tutorial*)), + this, SLOT(showTutorialUI(Tutorial*))); + + connect(mTutorialManager, SIGNAL(started(Tutorial*)), + this, SLOT(disableTutorialsAction())); + connect(mTutorialManager, SIGNAL(finished()), + this, SLOT(enableTutorialsAction())); + + mTutorialManager->registerTutorial(new UsingKTutorial()); +} + +QWidget* DefaultKdeCustomization::mainApplicationWindow() { + return mWindow; +} + +//public slots: + +void DefaultKdeCustomization::showTutorialUI(Tutorial* tutorial) { + QString tutorialName = tutorial->tutorialInformation()->name(); + + StepWidget* stepWidget = new StepWidget(tutorialName, mWindow); + stepWidget->setMainApplicationWindow(mWindow); + stepWidget->setObjectName("ktutorial_StepWidget"); + connect(tutorial, SIGNAL(stepActivated(Step*)), + stepWidget, SLOT(setStep(Step*))); + connect(stepWidget, SIGNAL(finished()), tutorial, SLOT(finish())); + //Invalid tutorials finish just after being started. Deleting the StepWidget + //when the tutorial finishes ensures that it is deleted in those cases and, + //as deleteLater() is used, it does not interfere with the deletion when the + //StepWidget is closed by the user. + connect(tutorial, SIGNAL(finished(Tutorial*)), + stepWidget, SLOT(deleteLater())); +} + +//private slots: + +void DefaultKdeCustomization::showTutorialManagerDialog() const { + QDialog* dialog = new TutorialManagerDialog(mTutorialManager, mWindow); + dialog->setAttribute(Qt::WA_DeleteOnClose); + dialog->setModal(true); + dialog->setObjectName("ktutorial_TutorialManagerDialog"); + + dialog->show(); +} + +void DefaultKdeCustomization::disableTutorialsAction() { + mTutorialsAction->setEnabled(false); +} + +void DefaultKdeCustomization::enableTutorialsAction() { + mTutorialsAction->setEnabled(true); +} + +} +} + +#include "moc_DefaultKdeCustomization.cpp" +#include "DefaultKdeCustomization.moc" Property changes on: trunk/ktutorial/ktutorial-library/src/customization/DefaultKdeCustomization.cpp ___________________________________________________________________ Added: svn:eol-style + native Added: trunk/ktutorial/ktutorial-library/src/customization/DefaultKdeCustomization.h =================================================================== --- trunk/ktutorial/ktutorial-library/src/customization/DefaultKdeCustomization.h (rev 0) +++ trunk/ktutorial/ktutorial-library/src/customization/DefaultKdeCustomization.h 2012-08-20 10:59:41 UTC (rev 372) @@ -0,0 +1,128 @@ +/*************************************************************************** + * Copyright (C) 2012 by Daniel Calviño Sánchez * + * dan...@gm... * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 3 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; If not, see <http://www.gnu.org/licenses/>. * + ***************************************************************************/ + +#ifndef KTUTORIAL_CUSTOMIZATION_DEFAULTKDECUSTOMIZATION_H +#define KTUTORIAL_CUSTOMIZATION_DEFAULTKDECUSTOMIZATION_H + +#include <KXmlGuiWindow> + +#include "../KTutorialCustomization.h" + +class KAction; + +namespace ktutorial { +namespace customization { + +/** + * Default KTutorial customization for KDE. + * The default customization adds a "Tutorials" menu entry in the "Help" menu + * that shows a TutorialManagerDialog; the tutorial UI shown is a StepWidget. It + * also registers the default tutorial, "Using KTutorial". + * + * Note that the default customization can be used only with XMLGUI applications + * (applications where the main window is a KXmlGuiWindow). In order to add the + * menu entry in the proper place in the menu a change must be done in your + * "applicationnameui.rc". Please refer to KTutorial manual for further + * information. + */ +class DefaultKdeCustomization: public KTutorialCustomization { +Q_OBJECT +public: + + /** + * Creates a new DefaultKdeCustomization for the given window. + * + * @param window The main window of the application. + */ + DefaultKdeCustomization(KXmlGuiWindow* window); + + /** + * Sets up the KTutorial user interface. + * It adds the Tutorials action and the menu entry for it in Help menu, and + * prepares the TutorialManagerDialog and the StepWidget to be shown when + * needed. + * + * Besides setting up the user interface, the default "Using KTutorial" + * tutorial is registered. + * + * @param tutorialManager The TutorialManager used by KTutorial. + */ + virtual void setup(TutorialManager* tutorialManager); + + /** + * Returns the main window of the application. + * + * @return The main window of the application. + */ + virtual QWidget* mainApplicationWindow(); + +public slots: + + /** + * Shows the tutorial UI. + * A StepWidget is used. + * + * @param tutorial The tutorial to show its StepWidget. + */ + virtual void showTutorialUI(Tutorial* tutorial); + +private: + + /** + * The main application window. + */ + KXmlGuiWindow* mWindow; + + /** + * The manager for the tutorials. + */ + TutorialManager* mTutorialManager; + + /** + * The KAction to show the TutorialManagerDialog. + */ + KAction* mTutorialsAction; + +private slots: + + /** + * Shows a modal TutorialManagerDialog. + * Called when the tutorials action is triggered. + */ + void showTutorialManagerDialog() const; + + /** + * Disables mTutorialsAction. + * Just a wrapper to be connected with signals that can't pass false to the + * setEnabled(bool) slot. + */ + void disableTutorialsAction(); + + /** + * Enables mTutorialsAction. + * Just a wrapper to be connected with signals that can't pass true to the + * setEnabled(bool) slot. + */ + void enableTutorialsAction(); + +}; + +} +} + +#endif Property changes on: trunk/ktutorial/ktutorial-library/src/customization/DefaultKdeCustomization.h ___________________________________________________________________ Added: svn:eol-style + native Modified: trunk/ktutorial/ktutorial-library/src/scripting/ScriptingModule.cpp =================================================================== --- trunk/ktutorial/ktutorial-library/src/scripting/ScriptingModule.cpp 2012-08-15 02:08:03 UTC (rev 371) +++ trunk/ktutorial/ktutorial-library/src/scripting/ScriptingModule.cpp 2012-08-20 10:59:41 UTC (rev 372) @@ -16,6 +16,8 @@ * along with this program; If not, see <http://www.gnu.org/licenses/>. * ***************************************************************************/ +#include <QMetaMethod> + #include <KDebug> #include "ScriptingModule.h" Modified: trunk/ktutorial/ktutorial-library/src/tutorials/UsingKTutorial.cpp =================================================================== --- trunk/ktutorial/ktutorial-library/src/tutorials/UsingKTutorial.cpp 2012-08-15 02:08:03 UTC (rev 371) +++ trunk/ktutorial/ktutorial-library/src/tutorials/UsingKTutorial.cpp 2012-08-20 10:59:41 UTC (rev 372) @@ -62,7 +62,8 @@ } virtual void setup() { - QTextEdit* textEdit = new QTextEdit(KTutorial::self()->parentWidget()); + QTextEdit* textEdit = new QTextEdit( + KTutorial::self()->mainApplicationWindow()); textEdit->setAttribute(Qt::WA_DeleteOnClose); textEdit->setWindowFlags(Qt::Window | Qt::WindowStaysOnTopHint); textEdit->setObjectName("usingKTutorialTextEdit"); Modified: trunk/ktutorial/ktutorial-library/tests/CMakeLists.txt =================================================================== --- trunk/ktutorial/ktutorial-library/tests/CMakeLists.txt 2012-08-15 02:08:03 UTC (rev 371) +++ trunk/ktutorial/ktutorial-library/tests/CMakeLists.txt 2012-08-20 10:59:41 UTC (rev 372) @@ -1,4 +1,5 @@ add_subdirectory(common) +add_subdirectory(customization) add_subdirectory(extendedinformation) add_subdirectory(scripting) add_subdirectory(view) Added: trunk/ktutorial/ktutorial-library/tests/customization/CMakeLists.txt =================================================================== --- trunk/ktutorial/ktutorial-library/tests/customization/CMakeLists.txt (rev 0) +++ trunk/ktutorial/ktutorial-library/tests/customization/CMakeLists.txt 2012-08-20 10:59:41 UTC (rev 372) @@ -0,0 +1,31 @@ +# Used by kde4_add_unit_test to set the full path to test executables +set(EXECUTABLE_OUTPUT_PATH ${CMAKE_CURRENT_BINARY_DIR}) + +include_directories(${CMAKE_CURRENT_BINARY_DIR} ${ktutorial-library_SOURCE_DIR}/src/customization ${KDE4_INCLUDES}) + +# Since Qt 4.6.0, this definition is needed for GUI testing. +# It is backwards compatible with previous Qt versions, unlike the alternative +# which is to add #include <QTestGui> in the test files. +add_definitions(-DQT_GUI_LIB) + +MACRO(UNIT_TESTS) + FOREACH(_className ${ARGN}) + set(_testName ${_className}Test) + kde4_add_unit_test(${_testName} TESTNAME ktutorial-${_testName} ${_testName}.cpp) + target_link_libraries(${_testName} ktutorial ktutorial_customization ${QT_QTTEST_LIBRARY}) + ENDFOREACH(_className) +ENDMACRO(UNIT_TESTS) + +unit_tests( + DefaultKdeCustomization +) + +MACRO(MEM_TESTS) + FOREACH(_testname ${ARGN}) + add_test(ktutorial-mem-${_testname} ${CMAKE_CURRENT_SOURCE_DIR}/../runMemcheck.py ${CMAKE_CURRENT_BINARY_DIR}/${_testname}Test ${CMAKE_CURRENT_BINARY_DIR}) + ENDFOREACH(_testname) +ENDMACRO(MEM_TESTS) + +mem_tests( + DefaultKdeCustomization +) Property changes on: trunk/ktutorial/ktutorial-library/tests/customization/CMakeLists.txt ___________________________________________________________________ Added: svn:eol-style + native Added: trunk/ktutorial/ktutorial-library/tests/customization/DefaultKdeCustomizationTest.cpp =================================================================== --- trunk/ktutorial/ktutorial-library/tests/customization/DefaultKdeCustomizationTest.cpp (rev 0) +++ trunk/ktutorial/ktutorial-library/tests/customization/DefaultKdeCustomizationTest.cpp 2012-08-20 10:59:41 UTC (rev 372) @@ -0,0 +1,154 @@ +/*************************************************************************** + * Copyright (C) 2012 by Daniel Calviño Sánchez * + * dan...@gm... * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 3 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; If not, see <http://www.gnu.org/licenses/>. * + ***************************************************************************/ + +#include <QtTest> + +#include <KAction> +#include <KActionCollection> + +#include "DefaultKdeCustomization.h" +#include "../Step.h" +#include "../Tutorial.h" +#include "../TutorialInformation.h" +#include "../TutorialManager.h" +#include "../view/StepTextWidget.h" +#include "../view/StepWidget.h" +#include "../view/TutorialManagerDialog.h" + +using ktutorial::Step; +using ktutorial::Tutorial; +using ktutorial::TutorialInformation; +using ktutorial::view::StepTextWidget; +using ktutorial::view::StepWidget; +using ktutorial::view::TutorialManagerDialog; + +namespace ktutorial { +namespace customization { + +class DefaultKdeCustomizationTest: public QObject { +Q_OBJECT +private slots: + + void initTestCase(); + + void testSetup(); + + void testMainApplicationWindow(); + + void testShowTutorialUI(); + + void testShowTutorialManagerUsingAction(); + +}; + +void DefaultKdeCustomizationTest::initTestCase() { + //Set the application name to prevent a QtDBus assert complaining that + //"Invalid object path given", as KXmlGuiWindow seems to register itself in + //DBus. + QApplication::setApplicationName("DefaultKdeCustomizationTest"); +} + +void DefaultKdeCustomizationTest::testSetup() { + KXmlGuiWindow window; + DefaultKdeCustomization customization(&window); + TutorialManager tutorialManager; + + QVERIFY(!window.actionCollection()->action("tutorials")); + QCOMPARE(tutorialManager.tutorialInformations().size(), 0); + + customization.setup(&tutorialManager); + + QAction* action = window.findChild<QAction*>("tutorials"); + QVERIFY(action); + QVERIFY(action->isEnabled()); + + QCOMPARE(tutorialManager.tutorialInformations().size(), 1); + QCOMPARE(tutorialManager.tutorialInformations()[0]->id(), + QString("usingKTutorial")); +} + +void DefaultKdeCustomizationTest::testMainApplicationWindow() { + KXmlGuiWindow window; + DefaultKdeCustomization customization(&window); + + QCOMPARE(customization.mainApplicationWindow(), &window); +} + +void DefaultKdeCustomizationTest::testShowTutorialUI() { + KXmlGuiWindow window; + DefaultKdeCustomization customization(&window); + + Tutorial tutorial(new TutorialInformation("test")); + + //Tutorial* must be registered in order to be used with QSignalSpy + qRegisterMetaType<Tutorial*>("Tutorial*"); + QSignalSpy finishedSpy(&tutorial, SIGNAL(finished(Tutorial*))); + + Step* startStep = new Step("start"); + startStep->setText("Start step"); + tutorial.addStep(startStep); + + Step* endStep = new Step("end"); + endStep->setText("End step"); + tutorial.addStep(endStep); + + customization.showTutorialUI(&tutorial); + tutorial.start(); + + StepWidget* stepWidget = window.findChild<StepWidget*>(); + QVERIFY(stepWidget); + + StepTextWidget* stepTextWidget = stepWidget->findChild<StepTextWidget*>(); + QVERIFY(stepTextWidget); + QCOMPARE(stepTextWidget->toPlainText(), QString("Start step")); + + tutorial.nextStep("end"); + + QCOMPARE(stepTextWidget->toPlainText(), QString("End step")); + + QCOMPARE(finishedSpy.count(), 0); + + stepWidget->close(); + + QCOMPARE(finishedSpy.count(), 1); +} + +void DefaultKdeCustomizationTest::testShowTutorialManagerUsingAction() { + KXmlGuiWindow window; + DefaultKdeCustomization customization(&window); + TutorialManager tutorialManager; + customization.setup(&tutorialManager); + + QAction* action = window.findChild<QAction*>("tutorials"); + QVERIFY(action); + QVERIFY(action->isEnabled()); + + action->trigger(); + TutorialManagerDialog* dialog = window.findChild<TutorialManagerDialog*>(); + + QVERIFY(dialog); + QVERIFY(dialog->isModal()); + QVERIFY(dialog->testAttribute(Qt::WA_DeleteOnClose)); +} + +} +} + +QTEST_MAIN(ktutorial::customization::DefaultKdeCustomizationTest) + +#include "DefaultKdeCustomizationTest.moc" Property changes on: trunk/ktutorial/ktutorial-library/tests/customization/DefaultKdeCustomizationTest.cpp ___________________________________________________________________ Added: svn:eol-style + native This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |