Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
actions-extra-data.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Extra data associated with actions: Label, Section, Tooltip.
4 *
5 * Copyright (C) 2020 Tavmjong Bah
6 *
7 * Extra data is indexed by "detailed action names", that is an action
8 * with prefix and value (if statefull). For example:
9 * "win.canvas-display-mode(1)"
10 *
11 * The contents of this file may be used under the GNU General Public License Version 2 or later.
12 *
13 */
14
15#include "actions-extra-data.h"
16
17#include <glibmm/i18n.h>
18#include <glibmm/regex.h>
19
20std::vector<Glib::ustring> InkActionExtraData::get_actions()
21{
22 std::vector<Glib::ustring> action_names;
23 for (auto const &datum : data) {
24 action_names.emplace_back(datum.first);
25 }
26 return action_names;
27}
28
29Glib::ustring
31 bool const translated) const
32{
33 Glib::ustring value;
34 auto const search = data.find(action_name.raw());
35 if (search != data.end()) {
36 value = translated ? _(search->second.label.c_str())
37 : search->second.label;
38 }
39 return value;
40}
41
42// TODO: Section should be translatable, too
43Glib::ustring
45{
46 Glib::ustring value;
47 auto const search = data.find(action_name.raw());
48 if (search != data.end()) {
49 value = search->second.section;
50 }
51 return value;
52}
53
55 bool const translated ,
56 bool const expanded ) const
57{
58 Glib::ustring value;
59 auto const search = data.find(action_name.raw());
60 if (search != data.end()) {
61 if (expanded && strncmp(action_name.c_str(), "win:tool-switch('", 17)) {
62 value = translated ? ("<b>" + Glib::ustring(_(search->second.label.c_str())) + "</b>\n" +
63 Glib::ustring(_(search->second.tooltip.c_str())))
64 : (search->second.label + "\n" + search->second.tooltip);
65 } else {
66 value = translated ? _(search->second.tooltip.c_str()) : search->second.tooltip;
67 }
68 }
69 return value;
70}
71
72void InkActionExtraData::add_data(std::vector<std::vector<Glib::ustring>> const &raw_data)
73{
74 data.reserve(data.size() + raw_data.size());
75 for (auto const &raw : raw_data) {
76 data.emplace(raw[0], InkActionExtraDatum{ raw[1], raw[2], raw[3] });
77 }
78}
79
89bool InkActionExtraData::isSameContext(Glib::ustring const &action_one, Glib::ustring const &action_two) const
90{
91 if (action_one.empty() || action_two.empty())
92 return true;
93
94 std::vector<Glib::ustring> ones = Glib::Regex::split_simple("." , action_one);
95 std::vector<Glib::ustring> twos = Glib::Regex::split_simple("." , action_two);
96
97 // Only tool shortcuts have a context at the moment
98 if (ones.size() < 2 || ones[0] != "tool" || twos.size() < 2 || twos[0] != "tool") {
99 return true;
100 }
101 // The same tool means the same context, or if the tool is all tools (i.e. tool-base)
102 return ones[1] == twos[1] || ones[1] == "all" || twos[1] == "all";
103}
104
105/*
106 Local Variables:
107 mode:c++
108 c-file-style:"stroustrup"
109 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
110 indent-tabs-mode:nil
111 fill-column:99
112 End:
113*/
114// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :
const char * action_name
Glib::ustring get_label_for_action(Glib::ustring const &action_name, bool translated=true) const
void add_data(std::vector< std::vector< Glib::ustring > > const &raw_data)
bool isSameContext(Glib::ustring const &action_one, Glib::ustring const &action_two) const
Return true if the action/shortcut context is the same.
Glib::ustring get_section_for_action(Glib::ustring const &action_name) const
std::vector< Glib::ustring > get_actions()
std::unordered_map< std::string, InkActionExtraDatum > data
Glib::ustring get_tooltip_for_action(Glib::ustring const &action_name, bool translated=true, bool expanded=false) const