summaryrefslogtreecommitdiffstats
path: root/src/display/snap-indicator.cpp
blob: cb798e612d3e422179c5446aa45b02bf0e71337a (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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Provides a class that shows a temporary indicator on the canvas of where the snap was, and what kind of snap
 *
 * Authors:
 *   Johan Engelen
 *   Diederik van Lierop
 *
 * Copyright (C) Johan Engelen 2009 <j.b.c.engelen@utwente.nl>
 * Copyright (C) Diederik van Lierop 2010 - 2012 <mail@diedenrezi.nl>
 *
 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
 */

#include "display/snap-indicator.h"

#include "desktop.h"

#include "display/sodipodi-ctrl.h"
#include "display/sodipodi-ctrlrect.h"
#include "display/canvas-text.h"
#include "display/sp-canvas-util.h"
#include "knot.h"
#include "preferences.h"
#include <glibmm/i18n.h>
#include "ui/tools-switch.h"
#include "enums.h"

namespace Inkscape {
namespace Display {

SnapIndicator::SnapIndicator(SPDesktop * desktop)
    :   _snaptarget(nullptr),
        _snaptarget_tooltip(nullptr),
        _snaptarget_bbox(nullptr),
        _snapsource(nullptr),
        _snaptarget_is_presnap(false),
        _desktop(desktop)
{
}

SnapIndicator::~SnapIndicator()
{
    // remove item that might be present
    remove_snaptarget();
    remove_snapsource();
}

void
SnapIndicator::set_new_snaptarget(Inkscape::SnappedPoint const &p, bool pre_snap)
{
    remove_snaptarget(); //only display one snaptarget at a time

    g_assert(_desktop != nullptr);

    if (!p.getSnapped()) {
        return; // If we haven't snapped, then it is of no use to draw a snapindicator
    }

    if (p.getTarget() == SNAPTARGET_CONSTRAINT) {
        // This is not a real snap, although moving along the constraint did affect the mouse pointer's position.
        // Maybe we should only show a snap indicator when the user explicitly asked for a constraint by pressing ctrl?
        // We should not show a snap indicator when stretching a selection box, which is also constrained. That would be
        // too much information.
        return;
    }

    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
    bool value = prefs->getBool("/options/snapindicator/value", true);

    if (value) {
        // TRANSLATORS: undefined target for snapping
        gchar *target_name = _("UNDEFINED");
        switch (p.getTarget()) {
            case SNAPTARGET_UNDEFINED:
                target_name = _("UNDEFINED");
                g_warning("Snap target has not been specified");
                break;
            case SNAPTARGET_GRID:
                target_name = _("grid line");
                break;
            case SNAPTARGET_GRID_INTERSECTION:
                target_name = _("grid intersection");
                break;
            case SNAPTARGET_GRID_PERPENDICULAR:
                target_name = _("grid line (perpendicular)");
                break;
            case SNAPTARGET_GUIDE:
                target_name = _("guide");
                break;
            case SNAPTARGET_GUIDE_INTERSECTION:
                target_name = _("guide intersection");
                break;
            case SNAPTARGET_GUIDE_ORIGIN:
                target_name = _("guide origin");
                break;
            case SNAPTARGET_GUIDE_PERPENDICULAR:
                target_name = _("guide (perpendicular)");
                break;
            case SNAPTARGET_GRID_GUIDE_INTERSECTION:
                target_name = _("grid-guide intersection");
                break;
            case SNAPTARGET_NODE_CUSP:
                target_name = _("cusp node");
                break;
            case SNAPTARGET_NODE_SMOOTH:
                target_name = _("smooth node");
                break;
            case SNAPTARGET_PATH:
                target_name = _("path");
                break;
            case SNAPTARGET_PATH_PERPENDICULAR:
                target_name = _("path (perpendicular)");
                break;
            case SNAPTARGET_PATH_TANGENTIAL:
                target_name = _("path (tangential)");
                break;
            case SNAPTARGET_PATH_INTERSECTION:
                target_name = _("path intersection");
                break;
            case SNAPTARGET_PATH_GUIDE_INTERSECTION:
                target_name = _("guide-path intersection");
                break;
            case SNAPTARGET_PATH_CLIP:
                target_name = _("clip-path");
                break;
            case SNAPTARGET_PATH_MASK:
                target_name = _("mask-path");
                break;
            case SNAPTARGET_BBOX_CORNER:
                target_name = _("bounding box corner");
                break;
            case SNAPTARGET_BBOX_EDGE:
                target_name = _("bounding box side");
                break;
            case SNAPTARGET_PAGE_BORDER:
                target_name = _("page border");
                break;
            case SNAPTARGET_LINE_MIDPOINT:
                target_name = _("line midpoint");
                break;
            case SNAPTARGET_OBJECT_MIDPOINT:
                target_name = _("object midpoint");
                break;
            case SNAPTARGET_ROTATION_CENTER:
                target_name = _("object rotation center");
                break;
            case SNAPTARGET_BBOX_EDGE_MIDPOINT:
                target_name = _("bounding box side midpoint");
                break;
            case SNAPTARGET_BBOX_MIDPOINT:
                target_name = _("bounding box midpoint");
                break;
            case SNAPTARGET_PAGE_CORNER:
                target_name = _("page corner");
                break;
            case SNAPTARGET_ELLIPSE_QUADRANT_POINT:
                target_name = _("quadrant point");
                break;
            case SNAPTARGET_RECT_CORNER:
            case SNAPTARGET_IMG_CORNER:
                target_name = _("corner");
                break;
            case SNAPTARGET_TEXT_ANCHOR:
                target_name = _("text anchor");
                break;
            case SNAPTARGET_TEXT_BASELINE:
                target_name = _("text baseline");
                break;
            case SNAPTARGET_CONSTRAINED_ANGLE:
                target_name = _("constrained angle");
                break;
            case SNAPTARGET_CONSTRAINT:
                target_name = _("constraint");
                break;
            default:
                g_warning("Snap target not in SnapTargetType enum");
                break;
        }

        gchar *source_name = _("UNDEFINED");
        switch (p.getSource()) {
            case SNAPSOURCE_UNDEFINED:
                source_name = _("UNDEFINED");
                g_warning("Snap source has not been specified");
                break;
            case SNAPSOURCE_BBOX_CORNER:
                source_name = _("Bounding box corner");
                break;
            case SNAPSOURCE_BBOX_MIDPOINT:
                source_name = _("Bounding box midpoint");
                break;
            case SNAPSOURCE_BBOX_EDGE_MIDPOINT:
                source_name = _("Bounding box side midpoint");
                break;
            case SNAPSOURCE_NODE_SMOOTH:
                source_name = _("Smooth node");
                break;
            case SNAPSOURCE_NODE_CUSP:
                source_name = _("Cusp node");
                break;
            case SNAPSOURCE_LINE_MIDPOINT:
                source_name = _("Line midpoint");
                break;
            case SNAPSOURCE_OBJECT_MIDPOINT:
                source_name = _("Object midpoint");
                break;
            case SNAPSOURCE_ROTATION_CENTER:
                source_name = _("Object rotation center");
                break;
            case SNAPSOURCE_NODE_HANDLE:
            case SNAPSOURCE_OTHER_HANDLE:
                source_name = _("Handle");
                break;
            case SNAPSOURCE_PATH_INTERSECTION:
                source_name = _("Path intersection");
                break;
            case SNAPSOURCE_GUIDE:
                source_name = _("Guide");
                break;
            case SNAPSOURCE_GUIDE_ORIGIN:
                source_name = _("Guide origin");
                break;
            case SNAPSOURCE_CONVEX_HULL_CORNER:
                source_name = _("Convex hull corner");
                break;
            case SNAPSOURCE_ELLIPSE_QUADRANT_POINT:
                source_name = _("Quadrant point");
                break;
            case SNAPSOURCE_RECT_CORNER:
            case SNAPSOURCE_IMG_CORNER:
                source_name = _("Corner");
                break;
            case SNAPSOURCE_TEXT_ANCHOR:
                source_name = _("Text anchor");
                break;
            case SNAPSOURCE_GRID_PITCH:
                source_name = _("Multiple of grid spacing");
                break;
            default:
                g_warning("Snap source not in SnapSourceType enum");
                break;
        }
        //std::cout << "Snapped " << source_name << " to " << target_name << std::endl;

        remove_snapsource(); // Don't set both the source and target indicators, as these will overlap

        // Display the snap indicator (i.e. the cross)
        SPCanvasItem * canvasitem = nullptr;
        canvasitem = sp_canvas_item_new(_desktop->getTempGroup(),
                                        SP_TYPE_CTRL,
                                        "anchor", SP_ANCHOR_CENTER,
                                        "size", 11,
                                        "stroked", TRUE,
                                        "stroke_color", pre_snap ? 0x7f7f7fff : 0xff0000ff,
                                        "mode", SP_KNOT_MODE_XOR,
                                        "shape", SP_KNOT_SHAPE_CROSS,
                                        NULL );

        double timeout_val = prefs->getDouble("/options/snapindicatorpersistence/value", 2.0);
        if (timeout_val < 0.1) {
            timeout_val = 0.1; // a zero value would mean infinite persistence (i.e. until new snap occurs)
            // Besides, negatives values would ....?
        }


        // The snap indicator will be deleted after some time-out, and sp_canvas_item_dispose
        // will be called. This will set canvas->current_item to NULL if the snap indicator was
        // the current item, after which any events will go to the root handler instead of any
        // item handler. Dragging an object which has just snapped might therefore not be possible
        // without selecting / repicking it again. To avoid this, we make sure here that the
        // snap indicator will never be picked, and will therefore never be the current item.
        // Reported bugs:
        //   - scrolling when hovering above a pre-snap indicator won't work (for example)
        //     (https://bugs.launchpad.net/inkscape/+bug/522335/comments/8)
        //   - dragging doesn't work without repicking
        //     (https://bugs.launchpad.net/inkscape/+bug/1420301/comments/15)
        SP_CTRL(canvasitem)->pickable = false;

        SP_CTRL(canvasitem)->moveto(p.getPoint());
        _snaptarget = _desktop->add_temporary_canvasitem(canvasitem, timeout_val*1000.0);
        _snaptarget_is_presnap = pre_snap;

        // Display the tooltip, which reveals the type of snap source and the type of snap target
        gchar *tooltip_str = nullptr;
        if ( (p.getSource() != SNAPSOURCE_GRID_PITCH) && (p.getTarget() != SNAPTARGET_UNDEFINED) ) {
            tooltip_str = g_strconcat(source_name, _(" to "), target_name, NULL);
        } else if (p.getSource() != SNAPSOURCE_UNDEFINED) {
            tooltip_str = g_strdup(source_name);
        }
        double fontsize = prefs->getDouble("/tools/measure/fontsize", 10.0);

        if (tooltip_str) {
            Geom::Point tooltip_pos = p.getPoint();
            if (tools_isactive(_desktop, TOOLS_MEASURE)) {
                // Make sure that the snap tooltips do not overlap the ones from the measure tool
                tooltip_pos += _desktop->w2d(Geom::Point(0, -3*fontsize));
            } else {
                tooltip_pos += _desktop->w2d(Geom::Point(0, -2*fontsize));
            }

            SPCanvasItem *canvas_tooltip = sp_canvastext_new(_desktop->getTempGroup(), _desktop, tooltip_pos, tooltip_str);
            sp_canvastext_set_fontsize(SP_CANVASTEXT(canvas_tooltip), fontsize);
            SP_CANVASTEXT(canvas_tooltip)->pickable = false; // See the extensive comment above
            SP_CANVASTEXT(canvas_tooltip)->rgba = 0xffffffff;
            SP_CANVASTEXT(canvas_tooltip)->outline = false;
            SP_CANVASTEXT(canvas_tooltip)->background = true;
            if (pre_snap) {
                SP_CANVASTEXT(canvas_tooltip)->rgba_background = 0x33337f40;
            } else {
                SP_CANVASTEXT(canvas_tooltip)->rgba_background = 0x33337f7f;
            }
            SP_CANVASTEXT(canvas_tooltip)->anchor_position = TEXT_ANCHOR_CENTER;
            g_free(tooltip_str);

            _snaptarget_tooltip = _desktop->add_temporary_canvasitem(canvas_tooltip, timeout_val*1000.0);
        }

        // Display the bounding box, if we snapped to one
        Geom::OptRect const bbox = p.getTargetBBox();
        if (bbox) {
            SPCanvasItem* box = sp_canvas_item_new(_desktop->getTempGroup(),
                                                     SP_TYPE_CTRLRECT,
                                                     nullptr);

            SP_CTRLRECT(box)->setRectangle(*bbox);
            SP_CTRLRECT(box)->setColor(pre_snap ? 0x7f7f7fff : 0xff0000ff, false, 0);
            SP_CTRLRECT(box)->setDashed(true);
            SP_CTRLRECT(box)->pickable = false;  // See the extensive comment above
            sp_canvas_item_move_to_z(box, 0);
            _snaptarget_bbox = _desktop->add_temporary_canvasitem(box, timeout_val*1000.0);
        }
    }
}

void
SnapIndicator::remove_snaptarget(bool only_if_presnap)
{
    if (only_if_presnap && !_snaptarget_is_presnap) {
        return;
    }

    if (_snaptarget) {
        _desktop->remove_temporary_canvasitem(_snaptarget);
        _snaptarget = nullptr;
        _snaptarget_is_presnap = false;
    }

    if (_snaptarget_tooltip) {
        _desktop->remove_temporary_canvasitem(_snaptarget_tooltip);
        _snaptarget_tooltip = nullptr;
    }

    if (_snaptarget_bbox) {
        _desktop->remove_temporary_canvasitem(_snaptarget_bbox);
        _snaptarget_bbox = nullptr;
    }

}

void
SnapIndicator::set_new_snapsource(Inkscape::SnapCandidatePoint const &p)
{
    remove_snapsource();

    g_assert(_desktop != nullptr); // If this fails, then likely setup() has not been called on the snap manager (see snap.cpp -> setup())

    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
    bool value = prefs->getBool("/options/snapindicator/value", true);

    if (value) {
        SPCanvasItem * canvasitem = sp_canvas_item_new( _desktop->getTempGroup(),
                                                        SP_TYPE_CTRL,
                                                        "anchor", SP_ANCHOR_CENTER,
                                                        "size", 7,
                                                        "stroked", TRUE,
                                                        "stroke_color", 0xff0000ff,
                                                        "mode", SP_KNOT_MODE_XOR,
                                                        "shape", SP_KNOT_SHAPE_CIRCLE,
                                                        NULL );

        SP_CTRL(canvasitem)->moveto(p.getPoint());
        _snapsource = _desktop->add_temporary_canvasitem(canvasitem, 1000);
    }
}

void
SnapIndicator::set_new_debugging_point(Geom::Point const &p)
{
    g_assert(_desktop != nullptr);
    SPCanvasItem * canvasitem = sp_canvas_item_new( _desktop->getTempGroup(),
                                                    SP_TYPE_CTRL,
                                                    "anchor", SP_ANCHOR_CENTER,
                                                    "size", 11,
                                                    "fill_color", 0x00ff00ff,
                                                    "stroked", FALSE,
                                                    "mode", SP_KNOT_MODE_XOR,
                                                    "shape", SP_KNOT_SHAPE_DIAMOND,
                                                    NULL );

    SP_CTRL(canvasitem)->moveto(p);
    _debugging_points.push_back(_desktop->add_temporary_canvasitem(canvasitem, 5000));

}

void
SnapIndicator::remove_snapsource()
{
    if (_snapsource) {
        _desktop->remove_temporary_canvasitem(_snapsource);
        _snapsource = nullptr;
    }
}

void
SnapIndicator::remove_debugging_points()
{
    for (std::list<TemporaryItem *>::const_iterator i = _debugging_points.begin(); i != _debugging_points.end(); ++i) {
        _desktop->remove_temporary_canvasitem(*i);
    }
    _debugging_points.clear();
}


} //namespace Display
} /* namespace Inkscape */


/*
  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=4:softtabstop=4 :