From e8e881ff51d23145b59f4973073b608db6961686 Mon Sep 17 00:00:00 2001 From: xavierqet Date: Wed, 30 Jul 2008 11:51:27 +0000 Subject: [PATCH] Ajout des "fichiers recents" git-svn-id: svn+ssh://svn.tuxfamily.org/svnroot/qet/qet/trunk@359 bfdf4180-ca20-0410-9c96-a3a8aa849046 --- editor/qetelementeditor.cpp | 31 ++++++++- editor/qetelementeditor.h | 2 + qelectrotech.pro | 2 + qetapp.cpp | 25 +++++++ qetapp.h | 7 +- qetdiagrameditor.cpp | 17 ++++- qetdiagrameditor.h | 2 + recentfiles.cpp | 132 ++++++++++++++++++++++++++++++++++++ recentfiles.h | 65 ++++++++++++++++++ 9 files changed, 278 insertions(+), 5 deletions(-) create mode 100644 recentfiles.cpp create mode 100644 recentfiles.h diff --git a/editor/qetelementeditor.cpp b/editor/qetelementeditor.cpp index e00e2e353..5759adf63 100644 --- a/editor/qetelementeditor.cpp +++ b/editor/qetelementeditor.cpp @@ -22,6 +22,7 @@ #include "customelementpart.h" #include "newelementwizard.h" #include "elementitemeditor.h" +#include "recentfiles.h" /** Constructeur @@ -260,6 +261,8 @@ void QETElementEditor::setupMenus() { file_menu -> addAction(new_element); file_menu -> addAction(open); + file_menu -> addMenu(QETApp::elementsRecentFiles() -> menu()); + connect(QETApp::elementsRecentFiles(), SIGNAL(fileOpeningRequested(const QString &)), this, SLOT(openRecentFile(const QString &))); file_menu -> addAction(save); file_menu -> addAction(save_as); file_menu -> addSeparator(); @@ -520,6 +523,7 @@ void QETElementEditor::fromFile(const QString &filepath) { // memorise le fichier setFileName(filepath); + QETApp::elementsRecentFiles() -> fileWasOpened(filepath); slot_updateMenus(); } @@ -583,7 +587,7 @@ void QETElementEditor::slot_new() { } /** - Ouvre un fichier + Demande un fichier a l'utilisateur et ouvre ce fichier */ void QETElementEditor::slot_open() { // demande un nom de fichier a ouvrir a l'utilisateur @@ -593,9 +597,30 @@ void QETElementEditor::slot_open() { _filename.isEmpty() ? QETApp::customElementsDir() : QDir(_filename).absolutePath(), tr("\311l\351ments QElectroTech (*.elmt);;Fichiers XML (*.xml);;Tous les fichiers (*)") ); - if (user_filename.isEmpty()) return; + openElement(user_filename); +} + +/** + Slot utilise pour ouvrir un fichier recent. + Transfere filepath au slot openElement seulement si cet editeur est actif + @param filepath Fichier a ouvrir + @see openElement +*/ +void QETElementEditor::openRecentFile(const QString &filepath) { + if (qApp -> activeWindow() != this) return; + openElement(filepath); +} + +/** + Ouvre un fichier element dans un nouvel editeur + Cette methode ne controle pas si le fichier est deja ouvert + @param filepath Fichier a ouvrir + @see fromFile +*/ +void QETElementEditor::openElement(const QString &filepath) { + if (filepath.isEmpty()) return; QETElementEditor *cee = new QETElementEditor(); - cee -> fromFile(user_filename); + cee -> fromFile(filepath); cee -> show(); } diff --git a/editor/qetelementeditor.h b/editor/qetelementeditor.h index a41a139dc..e54b5045c 100644 --- a/editor/qetelementeditor.h +++ b/editor/qetelementeditor.h @@ -106,6 +106,8 @@ class QETElementEditor : public QMainWindow { public slots: void slot_new(); void slot_open(); + void openRecentFile(const QString &); + void openElement(const QString &); void slot_reload(); bool slot_save(); bool slot_saveAs(); diff --git a/qelectrotech.pro b/qelectrotech.pro index 6a703aba0..e43dd0de3 100644 --- a/qelectrotech.pro +++ b/qelectrotech.pro @@ -81,6 +81,7 @@ HEADERS += aboutqet.h \ qetdiagrameditor.h \ qetsingleapplication.h \ qgimanager.h \ + recentfiles.h \ terminal.h \ editor/arceditor.h \ editor/circleeditor.h \ @@ -149,6 +150,7 @@ SOURCES += aboutqet.cpp \ qetdiagrameditor.cpp \ qetsingleapplication.cpp \ qgimanager.cpp \ + recentfiles.cpp \ terminal.cpp \ editor/arceditor.cpp \ editor/circleeditor.cpp \ diff --git a/qetapp.cpp b/qetapp.cpp index 17bd553d9..68c407972 100644 --- a/qetapp.cpp +++ b/qetapp.cpp @@ -18,6 +18,7 @@ #include "qetapp.h" #include "qetdiagrameditor.h" #include "qetelementeditor.h" +#include "recentfiles.h" #include #include #define QUOTE(x) STRINGIFY(x) @@ -26,6 +27,8 @@ QString QETApp::common_elements_dir = QString(); QString QETApp::config_dir = QString(); QString QETApp::diagram_texts_font = QString(); +RecentFiles *QETApp::projects_recent_files_ = 0; +RecentFiles *QETApp::elements_recent_files_ = 0; /** Constructeur @@ -78,6 +81,10 @@ QETApp::QETApp(int &argc, char **argv) : /// Destructeur QETApp::~QETApp() { + elements_recent_files_ -> save(); + projects_recent_files_ -> save(); + delete elements_recent_files_; + delete projects_recent_files_; delete qsti; } @@ -388,6 +395,20 @@ QList QETApp::elementEditors() const { return(element_editors); } +/** + @return La liste des fichiers recents pour les projets +*/ +RecentFiles *QETApp::projectsRecentFiles() { + return(projects_recent_files_); +} + +/** + @return La liste des fichiers recents pour les elements +*/ +RecentFiles *QETApp::elementsRecentFiles() { + return(elements_recent_files_); +} + /** Affiche ou cache une fenetre (editeurs de schemas / editeurs d'elements) @param window fenetre a afficher / cacher @@ -629,6 +650,10 @@ void QETApp::initConfiguration() { // police a utiliser pour le rendu de texte diagram_texts_font = qet_settings -> value("diagramfont", "Sans Serif").toString(); + + // fichiers recents + projects_recent_files_ = new RecentFiles("projects"); + elements_recent_files_ = new RecentFiles("elements"); } /** diff --git a/qetapp.h b/qetapp.h index 1a2529022..f745987aa 100644 --- a/qetapp.h +++ b/qetapp.h @@ -23,6 +23,7 @@ #include "qetarguments.h" class QETDiagramEditor; class QETElementEditor; +class RecentFiles; /** Cette classe represente l'application QElectroTech. @@ -56,6 +57,8 @@ class QETApp : public QETSingleApplication { static QETDiagramEditor *diagramEditorForFile(const QString &); QList diagramEditors() const; QList elementEditors() const; + static RecentFiles *projectsRecentFiles(); + static RecentFiles *elementsRecentFiles(); #ifdef QET_ALLOW_OVERRIDE_CED_OPTION public: static void overrideCommonElementsDir(const QString &); @@ -103,7 +106,9 @@ class QETApp : public QETSingleApplication { QETArguments qet_arguments_; ///< Analyseur d'arguments bool non_interactive_execution_; ///< booleen indiquant si l'application va se terminer immediatement apres un court traitement static QString diagram_texts_font; - + static RecentFiles *projects_recent_files_; + static RecentFiles *elements_recent_files_; + public slots: void systray(QSystemTrayIcon::ActivationReason); void reduceEveryEditor(); diff --git a/qetdiagrameditor.cpp b/qetdiagrameditor.cpp index e8d0096ef..e143cdda9 100644 --- a/qetdiagrameditor.cpp +++ b/qetdiagrameditor.cpp @@ -23,7 +23,7 @@ #include "aboutqet.h" #include "conductorpropertieswidget.h" #include "configdialog.h" - +#include "recentfiles.h" /** constructeur @param files Liste de fichiers a ouvrir @@ -384,6 +384,8 @@ void QETDiagramEditor::menus() { // menu Fichier menu_fichier -> addAction(new_file); menu_fichier -> addAction(open_file); + menu_fichier -> addMenu(QETApp::projectsRecentFiles() -> menu()); + connect(QETApp::projectsRecentFiles(), SIGNAL(fileOpeningRequested(const QString &)), this, SLOT(openRecentFile(const QString &))); menu_fichier -> addAction(save_file); menu_fichier -> addAction(save_file_sous); menu_fichier -> addAction(close_file); @@ -549,6 +551,18 @@ bool QETDiagramEditor::newDiagram() { return(true); } +/** + Slot utilise pour ouvrir un fichier recent. + Transfere filepath au slot openAndAddDiagram seulement si cet editeur est + actif + @param filepath Fichier a ouvrir + @see openAndAddDiagram +*/ +bool QETDiagramEditor::openRecentFile(const QString &filepath) { + if (qApp -> activeWindow() != this) return(false); + return(openAndAddDiagram(filepath)); +} + /** Cette fonction demande un nom de fichier a ouvrir a l'utilisateur @return true si l'ouverture a reussi, false sinon @@ -595,6 +609,7 @@ bool QETDiagramEditor::openAndAddDiagram(const QString &nom_fichier) { if (sv -> open(nom_fichier, &code_erreur)) { addDiagramView(sv); activateWindow(); + QETApp::projectsRecentFiles() -> fileWasOpened(nom_fichier); return(true); } else { QString message_erreur; diff --git a/qetdiagrameditor.h b/qetdiagrameditor.h index ff27e318e..19a28b41c 100644 --- a/qetdiagrameditor.h +++ b/qetdiagrameditor.h @@ -21,6 +21,7 @@ #include "insetproperties.h" class DiagramView; class ElementsPanelWidget; +class RecentFiles; /** Cette classe represente la fenetre principale de QElectroTech et, ipso facto, la plus grande partie de l'interface graphique de QElectroTech. @@ -66,6 +67,7 @@ class QETDiagramEditor : public QMainWindow { bool save(); bool newDiagram(); bool openDiagram(); + bool openRecentFile(const QString &); bool openAndAddDiagram(const QString &); bool closeDiagram(); void slot_editInfos(); diff --git a/recentfiles.cpp b/recentfiles.cpp new file mode 100644 index 000000000..702de7842 --- /dev/null +++ b/recentfiles.cpp @@ -0,0 +1,132 @@ +#include "recentfiles.h" +#include "qetapp.h" + +RecentFiles::RecentFiles(const QString &identifier, int size, QObject *parent) : + QObject(parent), + identifier_(identifier.isEmpty() ? "unnamed" : identifier), + size_(size > 0 ? size : 10), + menu_(0) +{ + mapper_ = new QSignalMapper(this); + connect(mapper_, SIGNAL(mapped(const QString &)), this, SLOT(handleMenuRequest(const QString &))); + + extractFilesFromSettings(); + buildMenu(); +} + +/** + Destructeur + @todo determiner s'il faut detruire ou non le menu +*/ +RecentFiles::~RecentFiles() { + delete menu_; +} + +/** + @return le nombre de fichiers a retenir +*/ +int RecentFiles::size() const { + return(size_); +} + +/** + @return un menu listant les derniers fichiers ouverts +*/ +QMenu *RecentFiles::menu() const { + return(menu_); +} + +/** + Oublie les fichiers recents +*/ +void RecentFiles::clear() { + list_.clear(); + buildMenu(); +} + +/** + Sauvegarde les fichiers récents dans la configuration +*/ +void RecentFiles::save() { + saveFilesToSettings(); +} + +/** + Gere les actions sur le menu +*/ +void RecentFiles::handleMenuRequest(const QString &filepath) { + emit(fileOpeningRequested(filepath)); +} + +/** + Gere le fait qu'un fichier ait ete ouvert + @param filepath Chemin du fichier ouvert +*/ +void RecentFiles::fileWasOpened(const QString &filepath) { + insertFile(filepath); + buildMenu(); +} + +/** + lit la liste des fichiers recents dans la configuration +*/ +void RecentFiles::extractFilesFromSettings() { + // oublie la liste des fichiers recents + list_.clear(); + + // recupere les derniers fichiers ouverts dans la configuration + for (int i = size_ ; i >= 1 ; -- i) { + QString key(identifier_ + "-recentfiles/file" + QString::number(i)); + QString value(QETApp::settings().value(key, QString()).toString()); + insertFile(value); + } +} + +/** + Insere un fichier dans la liste des fichiers recents +*/ +void RecentFiles::insertFile(const QString &filepath) { + // evite d'inserer un chemin de fichier vide ou en double + if (filepath.isEmpty()) return; + list_.removeAll(filepath); + + // insere le chemin de fichier + list_.push_front(filepath); + + // s'assure que l'on ne retient pas plus de fichiers que necessaire + while (list_.count() > size_) list_.removeLast(); +} + +/** + ecrit la liste des fichiers recents dans la configuration +*/ +void RecentFiles::saveFilesToSettings() { + for (int i = 0 ; i < size_ && i < list_.count() ; ++ i) { + QString key(identifier_ + "-recentfiles/file" + QString::number(i + 1)); + QETApp::settings().setValue(key, list_[i]); + } +} + +/** + Construit le menu +*/ +void RecentFiles::buildMenu() { + // reinitialise le menu + if (!menu_) { + menu_ = new QMenu(tr("&R\351cemment ouvert(s)")); + menu_ -> setIcon(QIcon(":/ico/open.png")); + } else { + menu_ -> clear(); + } + + // remplit le menu + foreach (QString filepath, list_) { + // creee une nouvelle action pour le fichier + QAction *action = new QAction(filepath, 0); + menu_ -> addAction(action); + + // lie l'action et le mapper + mapper_ -> setMapping(action, filepath); + connect(action, SIGNAL(triggered()), mapper_, SLOT(map())); + } +} diff --git a/recentfiles.h b/recentfiles.h new file mode 100644 index 000000000..cadd9e412 --- /dev/null +++ b/recentfiles.h @@ -0,0 +1,65 @@ +/* + Copyright 2006-2008 Xavier Guerrin + This file is part of QElectroTech. + + QElectroTech 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 2 of the License, or + (at your option) any later version. + + QElectroTech 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 QElectroTech. If not, see . +*/ +#ifndef RECENT_FILES_H +#define RECENT_FILES_H +#include +class QMenu; +/** + Cette classe permet de gerer des fichiers recents. +*/ +class RecentFiles : public QObject { + Q_OBJECT + + // constructeurs, destructeur + public: + RecentFiles(const QString &, int = 10, QObject * = 0); + virtual ~RecentFiles(); + private: + RecentFiles(const RecentFiles &); + + // methodes + public: + int size() const; + QMenu *menu() const; + + public slots: + void clear(); + void save(); + void fileWasOpened(const QString &); + + signals: + void fileOpeningRequested(const QString &); + + private: + void extractFilesFromSettings(); + void insertFile(const QString &); + void saveFilesToSettings(); + void buildMenu(); + + private slots: + void handleMenuRequest(const QString &); + + // attributs + private: + QString identifier_; + int size_; + QList list_; + QMenu *menu_; + QSignalMapper *mapper_; +}; +#endif