provide a heuristic to decide if the color has been set for a PresentationInfo object
[ardour.git] / libs / ardour / presentation_info.cc
1 /*
2     Copyright (C) 2016 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
19 #include <sstream>
20 #include <typeinfo>
21
22 #include <cassert>
23
24 #include "pbd/convert.h"
25 #include "pbd/debug.h"
26 #include "pbd/enumwriter.h"
27 #include "pbd/error.h"
28 #include "pbd/failed_constructor.h"
29 #include "pbd/xml++.h"
30
31 #include "ardour/presentation_info.h"
32
33 #include "i18n.h"
34
35 using namespace ARDOUR;
36 using namespace PBD;
37 using std::string;
38
39 string PresentationInfo::state_node_name = X_("PresentationInfo");
40 PBD::Signal0<void> PresentationInfo::Change;
41
42 namespace ARDOUR {
43         namespace Properties {
44                 PBD::PropertyDescriptor<bool>     selected;
45                 PBD::PropertyDescriptor<uint32_t> order;
46                 PBD::PropertyDescriptor<uint32_t> color;
47         }
48 }
49
50 const PresentationInfo::order_t PresentationInfo::max_order = UINT32_MAX;
51 const PresentationInfo::Flag PresentationInfo::Bus = PresentationInfo::Flag (PresentationInfo::AudioBus|PresentationInfo::MidiBus);
52 const PresentationInfo::Flag PresentationInfo::Track = PresentationInfo::Flag (PresentationInfo::AudioTrack|PresentationInfo::MidiTrack);
53 const PresentationInfo::Flag PresentationInfo::Route = PresentationInfo::Flag (PresentationInfo::Bus|PresentationInfo::Track);
54 const PresentationInfo::Flag PresentationInfo::AllRoutes = PresentationInfo::Flag (PresentationInfo::Route|PresentationInfo::MasterOut|PresentationInfo::MonitorOut);
55
56 void
57 PresentationInfo::make_property_quarks ()
58 {
59         Properties::selected.property_id = g_quark_from_static_string (X_("selected"));
60         DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for selected = %1\n",    Properties::selected.property_id));
61         Properties::color.property_id = g_quark_from_static_string (X_("color"));
62         DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for color = %1\n",       Properties::color.property_id));
63         Properties::order.property_id = g_quark_from_static_string (X_("order"));
64         DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for order = %1\n",       Properties::order.property_id));
65 }
66
67 PresentationInfo::PresentationInfo (Flag f)
68         : _order (0)
69         , _flags (Flag (f & ~OrderSet))
70         , _color (0)
71 {
72         /* OrderSet is not set */
73 }
74
75 PresentationInfo::PresentationInfo (order_t o, Flag f)
76         : _order (o)
77         , _flags (Flag (f | OrderSet))
78         , _color (0)
79 {
80         /* OrderSet is set */
81 }
82 PresentationInfo::PresentationInfo (PresentationInfo const& other)
83         : _order (other.order())
84         , _flags (other.flags())
85         , _color (other.color())
86 {
87 }
88
89 XMLNode&
90 PresentationInfo::get_state ()
91 {
92         XMLNode* node = new XMLNode (state_node_name);
93         node->add_property ("order", PBD::to_string (_order, std::dec));
94         node->add_property ("flags", enum_2_string (_flags));
95         node->add_property ("color", PBD::to_string (_color, std::dec));
96
97         return *node;
98 }
99
100 int
101 PresentationInfo::set_state (XMLNode const& node, int /* version */)
102 {
103         if (node.name() != state_node_name) {
104                 return -1;
105         }
106
107         XMLProperty const* prop;
108         PropertyChange pc;
109
110         if ((prop = node.property (X_("order"))) != 0) {
111                 order_t o = atoi (prop->value());
112                 if (o != _order) {
113                         pc.add (Properties::order);
114                         _order = o;
115                 }
116                 _order = atoi (prop->value());
117         }
118
119         if ((prop = node.property (X_("flags"))) != 0) {
120                 Flag f = Flag (string_2_enum (prop->value(), f));
121                 if ((f&Hidden) != (_flags&Hidden)) {
122                         pc.add (Properties::hidden);
123                 }
124                 _flags = f;
125         }
126
127         if ((prop = node.property (X_("color"))) != 0) {
128                 color_t c = atoi (prop->value());
129                 if (c != _color) {
130                         pc.add (Properties::order);
131                         _color = c;
132                 }
133         }
134
135         send_change (PropertyChange (pc));
136
137         return 0;
138
139 }
140
141 PresentationInfo::Flag
142 PresentationInfo::get_flags (XMLNode const& node)
143 {
144         XMLNodeList nlist = node.children ();
145
146         for (XMLNodeConstIterator niter = nlist.begin(); niter != nlist.end(); ++niter){
147                 XMLNode* child = *niter;
148
149                 if (child->name() == PresentationInfo::state_node_name) {
150                         XMLProperty const* prop = child->property (X_("flags"));
151                         if (prop) {
152                                 Flag f = (Flag) string_2_enum (prop->value(), f);
153                                 return f;
154                         }
155                 }
156         }
157         return Flag (0);
158 }
159
160 void
161 PresentationInfo::set_color (PresentationInfo::color_t c)
162 {
163         if (c != _color) {
164                 _color = c;
165                 send_change (PropertyChange (Properties::color));
166         }
167 }
168
169 bool
170 PresentationInfo::color_set () const
171 {
172         /* all RGBA values zero? not set.
173          *
174          * this is heuristic, but it is fairly realistic. who will ever set
175          * a color to completely transparent black? only the constructor ..
176          */
177
178         return _color == 0;
179 }
180
181 void
182 PresentationInfo::set_selected (bool yn)
183 {
184         if (yn != selected()) {
185                 if (yn) {
186                         _flags = Flag (_flags | Selected);
187                 } else {
188                         _flags = Flag (_flags & ~Selected);
189                 }
190                 send_change (PropertyChange (Properties::selected));
191         }
192 }
193
194 void
195 PresentationInfo::set_hidden (bool yn)
196 {
197         if (yn != hidden()) {
198
199                 if (yn) {
200                         _flags = Flag (_flags | Hidden);
201                 } else {
202                         _flags = Flag (_flags & ~Hidden);
203                 }
204
205                 send_change (PropertyChange (Properties::hidden));
206         }
207 }
208
209 void
210 PresentationInfo::set_order (order_t order)
211 {
212         _flags = Flag (_flags|OrderSet);
213
214         if (order != _order) {
215                 _order = order;
216                 send_change (PropertyChange (Properties::order));
217         }
218 }
219
220 PresentationInfo&
221 PresentationInfo::operator= (PresentationInfo const& other)
222 {
223         if (this != &other) {
224                 _order = other.order();
225                 _flags = other.flags();
226                 _color = other.color();
227         }
228
229         return *this;
230 }
231
232 std::ostream&
233 operator<<(std::ostream& o, ARDOUR::PresentationInfo const& pi)
234 {
235         return o << pi.order() << '/' << enum_2_string (pi.flags()) << '/' << pi.color();
236 }