summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJabiertxof <jabier.arraiza@marker.es>2019-08-29 16:35:15 +0000
committerJabiertxof <jabier.arraiza@marker.es>2019-08-29 16:35:27 +0000
commit50edd0c9a6e33fa5e3addefac95477b8fb365551 (patch)
tree01746a2dc08916b75c5fb581171b08bea9881eed /src
parentNoumerous bugfixes pointed in Rocket Chat (diff)
downloadinkscape-50edd0c9a6e33fa5e3addefac95477b8fb365551.tar.gz
inkscape-50edd0c9a6e33fa5e3addefac95477b8fb365551.zip
Improve CSS and Selectors dialog
Diffstat (limited to 'src')
-rw-r--r--src/ui/dialog/selectorsdialog.cpp38
-rw-r--r--src/ui/dialog/styledialog.cpp108
-rw-r--r--src/ui/dialog/styledialog.h3
3 files changed, 123 insertions, 26 deletions
diff --git a/src/ui/dialog/selectorsdialog.cpp b/src/ui/dialog/selectorsdialog.cpp
index bbe0576dc..f66876475 100644
--- a/src/ui/dialog/selectorsdialog.cpp
+++ b/src/ui/dialog/selectorsdialog.cpp
@@ -25,6 +25,7 @@
#include "verbs.h"
#include "xml/attribute-record.h"
+#include "xml/sp-css-attr.h"
#include "xml/node-observer.h"
#include <glibmm/i18n.h>
@@ -569,7 +570,6 @@ void SelectorsDialog::_readStyleElement()
bool rewrite = false;
for (unsigned i = 0; i < tokens.size()-1; i += 2) {
-
Glib::ustring selector = tokens[i];
REMOVE_SPACES(selector); // Remove leading/trailing spaces
Glib::ustring selector_old = selector;
@@ -577,7 +577,7 @@ void SelectorsDialog::_readStyleElement()
if (selector_old != selector) {
rewrite = true;
}
- if (selector.empty()) {
+ if (selector.empty() || selector == "* > .inkscapehacktmp") {
continue;
}
std::vector<Glib::ustring> tokensplus = Glib::Regex::split_simple("[,]+", selector);
@@ -660,7 +660,7 @@ void SelectorsDialog::_writeStyleElement()
_scroollock = true;
_updating = true;
SPDocument *document = SP_ACTIVE_DOCUMENT;
- Glib::ustring styleContent;
+ Glib::ustring styleContent = "";
for (auto& row: _store->children()) {
Glib::ustring selector = row[_mColumns._colSelector];
/*
@@ -675,8 +675,18 @@ void SelectorsDialog::_writeStyleElement()
// We could test if styleContent is empty and then delete the style node here but there is no
// harm in keeping it around ...
Inkscape::XML::Node *textNode = _getStyleTextNode();
+ bool empty = false;
+ if (styleContent.empty()) {
+ empty = true;
+ styleContent = "* > .inkscapehacktmp{}";
+ }
+ textNode->setContent(styleContent.c_str());
+ INKSCAPE.readStyleSheets(true);
+ if (empty) {
+ styleContent = "";
+ textNode->setContent(styleContent.c_str());
+ }
textNode->setContent(styleContent.c_str());
- INKSCAPE.readStyleSheets();
DocumentUndo::done(SP_ACTIVE_DOCUMENT, SP_VERB_DIALOG_SELECTORS, _("Edited style element."));
_updating = false;
@@ -860,6 +870,24 @@ void SelectorsDialog::_addToSelector(Gtk::TreeModel::Row row)
_updating = false;
// Add entry to style element
+ for (auto &obj : toAddObjVec) {
+ Glib::ustring css_str = "";
+ SPCSSAttr *css = sp_repr_css_attr_new();
+ SPCSSAttr *css_selector = sp_repr_css_attr_new();
+ sp_repr_css_attr_add_from_string(css, obj->getRepr()->attribute("style"));
+ Glib::ustring selprops = row[_mColumns._colProperties];
+ sp_repr_css_attr_add_from_string(css_selector, selprops.c_str());
+ for ( List<AttributeRecord const> iter = css_selector->attributeList() ;
+ iter ; ++iter )
+ {
+ gchar const * key = g_quark_to_string(iter->key);
+ css->setAttribute(key, nullptr);
+ }
+ sp_repr_css_write_string(css, css_str);
+ obj->getRepr()->setAttribute("style", css_str.c_str());
+ obj->style->readFromObject(obj);
+ obj->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
+ }
_writeStyleElement();
}
}
@@ -914,6 +942,8 @@ void SelectorsDialog::_removeFromSelector(Gtk::TreeModel::Row row)
// Add entry to style element
_writeStyleElement();
+ obj->style->readFromObject(obj);
+ obj->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
_scroollock = false;
_vadj->set_value(std::min(_scroolpos, _vadj->get_upper()));
}
diff --git a/src/ui/dialog/styledialog.cpp b/src/ui/dialog/styledialog.cpp
index 975e28dd7..98d81bb7c 100644
--- a/src/ui/dialog/styledialog.cpp
+++ b/src/ui/dialog/styledialog.cpp
@@ -28,6 +28,7 @@
#include "verbs.h"
#include "xml/attribute-record.h"
#include "xml/node-observer.h"
+#include "xml/sp-css-attr.h"
#include <map>
#include <regex>
@@ -190,6 +191,8 @@ StyleDialog::StyleDialog()
, _textNode(nullptr)
, _scroolpos(0)
, _desktopTracker()
+ , _deleted_pos(0)
+ , _deletion(false)
{
g_debug("StyleDialog::StyleDialog");
// Pack widgets
@@ -238,12 +241,10 @@ Glib::ustring StyleDialog::fixCSSSelectors(Glib::ustring selector)
{
g_debug("SelectorsDialog::fixCSSSelectors");
REMOVE_SPACES(selector);
- Glib::ustring selector_out = "";
std::vector<Glib::ustring> tokens = Glib::Regex::split_simple("[,]+", selector);
+ Glib::ustring my_selector = selector + " {"; // Parsing fails sometimes without '{'. Fix me
+ CRSelector *cr_selector = cr_selector_parse_from_buf((guchar *)my_selector.c_str(), CR_UTF_8);
for (auto token : tokens) {
- if (!selector_out.empty()) {
- selector_out += ",";
- }
REMOVE_SPACES(token);
std::vector<Glib::ustring> subtokens = Glib::Regex::split_simple("[ ]+", token);
for (auto subtoken : subtokens) {
@@ -264,11 +265,13 @@ Glib::ustring StyleDialog::fixCSSSelectors(Glib::ustring selector)
return "";
}
}
- selector_out = selector_out.empty() ? toadd : selector_out + " " + toadd;
}
}
}
- return selector_out;
+ if (cr_selector) {
+ return selector;
+ }
+ return "";
}
/**
@@ -610,11 +613,32 @@ void StyleDialog::readStyleElement()
continue;
}
}
- if (!obj && _current_selector != selector) {
+ if (!obj && _current_selector != "" && _current_selector != selector) {
_updating = false;
selectorpos++;
continue;
}
+ if (!obj) {
+ bool present = false;
+ for (auto objv : objVec) {
+ Glib::ustring id = objv->getId();
+ for (auto objsel : selection->objects()) {
+ Glib::ustring idsel = objsel->getId();
+ if (idsel == id) {
+ present = true;
+ break;
+ }
+ }
+ if (present) {
+ break;
+ }
+ }
+ if (!present) {
+ _updating = false;
+ selectorpos++;
+ continue;
+ }
+ }
Glib::ustring properties;
// Check to make sure we do have a value to match selector.
if ((i + 1) < tokens.size()) {
@@ -976,12 +1000,13 @@ void StyleDialog::_onLinkObj(Glib::ustring path, Glib::RefPtr<Gtk::TreeStore> st
void StyleDialog::_onPropDelete(Glib::ustring path, Glib::RefPtr<Gtk::TreeStore> store)
{
g_debug("StyleDialog::_onPropDelete");
-
Gtk::TreeModel::Row row = *store->get_iter(path);
if (row) {
Glib::ustring selector = row[_mColumns._colSelector];
row[_mColumns._colName] = "";
+ _deleted_pos = row[_mColumns._colSelectorPos];
store->erase(row);
+ _deletion = true;
_writeStyleElement(store, selector);
}
}
@@ -1058,6 +1083,7 @@ void StyleDialog::_writeStyleElement(Glib::RefPtr<Gtk::TreeStore> store, Glib::u
}
styleContent = "\n" + selector + " { \n";
}
+ selectorpos = _deleted_pos;
for (auto &row : store->children()) {
selector = row[_mColumns._colSelector];
selectorpos = row[_mColumns._colSelectorPos];
@@ -1110,22 +1136,30 @@ void StyleDialog::_writeStyleElement(Glib::RefPtr<Gtk::TreeStore> store, Glib::u
selectormatch = selectormatch + "[^}]*?}";
}
selectormatch = selectormatch + ")([^}]*?})((.|\n)*)";
+
Inkscape::XML::Node *textNode = _getStyleTextNode();
std::regex e(selectormatch.c_str());
std::string content = (textNode->content() ? textNode->content() : "");
std::string result;
std::regex_replace(std::back_inserter(result), content.begin(), content.end(), e, "$1" + styleContent + "$3");
+ bool empty = false;
+ if (result.empty()) {
+ empty = true;
+ result = "* > .inkscapehacktmp{}";
+ }
textNode->setContent(result.c_str());
INKSCAPE.readStyleSheets(true);
+ if (empty) {
+ textNode->setContent("");
+ }
}
_updating = false;
readStyleElement();
- /* SPDocument *document = SP_ACTIVE_DOCUMENT;
- for (auto iter : document->getObjectsBySelector(selector)) {
- std::cout << std::endl;
- iter->style->readFromObject(iter);
- iter->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
- } */
+ SPDocument *document = SP_ACTIVE_DOCUMENT;
+ for (auto iter : document->getObjectsBySelector(selector)) {
+ iter->style->readFromObject(iter);
+ iter->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
+ }
DocumentUndo::done(SP_ACTIVE_DOCUMENT, SP_VERB_DIALOG_STYLE, _("Edited style element."));
g_debug("StyleDialog::_writeStyleElement(): | %s |", styleContent.c_str());
@@ -1211,7 +1245,7 @@ void
StyleDialog::_startValueEdit(Gtk::CellEditable* cell, const Glib::ustring& path, Glib::RefPtr<Gtk::TreeStore> store)
{
g_debug("StyleDialog::_startValueEdit");
-
+ _deletion = false;
_scroollock = true;
Gtk::TreeModel::Row row = *store->get_iter(path);
if (row) {
@@ -1273,8 +1307,8 @@ StyleDialog::_startValueEdit(Gtk::CellEditable* cell, const Glib::ustring& path,
void StyleDialog::_startNameEdit(Gtk::CellEditable *cell, const Glib::ustring &path)
{
+ _deletion = false;
g_debug("StyleDialog::_startNameEdit");
-
_scroollock = true;
Glib::RefPtr<Gtk::ListStore> completionModel = Gtk::ListStore::create(_mCSSData);
Glib::RefPtr<Gtk::EntryCompletion> entry_completion = Gtk::EntryCompletion::create();
@@ -1297,11 +1331,13 @@ void StyleDialog::_startNameEdit(Gtk::CellEditable *cell, const Glib::ustring &p
gboolean sp_styledialog_store_move_to_next(gpointer data)
{
StyleDialog *styledialog = reinterpret_cast<StyleDialog *>(data);
- auto selection = styledialog->_current_css_tree->get_selection();
- Gtk::TreeIter iter = *(selection->get_selected());
- Gtk::TreeModel::Path model = (Gtk::TreeModel::Path)iter;
- if (model == styledialog->_current_path) {
- styledialog->_current_css_tree->set_cursor(styledialog->_current_path, *styledialog->_current_value_col, true);
+ if (!styledialog->_deletion) {
+ auto selection = styledialog->_current_css_tree->get_selection();
+ Gtk::TreeIter iter = *(selection->get_selected());
+ Gtk::TreeModel::Path model = (Gtk::TreeModel::Path)iter;
+ if (model == styledialog->_current_path) {
+ styledialog->_current_css_tree->set_cursor(styledialog->_current_path, *styledialog->_current_value_col, true);
+ }
}
return FALSE;
}
@@ -1339,6 +1375,7 @@ void StyleDialog::_nameEdited(const Glib::ustring &path, const Glib::ustring &na
Glib::ustring old_name = row[_mColumns._colName];
row[_mColumns._colName] = finalname;
if (finalname.empty() && value.empty()) {
+ _deleted_pos = row[_mColumns._colSelectorPos];
store->erase(row);
}
gint col = 3;
@@ -1348,6 +1385,21 @@ void StyleDialog::_nameEdited(const Glib::ustring &path, const Glib::ustring &na
_current_value_col = css_tree->get_column(col);
if (write && old_name != name) {
_writeStyleElement(store, selector);
+ /*
+ I think is better comment this, is enoght update on value change
+ if (selector != "style_properties" && selector != "attributes") {
+ std::vector<SPObject *> objs = _getObjVec(selector);
+ for (auto obj : objs){
+ Glib::ustring css_str = "";
+ SPCSSAttr *css = sp_repr_css_attr_new();
+ sp_repr_css_attr_add_from_string(css, obj->getRepr()->attribute("style"));
+ css->setAttribute(name.c_str(), nullptr);
+ sp_repr_css_write_string(css, css_str);
+ obj->getRepr()->setAttribute("style", css_str.c_str());
+ obj->style->readFromObject(obj);
+ obj->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
+ }
+ } */
} else {
g_timeout_add(50, &sp_styledialog_store_move_to_next, this);
grab_focus();
@@ -1383,9 +1435,23 @@ void StyleDialog::_valueEdited(const Glib::ustring &path, const Glib::ustring &v
Glib::ustring selector = row[_mColumns._colSelector];
Glib::ustring name = row[_mColumns._colName];
if (name.empty() && finalvalue.empty()) {
+ _deleted_pos = row[_mColumns._colSelectorPos];
store->erase(row);
}
_writeStyleElement(store, selector);
+ if (selector != "style_properties" && selector != "attributes") {
+ std::vector<SPObject *> objs = _getObjVec(selector);
+ for (auto obj : objs){
+ Glib::ustring css_str = "";
+ SPCSSAttr *css = sp_repr_css_attr_new();
+ sp_repr_css_attr_add_from_string(css, obj->getRepr()->attribute("style"));
+ css->setAttribute(name.c_str(), nullptr);
+ sp_repr_css_write_string(css, css_str);
+ obj->getRepr()->setAttribute("style", css_str.c_str());
+ obj->style->readFromObject(obj);
+ obj->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
+ }
+ }
}
}
diff --git a/src/ui/dialog/styledialog.h b/src/ui/dialog/styledialog.h
index 07dfd8a0e..a5a559733 100644
--- a/src/ui/dialog/styledialog.h
+++ b/src/ui/dialog/styledialog.h
@@ -70,6 +70,7 @@ class StyleDialog : public Widget::Panel {
Gtk::TreeView *_current_css_tree;
Gtk::TreeViewColumn *_current_value_col;
Gtk::TreeModel::Path _current_path;
+ bool _deletion;
Glib::ustring fixCSSSelectors(Glib::ustring selector);
void readStyleElement();
private:
@@ -117,7 +118,7 @@ class StyleDialog : public Widget::Panel {
Gtk::TreeModelColumn<Glib::ustring> _colCSSData; // Name of the property.
};
CSSData _mCSSData;
-
+ guint _deleted_pos;
// Widgets
Gtk::ScrolledWindow _scrolledWindow;
Glib::RefPtr<Gtk::Adjustment> _vadj;