File: progress_bar.cpp

package info (click to toggle)
sight 25.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 42,184 kB
  • sloc: cpp: 289,476; xml: 17,257; ansic: 9,878; python: 1,379; sh: 144; makefile: 33
file content (130 lines) | stat: -rw-r--r-- 4,250 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/************************************************************************
 *
 * Copyright (C) 2024-2025 IRCAD France
 *
 * This file is part of Sight.
 *
 * Sight is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Sight is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with Sight. If not, see <https://www.gnu.org/licenses/>.
 *
 ***********************************************************************/

#include "progress_bar.hpp"

#include <core/com/signal.hxx>
#include <core/com/slots.hxx>

#include <ui/qt/container/widget.hpp>

namespace sight::module::ui::qt
{

//-----------------------------------------------------------------------------

progress_bar::progress_bar() noexcept
{
    new_slot(slots::ADD_MONITOR, &progress_bar::add_monitor, this);
    new_signal<signals::finished_t>(signals::FINISHED);
}

//-----------------------------------------------------------------------------

void progress_bar::starting()
{
    this->create();

    const auto widget_container = std::dynamic_pointer_cast<sight::ui::qt::container::widget>(this->get_container());
    auto* const qt_container    = widget_container->get_qt_container();

    const auto service_id = QString::fromStdString(base_id());
    qt_container->setObjectName(service_id);
    qt_container->setContentsMargins(0, 0, 0, 0);

    // Get the progress bar configuration.
    const auto& config = this->get_config();

    const auto show_title  = config.get<bool>("config.<xmlattr>.show_title", true);
    const auto show_cancel = config.get<bool>("config.<xmlattr>.show_cancel", true);
    const auto svg_path    = config.get_optional<std::filesystem::path>("config.<xmlattr>.svg");
    const auto pulse       = config.get<bool>("config.<xmlattr>.pulse", svg_path && !svg_path->empty());

    const auto svg_size = config.get_optional<int>("config.<xmlattr>.svg_size");

    if(svg_size)
    {
        SIGHT_ASSERT("The svg size attribute is only valid if an svg file is used.", svg_path && !svg_path->empty());
    }

    const auto weak_this = this->weak_from_this();

    // Create the progress bar widget.
    m_progress_bar_widget = std::make_shared<sight::ui::qt::widget::progress_bar>(
        qt_container,
        show_title,
        show_cancel,
        pulse,
        svg_path ? std::make_optional(*svg_path) : std::nullopt,
        svg_size ? std::make_optional(*svg_size) : std::nullopt,
        [weak_this]
        {
            if(auto shared_this = dynamic_pointer_cast<progress_bar>(weak_this.lock()); shared_this)
            {
                shared_this->async_emit(signals::FINISHED);
            }
        });

    // Create the layout.
    auto* const layout = new QHBoxLayout();
    layout->setObjectName(service_id + QString::fromLatin1("/QHBoxLayout"));
    layout->setContentsMargins(0, 0, 0, 0);
    layout->addWidget(m_progress_bar_widget->widget());

    // Add layout to the qt_container.
    widget_container->set_layout(layout);
}

//-----------------------------------------------------------------------------

void progress_bar::stopping()
{
    m_progress_bar_widget.reset();

    this->destroy();
} // namespace sight::module::ui::qt

//-----------------------------------------------------------------------------

void progress_bar::updating()
{
}

//-----------------------------------------------------------------------------

void progress_bar::configuring()
{
    this->initialize();
}

//-----------------------------------------------------------------------------

void progress_bar::add_monitor(core::progress::monitor::sptr _monitor)
{
    if(m_progress_bar_widget)
    {
        m_progress_bar_widget->add_monitor(_monitor);
    }
}

//-----------------------------------------------------------------------------

} // namespace sight::module::ui::qt