X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=src%2Fselection.cpp;h=d3b667a128a0e76cdd5650ed276898bc41af71ba;hb=84e5676034b77e63dbc43746cec0a8b48fd06f7c;hp=c40b7b546346da91531deac04cb8bc8a53e437e9;hpb=f951374eef04129c6a0d213e7ab4d9ed2095ca69;p=inkscape.git diff --git a/src/selection.cpp b/src/selection.cpp index c40b7b546..d3b667a12 100644 --- a/src/selection.cpp +++ b/src/selection.cpp @@ -5,7 +5,9 @@ * Lauris Kaplinski * MenTaLguY * bulia byak + * Andrius R. * + * Copyright (C) 2006 Andrius R. * Copyright (C) 2004-2005 MenTaLguY * Copyright (C) 1999-2002 Lauris Kaplinski * Copyright (C) 2001-2002 Ximian, Inc. @@ -25,7 +27,11 @@ #include "xml/repr.h" #include "sp-shape.h" +#include "sp-path.h" +#include "sp-item-group.h" +#include "box3d.h" +#include #define SP_SELECTION_UPDATE_PRIORITY (G_PRIORITY_HIGH_IDLE + 1) @@ -36,10 +42,10 @@ Selection::Selection(SPDesktop *desktop) : _reprs(NULL), _items(NULL), _desktop(desktop), + _selection_context(NULL), _flags(0), _idle(0) { - clearOnceInaccessible(&_desktop); } Selection::~Selection() { @@ -51,24 +57,16 @@ Selection::~Selection() { } } -void -Selection::_release(SPObject *obj, Selection *selection) -{ - selection->remove(obj); -} - /* Handler for selected objects "modified" signal */ -void -Selection::_schedule_modified(SPObject *obj, guint flags, Selection *selection) -{ - if (!selection->_idle) { +void Selection::_schedule_modified(SPObject */*obj*/, guint flags) { + if (!this->_idle) { /* Request handling to be run in _idle loop */ - selection->_idle = g_idle_add_full(SP_SELECTION_UPDATE_PRIORITY, GSourceFunc(&Selection::_emit_modified), selection, NULL); + this->_idle = g_idle_add_full(SP_SELECTION_UPDATE_PRIORITY, GSourceFunc(&Selection::_emit_modified), this, NULL); } /* Collect all flags */ - selection->_flags |= flags; + this->_flags |= flags; } gboolean @@ -90,11 +88,33 @@ void Selection::_emitModified(guint flags) { _modified_signal.emit(this, flags); } -void Selection::_emitChanged() { +void Selection::_emitChanged(bool persist_selection_context/* = false */) { + if (persist_selection_context) { + if (NULL == _selection_context) { + _selection_context = desktop()->currentLayer(); + sp_object_ref(_selection_context, NULL); + _context_release_connection = _selection_context->connectRelease(sigc::mem_fun(*this, &Selection::_releaseContext)); + } + } else { + _releaseContext(_selection_context); + } + inkscape_selection_changed(this); _changed_signal.emit(this); } +void +Selection::_releaseContext(SPObject *obj) +{ + if (NULL == _selection_context || _selection_context != obj) + return; + + _context_release_connection.disconnect(); + + sp_object_unref(_selection_context, NULL); + _selection_context = NULL; +} + void Selection::_invalidateCachedLists() { g_slist_free(_items); _items = NULL; @@ -107,11 +127,16 @@ void Selection::_clear() { _invalidateCachedLists(); while (_objs) { SPObject *obj=reinterpret_cast(_objs->data); - sp_signal_disconnect_by_data(obj, this); - _objs = g_slist_remove(_objs, obj); + _remove(obj); } } +SPObject *Selection::activeContext() { + if (NULL != _selection_context) + return _selection_context; + return desktop()->currentLayer(); + } + bool Selection::includes(SPObject *obj) const { if (obj == NULL) return FALSE; @@ -121,7 +146,7 @@ bool Selection::includes(SPObject *obj) const { return ( g_slist_find(_objs, obj) != NULL ); } -void Selection::add(SPObject *obj) { +void Selection::add(SPObject *obj, bool persist_selection_context/* = false */) { g_return_if_fail(obj != NULL); g_return_if_fail(SP_IS_OBJECT(obj)); @@ -131,7 +156,7 @@ void Selection::add(SPObject *obj) { _invalidateCachedLists(); _add(obj); - _emitChanged(); + _emitChanged(persist_selection_context); } void Selection::_add(SPObject *obj) { @@ -141,21 +166,19 @@ void Selection::_add(SPObject *obj) { _removeObjectAncestors(obj); _objs = g_slist_prepend(_objs, obj); - g_signal_connect(G_OBJECT(obj), "release", - G_CALLBACK(&Selection::_release), this); - g_signal_connect(G_OBJECT(obj), "modified", - G_CALLBACK(&Selection::_schedule_modified), this); - - /* - if (!SP_IS_SHAPE(obj)) { - printf("This is not a shape\n"); + + if (SP_IS_BOX3D(obj)) { + // keep track of selected boxes for transformations + box3d_add_to_selection(SP_BOX3D(obj)); } - */ + + _release_connections[obj] = obj->connectRelease(sigc::mem_fun(*this, (void (Selection::*)(SPObject *))&Selection::remove)); + _modified_connections[obj] = obj->connectModified(sigc::mem_fun(*this, &Selection::_schedule_modified)); } -void Selection::set(SPObject *object) { +void Selection::set(SPObject *object, bool persist_selection_context) { _clear(); - add(object); + add(object, persist_selection_context); } void Selection::toggle(SPObject *obj) { @@ -177,7 +200,17 @@ void Selection::remove(SPObject *obj) { } void Selection::_remove(SPObject *obj) { - sp_signal_disconnect_by_data(obj, this); + _modified_connections[obj].disconnect(); + _modified_connections.erase(obj); + + _release_connections[obj].disconnect(); + _release_connections.erase(obj); + + if (SP_IS_BOX3D(obj)) { + // keep track of selected boxes for transformations + box3d_remove_from_selection(SP_BOX3D(obj)); + } + _objs = g_slist_remove(_objs, obj); } @@ -283,36 +316,25 @@ Inkscape::XML::Node *Selection::singleRepr() { return obj ? SP_OBJECT_REPR(obj) : NULL; } -NRRect *Selection::bounds(NRRect *bbox) const +NRRect *Selection::bounds(NRRect *bbox, SPItem::BBoxType type) const { g_return_val_if_fail (bbox != NULL, NULL); - NR::Rect const b = bounds(); - bbox->x0 = b.min()[NR::X]; - bbox->y0 = b.min()[NR::Y]; - bbox->x1 = b.max()[NR::X]; - bbox->y1 = b.max()[NR::Y]; + *bbox = NRRect(bounds(type)); return bbox; } -NR::Rect Selection::bounds() const +NR::Maybe Selection::bounds(SPItem::BBoxType type) const { GSList const *items = const_cast(this)->itemList(); - if (!items) { - return NR::Rect(NR::Point(0, 0), NR::Point(0, 0)); - } - - GSList const *i = items; - NR::Rect bbox = sp_item_bbox_desktop(SP_ITEM(i->data)); - while (i != NULL) { - bbox = NR::Rect::union_bounds(bbox, sp_item_bbox_desktop(SP_ITEM(i->data))); - i = i->next; + NR::Maybe bbox = NR::Nothing(); + for ( GSList const *i = items ; i != NULL ; i = i->next ) { + bbox = NR::union_bounds(bbox, sp_item_bbox_desktop(SP_ITEM(i->data), type)); } - return bbox; } -NRRect *Selection::boundsInDocument(NRRect *bbox) const { +NRRect *Selection::boundsInDocument(NRRect *bbox, SPItem::BBoxType type) const { g_return_val_if_fail (bbox != NULL, NULL); GSList const *items=const_cast(this)->itemList(); @@ -326,26 +348,50 @@ NRRect *Selection::boundsInDocument(NRRect *bbox) const { for ( GSList const *iter=items ; iter != NULL ; iter = iter->next ) { SPItem *item=SP_ITEM(iter->data); - NR::Matrix const i2doc(sp_item_i2doc_affine(item)); - sp_item_invoke_bbox(item, bbox, i2doc, FALSE); + NR::Matrix i2doc(sp_item_i2doc_affine(item)); + sp_item_invoke_bbox(item, bbox, i2doc, FALSE, type); } return bbox; } -NR::Rect Selection::boundsInDocument() const { +NR::Maybe Selection::boundsInDocument(SPItem::BBoxType type) const { NRRect r; - return NR::Rect(*boundsInDocument(&r)); + return boundsInDocument(&r, type)->upgrade(); +} + +/** Extract the position of the center from the first selected object */ +NR::Maybe Selection::center() const { + GSList *items = (GSList *) const_cast(this)->itemList(); + NR::Point center; + if (items) { + SPItem *first = reinterpret_cast(g_slist_last(items)->data); // from the first item in selection + if (first->isCenterSet()) { // only if set explicitly + return first->getCenter(); + } + } + NR::Maybe bbox = bounds(); + if (bbox) { + return bounds()->midpoint(); + } else { + return NR::Nothing(); + } } /** * Compute the list of points in the selection that are to be considered for snapping. */ -std::vector Selection::getSnapPoints() const { +std::vector Selection::getSnapPoints(bool includeItemCenter) const { GSList const *items = const_cast(this)->itemList(); std::vector p; for (GSList const *iter = items; iter != NULL; iter = iter->next) { - sp_item_snappoints(SP_ITEM(iter->data), SnapPointsIter(p)); + SPItem *this_item = SP_ITEM(iter->data); + sp_item_snappoints(this_item, false, SnapPointsIter(p)); + //Include the transformation origin for snapping + //For a group only the group's origin is considered + if (includeItemCenter) { + p.push_back(this_item->getCenter()); + } } return p; @@ -353,38 +399,30 @@ std::vector Selection::getSnapPoints() const { std::vector Selection::getSnapPointsConvexHull() const { GSList const *items = const_cast(this)->itemList(); + std::vector p; for (GSList const *iter = items; iter != NULL; iter = iter->next) { - sp_item_snappoints(SP_ITEM(iter->data), SnapPointsIter(p)); - } - - std::vector::iterator i; - NR::ConvexHull cvh(*(p.begin())); - for (i = p.begin(); i != p.end(); i++) { - // these are the points we get back - cvh.add(*i); + sp_item_snappoints(SP_ITEM(iter->data), false, SnapPointsIter(p)); } - NR::Rect rHull = cvh.bounds(); - std::vector pHull(4); - pHull[0] = rHull.corner(0); - pHull[1] = rHull.corner(1); - pHull[2] = rHull.corner(2); - pHull[3] = rHull.corner(3); - - return pHull; -} + std::vector pHull; + if (!p.empty()) { + std::vector::iterator i; + NR::ConvexHull cvh(p.front()); + for (i = p.begin(); i != p.end(); i++) { + // these are the points we get back + cvh.add(*i); + } -std::vector Selection::getBBoxPoints() const { - GSList const *items = const_cast(this)->itemList(); - std::vector p; - for (GSList const *iter = items; iter != NULL; iter = iter->next) { - NR::Rect b = sp_item_bbox_desktop(SP_ITEM(iter->data)); - p.push_back(b.min()); - p.push_back(b.max()); + NR::Maybe rHull = cvh.bounds(); + if (rHull) { + for ( unsigned i = 0 ; i < 4 ; ++i ) { + pHull.push_back(rHull->corner(i)); + } + } } - return p; + return pHull; } void Selection::_removeObjectDescendants(SPObject *obj) { @@ -417,7 +455,7 @@ SPObject *Selection::_objectForXMLNode(Inkscape::XML::Node *repr) const { g_return_val_if_fail(repr != NULL, NULL); gchar const *id = repr->attribute("id"); g_return_val_if_fail(id != NULL, NULL); - SPObject *object=SP_DT_DOCUMENT(_desktop)->getObjectById(id); + SPObject *object=sp_desktop_document(_desktop)->getObjectById(id); g_return_val_if_fail(object != NULL, NULL); return object; } @@ -436,6 +474,20 @@ guint Selection::numberOfLayers() { return ret; } +guint Selection::numberOfParents() { + GSList const *items = const_cast(this)->itemList(); + GSList *parents = NULL; + for (GSList const *iter = items; iter != NULL; iter = iter->next) { + SPObject *parent = SP_OBJECT_PARENT(iter->data); + if (g_slist_find (parents, parent) == NULL) { + parents = g_slist_prepend (parents, parent); + } + } + guint ret = g_slist_length (parents); + g_slist_free (parents); + return ret; +} + } /*