GCC Code Coverage Report


Directory: ./
File: libs/ui/qt/dialog/pulse_progress.cpp
Date: 2024-05-31 17:23:24
Exec Total Coverage
Lines: 0 25 0.0%
Branches: 0 42 0.0%

Line Branch Exec Source
1 /************************************************************************
2 *
3 * Copyright (C) 2009-2023 IRCAD France
4 * Copyright (C) 2012-2015 IHU Strasbourg
5 *
6 * This file is part of Sight.
7 *
8 * Sight is free software: you can redistribute it and/or modify it under
9 * the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * Sight is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with Sight. If not, see <https://www.gnu.org/licenses/>.
20 *
21 ***********************************************************************/
22
23 #include "ui/qt/dialog/pulse_progress.hpp"
24
25 #include <core/base.hpp>
26
27 #include <ui/__/macros.hpp>
28
29 #include <QApplication>
30 #include <QFutureWatcher>
31 #include <QPushButton>
32 #include <QString>
33 #include <QtConcurrent>
34 #include <QtCore>
35
36 namespace sight::ui::qt::dialog
37 {
38
39 //------------------------------------------------------------------------------
40
41 pulse_progress::pulse_progress() :
42 m_dialog(new QProgressDialog(qApp->activeWindow()))
43 {
44 }
45
46 //------------------------------------------------------------------------------
47
48 pulse_progress::~pulse_progress()
49 {
50 if(m_dialog != nullptr)
51 {
52 m_dialog->hide();
53 delete m_dialog;
54 }
55 }
56
57 //------------------------------------------------------------------------------
58
59 void pulse_progress::set_title(const std::string& _title)
60 {
61 m_dialog->setWindowTitle(QString::fromStdString(_title));
62 }
63
64 //------------------------------------------------------------------------------
65
66 void pulse_progress::set_message(const std::string& _msg)
67 {
68 m_dialog->setLabelText(QString::fromStdString(_msg));
69 }
70
71 //------------------------------------------------------------------------------
72
73 void pulse_progress::set_cancellable(bool _cancellable)
74 {
75 if(!_cancellable)
76 {
77 m_dialog->setCancelButton(nullptr);
78 }
79 else if(!m_cancellable)
80 {
81 m_dialog->setCancelButton(new QPushButton("Cancel", m_dialog));
82 }
83
84 m_cancellable = _cancellable;
85 }
86
87 //------------------------------------------------------------------------------
88
89 void pulse_progress::show()
90 {
91 // Create a QFutureWatcher and connect signals and slots.
92 QFutureWatcher<void> future_watcher;
93 QObject::connect(&future_watcher, SIGNAL(finished()), m_dialog, SLOT(reset()));
94 QObject::connect(&future_watcher, SIGNAL(progressRangeChanged(int,int)), m_dialog, SLOT(setRange(int,int)));
95 QObject::connect(&future_watcher, SIGNAL(progressValueChanged(int)), m_dialog, SLOT(setValue(int)));
96
97 if(m_cancellable)
98 {
99 QObject::connect(m_dialog, SIGNAL(canceled()), &future_watcher, SLOT(cancel()));
100 }
101
102 // Start the computation.
103 future_watcher.setFuture(QtConcurrent::run(m_stuff));
104
105 m_dialog->exec();
106 }
107
108 //------------------------------------------------------------------------------
109
110 } // namespace sight::ui::qt::dialog
111