ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
thread_control_widget.cc
Go to the documentation of this file.
1 // Copyright (c) 2018, ETH Zurich and UNC Chapel Hill.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 //
7 // * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 //
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 //
14 // * Neither the name of ETH Zurich and UNC Chapel Hill nor the names of
15 // its contributors may be used to endorse or promote products derived
16 // from this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 // POSSIBILITY OF SUCH DAMAGE.
29 //
30 // Author: Johannes L. Schoenberger (jsch-at-demuc-dot-de)
31 
33 
34 namespace colmap {
35 
37  : QWidget(parent),
38  progress_bar_(nullptr),
39  destructor_(new QAction(this)),
40  thread_(nullptr) {
41  connect(destructor_, &QAction::triggered, this, [this]() {
42  if (thread_) {
43  thread_->Stop();
44  thread_->Wait();
45  thread_.reset();
46  }
47  if (progress_bar_ != nullptr) {
48  progress_bar_->hide();
49  }
50  });
51 }
52 
53 void ThreadControlWidget::StartThread(const QString& progress_text,
54  const bool stoppable, Thread* thread) {
55  CHECK(!thread_);
56  CHECK_NOTNULL(thread);
57 
58  thread_.reset(thread);
59 
60  if (progress_bar_ == nullptr) {
61  progress_bar_ = new QProgressDialog(this);
62  progress_bar_->setWindowModality(Qt::ApplicationModal);
63  progress_bar_->setWindowFlags(Qt::Dialog | Qt::WindowTitleHint |
64  Qt::CustomizeWindowHint);
65  // Use a single space to clear the window title on Windows, otherwise it
66  // will contain the name of the executable.
67  progress_bar_->setWindowTitle(" ");
68  progress_bar_->setLabel(new QLabel(this));
69  progress_bar_->setMaximum(0);
70  progress_bar_->setMinimum(0);
71  progress_bar_->setValue(0);
72  connect(progress_bar_, &QProgressDialog::canceled,
73  [this]() { destructor_->trigger(); });
74  }
75 
76  // Enable the cancel button if the thread is stoppable.
77  QPushButton* cancel_button =
78  progress_bar_->findChildren<QPushButton*>().at(0);
79  cancel_button->setEnabled(stoppable);
80 
81  progress_bar_->setLabelText(progress_text);
82 
83  // Center the progress bar wrt. the parent widget.
84  const QPoint global =
85  parentWidget()->mapToGlobal(parentWidget()->rect().center());
86  progress_bar_->move(global.x() - progress_bar_->width() / 2,
87  global.y() - progress_bar_->height() / 2);
88 
89  progress_bar_->show();
90  progress_bar_->raise();
91 
92  thread_->AddCallback(Thread::FINISHED_CALLBACK,
93  [this]() { destructor_->trigger(); });
94  thread_->Start();
95 }
96 
97 void ThreadControlWidget::StartFunction(const QString& progress_text,
98  const std::function<void()>& func) {
99  class FunctionThread : public Thread {
100  public:
101  explicit FunctionThread(const std::function<void()>& f) : func_(f) {}
102 
103  private:
104  void Run() { func_(); }
105  const std::function<void()> func_;
106  };
107 
108  StartThread(progress_text, false, new FunctionThread(func));
109 }
110 
111 } // namespace colmap
int Run(int argc, const char *argv[])
utility::CountingProgressReporter * progress_bar_
Definition: FilePLY.cpp:42
void StartThread(const QString &progress_text, const bool stoppable, Thread *thread)
void StartFunction(const QString &progress_text, const std::function< void()> &func)
Rgb at(size_t color_id)