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
|
// 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 <glibmm/i18n.h>
#include <glibmm/main.h>
#include <glibmm/timer.h>
#include <gtkmm.h>
#include "display/cairo-utils.h"
#include "inkscape.h"
#include "object/sp-defs.h"
#include "object/sp-item.h"
#include "object/sp-namedview.h"
#include "object/sp-root.h"
#include "util/preview.h"
namespace Inkscape {
namespace UI {
namespace Dialog {
void ExportPreview::resetPixels()
{
clear();
show();
}
ExportPreview::~ExportPreview()
{
if (drawing) {
if (_document) {
_document->getRoot()->invoke_hide(visionkey);
}
delete drawing;
drawing = nullptr;
}
if (timer) {
timer->stop();
delete timer;
timer = nullptr;
}
if (renderTimer) {
renderTimer->stop();
delete renderTimer;
renderTimer = nullptr;
}
_item = nullptr;
_document = nullptr;
}
void ExportPreview::setItem(SPItem *item)
{
_item = item;
_dbox = Geom::OptRect();
}
void ExportPreview::setDbox(double x0, double x1, double y0, double y1)
{
if (!_document) {
return;
}
if ((x1 - x0 == 0) || (y1 - y0) == 0) {
return;
}
_item = nullptr;
_dbox = Geom::Rect(Geom::Point(x0, y0), Geom::Point(x1, y1)) * _document->dt2doc();
}
void ExportPreview::setDocument(SPDocument *document)
{
if (drawing) {
if (_document) {
_document->getRoot()->invoke_hide(visionkey);
}
delete drawing;
drawing = nullptr;
}
_document = document;
if (_document) {
drawing = new Inkscape::Drawing();
visionkey = SPItem::display_key_new(1);
DrawingItem *ai = _document->getRoot()->invoke_show(*drawing, visionkey, SP_ITEM_SHOW_DISPLAY);
if (ai) {
drawing->setRoot(ai);
}
}
}
void ExportPreview::refreshHide(const std::vector<SPItem *> &list)
{
_hidden_excluded = std::vector<SPItem *>(list.begin(), list.end());
_hidden_requested = true;
}
void ExportPreview::performHide(const std::vector<SPItem *> *list)
{
if (_document) {
if (isLastHide) {
if (drawing) {
if (_document) {
_document->getRoot()->invoke_hide(visionkey);
}
delete drawing;
drawing = nullptr;
}
drawing = new Inkscape::Drawing();
visionkey = SPItem::display_key_new(1);
DrawingItem *ai = _document->getRoot()->invoke_show(*drawing, visionkey, SP_ITEM_SHOW_DISPLAY);
if (ai) {
drawing->setRoot(ai);
}
isLastHide = false;
}
if (list && !list->empty()) {
hide_other_items_recursively(_document->getRoot(), *list);
isLastHide = true;
}
}
}
void ExportPreview::hide_other_items_recursively(SPObject *o, const std::vector<SPItem *> &list)
{
if (SP_IS_ITEM(o) && !SP_IS_DEFS(o) && !SP_IS_ROOT(o) && !SP_IS_GROUP(o) &&
list.end() == find(list.begin(), list.end(), o)) {
SP_ITEM(o)->invoke_hide(visionkey);
}
// recurse
if (list.end() == find(list.begin(), list.end(), o)) {
for (auto &child : o->children) {
hide_other_items_recursively(&child, list);
}
}
}
void ExportPreview::queueRefresh()
{
if (drawing == nullptr) {
return;
}
if (!pending) {
pending = true;
if (!timer) {
timer = new Glib::Timer();
}
Glib::signal_idle().connect(sigc::mem_fun(this, &ExportPreview::refreshCB), Glib::PRIORITY_DEFAULT_IDLE);
}
}
bool ExportPreview::refreshCB()
{
bool callAgain = true;
if (!timer) {
timer = new Glib::Timer();
}
if (timer->elapsed() > minDelay) {
callAgain = false;
refreshPreview();
pending = false;
}
return callAgain;
}
void ExportPreview::refreshPreview()
{
auto document = _document;
if (!timer) {
timer = new Glib::Timer();
}
if (timer->elapsed() < minDelay) {
// Do not refresh too quickly
queueRefresh();
} else if (document) {
renderPreview();
timer->reset();
}
}
/*
This is main function which finally render preview. Call this after setting document, item and dbox.
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 simply we do nothing.
*/
void ExportPreview::renderPreview()
{
if (!renderTimer) {
renderTimer = new Glib::Timer();
}
renderTimer->reset();
if (drawing == nullptr) {
return;
}
if (_hidden_requested) {
this->performHide(&_hidden_excluded);
_hidden_requested = false;
}
if (_document) {
GdkPixbuf *pb = nullptr;
if (_item) {
pb = Inkscape::UI::PREVIEW::render_preview(_document, *drawing, _item, size, size);
} else if (_dbox) {
pb = Inkscape::UI::PREVIEW::render_preview(_document, *drawing, nullptr, size, size, &_dbox);
}
if (pb) {
set(Glib::wrap(pb));
show();
}
}
renderTimer->stop();
minDelay = std::max(0.1, renderTimer->elapsed() * 3.0);
}
} // 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 :
|