]> git.tdb.fi Git - r2c2.git/blob - source/designer/selection.cpp
Rewrite goal initialization for TrainRouteMetric
[r2c2.git] / source / designer / selection.cpp
1 #include <algorithm>
2 #include "libr2c2/block.h"
3 #include "selection.h"
4
5 using namespace std;
6 using namespace R2C2;
7 using namespace Msp;
8
9 Object *Selection::get_object() const
10 {
11         if(objects.empty())
12                 return 0;
13         else
14                 return *objects.begin();
15 }
16
17 void Selection::clear()
18 {
19         objects.clear();
20         signal_changed.emit();
21 }
22
23 void Selection::add_object(Object *o)
24 {
25         if(objects.insert(o).second)
26                 signal_changed.emit();
27 }
28
29 void Selection::remove_object(Object *o)
30 {
31         if(objects.erase(o))
32                 signal_changed.emit();
33 }
34
35 void Selection::toggle_object(Object *o)
36 {
37         if(!objects.erase(o))
38                 objects.insert(o);
39
40         signal_changed.emit();
41 }
42
43 void Selection::select_more()
44 {
45         set<Object *> new_objects;
46         for(set<Object *>::iterator i=objects.begin(); i!=objects.end(); ++i)
47         {
48                 unsigned nls = (*i)->get_n_link_slots();
49                 for(unsigned j=0; j<nls; ++j)
50                         if(Object *obj = (*i)->get_link(j))
51                                 new_objects.insert(obj);
52         }
53
54         bool changed = false;
55         for(set<Object *>::iterator i=new_objects.begin(); i!=new_objects.end(); ++i)
56                 if(objects.insert(*i).second)
57                         changed = true;
58
59         if(changed)
60                 signal_changed.emit();
61 }
62
63 void Selection::select_linked()
64 {
65         bool changed = false;
66         list<Object *> queue(objects.begin(), objects.end());
67
68         while(!queue.empty())
69         {
70                 Object *obj = queue.front();
71                 queue.pop_front();
72
73                 unsigned nls = obj->get_n_link_slots();
74                 for(unsigned j=0; j<nls; ++j)
75                         if(Object *linked = obj->get_link(j))
76                                 if(objects.insert(linked).second)
77                                 {
78                                         queue.push_back(linked);
79                                         changed = true;
80                                 }
81         }
82
83         if(changed)
84                 signal_changed.emit();
85 }
86
87 void Selection::select_blocks()
88 {
89         bool changed = false;
90         for(set<Object *>::iterator i=objects.begin(); i!=objects.end(); ++i)
91                 if(Track *track = dynamic_cast<Track *>(*i))
92                 {
93                         const set<Track *> &btracks = track->get_block().get_tracks();
94                         for(set<Track *>::iterator j=btracks.begin(); j!=btracks.end(); ++j)
95                                 if(objects.insert(*j).second)
96                                         changed = true;
97                 }
98
99         if(changed)
100                 signal_changed.emit();
101 }