6996e3ec8684f36e6b4ab7426cda65004e933859
1 #ifndef SEEN_INKSCAPE_SELECTION_H
2 #define SEEN_INKSCAPE_SELECTION_H
4 /** \file
5 * Inkscape::Selection: per-desktop selection container
6 *
7 * Authors:
8 * Lauris Kaplinski <lauris@kaplinski.com>
9 * MenTaLguY <mental@rydia.net>
10 * bulia byak <buliabyak@users.sf.net>
11 *
12 * Copyright (C) 2004-2005 MenTaLguY
13 * Copyright (C) 1999-2002 Lauris Kaplinski
14 * Copyright (C) 2001-2002 Ximian, Inc.
15 *
16 * Released under GNU GPL, read the file 'COPYING' for more information
17 */
19 #include <vector>
20 #include <sigc++/sigc++.h>
22 #include "libnr/nr-rect.h"
23 #include "libnr/nr-convex-hull.h"
24 #include "forward.h"
25 #include "gc-managed.h"
26 #include "gc-finalized.h"
27 #include "gc-anchored.h"
28 #include "gc-soft-ptr.h"
29 #include "util/list.h"
31 class SPItem;
33 namespace Inkscape {
34 namespace XML {
35 class Node;
36 }
37 }
39 namespace Inkscape {
41 /**
42 * @brief The set of selected SPObjects for a given desktop.
43 *
44 * This class represents the set of selected SPItems for a given
45 * SPDesktop.
46 *
47 * An SPObject and its parent cannot be simultaneously selected;
48 * selecting an SPObjects has the side-effect of unselecting any of
49 * its children which might have been selected.
50 *
51 * This is a per-desktop object that keeps the list of selected objects
52 * at the given desktop. Both SPItem and SPRepr lists can be retrieved
53 * from the selection. Many actions operate on the selection, so it is
54 * widely used throughout the code.
55 * It also implements its own asynchronous notification signals that
56 * UI elements can listen to.
57 */
58 class Selection : public Inkscape::GC::Managed<>,
59 public Inkscape::GC::Finalized,
60 public Inkscape::GC::Anchored
61 {
62 public:
63 /**
64 * Constructs an selection object, bound to a particular
65 * SPDesktop
66 *
67 * @param desktop the desktop in question
68 */
69 Selection(SPDesktop *desktop);
70 ~Selection();
72 /**
73 * @brief Returns the desktop the seoection is bound to
74 *
75 * @return the desktop the selection is bound to
76 */
77 SPDesktop *desktop() { return _desktop; }
79 /**
80 * @brief Add an SPObject to the set of selected objects
81 *
82 * @param obj the SPObject to add
83 */
84 void add(SPObject *obj);
86 /**
87 * @brief Add an XML node's SPObject to the set of selected objects
88 *
89 * @param the xml node of the item to add
90 */
91 void add(XML::Node *repr) { add(_objectForXMLNode(repr)); }
93 /**
94 * @brief Set the selection to a single specific object
95 *
96 * @param obj the object to select
97 */
98 void set(SPObject *obj);
100 /**
101 * @brief Set the selection to an XML node's SPObject
102 *
103 * @param repr the xml node of the item to select
104 */
105 void set(XML::Node *repr) { set(_objectForXMLNode(repr)); }
107 /**
108 * @brief Removes an item from the set of selected objects
109 *
110 * It is ok to call this method for an unselected item.
111 *
112 * @param item the item to unselect
113 */
114 void remove(SPObject *obj);
116 /**
117 * @brief Removes an item if selected, adds otherwise
118 *
119 * @param item the item to unselect
120 */
121 void toggle(SPObject *obj);
123 /**
124 * @brief Removes an item from the set of selected objects
125 *
126 * It is ok to call this method for an unselected item.
127 *
128 * @param repr the xml node of the item to remove
129 */
130 void remove(XML::Node *repr) { remove(_objectForXMLNode(repr)); }
132 /**
133 * @brief Selects exactly the specified objects
134 *
135 * @param objs the objects to select
136 */
137 void setList(GSList const *objs);
139 /**
140 * @brief Adds the specified objects to selection, without deselecting first
141 *
142 * @param objs the objects to select
143 */
144 void addList(GSList const *objs);
146 /**
147 * @brief Clears the selection and selects the specified objects
148 *
149 * @param repr a list of xml nodes for the items to select
150 */
151 void setReprList(GSList const *reprs);
153 /** \brief Add items from an STL iterator range to the selection
154 * \param from the begin iterator
155 * \param to the end iterator
156 */
157 template <typename InputIterator>
158 void add(InputIterator from, InputIterator to) {
159 _invalidateCachedLists();
160 while ( from != to ) {
161 _add(*from);
162 ++from;
163 }
164 _emitChanged();
165 }
167 /**
168 * @brief Unselects all selected objects.
169 */
170 void clear();
172 /**
173 * @brief Returns true if no items are selected
174 */
175 bool isEmpty() const { return _objs == NULL; }
177 /**
178 * @brief Returns true if the given object is selected
179 */
180 bool includes(SPObject *obj) const;
182 /**
183 * @brief Returns true if the given item is selected
184 */
185 bool includes(XML::Node *repr) const {
186 return includes(_objectForXMLNode(repr));
187 }
189 /**
190 * @brief Returns a single selected object
191 *
192 * @return NULL unless exactly one object is selected
193 */
194 SPObject *single();
196 /**
197 * @brief Returns a single selected item
198 *
199 * @return NULL unless exactly one object is selected
200 */
201 SPItem *singleItem();
203 /**
204 * @brief Returns a single selected object's xml node
205 *
206 * @return NULL unless exactly one object is selected
207 */
208 XML::Node *singleRepr();
210 /** @brief Returns the list of selected objects */
211 GSList const *list();
212 /** @brief Returns the list of selected SPItems */
213 GSList const *itemList();
214 /** @brief Returns a list of the xml nodes of all selected objects */
215 /// \todo only returns reprs of SPItems currently; need a separate
216 /// method for that
217 GSList const *reprList();
219 /** @brief Returns the number of layers in which there are selected objects */
220 guint numberOfLayers();
222 /** @brief Returns the number of parents to which the selected objects belong */
223 guint numberOfParents();
225 /** @brief Returns the bounding rectangle of the selection */
226 NRRect *bounds(NRRect *dest) const;
227 /** @brief Returns the bounding rectangle of the selection */
228 ::NR::Rect bounds() const;
230 /**
231 * @brief Returns the bounding rectangle of the selection
232 *
233 * \todo how is this different from bounds()?
234 */
235 NRRect *boundsInDocument(NRRect *dest) const;
237 /**
238 * @brief Returns the bounding rectangle of the selection
239 *
240 * \todo how is this different from bounds()?
241 */
242 ::NR::Rect boundsInDocument() const;
244 /**
245 * @brief Gets the selection's snap points.
246 * @return Selection's snap points
247 */
248 std::vector<NR::Point> getSnapPoints() const;
250 /**
251 * @brief Gets the snap points of a selection that form a convex hull.
252 * @return Selection's convex hull points
253 */
254 std::vector<NR::Point> getSnapPointsConvexHull() const;
256 /**
257 * @return A vector containing the top-left and bottom-right
258 * corners of each selected object's bounding box.
259 */
260 std::vector<NR::Point> getBBoxPoints() const;
262 /**
263 * @brief Connects a slot to be notified of selection changes
264 *
265 * This method connects the given slot such that it will
266 * be called upon any change in the set of selected objects.
267 *
268 * @param slot the slot to connect
269 *
270 * @return the resulting connection
271 */
272 sigc::connection connectChanged(sigc::slot<void, Selection *> const &slot) {
273 return _changed_signal.connect(slot);
274 }
276 /**
277 * @brief Connects a slot to be notified of selected
278 * object modifications
279 *
280 * This method connects the given slot such that it will
281 * receive notifications whenever any selected item is
282 * modified.
283 *
284 * @param slot the slot to connect
285 *
286 * @return the resulting connection
287 *
288 */
289 sigc::connection connectModified(sigc::slot<void, Selection *, guint> const &slot)
290 {
291 return _modified_signal.connect(slot);
292 }
294 private:
295 /** @brief no copy */
296 Selection(Selection const &);
297 /** @brief no assign */
298 void operator=(Selection const &);
300 /** @brief Issues modification notification signals */
301 static gboolean _emit_modified(Selection *selection);
302 /** @brief Schedules an item modification signal to be sent */
303 static void _schedule_modified(SPObject *obj, guint flags, Selection *selection);
304 /** @brief Releases a selected object that is being removed */
305 static void _release(SPObject *obj, Selection *selection);
307 /** @brief Issues modified selection signal */
308 void _emitModified(guint flags);
309 /** @brief Issues changed selection signal */
310 void _emitChanged();
312 void _invalidateCachedLists();
314 /** @brief unselect all descendants of the given item */
315 void _removeObjectDescendants(SPObject *obj);
316 /** @brief unselect all ancestors of the given item */
317 void _removeObjectAncestors(SPObject *obj);
318 /** @brief clears the selection (without issuing a notification) */
319 void _clear();
320 /** @brief adds an object (without issuing a notification) */
321 void _add(SPObject *obj);
322 /** @brief removes an object (without issuing a notification) */
323 void _remove(SPObject *obj);
324 /** @brief returns the SPObject corresponding to an xml node (if any) */
325 SPObject *_objectForXMLNode(XML::Node *repr) const;
327 mutable GSList *_objs;
328 mutable GSList *_reprs;
329 mutable GSList *_items;
331 GC::soft_ptr<SPDesktop> _desktop;
332 guint _flags;
333 guint _idle;
335 sigc::signal<void, Selection *> _changed_signal;
336 sigc::signal<void, Selection *, guint> _modified_signal;
337 };
339 }
341 #endif
342 /*
343 Local Variables:
344 mode:c++
345 c-file-style:"stroustrup"
346 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
347 indent-tabs-mode:nil
348 fill-column:99
349 End:
350 */
351 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :