From f0c0e7c5d5ef8da312a7989205d1c176366e7b07 Mon Sep 17 00:00:00 2001 From: blacksun Date: Wed, 4 Apr 2018 15:07:52 +0000 Subject: [PATCH] improve waiting dialog when open a project git-svn-id: svn+ssh://svn.tuxfamily.org/svnroot/qet/qet/trunk@5295 bfdf4180-ca20-0410-9c96-a3a8aa849046 --- sources/projectview.cpp | 22 ++++++++++-- sources/qetdiagrameditor.cpp | 65 +++++++++++++++++++++--------------- sources/qetdiagrameditor.h | 2 +- sources/qetproject.cpp | 46 +++++++++++++++++++------ sources/ui/dialogwaiting.cpp | 14 ++++++-- sources/ui/dialogwaiting.h | 62 +++++++++++++++++++++++++++------- 6 files changed, 157 insertions(+), 54 deletions(-) diff --git a/sources/projectview.cpp b/sources/projectview.cpp index b7dc709c1..8fa8f8262 100644 --- a/sources/projectview.cpp +++ b/sources/projectview.cpp @@ -33,6 +33,7 @@ #include "projectpropertiesdialog.h" #include "xmlelementcollection.h" #include "autoNum/assignvariables.h" +#include "dialogwaiting.h" /** Constructeur @@ -844,12 +845,29 @@ void ProjectView::initLayout() { * We create a diagram view for each diagram, * and add it to the project view. */ -void ProjectView::loadDiagrams() { +void ProjectView::loadDiagrams() +{ if (!m_project) return; setDisplayFallbackWidget(m_project -> diagrams().isEmpty()); - foreach(Diagram *diagram, m_project -> diagrams()) { + DialogWaiting *dialog = nullptr; + if(DialogWaiting::hasInstance()) + { + dialog = DialogWaiting::instance(); + dialog->setTitle( tr("

" + "Ouverture du projet en cours...
" + "Création des onglets de folio :" + "

