From 2fc183d595a59ff9d84b67dc32448234ebe4d6ff Mon Sep 17 00:00:00 2001 From: Marc Zinnschlag Date: Fri, 23 Nov 2012 12:20:35 +0100 Subject: [PATCH] added operations progress bar --- apps/opencs/CMakeLists.txt | 4 +-- apps/opencs/model/doc/document.cpp | 13 +++++--- apps/opencs/model/doc/document.hpp | 4 +-- apps/opencs/view/doc/operation.cpp | 49 ++++++++++++++++++++++++++++ apps/opencs/view/doc/operation.hpp | 31 ++++++++++++++++++ apps/opencs/view/doc/operations.cpp | 47 ++++++++++++++++++++++++++ apps/opencs/view/doc/operations.hpp | 37 +++++++++++++++++++++ apps/opencs/view/doc/view.cpp | 20 ++++++++++-- apps/opencs/view/doc/view.hpp | 4 ++- apps/opencs/view/doc/viewmanager.cpp | 8 ++--- apps/opencs/view/doc/viewmanager.hpp | 2 +- 11 files changed, 202 insertions(+), 17 deletions(-) create mode 100644 apps/opencs/view/doc/operation.cpp create mode 100644 apps/opencs/view/doc/operation.hpp create mode 100644 apps/opencs/view/doc/operations.cpp create mode 100644 apps/opencs/view/doc/operations.hpp diff --git a/apps/opencs/CMakeLists.txt b/apps/opencs/CMakeLists.txt index d7f113447a..66b9f4942a 100644 --- a/apps/opencs/CMakeLists.txt +++ b/apps/opencs/CMakeLists.txt @@ -4,7 +4,7 @@ set (OPENCS_SRC model/doc/documentmanager.cpp model/doc/document.cpp - view/doc/viewmanager.cpp view/doc/view.cpp + view/doc/viewmanager.cpp view/doc/view.cpp view/doc/operations.cpp view/doc/operation.cpp ) set (OPENCS_HDR @@ -12,7 +12,7 @@ set (OPENCS_HDR model/doc/documentmanager.hpp model/doc/document.hpp - view/doc/viewmanager.hpp view/doc/view.hpp + view/doc/viewmanager.hpp view/doc/view.hpp view/doc/operations.hpp view/doc/operation.hpp ) set (OPENCS_US diff --git a/apps/opencs/model/doc/document.cpp b/apps/opencs/model/doc/document.cpp index c4f830531a..473d723754 100644 --- a/apps/opencs/model/doc/document.cpp +++ b/apps/opencs/model/doc/document.cpp @@ -33,13 +33,16 @@ void CSMDoc::Document::save() mSaveCount = 1; mSaveTimer.start (500); emit stateChanged (getState(), this); - emit progress (1, 16, State_Saving, this); + emit progress (1, 16, State_Saving, 1, this); } -void CSMDoc::Document::abortSave() +void CSMDoc::Document::abortOperation (int type) { - mSaveTimer.stop(); - emit stateChanged (getState(), this); + if (type==State_Saving) + { + mSaveTimer.stop(); + emit stateChanged (getState(), this); + } } void CSMDoc::Document::modificationStateChanged (bool clean) @@ -51,7 +54,7 @@ void CSMDoc::Document::saving() { ++mSaveCount; - emit progress (mSaveCount, 16, State_Saving, this); + emit progress (mSaveCount, 16, State_Saving, 1, this); if (mSaveCount>15) { diff --git a/apps/opencs/model/doc/document.hpp b/apps/opencs/model/doc/document.hpp index 8553427a1c..3bed0151de 100644 --- a/apps/opencs/model/doc/document.hpp +++ b/apps/opencs/model/doc/document.hpp @@ -39,13 +39,13 @@ namespace CSMDoc void save(); - void abortSave(); + void abortOperation (int type); signals: void stateChanged (int state, CSMDoc::Document *document); - void progress (int current, int max, int type, CSMDoc::Document *document); + void progress (int current, int max, int type, int threads, CSMDoc::Document *document); private slots: diff --git a/apps/opencs/view/doc/operation.cpp b/apps/opencs/view/doc/operation.cpp new file mode 100644 index 0000000000..cd9477557a --- /dev/null +++ b/apps/opencs/view/doc/operation.cpp @@ -0,0 +1,49 @@ + +#include "operation.hpp" + +#include + +#include "../../model/doc/document.hpp" + +void CSVDoc::Operation::updateLabel (int threads) +{ + if (threads==-1 || ((threads==0)!=mStalling)) + { + std::string name ("unknown operation"); + + switch (mType) + { + case CSMDoc::Document::State_Saving: name = "saving"; break; + } + + std::ostringstream stream; + + if ((mStalling = (threads<=0))) + { + stream << name << " (waiting for a free worker thread)"; + } + else + { + stream << name << " (%p%)"; + } + + setFormat (stream.str().c_str()); + } +} + +CSVDoc::Operation::Operation (int type) : mType (type), mStalling (false) +{ + updateLabel(); +} + +void CSVDoc::Operation::setProgress (int current, int max, int threads) +{ + updateLabel (threads); + setRange (0, max); + setValue (current); +} + +int CSVDoc::Operation::getType() const +{ + return mType; +} \ No newline at end of file diff --git a/apps/opencs/view/doc/operation.hpp b/apps/opencs/view/doc/operation.hpp new file mode 100644 index 0000000000..362725b6f4 --- /dev/null +++ b/apps/opencs/view/doc/operation.hpp @@ -0,0 +1,31 @@ +#ifndef CSV_DOC_OPERATION_H +#define CSV_DOC_OPERATION_H + +#include + +namespace CSVDoc +{ + class Operation : public QProgressBar + { + Q_OBJECT + + int mType; + bool mStalling; + + // not implemented + Operation (const Operation&); + Operation& operator= (const Operation&); + + void updateLabel (int threads = -1); + + public: + + Operation (int type); + + void setProgress (int current, int max, int threads); + + int getType() const; + }; +} + +#endif \ No newline at end of file diff --git a/apps/opencs/view/doc/operations.cpp b/apps/opencs/view/doc/operations.cpp new file mode 100644 index 0000000000..ba444a119a --- /dev/null +++ b/apps/opencs/view/doc/operations.cpp @@ -0,0 +1,47 @@ + +#include "operations.hpp" + +#include + +#include "operation.hpp" + +CSVDoc::Operations::Operations() +{ + /// \todo make widget height fixed (exactly the height required to display all operations) + + setFeatures (QDockWidget::NoDockWidgetFeatures); + + QWidget *widget = new QWidget; + setWidget (widget); + + mLayout = new QVBoxLayout; + + widget->setLayout (mLayout); +} + +void CSVDoc::Operations::setProgress (int current, int max, int type, int threads) +{ + for (std::vector::iterator iter (mOperations.begin()); iter!=mOperations.end(); ++iter) + if ((*iter)->getType()==type) + { + (*iter)->setProgress (current, max, threads); + return; + } + + Operation *operation = new Operation (type); + + mLayout->addWidget (operation); + mOperations.push_back (operation); + operation->setProgress (current, max, threads); +} + +void CSVDoc::Operations::quitOperation (int type) +{ + for (std::vector::iterator iter (mOperations.begin()); iter!=mOperations.end(); ++iter) + if ((*iter)->getType()==type) + { + delete *iter; + mOperations.erase (iter); + break; + } +} \ No newline at end of file diff --git a/apps/opencs/view/doc/operations.hpp b/apps/opencs/view/doc/operations.hpp new file mode 100644 index 0000000000..b966774502 --- /dev/null +++ b/apps/opencs/view/doc/operations.hpp @@ -0,0 +1,37 @@ +#ifndef CSV_DOC_OPERATIONS_H +#define CSV_DOC_OPERATIONS_H + +#include + +#include + +class QVBoxLayout; + +namespace CSVDoc +{ + class Operation; + + class Operations : public QDockWidget + { + Q_OBJECT + + QVBoxLayout *mLayout; + std::vector mOperations; + + // not implemented + Operations (const Operations&); + Operations& operator= (const Operations&); + + public: + + Operations(); + + void setProgress (int current, int max, int type, int threads); + ///< Implicitly starts the operation, if it is not running already. + + void quitOperation (int type); + ///< Calling this function for an operation that is not running is a no-op. + }; +} + +#endif \ No newline at end of file diff --git a/apps/opencs/view/doc/view.cpp b/apps/opencs/view/doc/view.cpp index 3e9701669c..fe018ab3bb 100644 --- a/apps/opencs/view/doc/view.cpp +++ b/apps/opencs/view/doc/view.cpp @@ -9,6 +9,7 @@ #include "../../model/doc/document.hpp" #include "viewmanager.hpp" +#include "operations.hpp" void CSVDoc::View::closeEvent (QCloseEvent *event) { @@ -94,6 +95,9 @@ CSVDoc::View::View (ViewManager& viewManager, CSMDoc::Document *document, int to setCentralWidget (new QWidget); resize (200, 200); + mOperations = new Operations; + addDockWidget (Qt::BottomDockWidgetArea, mOperations); + updateTitle(); setupUi(); @@ -120,11 +124,23 @@ void CSVDoc::View::updateDocumentState() { updateTitle(); updateActions(); + + static const int operations[] = + { + CSMDoc::Document::State_Saving, + -1 // end marker + }; + + int state = mDocument->getState() ; + + for (int i=0; operations[i]!=-1; ++i) + if (!(state & operations[i])) + mOperations->quitOperation (operations[i]); } -void CSVDoc::View::updateProgress (int current, int max, int type) +void CSVDoc::View::updateProgress (int current, int max, int type, int threads) { - + mOperations->setProgress (current, max, type, threads); } void CSVDoc::View::newView() diff --git a/apps/opencs/view/doc/view.hpp b/apps/opencs/view/doc/view.hpp index 1836ced8b6..191af36b51 100644 --- a/apps/opencs/view/doc/view.hpp +++ b/apps/opencs/view/doc/view.hpp @@ -15,6 +15,7 @@ namespace CSMDoc namespace CSVDoc { class ViewManager; + class Operations; class View : public QMainWindow { @@ -28,6 +29,7 @@ namespace CSVDoc QAction *mRedo; QAction *mSave; std::vector mEditingActions; + Operations *mOperations; // not implemented View (const View&); @@ -62,7 +64,7 @@ namespace CSVDoc void updateDocumentState(); - void updateProgress (int current, int max, int type); + void updateProgress (int current, int max, int type, int threads); private slots: diff --git a/apps/opencs/view/doc/viewmanager.cpp b/apps/opencs/view/doc/viewmanager.cpp index ed31fdb754..673afbad82 100644 --- a/apps/opencs/view/doc/viewmanager.cpp +++ b/apps/opencs/view/doc/viewmanager.cpp @@ -46,8 +46,8 @@ CSVDoc::View *CSVDoc::ViewManager::addView (CSMDoc::Document *document) connect (document, SIGNAL (stateChanged (int, CSMDoc::Document *)), this, SLOT (documentStateChanged (int, CSMDoc::Document *))); - connect (document, SIGNAL (progress (int, int, int, CSMDoc::Document *)), - this, SLOT (progress (int, int, int, CSMDoc::Document *))); + connect (document, SIGNAL (progress (int, int, int, int, CSMDoc::Document *)), + this, SLOT (progress (int, int, int, int, CSMDoc::Document *))); } View *view = new View (*this, document, countViews (document)+1); @@ -102,9 +102,9 @@ void CSVDoc::ViewManager::documentStateChanged (int state, CSMDoc::Document *doc (*iter)->updateDocumentState(); } -void CSVDoc::ViewManager::progress (int current, int max, int type, CSMDoc::Document *document) +void CSVDoc::ViewManager::progress (int current, int max, int type, int threads, CSMDoc::Document *document) { for (std::vector::const_iterator iter (mViews.begin()); iter!=mViews.end(); ++iter) if ((*iter)->getDocument()==document) - (*iter)->updateProgress (current, max, type); + (*iter)->updateProgress (current, max, type, threads); } \ No newline at end of file diff --git a/apps/opencs/view/doc/viewmanager.hpp b/apps/opencs/view/doc/viewmanager.hpp index eac4902502..caf75a0cdb 100644 --- a/apps/opencs/view/doc/viewmanager.hpp +++ b/apps/opencs/view/doc/viewmanager.hpp @@ -46,7 +46,7 @@ namespace CSVDoc void documentStateChanged (int state, CSMDoc::Document *document); - void progress (int current, int max, int type, CSMDoc::Document *document); + void progress (int current, int max, int type, int threads, CSMDoc::Document *document); }; }