8c8d64ec00554f4d181ed0af779ec0081b22d0a5
1 /** @file
2 * @brief Align and Distribute dialog - implementation
3 */
4 /* Authors:
5 * Bryce W. Harrington <bryce@bryceharrington.org>
6 * Aubanel MONNIER <aubi@libertysurf.fr>
7 * Frank Felfe <innerspace@iname.com>
8 * Lauris Kaplinski <lauris@kaplinski.com>
9 * Tim Dwyer <tgdwyer@gmail.com>
10 *
11 * Copyright (C) 1999-2004, 2005 Authors
12 *
13 * Released under GNU GPL. Read the file 'COPYING' for more information.
14 */
17 #ifdef HAVE_CONFIG_H
18 # include <config.h>
19 #endif
21 #include <gtkmm/spinbutton.h>
23 #include "desktop-handles.h"
24 #include "unclump.h"
25 #include "document.h"
26 #include "enums.h"
27 #include "graphlayout/graphlayout.h"
28 #include "inkscape.h"
29 #include "macros.h"
30 #include "preferences.h"
31 #include "removeoverlap/removeoverlap.h"
32 #include "selection.h"
33 #include "sp-flowtext.h"
34 #include "sp-item-transform.h"
35 #include "sp-text.h"
36 #include "text-editing.h"
37 #include "tools-switch.h"
38 #include "ui/icon-names.h"
39 #include "ui/tool/node-tool.h"
40 #include "ui/tool/multi-path-manipulator.h"
41 #include "util/glib-list-iterators.h"
42 #include "verbs.h"
43 #include "widgets/icon.h"
45 #include "align-and-distribute.h"
47 namespace Inkscape {
48 namespace UI {
49 namespace Dialog {
51 /////////helper classes//////////////////////////////////
53 class Action {
54 public :
55 Action(const Glib::ustring &id,
56 const Glib::ustring &tiptext,
57 guint row, guint column,
58 Gtk::Table &parent,
59 Gtk::Tooltips &tooltips,
60 AlignAndDistribute &dialog):
61 _dialog(dialog),
62 _id(id),
63 _parent(parent)
64 {
65 Gtk::Widget* pIcon = Gtk::manage( sp_icon_get_icon( _id, Inkscape::ICON_SIZE_LARGE_TOOLBAR) );
66 Gtk::Button * pButton = Gtk::manage(new Gtk::Button());
67 pButton->set_relief(Gtk::RELIEF_NONE);
68 pIcon->show();
69 pButton->add(*pIcon);
70 pButton->show();
72 pButton->signal_clicked()
73 .connect(sigc::mem_fun(*this, &Action::on_button_click));
74 tooltips.set_tip(*pButton, tiptext);
75 parent.attach(*pButton, column, column+1, row, row+1, Gtk::FILL, Gtk::FILL);
76 }
77 virtual ~Action(){}
79 AlignAndDistribute &_dialog;
81 private :
82 virtual void on_button_click(){}
84 Glib::ustring _id;
85 Gtk::Table &_parent;
86 };
89 class ActionAlign : public Action {
90 public :
91 struct Coeffs {
92 double mx0, mx1, my0, my1;
93 double sx0, sx1, sy0, sy1;
94 };
95 ActionAlign(const Glib::ustring &id,
96 const Glib::ustring &tiptext,
97 guint row, guint column,
98 AlignAndDistribute &dialog,
99 guint coeffIndex):
100 Action(id, tiptext, row, column,
101 dialog.align_table(), dialog.tooltips(), dialog),
102 _index(coeffIndex),
103 _dialog(dialog)
104 {}
106 private :
108 virtual void on_button_click() {
109 //Retreive selected objects
110 SPDesktop *desktop = _dialog.getDesktop();
111 if (!desktop) return;
113 Inkscape::Selection *selection = sp_desktop_selection(desktop);
114 if (!selection) return;
116 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
117 bool sel_as_group = prefs->getBool("/dialogs/align/sel-as-groups");
119 using Inkscape::Util::GSListConstIterator;
120 std::list<SPItem *> selected;
121 selected.insert<GSListConstIterator<SPItem *> >(selected.end(), selection->itemList(), NULL);
122 if (selected.empty()) return;
124 Geom::Point mp; //Anchor point
125 AlignAndDistribute::AlignTarget target = _dialog.getAlignTarget();
126 const Coeffs &a= _allCoeffs[_index];
127 switch (target)
128 {
129 case AlignAndDistribute::LAST:
130 case AlignAndDistribute::FIRST:
131 case AlignAndDistribute::BIGGEST:
132 case AlignAndDistribute::SMALLEST:
133 {
134 //Check 2 or more selected objects
135 std::list<SPItem *>::iterator second(selected.begin());
136 ++second;
137 if (second == selected.end())
138 return;
139 //Find the master (anchor on which the other objects are aligned)
140 std::list<SPItem *>::iterator master(
141 _dialog.find_master (
142 selected,
143 (a.mx0 != 0.0) ||
144 (a.mx1 != 0.0) )
145 );
146 //remove the master from the selection
147 SPItem * thing = *master;
148 // TODO: either uncomment or remove the following commented lines, depending on which
149 // behaviour of moving objects makes most sense; also cf. discussion at
150 // https://bugs.launchpad.net/inkscape/+bug/255933
151 /*if (!sel_as_group) { */
152 selected.erase(master);
153 /*}*/
154 //Compute the anchor point
155 Geom::OptRect b = sp_item_bbox_desktop (thing);
156 if (b) {
157 mp = Geom::Point(a.mx0 * b->min()[Geom::X] + a.mx1 * b->max()[Geom::X],
158 a.my0 * b->min()[Geom::Y] + a.my1 * b->max()[Geom::Y]);
159 } else {
160 return;
161 }
162 break;
163 }
165 case AlignAndDistribute::PAGE:
166 mp = Geom::Point(a.mx1 * sp_document_width(sp_desktop_document(desktop)),
167 a.my1 * sp_document_height(sp_desktop_document(desktop)));
168 break;
170 case AlignAndDistribute::DRAWING:
171 {
172 Geom::OptRect b = sp_item_bbox_desktop
173 ( (SPItem *) sp_document_root (sp_desktop_document (desktop)) );
174 if (b) {
175 mp = Geom::Point(a.mx0 * b->min()[Geom::X] + a.mx1 * b->max()[Geom::X],
176 a.my0 * b->min()[Geom::Y] + a.my1 * b->max()[Geom::Y]);
177 } else {
178 return;
179 }
180 break;
181 }
183 case AlignAndDistribute::SELECTION:
184 {
185 Geom::OptRect b = selection->bounds();
186 if (b) {
187 mp = Geom::Point(a.mx0 * b->min()[Geom::X] + a.mx1 * b->max()[Geom::X],
188 a.my0 * b->min()[Geom::Y] + a.my1 * b->max()[Geom::Y]);
189 } else {
190 return;
191 }
192 break;
193 }
195 default:
196 g_assert_not_reached ();
197 break;
198 }; // end of switch
200 // Top hack: temporarily set clone compensation to unmoved, so that we can align/distribute
201 // clones with their original (and the move of the original does not disturb the
202 // clones). The only problem with this is that if there are outside-of-selection clones of
203 // a selected original, they will be unmoved too, possibly contrary to user's
204 // expecation. However this is a minor point compared to making align/distribute always
205 // work as expected, and "unmoved" is the default option anyway.
206 int saved_compensation = prefs->getInt("/options/clonecompensation/value", SP_CLONE_COMPENSATION_UNMOVED);
207 prefs->setInt("/options/clonecompensation/value", SP_CLONE_COMPENSATION_UNMOVED);
209 bool changed = false;
210 Geom::OptRect b;
211 if (sel_as_group)
212 b = selection->bounds();
214 //Move each item in the selected list separately
215 for (std::list<SPItem *>::iterator it(selected.begin());
216 it != selected.end();
217 it++)
218 {
219 sp_document_ensure_up_to_date(sp_desktop_document (desktop));
220 if (!sel_as_group)
221 b = sp_item_bbox_desktop (*it);
222 if (b) {
223 Geom::Point const sp(a.sx0 * b->min()[Geom::X] + a.sx1 * b->max()[Geom::X],
224 a.sy0 * b->min()[Geom::Y] + a.sy1 * b->max()[Geom::Y]);
225 Geom::Point const mp_rel( mp - sp );
226 if (LInfty(mp_rel) > 1e-9) {
227 sp_item_move_rel(*it, Geom::Translate(mp_rel));
228 changed = true;
229 }
230 }
231 }
233 // restore compensation setting
234 prefs->setInt("/options/clonecompensation/value", saved_compensation);
236 if (changed) {
237 sp_document_done ( sp_desktop_document (desktop) , SP_VERB_DIALOG_ALIGN_DISTRIBUTE,
238 _("Align"));
239 }
242 }
243 guint _index;
244 AlignAndDistribute &_dialog;
246 static const Coeffs _allCoeffs[10];
248 };
249 ActionAlign::Coeffs const ActionAlign::_allCoeffs[10] = {
250 {1., 0., 0., 0., 0., 1., 0., 0.},
251 {1., 0., 0., 0., 1., 0., 0., 0.},
252 {.5, .5, 0., 0., .5, .5, 0., 0.},
253 {0., 1., 0., 0., 0., 1., 0., 0.},
254 {0., 1., 0., 0., 1., 0., 0., 0.},
255 {0., 0., 0., 1., 0., 0., 1., 0.},
256 {0., 0., 0., 1., 0., 0., 0., 1.},
257 {0., 0., .5, .5, 0., 0., .5, .5},
258 {0., 0., 1., 0., 0., 0., 1., 0.},
259 {0., 0., 1., 0., 0., 0., 0., 1.}
260 };
262 BBoxSort::BBoxSort(SPItem *pItem, Geom::Rect bounds, Geom::Dim2 orientation, double kBegin, double kEnd) :
263 item(pItem),
264 bbox (bounds)
265 {
266 anchor = kBegin * bbox.min()[orientation] + kEnd * bbox.max()[orientation];
267 }
268 BBoxSort::BBoxSort(const BBoxSort &rhs) :
269 //NOTE : this copy ctor is called O(sort) when sorting the vector
270 //this is bad. The vector should be a vector of pointers.
271 //But I'll wait the bohem GC before doing that
272 item(rhs.item), anchor(rhs.anchor), bbox(rhs.bbox)
273 {
274 }
276 bool operator< (const BBoxSort &a, const BBoxSort &b)
277 {
278 return (a.anchor < b.anchor);
279 }
281 class ActionDistribute : public Action {
282 public :
283 ActionDistribute(const Glib::ustring &id,
284 const Glib::ustring &tiptext,
285 guint row, guint column,
286 AlignAndDistribute &dialog,
287 bool onInterSpace,
288 Geom::Dim2 orientation,
289 double kBegin, double kEnd
290 ):
291 Action(id, tiptext, row, column,
292 dialog.distribute_table(), dialog.tooltips(), dialog),
293 _dialog(dialog),
294 _onInterSpace(onInterSpace),
295 _orientation(orientation),
296 _kBegin(kBegin),
297 _kEnd( kEnd)
298 {}
300 private :
301 virtual void on_button_click() {
302 //Retreive selected objects
303 SPDesktop *desktop = _dialog.getDesktop();
304 if (!desktop) return;
306 Inkscape::Selection *selection = sp_desktop_selection(desktop);
307 if (!selection) return;
309 using Inkscape::Util::GSListConstIterator;
310 std::list<SPItem *> selected;
311 selected.insert<GSListConstIterator<SPItem *> >(selected.end(), selection->itemList(), NULL);
312 if (selected.empty()) return;
314 //Check 2 or more selected objects
315 std::list<SPItem *>::iterator second(selected.begin());
316 ++second;
317 if (second == selected.end()) return;
320 std::vector< BBoxSort > sorted;
321 for (std::list<SPItem *>::iterator it(selected.begin());
322 it != selected.end();
323 ++it)
324 {
325 Geom::OptRect bbox = sp_item_bbox_desktop(*it);
326 if (bbox) {
327 sorted.push_back(BBoxSort(*it, *bbox, _orientation, _kBegin, _kEnd));
328 }
329 }
330 //sort bbox by anchors
331 std::sort(sorted.begin(), sorted.end());
333 // see comment in ActionAlign above
334 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
335 int saved_compensation = prefs->getInt("/options/clonecompensation/value", SP_CLONE_COMPENSATION_UNMOVED);
336 prefs->setInt("/options/clonecompensation/value", SP_CLONE_COMPENSATION_UNMOVED);
338 unsigned int len = sorted.size();
339 bool changed = false;
340 if (_onInterSpace)
341 {
342 //overall bboxes span
343 float dist = (sorted.back().bbox.max()[_orientation] -
344 sorted.front().bbox.min()[_orientation]);
345 //space eaten by bboxes
346 float span = 0;
347 for (unsigned int i = 0; i < len; i++)
348 {
349 span += sorted[i].bbox[_orientation].extent();
350 }
351 //new distance between each bbox
352 float step = (dist - span) / (len - 1);
353 float pos = sorted.front().bbox.min()[_orientation];
354 for ( std::vector<BBoxSort> ::iterator it (sorted.begin());
355 it < sorted.end();
356 it ++ )
357 {
358 if (!NR_DF_TEST_CLOSE (pos, it->bbox.min()[_orientation], 1e-6)) {
359 Geom::Point t(0.0, 0.0);
360 t[_orientation] = pos - it->bbox.min()[_orientation];
361 sp_item_move_rel(it->item, Geom::Translate(t));
362 changed = true;
363 }
364 pos += it->bbox[_orientation].extent();
365 pos += step;
366 }
367 }
368 else
369 {
370 //overall anchor span
371 float dist = sorted.back().anchor - sorted.front().anchor;
372 //distance between anchors
373 float step = dist / (len - 1);
375 for ( unsigned int i = 0; i < len ; i ++ )
376 {
377 BBoxSort & it(sorted[i]);
378 //new anchor position
379 float pos = sorted.front().anchor + i * step;
380 //Don't move if we are really close
381 if (!NR_DF_TEST_CLOSE (pos, it.anchor, 1e-6)) {
382 //Compute translation
383 Geom::Point t(0.0, 0.0);
384 t[_orientation] = pos - it.anchor;
385 //translate
386 sp_item_move_rel(it.item, Geom::Translate(t));
387 changed = true;
388 }
389 }
390 }
392 // restore compensation setting
393 prefs->setInt("/options/clonecompensation/value", saved_compensation);
395 if (changed) {
396 sp_document_done ( sp_desktop_document (desktop), SP_VERB_DIALOG_ALIGN_DISTRIBUTE,
397 _("Distribute"));
398 }
399 }
400 guint _index;
401 AlignAndDistribute &_dialog;
402 bool _onInterSpace;
403 Geom::Dim2 _orientation;
405 double _kBegin;
406 double _kEnd;
408 };
411 class ActionNode : public Action {
412 public :
413 ActionNode(const Glib::ustring &id,
414 const Glib::ustring &tiptext,
415 guint column,
416 AlignAndDistribute &dialog,
417 Geom::Dim2 orientation, bool distribute):
418 Action(id, tiptext, 0, column,
419 dialog.nodes_table(), dialog.tooltips(), dialog),
420 _orientation(orientation),
421 _distribute(distribute)
422 {}
424 private :
425 Geom::Dim2 _orientation;
426 bool _distribute;
427 virtual void on_button_click()
428 {
430 if (!_dialog.getDesktop()) return;
431 SPEventContext *event_context = sp_desktop_event_context(_dialog.getDesktop());
432 if (!INK_IS_NODE_TOOL (event_context)) return;
433 InkNodeTool *nt = INK_NODE_TOOL(event_context);
435 if (_distribute)
436 nt->_multipath->distributeNodes(_orientation);
437 else
438 nt->_multipath->alignNodes(_orientation);
440 }
441 };
443 class ActionRemoveOverlaps : public Action {
444 private:
445 Gtk::Label removeOverlapXGapLabel;
446 Gtk::Label removeOverlapYGapLabel;
447 Gtk::SpinButton removeOverlapXGap;
448 Gtk::SpinButton removeOverlapYGap;
450 public:
451 ActionRemoveOverlaps(Glib::ustring const &id,
452 Glib::ustring const &tiptext,
453 guint row,
454 guint column,
455 AlignAndDistribute &dialog) :
456 Action(id, tiptext, row, column + 4,
457 dialog.removeOverlap_table(), dialog.tooltips(), dialog)
458 {
459 dialog.removeOverlap_table().set_col_spacings(3);
461 removeOverlapXGap.set_digits(1);
462 removeOverlapXGap.set_size_request(60, -1);
463 removeOverlapXGap.set_increments(1.0, 0);
464 removeOverlapXGap.set_range(-1000.0, 1000.0);
465 removeOverlapXGap.set_value(0);
466 dialog.tooltips().set_tip(removeOverlapXGap,
467 _("Minimum horizontal gap (in px units) between bounding boxes"));
468 //TRANSLATORS: only translate "string" in "context|string".
469 // For more details, see http://developer.gnome.org/doc/API/2.0/glib/glib-I18N.html#Q-:CAPS
470 // "H:" stands for horizontal gap
471 removeOverlapXGapLabel.set_label(Q_("gap|H:"));
473 removeOverlapYGap.set_digits(1);
474 removeOverlapYGap.set_size_request(60, -1);
475 removeOverlapYGap.set_increments(1.0, 0);
476 removeOverlapYGap.set_range(-1000.0, 1000.0);
477 removeOverlapYGap.set_value(0);
478 dialog.tooltips().set_tip(removeOverlapYGap,
479 _("Minimum vertical gap (in px units) between bounding boxes"));
480 /* TRANSLATORS: Vertical gap */
481 removeOverlapYGapLabel.set_label(_("V:"));
483 dialog.removeOverlap_table().attach(removeOverlapXGapLabel, column, column+1, row, row+1, Gtk::FILL, Gtk::FILL);
484 dialog.removeOverlap_table().attach(removeOverlapXGap, column+1, column+2, row, row+1, Gtk::FILL, Gtk::FILL);
485 dialog.removeOverlap_table().attach(removeOverlapYGapLabel, column+2, column+3, row, row+1, Gtk::FILL, Gtk::FILL);
486 dialog.removeOverlap_table().attach(removeOverlapYGap, column+3, column+4, row, row+1, Gtk::FILL, Gtk::FILL);
488 }
490 private :
491 virtual void on_button_click()
492 {
493 if (!_dialog.getDesktop()) return;
495 // see comment in ActionAlign above
496 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
497 int saved_compensation = prefs->getInt("/options/clonecompensation/value", SP_CLONE_COMPENSATION_UNMOVED);
498 prefs->setInt("/options/clonecompensation/value", SP_CLONE_COMPENSATION_UNMOVED);
500 // xGap and yGap are the minimum space required between bounding rectangles.
501 double const xGap = removeOverlapXGap.get_value();
502 double const yGap = removeOverlapYGap.get_value();
503 removeoverlap(sp_desktop_selection(_dialog.getDesktop())->itemList(),
504 xGap, yGap);
506 // restore compensation setting
507 prefs->setInt("/options/clonecompensation/value", saved_compensation);
509 sp_document_done(sp_desktop_document(_dialog.getDesktop()), SP_VERB_DIALOG_ALIGN_DISTRIBUTE,
510 _("Remove overlaps"));
511 }
512 };
514 class ActionGraphLayout : public Action {
515 public:
516 ActionGraphLayout(Glib::ustring const &id,
517 Glib::ustring const &tiptext,
518 guint row,
519 guint column,
520 AlignAndDistribute &dialog) :
521 Action(id, tiptext, row, column + 4,
522 dialog.graphLayout_table(), dialog.tooltips(), dialog)
523 {}
525 private :
526 virtual void on_button_click()
527 {
528 if (!_dialog.getDesktop()) return;
530 // see comment in ActionAlign above
531 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
532 int saved_compensation = prefs->getInt("/options/clonecompensation/value", SP_CLONE_COMPENSATION_UNMOVED);
533 prefs->setInt("/options/clonecompensation/value", SP_CLONE_COMPENSATION_UNMOVED);
535 graphlayout(sp_desktop_selection(_dialog.getDesktop())->itemList());
537 // restore compensation setting
538 prefs->setInt("/options/clonecompensation/value", saved_compensation);
540 sp_document_done(sp_desktop_document(_dialog.getDesktop()), SP_VERB_DIALOG_ALIGN_DISTRIBUTE,
541 _("Arrange connector network"));
542 }
543 };
545 class ActionUnclump : public Action {
546 public :
547 ActionUnclump(const Glib::ustring &id,
548 const Glib::ustring &tiptext,
549 guint row,
550 guint column,
551 AlignAndDistribute &dialog):
552 Action(id, tiptext, row, column,
553 dialog.distribute_table(), dialog.tooltips(), dialog)
554 {}
556 private :
557 virtual void on_button_click()
558 {
559 if (!_dialog.getDesktop()) return;
561 // see comment in ActionAlign above
562 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
563 int saved_compensation = prefs->getInt("/options/clonecompensation/value", SP_CLONE_COMPENSATION_UNMOVED);
564 prefs->setInt("/options/clonecompensation/value", SP_CLONE_COMPENSATION_UNMOVED);
566 unclump ((GSList *) sp_desktop_selection(_dialog.getDesktop())->itemList());
568 // restore compensation setting
569 prefs->setInt("/options/clonecompensation/value", saved_compensation);
571 sp_document_done (sp_desktop_document (_dialog.getDesktop()), SP_VERB_DIALOG_ALIGN_DISTRIBUTE,
572 _("Unclump"));
573 }
574 };
576 class ActionRandomize : public Action {
577 public :
578 ActionRandomize(const Glib::ustring &id,
579 const Glib::ustring &tiptext,
580 guint row,
581 guint column,
582 AlignAndDistribute &dialog):
583 Action(id, tiptext, row, column,
584 dialog.distribute_table(), dialog.tooltips(), dialog)
585 {}
587 private :
588 virtual void on_button_click()
589 {
590 SPDesktop *desktop = _dialog.getDesktop();
591 if (!desktop) return;
593 Inkscape::Selection *selection = sp_desktop_selection(desktop);
594 if (!selection) return;
596 using Inkscape::Util::GSListConstIterator;
597 std::list<SPItem *> selected;
598 selected.insert<GSListConstIterator<SPItem *> >(selected.end(), selection->itemList(), NULL);
599 if (selected.empty()) return;
601 //Check 2 or more selected objects
602 if (selected.size() < 2) return;
604 Geom::OptRect sel_bbox = selection->bounds();
605 if (!sel_bbox) {
606 return;
607 }
609 // This bbox is cached between calls to randomize, so that there's no growth nor shrink
610 // nor drift on sequential randomizations. Discard cache on global (or better active
611 // desktop's) selection_change signal.
612 if (!_dialog.randomize_bbox) {
613 _dialog.randomize_bbox = *sel_bbox;
614 }
616 // see comment in ActionAlign above
617 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
618 int saved_compensation = prefs->getInt("/options/clonecompensation/value", SP_CLONE_COMPENSATION_UNMOVED);
619 prefs->setInt("/options/clonecompensation/value", SP_CLONE_COMPENSATION_UNMOVED);
621 for (std::list<SPItem *>::iterator it(selected.begin());
622 it != selected.end();
623 ++it)
624 {
625 sp_document_ensure_up_to_date(sp_desktop_document (desktop));
626 Geom::OptRect item_box = sp_item_bbox_desktop (*it);
627 if (item_box) {
628 // find new center, staying within bbox
629 double x = _dialog.randomize_bbox->min()[Geom::X] + (*item_box)[Geom::X].extent() /2 +
630 g_random_double_range (0, (*_dialog.randomize_bbox)[Geom::X].extent() - (*item_box)[Geom::X].extent());
631 double y = _dialog.randomize_bbox->min()[Geom::Y] + (*item_box)[Geom::Y].extent()/2 +
632 g_random_double_range (0, (*_dialog.randomize_bbox)[Geom::Y].extent() - (*item_box)[Geom::Y].extent());
633 // displacement is the new center minus old:
634 Geom::Point t = Geom::Point (x, y) - 0.5*(item_box->max() + item_box->min());
635 sp_item_move_rel(*it, Geom::Translate(t));
636 }
637 }
639 // restore compensation setting
640 prefs->setInt("/options/clonecompensation/value", saved_compensation);
642 sp_document_done (sp_desktop_document (desktop), SP_VERB_DIALOG_ALIGN_DISTRIBUTE,
643 _("Randomize positions"));
644 }
645 };
647 struct Baselines
648 {
649 SPItem *_item;
650 Geom::Point _base;
651 Geom::Dim2 _orientation;
652 Baselines(SPItem *item, Geom::Point base, Geom::Dim2 orientation) :
653 _item (item),
654 _base (base),
655 _orientation (orientation)
656 {}
657 };
659 bool operator< (const Baselines &a, const Baselines &b)
660 {
661 return (a._base[a._orientation] < b._base[b._orientation]);
662 }
664 class ActionBaseline : public Action {
665 public :
666 ActionBaseline(const Glib::ustring &id,
667 const Glib::ustring &tiptext,
668 guint row,
669 guint column,
670 AlignAndDistribute &dialog,
671 Gtk::Table &table,
672 Geom::Dim2 orientation, bool distribute):
673 Action(id, tiptext, row, column,
674 table, dialog.tooltips(), dialog),
675 _orientation(orientation),
676 _distribute(distribute)
677 {}
679 private :
680 Geom::Dim2 _orientation;
681 bool _distribute;
682 virtual void on_button_click()
683 {
684 SPDesktop *desktop = _dialog.getDesktop();
685 if (!desktop) return;
687 Inkscape::Selection *selection = sp_desktop_selection(desktop);
688 if (!selection) return;
690 using Inkscape::Util::GSListConstIterator;
691 std::list<SPItem *> selected;
692 selected.insert<GSListConstIterator<SPItem *> >(selected.end(), selection->itemList(), NULL);
693 if (selected.empty()) return;
695 //Check 2 or more selected objects
696 if (selected.size() < 2) return;
698 Geom::Point b_min = Geom::Point (HUGE_VAL, HUGE_VAL);
699 Geom::Point b_max = Geom::Point (-HUGE_VAL, -HUGE_VAL);
701 std::vector<Baselines> sorted;
703 for (std::list<SPItem *>::iterator it(selected.begin());
704 it != selected.end();
705 ++it)
706 {
707 if (SP_IS_TEXT (*it) || SP_IS_FLOWTEXT (*it)) {
708 Inkscape::Text::Layout const *layout = te_get_layout(*it);
709 boost::optional<Geom::Point> pt = layout->baselineAnchorPoint();
710 if (pt) {
711 Geom::Point base = *pt * sp_item_i2d_affine(*it);
712 if (base[Geom::X] < b_min[Geom::X]) b_min[Geom::X] = base[Geom::X];
713 if (base[Geom::Y] < b_min[Geom::Y]) b_min[Geom::Y] = base[Geom::Y];
714 if (base[Geom::X] > b_max[Geom::X]) b_max[Geom::X] = base[Geom::X];
715 if (base[Geom::Y] > b_max[Geom::Y]) b_max[Geom::Y] = base[Geom::Y];
716 Baselines b (*it, base, _orientation);
717 sorted.push_back(b);
718 }
719 }
720 }
722 if (sorted.size() <= 1) return;
724 //sort baselines
725 std::sort(sorted.begin(), sorted.end());
727 bool changed = false;
729 if (_distribute) {
730 double step = (b_max[_orientation] - b_min[_orientation])/(sorted.size() - 1);
731 for (unsigned int i = 0; i < sorted.size(); i++) {
732 SPItem *item = sorted[i]._item;
733 Geom::Point base = sorted[i]._base;
734 Geom::Point t(0.0, 0.0);
735 t[_orientation] = b_min[_orientation] + step * i - base[_orientation];
736 sp_item_move_rel(item, Geom::Translate(t));
737 changed = true;
738 }
740 if (changed) {
741 sp_document_done (sp_desktop_document (desktop), SP_VERB_DIALOG_ALIGN_DISTRIBUTE,
742 _("Distribute text baselines"));
743 }
745 } else {
746 for (std::list<SPItem *>::iterator it(selected.begin());
747 it != selected.end();
748 ++it)
749 {
750 if (SP_IS_TEXT (*it) || SP_IS_FLOWTEXT (*it)) {
751 Inkscape::Text::Layout const *layout = te_get_layout(*it);
752 boost::optional<Geom::Point> pt = layout->baselineAnchorPoint();
753 if (pt) {
754 Geom::Point base = *pt * sp_item_i2d_affine(*it);
755 Geom::Point t(0.0, 0.0);
756 t[_orientation] = b_min[_orientation] - base[_orientation];
757 sp_item_move_rel(*it, Geom::Translate(t));
758 changed = true;
759 }
760 }
761 }
763 if (changed) {
764 sp_document_done (sp_desktop_document (desktop), SP_VERB_DIALOG_ALIGN_DISTRIBUTE,
765 _("Align text baselines"));
766 }
767 }
768 }
769 };
773 void on_tool_changed(Inkscape::Application */*inkscape*/, SPEventContext */*context*/, AlignAndDistribute *daad)
774 {
775 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
776 if (desktop && sp_desktop_event_context(desktop))
777 daad->setMode(tools_active(desktop) == TOOLS_NODES);
778 }
780 void on_selection_changed(Inkscape::Application */*inkscape*/, Inkscape::Selection */*selection*/, AlignAndDistribute *daad)
781 {
782 daad->randomize_bbox = Geom::OptRect();
783 }
785 /////////////////////////////////////////////////////////
790 AlignAndDistribute::AlignAndDistribute()
791 : UI::Widget::Panel ("", "/dialogs/align", SP_VERB_DIALOG_ALIGN_DISTRIBUTE),
792 randomize_bbox(),
793 _alignFrame(_("Align")),
794 _distributeFrame(_("Distribute")),
795 _removeOverlapFrame(_("Remove overlaps")),
796 _graphLayoutFrame(_("Connector network layout")),
797 _nodesFrame(_("Nodes")),
798 _alignTable(2, 6, true),
799 _distributeTable(3, 6, true),
800 _removeOverlapTable(1, 5, false),
801 _graphLayoutTable(1, 5, false),
802 _nodesTable(1, 4, true),
803 _anchorLabel(_("Relative to: ")),
804 _selgrpLabel(_("Treat selection as group: "))
805 {
806 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
808 //Instanciate the align buttons
809 addAlignButton(INKSCAPE_ICON_ALIGN_HORIZONTAL_RIGHT_TO_ANCHOR,
810 _("Align right edges of objects to the left edge of the anchor"),
811 0, 0);
812 addAlignButton(INKSCAPE_ICON_ALIGN_HORIZONTAL_LEFT,
813 _("Align left edges"),
814 0, 1);
815 addAlignButton(INKSCAPE_ICON_ALIGN_HORIZONTAL_CENTER,
816 _("Center on vertical axis"),
817 0, 2);
818 addAlignButton(INKSCAPE_ICON_ALIGN_HORIZONTAL_RIGHT,
819 _("Align right sides"),
820 0, 3);
821 addAlignButton(INKSCAPE_ICON_ALIGN_HORIZONTAL_LEFT_TO_ANCHOR,
822 _("Align left edges of objects to the right edge of the anchor"),
823 0, 4);
824 addAlignButton(INKSCAPE_ICON_ALIGN_VERTICAL_BOTTOM_TO_ANCHOR,
825 _("Align bottom edges of objects to the top edge of the anchor"),
826 1, 0);
827 addAlignButton(INKSCAPE_ICON_ALIGN_VERTICAL_TOP,
828 _("Align top edges"),
829 1, 1);
830 addAlignButton(INKSCAPE_ICON_ALIGN_VERTICAL_CENTER,
831 _("Center on horizontal axis"),
832 1, 2);
833 addAlignButton(INKSCAPE_ICON_ALIGN_VERTICAL_BOTTOM,
834 _("Align bottom edges"),
835 1, 3);
836 addAlignButton(INKSCAPE_ICON_ALIGN_VERTICAL_TOP_TO_ANCHOR,
837 _("Align top edges of objects to the bottom edge of the anchor"),
838 1, 4);
840 //Baseline aligns
841 addBaselineButton(INKSCAPE_ICON_ALIGN_HORIZONTAL_BASELINE,
842 _("Align baseline anchors of texts horizontally"),
843 0, 5, this->align_table(), Geom::X, false);
844 addBaselineButton(INKSCAPE_ICON_ALIGN_VERTICAL_BASELINE,
845 _("Align baselines of texts"),
846 1, 5, this->align_table(), Geom::Y, false);
848 //The distribute buttons
849 addDistributeButton(INKSCAPE_ICON_DISTRIBUTE_HORIZONTAL_GAPS,
850 _("Make horizontal gaps between objects equal"),
851 0, 4, true, Geom::X, .5, .5);
853 addDistributeButton(INKSCAPE_ICON_DISTRIBUTE_HORIZONTAL_LEFT,
854 _("Distribute left edges equidistantly"),
855 0, 1, false, Geom::X, 1., 0.);
856 addDistributeButton(INKSCAPE_ICON_DISTRIBUTE_HORIZONTAL_CENTER,
857 _("Distribute centers equidistantly horizontally"),
858 0, 2, false, Geom::X, .5, .5);
859 addDistributeButton(INKSCAPE_ICON_DISTRIBUTE_HORIZONTAL_RIGHT,
860 _("Distribute right edges equidistantly"),
861 0, 3, false, Geom::X, 0., 1.);
863 addDistributeButton(INKSCAPE_ICON_DISTRIBUTE_VERTICAL_GAPS,
864 _("Make vertical gaps between objects equal"),
865 1, 4, true, Geom::Y, .5, .5);
867 addDistributeButton(INKSCAPE_ICON_DISTRIBUTE_VERTICAL_TOP,
868 _("Distribute top edges equidistantly"),
869 1, 1, false, Geom::Y, 0, 1);
870 addDistributeButton(INKSCAPE_ICON_DISTRIBUTE_VERTICAL_CENTER,
871 _("Distribute centers equidistantly vertically"),
872 1, 2, false, Geom::Y, .5, .5);
873 addDistributeButton(INKSCAPE_ICON_DISTRIBUTE_VERTICAL_BOTTOM,
874 _("Distribute bottom edges equidistantly"),
875 1, 3, false, Geom::Y, 1., 0.);
877 //Baseline distribs
878 addBaselineButton(INKSCAPE_ICON_DISTRIBUTE_HORIZONTAL_BASELINE,
879 _("Distribute baseline anchors of texts horizontally"),
880 0, 5, this->distribute_table(), Geom::X, true);
881 addBaselineButton(INKSCAPE_ICON_DISTRIBUTE_VERTICAL_BASELINE,
882 _("Distribute baselines of texts vertically"),
883 1, 5, this->distribute_table(), Geom::Y, true);
885 //Randomize & Unclump
886 addRandomizeButton(INKSCAPE_ICON_DISTRIBUTE_RANDOMIZE,
887 _("Randomize centers in both dimensions"),
888 2, 2);
889 addUnclumpButton(INKSCAPE_ICON_DISTRIBUTE_UNCLUMP,
890 _("Unclump objects: try to equalize edge-to-edge distances"),
891 2, 4);
893 //Remove overlaps
894 addRemoveOverlapsButton(INKSCAPE_ICON_DISTRIBUTE_REMOVE_OVERLAPS,
895 _("Move objects as little as possible so that their bounding boxes do not overlap"),
896 0, 0);
897 //Graph Layout
898 addGraphLayoutButton(INKSCAPE_ICON_DISTRIBUTE_GRAPH,
899 _("Nicely arrange selected connector network"),
900 0, 0);
902 //Node Mode buttons
903 // NOTE: "align nodes vertically" means "move nodes vertically until they align on a common
904 // _horizontal_ line". This is analogous to what the "align-vertical-center" icon means.
905 // There is no doubt some ambiguity. For this reason the descriptions are different.
906 addNodeButton(INKSCAPE_ICON_ALIGN_VERTICAL_NODES,
907 _("Align selected nodes to a common horizontal line"),
908 0, Geom::X, false);
909 addNodeButton(INKSCAPE_ICON_ALIGN_HORIZONTAL_NODES,
910 _("Align selected nodes to a common vertical line"),
911 1, Geom::Y, false);
912 addNodeButton(INKSCAPE_ICON_DISTRIBUTE_HORIZONTAL_NODE,
913 _("Distribute selected nodes horizontally"),
914 2, Geom::X, true);
915 addNodeButton(INKSCAPE_ICON_DISTRIBUTE_VERTICAL_NODE,
916 _("Distribute selected nodes vertically"),
917 3, Geom::Y, true);
919 //Rest of the widgetry
921 _combo.append_text(_("Last selected"));
922 _combo.append_text(_("First selected"));
923 _combo.append_text(_("Biggest object"));
924 _combo.append_text(_("Smallest object"));
925 _combo.append_text(_("Page"));
926 _combo.append_text(_("Drawing"));
927 _combo.append_text(_("Selection"));
929 _combo.set_active(prefs->getInt("/dialogs/align/align-to", 6));
930 _combo.signal_changed().connect(sigc::mem_fun(*this, &AlignAndDistribute::on_ref_change));
932 _anchorBox.pack_start(_anchorLabel);
933 _anchorBox.pack_start(_combo);
935 _selgrpBox.pack_start(_selgrpLabel);
936 _selgrpBox.pack_start(_selgrp);
937 _selgrp.set_active(prefs->getBool("/dialogs/align/sel-as-groups"));
938 _selgrp.signal_toggled().connect(sigc::mem_fun(*this, &AlignAndDistribute::on_selgrp_toggled));
940 _alignBox.pack_start(_anchorBox);
941 _alignBox.pack_start(_selgrpBox);
942 _alignBox.pack_start(_alignTable);
944 _alignFrame.add(_alignBox);
945 _distributeFrame.add(_distributeTable);
946 _removeOverlapFrame.add(_removeOverlapTable);
947 _graphLayoutFrame.add(_graphLayoutTable);
948 _nodesFrame.add(_nodesTable);
950 Gtk::Box *contents = _getContents();
951 contents->set_spacing(4);
953 // Notebook for individual transformations
955 contents->pack_start(_alignFrame, true, true);
956 contents->pack_start(_distributeFrame, true, true);
957 contents->pack_start(_removeOverlapFrame, true, true);
958 contents->pack_start(_graphLayoutFrame, true, true);
959 contents->pack_start(_nodesFrame, true, true);
961 //Connect to the global tool change signal
962 g_signal_connect (G_OBJECT (INKSCAPE), "set_eventcontext", G_CALLBACK (on_tool_changed), this);
964 // Connect to the global selection change, to invalidate cached randomize_bbox
965 g_signal_connect (G_OBJECT (INKSCAPE), "change_selection", G_CALLBACK (on_selection_changed), this);
966 randomize_bbox = Geom::OptRect();
968 show_all_children();
970 on_tool_changed (NULL, NULL, this); // set current mode
971 }
973 AlignAndDistribute::~AlignAndDistribute()
974 {
975 sp_signal_disconnect_by_data (G_OBJECT (INKSCAPE), this);
977 for (std::list<Action *>::iterator it = _actionList.begin();
978 it != _actionList.end();
979 it ++)
980 delete *it;
981 }
983 void AlignAndDistribute::on_ref_change(){
984 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
985 prefs->setInt("/dialogs/align/align-to", _combo.get_active_row_number());
987 //Make blink the master
988 }
990 void AlignAndDistribute::on_selgrp_toggled(){
991 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
992 prefs->setInt("/dialogs/align/sel-as-groups", _selgrp.get_active());
994 //Make blink the master
995 }
1000 void AlignAndDistribute::setMode(bool nodeEdit)
1001 {
1002 //Act on widgets used in node mode
1003 void ( Gtk::Widget::*mNode) () = nodeEdit ?
1004 &Gtk::Widget::show_all : &Gtk::Widget::hide_all;
1006 //Act on widgets used in selection mode
1007 void ( Gtk::Widget::*mSel) () = nodeEdit ?
1008 &Gtk::Widget::hide_all : &Gtk::Widget::show_all;
1011 ((_alignFrame).*(mSel))();
1012 ((_distributeFrame).*(mSel))();
1013 ((_removeOverlapFrame).*(mSel))();
1014 ((_graphLayoutFrame).*(mSel))();
1015 ((_nodesFrame).*(mNode))();
1017 }
1018 void AlignAndDistribute::addAlignButton(const Glib::ustring &id, const Glib::ustring tiptext,
1019 guint row, guint col)
1020 {
1021 _actionList.push_back(
1022 new ActionAlign(
1023 id, tiptext, row, col,
1024 *this , col + row * 5));
1025 }
1026 void AlignAndDistribute::addDistributeButton(const Glib::ustring &id, const Glib::ustring tiptext,
1027 guint row, guint col, bool onInterSpace,
1028 Geom::Dim2 orientation, float kBegin, float kEnd)
1029 {
1030 _actionList.push_back(
1031 new ActionDistribute(
1032 id, tiptext, row, col, *this ,
1033 onInterSpace, orientation,
1034 kBegin, kEnd
1035 )
1036 );
1037 }
1039 void AlignAndDistribute::addNodeButton(const Glib::ustring &id, const Glib::ustring tiptext,
1040 guint col, Geom::Dim2 orientation, bool distribute)
1041 {
1042 _actionList.push_back(
1043 new ActionNode(
1044 id, tiptext, col,
1045 *this, orientation, distribute));
1046 }
1048 void AlignAndDistribute::addRemoveOverlapsButton(const Glib::ustring &id, const Glib::ustring tiptext,
1049 guint row, guint col)
1050 {
1051 _actionList.push_back(
1052 new ActionRemoveOverlaps(
1053 id, tiptext, row, col, *this)
1054 );
1055 }
1057 void AlignAndDistribute::addGraphLayoutButton(const Glib::ustring &id, const Glib::ustring tiptext,
1058 guint row, guint col)
1059 {
1060 _actionList.push_back(
1061 new ActionGraphLayout(
1062 id, tiptext, row, col, *this)
1063 );
1064 }
1066 void AlignAndDistribute::addUnclumpButton(const Glib::ustring &id, const Glib::ustring tiptext,
1067 guint row, guint col)
1068 {
1069 _actionList.push_back(
1070 new ActionUnclump(
1071 id, tiptext, row, col, *this)
1072 );
1073 }
1075 void AlignAndDistribute::addRandomizeButton(const Glib::ustring &id, const Glib::ustring tiptext,
1076 guint row, guint col)
1077 {
1078 _actionList.push_back(
1079 new ActionRandomize(
1080 id, tiptext, row, col, *this)
1081 );
1082 }
1084 void AlignAndDistribute::addBaselineButton(const Glib::ustring &id, const Glib::ustring tiptext,
1085 guint row, guint col, Gtk::Table &table, Geom::Dim2 orientation, bool distribute)
1086 {
1087 _actionList.push_back(
1088 new ActionBaseline(
1089 id, tiptext, row, col,
1090 *this, table, orientation, distribute));
1091 }
1096 std::list<SPItem *>::iterator AlignAndDistribute::find_master( std::list<SPItem *> &list, bool horizontal){
1097 std::list<SPItem *>::iterator master = list.end();
1098 switch (getAlignTarget()) {
1099 case LAST:
1100 return list.begin();
1101 break;
1103 case FIRST:
1104 return --(list.end());
1105 break;
1107 case BIGGEST:
1108 {
1109 gdouble max = -1e18;
1110 for (std::list<SPItem *>::iterator it = list.begin(); it != list.end(); it++) {
1111 Geom::OptRect b = sp_item_bbox_desktop (*it);
1112 if (b) {
1113 gdouble dim = (*b)[horizontal ? Geom::X : Geom::Y].extent();
1114 if (dim > max) {
1115 max = dim;
1116 master = it;
1117 }
1118 }
1119 }
1120 return master;
1121 break;
1122 }
1124 case SMALLEST:
1125 {
1126 gdouble max = 1e18;
1127 for (std::list<SPItem *>::iterator it = list.begin(); it != list.end(); it++) {
1128 Geom::OptRect b = sp_item_bbox_desktop (*it);
1129 if (b) {
1130 gdouble dim = (*b)[horizontal ? Geom::X : Geom::Y].extent();
1131 if (dim < max) {
1132 max = dim;
1133 master = it;
1134 }
1135 }
1136 }
1137 return master;
1138 break;
1139 }
1141 default:
1142 g_assert_not_reached ();
1143 break;
1145 } // end of switch statement
1146 return master;
1147 }
1149 AlignAndDistribute::AlignTarget AlignAndDistribute::getAlignTarget()const {
1150 return AlignTarget(_combo.get_active_row_number());
1151 }
1155 } // namespace Dialog
1156 } // namespace UI
1157 } // namespace Inkscape
1159 /*
1160 Local Variables:
1161 mode:c++
1162 c-file-style:"stroustrup"
1163 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1164 indent-tabs-mode:nil
1165 fill-column:99
1166 End:
1167 */
1168 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :