Export dialogue visual re-arrangement
[ardour.git] / gtk2_ardour / export_dialog.cc
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
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19 */
20
21
22 #include <sigc++/signal.h>
23
24 #include <gtkmm/messagedialog.h>
25
26 #include "ardour/audioregion.h"
27 #include "ardour/export_status.h"
28 #include "ardour/export_handler.h"
29
30 #include "export_dialog.h"
31 #include "gui_thread.h"
32 #include "nag.h"
33
34 #include "i18n.h"
35
36 using namespace ARDOUR;
37 using namespace PBD;
38 using std::string;
39
40 ExportDialog::ExportDialog (PublicEditor & editor, std::string title, ARDOUR::ExportProfileManager::ExportType type)
41   : ArdourDialog (title)
42   , type (type)
43   , editor (editor)
44
45   , warn_label ("", Gtk::ALIGN_LEFT)
46   , list_files_label (_("<span color=\"#ffa755\">Some already existing files will be overwritten.</span>"), Gtk::ALIGN_RIGHT)
47   , list_files_button (_("List files"))
48 { }
49
50 ExportDialog::~ExportDialog ()
51 { }
52
53 void
54 ExportDialog::set_session (ARDOUR::Session* s)
55 {
56         SessionHandlePtr::set_session (s);
57
58         if (!_session) {
59                 return;
60         }
61
62         /* Init handler and profile manager */
63
64         handler = _session->get_export_handler ();
65         status = _session->get_export_status ();
66
67         profile_manager.reset (new ExportProfileManager (*_session, type));
68
69         /* Possibly init stuff in derived classes */
70
71         init ();
72
73         /* Rest of _session related initialization */
74
75         preset_selector->set_manager (profile_manager);
76         file_notebook->set_session_and_manager (_session, profile_manager);
77
78         /* Hand on selection range to profile manager  */
79
80         TimeSelection const & time (editor.get_selection().time);
81         if (!time.empty()) {
82                 profile_manager->set_selection_range (time.front().start, time.front().end);
83         } else {
84                 profile_manager->set_selection_range ();
85         }
86
87         /* Load states */
88
89         profile_manager->load_profile ();
90         sync_with_manager ();
91
92         /* Warnings */
93
94         preset_selector->CriticalSelectionChanged.connect (sigc::mem_fun (*this, &ExportDialog::sync_with_manager));
95         timespan_selector->CriticalSelectionChanged.connect (sigc::mem_fun (*this, &ExportDialog::update_warnings_and_example_filename));
96         channel_selector->CriticalSelectionChanged.connect (sigc::mem_fun (*this, &ExportDialog::update_warnings_and_example_filename));
97         file_notebook->CriticalSelectionChanged.connect (sigc::mem_fun (*this, &ExportDialog::update_warnings_and_example_filename));
98
99         update_warnings_and_example_filename ();
100 }
101
102 void
103 ExportDialog::init ()
104 {
105         init_components ();
106         init_gui ();
107
108         /* warnings */
109
110         warning_widget.pack_start (warn_hbox, true, true, 6);
111         warning_widget.pack_end (list_files_hbox, false, false, 0);
112
113         warn_hbox.pack_start (warn_label, true, true, 16);
114         warn_label.set_use_markup (true);
115
116         list_files_hbox.pack_end (list_files_button, false, false, 6);
117         list_files_hbox.pack_end (list_files_label, false, false, 6);
118         list_files_label.set_use_markup (true);
119
120         list_files_button.signal_clicked().connect (sigc::mem_fun (*this, &ExportDialog::show_conflicting_files));
121
122         /* Progress indicators */
123
124         progress_widget.pack_start (progress_bar, false, false, 6);
125
126         /* Buttons */
127
128         cancel_button = add_button (Gtk::Stock::CANCEL, RESPONSE_CANCEL);
129         export_button = add_button (_("Export"), RESPONSE_FAST);
130         set_default_response (RESPONSE_FAST);
131
132         list_files_button.set_name ("PaddedButton");
133
134         cancel_button->signal_clicked().connect (sigc::mem_fun (*this, &ExportDialog::close_dialog));
135         export_button->signal_clicked().connect (sigc::mem_fun (*this, &ExportDialog::do_export));
136
137         /* Done! */
138
139         show_all_children ();
140         progress_widget.hide_all();
141 }
142
143 void
144 ExportDialog::init_gui ()
145 {
146         Gtk::Alignment * preset_align = Gtk::manage (new Gtk::Alignment());
147         preset_align->add (*preset_selector);
148         preset_align->set_padding (0, 12, 0, 0);
149
150         Gtk::VBox * file_format_selector = Gtk::manage (new Gtk::VBox());
151         file_format_selector->set_homogeneous (false);
152         file_format_selector->pack_start (*preset_align, false, false, 0);
153         file_format_selector->pack_start (*file_notebook, false, false, 0);
154
155         export_notebook.append_page (*file_format_selector, _("File format"));
156         export_notebook.append_page (*timespan_selector, _("Time Span"));
157         export_notebook.append_page (*channel_selector, _("Channels"));
158         
159         get_vbox()->pack_start (export_notebook, true, true, 0);
160         get_vbox()->pack_end   (warning_widget, false, false, 0);
161         get_vbox()->pack_end   (progress_widget, false, false, 0);
162
163 }
164
165 void
166 ExportDialog::init_components ()
167 {
168         preset_selector.reset (new ExportPresetSelector ());
169         timespan_selector.reset (new ExportTimespanSelectorMultiple (_session, profile_manager));
170         channel_selector.reset (new PortExportChannelSelector (_session, profile_manager));
171         file_notebook.reset (new ExportFileNotebook ());
172 }
173
174 void
175 ExportDialog::notify_errors ()
176 {
177         if (status->errors()) {
178                 std::string txt = _("Export has been aborted due to an error!\nSee the Log for details.");
179                 Gtk::MessageDialog msg (txt, false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, true);
180                 msg.run();
181         }
182 }
183
184 void
185 ExportDialog::close_dialog ()
186 {
187         if (status->running) {
188                 status->abort();
189         }
190
191         hide_all ();
192         set_modal (false);
193
194 }
195
196 void
197 ExportDialog::sync_with_manager ()
198 {
199         timespan_selector->sync_with_manager();
200         channel_selector->sync_with_manager();
201         file_notebook->sync_with_manager ();
202
203         update_warnings_and_example_filename ();
204 }
205
206 void
207 ExportDialog::update_warnings_and_example_filename ()
208 {
209         /* Reset state */
210
211         warn_string = "";
212         warn_label.set_markup (warn_string);
213
214         list_files_hbox.hide ();
215         list_files_string = "";
216
217         export_button->set_sensitive (true);
218
219         /* Add new warnings */
220
221         boost::shared_ptr<ExportProfileManager::Warnings> warnings = profile_manager->get_warnings();
222
223         for (std::list<string>::iterator it = warnings->errors.begin(); it != warnings->errors.end(); ++it) {
224                 add_error (*it);
225         }
226
227         for (std::list<string>::iterator it = warnings->warnings.begin(); it != warnings->warnings.end(); ++it) {
228                 add_warning (*it);
229         }
230
231         if (!warnings->conflicting_filenames.empty()) {
232                 list_files_hbox.show ();
233                 for (std::list<string>::iterator it = warnings->conflicting_filenames.begin(); it != warnings->conflicting_filenames.end(); ++it) {
234                         string::size_type pos = it->find_last_of ("/");
235                         list_files_string += it->substr (0, pos + 1) + "<b>" + it->substr (pos + 1) + "</b>\n";
236                 }
237         }
238
239         /* Update example filename */
240
241         file_notebook->update_example_filenames();
242 }
243
244 void
245 ExportDialog::show_conflicting_files ()
246 {
247         ArdourDialog dialog (_("Files that will be overwritten"), true);
248
249         Gtk::Label label ("", Gtk::ALIGN_LEFT);
250         label.set_use_markup (true);
251         label.set_markup (list_files_string);
252
253         dialog.get_vbox()->pack_start (label);
254         dialog.add_button (Gtk::Stock::OK, 0);
255         dialog.show_all_children ();
256
257         dialog.run();
258 }
259
260 void
261 ExportDialog::do_export ()
262 {
263         profile_manager->prepare_for_export ();
264         handler->do_export ();
265         show_progress ();
266 }
267
268 void
269 ExportDialog::show_progress ()
270 {
271         status->running = true;
272
273         cancel_button->set_label (_("Stop Export"));
274         export_button->set_sensitive (false);
275
276         progress_bar.set_fraction (0.0);
277         warning_widget.hide_all();
278         progress_widget.show ();
279         progress_widget.show_all_children ();
280         progress_connection = Glib::signal_timeout().connect (sigc::mem_fun(*this, &ExportDialog::progress_timeout), 100);
281
282         gtk_main_iteration ();
283
284         while (status->running) {
285                 if (gtk_events_pending()) {
286                         gtk_main_iteration ();
287                 } else {
288                         usleep (10000);
289                 }
290         }
291
292         if (!status->aborted()) {
293
294                 NagScreen* ns = NagScreen::maybe_nag (_("export"));
295                 
296                 if (ns) {
297                         ns->nag ();
298                         delete ns;
299                 }
300         } else {
301                 notify_errors ();
302         }
303
304         status->finish ();
305 }
306
307 gint
308 ExportDialog::progress_timeout ()
309 {
310         std::string status_text;
311         float progress = 0.0;
312         if (status->normalizing) {
313                 status_text = string_compose (_("Normalizing '%3' (timespan %1 of %2)"),
314                                               status->timespan, status->total_timespans, status->timespan_name);
315                 progress = ((float) status->current_normalize_cycle) / status->total_normalize_cycles;
316         } else {
317                 status_text = string_compose (_("Exporting '%3' (timespan %1 of %2)"),
318                                               status->timespan, status->total_timespans, status->timespan_name);
319                 progress = ((float) status->processed_frames_current_timespan) / status->total_frames_current_timespan;
320         }
321         progress_bar.set_text (status_text);
322
323         if (progress < previous_progress) {
324                 // Work around gtk bug
325                 progress_bar.hide();
326                 progress_bar.show();
327         }
328         previous_progress = progress;
329
330         progress_bar.set_fraction (progress);
331         return TRUE;
332 }
333
334 void
335 ExportDialog::add_error (string const & text)
336 {
337         export_button->set_sensitive (false);
338
339         if (warn_string.empty()) {
340                 warn_string = _("<span color=\"#ffa755\">Error: ") + text + "</span>";
341         } else {
342                 warn_string = _("<span color=\"#ffa755\">Error: ") + text + "</span>\n" + warn_string;
343         }
344
345         warn_label.set_markup (warn_string);
346 }
347
348 void
349 ExportDialog::add_warning (string const & text)
350 {
351         if (warn_string.empty()) {
352                 warn_string = _("<span color=\"#ffa755\">Warning: ") + text + "</span>";
353         } else {
354                 warn_string = warn_string + _("\n<span color=\"#ffa755\">Warning: ") + text + "</span>";
355         }
356
357         warn_label.set_markup (warn_string);
358 }
359
360 /*** Dialog specializations ***/
361
362 ExportRangeDialog::ExportRangeDialog (PublicEditor & editor, string range_id) :
363   ExportDialog (editor, _("Export Range"), ExportProfileManager::RangeExport),
364   range_id (range_id)
365 {}
366
367 void
368 ExportRangeDialog::init_components ()
369 {
370         preset_selector.reset (new ExportPresetSelector ());
371         timespan_selector.reset (new ExportTimespanSelectorSingle (_session, profile_manager, range_id));
372         channel_selector.reset (new PortExportChannelSelector (_session, profile_manager));
373         file_notebook.reset (new ExportFileNotebook ());
374 }
375
376 ExportSelectionDialog::ExportSelectionDialog (PublicEditor & editor) :
377   ExportDialog (editor, _("Export Selection"), ExportProfileManager::SelectionExport)
378 {}
379
380 void
381 ExportSelectionDialog::init_components ()
382 {
383         preset_selector.reset (new ExportPresetSelector ());
384         timespan_selector.reset (new ExportTimespanSelectorSingle (_session, profile_manager, X_("selection")));
385         channel_selector.reset (new PortExportChannelSelector (_session, profile_manager));
386         file_notebook.reset (new ExportFileNotebook ());
387 }
388
389 ExportRegionDialog::ExportRegionDialog (PublicEditor & editor, ARDOUR::AudioRegion const & region, ARDOUR::AudioTrack & track) :
390   ExportDialog (editor, _("Export Region"), ExportProfileManager::RegionExport),
391   region (region),
392   track (track)
393 {}
394
395 void
396 ExportRegionDialog::init_gui ()
397 {
398         ExportDialog::init_gui ();
399         export_notebook.set_tab_label_text(*export_notebook.get_nth_page(2), _("Source"));
400 }
401
402 void
403 ExportRegionDialog::init_components ()
404 {
405         string loc_id = profile_manager->set_single_range (region.position(), region.position() + region.length(), region.name());
406
407         preset_selector.reset (new ExportPresetSelector ());
408         timespan_selector.reset (new ExportTimespanSelectorSingle (_session, profile_manager, loc_id));
409         channel_selector.reset (new RegionExportChannelSelector (_session, profile_manager, region, track));
410         file_notebook.reset (new ExportFileNotebook ());
411 }
412
413 StemExportDialog::StemExportDialog (PublicEditor & editor)
414   : ExportDialog(editor, _("Stem Export"), ExportProfileManager::StemExport)
415 {
416
417 }
418
419 void
420 StemExportDialog::init_components ()
421 {
422         preset_selector.reset (new ExportPresetSelector ());
423         timespan_selector.reset (new ExportTimespanSelectorMultiple (_session, profile_manager));
424         channel_selector.reset (new TrackExportChannelSelector (_session, profile_manager));
425         file_notebook.reset (new ExportFileNotebook ());
426 }