File: images_selector.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 (232 lines) | stat: -rw-r--r-- 6,812 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/************************************************************************
 *
 * Copyright (C) 2014-2024 IRCAD France
 * Copyright (C) 2014-2020 IHU Strasbourg
 *
 * 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 "module/ui/qt/calibration/images_selector.hpp"

#include <core/com/slot.hpp>
#include <core/com/slot.hxx>
#include <core/com/slots.hpp>
#include <core/com/slots.hxx>
#include <core/id.hpp>

#include <data/image.hpp>

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

#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QWidget>

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

const core::com::slots::key_t images_selector::ADD_SLOT    = "add";
const core::com::slots::key_t images_selector::REMOVE_SLOT = "remove";
const core::com::slots::key_t images_selector::RESET_SLOT  = "reset";

//------------------------------------------------------------------------------
images_selector::images_selector() noexcept
{
    new_slot(ADD_SLOT, &images_selector::add, this);
    new_slot(REMOVE_SLOT, &images_selector::remove, this);
    new_slot(RESET_SLOT, &images_selector::reset, this);
}

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

images_selector::~images_selector() noexcept =
    default;

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

void images_selector::configuring()
{
    sight::ui::service::initialize();
}

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

void images_selector::starting()
{
    const auto frame_tl = m_frame_tl.lock();
    SIGHT_ASSERT("Frame timeline is not found.", frame_tl);

    sight::ui::service::create();
    auto qt_container = std::dynamic_pointer_cast<sight::ui::qt::container::widget>(get_container());

    // Main container, VBox
    auto* v_layout = new QVBoxLayout();

    //   First HBox, displays number of items and the remove button
    auto* nb_items_h_box = new QHBoxLayout();

    //     Fill the nbItemsHBox
    auto* label = new QLabel("nb captures:");
    nb_items_h_box->addWidget(label);

    m_nb_captures_label = new QLabel("0");
    nb_items_h_box->addWidget(m_nb_captures_label);
    nb_items_h_box->addStretch();

    //   The ListWidget
    m_captures_list_widget = new QListWidget();

    // Fill the main VBox
    v_layout->addLayout(nb_items_h_box);
    v_layout->addWidget(m_captures_list_widget);

    qt_container->set_layout(v_layout);

    this->updating();
}

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

void images_selector::stopping()
{
    this->destroy();
}

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

void images_selector::updating()
{
    const auto vector = m_selected_image.lock();

    m_captures_list_widget->clear();
    unsigned int capture_idx = 0;
    for(const data::object::sptr& obj : *vector)
    {
        data::image::sptr image = std::dynamic_pointer_cast<data::image>(obj);
        if(image)
        {
            QString count_string;

            count_string = QString("%1. %2").arg(capture_idx).arg(QString::fromStdString(image->get_id()));
            m_captures_list_widget->addItem(count_string);
            ++capture_idx;
        }
    }

    m_nb_captures_label->setText(QString("%1").arg(capture_idx));
}

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

void images_selector::remove()
{
    int idx = m_captures_list_widget->currentRow();

    if(idx >= 0)
    {
        const auto vector      = m_selected_image.lock();
        data::object::sptr obj = (*vector)[std::size_t(idx)];

        const auto scoped_emitter = vector->scoped_emit();
        vector->remove(obj);

        this->updating();
    }
}

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

void images_selector::reset()
{
    const auto vector = m_selected_image.lock();

    const auto scoped_emitter = vector->scoped_emit();
    vector->clear();

    m_captures_list_widget->clear();
    m_nb_captures_label->setText(QString("0"));
}

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

void images_selector::add(core::clock::type _timestamp)
{
    const auto frame_tl = m_frame_tl.lock();
    CSPTR(data::frame_tl::buffer_t) buffer = frame_tl->get_closest_buffer(_timestamp);

    if(!buffer)
    {
        SIGHT_INFO("Buffer not found with timestamp " << _timestamp);
        return;
    }

    data::image::sptr image = std::make_shared<data::image>();

    data::image::size_t size;
    size[0] = frame_tl->get_width();
    size[1] = frame_tl->get_height();
    size[2] = 1;

    enum data::image::pixel_format_t format
    {
        data::image::pixel_format_t::undefined
    };
    // FIXME since frameTL does not have format information, we assume that image are Grayscale, RGB or RGBA according
    // to the number of components.
    switch(frame_tl->num_components())
    {
        case 1:
            format = data::image::gray_scale;
            break;

        case 3:
            format = data::image::rgb;
            break;

        case 4:
            format = data::image::rgba;
            break;

        default:
            format = data::image::undefined;
    }

    image->resize(size, frame_tl->type(), format);
    const data::image::origin_t origin = {0., 0., 0.};
    image->set_origin(origin);
    const data::image::spacing_t spacing = {1., 1., 1.};
    image->set_spacing(spacing);
    image->set_window_width({100});
    image->set_window_center({0});

    const auto dump_lock = image->dump_lock();

    const std::uint8_t* frame_buff = &buffer->get_element(0);
    auto* img_buffer               = static_cast<std::uint8_t*>(image->buffer());
    std::copy(frame_buff, frame_buff + buffer->size(), img_buffer);

    const auto vector = m_selected_image.lock();

    const auto scoped_emitter = vector->scoped_emit();
    vector->push_back(image);

    this->updating();
}

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

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