summaryrefslogtreecommitdiffstats
path: root/src/actions/actions-hide-lock.cpp
blob: 01ccef9d8b20a91dd5a0c868029b859558bcd8dc (plain)
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
// SPDX-License-Identifier: GPL-2.0-or-later
/** \file
 *
 * Actions related to hide and lock
 *
 * Authors:
 *   Sushant A A <sushant.co19@gmail.com>
 *   Tavmjong Bah
 *
 * Copyright (C) 2021-2022 Authors
 *
 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
 */

#include "actions-hide-lock.h"

#include <giomm.h>  // Not <gtkmm.h>! To eventually allow a headless version!
#include <glibmm/i18n.h>

#include "inkscape-application.h"
#include "document-undo.h"

#include "object/sp-root.h"

// Helper to unlock/unhide everything. (Could also be used to lock/hide everything but that isn't very useful.)
static bool
hide_lock_recurse(bool (*f)(SPItem*, bool), SPItem *item, bool hide_or_lock)
{
    bool changed = false;

    if (f(item, hide_or_lock)) {
        changed = true;
    }

    for (auto& child : item->children) {
        auto item = dynamic_cast<SPItem*>(&child);
        if (item && hide_lock_recurse(f, item, hide_or_lock)) {
            changed = true;
        }
    }

    return changed;
}

// Helper to hide/unhide one item.
bool
hide_lock_hide(SPItem* item, bool hide)
{
    bool changed = false;
    if (item->isHidden() != hide) {
        item->setHidden(hide);
        changed = true;
    }
    return changed;
}

// Helper to lock/unlock one item.
bool
hide_lock_lock(SPItem* item, bool lock)
{
    bool changed = false;
    if (item->isLocked() != lock) {
        item->setLocked(lock);
        changed = true;
    }
    return changed;
}

// Unhide all
void
hide_lock_unhide_all(InkscapeApplication* app)
{
    auto document = app->get_active_document();
    auto root = document->getRoot();

    bool changed = hide_lock_recurse(&hide_lock_hide, root, false); // Unhide

    if (changed) {
        Inkscape::DocumentUndo::done(document, _("Unhid all objects in the current layer"), "");
    }
}

// Unlock all
void
hide_lock_unlock_all(InkscapeApplication* app)
{
    auto document = app->get_active_document();
    auto root = document->getRoot();

    bool changed = hide_lock_recurse(&hide_lock_lock, root, false); // Unlock

    if (changed) {
        Inkscape::DocumentUndo::done(document, _("Unlocked all objects in the current layer"), "");
    }
}

// Unhide selected items and their descendents.
void
hide_lock_unhide_below(InkscapeApplication *app)
{
    auto selection = app->get_active_selection();
    if (!selection) {
        std::cerr << "hide_lock_unhide_below: no selection!" << std::endl;
        return;
    }

    bool changed = false;
    for (auto item : selection->items()) {
        if (hide_lock_recurse(&hide_lock_hide, item, false)) {
            changed = true;
        }
    }

    if (changed) {
        auto document = app->get_active_document();
        Inkscape::DocumentUndo::done(document, _("Unhid selected items and their descendents."), "");
    }
}

// Unlock selected items and their descendents.
void
hide_lock_unlock_below(InkscapeApplication *app)
{
    auto selection = app->get_active_selection();
    if (!selection) {
        std::cerr << "hide_lock_unhide_below: no selection!" << std::endl;
        return;
    }

    bool changed = false;
    for (auto item : selection->items()) {
        if (hide_lock_recurse(&hide_lock_lock, item, false)) {
            changed = true;
        }
    }

    if (changed) {
        auto document = app->get_active_document();
        Inkscape::DocumentUndo::done(document, _("Unlocked selected items and their descendents."), "");
    }
}

