615968fef2a68a205863bd95b37787f177ba1b9b
1 /** @file
2 * Node selection - implementation
3 */
4 /* Authors:
5 * Krzysztof KosiĆski <tweenk.pl@gmail.com>
6 *
7 * Copyright (C) 2009 Authors
8 * Released under GNU GPL, read the file 'COPYING' for more information
9 */
11 #include <2geom/transforms.h>
12 #include "desktop.h"
13 #include "preferences.h"
14 #include "ui/tool/control-point-selection.h"
15 #include "ui/tool/event-utils.h"
16 #include "ui/tool/selectable-control-point.h"
17 #include "ui/tool/transform-handle-set.h"
19 namespace Inkscape {
20 namespace UI {
22 /**
23 * @class ControlPointSelection
24 * @brief Group of selected control points.
25 *
26 * Some operations can be performed on all selected points regardless of their type, therefore
27 * this class is also a Manipulator. It handles the transformations of points using
28 * the keyboard.
29 *
30 * The exposed interface is similar to that of an STL set. Internally, a hash map is used.
31 * @todo Correct iterators (that don't expose the connection list)
32 */
34 /** @var ControlPointSelection::signal_update
35 * Fires when the display needs to be updated to reflect changes.
36 */
37 /** @var ControlPointSelection::signal_point_changed
38 * Fires when a control point is added to or removed from the selection.
39 * The first param contains a pointer to the control point that changed sel. state.
40 * The second says whether the point is currently selected.
41 */
42 /** @var ControlPointSelection::signal_commit
43 * Fires when a change that needs to be committed to XML happens.
44 */
46 ControlPointSelection::ControlPointSelection(SPDesktop *d, SPCanvasGroup *th_group)
47 : Manipulator(d)
48 , _handles(new TransformHandleSet(d, th_group))
49 , _dragging(false)
50 , _handles_visible(true)
51 , _one_node_handles(false)
52 {
53 signal_update.connect( sigc::bind(
54 sigc::mem_fun(*this, &ControlPointSelection::_updateTransformHandles),
55 true));
56 signal_point_changed.connect(
57 sigc::hide( sigc::hide(
58 sigc::bind(
59 sigc::mem_fun(*this, &ControlPointSelection::_updateTransformHandles),
60 false))));
61 _handles->signal_transform.connect(
62 sigc::mem_fun(*this, &ControlPointSelection::transform));
63 _handles->signal_commit.connect(
64 sigc::mem_fun(*this, &ControlPointSelection::_commitTransform));
65 }
67 ControlPointSelection::~ControlPointSelection()
68 {
69 clear();
70 delete _handles;
71 }
73 /** Add a control point to the selection. */
74 std::pair<ControlPointSelection::iterator, bool> ControlPointSelection::insert(const value_type &x)
75 {
76 iterator found = _points.find(x);
77 if (found != _points.end()) {
78 return std::pair<iterator, bool>(found, false);
79 }
81 boost::shared_ptr<connlist_type> clist(new connlist_type());
83 // hide event param and always return false
84 clist->push_back(
85 x->signal_grabbed.connect(
86 sigc::bind_return(
87 sigc::hide(
88 sigc::mem_fun(*this, &ControlPointSelection::_pointGrabbed)),
89 false)));
90 clist->push_back(
91 x->signal_dragged.connect(
92 sigc::mem_fun(*this, &ControlPointSelection::_pointDragged)));
93 clist->push_back(
94 x->signal_ungrabbed.connect(
95 sigc::hide(
96 sigc::mem_fun(*this, &ControlPointSelection::_pointUngrabbed))));
97 clist->push_back(
98 x->signal_clicked.connect(
99 sigc::bind<0>(
100 sigc::mem_fun(*this, &ControlPointSelection::_pointClicked),
101 x)));
103 found = _points.insert(std::make_pair(x, clist)).first;
105 x->updateState();
106 _rot_radius.reset();
107 signal_point_changed.emit(x, true);
109 return std::pair<iterator, bool>(found, true);
110 }
112 /** Remove a point from the selection. */
113 void ControlPointSelection::erase(iterator pos)
114 {
115 SelectableControlPoint *erased = pos->first;
116 boost::shared_ptr<connlist_type> clist = pos->second;
117 for (connlist_type::iterator i = clist->begin(); i != clist->end(); ++i) {
118 i->disconnect();
119 }
120 _points.erase(pos);
121 erased->updateState();
122 _rot_radius.reset();
123 signal_point_changed.emit(erased, false);
124 }
125 ControlPointSelection::size_type ControlPointSelection::erase(const key_type &k)
126 {
127 iterator pos = _points.find(k);
128 if (pos == _points.end()) return 0;
129 erase(pos);
130 return 1;
131 }
132 void ControlPointSelection::erase(iterator first, iterator last)
133 {
134 while (first != last) erase(first++);
135 }
137 /** Remove all points from the selection, making it empty. */
138 void ControlPointSelection::clear()
139 {
140 for (iterator i = begin(); i != end(); )
141 erase(i++);
142 }
144 /** Select all points that this selection can contain. */
145 void ControlPointSelection::selectAll()
146 {
147 for (set_type::iterator i = _all_points.begin(); i != _all_points.end(); ++i) {
148 insert(*i);
149 }
150 }
151 /** Select all points inside the given rectangle (in desktop coordinates). */
152 void ControlPointSelection::selectArea(Geom::Rect const &r)
153 {
154 for (set_type::iterator i = _all_points.begin(); i != _all_points.end(); ++i) {
155 if (r.contains(**i))
156 insert(*i);
157 }
158 }
159 /** Unselect all selected points and select all unselected points. */
160 void ControlPointSelection::invertSelection()
161 {
162 for (set_type::iterator i = _all_points.begin(); i != _all_points.end(); ++i) {
163 if ((*i)->selected()) erase(*i);
164 else insert(*i);
165 }
166 }
167 void ControlPointSelection::spatialGrow(SelectableControlPoint *origin, int dir)
168 {
169 bool grow = (dir > 0);
170 Geom::Point p = origin->position();
171 double best_dist = grow ? HUGE_VAL : 0;
172 SelectableControlPoint *match = NULL;
173 for (set_type::iterator i = _all_points.begin(); i != _all_points.end(); ++i) {
174 bool selected = (*i)->selected();
175 if (grow && !selected) {
176 double dist = Geom::distance((*i)->position(), p);
177 if (dist < best_dist) {
178 best_dist = dist;
179 match = *i;
180 }
181 }
182 if (!grow && selected) {
183 double dist = Geom::distance((*i)->position(), p);
184 // use >= to also deselect the origin node when it's the last one selected
185 if (dist >= best_dist) {
186 best_dist = dist;
187 match = *i;
188 }
189 }
190 }
191 if (match) {
192 if (grow) insert(match);
193 else erase(match);
194 }
195 }
197 /** Transform all selected control points by the given affine transformation. */
198 void ControlPointSelection::transform(Geom::Matrix const &m)
199 {
200 for (iterator i = _points.begin(); i != _points.end(); ++i) {
201 SelectableControlPoint *cur = i->first;
202 cur->transform(m);
203 }
204 // TODO preserving the rotation radius needs some rethinking...
205 if (_rot_radius) (*_rot_radius) *= m.descrim();
206 signal_update.emit();
207 }
209 /** Align control points on the specified axis. */
210 void ControlPointSelection::align(Geom::Dim2 axis)
211 {
212 if (empty()) return;
213 Geom::Dim2 d = static_cast<Geom::Dim2>((axis + 1) % 2);
215 Geom::OptInterval bound;
216 for (iterator i = _points.begin(); i != _points.end(); ++i) {
217 bound.unionWith(Geom::OptInterval(i->first->position()[d]));
218 }
220 double new_coord = bound->middle();
221 for (iterator i = _points.begin(); i != _points.end(); ++i) {
222 Geom::Point pos = i->first->position();
223 pos[d] = new_coord;
224 i->first->move(pos);
225 }
226 }
228 /** Equdistantly distribute control points by moving them in the specified dimension. */
229 void ControlPointSelection::distribute(Geom::Dim2 d)
230 {
231 if (empty()) return;
233 // this needs to be a multimap, otherwise it will fail when some points have the same coord
234 typedef std::multimap<double, SelectableControlPoint*> SortMap;
236 SortMap sm;
237 Geom::OptInterval bound;
238 // first we insert all points into a multimap keyed by the aligned coord to sort them
239 // simultaneously we compute the extent of selection
240 for (iterator i = _points.begin(); i != _points.end(); ++i) {
241 Geom::Point pos = i->first->position();
242 sm.insert(std::make_pair(pos[d], i->first));
243 bound.unionWith(Geom::OptInterval(pos[d]));
244 }
246 // now we iterate over the multimap and set aligned positions.
247 double step = size() == 1 ? 0 : bound->extent() / (size() - 1);
248 double start = bound->min();
249 unsigned num = 0;
250 for (SortMap::iterator i = sm.begin(); i != sm.end(); ++i, ++num) {
251 Geom::Point pos = i->second->position();
252 pos[d] = start + num * step;
253 i->second->move(pos);
254 }
255 }
257 /** Get the bounds of the selection.
258 * @return Smallest rectangle containing the positions of all selected points,
259 * or nothing if the selection is empty */
260 Geom::OptRect ControlPointSelection::pointwiseBounds()
261 {
262 Geom::OptRect bound;
263 for (iterator i = _points.begin(); i != _points.end(); ++i) {
264 SelectableControlPoint *cur = i->first;
265 Geom::Point p = cur->position();
266 if (!bound) {
267 bound = Geom::Rect(p, p);
268 } else {
269 bound->expandTo(p);
270 }
271 }
272 return bound;
273 }
275 Geom::OptRect ControlPointSelection::bounds()
276 {
277 Geom::OptRect bound;
278 for (iterator i = _points.begin(); i != _points.end(); ++i) {
279 SelectableControlPoint *cur = i->first;
280 Geom::OptRect r = cur->bounds();
281 bound.unionWith(r);
282 }
283 return bound;
284 }
286 void ControlPointSelection::showTransformHandles(bool v, bool one_node)
287 {
288 _one_node_handles = one_node;
289 _handles_visible = v;
290 _updateTransformHandles(false);
291 }
293 void ControlPointSelection::hideTransformHandles()
294 {
295 _handles->setVisible(false);
296 }
297 void ControlPointSelection::restoreTransformHandles()
298 {
299 _updateTransformHandles(true);
300 }
302 void ControlPointSelection::toggleTransformHandlesMode()
303 {
304 if (_handles->mode() == TransformHandleSet::MODE_SCALE) {
305 _handles->setMode(TransformHandleSet::MODE_ROTATE_SKEW);
306 if (size() == 1) _handles->rotationCenter().setVisible(false);
307 } else {
308 _handles->setMode(TransformHandleSet::MODE_SCALE);
309 }
310 }
312 void ControlPointSelection::_pointGrabbed()
313 {
314 hideTransformHandles();
315 _dragging = true;
316 }
318 void ControlPointSelection::_pointDragged(Geom::Point const &old_pos, Geom::Point &new_pos,
319 GdkEventMotion */*event*/)
320 {
321 Geom::Point delta = new_pos - old_pos;
322 for (iterator i = _points.begin(); i != _points.end(); ++i) {
323 SelectableControlPoint *cur = i->first;
324 cur->move(cur->position() + delta);
325 }
326 _handles->rotationCenter().move(_handles->rotationCenter().position() + delta);
327 signal_update.emit();
328 }
330 void ControlPointSelection::_pointUngrabbed()
331 {
332 _dragging = false;
333 _grabbed_point = NULL;
334 restoreTransformHandles();
335 signal_commit.emit(COMMIT_MOUSE_MOVE);
336 }
338 bool ControlPointSelection::_pointClicked(SelectableControlPoint *p, GdkEventButton *event)
339 {
340 // clicking a selected node should toggle the transform handles between rotate and scale mode,
341 // if they are visible
342 if (held_no_modifiers(*event) && _handles_visible && p->selected()) {
343 toggleTransformHandlesMode();
344 return true;
345 }
346 return false;
347 }
349 void ControlPointSelection::_updateTransformHandles(bool preserve_center)
350 {
351 if (_dragging) return;
353 if (_handles_visible && size() > 1) {
354 Geom::OptRect b = pointwiseBounds();
355 _handles->setBounds(*b, preserve_center);
356 _handles->setVisible(true);
357 } else if (_one_node_handles && size() == 1) { // only one control point in selection
358 SelectableControlPoint *p = begin()->first;
359 _handles->setBounds(p->bounds());
360 _handles->rotationCenter().move(p->position());
361 _handles->rotationCenter().setVisible(false);
362 _handles->setVisible(true);
363 } else {
364 _handles->setVisible(false);
365 }
366 }
368 /** Moves the selected points along the supplied unit vector according to
369 * the modifier state of the supplied event. */
370 bool ControlPointSelection::_keyboardMove(GdkEventKey const &event, Geom::Point const &dir)
371 {
372 if (held_control(event)) return false;
373 unsigned num = 1 + consume_same_key_events(shortcut_key(event), 0);
375 Geom::Point delta = dir * num;
376 if (held_shift(event)) delta *= 10;
377 if (held_alt(event)) {
378 delta /= _desktop->current_zoom();
379 } else {
380 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
381 double nudge = prefs->getDoubleLimited("/options/nudgedistance/value", 2, 0, 1000);
382 delta *= nudge;
383 }
385 transform(Geom::Translate(delta));
386 if (fabs(dir[Geom::X]) > 0) {
387 signal_commit.emit(COMMIT_KEYBOARD_MOVE_X);
388 } else {
389 signal_commit.emit(COMMIT_KEYBOARD_MOVE_Y);
390 }
391 return true;
392 }
394 /** Rotates the selected points in the given direction according to the modifier state
395 * from the supplied event.
396 * @param event Key event to take modifier state from
397 * @param dir Direction of rotation (math convention: 1 = counterclockwise, -1 = clockwise)
398 */
399 bool ControlPointSelection::_keyboardRotate(GdkEventKey const &event, int dir)
400 {
401 if (empty()) return false;
403 Geom::Point rc = _handles->rotationCenter();
404 if (!_rot_radius) {
405 Geom::Rect b = *(size() == 1 ? bounds() : pointwiseBounds());
406 double maxlen = 0;
407 for (unsigned i = 0; i < 4; ++i) {
408 double len = (b.corner(i) - rc).length();
409 if (len > maxlen) maxlen = len;
410 }
411 _rot_radius = maxlen;
412 }
414 double angle;
415 if (held_alt(event)) {
416 // Rotate by "one pixel". We interpret this as rotating by an angle that causes
417 // the topmost point of a circle circumscribed about the selection's bounding box
418 // to move on an arc 1 screen pixel long.
419 angle = atan2(1.0 / _desktop->current_zoom(), *_rot_radius) * dir;
420 } else {
421 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
422 int snaps = prefs->getIntLimited("/options/rotationsnapsperpi/value", 12, 1, 1000);
423 angle = M_PI * dir / snaps;
424 }
426 // translate to origin, rotate, translate back to original position
427 Geom::Matrix m = Geom::Translate(-rc)
428 * Geom::Rotate(angle) * Geom::Translate(rc);
429 transform(m);
430 signal_commit.emit(COMMIT_KEYBOARD_ROTATE);
431 return true;
432 }
435 bool ControlPointSelection::_keyboardScale(GdkEventKey const &event, int dir)
436 {
437 if (empty()) return false;
439 // TODO should the saved rotation center or the current center be used?
440 Geom::Rect bound = (size() == 1 ? *bounds() : *pointwiseBounds());
441 double maxext = bound.maxExtent();
442 if (Geom::are_near(maxext, 0)) return false;
443 Geom::Point center = _handles->rotationCenter().position();
445 double length_change;
446 if (held_alt(event)) {
447 // Scale by "one pixel". It means shrink/grow 1px for the larger dimension
448 // of the bounding box.
449 length_change = 1.0 / _desktop->current_zoom() * dir;
450 } else {
451 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
452 length_change = prefs->getDoubleLimited("/options/defaultscale/value", 2, 1, 1000);
453 length_change *= dir;
454 }
455 double scale = (maxext + length_change) / maxext;
457 Geom::Matrix m = Geom::Translate(-center) * Geom::Scale(scale) * Geom::Translate(center);
458 transform(m);
459 signal_commit.emit(COMMIT_KEYBOARD_SCALE_UNIFORM);
460 return true;
461 }
463 bool ControlPointSelection::_keyboardFlip(Geom::Dim2 d)
464 {
465 if (empty()) return false;
467 Geom::Scale scale_transform(1, 1);
468 if (d == Geom::X) {
469 scale_transform = Geom::Scale(-1, 1);
470 } else {
471 scale_transform = Geom::Scale(1, -1);
472 }
474 SelectableControlPoint *scp =
475 dynamic_cast<SelectableControlPoint*>(ControlPoint::mouseovered_point);
476 Geom::Point center = scp ? scp->position() : _handles->rotationCenter().position();
478 Geom::Matrix m = Geom::Translate(-center) * scale_transform * Geom::Translate(center);
479 transform(m);
480 signal_commit.emit(d == Geom::X ? COMMIT_FLIP_X : COMMIT_FLIP_Y);
481 return true;
482 }
484 void ControlPointSelection::_commitTransform(CommitEvent ce)
485 {
486 _updateTransformHandles(true);
487 signal_commit.emit(ce);
488 }
490 bool ControlPointSelection::event(GdkEvent *event)
491 {
492 // implement generic event handling that should apply for all control point selections here;
493 // for example, keyboard moves and transformations. This way this functionality doesn't need
494 // to be duplicated in many places
495 // Later split out so that it can be reused in object selection
497 switch (event->type) {
498 case GDK_KEY_PRESS:
499 // do not handle key events if the selection is empty
500 if (empty()) break;
502 switch(shortcut_key(event->key)) {
503 // moves
504 case GDK_Up:
505 case GDK_KP_Up:
506 case GDK_KP_8:
507 return _keyboardMove(event->key, Geom::Point(0, 1));
508 case GDK_Down:
509 case GDK_KP_Down:
510 case GDK_KP_2:
511 return _keyboardMove(event->key, Geom::Point(0, -1));
512 case GDK_Right:
513 case GDK_KP_Right:
514 case GDK_KP_6:
515 return _keyboardMove(event->key, Geom::Point(1, 0));
516 case GDK_Left:
517 case GDK_KP_Left:
518 case GDK_KP_4:
519 return _keyboardMove(event->key, Geom::Point(-1, 0));
521 // rotates
522 case GDK_bracketleft:
523 return _keyboardRotate(event->key, 1);
524 case GDK_bracketright:
525 return _keyboardRotate(event->key, -1);
527 // scaling
528 case GDK_less:
529 case GDK_comma:
530 return _keyboardScale(event->key, -1);
531 case GDK_greater:
532 case GDK_period:
533 return _keyboardScale(event->key, 1);
535 // TODO: skewing
537 // flipping
538 // NOTE: H is horizontal flip, while Shift+H switches transform handle mode!
539 case GDK_h:
540 case GDK_H:
541 if (held_shift(event->key)) {
542 toggleTransformHandlesMode();
543 return true;
544 }
545 // any modifiers except shift should cause no action
546 if (held_any_modifiers(event->key)) break;
547 return _keyboardFlip(Geom::X);
548 case GDK_v:
549 case GDK_V:
550 if (held_any_modifiers(event->key)) break;
551 return _keyboardFlip(Geom::Y);
552 default: break;
553 }
554 break;
555 default: break;
556 }
557 return false;
558 }
560 } // namespace UI
561 } // namespace Inkscape
563 /*
564 Local Variables:
565 mode:c++
566 c-file-style:"stroustrup"
567 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
568 indent-tabs-mode:nil
569 fill-column:99
570 End:
571 */
572 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :