File: compositor_selector.cpp

package info (click to toggle)
sight 25.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 43,252 kB
  • sloc: cpp: 310,629; xml: 17,622; ansic: 9,960; python: 1,379; sh: 144; makefile: 33
file content (308 lines) | stat: -rw-r--r-- 9,438 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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/************************************************************************
 *
 * Copyright (C) 2014-2024 IRCAD France
 * Copyright (C) 2014-2019 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 "compositor_selector.hpp"

#include <core/com/slots.hxx>

#include <data/map.hpp>

#include <service/macros.hpp>
#include <service/registry.hpp>

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

#include <viz/scene3d/render.hpp>

#include <OGRE/OgreCompositorManager.h>
#include <OGRE/OgreResource.h>
#include <OGRE/OgreResourceManager.h>

#include <QListWidgetItem>
#include <QWidget>

namespace sight::module::ui::viz
{

using sight::viz::scene3d::layer;

const core::com::slots::key_t compositor_selector::INIT_COMPOSITOR_LIST_SLOT = "initCompositorList";

static const std::string COMPOSITOR_RESOURCEGROUP_NAME = "compositorsPostFX";

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

compositor_selector::compositor_selector() noexcept
{
    new_slot(INIT_COMPOSITOR_LIST_SLOT, &compositor_selector::init_compositor_list, this);
}

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

compositor_selector::~compositor_selector() noexcept =
    default;

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

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

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

    m_layers_box       = new QComboBox();
    m_compositor_chain = new QListWidget();

    auto* layout = new QVBoxLayout();
    layout->addWidget(m_layers_box);
    layout->addWidget(m_compositor_chain);

    qt_container->set_layout(layout);

    this->refresh_renderers();

    QObject::connect(m_layers_box, SIGNAL(activated(int)), this, SLOT(on_selected_layer_item(int)));
    QObject::connect(
        m_compositor_chain,
        SIGNAL(itemChanged(QListWidgetItem*)),
        this,
        SLOT(on_selected_compositor_item(QListWidgetItem*))
    );
}

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

void compositor_selector::stopping()
{
    m_connections.disconnect();

    this->destroy();
}

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

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

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

void compositor_selector::updating()
{
}

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

void compositor_selector::on_selected_layer_item(int _index)
{
    m_current_layer = m_layers[static_cast<std::size_t>(_index)];
    layer::sptr layer = m_current_layer.lock();

    if(layer)
    {
        // Set current context
        layer->render_service()->make_current();

        // Empty the list widget
        m_compositor_chain->clear();

        // Update the current layer
        m_layer_compositor_chain = layer->get_compositor_chain();

        // We need the ogre's viewport in order to add the compositors,
        // this is why we have to check the viewport's existence
        if(layer->get_viewport() != nullptr)
        {
            // Fill the list widget
            this->update_compositor_list();
            // Iterates through the compositors and checks the enabled ones
            this->check_enabled_compositors();
        }
    }
}

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

void compositor_selector::on_selected_compositor_item(QListWidgetItem* _compositor_item)
{
    const std::string compositor_name = _compositor_item->text().toStdString();
    const bool is_checked             = (_compositor_item->checkState() == Qt::Checked);
    layer::sptr layer                 = m_current_layer.lock();

    if(layer)
    {
        layer->render_service()->make_current();
        layer->update_compositor_state(compositor_name, is_checked);
    }
}

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

void compositor_selector::init_compositor_list(layer::sptr _layer)
{
    m_current_layer = m_layers[0];

    if(_layer == m_current_layer.lock())
    {
        on_selected_layer_item(0);
    }
}

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

void compositor_selector::refresh_renderers()
{
    m_layers_box->clear();

    // Fill layer box with all enabled layers
    const auto renderers = sight::service::get_services("sight::viz::scene3d::render");

    for(const auto& srv : renderers)
    {
        auto render = std::dynamic_pointer_cast<sight::viz::scene3d::render>(srv);

        for(auto& layer_map : render->get_layers())
        {
            const std::string id  = layer_map.first;
            std::string render_id = render->get_id();
            m_layers_box->addItem(QString::fromStdString(render_id + " : " + id));
            m_layers.push_back(layer_map.second);

            m_connections.connect(
                layer_map.second,
                layer::INIT_LAYER_SIG,
                this->get_sptr(),
                INIT_COMPOSITOR_LIST_SLOT
            );
        }
    }

    if(!m_layers.empty())
    {
        this->on_selected_layer_item(0);
    }
}

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

void compositor_selector::update_compositor_list()
{
    layer::sptr layer = m_current_layer.lock();

    if(layer)
    {
        layer->render_service()->make_current();

        // Retrieving all compositors
        Ogre::ResourceManager::ResourceMapIterator iter =
            Ogre::CompositorManager::getSingleton().getResourceIterator();
        while(iter.hasMoreElements())
        {
            Ogre::ResourcePtr compositor = iter.getNext();
            if(compositor->getGroup() == COMPOSITOR_RESOURCEGROUP_NAME)
            {
                QString compositor_name = compositor.get()->getName().c_str();
                layer->add_available_compositor(compositor_name.toStdString());

                auto* new_compositor = new QListWidgetItem(compositor_name, m_compositor_chain);
                new_compositor->setFlags(new_compositor->flags() | Qt::ItemIsUserCheckable);
                new_compositor->setCheckState(Qt::Unchecked);
            }
        }
    }
}

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

void compositor_selector::check_enabled_compositors()
{
    layer::sptr layer = m_current_layer.lock();

    if(layer)
    {
        layer->render_service()->make_current();

        if(!m_layer_compositor_chain.empty())
        {
            for(int i(0) ; i < m_compositor_chain->count() ; ++i)
            {
                QListWidgetItem* current_compositor = m_compositor_chain->item(i);
                std::string current_compositor_name = current_compositor->text().toStdString();

                auto layer_compositor = std::find_if(
                    m_layer_compositor_chain.begin(),
                    m_layer_compositor_chain.end(),
                    [&current_compositor_name](const auto& _compositor)
                    {
                        return _compositor.first == current_compositor_name;
                    });

                if(layer_compositor != m_layer_compositor_chain.end())
                {
                    if(layer_compositor->second)
                    {
                        current_compositor->setCheckState(Qt::Checked);
                        layer->update_compositor_state(current_compositor->text().toStdString(), true);
                    }
                }
            }
        }
    }
}

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

void compositor_selector::uncheck_compositors()
{
    for(int i(0) ; i < m_compositor_chain->count() ; ++i)
    {
        QListWidgetItem* current_compositor = m_compositor_chain->item(i);
        current_compositor->setCheckState(Qt::Unchecked);
    }
}

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

bool compositor_selector::is_enabled_compositor(const std::string& _compositor_name)
{
    auto layer_compositor = std::find_if(
        m_layer_compositor_chain.begin(),
        m_layer_compositor_chain.end(),
        [&_compositor_name](const auto& _compositor)
        {
            return _compositor.first == _compositor_name;
        });

    if(layer_compositor != m_layer_compositor_chain.end())
    {
        return layer_compositor->second;
    }

    return false;
}

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

} // namespace sight::module::ui::viz