From 5ddb92da9872d84970aebec706fffe5ca8d0b43f Mon Sep 17 00:00:00 2001 From: Simon Giraudot Date: Fri, 9 Mar 2018 13:13:44 +0100 Subject: [PATCH] Add a Qt progress bar (modal, no multithread mode) --- .../include/Qt_progress_bar_callback.h | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 Polyhedron/demo/Polyhedron/include/Qt_progress_bar_callback.h diff --git a/Polyhedron/demo/Polyhedron/include/Qt_progress_bar_callback.h b/Polyhedron/demo/Polyhedron/include/Qt_progress_bar_callback.h new file mode 100644 index 00000000000..18f5960d02b --- /dev/null +++ b/Polyhedron/demo/Polyhedron/include/Qt_progress_bar_callback.h @@ -0,0 +1,68 @@ +#ifndef PROGRESS_BAR_CALLBACK_H +#define PROGRESS_BAR_CALLBACK_H + +#include + +#include +#include + +class Qt_progress_bar_callback +{ + +private: + + CGAL::Real_timer timer; + double t_start; + mutable double t_latest; + + mutable std::size_t nb; + + QProgressDialog* dialog; + +public: + + Qt_progress_bar_callback() + { + } + + Qt_progress_bar_callback(const char* title, QWidget* parent) + : dialog (new QProgressDialog (QString(title), + QString("Cancel"), + 0, 100, + parent)) + { + dialog->setMinimumDuration(0); + dialog->setWindowModality(Qt::WindowModal); + timer.start(); + t_start = timer.time(); + t_latest = t_start; + } + ~Qt_progress_bar_callback() + { + } + + bool operator() (double advancement) const + { + // Avoid calling time() at every single iteration, which could + // impact performances very badly + ++ nb; + if (advancement != 1 && nb % 1000 != 0) + return true; + + // If the limit is reach, interrupt the algorithm + double t = timer.time(); + if (advancement == 1 || (t - t_latest) > 0.25) + { + dialog->setValue (int (100. * advancement)); + t_latest = t; + + if (dialog->wasCanceled()) + return false; + } + + return true; + } +}; + + +#endif // PROGRESS_BAR_CALLBACK_H