]> git.tdb.fi Git - r2c2.git/blob - source/designer/selection.cpp
Halt all trains in various unexpected situations
[r2c2.git] / source / designer / selection.cpp
1 /* $Id$
2
3 This file is part of the MSP Märklin suite
4 Copyright © 2006-2008 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
6 */
7
8 #include <algorithm>
9 #include "selection.h"
10
11 using namespace std;
12 using namespace Marklin;
13 using namespace Msp;
14
15 Track *Selection::get_track() const
16 {
17         if(tracks.empty())
18                 return 0;
19         else
20                 return *tracks.begin();
21 }
22
23 void Selection::clear()
24 {
25         tracks.clear();
26         signal_changed.emit();
27 }
28
29 void Selection::add_track(Track *t)
30 {
31         if(tracks.insert(t).second)
32                 signal_changed.emit();
33 }
34
35 void Selection::remove_track(Track *t)
36 {
37         if(tracks.erase(t))
38                 signal_changed.emit();
39 }
40
41 void Selection::toggle_track(Track *t)
42 {
43         if(!tracks.erase(t))
44                 tracks.insert(t);
45
46         signal_changed.emit();
47 }
48
49 void Selection::select_more()
50 {
51         set<Track *> new_tracks;
52         for(set<Track *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
53         {
54                 const vector<Track *> &links = (*i)->get_links();
55                 for(vector<Track *>::const_iterator j=links.begin(); j!=links.end(); ++j)
56                         if(*j)
57                                 new_tracks.insert(*j);
58         }
59
60         bool changed = false;
61         for(set<Track *>::iterator i=new_tracks.begin(); i!=new_tracks.end(); ++i)
62                 if(tracks.insert(*i).second)
63                         changed = true;
64
65         if(changed)
66                 signal_changed.emit();
67 }
68
69 void Selection::select_linked()
70 {
71         bool changed = false;
72         list<Track *> queue(tracks.begin(), tracks.end());
73         while(!queue.empty())
74         {
75                 Track *track = queue.front();
76                 queue.erase(queue.begin());
77
78                 const vector<Track *> &links = track->get_links();
79                 for(vector<Track *>::const_iterator j=links.begin(); j!=links.end(); ++j)
80                         if(*j && tracks.insert(*j).second)
81                         {
82                                 queue.push_back(*j);
83                                 changed = true;
84                         }
85         }
86         for(set<Track *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
87         {
88                 const vector<Track *> &links = (*i)->get_links();
89                 for(vector<Track *>::const_iterator j=links.begin(); j!=links.end(); ++j)
90                         if(*j && tracks.insert(*j).second)
91                                 changed = true;
92         }
93
94         if(changed)
95                 signal_changed.emit();
96 }