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