")); + } + for(Diagram *diagram : m_project->diagrams()) + { + if(dialog) + { + dialog->setDetail(diagram->title()); + dialog->setProgressBar(dialog->progressBarValue()+1); + } + DiagramView *sv = new DiagramView(diagram); addDiagram(sv); } diff --git a/sources/qetdiagrameditor.cpp b/sources/qetdiagrameditor.cpp index 2351f1663..87eae8c07 100644 --- a/sources/qetdiagrameditor.cpp +++ b/sources/qetdiagrameditor.cpp @@ -47,6 +47,7 @@ #include "undocommand/rotateselectioncommand.h" #include "rotatetextscommand.h" #include "diagramcommands.h" +#include "dialogwaiting.h" #include #include @@ -114,7 +115,7 @@ QETDiagramEditor::QETDiagramEditor(const QStringList &files, QWidget *parent) : { //So we open this files foreach(QString file, files) - if (openAndAddProject(file, false)) + if (openAndAddProject(file)) ++ opened_projects; } @@ -857,33 +858,39 @@ bool QETDiagramEditor::closeCurrentProject() { Ouvre un projet depuis un fichier et l'ajoute a cet editeur @param filepath Chemin du projet a ouvrir @param interactive true pour afficher des messages a l'utilisateur, false sinon - @param update_panel Whether the elements panel should be warned this - project has been added. Defaults to true. @return true si l'ouverture a reussi, false sinon */ -bool QETDiagramEditor::openAndAddProject(const QString &filepath, bool interactive, bool update_panel) { +bool QETDiagramEditor::openAndAddProject(const QString &filepath, bool interactive) +{ if (filepath.isEmpty()) return(false); QFileInfo filepath_info(filepath); - // verifie que le projet n'est pas deja ouvert dans un editeur - QString my_filepath = filepath_info.canonicalFilePath(); - if (QETDiagramEditor *diagram_editor = QETApp::diagramEditorForFile(filepath)) { - if (diagram_editor == this) { - if (ProjectView *project_view = viewForFile(filepath)) { + + //Check if project is not open in another editor + if (QETDiagramEditor *diagram_editor = QETApp::diagramEditorForFile(filepath)) + { + if (diagram_editor == this) + { + if (ProjectView *project_view = viewForFile(filepath)) + { activateWidget(project_view); show(); activateWindow(); } return(false); - } else { - // demande a l'autre editeur d'afficher le fichier + } + else + { + //Ask to the other editor to display the file return(diagram_editor -> openAndAddProject(filepath)); } } - // check the file exists - if (!filepath_info.exists()) { - if (interactive) { + // check the file exists + if (!filepath_info.exists()) + { + if (interactive) + { QET::QetMessageBox::critical( this, tr("Impossible d'ouvrir le fichier", "message box title"), @@ -896,8 +903,9 @@ bool QETDiagramEditor::openAndAddProject(const QString &filepath, bool interacti return(false); } - // verifie que le fichier est accessible en lecture - if (!filepath_info.isReadable()) { + //Check if file readable + if (!filepath_info.isReadable()) + { if (interactive) { QET::QetMessageBox::critical( this, @@ -910,8 +918,9 @@ bool QETDiagramEditor::openAndAddProject(const QString &filepath, bool interacti return(false); } - // gere le fait que le fichier puisse etre en lecture seule - if (!filepath_info.isWritable()) { + //Check if file is read only + if (!filepath_info.isWritable()) + { if (interactive) { QET::QetMessageBox::warning( this, @@ -923,10 +932,14 @@ bool QETDiagramEditor::openAndAddProject(const QString &filepath, bool interacti } } - // cree le projet a partir du fichier + //Create the project + DialogWaiting::instance(this); + QETProject *project = new QETProject(filepath); - if (project -> state() != QETProject::Ok) { - if (interactive && project -> state() != QETProject::FileOpenDiscard) { + if (project -> state() != QETProject::Ok) + { + if (interactive && project -> state() != QETProject::FileOpenDiscard) + { QET::QetMessageBox::warning( this, tr("Échec de l'ouverture du projet", "message box title"), @@ -940,16 +953,14 @@ bool QETDiagramEditor::openAndAddProject(const QString &filepath, bool interacti ); } delete project; + DialogWaiting::dropInstance(); return(false); } - // a ce stade, l'ouverture du fichier a reussi - // on l'ajoute a la liste des fichiers recents QETApp::projectsRecentFiles() -> fileWasOpened(filepath); - // ... et on l'ajoute dans l'application - // Note: we require the panel not to be updated when the project is added - // because it will update itself as soon as it becomes visible - return(addProject(project), update_panel); + addProject(project); + DialogWaiting::dropInstance(); + return true; } /** diff --git a/sources/qetdiagrameditor.h b/sources/qetdiagrameditor.h index 875676efc..ac8048d9f 100644 --- a/sources/qetdiagrameditor.h +++ b/sources/qetdiagrameditor.h @@ -64,7 +64,7 @@ class QETDiagramEditor : public QETMainWindow { void closeEvent (QCloseEvent *) override; QList openedProjects () const; void addProjectView (ProjectView *); - bool openAndAddProject (const QString &, bool = true, bool = true); + bool openAndAddProject (const QString &, bool = true); QList editedFiles () const; ProjectView *viewForFile (const QString &) const; ProjectView *acessCurrentProject (); diff --git a/sources/qetproject.cpp b/sources/qetproject.cpp index 9e84e6378..2ee5de6c9 100644 --- a/sources/qetproject.cpp +++ b/sources/qetproject.cpp @@ -1247,17 +1247,29 @@ void QETProject::readDiagramsXml(QDomDocument &xml_project) //@TODO try to solve a weird bug (dialog is black) since port to Qt5 with the DialogWaiting //show DialogWaiting - DialogWaiting* dlgWaiting = new DialogWaiting(); - dlgWaiting -> setModal(true); - dlgWaiting -> show(); - dlgWaiting -> setTitle( tr("Ouverture du projet en cours...") ); + DialogWaiting *dlgWaiting = nullptr; + if(DialogWaiting::hasInstance()) + { + dlgWaiting = DialogWaiting::instance(); + dlgWaiting -> setModal(true); + dlgWaiting -> show(); + dlgWaiting -> setTitle( tr("

" + "Ouverture du projet en cours...
" + "Création des folios" + "

