ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
ecvProgressDialog.cpp
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #include "ecvProgressDialog.h"
9 
10 // Qt
11 #include <QCoreApplication>
12 #include <QProgressBar>
13 #include <QPushButton>
14 
16  QWidget* parent /*=0*/)
17  : QProgressDialog(parent), m_currentValue(0), m_lastRefreshValue(-1) {
18  setAutoClose(true);
19 
20  resize(400, 200);
21  setRange(0, 100);
22  setMinimumWidth(400);
23 
24  QPushButton* cancelButton = 0;
25  if (showCancelButton) {
26  cancelButton = new QPushButton("Cancel");
27  cancelButton->setDefault(false);
28  cancelButton->setFocusPolicy(Qt::NoFocus);
29  }
30  setCancelButton(cancelButton);
31 
32  connect(this, &ecvProgressDialog::scheduleRefresh, this,
34  Qt::QueuedConnection); // can't use DirectConnection here!
35 }
36 
38  int value = m_currentValue;
39  if (m_lastRefreshValue != value) {
40  m_lastRefreshValue = value;
41  setValue(value); // See Qt doc: if the progress dialog is modal,
42  // setValue() calls QApplication::processEvents()
43  }
44 }
45 
46 void ecvProgressDialog::update(float percent) {
47  // thread-safe
48  int value = static_cast<int>(percent);
49  if (value != m_currentValue) {
50  m_currentValue = value;
51  emit scheduleRefresh();
52  QCoreApplication::processEvents(); // we let the main thread breath (so
53  // that the call to 'refresh' can be
54  // performed)
55  }
56 }
57 
58 void ecvProgressDialog::setMethodTitle(QString methodTitle) {
59  setWindowTitle(methodTitle);
60 }
61 
62 void ecvProgressDialog::setInfo(QString infoStr) {
63  setLabelText(infoStr);
64  if (isVisible()) {
65  QProgressDialog::update();
66  QCoreApplication::processEvents();
67  }
68 }
69 
71  m_lastRefreshValue = -1;
72  show();
73  QCoreApplication::processEvents();
74 }
75 
77  hide();
78  QCoreApplication::processEvents();
79 }
virtual void stop() override
Notifies the fact that the process has ended.
virtual void update(float percent) override
Notifies the algorithm progress.
ecvProgressDialog(bool cancelButton=false, QWidget *parent=0)
Default constructor.
virtual void setInfo(const char *infoStr) override
Notifies some information about the ongoing process.
void refresh()
Refreshes the progress.
QAtomicInt m_currentValue
Current progress value (percent)
void scheduleRefresh()
Schedules a call to refresh.
QAtomicInt m_lastRefreshValue
Last displayed progress value (percent)
virtual void start() override
virtual void setMethodTitle(const char *methodTitle) override
Notifies the algorithm title.