Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
sp-attribute-widget.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
6/* Authors:
7 * Lauris Kaplinski <lauris@ximian.com>
8 * Abhishek Sharma
9 * Kris De Gussem <Kris.DeGussem@gmail.com>
10 *
11 * Copyright (C) 2001 Ximian, Inc.
12 * Copyright (C) 2002,2011-2012 authors
13 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
14 */
15
16#include "sp-attribute-widget.h"
17
18#include <glibmm/i18n.h>
19#include <gtkmm/entry.h>
20#include <gtkmm/enums.h>
21#include <gtkmm/grid.h>
22#include <gtkmm/object.h>
23#include <gtkmm/scrolledwindow.h>
24#include <gtkmm/textview.h>
25#include <gtkmm/label.h>
26#include <sigc++/adaptors/bind.h>
27
28#include "document-undo.h"
29#include "object/sp-object.h"
30#include "preferences.h"
31#include "ui/syntax.h"
32#include "xml/node.h"
33
36
37void SPAttributeTable::EntryWidget::set_text(const Glib::ustring& text) {
38 if (_entry) {
39 _entry->set_text(text);
40 }
41 else {
42 _text_view->get_buffer()->set_text(text);
43 }
44}
45
47 return _entry ? _entry->get_text() : _text_view->get_buffer()->get_text();
48}
49
51 return _entry ? static_cast<Gtk::Widget*>(_entry) : static_cast<Gtk::Widget*>(_text_view);
52}
53
54static constexpr int XPAD = 4;
55static constexpr int YPAD = 2;
56
57SPAttributeTable::SPAttributeTable(std::vector<Glib::ustring> const &labels,
58 std::vector<Glib::ustring> const &attributes)
59{
60 create(labels, attributes);
61}
62
63void SPAttributeTable::create(const std::vector<Glib::ustring>& labels, const std::vector<Glib::ustring>& attributes) {
64 // build rows
65 _attributes = attributes;
66 _entries.clear();
67 _textviews.clear();
68 _entries.reserve(attributes.size());
69
70 table = std::make_unique<Gtk::Grid>();
71 append(*table);
72
73 auto theme = Inkscape::Preferences::get()->getString("/theme/syntax-color-theme", "-none-");
74
75 for (std::size_t i = 0; i < attributes.size(); ++i) {
76 auto const ll = Gtk::make_managed<Gtk::Label>(_(labels[i].c_str()));
77 ll->set_halign(Gtk::Align::START);
78 ll->set_valign(Gtk::Align::CENTER);
79 ll->set_vexpand(false);
80 ll->set_margin_end(XPAD);
81 ll->set_margin_top(YPAD);
82 ll->set_margin_bottom(YPAD);
83 table->attach(*ll, 0, i, 1, 1);
84
85 EntryWidget entry;
86 if (_syntax != SyntaxMode::PlainText) {
88 edit->setStyle(theme);
89 auto& tv = edit->getTextView();
90 entry._text_view = &tv;
91 tv.set_wrap_mode(Gtk::WrapMode::WORD);
92 auto wnd = Gtk::make_managed<Gtk::ScrolledWindow>();
93 wnd->set_hexpand();
94 wnd->set_vexpand(false);
95 wnd->set_margin_start(XPAD);
96 wnd->set_margin_top(YPAD);
97 wnd->set_margin_bottom(YPAD);
98 wnd->set_child(tv);
99 wnd->set_has_frame(true);
100 wnd->set_policy(Gtk::PolicyType::NEVER, Gtk::PolicyType::AUTOMATIC);
101 table->attach(*wnd, 1, i, 1, 1);
102
103 tv.get_buffer()->signal_end_user_action().connect([i, this](){
105 });
106 _textviews.push_back(std::move(edit));
107 }
108 else {
109 auto const ee = Gtk::make_managed<Gtk::Entry>();
110 entry._entry = ee;
111 ee->set_hexpand();
112 ee->set_vexpand(false);
113 ee->set_margin_start(XPAD);
114 ee->set_margin_top(YPAD);
115 ee->set_margin_bottom(YPAD);
116 table->attach(*ee, 1, i, 1, 1);
117
118 ee->signal_changed().connect([i, this](){
120 });
121 }
122
123 _entries.push_back(std::move(entry));
124 }
125}
126
128{
129 if (_object == object) return;
130
131 if (_object) {
132 modified_connection.disconnect();
133 release_connection.disconnect();
134 _object = nullptr;
135 }
136
137 _object = object;
138
139 blocked = true;
140
141 if (object) {
142 // Set up object
143 modified_connection = object->connectModified([this](SPObject* object, unsigned int flags){
144 attribute_table_object_modified(object, flags);
145 });
146 release_connection = object->connectRelease([this](SPObject* object){
148 });
149 }
150
151 for (std::size_t i = 0; i < _attributes.size(); ++i) {
152 auto const val = object ? object->getRepr()->attribute(_attributes[i].c_str()) : nullptr;
153 _entries[i].set_text(val ? val : "");
154 }
155
156 blocked = false;
157}
158
160{
161 blocked = true;
162 for (std::size_t i = 0; i < _attributes.size(); ++i) {
163 auto const val = _object->getRepr()->attribute(_attributes[i].c_str());
164 _entries.at(i).set_text(val ? val : "");
165 }
166 blocked = false;
167}
168
176void SPAttributeTable::attribute_table_object_modified(SPObject */*object*/, unsigned const flags) {
177 if (!(flags & SP_OBJECT_MODIFIED_FLAG)) return;
178
179 for (std::size_t i = 0; i < _attributes.size(); ++i) {
180 auto& e = _entries.at(i);
181 auto const val = _object->getRepr()->attribute(_attributes[i].c_str());
182 auto const new_text = Glib::ustring{val ? val : ""};
183 if (e.get_text() != new_text) {
184 // We are different
185 blocked = true;
186 e.set_text(new_text);
187 blocked = false;
188 }
189 }
190}
191
200 if (blocked) return;
201
202 if (index >= _attributes.size() || index >= _entries.size()) {
203 g_warning("file %s: line %d: Entry signalled change, but there is no such entry", __FILE__, __LINE__);
204 return;
205 }
206
207 auto& e = _entries[index];
208 blocked = true;
209 if (_object) {
210 auto text = e.get_text();
212 DocumentUndo::done(_object->document, _("Set attribute"), "");
213 }
214 blocked = false;
215}
216
226
227/*
228 Local Variables:
229 mode:c++
230 c-file-style:"stroustrup"
231 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
232 indent-tabs-mode:nil
233 fill-column:99
234 End:
235*/
236// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
Glib::ustring getString(Glib::ustring const &pref_path, Glib::ustring const &def="")
Retrieve an UTF-8 string.
static Preferences * get()
Access the singleton Preferences object.
static std::unique_ptr< TextEditView > create(SyntaxMode mode)
Create a styled text view using the desired syntax highlighting mode.
Definition syntax.cpp:382
void setAttribute(Util::const_char_ptr key, Util::const_char_ptr value)
Change an attribute of this node.
Definition node.cpp:25
virtual char const * attribute(char const *key) const =0
Get the string representation of a node's attribute.
sigc::scoped_connection modified_connection
Sets the callback for a modification of the selection.
void change_object(SPObject *object)
Update values in entry boxes on change of object.
void attribute_table_object_release(SPObject *object)
Callback for the deletion of the selected object.
void create(const std::vector< Glib::ustring > &labels, const std::vector< Glib::ustring > &attributes)
SPObject * _object
Stores pointer to the selected object.
std::vector< std::unique_ptr< Inkscape::UI::Syntax::TextEditView > > _textviews
void attribute_table_object_modified(SPObject *object, unsigned flags)
Callback for a modification of the selected object (size, color, properties, etc.).
std::unique_ptr< Gtk::Grid > table
Container widget for the dynamically created child widgets (labels and entry boxes).
void reread_properties()
Reads the object attributes.
bool blocked
Indicates whether SPAttributeTable is processing callbacks and whether it should accept any updating.
std::vector< Glib::ustring > _attributes
List of attributes.
void attribute_table_entry_changed(size_t index)
Callback for user input in one of the entries.
Inkscape::UI::Syntax::SyntaxMode _syntax
SPAttributeTable(Inkscape::UI::Syntax::SyntaxMode mode=Inkscape::UI::Syntax::SyntaxMode::PlainText)
Constructor defaulting to no content.
std::vector< EntryWidget > _entries
List of pointers to the respective entry boxes.
sigc::scoped_connection release_connection
Sets the callback for the deletion of the selected object.
SPObject is an abstract base class of all of the document nodes at the SVG document level.
Definition sp-object.h:160
SPDocument * document
Definition sp-object.h:188
Inkscape::XML::Node * getRepr()
Returns the XML representation of tree.
sigc::connection connectModified(sigc::slot< void(SPObject *, unsigned int)> slot)
Connects to the modification notification signal.
Definition sp-object.h:705
static constexpr int XPAD
static constexpr int YPAD
TODO: insert short description here.
SyntaxMode
Syntax highlighting mode (language).
Definition syntax.h:98
Singleton class to access the preferences file in a convenient way.
void append(std::vector< T > &vec, std::vector< T > const &other)
Definition sanitize.cpp:55
Widget that listens and modifies repr attributes.
const Gtk::Widget * get_widget() const
void set_text(const Glib::ustring &text)
int index
std::unique_ptr< Toolbar >(* create)()
Definition toolbars.cpp:56
Interface for XML nodes.