File: pdf_writer.hpp

package info (click to toggle)
sight 25.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 42,180 kB
  • sloc: cpp: 289,476; xml: 17,257; ansic: 9,878; python: 1,379; sh: 144; makefile: 33
file content (175 lines) | stat: -rw-r--r-- 5,289 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
174
175
/************************************************************************
 *
 * Copyright (C) 2016-2025 IRCAD France
 * Copyright (C) 2016-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/>.
 *
 ***********************************************************************/

#pragma once

#include <data/image.hpp>

#include <io/__/service/writer.hpp>

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

#include <QPdfWriter>

namespace sight::module::io::document
{

/**
 *
 * @brief Creates and writes a PDF containing images.
 *
 * The service will take IDs at the configuration. Those IDs can either be UIDs of data::image or
 * either SID/WID of Qt containers. Converts the images if needed, in order to writes them in the PDF.
 * A scaling is applied to fit the A4 format of the PDF pages.
 *
 * @warning When the visuVTKAdaptor::Snapshot service fills an data::image, the renderer must not be hidden.
 * For example if there are multiple tabs of renderer, only the image corresponding to the selected tab will be
 * correctly saved in the PDF.
 *
 * @section XML XML configuration
 * @code{.xml}
    <service uid="..." type="sight::module::io::document::pdf_writer">
        <in group="image">
            <key uid="..."/>
            <key uid="..."/>
        </in>
        <container uid="..."/>
    </service>
 *
 * @endcode
 * @subsection Input Input:
 * - \b image [sight::data::image] : Defines the UID of the data::image to write.
 * - \b container(optional) : Defines the SID or the WID of the container to write.
 */

class pdf_writer : public sight::io::service::writer
{
public:

    using images_scaled_list_t = std::vector<QImage>;
    using images_list_t        = std::vector<data::image::sptr>;
    using images_i_ds_t        = std::vector<std::string>;

    using containers_list_t = std::vector<QWidget*>;
    using containers_i_ds_t = std::vector<std::string>;

    SIGHT_DECLARE_SERVICE(pdf_writer, sight::io::service::writer);
    /**
     * @brief Constructor : does nothing
     */
    pdf_writer() noexcept;

    /**
     * @brief Destructor
     */
    ~pdf_writer() noexcept override = default;

protected:

    /** @name Service methods ( override from service::base )
     * @{
     */

    /**
     * @brief Starting method : Fills the list of Qt containers as well as the list of images got from
     * the configuration.
     *
     * This method is used to initialize the service.
     */
    void starting() override;

    /**
     * @brief Stopping method : default does nothing.
     *
     * The stopping method is empty for this service.
     */
    void stopping() override;

    /**
     * @brief Configure service. This method is called by configure() from base service ( service::base )
     *
     * Gets the images or containers from their IDs.
     *
     */
    void configuring() override;

    /**
     * @brief Configure the image path.
     *
     * This method is used to set the file path of the PDF to write.
     *
     */
    void open_location_dialog() override;

    /**
     * @brief Updating method. Creates a new PDF.
     *
     * Loops over the containers list and the images list. Creates a new A4 page for each,
     * converts the containers and the images to QImage and writes them into the newly created PDF.
     */
    void updating() override;

    /**
     * @brief Info method.
     *
     * This method is used to give
     * information about the service.
     *
     * @param[out] _sstream output stream
     */
    void info(std::ostream& _sstream) override;
    /// @}

    /// Returns managed path type, here service manages only single file
    sight::io::service::path_type_t get_path_type() const override;

private:

    /**
     * @brief convertFwImageToQImage: converts a data::image to a QImage
     * @param _image    data::image to convert
     * @return converted QImage
     */
    static QImage convert_fw_image_to_q_image(const data::image& _image);

    /**
     * @brief List of images IDs to export into the PDF.
     * Filled at the configuring from the XML configuration, and used at starting().
     */
    images_i_ds_t m_images_ui_ds;

    /**
     * @brief List of containers IDs to export into the PDF.
     * Filled at configuring from the XML configuration, and used at starting().
     */
    containers_i_ds_t m_containers_i_ds;

    /**
     * @brief List of containers to export into the PDF.
     */
    containers_list_t m_containers_to_export;

    static constexpr std::string_view IMAGE_INPUT = "image";
    data::ptr_vector<data::image, sight::data::access::in> m_images {this, IMAGE_INPUT};
};

} // namespace sight::module::io::document