From fa11ec59ad68d20f5151d40a2d04692980fac498 Mon Sep 17 00:00:00 2001 From: xavier Date: Thu, 12 Jan 2012 07:04:09 +0000 Subject: [PATCH] Title block template editor: added "Open from file" and "Save to file" actions. git-svn-id: svn+ssh://svn.tuxfamily.org/svnroot/qet/qet/branches/0.3@1457 bfdf4180-ca20-0410-9c96-a3a8aa849046 --- sources/qetapp.cpp | 15 ++- sources/qetapp.h | 1 + sources/titleblock/qettemplateeditor.cpp | 128 +++++++++++++++++++++-- sources/titleblock/qettemplateeditor.h | 9 +- 4 files changed, 142 insertions(+), 11 deletions(-) diff --git a/sources/qetapp.cpp b/sources/qetapp.cpp index a4dddc913..f459b9157 100644 --- a/sources/qetapp.cpp +++ b/sources/qetapp.cpp @@ -994,10 +994,7 @@ void QETApp::openElementLocations(const QList &locations_list) /** 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(); @@ -1005,6 +1002,16 @@ void QETApp::openTitleBlockTemplate(const TitleBlockTemplateLocation &location) qet_template_editor -> showMaximized(); } +/** + 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 diff --git a/sources/qetapp.h b/sources/qetapp.h index ec8f5378e..7a6c73fdb 100644 --- a/sources/qetapp.h +++ b/sources/qetapp.h @@ -194,6 +194,7 @@ class QETApp : public QETSingleApplication { void openElementFiles(const QStringList &); void openElementLocations(const QList &); void openTitleBlockTemplate(const TitleBlockTemplateLocation &); + void openTitleBlockTemplate(const QString &); void configureQET(); void aboutQET(); diff --git a/sources/titleblock/qettemplateeditor.cpp b/sources/titleblock/qettemplateeditor.cpp index ce8257f00..c50406627 100644 --- a/sources/titleblock/qettemplateeditor.cpp +++ b/sources/titleblock/qettemplateeditor.cpp @@ -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 @@ QETTitleBlockTemplateEditor::QETTitleBlockTemplateEditor(QWidget *parent) : initWidgets(); initActions(); initMenus(); + } /** @@ -115,7 +117,9 @@ bool QETTitleBlockTemplateEditor::edit(const TitleBlockTemplateLocation &locatio return(false); } + opened_from_file_ = false; location_ = location; + updateEditorTitle(); editCopyOf(tb_template_orig); return(true); } @@ -145,8 +149,10 @@ bool QETTitleBlockTemplateEditor::edit(QETProject *project, const QString &templ return(false); } + opened_from_file_ = false; location_.setParentCollection(project -> embeddedTitleBlockTemplatesCollection()); location_.setName(template_name); + updateEditorTitle(); return(editCopyOf(tb_template_orig)); } @@ -162,7 +168,18 @@ bool QETTitleBlockTemplateEditor::edit(const QString &file_path) { /// 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 @@ void QETTitleBlockTemplateEditor::initActions() { 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 @@ void QETTitleBlockTemplateEditor::initActions() { 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 @@ void QETTitleBlockTemplateEditor::initMenus() { 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 @@ void QETTitleBlockTemplateEditor::initWidgets() { this, SLOT(pushGridUndoCommand(TitleBlockTemplateCommand *)) ); + connect(undo_stack_, SIGNAL(cleanChanged(bool)), this, SLOT(updateEditorTitle())); } /** @@ -417,8 +441,15 @@ void QETTitleBlockTemplateEditor::updateEditorTitle() { ) ); + 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 @@ void QETTitleBlockTemplateEditor::updateEditorTitle() { "%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 @@ bool QETTitleBlockTemplateEditor::saveAs(const TitleBlockTemplateLocation &locat 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( @@ -466,14 +516,49 @@ void QETTitleBlockTemplateEditor::open() { } } +/** + 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()); + } } } @@ -491,6 +576,37 @@ bool QETTitleBlockTemplateEditor::saveAs() { return(false); } +/** + 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 diff --git a/sources/titleblock/qettemplateeditor.h b/sources/titleblock/qettemplateeditor.h index 9ca749a10..d8ea08c86 100644 --- a/sources/titleblock/qettemplateeditor.h +++ b/sources/titleblock/qettemplateeditor.h @@ -49,10 +49,14 @@ class QETTitleBlockTemplateEditor : public QMainWindow { /// 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 @@ class QETTitleBlockTemplateEditor : public QMainWindow { void editLogos(); void newTemplate(); void open(); + void openFromFile(); bool save(); bool saveAs(); + bool saveAsFile(); void quit(); private slots: @@ -103,6 +109,7 @@ class QETTitleBlockTemplateEditor : public QMainWindow { void pushUndoCommand(QUndoCommand *); void updateEditorTitle(); bool saveAs(const TitleBlockTemplateLocation &); + bool saveAs(const QString &); }; #endif