Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
actions-hide-lock.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
15#include "actions-hide-lock.h"
16#include "actions-helper.h"
17
18#include <giomm.h> // Not <gtkmm.h>! To eventually allow a headless version!
19#include <glibmm/i18n.h>
20
22#include "document.h"
23#include "document-undo.h"
24#include "selection.h"
25
26#include "object/sp-root.h"
27
28// Helper to unlock/unhide everything. (Could also be used to lock/hide everything but that isn't very useful.)
29static bool
30hide_lock_recurse(bool (*f)(SPItem*, bool), SPItem *item, bool hide_or_lock)
31{
32 bool changed = false;
33
34 if (f(item, hide_or_lock)) {
35 changed = true;
36 }
37
38 for (auto& child : item->children) {
39 auto item = cast<SPItem>(&child);
40 if (item && hide_lock_recurse(f, item, hide_or_lock)) {
41 changed = true;
42 }
43 }
44
45 return changed;
46}
47
48// Helper to hide/unhide one item.
49bool
51{
52 bool changed = false;
53 if (item->isHidden() != hide) {
54 item->setHidden(hide);
55 changed = true;
56 }
57 return changed;
58}
59
60// Helper to lock/unlock one item.
61bool
63{
64 bool changed = false;
65 if (item->isLocked() != lock) {
66 item->setLocked(lock);
67 changed = true;
68 }
69 return changed;
70}
71
72// Unhide all
73void
75{
76 auto document = app->get_active_document();
77 auto root = document->getRoot();
78
79 bool changed = hide_lock_recurse(&hide_lock_hide, root, false); // Unhide
80
81 if (changed) {
82 Inkscape::DocumentUndo::done(document, _("Unhid all objects in the current layer"), "");
83 }
84}
85
86// Unlock all
87void
89{
90 auto document = app->get_active_document();
91 auto root = document->getRoot();
92
93 bool changed = hide_lock_recurse(&hide_lock_lock, root, false); // Unlock
94
95 if (changed) {
96 Inkscape::DocumentUndo::done(document, _("Unlocked all objects in the current layer"), "");
97 }
98}
99
100// Unhide selected items and their descendents.
101void
103{
104 auto selection = app->get_active_selection();
105 if (!selection) {
106 show_output("hide_lock_unhide_below: no selection!");
107 return;
108 }
109
110 bool changed = false;
111 for (auto item : selection->items()) {
112 if (hide_lock_recurse(&hide_lock_hide, item, false)) {
113 changed = true;
114 }
115 }
116
117 if (changed) {
118 auto document = app->get_active_document();
119 Inkscape::DocumentUndo::done(document, _("Unhid selected items and their descendents."), "");
120 }
121}
122
123// Unlock selected items and their descendents.
124void
126{
127 auto selection = app->get_active_selection();
128 if (!selection) {
129 show_output("hide_lock_unhide_below: no selection!");
130 return;
131 }
132
133 bool changed = false;
134 for (auto item : selection->items()) {
135 if (hide_lock_recurse(&hide_lock_lock, item, false)) {
136 changed = true;
137 }
138 }
139
140 if (changed) {
141 auto document = app->get_active_document();
142 Inkscape::DocumentUndo::done(document, _("Unlocked selected items and their descendents."), "");
143 }
144}
145
146// Hide/unhide selected items.
147void
149{
150 auto selection = app->get_active_selection();
151 if (!selection) {
152 show_output("hide_lock_hide_selected: no selection!");
153 return;
154 }
155
156 bool changed = false;
157 for (auto item : selection->items()) {
158 if (hide_lock_hide(item, hide)) {
159 changed = true;
160 }
161 }
162
163 if (changed) {
164 auto document = app->get_active_document();
165 Inkscape::DocumentUndo::done(document, (hide ? _("Hid selected items.") : _("Unhid selected items.")), "");
166 selection->clear();
167 }
168}
169
170// Lock/Unlock selected items.
171void
173{
174 auto selection = app->get_active_selection();
175 if (!selection) {
176 show_output("hide_lock_lock_selected: no selection!");
177 return;
178 }
179
180 bool changed = false;
181 for (auto item : selection->items()) {
182 if (hide_lock_lock(item, lock)) {
183 changed = true;
184 }
185 }
186
187 if (changed) {
188 auto document = app->get_active_document();
189 Inkscape::DocumentUndo::done(document, (lock ? _("Locked selected items.") : _("Unlocked selected items.")), "");
190 selection->clear();
191 }
192}
193
194const Glib::ustring SECTION = NC_("Action Section", "Hide and Lock");
195
196std::vector<std::vector<Glib::ustring>> raw_data_hide_lock =
197{
198 // clang-format off
199 {"app.unhide-all", N_("Unhide All"), SECTION, N_("Unhide all objects") },
200 {"app.unlock-all", N_("Unlock All"), SECTION, N_("Unlock all objects") },
201
202 {"app.selection-hide", N_("Hide selection"), SECTION, N_("Hide all selected objects") },
203 {"app.selection-unhide", N_("Unhide selection"), SECTION, N_("Unhide all selected objects") },
204 {"app.selection-unhide-below", N_("Unhide descendents"), SECTION, N_("Unhide all items inside selected objects") },
205
206 {"app.selection-lock", N_("Lock selection"), SECTION, N_("Lock all selected objects") },
207 {"app.selection-unlock", N_("Unlock selection"), SECTION, N_("Unlock all selected objects") },
208 {"app.selection-unlock-below", N_("Unlock descendents"), SECTION, N_("Unlock all items inside selected objects") },
209 // clang-format on
210};
211
212void
214{
215 auto *gapp = app->gio_app();
216
217 // clang-format off
218 gapp->add_action( "unhide-all", sigc::bind( sigc::ptr_fun(&hide_lock_unhide_all), app));
219 gapp->add_action( "unlock-all", sigc::bind( sigc::ptr_fun(&hide_lock_unlock_all), app));
220
221 gapp->add_action( "selection-hide", sigc::bind(sigc::ptr_fun(&hide_lock_hide_selected), app, true ));
222 gapp->add_action( "selection-unhide", sigc::bind(sigc::ptr_fun(&hide_lock_hide_selected), app, false));
223 gapp->add_action( "selection-unhide-below", sigc::bind( sigc::ptr_fun(&hide_lock_unhide_below), app));
224
225 gapp->add_action( "selection-lock", sigc::bind(sigc::ptr_fun(&hide_lock_lock_selected), app, true ));
226 gapp->add_action( "selection-unlock", sigc::bind(sigc::ptr_fun(&hide_lock_lock_selected), app, false));
227 gapp->add_action( "selection-unlock-below", sigc::bind( sigc::ptr_fun(&hide_lock_unlock_below), app));
228 // clang-format on
229
231}
232
233/*
234 Local Variables:
235 mode:c++
236 c-file-style:"stroustrup"
237 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
238 indent-tabs-mode:nil
239 fill-column:99
240 End:
241*/
242// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
void show_output(Glib::ustring const &data, bool const is_cerr)
bool hide_lock_hide(SPItem *item, bool hide)
const Glib::ustring SECTION
void hide_lock_unhide_below(InkscapeApplication *app)
std::vector< std::vector< Glib::ustring > > raw_data_hide_lock
bool hide_lock_lock(SPItem *item, bool lock)
void hide_lock_unlock_all(InkscapeApplication *app)
void hide_lock_lock_selected(InkscapeApplication *app, bool lock)
void hide_lock_hide_selected(InkscapeApplication *app, bool hide)
void hide_lock_unlock_below(InkscapeApplication *app)
static bool hide_lock_recurse(bool(*f)(SPItem *, bool), SPItem *item, bool hide_or_lock)
void add_actions_hide_lock(InkscapeApplication *app)
void hide_lock_unhide_all(InkscapeApplication *app)
Authors: Sushant A A sushant.co19@gmail.com
void add_data(std::vector< std::vector< Glib::ustring > > const &raw_data)
InkActionExtraData & get_action_extra_data()
Gio::Application * gio_app()
The Gio application instance, never NULL.
SPDocument * get_active_document()
Inkscape::Selection * get_active_selection()
static void done(SPDocument *document, Glib::ustring const &event_description, Glib::ustring const &undo_icon, unsigned int object_modified_tag=0)
SPRoot * getRoot()
Returns our SPRoot.
Definition document.h:200
Base class for visual SVG elements.
Definition sp-item.h:109
bool isHidden() const
Definition sp-item.cpp:242
void setLocked(bool lock, bool recursive=false)
Definition sp-item.cpp:228
bool isLocked() const
Definition sp-item.cpp:218
void setHidden(bool hidden)
Definition sp-item.cpp:248
ChildrenList children
Definition sp-object.h:907
RootCluster root
TODO: insert short description here.
SPItem * item
Ocnode * child[8]
Definition quantize.cpp:33
SPRoot: SVG <svg> implementation.