[Ktutorial-commits] SF.net SVN: ktutorial:[200] trunk/ktutorial/ktutorial-editor/src
Status: Alpha
Brought to you by:
danxuliu
|
From: <dan...@us...> - 2010-03-27 07:05:54
|
Revision: 200
http://ktutorial.svn.sourceforge.net/ktutorial/?rev=200&view=rev
Author: danxuliu
Date: 2010-03-27 07:05:48 +0000 (Sat, 27 Mar 2010)
Log Message:
-----------
Add actions to save and load a tutorial from XML
Modified Paths:
--------------
trunk/ktutorial/ktutorial-editor/src/KTutorialEditor.cpp
trunk/ktutorial/ktutorial-editor/src/KTutorialEditor.h
Modified: trunk/ktutorial/ktutorial-editor/src/KTutorialEditor.cpp
===================================================================
--- trunk/ktutorial/ktutorial-editor/src/KTutorialEditor.cpp 2010-03-27 04:25:13 UTC (rev 199)
+++ trunk/ktutorial/ktutorial-editor/src/KTutorialEditor.cpp 2010-03-27 07:05:48 UTC (rev 200)
@@ -63,6 +63,10 @@
setupActions();
+ //Setting the url enables/disables File->Save action, so it has to be
+ //set after it is created
+ setTutorialUrl(KUrl());
+
setupGUI();
}
@@ -105,6 +109,24 @@
mTutorial = tutorial;
}
+void KTutorialEditor::setTutorialUrl(const KUrl& url) {
+ mTutorialUrl = url;
+
+ if (url.isEmpty()) {
+ actionCollection()->action("file_save")->setEnabled(false);
+
+ setCaption("");
+ } else {
+ actionCollection()->action("file_save")->setEnabled(true);
+
+ QString caption = url.prettyUrl();
+ if (caption.length() > 64) {
+ caption = "..." + caption.right(64);
+ }
+ setCaption(caption);
+ }
+}
+
void KTutorialEditor::setupDocks() {
mTutorialActionDock = new QDockWidget(i18nc("@title", "Edit tutorial"),
this);
@@ -124,6 +146,12 @@
void KTutorialEditor::setupActions() {
KStandardAction::openNew(this, SLOT(newTutorial()), actionCollection());
+ KStandardAction::open(this, SLOT(openTutorial()), actionCollection());
+
+ 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 "
@@ -313,8 +341,84 @@
void KTutorialEditor::newTutorial() {
setTutorialToBeEdited();
+ setTutorialUrl(KUrl());
}
+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;
+ }
+
+ Tutorial* tutorial;
+ try {
+ tutorial = Serialization::loadTutorial(dialog->selectedUrl());
+ } 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);
+ setTutorialUrl(dialog->selectedUrl());
+}
+
+void KTutorialEditor::saveTutorial() {
+ try {
+ Serialization::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);
+ }
+}
+
+void 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;
+ }
+
+ try {
+ Serialization::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;
+ }
+
+ setTutorialUrl(dialog->selectedUrl());
+}
+
void KTutorialEditor::exportTutorial() {
KUrl url;
QPointer<KFileDialog> dialog = new KFileDialog(url, QString(), this);
Modified: trunk/ktutorial/ktutorial-editor/src/KTutorialEditor.h
===================================================================
--- trunk/ktutorial/ktutorial-editor/src/KTutorialEditor.h 2010-03-27 04:25:13 UTC (rev 199)
+++ trunk/ktutorial/ktutorial-editor/src/KTutorialEditor.h 2010-03-27 07:05:48 UTC (rev 200)
@@ -20,6 +20,7 @@
#define KTUTORIALEDITOR_H
#include <KXmlGuiWindow>
+#include <KUrl>
class EditionWidget;
class QTreeView;
@@ -68,6 +69,11 @@
Tutorial* mTutorial;
/**
+ * The URL to save the tutorial to.
+ */
+ KUrl mTutorialUrl;
+
+ /**
* The currently selected step.
*/
Step* mCurrentStep;
@@ -89,6 +95,16 @@
void setTutorialToBeEdited(Tutorial* tutorial = 0);
/**
+ * Sets the URL to save the tutorial to.
+ * An empty URL disables Save action, otherwise it is enabled.
+ * The caption (window title) is updated with the URL. The URL in the
+ * caption is shown truncated if it is too lengthy.
+ *
+ * @param url The URL to save the tutorial to.
+ */
+ void setTutorialUrl(const KUrl& url);
+
+ /**
* Sets up the dock widgets.
*/
void setupDocks();
@@ -128,10 +144,31 @@
/**
* Creates a new empty tutorial replacing the current one, if any.
+ * The tutorial URL is cleared.
*/
void newTutorial();
/**
+ * Shows a KFileDialog to select the file to open the tutorial from.
+ * The tutorial URL is updated.
+ * An error message is shown if the tutorial couldn't be opened.
+ */
+ void openTutorial();
+
+ /**
+ * Saves the tutorial to the tutorial URL.
+ * An error message is shown if the tutorial couldn't be saved.
+ */
+ void saveTutorial();
+
+ /**
+ * Shows a KFileDialog to select the file to save the tutorial to.
+ * The tutorial URL is updated.
+ * An error message is shown if the tutorial couldn't be saved.
+ */
+ void 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.
*/
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|