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 "node-context.h" //For access to ShapeEditor
31 #include "preferences.h"
32 #include "removeoverlap/removeoverlap.h"
33 #include "selection.h"
34 #include "shape-editor.h" //For node align/distribute methods
35 #include "sp-flowtext.h"
36 #include "sp-item-transform.h"
37 #include "sp-text.h"
38 #include "text-editing.h"
39 #include "tools-switch.h"
40 #include "ui/icon-names.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 (!SP_IS_NODE_CONTEXT (event_context)) return ;
434 if (_distribute)
435 event_context->shape_editor->distribute((Geom::Dim2)_orientation);
436 else
437 event_context->shape_editor->align((Geom::Dim2)_orientation);
439 }
440 };
442 class ActionRemoveOverlaps : public Action {
443 private:
444 Gtk::Label removeOverlapXGapLabel;
445 Gtk::Label removeOverlapYGapLabel;
446 Gtk::SpinButton removeOverlapXGap;
447 Gtk::SpinButton removeOverlapYGap;
449 public:
450 ActionRemoveOverlaps(Glib::ustring const &id,
451 Glib::ustring const &tiptext,
452 guint row,
453 guint column,
454 AlignAndDistribute &dialog) :
455 Action(id, tiptext, row, column + 4,
456 dialog.removeOverlap_table(), dialog.tooltips(), dialog)
457 {
458 dialog.removeOverlap_table().set_col_spacings(3);
460 removeOverlapXGap.set_digits(1);
461 removeOverlapXGap.set_size_request(60, -1);
462 removeOverlapXGap.set_increments(1.0, 0);
463 removeOverlapXGap.set_range(-1000.0, 1000.0);
464 removeOverlapXGap.set_value(0);
465 dialog.tooltips().set_tip(removeOverlapXGap,
466 _("Minimum horizontal gap (in px units) between bounding boxes"));
467 /* TRANSLATORS: Horizontal gap. Only put "H:" equivalent in the translation */
468 removeOverlapXGapLabel.set_label(Q_("gap|H:"));
470 removeOverlapYGap.set_digits(1);
471 removeOverlapYGap.set_size_request(60, -1);
472 removeOverlapYGap.set_increments(1.0, 0);
473 removeOverlapYGap.set_range(-1000.0, 1000.0);
474 removeOverlapYGap.set_value(0);
475 dialog.tooltips().set_tip(removeOverlapYGap,
476 _("Minimum vertical gap (in px units) between bounding boxes"));
477 /* TRANSLATORS: Vertical gap */
478 removeOverlapYGapLabel.set_label(_("V:"));
480 dialog.removeOverlap_table().attach(removeOverlapXGapLabel, column, column+1, row, row+1, Gtk::FILL, Gtk::FILL);
481 dialog.removeOverlap_table().attach(removeOverlapXGap, column+1, column+2, row, row+1, Gtk::FILL, Gtk::FILL);
482 dialog.removeOverlap_table().attach(removeOverlapYGapLabel, column+2, column+3, row, row+1, Gtk::FILL, Gtk::FILL);
483 dialog.removeOverlap_table().attach(removeOverlapYGap, column+3, column+4, row, row+1, Gtk::FILL, Gtk::FILL);
485 }
487 private :
488 virtual void on_button_click()
489 {
490 if (!_dialog.getDesktop()) return;
492 // see comment in ActionAlign above
493 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
494 int saved_compensation = prefs->getInt("/options/clonecompensation/value", SP_CLONE_COMPENSATION_UNMOVED);
495 prefs->setInt("/options/clonecompensation/value", SP_CLONE_COMPENSATION_UNMOVED);
497 // xGap and yGap are the minimum space required between bounding rectangles.
498 double const xGap = removeOverlapXGap.get_value();
499 double const yGap = removeOverlapYGap.get_value();
500 removeoverlap(sp_desktop_selection(_dialog.getDesktop())->itemList(),
501 xGap, yGap);
503 // restore compensation setting
504 prefs->setInt("/options/clonecompensation/value", saved_compensation);
506 sp_document_done(sp_desktop_document(_dialog.getDesktop()), SP_VERB_DIALOG_ALIGN_DISTRIBUTE,
507 _("Remove overlaps"));
508 }
509 };
511 class ActionGraphLayout : public Action {
512 public:
513 ActionGraphLayout(Glib::ustring const &id,
514 Glib::ustring const &tiptext,
515 guint row,
516 guint column,
517 AlignAndDistribute &dialog) :
518 Action(id, tiptext, row, column + 4,
519 dialog.graphLayout_table(), dialog.tooltips(), dialog)
520 {}
522 private :
523 virtual void on_button_click()
524 {
525 if (!_dialog.getDesktop()) return;
527 // see comment in ActionAlign above
528 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
529 int saved_compensation = prefs->getInt("/options/clonecompensation/value", SP_CLONE_COMPENSATION_UNMOVED);
530 prefs->setInt("/options/clonecompensation/value", SP_CLONE_COMPENSATION_UNMOVED);
532 graphlayout(sp_desktop_selection(_dialog.getDesktop())->itemList());
534 // restore compensation setting
535 prefs->setInt("/options/clonecompensation/value", saved_compensation);
537 sp_document_done(sp_desktop_document(_dialog.getDesktop()), SP_VERB_DIALOG_ALIGN_DISTRIBUTE,
538 _("Arrange connector network"));
539 }
540 };
542 class ActionUnclump : public Action {
543 public :
544 ActionUnclump(const Glib::ustring &id,
545 const Glib::ustring &tiptext,
546 guint row,
547 guint column,
548 AlignAndDistribute &dialog):
549 Action(id, tiptext, row, column,
550 dialog.distribute_table(), dialog.tooltips(), dialog)
551 {}
553 private :
554 virtual void on_button_click()
555 {
556 if (!_dialog.getDesktop()) return;
558 // see comment in ActionAlign above
559 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
560 int saved_compensation = prefs->getInt("/options/clonecompensation/value", SP_CLONE_COMPENSATION_UNMOVED);
561 prefs->setInt("/options/clonecompensation/value", SP_CLONE_COMPENSATION_UNMOVED);
563 unclump ((GSList *) sp_desktop_selection(_dialog.getDesktop())->itemList());
565 // restore compensation setting
566 prefs->setInt("/options/clonecompensation/value", saved_compensation);
568 sp_document_done (sp_desktop_document (_dialog.getDesktop()), SP_VERB_DIALOG_ALIGN_DISTRIBUTE,
569 _("Unclump"));
570 }
571 };
573 class ActionRandomize : public Action {
574 public :
575 ActionRandomize(const Glib::ustring &id,
576 const Glib::ustring &tiptext,
577 guint row,
578 guint column,
579 AlignAndDistribute &dialog):
580 Action(id, tiptext, row, column,
581 dialog.distribute_table(), dialog.tooltips(), dialog)
582 {}
584 private :
585 virtual void on_button_click()
586 {
587 SPDesktop *desktop = _dialog.getDesktop();
588 if (!desktop) return;
590 Inkscape::Selection *selection = sp_desktop_selection(desktop);
591 if (!selection) return;
593 using Inkscape::Util::GSListConstIterator;
594 std::list<SPItem *> selected;
595 selected.insert<GSListConstIterator<SPItem *> >(selected.end(), selection->itemList(), NULL);
596 if (selected.empty()) return;
598 //Check 2 or more selected objects
599 if (selected.size() < 2) return;
601 Geom::OptRect sel_bbox = selection->bounds();
602 if (!sel_bbox) {
603 return;
604 }
606 // This bbox is cached between calls to randomize, so that there's no growth nor shrink
607 // nor drift on sequential randomizations. Discard cache on global (or better active
608 // desktop's) selection_change signal.
609 if (!_dialog.randomize_bbox) {
610 _dialog.randomize_bbox = *sel_bbox;
611 }
613 // see comment in ActionAlign above
614 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
615 int saved_compensation = prefs->getInt("/options/clonecompensation/value", SP_CLONE_COMPENSATION_UNMOVED);
616 prefs->setInt("/options/clonecompensation/value", SP_CLONE_COMPENSATION_UNMOVED);
618 for (std::list<SPItem *>::iterator it(selected.begin());
619 it != selected.end();
620 ++it)
621 {
622 sp_document_ensure_up_to_date(sp_desktop_document (desktop));
623 Geom::OptRect item_box = sp_item_bbox_desktop (*it);
624 if (item_box) {
625 // find new center, staying within bbox
626 double x = _dialog.randomize_bbox->min()[Geom::X] + (*item_box)[Geom::X].extent() /2 +
627 g_random_double_range (0, (*_dialog.randomize_bbox)[Geom::X].extent() - (*item_box)[Geom::X].extent());
628 double y = _dialog.randomize_bbox->min()[Geom::Y] + (*item_box)[Geom::Y].extent()/2 +
629 g_random_double_range (0, (*_dialog.randomize_bbox)[Geom::Y].extent() - (*item_box)[Geom::Y].extent());
630 // displacement is the new center minus old:
631 Geom::Point t = Geom::Point (x, y) - 0.5*(item_box->max() + item_box->min());
632 sp_item_move_rel(*it, Geom::Translate(t));
633 }
634 }
636 // restore compensation setting
637 prefs->setInt("/options/clonecompensation/value", saved_compensation);
639 sp_document_done (sp_desktop_document (desktop), SP_VERB_DIALOG_ALIGN_DISTRIBUTE,
640 _("Randomize positions"));
641 }
642 };
644 struct Baselines
645 {
646 SPItem *_item;
647 Geom::Point _base;
648 Geom::Dim2 _orientation;
649 Baselines(SPItem *item, Geom::Point base, Geom::Dim2 orientation) :
650 _item (item),
651 _base (base),
652 _orientation (orientation)
653 {}
654 };
656 bool operator< (const Baselines &a, const Baselines &b)
657 {
658 return (a._base[a._orientation] < b._base[b._orientation]);
659 }
661 class ActionBaseline : public Action {
662 public :
663 ActionBaseline(const Glib::ustring &id,
664 const Glib::ustring &tiptext,
665 guint row,
666 guint column,
667 AlignAndDistribute &dialog,
668 Gtk::Table &table,
669 Geom::Dim2 orientation, bool distribute):
670 Action(id, tiptext, row, column,
671 table, dialog.tooltips(), dialog),
672 _orientation(orientation),
673 _distribute(distribute)
674 {}
676 private :
677 Geom::Dim2 _orientation;
678 bool _distribute;
679 virtual void on_button_click()
680 {
681 SPDesktop *desktop = _dialog.getDesktop();
682 if (!desktop) return;
684 Inkscape::Selection *selection = sp_desktop_selection(desktop);
685 if (!selection) return;
687 using Inkscape::Util::GSListConstIterator;
688 std::list<SPItem *> selected;
689 selected.insert<GSListConstIterator<SPItem *> >(selected.end(), selection->itemList(), NULL);
690 if (selected.empty()) return;
692 //Check 2 or more selected objects
693 if (selected.size() < 2) return;
695 Geom::Point b_min = Geom::Point (HUGE_VAL, HUGE_VAL);
696 Geom::Point b_max = Geom::Point (-HUGE_VAL, -HUGE_VAL);
698 std::vector<Baselines> sorted;
700 for (std::list<SPItem *>::iterator it(selected.begin());
701 it != selected.end();
702 ++it)
703 {
704 if (SP_IS_TEXT (*it) || SP_IS_FLOWTEXT (*it)) {
705 Inkscape::Text::Layout const *layout = te_get_layout(*it);
706 Geom::Point base = layout->characterAnchorPoint(layout->begin()) * sp_item_i2d_affine(*it);
707 if (base[Geom::X] < b_min[Geom::X]) b_min[Geom::X] = base[Geom::X];
708 if (base[Geom::Y] < b_min[Geom::Y]) b_min[Geom::Y] = base[Geom::Y];
709 if (base[Geom::X] > b_max[Geom::X]) b_max[Geom::X] = base[Geom::X];
710 if (base[Geom::Y] > b_max[Geom::Y]) b_max[Geom::Y] = base[Geom::Y];
712 Baselines b (*it, base, _orientation);
713 sorted.push_back(b);
714 }
715 }
717 if (sorted.size() <= 1) return;
719 //sort baselines
720 std::sort(sorted.begin(), sorted.end());
722 bool changed = false;
724 if (_distribute) {
725 double step = (b_max[_orientation] - b_min[_orientation])/(sorted.size() - 1);
726 for (unsigned int i = 0; i < sorted.size(); i++) {
727 SPItem *item = sorted[i]._item;
728 Geom::Point base = sorted[i]._base;
729 Geom::Point t(0.0, 0.0);
730 t[_orientation] = b_min[_orientation] + step * i - base[_orientation];
731 sp_item_move_rel(item, Geom::Translate(t));
732 changed = true;
733 }
735 if (changed) {
736 sp_document_done (sp_desktop_document (desktop), SP_VERB_DIALOG_ALIGN_DISTRIBUTE,
737 _("Distribute text baselines"));
738 }
740 } else {
741 for (std::list<SPItem *>::iterator it(selected.begin());
742 it != selected.end();
743 ++it)
744 {
745 if (SP_IS_TEXT (*it) || SP_IS_FLOWTEXT (*it)) {
746 Inkscape::Text::Layout const *layout = te_get_layout(*it);
747 Geom::Point base = layout->characterAnchorPoint(layout->begin()) * sp_item_i2d_affine(*it);
748 Geom::Point t(0.0, 0.0);
749 t[_orientation] = b_min[_orientation] - base[_orientation];
750 sp_item_move_rel(*it, Geom::Translate(t));
751 changed = true;
752 }
753 }
755 if (changed) {
756 sp_document_done (sp_desktop_document (desktop), SP_VERB_DIALOG_ALIGN_DISTRIBUTE,
757 _("Align text baselines"));
758 }
759 }
760 }
761 };
765 void on_tool_changed(Inkscape::Application */*inkscape*/, SPEventContext */*context*/, AlignAndDistribute *daad)
766 {
767 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
768 if (desktop && sp_desktop_event_context(desktop))
769 daad->setMode(tools_active(desktop) == TOOLS_NODES);
770 }
772 void on_selection_changed(Inkscape::Application */*inkscape*/, Inkscape::Selection */*selection*/, AlignAndDistribute *daad)
773 {
774 daad->randomize_bbox = Geom::OptRect();
775 }
777 /////////////////////////////////////////////////////////
782 AlignAndDistribute::AlignAndDistribute()
783 : UI::Widget::Panel ("", "/dialogs/align", SP_VERB_DIALOG_ALIGN_DISTRIBUTE),
784 randomize_bbox(),
785 _alignFrame(_("Align")),
786 _distributeFrame(_("Distribute")),
787 _removeOverlapFrame(_("Remove overlaps")),
788 _graphLayoutFrame(_("Connector network layout")),
789 _nodesFrame(_("Nodes")),
790 _alignTable(2, 6, true),
791 _distributeTable(3, 6, true),
792 _removeOverlapTable(1, 5, false),
793 _graphLayoutTable(1, 5, false),
794 _nodesTable(1, 4, true),
795 _anchorLabel(_("Relative to: ")),
796 _selgrpLabel(_("Treat selection as group: "))
797 {
798 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
800 //Instanciate the align buttons
801 addAlignButton(INKSCAPE_ICON_ALIGN_HORIZONTAL_RIGHT_TO_ANCHOR,
802 _("Align right edges of objects to the left edge of the anchor"),
803 0, 0);
804 addAlignButton(INKSCAPE_ICON_ALIGN_HORIZONTAL_LEFT,
805 _("Align left edges"),
806 0, 1);
807 addAlignButton(INKSCAPE_ICON_ALIGN_HORIZONTAL_CENTER,
808 _("Center objects horizontally"),
809 0, 2);
810 addAlignButton(INKSCAPE_ICON_ALIGN_HORIZONTAL_RIGHT,
811 _("Align right sides"),
812 0, 3);
813 addAlignButton(INKSCAPE_ICON_ALIGN_HORIZONTAL_LEFT_TO_ANCHOR,
814 _("Align left edges of objects to the right edge of the anchor"),
815 0, 4);
816 addAlignButton(INKSCAPE_ICON_ALIGN_VERTICAL_BOTTOM_TO_ANCHOR,
817 _("Align bottom edges of objects to the top edge of the anchor"),
818 1, 0);
819 addAlignButton(INKSCAPE_ICON_ALIGN_VERTICAL_TOP,
820 _("Align top edges"),
821 1, 1);
822 addAlignButton(INKSCAPE_ICON_ALIGN_VERTICAL_CENTER,
823 _("Center on horizontal axis"),
824 1, 2);
825 addAlignButton(INKSCAPE_ICON_ALIGN_VERTICAL_BOTTOM,
826 _("Align bottom edges"),
827 1, 3);
828 addAlignButton(INKSCAPE_ICON_ALIGN_VERTICAL_TOP_TO_ANCHOR,
829 _("Align top edges of objects to the bottom edge of the anchor"),
830 1, 4);
832 //Baseline aligns
833 addBaselineButton(INKSCAPE_ICON_ALIGN_HORIZONTAL_BASELINE,
834 _("Align baseline anchors of texts horizontally"),
835 0, 5, this->align_table(), Geom::X, false);
836 addBaselineButton(INKSCAPE_ICON_ALIGN_VERTICAL_BASELINE,
837 _("Align baselines of texts"),
838 1, 5, this->align_table(), Geom::Y, false);
840 //The distribute buttons
841 addDistributeButton(INKSCAPE_ICON_DISTRIBUTE_HORIZONTAL_GAPS,
842 _("Make horizontal gaps between objects equal"),
843 0, 4, true, Geom::X, .5, .5);
845 addDistributeButton(INKSCAPE_ICON_DISTRIBUTE_HORIZONTAL_LEFT,
846 _("Distribute left edges equidistantly"),
847 0, 1, false, Geom::X, 1., 0.);
848 addDistributeButton(INKSCAPE_ICON_DISTRIBUTE_HORIZONTAL_CENTER,
849 _("Distribute centers equidistantly horizontally"),
850 0, 2, false, Geom::X, .5, .5);
851 addDistributeButton(INKSCAPE_ICON_DISTRIBUTE_HORIZONTAL_RIGHT,
852 _("Distribute right edges equidistantly"),
853 0, 3, false, Geom::X, 0., 1.);
855 addDistributeButton(INKSCAPE_ICON_DISTRIBUTE_VERTICAL_GAPS,
856 _("Make vertical gaps between objects equal"),
857 1, 4, true, Geom::Y, .5, .5);
859 addDistributeButton(INKSCAPE_ICON_DISTRIBUTE_VERTICAL_TOP,
860 _("Distribute top edges equidistantly"),
861 1, 1, false, Geom::Y, 0, 1);
862 addDistributeButton(INKSCAPE_ICON_DISTRIBUTE_VERTICAL_CENTER,
863 _("Distribute centers equidistantly vertically"),
864 1, 2, false, Geom::Y, .5, .5);
865 addDistributeButton(INKSCAPE_ICON_DISTRIBUTE_VERTICAL_BOTTOM,
866 _("Distribute bottom edges equidistantly"),
867 1, 3, false, Geom::Y, 1., 0.);
869 //Baseline distribs
870 addBaselineButton(INKSCAPE_ICON_DISTRIBUTE_HORIZONTAL_BASELINE,
871 _("Distribute baseline anchors of texts horizontally"),
872 0, 5, this->distribute_table(), Geom::X, true);
873 addBaselineButton(INKSCAPE_ICON_DISTRIBUTE_VERTICAL_BASELINE,
874 _("Distribute baselines of texts vertically"),
875 1, 5, this->distribute_table(), Geom::Y, true);
877 //Randomize & Unclump
878 addRandomizeButton(INKSCAPE_ICON_DISTRIBUTE_RANDOMIZE,
879 _("Randomize centers in both dimensions"),
880 2, 2);
881 addUnclumpButton(INKSCAPE_ICON_DISTRIBUTE_UNCLUMP,
882 _("Unclump objects: try to equalize edge-to-edge distances"),
883 2, 4);
885 //Remove overlaps
886 addRemoveOverlapsButton(INKSCAPE_ICON_DISTRIBUTE_REMOVE_OVERLAPS,
887 _("Move objects as little as possible so that their bounding boxes do not overlap"),
888 0, 0);
889 //Graph Layout
890 addGraphLayoutButton(INKSCAPE_ICON_DISTRIBUTE_GRAPH,
891 _("Nicely arrange selected connector network"),
892 0, 0);
894 //Node Mode buttons
895 addNodeButton(INKSCAPE_ICON_ALIGN_HORIZONTAL_NODES,
896 _("Align selected nodes horizontally"),
897 0, Geom::X, false);
898 addNodeButton(INKSCAPE_ICON_ALIGN_VERTICAL_NODES,
899 _("Align selected nodes vertically"),
900 1, Geom::Y, false);
901 addNodeButton(INKSCAPE_ICON_DISTRIBUTE_HORIZONTAL_NODE,
902 _("Distribute selected nodes horizontally"),
903 2, Geom::X, true);
904 addNodeButton(INKSCAPE_ICON_DISTRIBUTE_VERTICAL_NODE,
905 _("Distribute selected nodes vertically"),
906 3, Geom::Y, true);
908 //Rest of the widgetry
910 _combo.append_text(_("Last selected"));
911 _combo.append_text(_("First selected"));
912 _combo.append_text(_("Biggest item"));
913 _combo.append_text(_("Smallest item"));
914 _combo.append_text(_("Page"));
915 _combo.append_text(_("Drawing"));
916 _combo.append_text(_("Selection"));
918 _combo.set_active(prefs->getInt("/dialogs/align/align-to", 6));
919 _combo.signal_changed().connect(sigc::mem_fun(*this, &AlignAndDistribute::on_ref_change));
921 _anchorBox.pack_start(_anchorLabel);
922 _anchorBox.pack_start(_combo);
924 _selgrpBox.pack_start(_selgrpLabel);
925 _selgrpBox.pack_start(_selgrp);
926 _selgrp.set_active(prefs->getBool("/dialogs/align/sel-as-groups"));
927 _selgrp.signal_toggled().connect(sigc::mem_fun(*this, &AlignAndDistribute::on_selgrp_toggled));
929 _alignBox.pack_start(_anchorBox);
930 _alignBox.pack_start(_selgrpBox);
931 _alignBox.pack_start(_alignTable);
933 _alignFrame.add(_alignBox);
934 _distributeFrame.add(_distributeTable);
935 _removeOverlapFrame.add(_removeOverlapTable);
936 _graphLayoutFrame.add(_graphLayoutTable);
937 _nodesFrame.add(_nodesTable);
939 Gtk::Box *contents = _getContents();
940 contents->set_spacing(4);
942 // Notebook for individual transformations
944 contents->pack_start(_alignFrame, true, true);
945 contents->pack_start(_distributeFrame, true, true);
946 contents->pack_start(_removeOverlapFrame, true, true);
947 contents->pack_start(_graphLayoutFrame, true, true);
948 contents->pack_start(_nodesFrame, true, true);
950 //Connect to the global tool change signal
951 g_signal_connect (G_OBJECT (INKSCAPE), "set_eventcontext", G_CALLBACK (on_tool_changed), this);
953 // Connect to the global selection change, to invalidate cached randomize_bbox
954 g_signal_connect (G_OBJECT (INKSCAPE), "change_selection", G_CALLBACK (on_selection_changed), this);
955 randomize_bbox = Geom::OptRect();
957 show_all_children();
959 on_tool_changed (NULL, NULL, this); // set current mode
960 }
962 AlignAndDistribute::~AlignAndDistribute()
963 {
964 sp_signal_disconnect_by_data (G_OBJECT (INKSCAPE), this);
966 for (std::list<Action *>::iterator it = _actionList.begin();
967 it != _actionList.end();
968 it ++)
969 delete *it;
970 }
972 void AlignAndDistribute::on_ref_change(){
973 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
974 prefs->setInt("/dialogs/align/align-to", _combo.get_active_row_number());
976 //Make blink the master
977 }
979 void AlignAndDistribute::on_selgrp_toggled(){
980 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
981 prefs->setInt("/dialogs/align/sel-as-groups", _selgrp.get_active());
983 //Make blink the master
984 }
989 void AlignAndDistribute::setMode(bool nodeEdit)
990 {
991 //Act on widgets used in node mode
992 void ( Gtk::Widget::*mNode) () = nodeEdit ?
993 &Gtk::Widget::show_all : &Gtk::Widget::hide_all;
995 //Act on widgets used in selection mode
996 void ( Gtk::Widget::*mSel) () = nodeEdit ?
997 &Gtk::Widget::hide_all : &Gtk::Widget::show_all;
1000 ((_alignFrame).*(mSel))();
1001 ((_distributeFrame).*(mSel))();
1002 ((_removeOverlapFrame).*(mSel))();
1003 ((_graphLayoutFrame).*(mSel))();
1004 ((_nodesFrame).*(mNode))();
1006 }
1007 void AlignAndDistribute::addAlignButton(const Glib::ustring &id, const Glib::ustring tiptext,
1008 guint row, guint col)
1009 {
1010 _actionList.push_back(
1011 new ActionAlign(
1012 id, tiptext, row, col,
1013 *this , col + row * 5));
1014 }
1015 void AlignAndDistribute::addDistributeButton(const Glib::ustring &id, const Glib::ustring tiptext,
1016 guint row, guint col, bool onInterSpace,
1017 Geom::Dim2 orientation, float kBegin, float kEnd)
1018 {
1019 _actionList.push_back(
1020 new ActionDistribute(
1021 id, tiptext, row, col, *this ,
1022 onInterSpace, orientation,
1023 kBegin, kEnd
1024 )
1025 );
1026 }
1028 void AlignAndDistribute::addNodeButton(const Glib::ustring &id, const Glib::ustring tiptext,
1029 guint col, Geom::Dim2 orientation, bool distribute)
1030 {
1031 _actionList.push_back(
1032 new ActionNode(
1033 id, tiptext, col,
1034 *this, orientation, distribute));
1035 }
1037 void AlignAndDistribute::addRemoveOverlapsButton(const Glib::ustring &id, const Glib::ustring tiptext,
1038 guint row, guint col)
1039 {
1040 _actionList.push_back(
1041 new ActionRemoveOverlaps(
1042 id, tiptext, row, col, *this)
1043 );
1044 }
1046 void AlignAndDistribute::addGraphLayoutButton(const Glib::ustring &id, const Glib::ustring tiptext,
1047 guint row, guint col)
1048 {
1049 _actionList.push_back(
1050 new ActionGraphLayout(
1051 id, tiptext, row, col, *this)
1052 );
1053 }
1055 void AlignAndDistribute::addUnclumpButton(const Glib::ustring &id, const Glib::ustring tiptext,
1056 guint row, guint col)
1057 {
1058 _actionList.push_back(
1059 new ActionUnclump(
1060 id, tiptext, row, col, *this)
1061 );
1062 }
1064 void AlignAndDistribute::addRandomizeButton(const Glib::ustring &id, const Glib::ustring tiptext,
1065 guint row, guint col)
1066 {
1067 _actionList.push_back(
1068 new ActionRandomize(
1069 id, tiptext, row, col, *this)
1070 );
1071 }
1073 void AlignAndDistribute::addBaselineButton(const Glib::ustring &id, const Glib::ustring tiptext,
1074 guint row, guint col, Gtk::Table &table, Geom::Dim2 orientation, bool distribute)
1075 {
1076 _actionList.push_back(
1077 new ActionBaseline(
1078 id, tiptext, row, col,
1079 *this, table, orientation, distribute));
1080 }
1085 std::list<SPItem *>::iterator AlignAndDistribute::find_master( std::list<SPItem *> &list, bool horizontal){
1086 std::list<SPItem *>::iterator master = list.end();
1087 switch (getAlignTarget()) {
1088 case LAST:
1089 return list.begin();
1090 break;
1092 case FIRST:
1093 return --(list.end());
1094 break;
1096 case BIGGEST:
1097 {
1098 gdouble max = -1e18;
1099 for (std::list<SPItem *>::iterator it = list.begin(); it != list.end(); it++) {
1100 Geom::OptRect b = sp_item_bbox_desktop (*it);
1101 if (b) {
1102 gdouble dim = (*b)[horizontal ? Geom::X : Geom::Y].extent();
1103 if (dim > max) {
1104 max = dim;
1105 master = it;
1106 }
1107 }
1108 }
1109 return master;
1110 break;
1111 }
1113 case SMALLEST:
1114 {
1115 gdouble max = 1e18;
1116 for (std::list<SPItem *>::iterator it = list.begin(); it != list.end(); it++) {
1117 Geom::OptRect b = sp_item_bbox_desktop (*it);
1118 if (b) {
1119 gdouble dim = (*b)[horizontal ? Geom::X : Geom::Y].extent();
1120 if (dim < max) {
1121 max = dim;
1122 master = it;
1123 }
1124 }
1125 }
1126 return master;
1127 break;
1128 }
1130 default:
1131 g_assert_not_reached ();
1132 break;
1134 } // end of switch statement
1135 return master;
1136 }
1138 AlignAndDistribute::AlignTarget AlignAndDistribute::getAlignTarget()const {
1139 return AlignTarget(_combo.get_active_row_number());
1140 }
1144 } // namespace Dialog
1145 } // namespace UI
1146 } // namespace Inkscape
1148 /*
1149 Local Variables:
1150 mode:c++
1151 c-file-style:"stroustrup"
1152 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1153 indent-tabs-mode:nil
1154 fill-column:99
1155 End:
1156 */
1157 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :