a54f83758db8fd316ed4647d721341fdd9a384a2
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: only translate "string" in "context|string".
468 // For more details, see http://developer.gnome.org/doc/API/2.0/glib/glib-I18N.html#Q-:CAPS
469 // "H:" stands for horizontal gap
470 removeOverlapXGapLabel.set_label(Q_("gap|H:"));
472 removeOverlapYGap.set_digits(1);
473 removeOverlapYGap.set_size_request(60, -1);
474 removeOverlapYGap.set_increments(1.0, 0);
475 removeOverlapYGap.set_range(-1000.0, 1000.0);
476 removeOverlapYGap.set_value(0);
477 dialog.tooltips().set_tip(removeOverlapYGap,
478 _("Minimum vertical gap (in px units) between bounding boxes"));
479 /* TRANSLATORS: Vertical gap */
480 removeOverlapYGapLabel.set_label(_("V:"));
482 dialog.removeOverlap_table().attach(removeOverlapXGapLabel, column, column+1, row, row+1, Gtk::FILL, Gtk::FILL);
483 dialog.removeOverlap_table().attach(removeOverlapXGap, column+1, column+2, row, row+1, Gtk::FILL, Gtk::FILL);
484 dialog.removeOverlap_table().attach(removeOverlapYGapLabel, column+2, column+3, row, row+1, Gtk::FILL, Gtk::FILL);
485 dialog.removeOverlap_table().attach(removeOverlapYGap, column+3, column+4, row, row+1, Gtk::FILL, Gtk::FILL);
487 }
489 private :
490 virtual void on_button_click()
491 {
492 if (!_dialog.getDesktop()) return;
494 // see comment in ActionAlign above
495 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
496 int saved_compensation = prefs->getInt("/options/clonecompensation/value", SP_CLONE_COMPENSATION_UNMOVED);
497 prefs->setInt("/options/clonecompensation/value", SP_CLONE_COMPENSATION_UNMOVED);
499 // xGap and yGap are the minimum space required between bounding rectangles.
500 double const xGap = removeOverlapXGap.get_value();
501 double const yGap = removeOverlapYGap.get_value();
502 removeoverlap(sp_desktop_selection(_dialog.getDesktop())->itemList(),
503 xGap, yGap);
505 // restore compensation setting
506 prefs->setInt("/options/clonecompensation/value", saved_compensation);
508 sp_document_done(sp_desktop_document(_dialog.getDesktop()), SP_VERB_DIALOG_ALIGN_DISTRIBUTE,
509 _("Remove overlaps"));
510 }
511 };
513 class ActionGraphLayout : public Action {
514 public:
515 ActionGraphLayout(Glib::ustring const &id,
516 Glib::ustring const &tiptext,
517 guint row,
518 guint column,
519 AlignAndDistribute &dialog) :
520 Action(id, tiptext, row, column + 4,
521 dialog.graphLayout_table(), dialog.tooltips(), dialog)
522 {}
524 private :
525 virtual void on_button_click()
526 {
527 if (!_dialog.getDesktop()) return;
529 // see comment in ActionAlign above
530 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
531 int saved_compensation = prefs->getInt("/options/clonecompensation/value", SP_CLONE_COMPENSATION_UNMOVED);
532 prefs->setInt("/options/clonecompensation/value", SP_CLONE_COMPENSATION_UNMOVED);
534 graphlayout(sp_desktop_selection(_dialog.getDesktop())->itemList());
536 // restore compensation setting
537 prefs->setInt("/options/clonecompensation/value", saved_compensation);
539 sp_document_done(sp_desktop_document(_dialog.getDesktop()), SP_VERB_DIALOG_ALIGN_DISTRIBUTE,
540 _("Arrange connector network"));
541 }
542 };
544 class ActionUnclump : public Action {
545 public :
546 ActionUnclump(const Glib::ustring &id,
547 const Glib::ustring &tiptext,
548 guint row,
549 guint column,
550 AlignAndDistribute &dialog):
551 Action(id, tiptext, row, column,
552 dialog.distribute_table(), dialog.tooltips(), dialog)
553 {}
555 private :
556 virtual void on_button_click()
557 {
558 if (!_dialog.getDesktop()) return;
560 // see comment in ActionAlign above
561 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
562 int saved_compensation = prefs->getInt("/options/clonecompensation/value", SP_CLONE_COMPENSATION_UNMOVED);
563 prefs->setInt("/options/clonecompensation/value", SP_CLONE_COMPENSATION_UNMOVED);
565 unclump ((GSList *) sp_desktop_selection(_dialog.getDesktop())->itemList());
567 // restore compensation setting
568 prefs->setInt("/options/clonecompensation/value", saved_compensation);
570 sp_document_done (sp_desktop_document (_dialog.getDesktop()), SP_VERB_DIALOG_ALIGN_DISTRIBUTE,
571 _("Unclump"));
572 }
573 };
575 class ActionRandomize : public Action {
576 public :
577 ActionRandomize(const Glib::ustring &id,
578 const Glib::ustring &tiptext,
579 guint row,
580 guint column,
581 AlignAndDistribute &dialog):
582 Action(id, tiptext, row, column,
583 dialog.distribute_table(), dialog.tooltips(), dialog)
584 {}
586 private :
587 virtual void on_button_click()
588 {
589 SPDesktop *desktop = _dialog.getDesktop();
590 if (!desktop) return;
592 Inkscape::Selection *selection = sp_desktop_selection(desktop);
593 if (!selection) return;
595 using Inkscape::Util::GSListConstIterator;
596 std::list<SPItem *> selected;
597 selected.insert<GSListConstIterator<SPItem *> >(selected.end(), selection->itemList(), NULL);
598 if (selected.empty()) return;
600 //Check 2 or more selected objects
601 if (selected.size() < 2) return;
603 Geom::OptRect sel_bbox = selection->bounds();
604 if (!sel_bbox) {
605 return;
606 }
608 // This bbox is cached between calls to randomize, so that there's no growth nor shrink
609 // nor drift on sequential randomizations. Discard cache on global (or better active
610 // desktop's) selection_change signal.
611 if (!_dialog.randomize_bbox) {
612 _dialog.randomize_bbox = *sel_bbox;
613 }
615 // see comment in ActionAlign above
616 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
617 int saved_compensation = prefs->getInt("/options/clonecompensation/value", SP_CLONE_COMPENSATION_UNMOVED);
618 prefs->setInt("/options/clonecompensation/value", SP_CLONE_COMPENSATION_UNMOVED);
620 for (std::list<SPItem *>::iterator it(selected.begin());
621 it != selected.end();
622 ++it)
623 {
624 sp_document_ensure_up_to_date(sp_desktop_document (desktop));
625 Geom::OptRect item_box = sp_item_bbox_desktop (*it);
626 if (item_box) {
627 // find new center, staying within bbox
628 double x = _dialog.randomize_bbox->min()[Geom::X] + (*item_box)[Geom::X].extent() /2 +
629 g_random_double_range (0, (*_dialog.randomize_bbox)[Geom::X].extent() - (*item_box)[Geom::X].extent());
630 double y = _dialog.randomize_bbox->min()[Geom::Y] + (*item_box)[Geom::Y].extent()/2 +
631 g_random_double_range (0, (*_dialog.randomize_bbox)[Geom::Y].extent() - (*item_box)[Geom::Y].extent());
632 // displacement is the new center minus old:
633 Geom::Point t = Geom::Point (x, y) - 0.5*(item_box->max() + item_box->min());
634 sp_item_move_rel(*it, Geom::Translate(t));
635 }
636 }
638 // restore compensation setting
639 prefs->setInt("/options/clonecompensation/value", saved_compensation);
641 sp_document_done (sp_desktop_document (desktop), SP_VERB_DIALOG_ALIGN_DISTRIBUTE,
642 _("Randomize positions"));
643 }
644 };
646 struct Baselines
647 {
648 SPItem *_item;
649 Geom::Point _base;
650 Geom::Dim2 _orientation;
651 Baselines(SPItem *item, Geom::Point base, Geom::Dim2 orientation) :
652 _item (item),
653 _base (base),
654 _orientation (orientation)
655 {}
656 };
658 bool operator< (const Baselines &a, const Baselines &b)
659 {
660 return (a._base[a._orientation] < b._base[b._orientation]);
661 }
663 class ActionBaseline : public Action {
664 public :
665 ActionBaseline(const Glib::ustring &id,
666 const Glib::ustring &tiptext,
667 guint row,
668 guint column,
669 AlignAndDistribute &dialog,
670 Gtk::Table &table,
671 Geom::Dim2 orientation, bool distribute):
672 Action(id, tiptext, row, column,
673 table, dialog.tooltips(), dialog),
674 _orientation(orientation),
675 _distribute(distribute)
676 {}
678 private :
679 Geom::Dim2 _orientation;
680 bool _distribute;
681 virtual void on_button_click()
682 {
683 SPDesktop *desktop = _dialog.getDesktop();
684 if (!desktop) return;
686 Inkscape::Selection *selection = sp_desktop_selection(desktop);
687 if (!selection) return;
689 using Inkscape::Util::GSListConstIterator;
690 std::list<SPItem *> selected;
691 selected.insert<GSListConstIterator<SPItem *> >(selected.end(), selection->itemList(), NULL);
692 if (selected.empty()) return;
694 //Check 2 or more selected objects
695 if (selected.size() < 2) return;
697 Geom::Point b_min = Geom::Point (HUGE_VAL, HUGE_VAL);
698 Geom::Point b_max = Geom::Point (-HUGE_VAL, -HUGE_VAL);
700 std::vector<Baselines> sorted;
702 for (std::list<SPItem *>::iterator it(selected.begin());
703 it != selected.end();
704 ++it)
705 {
706 if (SP_IS_TEXT (*it) || SP_IS_FLOWTEXT (*it)) {
707 Inkscape::Text::Layout const *layout = te_get_layout(*it);
708 Geom::Point base = layout->characterAnchorPoint(layout->begin()) * sp_item_i2d_affine(*it);
709 if (base[Geom::X] < b_min[Geom::X]) b_min[Geom::X] = base[Geom::X];
710 if (base[Geom::Y] < b_min[Geom::Y]) b_min[Geom::Y] = base[Geom::Y];
711 if (base[Geom::X] > b_max[Geom::X]) b_max[Geom::X] = base[Geom::X];
712 if (base[Geom::Y] > b_max[Geom::Y]) b_max[Geom::Y] = base[Geom::Y];
714 Baselines b (*it, base, _orientation);
715 sorted.push_back(b);
716 }
717 }
719 if (sorted.size() <= 1) return;
721 //sort baselines
722 std::sort(sorted.begin(), sorted.end());
724 bool changed = false;
726 if (_distribute) {
727 double step = (b_max[_orientation] - b_min[_orientation])/(sorted.size() - 1);
728 for (unsigned int i = 0; i < sorted.size(); i++) {
729 SPItem *item = sorted[i]._item;
730 Geom::Point base = sorted[i]._base;
731 Geom::Point t(0.0, 0.0);
732 t[_orientation] = b_min[_orientation] + step * i - base[_orientation];
733 sp_item_move_rel(item, Geom::Translate(t));
734 changed = true;
735 }
737 if (changed) {
738 sp_document_done (sp_desktop_document (desktop), SP_VERB_DIALOG_ALIGN_DISTRIBUTE,
739 _("Distribute text baselines"));
740 }
742 } else {
743 for (std::list<SPItem *>::iterator it(selected.begin());
744 it != selected.end();
745 ++it)
746 {
747 if (SP_IS_TEXT (*it) || SP_IS_FLOWTEXT (*it)) {
748 Inkscape::Text::Layout const *layout = te_get_layout(*it);
749 Geom::Point base = layout->characterAnchorPoint(layout->begin()) * sp_item_i2d_affine(*it);
750 Geom::Point t(0.0, 0.0);
751 t[_orientation] = b_min[_orientation] - base[_orientation];
752 sp_item_move_rel(*it, Geom::Translate(t));
753 changed = true;
754 }
755 }
757 if (changed) {
758 sp_document_done (sp_desktop_document (desktop), SP_VERB_DIALOG_ALIGN_DISTRIBUTE,
759 _("Align text baselines"));
760 }
761 }
762 }
763 };
767 void on_tool_changed(Inkscape::Application */*inkscape*/, SPEventContext */*context*/, AlignAndDistribute *daad)
768 {
769 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
770 if (desktop && sp_desktop_event_context(desktop))
771 daad->setMode(tools_active(desktop) == TOOLS_NODES);
772 }
774 void on_selection_changed(Inkscape::Application */*inkscape*/, Inkscape::Selection */*selection*/, AlignAndDistribute *daad)
775 {
776 daad->randomize_bbox = Geom::OptRect();
777 }
779 /////////////////////////////////////////////////////////
784 AlignAndDistribute::AlignAndDistribute()
785 : UI::Widget::Panel ("", "/dialogs/align", SP_VERB_DIALOG_ALIGN_DISTRIBUTE),
786 randomize_bbox(),
787 _alignFrame(_("Align")),
788 _distributeFrame(_("Distribute")),
789 _removeOverlapFrame(_("Remove overlaps")),
790 _graphLayoutFrame(_("Connector network layout")),
791 _nodesFrame(_("Nodes")),
792 _alignTable(2, 6, true),
793 _distributeTable(3, 6, true),
794 _removeOverlapTable(1, 5, false),
795 _graphLayoutTable(1, 5, false),
796 _nodesTable(1, 4, true),
797 _anchorLabel(_("Relative to: ")),
798 _selgrpLabel(_("Treat selection as group: "))
799 {
800 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
802 //Instanciate the align buttons
803 addAlignButton(INKSCAPE_ICON_ALIGN_HORIZONTAL_RIGHT_TO_ANCHOR,
804 _("Align right edges of objects to the left edge of the anchor"),
805 0, 0);
806 addAlignButton(INKSCAPE_ICON_ALIGN_HORIZONTAL_LEFT,
807 _("Align left edges"),
808 0, 1);
809 addAlignButton(INKSCAPE_ICON_ALIGN_HORIZONTAL_CENTER,
810 _("Center objects horizontally"),
811 0, 2);
812 addAlignButton(INKSCAPE_ICON_ALIGN_HORIZONTAL_RIGHT,
813 _("Align right sides"),
814 0, 3);
815 addAlignButton(INKSCAPE_ICON_ALIGN_HORIZONTAL_LEFT_TO_ANCHOR,
816 _("Align left edges of objects to the right edge of the anchor"),
817 0, 4);
818 addAlignButton(INKSCAPE_ICON_ALIGN_VERTICAL_BOTTOM_TO_ANCHOR,
819 _("Align bottom edges of objects to the top edge of the anchor"),
820 1, 0);
821 addAlignButton(INKSCAPE_ICON_ALIGN_VERTICAL_TOP,
822 _("Align top edges"),
823 1, 1);
824 addAlignButton(INKSCAPE_ICON_ALIGN_VERTICAL_CENTER,
825 _("Center on horizontal axis"),
826 1, 2);
827 addAlignButton(INKSCAPE_ICON_ALIGN_VERTICAL_BOTTOM,
828 _("Align bottom edges"),
829 1, 3);
830 addAlignButton(INKSCAPE_ICON_ALIGN_VERTICAL_TOP_TO_ANCHOR,
831 _("Align top edges of objects to the bottom edge of the anchor"),
832 1, 4);
834 //Baseline aligns
835 addBaselineButton(INKSCAPE_ICON_ALIGN_HORIZONTAL_BASELINE,
836 _("Align baseline anchors of texts horizontally"),
837 0, 5, this->align_table(), Geom::X, false);
838 addBaselineButton(INKSCAPE_ICON_ALIGN_VERTICAL_BASELINE,
839 _("Align baselines of texts"),
840 1, 5, this->align_table(), Geom::Y, false);
842 //The distribute buttons
843 addDistributeButton(INKSCAPE_ICON_DISTRIBUTE_HORIZONTAL_GAPS,
844 _("Make horizontal gaps between objects equal"),
845 0, 4, true, Geom::X, .5, .5);
847 addDistributeButton(INKSCAPE_ICON_DISTRIBUTE_HORIZONTAL_LEFT,
848 _("Distribute left edges equidistantly"),
849 0, 1, false, Geom::X, 1., 0.);
850 addDistributeButton(INKSCAPE_ICON_DISTRIBUTE_HORIZONTAL_CENTER,
851 _("Distribute centers equidistantly horizontally"),
852 0, 2, false, Geom::X, .5, .5);
853 addDistributeButton(INKSCAPE_ICON_DISTRIBUTE_HORIZONTAL_RIGHT,
854 _("Distribute right edges equidistantly"),
855 0, 3, false, Geom::X, 0., 1.);
857 addDistributeButton(INKSCAPE_ICON_DISTRIBUTE_VERTICAL_GAPS,
858 _("Make vertical gaps between objects equal"),
859 1, 4, true, Geom::Y, .5, .5);
861 addDistributeButton(INKSCAPE_ICON_DISTRIBUTE_VERTICAL_TOP,
862 _("Distribute top edges equidistantly"),
863 1, 1, false, Geom::Y, 0, 1);
864 addDistributeButton(INKSCAPE_ICON_DISTRIBUTE_VERTICAL_CENTER,
865 _("Distribute centers equidistantly vertically"),
866 1, 2, false, Geom::Y, .5, .5);
867 addDistributeButton(INKSCAPE_ICON_DISTRIBUTE_VERTICAL_BOTTOM,
868 _("Distribute bottom edges equidistantly"),
869 1, 3, false, Geom::Y, 1., 0.);
871 //Baseline distribs
872 addBaselineButton(INKSCAPE_ICON_DISTRIBUTE_HORIZONTAL_BASELINE,
873 _("Distribute baseline anchors of texts horizontally"),
874 0, 5, this->distribute_table(), Geom::X, true);
875 addBaselineButton(INKSCAPE_ICON_DISTRIBUTE_VERTICAL_BASELINE,
876 _("Distribute baselines of texts vertically"),
877 1, 5, this->distribute_table(), Geom::Y, true);
879 //Randomize & Unclump
880 addRandomizeButton(INKSCAPE_ICON_DISTRIBUTE_RANDOMIZE,
881 _("Randomize centers in both dimensions"),
882 2, 2);
883 addUnclumpButton(INKSCAPE_ICON_DISTRIBUTE_UNCLUMP,
884 _("Unclump objects: try to equalize edge-to-edge distances"),
885 2, 4);
887 //Remove overlaps
888 addRemoveOverlapsButton(INKSCAPE_ICON_DISTRIBUTE_REMOVE_OVERLAPS,
889 _("Move objects as little as possible so that their bounding boxes do not overlap"),
890 0, 0);
891 //Graph Layout
892 addGraphLayoutButton(INKSCAPE_ICON_DISTRIBUTE_GRAPH,
893 _("Nicely arrange selected connector network"),
894 0, 0);
896 //Node Mode buttons
897 // NOTE: "align nodes vertically" means "move nodes vertically until they align on a common
898 // _horizontal_ line". This is analogous to what the "align-vertical-center" icon means.
899 // There is no doubt some ambiguity. For this reason the descriptions are different.
900 addNodeButton(INKSCAPE_ICON_ALIGN_VERTICAL_NODES,
901 _("Align selected nodes to a common horizontal line"),
902 0, Geom::X, false);
903 addNodeButton(INKSCAPE_ICON_ALIGN_HORIZONTAL_NODES,
904 _("Align selected nodes to a common vertical line"),
905 1, Geom::Y, false);
906 addNodeButton(INKSCAPE_ICON_DISTRIBUTE_HORIZONTAL_NODE,
907 _("Distribute selected nodes horizontally"),
908 2, Geom::X, true);
909 addNodeButton(INKSCAPE_ICON_DISTRIBUTE_VERTICAL_NODE,
910 _("Distribute selected nodes vertically"),
911 3, Geom::Y, true);
913 //Rest of the widgetry
915 _combo.append_text(_("Last selected"));
916 _combo.append_text(_("First selected"));
917 _combo.append_text(_("Biggest object"));
918 _combo.append_text(_("Smallest object"));
919 _combo.append_text(_("Page"));
920 _combo.append_text(_("Drawing"));
921 _combo.append_text(_("Selection"));
923 _combo.set_active(prefs->getInt("/dialogs/align/align-to", 6));
924 _combo.signal_changed().connect(sigc::mem_fun(*this, &AlignAndDistribute::on_ref_change));
926 _anchorBox.pack_start(_anchorLabel);
927 _anchorBox.pack_start(_combo);
929 _selgrpBox.pack_start(_selgrpLabel);
930 _selgrpBox.pack_start(_selgrp);
931 _selgrp.set_active(prefs->getBool("/dialogs/align/sel-as-groups"));
932 _selgrp.signal_toggled().connect(sigc::mem_fun(*this, &AlignAndDistribute::on_selgrp_toggled));
934 _alignBox.pack_start(_anchorBox);
935 _alignBox.pack_start(_selgrpBox);
936 _alignBox.pack_start(_alignTable);
938 _alignFrame.add(_alignBox);
939 _distributeFrame.add(_distributeTable);
940 _removeOverlapFrame.add(_removeOverlapTable);
941 _graphLayoutFrame.add(_graphLayoutTable);
942 _nodesFrame.add(_nodesTable);
944 Gtk::Box *contents = _getContents();
945 contents->set_spacing(4);
947 // Notebook for individual transformations
949 contents->pack_start(_alignFrame, true, true);
950 contents->pack_start(_distributeFrame, true, true);
951 contents->pack_start(_removeOverlapFrame, true, true);
952 contents->pack_start(_graphLayoutFrame, true, true);
953 contents->pack_start(_nodesFrame, true, true);
955 //Connect to the global tool change signal
956 g_signal_connect (G_OBJECT (INKSCAPE), "set_eventcontext", G_CALLBACK (on_tool_changed), this);
958 // Connect to the global selection change, to invalidate cached randomize_bbox
959 g_signal_connect (G_OBJECT (INKSCAPE), "change_selection", G_CALLBACK (on_selection_changed), this);
960 randomize_bbox = Geom::OptRect();
962 show_all_children();
964 on_tool_changed (NULL, NULL, this); // set current mode
965 }
967 AlignAndDistribute::~AlignAndDistribute()
968 {
969 sp_signal_disconnect_by_data (G_OBJECT (INKSCAPE), this);
971 for (std::list<Action *>::iterator it = _actionList.begin();
972 it != _actionList.end();
973 it ++)
974 delete *it;
975 }
977 void AlignAndDistribute::on_ref_change(){
978 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
979 prefs->setInt("/dialogs/align/align-to", _combo.get_active_row_number());
981 //Make blink the master
982 }
984 void AlignAndDistribute::on_selgrp_toggled(){
985 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
986 prefs->setInt("/dialogs/align/sel-as-groups", _selgrp.get_active());
988 //Make blink the master
989 }
994 void AlignAndDistribute::setMode(bool nodeEdit)
995 {
996 //Act on widgets used in node mode
997 void ( Gtk::Widget::*mNode) () = nodeEdit ?
998 &Gtk::Widget::show_all : &Gtk::Widget::hide_all;
1000 //Act on widgets used in selection mode
1001 void ( Gtk::Widget::*mSel) () = nodeEdit ?
1002 &Gtk::Widget::hide_all : &Gtk::Widget::show_all;
1005 ((_alignFrame).*(mSel))();
1006 ((_distributeFrame).*(mSel))();
1007 ((_removeOverlapFrame).*(mSel))();
1008 ((_graphLayoutFrame).*(mSel))();
1009 ((_nodesFrame).*(mNode))();
1011 }
1012 void AlignAndDistribute::addAlignButton(const Glib::ustring &id, const Glib::ustring tiptext,
1013 guint row, guint col)
1014 {
1015 _actionList.push_back(
1016 new ActionAlign(
1017 id, tiptext, row, col,
1018 *this , col + row * 5));
1019 }
1020 void AlignAndDistribute::addDistributeButton(const Glib::ustring &id, const Glib::ustring tiptext,
1021 guint row, guint col, bool onInterSpace,
1022 Geom::Dim2 orientation, float kBegin, float kEnd)
1023 {
1024 _actionList.push_back(
1025 new ActionDistribute(
1026 id, tiptext, row, col, *this ,
1027 onInterSpace, orientation,
1028 kBegin, kEnd
1029 )
1030 );
1031 }
1033 void AlignAndDistribute::addNodeButton(const Glib::ustring &id, const Glib::ustring tiptext,
1034 guint col, Geom::Dim2 orientation, bool distribute)
1035 {
1036 _actionList.push_back(
1037 new ActionNode(
1038 id, tiptext, col,
1039 *this, orientation, distribute));
1040 }
1042 void AlignAndDistribute::addRemoveOverlapsButton(const Glib::ustring &id, const Glib::ustring tiptext,
1043 guint row, guint col)
1044 {
1045 _actionList.push_back(
1046 new ActionRemoveOverlaps(
1047 id, tiptext, row, col, *this)
1048 );
1049 }
1051 void AlignAndDistribute::addGraphLayoutButton(const Glib::ustring &id, const Glib::ustring tiptext,
1052 guint row, guint col)
1053 {
1054 _actionList.push_back(
1055 new ActionGraphLayout(
1056 id, tiptext, row, col, *this)
1057 );
1058 }
1060 void AlignAndDistribute::addUnclumpButton(const Glib::ustring &id, const Glib::ustring tiptext,
1061 guint row, guint col)
1062 {
1063 _actionList.push_back(
1064 new ActionUnclump(
1065 id, tiptext, row, col, *this)
1066 );
1067 }
1069 void AlignAndDistribute::addRandomizeButton(const Glib::ustring &id, const Glib::ustring tiptext,
1070 guint row, guint col)
1071 {
1072 _actionList.push_back(
1073 new ActionRandomize(
1074 id, tiptext, row, col, *this)
1075 );
1076 }
1078 void AlignAndDistribute::addBaselineButton(const Glib::ustring &id, const Glib::ustring tiptext,
1079 guint row, guint col, Gtk::Table &table, Geom::Dim2 orientation, bool distribute)
1080 {
1081 _actionList.push_back(
1082 new ActionBaseline(
1083 id, tiptext, row, col,
1084 *this, table, orientation, distribute));
1085 }
1090 std::list<SPItem *>::iterator AlignAndDistribute::find_master( std::list<SPItem *> &list, bool horizontal){
1091 std::list<SPItem *>::iterator master = list.end();
1092 switch (getAlignTarget()) {
1093 case LAST:
1094 return list.begin();
1095 break;
1097 case FIRST:
1098 return --(list.end());
1099 break;
1101 case BIGGEST:
1102 {
1103 gdouble max = -1e18;
1104 for (std::list<SPItem *>::iterator it = list.begin(); it != list.end(); it++) {
1105 Geom::OptRect b = sp_item_bbox_desktop (*it);
1106 if (b) {
1107 gdouble dim = (*b)[horizontal ? Geom::X : Geom::Y].extent();
1108 if (dim > max) {
1109 max = dim;
1110 master = it;
1111 }
1112 }
1113 }
1114 return master;
1115 break;
1116 }
1118 case SMALLEST:
1119 {
1120 gdouble max = 1e18;
1121 for (std::list<SPItem *>::iterator it = list.begin(); it != list.end(); it++) {
1122 Geom::OptRect b = sp_item_bbox_desktop (*it);
1123 if (b) {
1124 gdouble dim = (*b)[horizontal ? Geom::X : Geom::Y].extent();
1125 if (dim < max) {
1126 max = dim;
1127 master = it;
1128 }
1129 }
1130 }
1131 return master;
1132 break;
1133 }
1135 default:
1136 g_assert_not_reached ();
1137 break;
1139 } // end of switch statement
1140 return master;
1141 }
1143 AlignAndDistribute::AlignTarget AlignAndDistribute::getAlignTarget()const {
1144 return AlignTarget(_combo.get_active_row_number());
1145 }
1149 } // namespace Dialog
1150 } // namespace UI
1151 } // namespace Inkscape
1153 /*
1154 Local Variables:
1155 mode:c++
1156 c-file-style:"stroustrup"
1157 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1158 indent-tabs-mode:nil
1159 fill-column:99
1160 End:
1161 */
1162 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :