ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
ThreadControlWidget.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 "ThreadControlWidget.h"
9 
10 #include "BundleAdjustmentWidget.h"
11 #include "util/threading.h"
12 
13 namespace cloudViewer {
14 
15 using namespace colmap;
16 
18  : QWidget(parent),
19  progress_bar_(nullptr),
20  destructor_(new QAction(this)),
21  thread_(nullptr) {
22  connect(destructor_, &QAction::triggered, this, [this]() {
23  if (thread_) {
24  thread_->Stop();
25  thread_->Wait();
26  thread_.reset();
27  }
28  if (progress_bar_ != nullptr) {
29  progress_bar_->hide();
30  }
31  });
32 }
33 
34 void ThreadControlWidget::StartThread(const QString& progress_text,
35  const bool stoppable,
36  Thread* thread) {
37  CHECK(!thread_);
38  CHECK_NOTNULL(thread);
39 
40  thread_.reset(thread);
41 
42  if (progress_bar_ == nullptr) {
43  progress_bar_ = new QProgressDialog(this);
44  progress_bar_->setWindowModality(Qt::ApplicationModal);
45  progress_bar_->setWindowFlags(Qt::Dialog | Qt::WindowTitleHint |
46  Qt::CustomizeWindowHint);
47  // Use a single space to clear the window title on Windows, otherwise it
48  // will contain the name of the executable.
49  progress_bar_->setWindowTitle(" ");
50  progress_bar_->setLabel(new QLabel(this));
51  progress_bar_->setMaximum(0);
52  progress_bar_->setMinimum(0);
53  progress_bar_->setValue(0);
54  connect(progress_bar_, &QProgressDialog::canceled,
55  [this]() { destructor_->trigger(); });
56  }
57 
58  // Enable the cancel button if the thread is stoppable.
59  QPushButton* cancel_button =
60  progress_bar_->findChildren<QPushButton*>().at(0);
61  cancel_button->setEnabled(stoppable);
62 
63  progress_bar_->setLabelText(progress_text);
64 
65  // Center the progress bar wrt. the parent widget.
66  const QPoint global =
67  parentWidget()->mapToGlobal(parentWidget()->rect().center());
68  progress_bar_->move(global.x() - progress_bar_->width() / 2,
69  global.y() - progress_bar_->height() / 2);
70 
71  progress_bar_->show();
72  progress_bar_->raise();
73 
74  thread_->AddCallback(Thread::FINISHED_CALLBACK,
75  [this]() { destructor_->trigger(); });
76  thread_->Start();
77 }
78 
79 void ThreadControlWidget::StartFunction(const QString& progress_text,
80  const std::function<void()>& func) {
81  class FunctionThread : public Thread {
82  public:
83  explicit FunctionThread(const std::function<void()>& f) : func_(f) {}
84 
85  private:
86  void Run() { func_(); }
87  const std::function<void()> func_;
88  };
89 
90  StartThread(progress_text, false, new FunctionThread(func));
91 }
92 
93 } // namespace cloudViewer
int Run(int argc, const char *argv[])
utility::CountingProgressReporter * progress_bar_
Definition: FilePLY.cpp:42
void StartThread(const QString &progress_text, const bool stoppable, colmap::Thread *thread)
void StartFunction(const QString &progress_text, const std::function< void()> &func)
Generic file read and write utility for python interface.
Rgb at(size_t color_id)
Definition: lsd.c:1170