[qet] [1457] Title block template editor: added "Open from file" and " Save to file" actions.

[ Thread Index | Date Index | More lists.tuxfamily.org/qet Archives ]


Revision: 1457
Author:   xavier
Date:     2012-01-12 08:04:09 +0100 (Thu, 12 Jan 2012)
Log Message:
-----------
Title block template editor: added "Open from file" and "Save to file" actions.

Modified Paths:
--------------
    branches/0.3/sources/qetapp.cpp
    branches/0.3/sources/qetapp.h
    branches/0.3/sources/titleblock/qettemplateeditor.cpp
    branches/0.3/sources/titleblock/qettemplateeditor.h

Modified: branches/0.3/sources/qetapp.cpp
===================================================================
--- branches/0.3/sources/qetapp.cpp	2012-01-12 06:47:02 UTC (rev 1456)
+++ branches/0.3/sources/qetapp.cpp	2012-01-12 07:04:09 UTC (rev 1457)
@@ -994,10 +994,7 @@
 
 /**
 	Launch a new title block template editor to edit the given template
-	@param project Parent project of the template to edit
-	@param template_name Name of the template to edit within its parent project
-	If no template name is supplied, the method assumes the editor has to be
-	launched for a template creation.
+	@param location location of the title block template to be edited
 */
 void QETApp::openTitleBlockTemplate(const TitleBlockTemplateLocation &location) {
 	QETTitleBlockTemplateEditor *qet_template_editor = new QETTitleBlockTemplateEditor();
@@ -1006,6 +1003,16 @@
 }
 
 /**
+	Launch a new title block template editor to edit the given template
+	@param filepath Path of the .titleblock file to be opened
+*/
+void QETApp::openTitleBlockTemplate(const QString &filepath) {
+	QETTitleBlockTemplateEditor *qet_template_editor = new QETTitleBlockTemplateEditor();
+	qet_template_editor -> edit(filepath);
+	qet_template_editor -> showMaximized();
+}
+
+/**
 	Permet a l'utilisateur de configurer QET en lancant un dialogue approprie.
 	@see ConfigDialog
 */

Modified: branches/0.3/sources/qetapp.h
===================================================================
--- branches/0.3/sources/qetapp.h	2012-01-12 06:47:02 UTC (rev 1456)
+++ branches/0.3/sources/qetapp.h	2012-01-12 07:04:09 UTC (rev 1457)
@@ -194,6 +194,7 @@
 	void openElementFiles(const QStringList &);
 	void openElementLocations(const QList<ElementsLocation> &);
 	void openTitleBlockTemplate(const TitleBlockTemplateLocation &);
+	void openTitleBlockTemplate(const QString &);
 	void configureQET();
 	void aboutQET();
 	

Modified: branches/0.3/sources/titleblock/qettemplateeditor.cpp
===================================================================
--- branches/0.3/sources/titleblock/qettemplateeditor.cpp	2012-01-12 06:47:02 UTC (rev 1456)
+++ branches/0.3/sources/titleblock/qettemplateeditor.cpp	2012-01-12 07:04:09 UTC (rev 1457)
@@ -33,6 +33,7 @@
 QETTitleBlockTemplateEditor::QETTitleBlockTemplateEditor(QWidget *parent) :
 	QMainWindow(parent),
 	read_only(false),