// Hide/unhide selected items.
void
hide_lock_hide_selected(InkscapeApplication* app, bool hide)
{
    auto selection = app->get_active_selection();
    if (!selection) {
        std::cerr << "hide_lock_hide_selected: no selection!" << std::endl;
        return;
    }

    bool changed = false;
    for (auto item : selection->items()) {
        if (hide_lock_hide(item, hide)) {
            changed = true;
        }
    }

    if (changed) {
        auto document = app->get_active_document();
        Inkscape::DocumentUndo::done(document, (hide ? _("Hid selected items.") : _("Unhid selected items.")), "");
        selection->clear();
    }
}

// Lock/Unlock selected items.
void
hide_lock_lock_selected(InkscapeApplication* app, bool lock)
{
    auto selection = app->get_active_selection();
    if (!selection) {
        std::cerr << "hide_lock_lock_selected: no selection!" << std::endl;
        return;
    }

    bool changed = false;
    for (auto item : selection->items()) {
        if (hide_lock_lock(item, lock)) {
            changed = true;
        }
    }

    if (changed) {
        auto document = app->get_active_document();
        Inkscape::DocumentUndo::done(document, (lock ? _("Locked selected items.") : _("Unlocked selected items.")), "");
        selection->clear();
    }
}

std::vector<std::vector<Glib::ustring>> raw_data_hide_lock =
{
    // clang-format off
    {"app.unhide-all",              N_("Unhide All"),         "Hide and Lock",      N_("Unhide all objects")      },
    {"app.unlock-all",              N_("Unlock All"),         "Hide and Lock",      N_("Unlock all objects")      },

    {"app.selection-hide",          N_("Hide selection"),     "Hide and Lock",      N_("Hide all selected objects")                    },
    {"app.selection-unhide",        N_("Unhide selection"),   "Hide and Lock",      N_("Unhide all selected objects")                  },
    {"app.selection-unhide-below",  N_("Unhide descendents"), "Hide and Lock",      N_("Unhide all items inside selected objects")     },

    {"app.selection-lock",          N_("Lock selection"),     "Hide and Lock",      N_("Lock all selected objects")                    },
    {"app.selection-unlock",        N_("Unlock selection"),   "Hide and Lock",      N_("Unlock all selected objects")                  },
    {"app.selection-unlock-below",  N_("Unlock descendents"), "Hide and Lock",      N_("Unlock all items inside selected objects")     },
    // clang-format on
};

void
add_actions_hide_lock(InkscapeApplication* app)
{
    auto *gapp = app->gio_app();

    // clang-format off
    gapp->add_action( "unhide-all",             sigc::bind<InkscapeApplication*>(      sigc::ptr_fun(&hide_lock_unhide_all),    app));
    gapp->add_action( "unlock-all",             sigc::bind<InkscapeApplication*>(      sigc::ptr_fun(&hide_lock_unlock_all),    app));

    gapp->add_action( "selection-hide",         sigc::bind<InkscapeApplication*, bool>(sigc::ptr_fun(&hide_lock_hide_selected), app, true ));
    gapp->add_action( "selection-unhide",       sigc::bind<InkscapeApplication*, bool>(sigc::ptr_fun(&hide_lock_hide_selected), app, false));
    gapp->add_action( "selection-unhide-below", sigc::bind<InkscapeApplication*>(      sigc::ptr_fun(&hide_lock_unhide_below),  app));

    gapp->add_action( "selection-lock",         sigc::bind<InkscapeApplication*, bool>(sigc::ptr_fun(&hide_lock_lock_selected), app, true ));
    gapp->add_action( "selection-unlock",       sigc::bind<InkscapeApplication*, bool>(sigc::ptr_fun(&hide_lock_lock_selected), app, false));
    gapp->add_action( "selection-unlock-below", sigc::bind<InkscapeApplication*>(      sigc::ptr_fun(&hide_lock_unlock_below),  app));
    // clang-format on

    app->get_action_extra_data().add_data(raw_data_hide_lock);
}

/*
  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 :