[Ktutorial-commits] SF.net SVN: ktutorial:[223] trunk/ktutorial/ktutorial-editor/src
Status: Alpha
Brought to you by:
danxuliu
|
From: <dan...@us...> - 2010-03-31 06:55:39
|
Revision: 223
http://ktutorial.svn.sourceforge.net/ktutorial/?rev=223&view=rev
Author: danxuliu
Date: 2010-03-31 06:55:32 +0000 (Wed, 31 Mar 2010)
Log Message:
-----------
Refactor KTutorialEditor class to extract file related actions to its own class, FileActions.
Modified Paths:
--------------
trunk/ktutorial/ktutorial-editor/src/CMakeLists.txt
trunk/ktutorial/ktutorial-editor/src/KTutorialEditor.cpp
trunk/ktutorial/ktutorial-editor/src/KTutorialEditor.h
Added Paths:
-----------
trunk/ktutorial/ktutorial-editor/src/FileActions.cpp
trunk/ktutorial/ktutorial-editor/src/FileActions.h
Modified: trunk/ktutorial/ktutorial-editor/src/CMakeLists.txt
===================================================================
--- trunk/ktutorial/ktutorial-editor/src/CMakeLists.txt 2010-03-31 05:33:29 UTC (rev 222)
+++ trunk/ktutorial/ktutorial-editor/src/CMakeLists.txt 2010-03-31 06:55:32 UTC (rev 223)
@@ -11,6 +11,7 @@
set(ktutorial_editor_SRCS
Exception.cpp
+ FileActions.cpp
KTutorialEditor.cpp
)
Added: trunk/ktutorial/ktutorial-editor/src/FileActions.cpp
===================================================================
--- trunk/ktutorial/ktutorial-editor/src/FileActions.cpp (rev 0)
+++ trunk/ktutorial/ktutorial-editor/src/FileActions.cpp 2010-03-31 06:55:32 UTC (rev 223)
@@ -0,0 +1,268 @@
+/***************************************************************************
+ * Copyright (C) 2010 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 "FileActions.h"
+
+#include <KActionCollection>
+#include <KFileDialog>
+#include <KFileFilterCombo>
+#include <KLocalizedString>
+#include <KMessageBox>
+#include <KRecentFilesAction>
+#include <KStandardAction>
+
+#include "KTutorialEditor.h"
+#include "data/Tutorial.h"
+#include "serialization/Serialization.h"
+
+//public:
+
+FileActions::FileActions(KTutorialEditor* tutorialEditor):
+ QObject(tutorialEditor),
+ mTutorialEditor(tutorialEditor),
+ mClean(true),
+ mRecentFilesAction(0) {
+
+ setupActions();
+
+ connect(tutorialEditor, SIGNAL(cleanChanged(bool)),
+ this, SLOT(handleUndoStackCleanChanged(bool)));
+}
+
+FileActions::~FileActions() {
+ mRecentFilesAction->saveEntries(KGlobal::config()->group("RecentFiles"));
+}
+
+const KUrl& FileActions::tutorialUrl() const {
+ return mTutorialUrl;
+}
+
+bool FileActions::queryCloseTutorial() {
+ if (mClean) {
+ return true;
+ }
+
+ QString text = i18nc("@label", "The tutorial has been modified.<nl/>"
+"Do you want to save your changes or discard them?");
+ QString caption = i18nc("@title:window", "Close tutorial");
+ int button = KMessageBox::warningYesNoCancel(mTutorialEditor, text, caption,
+ KStandardGuiItem::save(),
+ KStandardGuiItem::discard());
+
+ if (button == KMessageBox::Cancel) {
+ return false;
+ }
+
+ if (button == KMessageBox::Yes) {
+ return saveTutorial();
+ }
+
+ return true;
+}
+
+//public slots:
+
+//Don't use a reference to the URL, as if it is the argument in
+//urlSelected(KUrl) signal emitted by mRecentFilesAction, it is removed in
+//mRecentFilesAction->addUrl(KUrl), which leads to a crash
+void FileActions::loadTutorialFromUrl(KUrl url) {
+ if (!queryCloseTutorial()) {
+ return;
+ }
+
+ Tutorial* tutorial;
+ try {
+ tutorial = Serialization(mTutorialEditor).loadTutorial(url);
+ } catch (IOException e) {
+ QString text = i18nc("@label", "There was a problem when trying to "
+"open the file:<nl/>%1", e.message());
+ QString caption = i18nc("@title:window", "File could not be read");
+ KMessageBox::error(mTutorialEditor, text, caption);
+ return;
+ } catch (DeserializationException e) {
+ QString text = i18nc("@label", "There was a problem when trying to "
+"load the tutorial:<nl/>%1", e.message());
+ QString caption = i18nc("@title:window", "Tutorial could not be "
+"loaded");
+ KMessageBox::error(mTutorialEditor, text, caption);
+ return;
+ }
+
+ mTutorialEditor->setTutorialToBeEdited(tutorial);
+ mRecentFilesAction->addUrl(url);
+ mTutorialUrl = url;
+ mTutorialEditor->setClean();
+}
+
+void FileActions::newTutorial() {
+ if (!queryCloseTutorial()) {
+ return;
+ }
+
+ mTutorialEditor->setTutorialToBeEdited();
+ mTutorialUrl = KUrl();
+ //Force clean state, as clearing an empty stack (in setTutorialToBeEdited())
+ //would not emit cleanChanged()
+ mTutorialEditor->setClean();
+}
+
+void FileActions::handleUndoStackCleanChanged(bool clean) {
+ mClean = clean;
+
+ KActionCollection* actionCollection = mTutorialEditor->actionCollection();
+ if (clean && !mTutorialUrl.isEmpty()) {
+ actionCollection->action("file_save")->setEnabled(false);
+ } else {
+ actionCollection->action("file_save")->setEnabled(true);
+ }
+}
+
+//private:
+
+void FileActions::setupActions() {
+ KActionCollection* actionCollection = mTutorialEditor->actionCollection();
+
+ KStandardAction::openNew(this, SLOT(newTutorial()), actionCollection);
+
+ KStandardAction::open(this, SLOT(openTutorial()), actionCollection);
+
+ mRecentFilesAction = KStandardAction::openRecent(
+ this, SLOT(loadTutorialFromUrl(KUrl)), actionCollection);
+ mRecentFilesAction->loadEntries(KGlobal::config()->group("RecentFiles"));
+
+ KStandardAction::save(this, SLOT(saveTutorial()), actionCollection);
+
+ KStandardAction::saveAs(this, SLOT(saveTutorialAs()), actionCollection);
+
+ KAction* action = new KAction(this);
+ action->setText(i18nc("@action", "Export..."));
+ action->setStatusTip(i18nc("@info:status", "Exports the tutorial to a "
+"script."));
+ action->setIcon(KIcon("document-export"));
+ actionCollection->addAction("exportTutorial", action);
+ connect(action, SIGNAL(triggered(bool)), this, SLOT(exportTutorial()));
+}
+
+//private slots:
+
+void FileActions::openTutorial() {
+ KUrl url = mTutorialUrl;
+ QPointer<KFileDialog> dialog = new KFileDialog(url, QString(),
+ mTutorialEditor);
+
+ dialog->setCaption(i18nc("@title", "Open Tutorial"));
+ dialog->setOperationMode(KFileDialog::Opening);
+ dialog->setFilter(i18nc("@item:inlistbox A KFileDialog filter",
+ "*.xml|XML file"));
+
+ if (dialog->exec() == QDialog::Rejected) {
+ return;
+ }
+
+ loadTutorialFromUrl(dialog->selectedUrl());
+}
+
+bool FileActions::saveTutorial() {
+ if (mTutorialUrl.isEmpty()) {
+ return saveTutorialAs();
+ }
+
+ const Tutorial* tutorial = mTutorialEditor->tutorial();
+ try {
+ Serialization(mTutorialEditor).saveTutorial(tutorial, mTutorialUrl);
+ } catch (IOException e) {
+ QString text = i18nc("@label", "There was a problem when trying to "
+"save the tutorial:<nl/>%1", e.message());
+ QString caption = i18nc("@title:window", "Tutorial could not be saved");
+ KMessageBox::error(mTutorialEditor, text, caption);
+ return false;
+ }
+
+ mTutorialEditor->setClean();
+
+ return true;
+}
+
+bool FileActions::saveTutorialAs() {
+ const Tutorial* tutorial = mTutorialEditor->tutorial();
+
+ KUrl url = mTutorialUrl;
+ QPointer<KFileDialog> dialog = new KFileDialog(url, QString(),
+ mTutorialEditor);
+
+ dialog->setSelection(tutorial->id());
+ dialog->setCaption(i18nc("@title", "Save Tutorial"));
+ dialog->setOperationMode(KFileDialog::Saving);
+ dialog->setConfirmOverwrite(true);
+ dialog->setFilter(i18nc("@item:inlistbox A KFileDialog filter",
+ "*.xml|XML file"));
+ dialog->filterWidget()->setEditable(false);
+
+ if (dialog->exec() == QDialog::Rejected) {
+ return false;
+ }
+
+ try {
+ Serialization(mTutorialEditor).saveTutorial(tutorial,
+ dialog->selectedUrl());
+ } catch (IOException e) {
+ QString text = i18nc("@label", "There was a problem when trying to "
+"save the tutorial:<nl/>%1", e.message());
+ QString caption = i18nc("@title:window", "Tutorial could not be saved");
+ KMessageBox::error(mTutorialEditor, text, caption);
+ return false;
+ }
+
+ mRecentFilesAction->addUrl(dialog->selectedUrl());
+ mTutorialUrl = dialog->selectedUrl();
+ mTutorialEditor->setClean();
+
+ return true;
+}
+
+void FileActions::exportTutorial() {
+ const Tutorial* tutorial = mTutorialEditor->tutorial();
+
+ KUrl url;
+ QPointer<KFileDialog> dialog = new KFileDialog(url, QString(),
+ mTutorialEditor);
+
+ dialog->setSelection(tutorial->id());
+ dialog->setCaption(i18nc("@title", "Export Tutorial"));
+ dialog->setOperationMode(KFileDialog::Saving);
+ dialog->setConfirmOverwrite(true);
+ dialog->setFilter(Serialization(mTutorialEditor).availableExporterTypes());
+ dialog->filterWidget()->setEditable(false);
+
+ if (dialog->exec() == QDialog::Rejected) {
+ return;
+ }
+
+ try {
+ Serialization(mTutorialEditor).exportTutorial(tutorial,
+ dialog->currentFilter(),
+ dialog->selectedUrl());
+ } catch (IOException e) {
+ QString text = i18nc("@label", "There was a problem when trying to "
+"save the exported tutorial:<nl/>%1", e.message());
+ QString caption = i18nc("@title:window", "Exported tutorial could not "
+"be saved");
+ KMessageBox::error(mTutorialEditor, text, caption);
+ return;
+ }
+}
Property changes on: trunk/ktutorial/ktutorial-editor/src/FileActions.cpp
___________________________________________________________________
Added: svn:eol-style
+ native
Added: trunk/ktutorial/ktutorial-editor/src/FileActions.h
===================================================================
--- trunk/ktutorial/ktutorial-editor/src/FileActions.h (rev 0)
+++ trunk/ktutorial/ktutorial-editor/src/FileActions.h 2010-03-31 06:55:32 UTC (rev 223)
@@ -0,0 +1,182 @@
+/***************************************************************************
+ * Copyright (C) 2010 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 FILEACTIONS_H
+#define FILEACTIONS_H
+
+#include <QObject>
+
+#include <KUrl>
+
+class KActionCollection;
+class KRecentFilesAction;
+class KTutorialEditor;
+
+/**
+ * File related actions.
+ * FileActions provide the actions that create a new tutorial, open an existing
+ * tutorial, open one of the recently opened tutorials, save the tutorial or
+ * export the tutorial.
+ *
+ * Note, however, that it does not provide a quit action (as it has more to do
+ * with the application as a whole than with the files).
+ *
+ * FileActions provides also the URL of the current tutorial, and a way to check
+ * whether it can be closed or not, warning the user if needed.
+ *
+ * FileActions works closely with KTutorialEditor. KTutorialEditor is used to
+ * know the tutorial and whether it is clean or not. FileActions provides the
+ * tutorial URL and methods to create a new tutorial, load a tutorial from an
+ * URL and check whether the current tutorial was modified after the last time
+ * it was saved or not.
+ *
+ * The KTutorialEditor window is also used as the parent for every dialog shown
+ * by the actions.
+ */
+class FileActions: public QObject {
+Q_OBJECT
+public:
+
+ /**
+ * Creates a new FileActions for the given KTutorialEditor.
+ * All the actions are set up and added to the KTutorialEditor.
+ *
+ * @param tutorialEditor The KTutorialEditor to work with.
+ */
+ explicit FileActions(KTutorialEditor* tutorialEditor);
+
+ /**
+ * Destroys this FileActions.
+ * The entries in the recent files action are saved in the configuration.
+ */
+ ~FileActions();
+
+ /**
+ * Returns the tutorial URL.
+ *
+ * @return The tutorial URL.
+ */
+ const KUrl& tutorialUrl() const;
+
+ /**
+ * Checks whether the current tutorial can be closed or not.
+ * If the tutorial is not clean, the user is asked if it has to be saved or
+ * not, or if the close operation should be cancelled.
+ *
+ * @return True if the tutorial can be closed, false otherwise.
+ */
+ bool queryCloseTutorial();
+
+public Q_SLOTS:
+
+ /**
+ * Loads the tutorial to be edited from the given URL.
+ * The tutorial URL is updated and added to the recent files action, and a
+ * clean state is forced.
+ * An error message is shown if the tutorial could not be opened.
+ *
+ * Nothing is done if the current tutorial, if any, should not be closed.
+ *
+ * @param url The URL to load the tutorial from.
+ */
+ void loadTutorialFromUrl(KUrl url);
+
+ /**
+ * Creates a new empty tutorial replacing the current one, if any.
+ * The tutorial URL is cleared.
+ *
+ * Nothing is done if the current tutorial, if any, should not be closed.
+ */
+ void newTutorial();
+
+ /**
+ * Enables or disables Save action based on the clean state of the stack.
+ * When the stack is clean and the tutorial has an associated URL, the Save
+ * action is disabled. Otherwise, it is enabled, so if there is no
+ * associated URL the Save action is always kept enabled, even if the stack
+ * is clean.
+ *
+ * @param clean Whether the undo stack entered clean state or not.
+ */
+ void handleUndoStackCleanChanged(bool clean);
+
+private:
+
+ /**
+ * The KTutorialEditor to work with.
+ */
+ KTutorialEditor* mTutorialEditor;
+
+ /**
+ * The URL to save the tutorial to.
+ */
+ KUrl mTutorialUrl;
+
+ /**
+ * True if the tutorial was not modified since the last time it was saved,
+ * false otherwise.
+ */
+ bool mClean;
+
+ /**
+ * The "Open Recent" action.
+ */
+ KRecentFilesAction* mRecentFilesAction;
+
+ /**
+ * Sets up all the file related actions.
+ */
+ void setupActions();
+
+private Q_SLOTS:
+
+ /**
+ * Shows a KFileDialog to select the file to open the tutorial from.
+ * The tutorial is loaded from the URL selected by the user.
+ */
+ void openTutorial();
+
+ /**
+ * Saves the tutorial to the tutorial URL.
+ * A clean state is set. If there is no tutorial URL it behaves like
+ * saveTutorialAs().
+ * An error message is shown if the tutorial could not be saved.
+ *
+ * @return True if the tutorial was successfully saved, false otherwise.
+ */
+ bool saveTutorial();
+
+ /**
+ * Shows a KFileDialog to select the file to save the tutorial to.
+ * The tutorial URL is updated and added to the recent files action, and a
+ * clean state is forced.
+ * An error message is shown if the tutorial could not be saved.
+ *
+ * @return True if the tutorial was successfully saved, false otherwise.
+ */
+ bool saveTutorialAs();
+
+ /**
+ * Shows a KFileDialog to select the file to save the exported tutorial in.
+ * An error message is shown if the tutorial could not be saved.
+ */
+ void exportTutorial();
+
+};
+
+#endif
Property changes on: trunk/ktutorial/ktutorial-editor/src/FileActions.h
___________________________________________________________________
Added: svn:eol-style
+ native
Modified: trunk/ktutorial/ktutorial-editor/src/KTutorialEditor.cpp
===================================================================
--- trunk/ktutorial/ktutorial-editor/src/KTutorialEditor.cpp 2010-03-31 05:33:29 UTC (rev 222)
+++ trunk/ktutorial/ktutorial-editor/src/KTutorialEditor.cpp 2010-03-31 06:55:32 UTC (rev 223)
@@ -24,20 +24,15 @@
#include <KAction>
#include <KActionCollection>
#include <KApplication>
-#include <KFileDialog>
-#include <KFileFilterCombo>
#include <KLocalizedString>
-#include <KMessageBox>
-#include <KRecentFilesAction>
#include <KUndoStack>
-#include <KIO/NetAccess>
+#include "FileActions.h"
#include "commands/StepCommands.h"
#include "commands/TutorialCommands.h"
#include "data/Reaction.h"
#include "data/Step.h"
#include "data/Tutorial.h"
-#include "serialization/Serialization.h"
#include "view/ActionListWidget.h"
#include "view/AutoExpandableTreeView.h"
#include "view/EditionDialog.h"
@@ -64,100 +59,55 @@
mUndoStack = new KUndoStack(this);
connect(mUndoStack, SIGNAL(cleanChanged(bool)),
+ this, SIGNAL(cleanChanged(bool)));
+ connect(this, SIGNAL(cleanChanged(bool)),
this, SLOT(handleUndoStackCleanChanged(bool)));
setupDocks();
setupActions();
- newTutorial();
+ mFileActions->newTutorial();
setupGUI();
}
-KTutorialEditor::~KTutorialEditor() {
- mRecentFilesAction->saveEntries(KGlobal::config()->group("RecentFiles"));
+Tutorial* KTutorialEditor::tutorial() {
+ return mTutorial;
}
-//public slots:
-
-//Don't use a reference to the URL, as if it is the argument in
-//urlSelected(KUrl) signal emitted by mRecentFilesAction, it is removed in
-//mRecentFilesAction->addUrl(KUrl), which leads to a crash
-void KTutorialEditor::loadTutorialFromUrl(KUrl url) {
- if (!queryCloseTutorial()) {
- return;
- }
-
- Tutorial* tutorial;
- try {
- tutorial = Serialization(this).loadTutorial(url);
- } catch (IOException e) {
- QString text = i18nc("@label", "There was a problem when trying to "
-"open the file:<nl/>%1", e.message());
- QString caption = i18nc("@title:window", "File could not be read");
- KMessageBox::error(this, text, caption);
- return;
- } catch (DeserializationException e) {
- QString text = i18nc("@label", "There was a problem when trying to "
-"load the tutorial:<nl/>%1", e.message());
- QString caption = i18nc("@title:window", "Tutorial could not be "
-"loaded");
- KMessageBox::error(this, text, caption);
- return;
- }
-
- setTutorialToBeEdited(tutorial);
- mRecentFilesAction->addUrl(url);
- mTutorialUrl = url;
+void KTutorialEditor::setClean() {
mUndoStack->setClean();
+
//Force clean state, as setting an empty stack as clean would not emit
//cleanChanged()
handleUndoStackCleanChanged(true);
+ mFileActions->handleUndoStackCleanChanged(true);
}
+void KTutorialEditor::loadTutorialFromUrl(const KUrl& url) {
+ mFileActions->loadTutorialFromUrl(url);
+}
+
//protected:
bool KTutorialEditor::queryClose() {
- return queryCloseTutorial();
+ return mFileActions->queryCloseTutorial();
}
void KTutorialEditor::readProperties(const KConfigGroup& configGroup) {
KUrl url = configGroup.readEntry("TutorialUrl");
if (!url.isEmpty()) {
- loadTutorialFromUrl(url);
+ mFileActions->loadTutorialFromUrl(url);
}
}
void KTutorialEditor::saveProperties(KConfigGroup& configGroup) {
- configGroup.writeEntry("TutorialUrl", mTutorialUrl);
+ configGroup.writeEntry("TutorialUrl", mFileActions->tutorialUrl());
}
//private:
-bool KTutorialEditor::queryCloseTutorial() {
- if (mUndoStack->isClean()) {
- return true;
- }
-
- QString text = i18nc("@label", "The tutorial has been modified.<nl/>"
-"Do you want to save your changes or discard them?");
- QString caption = i18nc("@title:window", "Close tutorial");
- int button = KMessageBox::warningYesNoCancel(this, text, caption,
- KStandardGuiItem::save(),
- KStandardGuiItem::discard());
-
- if (button == KMessageBox::Cancel) {
- return false;
- }
-
- if (button == KMessageBox::Yes) {
- return saveTutorial();
- }
-
- return true;
-}
-
void KTutorialEditor::setTutorialToBeEdited(Tutorial* tutorial) {
if (!tutorial) {
tutorial = new Tutorial(this);
@@ -214,26 +164,8 @@
}
void KTutorialEditor::setupActions() {
- KStandardAction::openNew(this, SLOT(newTutorial()), actionCollection());
+ mFileActions = new FileActions(this);
- KStandardAction::open(this, SLOT(openTutorial()), actionCollection());
-
- mRecentFilesAction = KStandardAction::openRecent(
- this, SLOT(loadTutorialFromUrl(KUrl)), actionCollection());
- mRecentFilesAction->loadEntries(KGlobal::config()->group("RecentFiles"));
-
- KStandardAction::save(this, SLOT(saveTutorial()), actionCollection());
-
- KStandardAction::saveAs(this, SLOT(saveTutorialAs()), actionCollection());
-
- KAction* action = new KAction(this);
- action->setText(i18nc("@action", "Export..."));
- action->setStatusTip(i18nc("@info:status", "Exports the tutorial to a "
-"script."));
- action->setIcon(KIcon("document-export"));
- actionCollection()->addAction("exportTutorial", action);
- connect(action, SIGNAL(triggered(bool)), this, SLOT(exportTutorial()));
-
KStandardAction::quit(this, SLOT(close()), actionCollection());
mUndoStack->createUndoAction(actionCollection());
@@ -243,7 +175,7 @@
ActionListWidget* actionListWidget =
new ActionListWidget(mTutorialActionDock);
- action = new KAction(this);
+ KAction* action = new KAction(this);
action->setText(i18nc("@action", "Set information..."));
action->setStatusTip(i18nc("@info:status", "Set the name and description "
"of the tutorial."));
@@ -377,12 +309,12 @@
}
QString KTutorialEditor::captionFromTutorialUrl() {
- if (mTutorialUrl.isEmpty()) {
+ if (mFileActions->tutorialUrl().isEmpty()) {
return i18nc("@title:window Window title for KTutorial editor when the \
tutorial has no associated URL", "New file");
}
- QString caption = mTutorialUrl.prettyUrl();
+ QString caption = mFileActions->tutorialUrl().prettyUrl();
if (caption.length() > 64) {
caption = "..." + caption.right(64);
}
@@ -441,126 +373,8 @@
setCaption(i18nc("@title:window Wrapper for the window title when the \
tutorial was modified but not saved yet", "%1 [not saved]", caption));
}
-
- if (clean && !mTutorialUrl.isEmpty()) {
- actionCollection()->action("file_save")->setEnabled(false);
- } else {
- actionCollection()->action("file_save")->setEnabled(true);
- }
}
-void KTutorialEditor::newTutorial() {
- if (!queryCloseTutorial()) {
- return;
- }
-
- setTutorialToBeEdited();
- mTutorialUrl = KUrl();
- //Force clean state, as clearing an empty stack would not emit
- //cleanChanged()
- handleUndoStackCleanChanged(true);
-}
-
-void KTutorialEditor::openTutorial() {
- KUrl url = mTutorialUrl;
- QPointer<KFileDialog> dialog = new KFileDialog(url, QString(), this);
-
- dialog->setCaption(i18nc("@title", "Open Tutorial"));
- dialog->setOperationMode(KFileDialog::Opening);
- dialog->setFilter(i18nc("@item:inlistbox A KFileDialog filter",
- "*.xml|XML file"));
-
- if (dialog->exec() == QDialog::Rejected) {
- return;
- }
-
- loadTutorialFromUrl(dialog->selectedUrl());
-}
-
-bool KTutorialEditor::saveTutorial() {
- if (mTutorialUrl.isEmpty()) {
- return saveTutorialAs();
- }
-
- try {
- Serialization(this).saveTutorial(mTutorial, mTutorialUrl);
- } catch (IOException e) {
- QString text = i18nc("@label", "There was a problem when trying to "
-"save the tutorial:<nl/>%1", e.message());
- QString caption = i18nc("@title:window", "Tutorial could not be saved");
- KMessageBox::error(this, text, caption);
- return false;
- }
-
- mUndoStack->setClean();
-
- return true;
-}
-
-bool KTutorialEditor::saveTutorialAs() {
- KUrl url = mTutorialUrl;
- QPointer<KFileDialog> dialog = new KFileDialog(url, QString(), this);
-
- dialog->setSelection(mTutorial->id());
- dialog->setCaption(i18nc("@title", "Save Tutorial"));
- dialog->setOperationMode(KFileDialog::Saving);
- dialog->setConfirmOverwrite(true);
- dialog->setFilter(i18nc("@item:inlistbox A KFileDialog filter",
- "*.xml|XML file"));
- dialog->filterWidget()->setEditable(false);
-
- if (dialog->exec() == QDialog::Rejected) {
- return false;
- }
-
- try {
- Serialization(this).saveTutorial(mTutorial, dialog->selectedUrl());
- } catch (IOException e) {
- QString text = i18nc("@label", "There was a problem when trying to "
-"save the tutorial:<nl/>%1", e.message());
- QString caption = i18nc("@title:window", "Tutorial could not be saved");
- KMessageBox::error(this, text, caption);
- return false;
- }
-
- mRecentFilesAction->addUrl(dialog->selectedUrl());
- mTutorialUrl = dialog->selectedUrl();
- mUndoStack->setClean();
- //Force clean state, as setting an empty stack as clean would not emit
- //cleanChanged()
- handleUndoStackCleanChanged(true);
-
- return true;
-}
-
-void KTutorialEditor::exportTutorial() {
- KUrl url;
- QPointer<KFileDialog> dialog = new KFileDialog(url, QString(), this);
-
- dialog->setSelection(mTutorial->id());
- dialog->setCaption(i18nc("@title", "Export Tutorial"));
- dialog->setOperationMode(KFileDialog::Saving);
- dialog->setConfirmOverwrite(true);
- dialog->setFilter(Serialization(this).availableExporterTypes());
- dialog->filterWidget()->setEditable(false);
-
- if (dialog->exec() == QDialog::Rejected) {
- return;
- }
-
- try {
- Serialization(this).exportTutorial(mTutorial, dialog->currentFilter(),
- dialog->selectedUrl());
- } catch (IOException e) {
- QString text = i18nc("@label", "There was a problem when trying to "
-"save the exported tutorial:<nl/>%1", e.message());
- QString caption = i18nc("@title:window", "Exported tutorial could not "
-"be saved");
- KMessageBox::error(this, text, caption);
- return;
- }
-}
-
void KTutorialEditor::setTutorialInformation() {
showEditionDialog(new TutorialInformationWidget(mTutorial));
}
Modified: trunk/ktutorial/ktutorial-editor/src/KTutorialEditor.h
===================================================================
--- trunk/ktutorial/ktutorial-editor/src/KTutorialEditor.h 2010-03-31 05:33:29 UTC (rev 222)
+++ trunk/ktutorial/ktutorial-editor/src/KTutorialEditor.h 2010-03-31 06:55:32 UTC (rev 223)
@@ -24,7 +24,7 @@
class CommandWidget;
class EditionWidget;
-class KRecentFilesAction;
+class FileActions;
class KUndoStack;
class QTreeView;
class Reaction;
@@ -45,25 +45,46 @@
KTutorialEditor();
/**
- * Destroys this KTutorialEditor.
- * The entries in the recent files action are saved in the configuration.
+ * Returns the tutorial being edited.
+ *
+ * @return The tutorial being edited.
*/
- virtual ~KTutorialEditor();
+ Tutorial* tutorial();
-public Q_SLOTS:
+ /**
+ * Sets the tutorial as clean (not modified after the last time it was
+ * saved).
+ */
+ void setClean();
/**
* Loads the tutorial to be edited from the given URL.
- * The tutorial URL is updated and added to the recent files action, and a
- * clean state is forced.
- * An error message is shown if the tutorial couldn't be opened.
*
- * Nothing is done if the current tutorial, if any, should not be closed.
- *
* @param url The URL to load the tutorial from.
+ * @see FileActions::loadTutorialFromUrl(KUrl)
*/
- void loadTutorialFromUrl(KUrl url);
+ void loadTutorialFromUrl(const KUrl& url);
+ /**
+ * Sets the tutorial to be edited.
+ * It creates a new tutorial, prepares the tree view to represent it and
+ * handles the selection of items.
+ *
+ * If the tutorial is null, a new empty tutorial is set.
+ *
+ * @param tutorial The tutorial to set.
+ */
+ void setTutorialToBeEdited(Tutorial* tutorial = 0);
+
+Q_SIGNALS:
+
+ /**
+ * Emitted when the clean state changes.
+ *
+ * @param clean True if the tutorial is clean, false otherwise.
+ */
+ void cleanChanged(bool clean);
+
protected:
/**
@@ -120,9 +141,9 @@
QDockWidget* mReactionActionDock;
/**
- * The "Open Recent" action.
+ * The file related actions and data.
*/
- KRecentFilesAction* mRecentFilesAction;
+ FileActions* mFileActions;
/**
* The stack of undoable commands.
@@ -135,11 +156,6 @@
Tutorial* mTutorial;
/**
- * The URL to save the tutorial to.
- */
- KUrl mTutorialUrl;
-
- /**
* The currently selected step.
*/
Step* mCurrentStep;
@@ -150,26 +166,6 @@
Reaction* mCurrentReaction;
/**
- * Checks whether the tutorial can be closed or not.
- * If the tutorial is not clean, the user is asked if it has to be saved or
- * not, or if the close operation should be cancelled.
- *
- * @return True if the tutorial can be closed, false otherwise.
- */
- bool queryCloseTutorial();
-
- /**
- * Sets the tutorial to be edited.
- * It creates a new tutorial, prepares the tree view to represent it and
- * handles the selection of items.
- *
- * If the tutorial is null, a new empty tutorial is set.
- *
- * @param tutorial The tutorial to set.
- */
- void setTutorialToBeEdited(Tutorial* tutorial = 0);
-
- /**
* Sets up the dock widgets.
*/
void setupDocks();
@@ -218,62 +214,16 @@
void selectReaction(Reaction* reaction);
/**
- * Modifies the caption and enables or disables Save action based on the
- * clean state of the stack.
+ * Modifies the caption based on the clean state of the stack.
* When the stack is not clean, "[not saved]" is added after the caption
* (the URL or "New file", depending on the case). Otherwise, the caption is
* the URL associated to the tutorial.
*
- * When the stack is clean and the tutorial has an associated URL, the Save
- * action is disabled. Otherwise, it is enabled, so if there is no
- * associated URL the Save action is always kept enabled, even if the stack
- * is clean.
- *
* @param clean Whether the undo stack entered clean state or not.
*/
void handleUndoStackCleanChanged(bool clean);
/**
- * Creates a new empty tutorial replacing the current one, if any.
- * The tutorial URL is cleared.
- *
- * Nothing is done if the current tutorial, if any, should not be closed.
- */
- void newTutorial();
-
- /**
- * Shows a KFileDialog to select the file to open the tutorial from.
- * The tutorial is loaded from the URL selected by the user.
- */
- void openTutorial();
-
- /**
- * Saves the tutorial to the tutorial URL.
- * A clean state is set. If there is no tutorial URL it behaves like
- * saveTutorialAs().
- * An error message is shown if the tutorial couldn't be saved.
- *
- * @return True if the tutorial was successfully saved, false otherwise.
- */
- bool saveTutorial();
-
- /**
- * Shows a KFileDialog to select the file to save the tutorial to.
- * The tutorial URL is updated and added to the recent files action, and a
- * clean state is forced.
- * An error message is shown if the tutorial couldn't be saved.
- *
- * @return True if the tutorial was successfully saved, false otherwise.
- */
- bool saveTutorialAs();
-
- /**
- * Shows a KFileDialog to select the file to save the exported tutorial in.
- * An error message is shown if the tutorial couldn't be saved.
- */
- void exportTutorial();
-
- /**
* Shows a TutorialInformationWidget for the tutorial.
*/
void setTutorialInformation();
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|