File: snapshot_editor.cpp

package info (click to toggle)
sight 25.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 42,184 kB
  • sloc: cpp: 289,476; xml: 17,257; ansic: 9,878; python: 1,379; sh: 144; makefile: 33
file content (173 lines) | stat: -rw-r--r-- 5,546 bytes parent folder | download
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
/************************************************************************
 *
 * Copyright (C) 2009-2024 IRCAD France
 * Copyright (C) 2012-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/viz/snapshot_editor.hpp"

#include <core/base.hpp>
#include <core/com/signal.hxx>
#include <core/com/signals.hpp>
#include <core/location/single_file.hpp>
#include <core/location/single_folder.hpp>
#include <core/runtime/path.hpp>

#include <ui/__/dialog/location.hpp>
#include <ui/__/dialog/message.hpp>
#include <ui/qt/container/widget.hpp>

#include <QHBoxLayout>
#include <QIcon>
#include <QPushButton>
#include <QString>
#include <QWidget>

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

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

const core::com::signals::key_t snapshot_editor::SNAPPED_SIG = "snapped";

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

snapshot_editor::snapshot_editor() noexcept
{
    m_sig_snapped = new_signal<snapped_signal_t>(SNAPPED_SIG);
}

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

snapshot_editor::~snapshot_editor() noexcept =
    default;

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

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

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

    std::filesystem::path path = core::runtime::get_module_resource_file_path(
        "sight::module::ui::qt",
        "frame.svg"
    );
    QIcon icon(QString::fromStdString(path.string()));
    m_snap_button = new QPushButton(icon, "");
    m_snap_button->setToolTip(QObject::tr("Snapshot"));

    auto* h_layout = new QHBoxLayout();
    h_layout->addWidget(m_snap_button);
    h_layout->setContentsMargins(0, 0, 0, 0);

    qt_container->set_layout(h_layout);

    QObject::connect(m_snap_button, &QPushButton::clicked, this, &self_t::on_snap_button);
}

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

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

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

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

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

void snapshot_editor::updating()
{
}

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

void snapshot_editor::info(std::ostream& /*_sstream*/)
{
}

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

void snapshot_editor::on_snap_button()
{
    auto qt_container = std::dynamic_pointer_cast<sight::ui::qt::container::widget>(
        this->get_container()
    );
    QWidget* container = qt_container->get_qt_container();
    SIGHT_ASSERT("container not instanced", container);
    if(container->isVisible())
    {
        std::string filename = sight::module::ui::qt::viz::snapshot_editor::request_file_name();

        if(!filename.empty())
        {
            m_sig_snapped->async_emit(filename);
        }
    }
    else
    {
        std::string msg_info("It is not possible to snapshot the negato view. This view is not shown on screen.");
        sight::ui::dialog::message message_box;
        message_box.set_title("Negato view snapshot");
        message_box.set_message(msg_info);
        message_box.set_icon(sight::ui::dialog::message::warning);
        message_box.add_button(sight::ui::dialog::message::ok);
        message_box.show();
    }
}

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

std::string snapshot_editor::request_file_name()
{
    static auto default_directory = std::make_shared<core::location::single_folder>();
    std::string file_name;

    sight::ui::dialog::location dialog_file;
    dialog_file.set_title("Save snapshot as");
    dialog_file.set_default_location(default_directory);
    dialog_file.add_filter("Image file", "*.jpg *.jpeg *.bmp *.png *.tiff");
    dialog_file.add_filter("jpeg", "*.jpg *.jpeg");
    dialog_file.add_filter("bmp", "*.bmp");
    dialog_file.add_filter("png", "*.png");
    dialog_file.add_filter("tiff", "*.tiff");
    dialog_file.add_filter("all", "*.*");
    dialog_file.set_option(sight::ui::dialog::location::write);

    auto result = std::dynamic_pointer_cast<core::location::single_file>(dialog_file.show());
    if(result)
    {
        file_name = result->get_file().string();
        default_directory->set_folder(result->get_file().parent_path());
        dialog_file.save_default_location(default_directory);
    }

    return file_name;
}

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

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