e06dbe010cb3dc923a2e9c0db6ba1783a9fa0af3
[ardour.git] / gtk2_ardour / session_metadata_dialog.h
1 /*
2     Copyright (C) 2008 Paul Davis 
3     Author: Sakari Bergen
4
5     This program is free software; you can redistribute it and/or modify it
6     under the terms of the GNU General Public License as published by the Free
7     Software Foundation; either version 2 of the License, or (at your option)
8     any later version.
9
10     This program is distributed in the hope that it will be useful, but WITHOUT
11     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13     for more details.
14
15     You should have received a copy of the GNU General Public License along
16     with this program; if not, write to the Free Software Foundation, Inc.,
17     675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #ifndef __session_metadata_dialog_h__
21 #define __session_metadata_dialog_h__
22
23 #include "ardour_dialog.h"
24
25 #include <gtkmm.h>
26 #include <boost/shared_ptr.hpp>
27
28 #include <string>
29 #include <list>
30
31 #include "ardour/session_metadata.h"
32
33 using std::string;
34 using Glib::ustring;
35
36 class MetadataField;
37 typedef boost::shared_ptr<MetadataField> MetadataPtr; 
38
39 /// Wraps a metadata field to be used in a GUI
40 class MetadataField {
41   public:
42         MetadataField (ustring const & field_name);
43         virtual ~MetadataField();
44         virtual MetadataPtr copy () = 0;
45         
46         virtual void save_data (ARDOUR::SessionMetadata & data) const = 0;
47         virtual void load_data (ARDOUR::SessionMetadata const & data) = 0;
48         
49         virtual ustring name() { return _name; }
50         virtual ustring value() { return _value; }
51         
52         /// Get widget containing name of field
53         virtual Gtk::Widget & name_widget () = 0;
54         /// Get label containing value of field
55         virtual Gtk::Widget & value_widget () = 0;
56         /// Get widget for editing value
57         virtual Gtk::Widget & edit_widget () = 0;
58   protected:
59         ustring _name;
60         ustring _value;
61 };
62
63 /// MetadataField that contains text
64 class TextMetadataField : public MetadataField {
65   private:
66         typedef ustring (ARDOUR::SessionMetadata::*Getter) () const;
67         typedef void (ARDOUR::SessionMetadata::*Setter) (ustring const &);
68   public:
69         TextMetadataField (Getter getter, Setter setter, ustring const & field_name, guint width = 50);
70         MetadataPtr copy ();
71         
72         void save_data (ARDOUR::SessionMetadata & data) const;
73         void load_data (ARDOUR::SessionMetadata const & data);
74         
75         Gtk::Widget & name_widget ();
76         Gtk::Widget & value_widget ();
77         Gtk::Widget & edit_widget ();
78   private:
79         void update_value ();
80         
81         Getter getter;
82         Setter setter;
83         
84         Gtk::Label* label;
85         Gtk::Label* value_label;
86         Gtk::Entry* entry;
87         
88         uint width;
89 };
90
91 /// MetadataField that accepts only numbers
92 class NumberMetadataField : public MetadataField {
93   private:
94         typedef uint32_t (ARDOUR::SessionMetadata::*Getter) () const;
95         typedef void (ARDOUR::SessionMetadata::*Setter) (uint32_t);
96   public:
97         NumberMetadataField (Getter getter, Setter setter, ustring const & field_name, guint numbers, guint width = 50);
98         MetadataPtr copy ();
99         
100         void save_data (ARDOUR::SessionMetadata & data) const;
101         void load_data (ARDOUR::SessionMetadata const & data);
102         
103         Gtk::Widget & name_widget ();
104         Gtk::Widget & value_widget ();
105         Gtk::Widget & edit_widget ();
106   private:
107         void update_value ();
108         ustring uint_to_str (uint32_t i) const;
109         uint32_t str_to_uint (ustring const & str) const;
110         
111         Getter getter;
112         Setter setter;
113         
114         Gtk::Label* label;
115         Gtk::Label* value_label;
116         Gtk::Entry* entry;
117         
118         guint numbers;
119         guint width;
120 };
121
122 /// Interface for MetadataFields
123 class SessionMetadataSet {
124   public:
125         SessionMetadataSet (ustring const & name);
126         virtual ~SessionMetadataSet () {};
127         
128         void add_data_field (MetadataPtr field);
129         
130         /// Sets session, into which the data is eventually saved
131         virtual void set_session (ARDOUR::Session * s) { session = s; }
132         /// allows loading extra data into data sets (for importing etc.)
133         virtual void load_extra_data (ARDOUR::SessionMetadata const & data) { }
134         /// Saves data to session
135         virtual void save_data () = 0;
136         
137         virtual Gtk::Widget & get_widget () = 0;
138         virtual Gtk::Widget & get_tab_widget () = 0;
139         
140   protected:
141         typedef std::list<MetadataPtr> DataList;
142         DataList list;
143         ustring name;
144         ARDOUR::Session *session;
145 };
146
147 /// Contains MetadataFields for editing
148 class SessionMetadataSetEditable : public SessionMetadataSet {
149   public:
150         SessionMetadataSetEditable (ustring const & name);
151         
152         Gtk::Widget & get_widget () { return table; }
153         Gtk::Widget & get_tab_widget ();
154         
155         /// Sets session and loads data
156         void set_session (ARDOUR::Session * s);
157         /// Saves from MetadataFields into data
158         void save_data ();
159         
160   private:
161         Gtk::Table table;
162         Gtk::Label tab_widget;
163 };
164
165 /// Contains MetadataFields for importing
166 class SessionMetadataSetImportable : public SessionMetadataSet {
167   public:
168         SessionMetadataSetImportable (ustring const & name);
169         
170         Gtk::Widget & get_widget () { return tree_view; }
171         Gtk::Widget & get_tab_widget ();
172         Gtk::Widget & get_select_all_widget ();
173         
174         /// Loads importable data from data
175         void load_extra_data (ARDOUR::SessionMetadata const & data);
176         /// Saves from importable data (see load_data) to session_data
177         void save_data ();
178         
179   private:
180         DataList & session_list; // References MetadataSet::list
181         DataList import_list;
182         
183         struct Columns : public Gtk::TreeModel::ColumnRecord
184         {
185           public:
186                 Gtk::TreeModelColumn<ustring>     field;
187                 Gtk::TreeModelColumn<ustring>     values;
188                 Gtk::TreeModelColumn<bool>        import;
189                 Gtk::TreeModelColumn<MetadataPtr> data;
190         
191                 Columns() { add (field); add (values); add (import); add (data); }
192         };
193         
194         Glib::RefPtr<Gtk::ListStore>  tree;
195         Columns                       tree_cols;
196         Gtk::TreeView                 tree_view;
197         
198         Gtk::Label                    tab_widget;       
199         Gtk::CheckButton              select_all_check;
200         
201         void select_all ();
202         void selection_changed (ustring const & path);
203 };
204
205 /// Metadata dialog interface
206 /**
207  * The DataSets are initalized in this class so that all
208  * Dialogs have the same sets of data in the same order.
209  */
210 template <typename DataSet>
211 class SessionMetadataDialog : public ArdourDialog
212 {
213   public:
214         SessionMetadataDialog (ustring const & name);
215
216   protected:
217         void init_data ();
218         void load_extra_data (ARDOUR::SessionMetadata const & data);
219         void save_data ();
220         
221         virtual void init_gui () = 0;
222         virtual void save_and_close ();
223         virtual void end_dialog ();
224         
225         void warn_user (ustring const & string);
226         
227         typedef std::list<Gtk::Widget *> WidgetList;
228         typedef boost::shared_ptr<WidgetList> WidgetListPtr;
229         typedef Gtk::Widget & (DataSet::*WidgetFunc) ();
230         
231         /// Returns list of widgets gathered by calling f for each data set
232         WidgetListPtr get_custom_widgets (WidgetFunc f);
233         
234         /// Adds a widget to the table (vertical stacking) with automatic spacing
235         void add_widget (Gtk::Widget & widget);
236
237         Gtk::Notebook     notebook;
238
239   private:
240         void init_track_data ();
241         void init_album_data ();
242         void init_people_data ();
243
244         typedef boost::shared_ptr<SessionMetadataSet> DataSetPtr;
245         typedef std::list<DataSetPtr> DataSetList;
246         DataSetList data_list;
247         
248         Gtk::Button *     save_button;
249         Gtk::Button *     cancel_button;
250 };
251
252 class SessionMetadataEditor : public SessionMetadataDialog<SessionMetadataSetEditable> {
253   public:
254         SessionMetadataEditor ();
255         ~SessionMetadataEditor ();
256         void run ();
257   private:
258         void init_gui ();
259 };
260
261 class SessionMetadataImporter : public SessionMetadataDialog<SessionMetadataSetImportable> {
262   public:
263         SessionMetadataImporter ();
264         ~SessionMetadataImporter ();
265         void run ();
266
267   private:
268         void init_gui ();
269         
270         // Select all from -widget
271         Gtk::HBox    selection_hbox;
272         Gtk::Label   selection_label;
273         
274 };
275
276 #endif