Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
actions-canvas-mode.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Gio::Actions for changing the canvas display mode. Tied to a particular InkscapeWindow.
4 *
5 * Copyright (C) 2020 Tavmjong Bah
6 *
7 * The contents of this file may be used under the GNU General Public License Version 2 or later.
8 *
9 */
10
11#include "actions-canvas-mode.h"
12
13#include <iostream>
14
15#include <giomm.h> // Not <gtkmm.h>! To eventually allow a headless version!
16#include <glibmm/i18n.h>
17
18#include "ui/interface.h"
19
20#include "actions-helper.h"
21
22#include "desktop.h"
24#include "inkscape-window.h"
25
26#include "display/rendermode.h"
27#include "display/drawing.h" // Setting gray scale parameters.
29
30#include "ui/widget/canvas.h"
31
32// TODO: Use action state rather than set variable in Canvas (via Desktop).
33// TODO: Move functions from Desktop to Canvas.
34// TODO: Canvas actions should belong to canvas (not window)!
35
39void
40canvas_set_display_mode(Inkscape::RenderMode value, InkscapeWindow *win, Glib::RefPtr<Gio::SimpleAction> saction)
41{
42 g_assert(value != Inkscape::RenderMode::size);
43 saction->change_state((int)value);
44
45 // Save value as a preference
46 auto pref = Inkscape::Preferences::get();
47 pref->setInt("/options/displaymode", (int)value);
48
50}
51
55void
57{
58 if (value < 0 || value >= (int)Inkscape::RenderMode::size) {
59 show_output(Glib::ustring("canvas_display_mode: value out of bound! : ") + Glib::ustring::format(value));
60 return;
61 }
62
63 auto action = win->lookup_action("canvas-display-mode");
64 if (!action) {
65 show_output("canvas_display_mode: action 'canvas-display-mode' missing!");
66 return;
67 }
68
69 auto saction = std::dynamic_pointer_cast<Gio::SimpleAction>(action);
70 if (!saction) {
71 show_output("canvas_display_mode: action 'canvas-display-mode' not SimpleAction!");
72 return;
73 }
74
76}
77
81void
83{
84 auto action = win->lookup_action("canvas-display-mode");
85 if (!action) {
86 show_output("canvas_display_mode_cycle: action 'canvas-display-mode' missing!");
87 return;
88 }
89
90 auto saction = std::dynamic_pointer_cast<Gio::SimpleAction>(action);
91 if (!saction) {
92 show_output("canvas_display_mode_cycle: action 'canvas-display-mode' not SimpleAction!");
93 return;
94 }
95
96 int value = -1;
97 saction->get_state(value);
98 // TODO: match order of UI instead
99 value++;
100 value %= (int)Inkscape::RenderMode::size;
101
102 saction->activate_variant(Glib::Variant<int>::create(value));
103}
104
105
109void
111{
112 auto action = win->lookup_action("canvas-display-mode");
113 if (!action) {
114 show_output("canvas_display_mode_toggle: action 'canvas-display-mode' missing!");
115 return;
116 }
117
118 auto saction = std::dynamic_pointer_cast<Gio::SimpleAction>(action);
119 if (!saction) {
120 show_output("canvas_display_mode_toogle: action 'canvas-display-mode' not SimpleAction!");
121 return;
122 }
123
125
126 int value = -1;
127 saction->get_state(value);
128 int new_value = 0;
129 const int normal = static_cast<int>(Inkscape::RenderMode::NORMAL);
130
131 if (value == normal) {
132 new_value = static_cast<int>(old_value);
133 } else {
134 old_value = Inkscape::RenderMode(value);
135 new_value = normal;
136 }
137 saction->activate_variant(Glib::Variant<int>::create(new_value));
138}
139
143void
145{
146 if (value < 0 || value >= (int)Inkscape::SplitMode::size) {
147 show_output("canvas_split_mode: value out of bound! : " + Glib::ustring::format(value));
148 return;
149 }
150
151 auto action = win->lookup_action("canvas-split-mode");
152 if (!action) {
153 show_output("canvas_split_mode: action 'canvas-split-mode' missing!");
154 return;
155 }
156
157 auto saction = std::dynamic_pointer_cast<Gio::SimpleAction>(action);
158 if (!saction) {
159 show_output("canvas_split_mode: action 'canvas-split-mode' not SimpleAction!");
160 return;
161 }
162
163 // If split mode is already set to the requested mode, turn it off.
164 int old_value = -1;
165 saction->get_state(old_value);
166 if (value == old_value) {
167 value = (int)Inkscape::SplitMode::NORMAL;
168 }
169
170 saction->change_state(value);
171
172 SPDesktop* dt = win->get_desktop();
173 auto canvas = dt->getCanvas();
174 canvas->set_split_mode(Inkscape::SplitMode(value));
175}
176
180void
182{
184 gdouble r = prefs->getDoubleLimited("/options/rendering/grayscale/red-factor", 0.21, 0.0, 1.0);
185 gdouble g = prefs->getDoubleLimited("/options/rendering/grayscale/green-factor", 0.72, 0.0, 1.0);
186 gdouble b = prefs->getDoubleLimited("/options/rendering/grayscale/blue-factor", 0.072, 0.0, 1.0);
187 gdouble grayscale_value_matrix[20] =
188 { r, g, b, 0, 0,
189 r, g, b, 0, 0,
190 r, g, b, 0, 0,
191 0, 0, 0, 1, 0 };
192 SPDesktop* dt = win->get_desktop();
193 dt->getCanvasDrawing()->get_drawing()->setGrayscaleMatrix(grayscale_value_matrix);
194}
195
199void
201{
202 auto action = win->lookup_action("canvas-color-mode");
203 if (!action) {
204 show_output("canvas_color_mode_toggle: action missing!");
205 return;
206 }
207
208 auto saction = std::dynamic_pointer_cast<Gio::SimpleAction>(action);
209 if (!saction) {
210 show_output("canvas_color_mode_toggle: action not SimpleAction!");
211 return;
212 }
213
214 bool state = false;
215 saction->get_state(state);
216 state = !state;
217 saction->change_state(state);
218
219 if (state) {
220 // Set gray scale parameters.
222 }
223
225}
226
230void
232{
233 auto action = win->lookup_action("canvas-color-manage");
234 if (!action) {
235 show_output("canvas_color_manage_toggle: action missing!");
236 return;
237 }
238
239 auto saction = std::dynamic_pointer_cast<Gio::SimpleAction>(action);
240 if (!saction) {
241 show_output("canvas_color_manage_toggle: action not SimpleAction!");
242 return;
243 }
244
245 bool state = false;
246 saction->get_state(state);
247 state = !state;
248 saction->change_state(state);
249
250 // Save value as a preference
252 pref->setBool("/options/displayprofile/enable", state);
253
254 SPDesktop* dt = win->get_desktop();
255 auto canvas = dt->getCanvas();
256 canvas->set_cms_active(state);
257 canvas->redraw_all();
258}
259
260const Glib::ustring SECTION = NC_("Action Section", "Canvas Display");
261
262std::vector<std::vector<Glib::ustring>> raw_data_canvas_mode =
263{
264 // clang-format off
265 {"win.canvas-display-mode(0)", N_("Display Mode: Normal"), SECTION, N_("Use normal rendering mode") },
266 {"win.canvas-display-mode(1)", N_("Display Mode: Outline"), SECTION, N_("Show only object outlines") },
267 {"win.canvas-display-mode(2)", N_("Display Mode: No Filters"), SECTION, N_("Do not render filters (for speed)") },
268 {"win.canvas-display-mode(3)", N_("Display Mode: Enhance Thin Lines"), SECTION, N_("Ensure all strokes are displayed on screen as at least 1 pixel wide")},
269 {"win.canvas-display-mode(4)", N_("Display Mode: Outline Overlay"), SECTION, N_("Show objects as outlines, and the actual drawing below them with reduced opacity")},
270 {"win.canvas-display-mode-cycle", N_("Display Mode: Cycle"), SECTION, N_("Cycle through display modes") },
271 {"win.canvas-display-mode-toggle", N_("Display Mode: Toggle"), SECTION, N_("Toggle between normal and last non-normal mode")},
272 {"win.canvas-display-mode-toggle-preview", N_("Display Mode: Toggle Preview"), SECTION, N_("Toggle between preview and previous mode") },
273
274 {"win.canvas-split-mode(0)", N_("Split Mode: Normal"), SECTION, N_("Do not split canvas") },
275 {"win.canvas-split-mode(1)", N_("Split Mode: Split"), SECTION, N_("Render part of the canvas in outline mode") },
276 {"win.canvas-split-mode(2)", N_("Split Mode: X-Ray"), SECTION, N_("Render a circular area in outline mode") },
277
278 {"win.canvas-color-mode", N_("Color Mode"), SECTION, N_("Toggle between normal and grayscale modes") },
279 {"win.canvas-color-manage", N_("Color Managed Mode"), SECTION, N_("Toggle between normal and color managed modes") }
280 // clang-format on
281};
282
283void
285{
286 // Sync action with desktop variables. TODO: Remove!
287 auto prefs = Inkscape::Preferences::get();
288
289 // Initial States of Actions
290 int display_mode = prefs->getIntLimited("/options/displaymode", 0, 0, static_cast<int>(Inkscape::RenderMode::size) - 1); // Default, minimum, maximum
291 bool color_manage = prefs->getBool("/options/displayprofile/enable");
292
293 // clang-format off
294 win->add_action_radio_integer ("canvas-display-mode", sigc::bind(sigc::ptr_fun(&canvas_display_mode), win), display_mode);
295 win->add_action( "canvas-display-mode-cycle", sigc::bind(sigc::ptr_fun(&canvas_display_mode_cycle), win));
296 win->add_action( "canvas-display-mode-toggle", sigc::bind(sigc::ptr_fun(&canvas_display_mode_toggle), win));
297 win->add_action_radio_integer ("canvas-split-mode", sigc::bind(sigc::ptr_fun(&canvas_split_mode), win), (int)Inkscape::SplitMode::NORMAL);
298 win->add_action_bool( "canvas-color-mode", sigc::bind(sigc::ptr_fun(&canvas_color_mode_toggle), win));
299 win->add_action_bool( "canvas-color-manage", sigc::bind(sigc::ptr_fun(&canvas_color_manage_toggle), win), color_manage);
300 // clang-format on
301
303 if (!app) {
304 show_output("add_actions_canvas_mode: no app!");
305 return;
306 }
307 app->get_action_extra_data().add_data(raw_data_canvas_mode);
308}
309
311{
312 // Sync action with desktop variables. TODO: Remove!
313 auto prefs = Inkscape::Preferences::get();
314
315 // Initial States of Actions
316 int display_mode = prefs->getIntLimited(
317 "/options/displaymode", 0, 0, static_cast<int>(Inkscape::RenderMode::size) - 1); // Default, minimum, maximum
318 bool color_manage = prefs->getBool("/options/displayprofile/enable");
319
320 dt->setRenderMode(Inkscape::RenderMode(display_mode));
321 dt->getCanvas()->set_cms_active(color_manage);
322}
323
324/*
325 Local Variables:
326 mode:c++
327 c-file-style:"stroustrup"
328 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
329 indent-tabs-mode:nil
330 fill-column:99
331 End:
332*/
333// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :
void canvas_set_display_mode(Inkscape::RenderMode value, InkscapeWindow *win, Glib::RefPtr< Gio::SimpleAction > saction)
Helper function to set display mode.
void canvas_color_mode_toggle(InkscapeWindow *win)
Toggle Gray scale on/off.
const Glib::ustring SECTION
void canvas_color_mode_gray(InkscapeWindow *win)
Set gray scale for canvas.
void canvas_display_mode_cycle(InkscapeWindow *win)
Cycle between values.
void canvas_split_mode(int value, InkscapeWindow *win)
Set split mode.
std::vector< std::vector< Glib::ustring > > raw_data_canvas_mode
void add_actions_canvas_mode(InkscapeWindow *win)
void apply_preferences_canvas_mode(SPDesktop *dt)
void canvas_color_manage_toggle(InkscapeWindow *win)
Toggle Color management on/off.
void canvas_display_mode_toggle(InkscapeWindow *win)
Toggle between normal and last set other value.
void canvas_display_mode(int value, InkscapeWindow *win)
Set display mode.
void show_output(Glib::ustring const &data, bool const is_cerr)
Inkscape canvas widget.
static InkscapeApplication * instance()
Singleton instance.
SPDesktop * get_desktop()
Inkscape::Drawing * get_drawing()
void setGrayscaleMatrix(double[20])
Definition drawing.cpp:107
Preference storage class.
Definition preferences.h:66
static Preferences * get()
Access the singleton Preferences object.
void setBool(Glib::ustring const &pref_path, bool value)
Set a Boolean value.
double getDoubleLimited(Glib::ustring const &pref_path, double def=0.0, double min=DBL_MIN, double max=DBL_MAX, Glib::ustring const &unit="")
Retrieve a limited floating point value.
void set_cms_active(bool active)
Definition canvas.h:105
void set_split_mode(Inkscape::SplitMode mode)
Definition canvas.cpp:1771
To do: update description of desktop.
Definition desktop.h:149
Inkscape::UI::Widget::Canvas * getCanvas() const
Definition desktop.h:190
void setRenderMode(Inkscape::RenderMode mode)
Definition desktop.cpp:1002
Inkscape::CanvasItemDrawing * getCanvasDrawing() const
Definition desktop.h:204
void setColorMode(Inkscape::ColorMode mode)
Definition desktop.cpp:1010
Editable view implementation.
SVG drawing for display.
Inkscape - An SVG editor.
TODO: insert short description here.