]> git.tdb.fi Git - r2c2.git/blob - zonebar.cpp
88119c72166f4cb23e93afc9d2595a15fdd3bbd7
[r2c2.git] / zonebar.cpp
1 /* $Id$
2
3 This file is part of R²C²
4 Copyright © 2010-2011 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
6 */
7
8 #include <msp/strings/formatter.h>
9 #include "libr2c2/zone.h"
10 #include "designer.h"
11 #include "zonebar.h"
12
13 using namespace std;
14 using namespace Msp;
15 using namespace R2C2;
16
17 Zonebar::Zonebar(Designer &d):
18         Toolbar("Zone", 420),
19         designer(d)
20 {
21         pnl_content->add(*(drp_groups = new GLtk::Dropdown));
22         drp_groups->set_geometry(GLtk::Geometry(0, 10, 195, 20));
23         drp_groups->set_tooltip("Select zone group to edit");
24         drp_groups->signal_item_selected.connect(sigc::mem_fun(this, &Zonebar::group_selected));
25
26         pnl_content->add(*(drp_numbers = new GLtk::Dropdown));
27         drp_numbers->set_geometry(GLtk::Geometry(200, 10, 100, 20));
28         drp_groups->set_tooltip("Select zone to edit");
29         drp_numbers->signal_item_selected.connect(sigc::mem_fun(this, &Zonebar::number_selected));
30
31         GLtk::Button *btn;
32
33         pnl_content->add(*(btn = new GLtk::Button("Del")));
34         btn->set_geometry(GLtk::Geometry(300, 10, 40, 24));
35         btn->set_style("red");
36         btn->set_tooltip("Delete selected zone");
37         btn->signal_clicked.connect(sigc::mem_fun(this, &Zonebar::delete_zone_clicked));
38
39         pnl_content->add(*(btn = new GLtk::Button("Prop")));
40         btn->set_geometry(GLtk::Geometry(340, 10, 40, 24));
41         btn->set_tooltip("Change properties of the selected zone");
42         btn->signal_clicked.connect(sigc::mem_fun(&designer, &Designer::zone_properties));
43
44         pnl_content->add(*(btn = new GLtk::Button("AddT")));
45         btn->set_geometry(GLtk::Geometry(380, 10, 40, 24));
46         btn->set_tooltip("Add selected tracks to zone");
47         btn->signal_clicked.connect(sigc::mem_fun(&designer, &Designer::add_selection_to_zone));
48
49         Layout &rlayout = designer.get_layout();
50         rlayout.signal_zone_added.connect(sigc::mem_fun(this, &Zonebar::zone_added));
51         rlayout.signal_zone_removed.connect(sigc::hide(sigc::mem_fun(this, &Zonebar::update_groups)));
52
53         const Layout::ZoneSet &zones = rlayout.get_zones();
54         for(Layout::ZoneSet::const_iterator i=zones.begin(); i!=zones.end(); ++i)
55                 (*i)->signal_name_changed.connect(sigc::mem_fun(this, &Zonebar::zone_renamed));
56
57         update_groups();
58 }
59
60 void Zonebar::zone_added(Zone &zone)
61 {
62         zone.signal_name_changed.connect(sigc::mem_fun(this, &Zonebar::zone_renamed));
63         update_groups();
64 }
65
66 void Zonebar::zone_renamed(const string &, const string &, unsigned)
67 {
68         update_groups();
69 }
70
71 void Zonebar::group_selected(unsigned index, const string &group)
72 {
73         if(index==drp_groups->get_n_items()-1)
74         {
75                 Zone *zone = new Zone(designer.get_layout());
76                 zone->set_name("New zone", "track", 1);
77                 designer.edit_zone(zone);
78                 update_groups();
79         }
80         else
81         {
82                 Zone *cur = designer.get_current_zone();
83                 if(cur && group==cur->get_group())
84                         return;
85
86                 designer.edit_zone(0);
87                 update_numbers(group);
88         }
89 }
90
91 void Zonebar::number_selected(unsigned index, const string &)
92 {
93         if(drp_groups->get_selected_index()<0)
94                 return;
95
96         string group = drp_groups->get_selected();
97         Layout::ZoneArray zones = designer.get_layout().get_zones(group);
98         if(index==drp_numbers->get_n_items()-1)
99         {
100                 Zone *cur = designer.get_current_zone();
101                 string qualifier = (cur ? cur->get_qualifier() : "track");
102
103                 Zone *zone = new Zone(designer.get_layout());
104                 zone->set_name(group, qualifier, zones.size()+1);
105                 designer.edit_zone(zone);
106                 if(cur)
107                         update_numbers(group);
108                 else
109                         update_groups();
110         }
111         else if(index<zones.size())
112                 designer.edit_zone(zones[index]);
113 }
114
115 void Zonebar::delete_zone_clicked()
116 {
117         Zone *cur = designer.get_current_zone();
118         designer.edit_zone(0);
119         delete cur;
120 }
121
122 void Zonebar::update_groups()
123 {
124         const Layout::ZoneSet &zones = designer.get_layout().get_zones();
125         set<string> groups;
126         for(Layout::ZoneSet::const_iterator i=zones.begin(); i!=zones.end(); ++i)
127                 groups.insert((*i)->get_group());
128
129         Zone *cur = designer.get_current_zone();
130
131         drp_groups->clear();
132         int selected = -1;
133         unsigned n = 0;
134         for(set<string>::iterator i=groups.begin(); i!=groups.end(); ++i, ++n)
135         {
136                 drp_groups->append(*i);
137                 if(cur && *i==cur->get_group())
138                         selected = n;
139         }
140         drp_groups->append("(new)");
141         drp_groups->set_selected_index(selected);
142
143         if(cur)
144                 update_numbers(cur->get_group());
145         else
146                 drp_numbers->clear();
147 }
148
149 void Zonebar::update_numbers(const string &group)
150 {
151         Layout::ZoneArray zones = designer.get_layout().get_zones(group);
152         Zone *cur = designer.get_current_zone();
153
154         drp_numbers->clear();
155         int selected = -1;
156         unsigned n = 0;
157         for(Layout::ZoneArray::iterator i=zones.begin(); i!=zones.end(); ++i, ++n)
158         {
159                 drp_numbers->append(format("%s %d", (*i)->get_qualifier(), (*i)->get_number()));
160                 if(cur && *i==cur)
161                         selected = n;
162         }
163         drp_numbers->append("(new)");
164         drp_numbers->set_selected_index(selected);
165 }