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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
|
/** @file
* @brief A dialog for CSS selectors
*/
/* Authors:
* Kamalpreet Kaur Grewal
*
* Copyright (C) Kamalpreet Kaur Grewal 2016 <grewalkamal005@gmail.com>
*
* Released under GNU GPL, read the file 'COPYING' for more information
*/
#include "styledialog.h"
#include "ui/widget/addtoicon.h"
#include "widgets/icon.h"
#include "verbs.h"
#include "sp-object.h"
#include "selection.h"
#include "xml/attribute-record.h"
using Inkscape::Util::List;
using Inkscape::XML::AttributeRecord;
#define REMOVE_SPACES(x) x.erase(std::remove(x.begin(), x.end(), ' '), x.end());
namespace Inkscape {
namespace UI {
namespace Dialog {
/**
* @brief StyleDialog::_styleButton
* @param btn
* @param iconName
* @param tooltip
* This function sets the style of '+' and '-' buttons at the bottom of dialog.
*/
void StyleDialog::_styleButton(Gtk::Button& btn, char const* iconName,
char const* tooltip)
{
GtkWidget *child = sp_icon_new(Inkscape::ICON_SIZE_SMALL_TOOLBAR, iconName);
gtk_widget_show(child);
btn.add(*manage(Glib::wrap(child)));
btn.set_relief(Gtk::RELIEF_NONE);
btn.set_tooltip_text (tooltip);
}
/**
* Constructor
* A treeview and a set of two buttons are added to the dialog. _addSelector
* adds selectors to treeview. Currently, delete button is disabled.
*/
StyleDialog::StyleDialog() :
UI::Widget::Panel("", "/dialogs/style", SP_VERB_DIALOG_STYLE),
_desktop(0)
{
set_size_request(200, 200);
_mainBox.pack_start(_scrolledWindow, Gtk::PACK_EXPAND_WIDGET);
_treeView.set_headers_visible(false);
_scrolledWindow.add(_treeView);
_scrolledWindow.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
_store = Gtk::ListStore::create(_mColumns);
_treeView.set_model(_store);
Inkscape::UI::Widget::AddToIcon * addRenderer = manage(
new Inkscape::UI::Widget::AddToIcon() );
addRenderer->property_active() = true;
_treeView.append_column("type", *addRenderer);
_treeView.append_column("Selector Name", _mColumns._selectorLabel);
create = manage( new Gtk::Button() );
_styleButton(*create, "list-add", "Add a new CSS Selector");
create->signal_clicked().connect(sigc::mem_fun(*this,
&StyleDialog::_addSelector));
del = manage( new Gtk::Button() );
_styleButton(*del, "list-remove", "Remove a CSS Selector");
del->signal_clicked().connect(sigc::mem_fun(*this,
&StyleDialog::_delSelector));
del->set_sensitive(false);
_mainBox.pack_end(_buttonBox, Gtk::PACK_SHRINK);
_buttonBox.pack_start(*create, Gtk::PACK_SHRINK);
_buttonBox.pack_start(*del, Gtk::PACK_SHRINK);
_getContents()->pack_start(_mainBox, Gtk::PACK_EXPAND_WIDGET);
_targetDesktop = getDesktop();
setDesktop(_targetDesktop);
/**
* @brief document
* If an existing document is opened, its XML representation is obtained
* and is then used to populate the treeview with the already existing
* selectors in the style element.
*/
_styleExists = false;
_document = _targetDesktop->doc();
_num = _document->getReprRoot()->childCount();
_sValue = _populateTree(_getSelectorVec());
}
StyleDialog::~StyleDialog()
{
setDesktop(NULL);
}
void StyleDialog::setDesktop( SPDesktop* desktop )
{
Panel::setDesktop(desktop);
_desktop = Panel::getDesktop();
}
/**
* @brief StyleDialog::_addSelector
* This function is the slot to the signal emitted when '+' at the bottom of
* the dialog is clicked.
*/
void StyleDialog::_addSelector()
{
Gtk::TreeModel::Row row = *(_store->append());
/**
* On clicking '+' button, an entrybox with default text opens up. If an
* object is already selected, a selector with value in the entry
* is added to a new style element.
*/
Gtk::Dialog *textDialogPtr = new Gtk::Dialog();
Gtk::Entry *textEditPtr = manage ( new Gtk::Entry() );
textDialogPtr->add_button("Add", Gtk::RESPONSE_OK);
textDialogPtr->get_vbox()->pack_start(*textEditPtr, Gtk::PACK_SHRINK);
/**
* By default, the entrybox contains 'Class1' as text. However, if object(s)
* is(are) selected and user clicks '+' at the bottom of dialog, the
* entrybox will have the id(s) of the selected objects as text.
*/
if ( _desktop->selection->isEmpty() )
textEditPtr->set_text("Class1");
else {
std::vector<SPObject*> selected = _desktop->getSelection()->list();
textEditPtr->set_text(_setClassAttribute(selected));
}
textDialogPtr->set_size_request(200, 100);
textDialogPtr->show_all();
int result = textDialogPtr->run();
/**
* @brief selectorName
* This string stores selector name. If '#' or a '.' is present in the
* beginning of string, text from entrybox is saved directly as name for
* selector. If text like 'red' is written in entrybox, it is prefixed
* with a dot.
*/
std::string selectorName = "";
if ( !textEditPtr->get_text().empty() ) {
if ( textEditPtr->get_text().at(0) == '#' ||
textEditPtr->get_text().at(0) == '.' )
selectorName = textEditPtr->get_text();
else
selectorName = "." + textEditPtr->get_text();
}
else {
selectorName = ".Class1";
}
switch (result) {
case Gtk::RESPONSE_OK:
textDialogPtr->hide();
row[_mColumns._selectorLabel] = selectorName;
break;
default:
break;
}
del->set_sensitive(true);
/**
* The selector name objects is set to the text that the user sets in the
* entrybox. If the attribute does not exist, it is
* created. In case the attribute already has a value, the new value entered
* is appended to the values. If a style attribute does not exist, it is
* created with an empty value. Also if a class selector is added, then
* class attribute for the selected object is set too.
*/
if ( _desktop->selection ) {
std::vector<SPObject*> selected = _desktop->getSelection()->list();
std::string selectorValue;
for ( unsigned i = 0; i < selected.size(); ++i ) {
SPObject *obj = selected.at(i);
std::string style;
if (obj->getRepr()->attribute("style"))
{
for ( List<AttributeRecord const> iter = obj->getRepr()->attributeList();
iter; ++iter ) {
gchar const * property = g_quark_to_string(iter->key);
gchar const * value = iter->value;
if ( std::string(property) == "style" )
{
selectorValue = row[_mColumns._selectorLabel] + "{"
+ std::string(value) + "}" + "\n";
}
}
}
else
{
style = " ";
obj->getRepr()->setAttribute("style", style);
}
if ( strcmp(selectorName.substr(0,1).c_str(), ".") == 0 ){
if (!obj->getRepr()->attribute("class"))
obj->getRepr()->setAttribute("class", textEditPtr->get_text()
.erase(0,1));
else
obj->getRepr()->setAttribute("class", std::string(obj->
getRepr()->
attribute("class"))
+ " " + textEditPtr->get_text()
.erase(0,0));
}
/**
* @brief root
* A new style element is added to the document with value obtained
* from selectorValue above. If style element already exists, then
* the new selector's content is appended to its previous content.
*/
for ( unsigned i = 0; i < _num; ++i )
{
if ( std::string(_document->getReprRoot()->nthChild(i)->name())
== "svg:style" )
{
_styleExists = true;
_styleChild = _document->getReprRoot()->nthChild(i);
break;
}
else
_styleExists = false;
}
if ( _styleExists )
{
_sValue = _sValue + selectorValue;
_styleChild->firstChild()->setContent(_sValue.c_str());
}
else
{
_sValue = selectorValue;
Inkscape::XML::Node *root = obj->getRepr()->document()->root();
Inkscape::XML::Node *newChild = obj->getRepr()->document()
->createElement("svg:style");
Inkscape::XML::Node *smallChildren = obj->getRepr()->document()
->createTextNode(selectorValue.c_str());
newChild->appendChild(smallChildren);
Inkscape::GC::release(smallChildren);
root->addChild(newChild, NULL);
Inkscape::GC::release(newChild);
}
_selectorVec.push_back(std::make_pair(selectorName, selectorValue));
}
}
}
/**
* @brief StyleDialog::_delSelector
* This function deletes selector when '-' at the bottom is clicked. The index
* of selected row is obtained and the corresponding selector and its values are
* deleted from the selector vector. Then _sValue's content is reset and contains
* only selectors remaining in the selVec (or treeview).
*/
void StyleDialog::_delSelector()
{
Glib::RefPtr<Gtk::TreeSelection> refTreeSelection = _treeView.get_selection();
Gtk::TreeModel::iterator iter = refTreeSelection->get_selected();
std::vector<std::pair<std::string, std::string> >selVec = _getSelectorVec();
Gtk::TreeModel::Path path;
if (iter)
{
Gtk::TreeModel::Row row = *iter;
path = _treeView.get_model()->get_path(iter);
int i = atoi(path.to_string().c_str());
if (selVec.size() != 0)
{
selVec.erase(selVec.begin()+i);
_sValue.clear();
if (selVec.size() != 0)
{
for (unsigned i = 0; i < selVec.size(); ++i)
{
std::string selValue = (selVec[i].first + "{"
+ selVec[i].second + " }\n");
_sValue.append(selValue.c_str());
}
}
/**
* Only if a value exists in _sValue and there is a _styleChild
* which contains the style element, then the content in style
* element is updated else the _styleChild is set and its content
* is set to an empty string.
*/
if (!_sValue.empty() && _styleChild)
_styleChild->firstChild()->setContent(_sValue.c_str());
else
{
for ( unsigned i = 0; i < _num; ++i )
{
if ( std::string(_document->getReprRoot()->nthChild(i)->name())
== "svg:style" )
{
_styleChild = _document->getReprRoot()->nthChild(i);
}
}
_styleChild->firstChild()->setContent("");
}
}
_store->erase(row);
}
}
/**
* @brief StyleDialog::_setClassAttribute
* @param sel
* @return This function returns the ids of objects selected which are passed
* to entrybox.
*/
std::string StyleDialog::_setClassAttribute(std::vector<SPObject*> sel)
{
std::string str = "";
for ( unsigned i = 0; i < sel.size(); ++i ) {
SPObject *obj = sel.at(i);
str = str + "#" + std::string(obj->getId()) + " ";
}
return str;
}
/**
* @brief StyleDialog::_getSelectorVec
* @return selVec
* This function returns a vector whose key is the style selector name and value
* is the style properties. All style selectors are extracted from svg:style
* element.
*/
std::vector<std::pair<std::string, std::string> >StyleDialog::_getSelectorVec()
{
std::string key, value;
std::vector<std::pair<std::string, std::string> > selVec;
for ( unsigned i = 0; i < _num; ++i )
{
if ( std::string(_document->getReprRoot()->nthChild(i)->name()) == "svg:style" )
{
std::stringstream str;
str << _document->getReprRoot()->nthChild(i)->firstChild()->content();
std::string sel;
while(std::getline(str, sel, '\n')){
REMOVE_SPACES(sel);
if (!sel.empty())
{
key = strtok(strdup(sel.c_str()), "{");
value = strtok(NULL, "}");
selVec.push_back(std::make_pair(key, value));
}
}
}
}
return selVec;
}
/**
* @brief StyleDialog::_populateTree
* @param _selVec
* This function populates the treeview with selectors available in the
* stylesheet.
*/
std::string StyleDialog::_populateTree(std::vector<std::pair<std::string,
std::string> > _selVec)
{
std::vector<std::pair<std::string, std::string> > _selectVec = _selVec;
std::string selectorValue;
for( unsigned it = 0; it < _selectVec.size(); ++it ) {
Gtk::TreeModel::Row row = *(_store->append());
row[_mColumns._selectorLabel] = _selectVec[it].first;
std::string selValue = _selectVec[it].first + " { "
+ _selectVec[it].second + " }" + "\n";
selectorValue.append(selValue.c_str());
}
if (_selectVec.size() > 0)
del->set_sensitive(true);
for ( unsigned i = 0; i < _num; ++i )
{
if ( std::string(_document->getReprRoot()->nthChild(i)->name())
== "svg:style" )
{
_styleChild = _document->getReprRoot()->nthChild(i);
}
}
return selectorValue;
}
} // namespace Dialog
} // namespace UI
} // namespace Inkscape
|