GCC Code Coverage Report


./
File: modules/ui/viz/texture_selector.cpp
Date: 2025-01-21 16:21:04
Lines:
2/64
3.1%
Functions:
2/8
25.0%
Branches:
0/150
0.0%

Line Branch Exec Source
1 /************************************************************************
2 *
3 * Copyright (C) 2014-2024 IRCAD France
4 * Copyright (C) 2014-2020 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 "texture_selector.hpp"
24
25 #include <core/com/signal.hxx>
26
27 #include <data/array.hpp>
28 #include <data/image.hpp>
29 #include <data/material.hpp>
30
31 #include <io/__/service/io_types.hpp>
32
33 #include <service/macros.hpp>
34 #include <service/op.hpp>
35
36 #include <ui/__/dialog_editor.hpp>
37 #include <ui/qt/container/widget.hpp>
38
39 #include <QFileDialog>
40 #include <QHBoxLayout>
41 #include <QPushButton>
42 #include <QString>
43 #include <QVBoxLayout>
44
45 namespace sight::module::ui::viz
46 {
47
48 //------------------------------------------------------------------------------
49
50 3 texture_selector::texture_selector() noexcept =
51 default;
52
53 //------------------------------------------------------------------------------
54
55 6 texture_selector::~texture_selector() noexcept =
56 default;
57
58 //------------------------------------------------------------------------------
59
60 void texture_selector::starting()
61 {
62 this->create();
63
64 const QString service_id = QString::fromStdString(base_id());
65
66 auto qt_container = std::dynamic_pointer_cast<sight::ui::qt::container::widget>(
67 this->get_container()
68 );
69 qt_container->get_qt_container()->setObjectName(service_id);
70
71 m_load_button = new QPushButton(QString("Load texture"));
72 m_load_button->setObjectName(service_id + "/" + m_load_button->text());
73 m_load_button->setToolTip(QString("Selected organ's texture"));
74 m_load_button->setMinimumSize(m_load_button->sizeHint());
75
76 m_delete_button = new QPushButton(QString("Remove texture"));
77 m_delete_button->setObjectName(service_id + "/" + m_delete_button->text());
78 m_delete_button->setToolTip(QString("Remove organ's texture"));
79 m_delete_button->setMinimumSize(m_delete_button->sizeHint());
80
81 auto* layout = new QVBoxLayout();
82 layout->addWidget(m_load_button, 0);
83 layout->addWidget(m_delete_button, 0);
84
85 qt_container->set_layout(layout);
86 qt_container->set_enabled(true);
87
88 QObject::connect(m_load_button, &QPushButton::clicked, this, &self_t::on_load_button);
89 QObject::connect(m_delete_button, &QPushButton::clicked, this, &self_t::on_delete_button);
90
91 this->updating();
92 }
93
94 //------------------------------------------------------------------------------
95
96 void texture_selector::stopping()
97 {
98 QObject::disconnect(m_load_button, &QPushButton::clicked, this, &self_t::on_load_button);
99 QObject::disconnect(m_delete_button, &QPushButton::clicked, this, &self_t::on_delete_button);
100
101 this->destroy();
102 }
103
104 //------------------------------------------------------------------------------
105
106 void texture_selector::configuring()
107 {
108 this->initialize();
109 }
110
111 //------------------------------------------------------------------------------
112
113 void texture_selector::updating()
114 {
115 }
116
117 //------------------------------------------------------------------------------
118
119 void texture_selector::on_load_button()
120 {
121 const auto reconstruction = m_reconstruction.lock();
122 SIGHT_ASSERT("No associated Reconstruction", reconstruction);
123
124 data::material::sptr material = reconstruction->get_material();
125 data::image::sptr image = material->get_diffuse_texture();
126
127 bool existing_texture = (image != nullptr);
128
129 // We have to instantiate a new image if necessary
130 if(!existing_texture)
131 {
132 image = std::make_shared<data::image>();
133 material->set_diffuse_texture(image);
134 }
135
136 auto srv = sight::service::add<sight::ui::dialog_editor>("sight::module::ui::io::selector");
137 srv->set_inout(image, io::service::DATA_KEY);
138
139 srv->configure();
140 srv->start();
141 srv->update();
142 srv->stop();
143 sight::service::remove(srv);
144
145 // If we didn't have to create a new texture, we can notify the associated image
146 if(existing_texture)
147 {
148 auto sig = image->signal<data::object::modified_signal_t>(data::object::MODIFIED_SIG);
149 sig->emit();
150 }
151 else
152 {
153 auto sig = material->signal<data::material::added_texture_signal_t>(
154 data::material::ADDED_TEXTURE_SIG
155 );
156 sig->emit(image);
157 }
158 }
159
160 //------------------------------------------------------------------------------
161
162 void texture_selector::on_delete_button()
163 {
164 const auto reconstruction = m_reconstruction.lock();
165 SIGHT_ASSERT("No associated Reconstruction", reconstruction);
166
167 data::material::sptr material = reconstruction->get_material();
168 data::image::sptr image = material->get_diffuse_texture();
169
170 if(image)
171 {
172 material->set_diffuse_texture(nullptr);
173 auto sig = material->signal<data::material::removed_texture_signal_t>(
174 data::material::REMOVED_TEXTURE_SIG
175 );
176 sig->emit(image);
177 }
178 }
179
180 //------------------------------------------------------------------------------
181
182 } // namespace sight::module::ui::viz
183