+	opened_from_file_(false),
 	tb_template_(0),
 	logo_manager_(0)
 {
@@ -42,6 +43,7 @@
 	initWidgets();
 	initActions();
 	initMenus();
+	
 }
 
 /**
@@ -115,7 +117,9 @@
 		return(false);
 	}
 	
+	opened_from_file_ = false;
 	location_ = location;
+	updateEditorTitle();
 	editCopyOf(tb_template_orig);
 	return(true);
 }
@@ -145,8 +149,10 @@
 		return(false);
 	}
 	
+	opened_from_file_ = false;
 	location_.setParentCollection(project -> embeddedTitleBlockTemplatesCollection());
 	location_.setName(template_name);
+	updateEditorTitle();
 	return(editCopyOf(tb_template_orig));
 }
 
@@ -162,7 +168,18 @@
 		/// TODO the file opening failed, warn the user?
 		return(false);
 	}
-	return(edit(tbt));
+	
+	bool editing = edit(tbt);
+	if (!editing) {
+		/// TODO the file editing failed, warn the user?
+		return(false);
+	}
+	
+	filepath_ = file_path;
+	opened_from_file_ = true;
+	updateEditorTitle();
+	
+	return(true);
 }
 
 /**
@@ -217,8 +234,10 @@
 	
 	new_            = new QAction(QET::Icons::DocumentNew,          tr("&Nouveau",                     "menu entry"), this);
 	open_           = new QAction(QET::Icons::DocumentOpen,         tr("&Ouvrir",                      "menu entry"), this);
+	open_from_file_ = new QAction(QET::Icons::DocumentOpen,         tr("Ouvrir depuis un fichier",     "menu entry"), this);
 	save_           = new QAction(QET::Icons::DocumentSave,         tr("&Enregistrer",                 "menu entry"), this);
 	save_as_        = new QAction(QET::Icons::DocumentSave,         tr("Enregistrer sous",             "menu entry"), this);
+	save_as_file_   = new QAction(QET::Icons::DocumentSave,         tr("Enregistrer vers un fichier",  "menu entry"), this);
 	quit_           = new QAction(QET::Icons::ApplicationExit,      tr("&Quitter",                     "menu entry"), this);
 	zoom_in_        = new QAction(QET::Icons::ZoomIn,               tr("Zoom avant",                   "menu entry"), this);
 	zoom_out_       = new QAction(QET::Icons::ZoomOut,              tr("Zoom arri\350re",              "menu entry"), this);
@@ -247,8 +266,10 @@
 	
 	connect(new_,             SIGNAL(triggered()), this,     SLOT(newTemplate()));
 	connect(open_,            SIGNAL(triggered()), this,     SLOT(open()));
+	connect(open_from_file_,  SIGNAL(triggered()), this,     SLOT(openFromFile()));
 	connect(save_,            SIGNAL(triggered()), this,     SLOT(save()));
 	connect(save_as_,         SIGNAL(triggered()), this,     SLOT(saveAs()));
+	connect(save_as_file_,    SIGNAL(triggered()), this,     SLOT(saveAsFile()));
 	connect(quit_,            SIGNAL(triggered()), this,     SLOT(quit()));
 	connect(zoom_in_,         SIGNAL(triggered()), template_edition_area_view_, SLOT(zoomIn()));
 	connect(zoom_out_,        SIGNAL(triggered()), template_edition_area_view_, SLOT(zoomOut()));
@@ -279,8 +300,10 @@
 	
 	file_menu_    -> addAction(new_);
 	file_menu_    -> addAction(open_);
+	file_menu_    -> addAction(open_from_file_);
 	file_menu_    -> addAction(save_);
 	file_menu_    -> addAction(save_as_);
+	file_menu_    -> addAction(save_as_file_);
 	file_menu_    -> addSeparator();
 	file_menu_    -> addAction(quit_);
 	
@@ -353,6 +376,7 @@
 		this,
 		SLOT(pushGridUndoCommand(TitleBlockTemplateCommand *))
 	);
+	connect(undo_stack_, SIGNAL(cleanChanged(bool)), this, SLOT(updateEditorTitle()));
 }
 
 /**
@@ -417,8 +441,15 @@
 		)
 	);
 	
+	QString titleblock_title;
+	if (opened_from_file_) {
+		titleblock_title = filepath_;
+	} else {
+		titleblock_title = location_.name();
+	}
+	
 	QString title;
-	if (location_.name().isEmpty()) {
+	if (titleblock_title.isEmpty()) {
 		title = min_title;
 	} else {
 		title = QString(
@@ -426,7 +457,7 @@
 				"%1 - %2",
 				"window title: %1 is the base window title, %2 is a template name"
 			)
-		).arg(min_title).arg(location_.name());
+		).arg(min_title).arg(titleblock_title);
 	}
 	setWindowTitle(title);
 }
@@ -448,13 +479,32 @@
 	
 	collection -> setTemplateXmlDescription(location.name(), elmt);
 	
+	opened_from_file_ = false;
 	location_ = location;
 	undo_stack_ -> setClean();
+	updateEditorTitle();
 	return(true);
 }
 
 /**
+	Save the template in the provided filepath.
+	@see TitleBlockTemplate::saveToXmlFile()
+	@param filepath location Location where the title block template should be saved.
+*/
+bool QETTitleBlockTemplateEditor::saveAs(const QString &filepath) {
+	bool saving = tb_template_ -> saveToXmlFile(filepath);
+	if (!saving) return(false);
 	
+	opened_from_file_ = true;
+	filepath_ = filepath;
+	undo_stack_ -> setClean();
+	updateEditorTitle();
+	return(true);
+}
+
+/**
+	Ask the user to choose a title block template from the known collections
+	then open it for edition.
 */
 void QETTitleBlockTemplateEditor::open() {
 	TitleBlockTemplateLocation location = getTitleBlockTemplateLocationFromUser(
@@ -467,13 +517,48 @@
 }
 
 /**
+	Ask the user to choose a file supposed to contain a title block template,
+	then open it for edition.
+*/
+void QETTitleBlockTemplateEditor::openFromFile() {
+	// directory to show
+	QString initial_dir = filepath_.isEmpty() ? QETApp::customTitleBlockTemplatesDir() : QDir(filepath_).absolutePath();
+	
+	// ask the user to choose a filepath
+	QString user_filepath = QFileDialog::getOpenFileName(
+		this,
+		tr("Ouvrir un fichier", "dialog title"),
+		initial_dir,
+		tr(
+			"Mod\350les de cartouches QElectroTech (*%1);;"
+			"Fichiers XML (*.xml);;"
+			"Tous les fichiers (*)",
+			"filetypes allowed when opening a title block template file - %1 is the .titleblock extension"
+		).arg(QString(TITLEBLOCKS_FILE_EXTENSION))
+	);
+	
+	
+	if (!user_filepath.isEmpty()) QETApp::instance() -> openTitleBlockTemplate(user_filepath);
+}
+
+/**
 	Save the currently edited title block template back to its parent project.
 */
 bool QETTitleBlockTemplateEditor::save() {
-	if (location_.isValid()) {
-		return(saveAs(location_));
+	if (opened_from_file_) {
+		if (!filepath_.isEmpty()) {
+			return(saveAs(filepath_));
+		} else {
+			// Actually, this should never happen since opened_from_file_ is always set
+			// along with a valid path. There is a dedicated menu item to call this.s
+			return(saveAsFile());
+		}
 	} else {
-		return(saveAs());
+		if (location_.isValid()) {
+			return(saveAs(location_));
+		} else {
+			return(saveAs());
+		}
 	}
 }
 
@@ -492,6 +577,37 @@
 }
 
 /**
+	Ask the user where on the filesystem he wishes to save the currently edited template.
+*/
+bool QETTitleBlockTemplateEditor::saveAsFile() {
+	// directory to show
+	QString initial_dir = filepath_.isEmpty() ? QETApp::customTitleBlockTemplatesDir() : QDir(filepath_).absolutePath();
+	
+	// ask the user to choose a target file
+	QString filepath = QFileDialog::getSaveFileName(
+		this,
+		tr("Enregistrer sous", "dialog title"),
+		initial_dir,
+		tr(
+			"Mod\350les de cartouches QElectroTech (*%1)",
+			"filetypes allowed when saving a title block template file - %1 is the .titleblock extension"
+		).arg(QString(TITLEBLOCKS_FILE_EXTENSION))
+	);
+	
+	// if no name was entered, return false
+	if (filepath.isEmpty()) return(false);
+	
+	// if the name does not end with ".titleblock", add it
+	if (!filepath.endsWith(".titleblock", Qt::CaseInsensitive)) filepath += ".titleblock";
+	
+	// attempts to save the file
+	bool saving = saveAs(filepath);
+	
+	// retourne un booleen representatif de la reussite de l'enregistrement
+	return(saving);
+}
+
+/**
 	Ask the user for a title block template location
 	@param title Title displayed by the dialog window
 	@param existing_only True for the user to be forced to choose an existing

Modified: branches/0.3/sources/titleblock/qettemplateeditor.h
===================================================================
--- branches/0.3/sources/titleblock/qettemplateeditor.h	2012-01-12 06:47:02 UTC (rev 1456)
+++ branches/0.3/sources/titleblock/qettemplateeditor.h	2012-01-12 07:04:09 UTC (rev 1457)
@@ -49,10 +49,14 @@
 	/// menus TODO
 	QMenu *file_menu_, *edit_menu_,/* *paste_from_menu_, */*display_menu_,/* *tools_menu_,*/ *config_menu_, *help_menu_;
 	/// actions
-	QAction *new_, *open_, *save_, *save_as_, *quit_, *configure_, *about_qt_, *about_qet_, *merge_cells_, *split_cell_;
+	QAction *new_, *open_, *open_from_file_, *save_, *save_as_, *save_as_file_, *quit_, *configure_, *about_qt_, *about_qet_, *merge_cells_, *split_cell_;
 	QAction *zoom_in_, *zoom_out_, *zoom_fit_, *zoom_reset_;
 	/// Location of the currently edited template
 	TitleBlockTemplateLocation location_;
+	/// Filepath of the currently edited template, if opened from a file
+	QString filepath_;
+	/// Whether to consider the location or the filepath
+	bool opened_from_file_;
 	/// Template Object edited
 	TitleBlockTemplate *tb_template_;
 	/// Template preview
@@ -92,8 +96,10 @@
 	void editLogos();
 	void newTemplate();
 	void open();
+	void openFromFile();
 	bool save();
 	bool saveAs();
+	bool saveAsFile();
 	void quit();
 	
 	private slots:
@@ -103,6 +109,7 @@
 	void pushUndoCommand(QUndoCommand *);
 	void updateEditorTitle();
 	bool saveAs(const TitleBlockTemplateLocation &);
+	bool saveAs(const QString &);
 };
 
 #endif


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/