")); + } //Search the diagrams in the project QDomNodeList diagram_nodes = xml_project.elementsByTagName("diagram"); - dlgWaiting->setProgressBarRange(0, diagram_nodes.length()); + + if(dlgWaiting) + dlgWaiting->setProgressBarRange(0, diagram_nodes.length()*3); + for (int i = 0 ; i < diagram_nodes.length() ; ++ i) { - dlgWaiting->setProgressBar(i+1); + if(dlgWaiting) + dlgWaiting->setProgressBar(i+1); + if (diagram_nodes.at(i).isElement()) { QDomElement diagram_xml_element = diagram_nodes.at(i).toElement(); @@ -1265,7 +1277,9 @@ void QETProject::readDiagramsXml(QDomDocument &xml_project) bool diagram_loading = diagram -> initFromXml(diagram_xml_element); if (diagram_loading) { - dlgWaiting->setDetail( diagram->title() ); + if(dlgWaiting) + dlgWaiting->setDetail( diagram->title() ); + //Get the attribute "order" of the diagram int diagram_order = -1; if (!QET::attributeIsAnInteger(diagram_xml_element, "order", &diagram_order)) diagram_order = 500000; @@ -1284,10 +1298,22 @@ void QETProject::readDiagramsXml(QDomDocument &xml_project) //Initialise links between elements in this project //and refresh the text of conductor - foreach (Diagram *d, diagrams()) + if(dlgWaiting) + { + dlgWaiting->setTitle( tr("

" + "Ouverture du projet en cours...
" + "Mise en place des références croisées" + "

")); + } + for(Diagram *d : diagrams()) + { + if(dlgWaiting) + { + dlgWaiting->setProgressBar(dlgWaiting->progressBarValue()+1); + dlgWaiting->setDetail(d->title()); + } d->refreshContents(); - - delete dlgWaiting; + } } /** diff --git a/sources/ui/dialogwaiting.cpp b/sources/ui/dialogwaiting.cpp index b20c612ed..e30dd71ea 100644 --- a/sources/ui/dialogwaiting.cpp +++ b/sources/ui/dialogwaiting.cpp @@ -20,6 +20,8 @@ #include "ui_dialogwaiting.h" #include + +DialogWaiting *DialogWaiting::m_static_dialog = nullptr; /** * @brief DialogWaiting::DialogWaiting * @param parent @@ -70,8 +72,7 @@ void DialogWaiting::setProgressBarRange(int min, int max){ * @param val is the string of action */ void DialogWaiting::setTitle(const QString& val){ - QString title=" "+val+" "; - ui->labelTitle->setText(title); + ui->labelTitle->setText(val); } /** @@ -82,3 +83,12 @@ void DialogWaiting::setDetail(const QString& val){ ui->label_detail->setText(val); } +/** + * @brief DialogWaiting::progressBarValue + * @return The current vcalue of the progress bar + */ +int DialogWaiting::progressBarValue() const +{ + ui->progressBar->value(); +} + diff --git a/sources/ui/dialogwaiting.h b/sources/ui/dialogwaiting.h index 88de170d1..7f61d31e5 100644 --- a/sources/ui/dialogwaiting.h +++ b/sources/ui/dialogwaiting.h @@ -20,6 +20,7 @@ #define DIALOGWAITING_H #include +#include namespace Ui { class DialogWaiting; @@ -28,19 +29,56 @@ namespace Ui { class DialogWaiting : public QDialog { Q_OBJECT - -public: - explicit DialogWaiting(QWidget *parent = nullptr); - ~DialogWaiting() override; - - void setProgressBar(int val); - void setProgressBarRange(int min, int max); - void setProgressReset(); - void setTitle(const QString& val); - void setDetail(const QString& val); - + public: + static DialogWaiting* instance(QWidget *parent = nullptr) + { + static QMutex mutex; + if(!m_static_dialog) + { + mutex.lock(); + if(!m_static_dialog) + m_static_dialog = new DialogWaiting(parent); + mutex.unlock(); + } + return m_static_dialog; + } + + static bool hasInstance() + { + if(m_static_dialog == nullptr) + return false; + else + return true; + } + + static void dropInstance() + { + static QMutex mutex; + if(m_static_dialog) + { + mutex.lock(); + m_static_dialog->deleteLater(); + m_static_dialog = nullptr; + mutex.unlock(); + } + } private: - Ui::DialogWaiting *ui; + static DialogWaiting *m_static_dialog; + + + public: + explicit DialogWaiting(QWidget *parent = nullptr); + ~DialogWaiting() override; + + void setProgressBar(int val); + void setProgressBarRange(int min, int max); + void setProgressReset(); + void setTitle(const QString& val); + void setDetail(const QString& val); + int progressBarValue() const; + + private: + Ui::DialogWaiting *ui; }; #endif // DIALOGWAITING_H