summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorAdrian Boguszewski <adrbogus1@student.pg.gda.pl>2016-06-05 21:20:55 +0000
committerAdrian Boguszewski <adrbogus1@student.pg.gda.pl>2016-06-05 21:20:55 +0000
commit9abb5e658d005b3ac82afeec13fd59384a8e65eb (patch)
treea6bdcceacd73ff829dd54d891ac7dc3cf70273ee /src
parent[Bug #1545333] Convenience option (default: ON) for cmake builds to enable SV... (diff)
downloadinkscape-9abb5e658d005b3ac82afeec13fd59384a8e65eb.tar.gz
inkscape-9abb5e658d005b3ac82afeec13fd59384a8e65eb.zip
Added object set
(bzr r14954.1.1)
Diffstat (limited to 'src')
-rw-r--r--src/CMakeLists.txt4
-rw-r--r--src/object-set.cpp133
-rw-r--r--src/object-set.h58
-rw-r--r--src/object.cpp50
-rw-r--r--src/object.h76
5 files changed, 321 insertions, 0 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index df25728f4..802a79c4f 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -229,7 +229,9 @@ set(inkscape_SRC
message-context.cpp
message-stack.cpp
mod360.cpp
+ object.cpp
object-hierarchy.cpp
+ object-set.cpp
object-snapper.cpp
path-chemistry.cpp
persp3d-reference.cpp
@@ -353,7 +355,9 @@ set(inkscape_SRC
mod360-test.h
mod360.h
number-opt-number.h
+ object.h
object-hierarchy.h
+ object-set.h
object-snapper.h
object-test.h
path-chemistry.h
diff --git a/src/object-set.cpp b/src/object-set.cpp
new file mode 100644
index 000000000..f071aa4d6
--- /dev/null
+++ b/src/object-set.cpp
@@ -0,0 +1,133 @@
+/*
+ * Multiindex container for selection
+ *
+ * Authors:
+ * Adrian Boguszewski
+ *
+ * Copyright (C) 2016 Adrian Boguszewski
+ *
+ * Released under GNU GPL, read the file 'COPYING' for more information
+ */
+
+#include "object-set.h"
+
+bool ObjectSet::add(Object* object) {
+ // any ancestor is in the set - do nothing
+ if (_anyAncestorIsInSet(object)) {
+ return false;
+ }
+
+ // check if there is mutual ancestor for some elements, which can replace all of them in the set
+ Object* o = _getMutualAncestor(object);
+
+ // remove all descendants from the set
+ _removeDescendantsFromSet(o);
+
+ _add(o);
+ return true;
+}
+
+bool ObjectSet::remove(Object* object) {
+ // object is the top of subtree
+ if (contains(object)) {
+ _remove(object);
+ return true;
+ }
+
+ // any ancestor of object is in the set
+ if (_anyAncestorIsInSet(object)) {
+ _removeAncestorsFromSet(object);
+ return true;
+ }
+
+ // no object nor any parent in the set
+ return false;
+}
+
+bool ObjectSet::contains(Object* object) {
+ return container.get<hashed>().find(object) != container.get<hashed>().end();
+}
+
+void ObjectSet::clear() {
+ for (auto object: container) {
+ _remove(object);
+ }
+}
+
+int ObjectSet::size() {
+ return container.size();
+}
+
+bool ObjectSet::_anyAncestorIsInSet(Object *object) {
+ Object* o = object;
+ while (o != nullptr) {
+ if (contains(o)) {
+ return true;
+ }
+ o = o->getParent();
+ }
+
+ return false;
+}
+
+void ObjectSet::_removeDescendantsFromSet(Object *object) {
+ for (auto& child: object->getChildren()) {
+ if (contains(&child)) {
+ _remove(&child);
+ // there is certainly no children of this child in the set
+ continue;
+ }
+
+ _removeDescendantsFromSet(&child);
+ }
+}
+
+void ObjectSet::_remove(Object *object) {
+ releaseConnections[object].disconnect();
+ releaseConnections.erase(object);
+ container.get<hashed>().erase(object);
+}
+
+void ObjectSet::_add(Object *object) {
+ releaseConnections[object] = object->connectRelease(sigc::mem_fun(*this, &ObjectSet::remove));
+ container.push_back(object);
+}
+
+Object *ObjectSet::_getMutualAncestor(Object *object) {
+ Object *o = object;
+
+ bool flag = true;
+ while (o->getParent() != nullptr) {
+ for (auto &child: o->getParent()->getChildren()) {
+ if(&child != o && !contains(&child)) {
+ flag = false;
+ break;
+ }
+ }
+ if (!flag) {
+ break;
+ }
+ o = o->getParent();
+ }
+ return o;
+}
+
+void ObjectSet::_removeAncestorsFromSet(Object *object) {
+ Object* o = object;
+ while (o->getParent() != nullptr) {
+ for (auto &child: o->getParent()->getChildren()) {
+ if (&child != o) {
+ _add(&child);
+ }
+ }
+ if (contains(o->getParent())) {
+ _remove(o->getParent());
+ break;
+ }
+ o = o->getParent();
+ }
+}
+
+ObjectSet::~ObjectSet() {
+ clear();
+}
diff --git a/src/object-set.h b/src/object-set.h
new file mode 100644
index 000000000..a3962356b
--- /dev/null
+++ b/src/object-set.h
@@ -0,0 +1,58 @@
+/*
+ * Multiindex container for selection
+ *
+ * Authors:
+ * Adrian Boguszewski
+ *
+ * Copyright (C) 2016 Adrian Boguszewski
+ *
+ * Released under GNU GPL, read the file 'COPYING' for more information
+ */
+
+#ifndef INKSCAPE_PROTOTYPE_OBJECTSET_H
+#define INKSCAPE_PROTOTYPE_OBJECTSET_H
+
+#include "object.h"
+#include <string>
+#include <unordered_map>
+#include <boost/multi_index_container.hpp>
+#include <boost/multi_index/identity.hpp>
+#include <boost/multi_index/sequenced_index.hpp>
+#include <boost/multi_index/hashed_index.hpp>
+#include <sigc++/connection.h>
+
+struct hashed{};
+
+typedef boost::multi_index_container<
+ Object*,
+ boost::multi_index::indexed_by<
+ boost::multi_index::sequenced<>,
+ boost::multi_index::hashed_unique<
+ boost::multi_index::tag<hashed>,
+ boost::multi_index::identity<Object*>>
+ >> multi_index_container;
+
+class ObjectSet {
+public:
+ ObjectSet() {};
+ ~ObjectSet();
+ bool add(Object* object);
+ bool remove(Object* object);
+ bool contains(Object* object);
+ void clear();
+ int size();
+
+private:
+ void _add(Object* object);
+ void _remove(Object* object);
+ bool _anyAncestorIsInSet(Object *object);
+ void _removeDescendantsFromSet(Object *object);
+ void _removeAncestorsFromSet(Object *object);
+ Object *_getMutualAncestor(Object *object);
+
+ multi_index_container container;
+ std::unordered_map<Object*, sigc::connection> releaseConnections;
+};
+
+
+#endif //INKSCAPE_PROTOTYPE_OBJECTSET_H
diff --git a/src/object.cpp b/src/object.cpp
new file mode 100644
index 000000000..c05d50b3a
--- /dev/null
+++ b/src/object.cpp
@@ -0,0 +1,50 @@
+/*
+ * Temporary file
+ *
+ * Authors:
+ * Adrian Boguszewski
+ *
+ * Copyright (C) 2016 Adrian Boguszewski
+ *
+ * Released under GNU GPL, read the file 'COPYING' for more information
+ */
+
+#include "object.h"
+
+Object::Object(std::string name) : name(name), parent(NULL) { }
+
+Object::~Object() {
+ // only for this prototype
+ // call destructor on every child
+ children.clear_and_dispose(delete_disposer());
+ release_signal.emit(this);
+}
+
+const std::string& Object::getName() const {
+ return name;
+}
+
+Object *Object::getParent() {
+ return parent;
+}
+
+bool Object::isDescendantOf(Object *o) {
+ Object* p = parent;
+ while(p != NULL) {
+ if (p == o) {
+ return true;
+ }
+ p = p->parent;
+ }
+ return false;
+}
+
+void Object::addChild(Object* o) {
+ o->parent = this;
+ children.push_back(*o);
+}
+
+sigc::connection Object::connectRelease(sigc::slot<bool, Object*> slot) {
+ return release_signal.connect(slot);
+}
+
diff --git a/src/object.h b/src/object.h
new file mode 100644
index 000000000..a5ad5692e
--- /dev/null
+++ b/src/object.h
@@ -0,0 +1,76 @@
+/*
+ * Temporary file
+ *
+ * Authors:
+ * Adrian Boguszewski
+ *
+ * Copyright (C) 2016 Adrian Boguszewski
+ *
+ * Released under GNU GPL, read the file 'COPYING' for more information
+ */
+
+#ifndef INKSCAPE_PROTOTYPE_OBJECT_H
+#define INKSCAPE_PROTOTYPE_OBJECT_H
+
+#include <string>
+#include <sigc++/signal.h>
+#include <sigc++/connection.h>
+#include <boost/intrusive/list.hpp>
+
+// this causes some warning, but it's only for this prototype
+class Object;
+struct delete_disposer
+{
+ void operator()(Object *delete_this) {
+ delete delete_this;
+ }
+};
+
+class Object {
+private:
+ std::string name;
+ Object* parent;
+
+ typedef boost::intrusive::list_member_hook<
+ boost::intrusive::link_mode<
+ boost::intrusive::auto_unlink
+ >> list_hook;
+ list_hook child_hook;
+
+ typedef boost::intrusive::list<
+ Object,
+ boost::intrusive::constant_time_size<false>,
+ boost::intrusive::member_hook<
+ Object,
+ list_hook,
+ &Object::child_hook
+ >> list;
+ list children;
+
+ sigc::signal<bool, Object*> release_signal;
+
+public:
+ Object(std::string name);
+ virtual ~Object();
+
+ list &getChildren() {
+ return children;
+ }
+
+ const std::string &getName() const;
+ Object * getParent() ;
+
+ sigc::connection connectRelease(sigc::slot<bool, Object*> slot);
+ void addChild(Object* o);
+ bool isDescendantOf(Object* o);
+
+ bool operator==(Object o) {
+ return name == o.name;
+ }
+
+ bool operator!=(Object o) {
+ return name != o.name;
+ }
+};
+
+#endif //INKSCAPE_PROTOTYPE_OBJECT_H