KTutorial  0.5.1
DefaultKdeCustomization.cpp
00001 /***************************************************************************
00002  *   Copyright (C) 2012 by Daniel Calviño Sánchez <danxuliu@gmail.com>     *
00003  *                                                                         *
00004  *   This program is free software; you can redistribute it and/or modify  *
00005  *   it under the terms of the GNU General Public License as published by  *
00006  *   the Free Software Foundation; either version 2 of the License, or     *
00007  *   (at your option) any later version.                                   *
00008  *                                                                         *
00009  *   This program is distributed in the hope that it will be useful,       *
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00012  *   GNU General Public License for more details.                          *
00013  *                                                                         *
00014  *   You should have received a copy of the GNU General Public License     *
00015  *   along with this program; If not, see <http://www.gnu.org/licenses/>.  *
00016  ***************************************************************************/
00017 
00018 #include "DefaultKdeCustomization.h"
00019 
00020 #include <KAction>
00021 #include <KActionCollection>
00022 #include <KLocalizedString>
00023 #include <KXmlGuiWindow>
00024 
00025 #include "../Tutorial.h"
00026 #include "../TutorialInformation.h"
00027 #include "../TutorialManager.h"
00028 #include "../tutorials/UsingKTutorial.h"
00029 #include "../view/StepWidget.h"
00030 #include "../view/TutorialManagerDialog.h"
00031 
00032 using ktutorial::view::StepWidget;
00033 using ktutorial::view::TutorialManagerDialog;
00034 
00035 namespace ktutorial {
00036 namespace customization {
00037 
00038 //public:
00039 
00040 //It inherits from QObject to be automatically destroyed when the parent
00041 //KXmlGuiWindow is destroyed. It uses the same strategy as
00042 //KDEPrivate::ToolBarHandler.
00043 //Usually I do not like multiple inheritance, but it comes very handy here :)
00044 class TutorialKXmlGuiClient: public QObject, public KXMLGUIClient {
00045 Q_OBJECT
00046 public:
00047     TutorialKXmlGuiClient(KXmlGuiWindow* parent):
00048             QObject(parent), KXMLGUIClient(parent) {
00049         setComponentData(KComponentData("ktutorial"));
00050         setXMLFile("ktutorialui.rc");
00051     }
00052 };
00053 
00054 DefaultKdeCustomization::DefaultKdeCustomization(KXmlGuiWindow* window):
00055     mWindow(window) {
00056 }
00057 
00058 void DefaultKdeCustomization::setup(TutorialManager* tutorialManager) {
00059     mTutorialManager = tutorialManager;
00060 
00061     TutorialKXmlGuiClient* tutorialClient= new TutorialKXmlGuiClient(mWindow);
00062 
00063     mTutorialsAction = new KAction(mWindow);
00064     mTutorialsAction->setText(i18nc("@action:inmenu", "Tutorials..."));
00065     tutorialClient->actionCollection()->addAction("tutorials", mTutorialsAction);
00066     connect(mTutorialsAction, SIGNAL(triggered(bool)),
00067             this, SLOT(showTutorialManagerDialog()));
00068 
00069     connect(mTutorialManager, SIGNAL(started(Tutorial*)),
00070             this, SLOT(showTutorialUI(Tutorial*)));
00071 
00072     connect(mTutorialManager, SIGNAL(started(Tutorial*)),
00073             this, SLOT(disableTutorialsAction()));
00074     connect(mTutorialManager, SIGNAL(finished()),
00075             this, SLOT(enableTutorialsAction()));
00076 
00077     mTutorialManager->registerTutorial(new UsingKTutorial());
00078 }
00079 
00080 QWidget* DefaultKdeCustomization::mainApplicationWindow() {
00081     return mWindow;
00082 }
00083 
00084 //public slots:
00085 
00086 void DefaultKdeCustomization::showTutorialUI(Tutorial* tutorial) {
00087     QString tutorialName = tutorial->tutorialInformation()->name();
00088 
00089     StepWidget* stepWidget = new StepWidget(tutorialName, mWindow);
00090     stepWidget->setMainApplicationWindow(mWindow);
00091     stepWidget->setObjectName("ktutorial_StepWidget");
00092     connect(tutorial, SIGNAL(stepActivated(Step*)),
00093             stepWidget, SLOT(setStep(Step*)));
00094     connect(stepWidget, SIGNAL(finished()), tutorial, SLOT(finish()));
00095     //Invalid tutorials finish just after being started. Deleting the StepWidget
00096     //when the tutorial finishes ensures that it is deleted in those cases and,
00097     //as deleteLater() is used, it does not interfere with the deletion when the
00098     //StepWidget is closed by the user.
00099     connect(tutorial, SIGNAL(finished(Tutorial*)),
00100             stepWidget, SLOT(deleteLater()));
00101 }
00102 
00103 //private slots:
00104 
00105 void DefaultKdeCustomization::showTutorialManagerDialog() const {
00106     QDialog* dialog = //krazy:exclude=qclasses
00107                 new TutorialManagerDialog(mTutorialManager, mWindow);
00108     dialog->setAttribute(Qt::WA_DeleteOnClose);
00109     dialog->setModal(true);
00110     dialog->setObjectName("ktutorial_TutorialManagerDialog");
00111 
00112     dialog->show();
00113 }
00114 
00115 void DefaultKdeCustomization::disableTutorialsAction() {
00116     mTutorialsAction->setEnabled(false);
00117 }
00118 
00119 void DefaultKdeCustomization::enableTutorialsAction() {
00120     mTutorialsAction->setEnabled(true);
00121 }
00122 
00123 }
00124 }
00125 
00126 #include "moc_DefaultKdeCustomization.cpp"
00127 #include "DefaultKdeCustomization.moc"