31 #include "util/base/exception.h"
32 #include "util/log/logger.h"
33 #include "model/structures/instance.h"
34 #include "util/structures/rect.h"
36 #include "instancetree.h"
40 static Logger _log(LM_STRUCTURES);
42 InstanceTree::InstanceTree(): FifeClass() {
45 InstanceTree::~InstanceTree() {
48 void InstanceTree::addInstance(Instance* instance) {
49 ModelCoordinate coords = instance->getLocationRef().getLayerCoordinates();
50 InstanceTreeNode * node = m_tree.find_container(coords.x,coords.y,0,0);
51 InstanceList& list = node->data();
52 list.push_back(instance);
53 if( m_reverse.find(instance) != m_reverse.end() ) {
54 FL_WARN(_log,
"InstanceTree::addInstance() - Duplicate Instance. Ignoring.");
57 m_reverse[instance] = node;
60 void InstanceTree::removeInstance(Instance* instance) {
61 ModelCoordinate coords = instance->getLocationRef().getLayerCoordinates();
62 InstanceTreeNode * node = m_reverse[instance];
64 FL_WARN(_log,
"InstanceTree::removeInstance() - Instance not part of tree.");
67 m_reverse.erase(instance);
68 InstanceList& list = node->data();
69 for(InstanceList::iterator i = list.begin(); i != list.end(); ++i) {
70 if((*i) == instance) {
75 FL_WARN(_log,
"InstanceTree::removeInstance() - Instance part of tree but not found in the expected tree node.");
78 class InstanceListCollector {
80 InstanceTree::InstanceList& instanceList;
82 InstanceListCollector(InstanceTree::InstanceList& a_instanceList,
const Rect& rect)
83 : instanceList(a_instanceList), searchRect(rect) {
85 bool visit(InstanceTree::InstanceTreeNode* node, int32_t d);
88 bool InstanceListCollector::visit(InstanceTree::InstanceTreeNode* node, int32_t d) {
89 InstanceTree::InstanceList& list = node->data();
90 for(InstanceTree::InstanceList::const_iterator it(list.begin()); it != list.end(); ++it) {
91 ModelCoordinate coords = (*it)->getLocationRef().getLayerCoordinates();
92 if( searchRect.contains(Point(coords.x,coords.y)) ) {
93 instanceList.push_back(*it);
99 void InstanceTree::findInstances(
const ModelCoordinate& point, int32_t w, int32_t h, InstanceTree::InstanceList& list) {
100 InstanceTreeNode * node = m_tree.find_container(point.x, point.y, w, h);
101 Rect rect(point.x, point.y, w, h);
102 InstanceListCollector collector(list,rect);
104 node->apply_visitor(collector);
106 node = node->parent();
108 for(InstanceList::const_iterator it(node->data().begin()); it != node->data().end(); ++it) {
109 ModelCoordinate coords = (*it)->getLocationRef().getLayerCoordinates();
110 if( rect.contains(Point(coords.x,coords.y)) ) {
114 node = node->parent();
credit to phoku for his NodeDisplay example which the visitor code is adapted from ( he coded the qua...