cgv
Loading...
Searching...
No Matches
traverser.h
1#pragma once
2
3#include "group.h"
4#include "action.h"
5#include <string>
6
7#include "lib_begin.h"
8
9namespace cgv {
10 namespace base {
11
21
23class CGV_API traverse_policy
24{
25protected:
26 TraversePolicy policy;
27 bool active;
28 int focus;
29public:
31 traverse_policy(int _policy = TP_ALL+TP_STOP_ON_SUCCESS, bool _active = true, int _focus = -1);
33 int get_policy() const;
35 bool stop_on_success() const;
37 bool stop_on_failure() const;
39 void set_policy(int _policy);
41 int get_focused_child() const;
43 void set_focused_child(int _focused_child);
45 bool get_active() const;
47 void set_active(bool _active);
48};
49
51template <class X>
52bool grab_focus(X* instance) {
53 node* n = dynamic_cast<node*>(instance);
54 if (!n)
55 return true;
56 base_ptr p = n->get_parent();
57 if (p.empty())
58 return true;
59 group_ptr g = p->get_group();
60 if (g.empty())
61 return true;
62 X* pinstance = p->get_interface<X>();
63 if (!pinstance)
64 return true;
65 for (unsigned int i=0; i<g->get_nr_children(); ++i) {
66 if (g->get_child(i)->get_interface<X>() == instance) {
67 pinstance->set_focused_child(i);
68 return grab_focus(pinstance);
69 }
70 }
71 // at this point we did not find the instance in the children of its parent
72 return false;
73}
74
77{
78public:
80 virtual bool on_enter_node(base_ptr b);
82 virtual bool on_leave_node(base_ptr b);
84 virtual bool on_enter_children(group_ptr g);
86 virtual bool on_leave_children(group_ptr g);
88 virtual bool on_enter_parent(node_ptr n);
90 virtual bool on_leave_parent(node_ptr n);
91};
92
95{
96 TS_DEPTH_FIRST,
97 TS_BREADTH_FIRST
98};
99
101class CGV_API traverser
102{
103protected:
104 action& a;
105 TraverseStrategy strategy;
106 std::string visit_order;
107 bool stop_if_not_implemented;
111 template <typename TCH>
112 bool traverse_tmp_1(base_ptr dest, base_ptr src, bool& force_termination, TCH& tch);
114 template <typename TCH>
115 bool traverse_tmp_2(base_ptr p, base_ptr dest, base_ptr src, traverse_policy* tp, bool& force_termination, TCH& tch);
116public:
119 const std::string& _visit_order = "pnc",
120 TraverseStrategy _strategy = TS_DEPTH_FIRST,
121 bool _stop_if_not_implemented = false, bool _ignore_activation_state = false);
127 bool traverse(base_ptr start, traverse_callback_handler* tch = 0);
128};
129
130 }
131}
132
133#include <cgv/config/lib_end.h>
The action class is used in tree traversals together with the traverser.
Definition action.h:17
T * get_interface()
use dynamic type cast to check for the given interface
Definition base.h:128
virtual data::ref_ptr< group, true > get_group()
perform downcast to group
Definition base.cxx:45
unsigned int get_nr_children() const
return the number of children
Definition group.cxx:29
base_ptr get_child(unsigned int i) const
return the i-th child
Definition group.cxx:33
The node class keeps a pointer to its parent.
Definition node.h:19
node_ptr get_parent() const
return the parent node
Definition node.cxx:9
complete implementation of method actions that only call one method when entering a node
Definition action.h:113
interface of a handler for traverse callbacks
Definition traverser.h:77
nodes should inherit from this policy class to allow selective tree traversals
Definition traverser.h:24
class used to traverse a tree structure
Definition traverser.h:102
bool ignore_activation_state
whether to ignore the active flag of the traverse policy of the traversed object
Definition traverser.h:109
traverser & set_visit_order(const std::string &_visit_order)
set a different visiting order of node, children and parent
traverser & set_strategy(TraverseStrategy _strategy)
set a different traverse strategy
bool empty() const
check if pointer is not yet set
Definition ref_ptr.h:230
bool grab_focus(X *instance)
try to grab the focus in the path of this node to the root of the tree
Definition traverser.h:52
TraverseStrategy
not yet implemented
Definition traverser.h:95
TraversePolicy
different traversal policies
Definition traverser.h:13
@ TP_AUTO_FOCUS
first traverse focused and then the remaining children
Definition traverser.h:17
@ TP_STOP_ON_FAILURE
this is an optional flag for traversals with methods that return a bool. If the returned bool is true...
Definition traverser.h:19
@ TP_ONLY_FOCUS
traverse all children
Definition traverser.h:15
@ TP_FIRST_FOCUS
traverse only the focused child
Definition traverser.h:16
@ TP_STOP_ON_SUCCESS
like previous but change focus to the child, where traversal succeeded
Definition traverser.h:18
the cgv namespace
Definition print.h:11