summaryrefslogtreecommitdiffstats
path: root/src/ui/widget/export-preview.cpp
blob: cf992587fe40a24022a5cae8ef307bc81788ea02 (plain)
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
// SPDX-License-Identifier: GPL-2.0-or-later
/* Authors:
 *   Anshudhar Kumar Singh <anshudhar2001@gmail.com>
 *
 * Copyright (C) 2021 Authors
 *
 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
 */

#include "export-preview.h"

#include "document.h"
#include "display/cairo-utils.h"
#include "object/sp-item.h"
#include "object/sp-root.h"
#include "util/preview.h"
#include "io/resource.h"

namespace Inkscape {
namespace UI {
namespace Dialog {

/**
 * A preview drawing object is responsible for constructing a drawing and showing it's contents
 *
 * On destruction it will gracefully invoke hide itself. You should destroy this object when
 * you need to change the document object being used for the preview.
 */
PreviewDrawing::PreviewDrawing(SPDocument *doc)
{
    _document = doc;
}

PreviewDrawing::~PreviewDrawing()
{
    destruct();
    _document = nullptr;
}

void PreviewDrawing::destruct()
{
    if (!_visionkey)
        return;

    // On exiting the document root might have gone already.
    if (auto root = _document->getRoot()) {
        root->invoke_hide(_visionkey);
    }
    _drawing.reset();
    _visionkey = 0;
}

/**
 * Construct the drawing, when needed
 */
void PreviewDrawing::construct()
{
    auto drawing = std::make_shared<Inkscape::Drawing>();
    _visionkey = SPItem::display_key_new(1);
    if (auto di = _document->getRoot()->invoke_show(*drawing, _visionkey, SP_ITEM_SHOW_DISPLAY)) {
        drawing->setRoot(di);
    } else {
        drawing.reset();
    }

    if (!_shown_items.empty()) {
        _document->getRoot()->invoke_hide_except(_visionkey, _shown_items);
    }
    _drawing = drawing;
}

/**
  * Render the drawing into a cairo image surface.
  */
bool PreviewDrawing::render(ExportPreview *widget, uint32_t bg, SPItem *item, unsigned size, Geom::OptRect const &dbox)
{
    if (!_drawing || _to_destruct) {
        if (!_construct_idle.connected()) {
            _construct_idle = Glib::signal_timeout().connect([=]() {
                _to_destruct = false;
                destruct();
                construct();
                return false;
            }, 100);
        }
        return false;
    }

    Geom::OptRect bbox = dbox;
    DrawingItem *di = nullptr;

    if (item) {
        bbox = item->documentVisualBounds();
        di = item->get_arenaitem(_visionkey);
    } else if (!dbox)
        bbox = _document->getRoot()->documentVisualBounds();

    if (!bbox)
        return true; // Force quit

    // Use a callback to set the preview rendering;
    widget->setPreview(UI::Preview::render_preview(_document, _drawing, bg, di, size, size, *bbox));
    return true;
}

/**
 * Limit the preview to just these items.
 *
 * You must call refresh after this for the change to take effect.
 */
void PreviewDrawing::set_shown_items(std::vector<SPItem*> &&list)
{
    _shown_items = std::move(list);
    _to_destruct = true;
}

void ExportPreview::resetPixels(bool new_size)
{
    clear();
    // An icon to use when the preview hasn't loaded yet
    static Glib::RefPtr<Gdk::Pixbuf> preview_loading;
    if (!preview_loading || new_size) {
        using namespace Inkscape::IO::Resource;
        preview_loading = Gdk::Pixbuf::create_from_file(get_filename(PIXMAPS, "preview_loading.svg"), size, size);
    }
    if (preview_loading) {
        set(preview_loading);
    }
    show();
}

void ExportPreview::setSize(int newSize)
{
    size = newSize;
    resetPixels(true);
}

ExportPreview::~ExportPreview()
{
    refresh_conn.disconnect();
}

void ExportPreview::setItem(SPItem *item)
{
    _item = item;
    _dbox = {};
}

void ExportPreview::setBox(Geom::Rect const &bbox)
{
    if (bbox.hasZeroArea())
        return;

    _item = nullptr;
    _dbox = bbox;
}

void ExportPreview::setDrawing(std::shared_ptr<PreviewDrawing> drawing)
{
    _drawing = drawing;
}

/*
 * This is the main function which finally renders the preview.
 * If dbox is given it will use it.
 * if item is given and not dbox then item is used.
 * If both are not given then we simply do nothing.
 */
void ExportPreview::queueRefresh()
{
    if (!_drawing || _render_idle.connected())
        return;

    _render_idle = Glib::signal_timeout().connect([=]() {
        return !_drawing->render(this, _bg_color, _item, size, _dbox);
    }, 100);
}

/**
 * Callback when the rendering is complete.
 */
void ExportPreview::setPreview(Cairo::RefPtr<Cairo::ImageSurface> surface)
{
    if (surface) {
        set(Gdk::Pixbuf::create(surface, 0, 0, surface->get_width(), surface->get_height()));
        show();
    }
}

void ExportPreview::setBackgroundColor(uint32_t bg_color)
{
    _bg_color = bg_color;
}

} // namespace Dialog
} // namespace UI
} // namespace Inkscape

/*
  Local Variables:
  mode:c++
  c-file-style:"stroustrup"
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
  indent-tabs-mode:nil
  fill-column:99
  End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :