Location timestamp changes - can now sort by location creation date: gtk part
[ardour.git] / gtk2_ardour / export_timespan_selector.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 #include <sstream>
22 #include <iomanip>
23
24 #include "pbd/enumwriter.h"
25 #include "pbd/string_convert.h"
26
27 #include "ardour/location.h"
28 #include "ardour/types.h"
29 #include "ardour/session.h"
30 #include "ardour/export_handler.h"
31 #include "ardour/export_timespan.h"
32
33 #include "export_timespan_selector.h"
34
35 #include "pbd/i18n.h"
36
37 using namespace Glib;
38 using namespace ARDOUR;
39 using namespace PBD;
40 using std::string;
41
42 ExportTimespanSelector::ExportTimespanSelector (ARDOUR::Session * session, ProfileManagerPtr manager, bool multi)
43         : manager (manager)
44         , _realtime_available (true)
45         , time_format_label (_("Show Times as:"), Gtk::ALIGN_LEFT)
46         , realtime_checkbutton (_("Realtime Export"))
47 {
48         set_session (session);
49
50         option_hbox.pack_start (time_format_label, false, false, 0);
51         option_hbox.pack_start (time_format_combo, false, false, 6);
52
53         if (multi) {
54                 Gtk::Button* b = Gtk::manage (new Gtk::Button (_("Select All")));
55                 b->signal_clicked().connect (
56                                 sigc::bind (
57                                         sigc::mem_fun (*this, &ExportTimespanSelector::set_selection_state_of_all_timespans), true
58                                         )
59                                 );
60                 option_hbox.pack_start (*b, false, false, 6);
61
62                 b = Gtk::manage (new Gtk::Button (_("Deselect All")));
63                 b->signal_clicked().connect (
64                                 sigc::bind (
65                                         sigc::mem_fun (*this, &ExportTimespanSelector::set_selection_state_of_all_timespans), false
66                                         )
67                                 );
68                 option_hbox.pack_start (*b, false, false, 6);
69         }
70         option_hbox.pack_start (realtime_checkbutton, false, false, 6);
71         realtime_checkbutton.set_active (session->config.get_realtime_export ());
72
73         realtime_checkbutton.signal_toggled ().connect (
74                         sigc::mem_fun (*this, &ExportTimespanSelector::toggle_realtime)
75                         );
76
77         range_scroller.add (range_view);
78
79         pack_start (option_hbox, false, false, 0);
80         pack_start (range_scroller, true, true, 6);
81
82         /*** Combo boxes ***/
83
84         Gtk::TreeModel::iterator iter;
85         Gtk::TreeModel::Row row;
86
87         /* Time format combo */
88
89         time_format_list = Gtk::ListStore::create (time_format_cols);
90         time_format_combo.set_model (time_format_list);
91
92         iter = time_format_list->append();
93         row = *iter;
94         row[time_format_cols.format] = ExportProfileManager::Timecode;
95         row[time_format_cols.label] = _("Timecode");
96
97         iter = time_format_list->append();
98         row = *iter;
99         row[time_format_cols.format] = ExportProfileManager::MinSec;
100         row[time_format_cols.label] = _("Minutes:Seconds");
101
102         iter = time_format_list->append();
103         row = *iter;
104         row[time_format_cols.format] = ExportProfileManager::BBT;
105         row[time_format_cols.label] = _("Bars:Beats");
106
107         time_format_combo.pack_start (time_format_cols.label);
108         time_format_combo.set_active (0);
109
110         time_format_combo.signal_changed().connect (sigc::mem_fun (*this, &ExportTimespanSelector::change_time_format));
111
112         /* Range view */
113
114         range_list = Gtk::ListStore::create (range_cols);
115         // order by location start times
116         range_list->set_sort_column(range_cols.location, Gtk::SORT_ASCENDING);
117         range_list->set_sort_func(range_cols.location, sigc::mem_fun(*this, &ExportTimespanSelector::location_sorter));
118         range_view.set_model (range_list);
119         range_view.set_headers_visible (true);
120 }
121
122 ExportTimespanSelector::~ExportTimespanSelector ()
123 {
124
125 }
126
127 int
128 ExportTimespanSelector::location_sorter(Gtk::TreeModel::iterator a, Gtk::TreeModel::iterator b)
129 {
130         Location *l1 = (*a)[range_cols.location];
131         Location *l2 = (*b)[range_cols.location];
132         const Location *ls = _session->locations()->session_range_location();
133
134         // always sort session range first
135         if (l1 == ls)
136                 return -1;
137         if (l2 == ls)
138                 return +1;
139
140         return l1->start() - l2->start();
141 }
142
143 void
144 ExportTimespanSelector::add_range_to_selection (ARDOUR::Location const * loc, bool rt)
145 {
146         ExportTimespanPtr span = _session->get_export_handler()->add_timespan();
147
148         std::string id;
149         if (loc == state->selection_range.get()) {
150                 id = "selection";
151         } else {
152                 id = loc->id().to_s();
153         }
154
155         span->set_range (loc->start(), loc->end());
156         span->set_name (loc->name());
157         span->set_range_id (id);
158         span->set_realtime (rt);
159         state->timespans->push_back (span);
160 }
161
162 void
163 ExportTimespanSelector::set_time_format_from_state ()
164 {
165         Gtk::TreeModel::Children::iterator tree_it;
166         for (tree_it = time_format_list->children().begin(); tree_it != time_format_list->children().end(); ++tree_it) {
167                 if (tree_it->get_value (time_format_cols.format) == state->time_format) {
168                         time_format_combo.set_active (tree_it);
169                 }
170         }
171 }
172
173 void
174 ExportTimespanSelector::sync_with_manager ()
175 {
176         state = manager->get_timespans().front();
177         fill_range_list ();
178         CriticalSelectionChanged();
179 }
180
181 void
182 ExportTimespanSelector::allow_realtime_export (bool yn)
183 {
184         if (_realtime_available == yn) {
185                 return;
186         }
187         _realtime_available = yn;
188         realtime_checkbutton.set_sensitive (_realtime_available);
189         update_timespans ();
190 }
191
192 void
193 ExportTimespanSelector::toggle_realtime ()
194 {
195         const bool realtime = !_session->config.get_realtime_export ();
196         _session->config.set_realtime_export (realtime);
197         realtime_checkbutton.set_inconsistent (false);
198         realtime_checkbutton.set_active (realtime);
199
200         for (Gtk::TreeStore::Children::iterator it = range_list->children().begin(); it != range_list->children().end(); ++it) {
201                 it->set_value (range_cols.realtime, realtime);
202         }
203 }
204
205 void
206 ExportTimespanSelector::change_time_format ()
207 {
208         state->time_format = time_format_combo.get_active()->get_value (time_format_cols.format);
209
210         for (Gtk::ListStore::Children::iterator it = range_list->children().begin(); it != range_list->children().end(); ++it) {
211                 Location * location = it->get_value (range_cols.location);
212                 it->set_value (range_cols.label, construct_label (location));
213                 it->set_value (range_cols.length, construct_length (location));
214         }
215 }
216
217 std::string
218 ExportTimespanSelector::construct_label (ARDOUR::Location const * location) const
219 {
220         std::string label;
221         std::string start;
222         std::string end;
223
224         samplepos_t start_sample = location->start();
225         samplepos_t end_sample = location->end();
226
227         switch (state->time_format) {
228           case ExportProfileManager::BBT:
229                 start = bbt_str (start_sample);
230                 end = bbt_str (end_sample);
231                 break;
232
233           case ExportProfileManager::Timecode:
234                 start = timecode_str (start_sample);
235                 end = timecode_str (end_sample);
236                 break;
237
238           case ExportProfileManager::MinSec:
239                 start = ms_str (start_sample);
240                 end = ms_str (end_sample);
241                 break;
242
243           case ExportProfileManager::Samples:
244                 start = to_string (start_sample);
245                 end = to_string (end_sample);
246                 break;
247         }
248
249         label += start;
250         label += _(" to ");
251         label += end;
252
253         return label;
254 }
255
256 std::string
257 ExportTimespanSelector::construct_length (ARDOUR::Location const * location) const
258 {
259         if (location->length() == 0) {
260                 return "";
261         }
262
263         std::stringstream s;
264
265         switch (state->time_format) {
266         case ExportProfileManager::BBT:
267                 s << bbt_str (location->length ());
268                 break;
269
270         case ExportProfileManager::Timecode:
271         {
272                 Timecode::Time tc;
273                 _session->timecode_duration (location->length(), tc);
274                 tc.print (s);
275                 break;
276         }
277
278         case ExportProfileManager::MinSec:
279                 s << ms_str (location->length ());
280                 break;
281
282         case ExportProfileManager::Samples:
283                 s << location->length ();
284                 break;
285         }
286
287         return s.str ();
288 }
289
290
291 std::string
292 ExportTimespanSelector::bbt_str (samplepos_t samples) const
293 {
294         if (!_session) {
295                 return "Error!";
296         }
297
298         std::ostringstream oss;
299         Timecode::BBT_Time time;
300         _session->bbt_time (samples, time);
301
302         print_padded (oss, time);
303         return oss.str ();
304 }
305
306 std::string
307 ExportTimespanSelector::timecode_str (samplecnt_t samples) const
308 {
309         if (!_session) {
310                 return "Error!";
311         }
312
313         std::ostringstream oss;
314         Timecode::Time time;
315
316         _session->timecode_time (samples, time);
317
318         oss << std::setfill('0') << std::right <<
319           std::setw(2) <<
320           time.hours << ":" <<
321           std::setw(2) <<
322           time.minutes << ":" <<
323           std::setw(2) <<
324           time.seconds << ":" <<
325           std::setw(2) <<
326           time.frames;
327
328         return oss.str();
329 }
330
331 std::string
332 ExportTimespanSelector::ms_str (samplecnt_t samples) const
333 {
334         if (!_session) {
335                 return "Error!";
336         }
337
338         std::ostringstream oss;
339         samplecnt_t left;
340         int hrs;
341         int mins;
342         int secs;
343         int sec_promilles;
344
345         left = samples;
346         hrs = (int) floor (left / (_session->sample_rate() * 60.0f * 60.0f));
347         left -= (samplecnt_t) floor (hrs * _session->sample_rate() * 60.0f * 60.0f);
348         mins = (int) floor (left / (_session->sample_rate() * 60.0f));
349         left -= (samplecnt_t) floor (mins * _session->sample_rate() * 60.0f);
350         secs = (int) floor (left / (float) _session->sample_rate());
351         left -= (samplecnt_t) floor ((double)(secs * _session->sample_rate()));
352         sec_promilles = (int) (left * 1000 / (float) _session->sample_rate() + 0.5);
353
354         oss << std::setfill('0') << std::right <<
355           std::setw(2) <<
356           hrs << ":" <<
357           std::setw(2) <<
358           mins << ":" <<
359           std::setw(2) <<
360           secs << "." <<
361           std::setw(3) <<
362           sec_promilles;
363
364         return oss.str();
365 }
366
367 void
368 ExportTimespanSelector::update_range_name (std::string const & path, std::string const & new_text)
369 {
370         Gtk::TreeStore::iterator it = range_list->get_iter (path);
371         it->get_value (range_cols.location)->set_name (new_text);
372
373         CriticalSelectionChanged();
374 }
375
376 void
377 ExportTimespanSelector::set_selection_state_of_all_timespans (bool s)
378 {
379         for (Gtk::ListStore::Children::iterator it = range_list->children().begin(); it != range_list->children().end(); ++it) {
380                 it->set_value (range_cols.selected, s);
381         }
382 }
383
384 /*** ExportTimespanSelectorSingle ***/
385
386 ExportTimespanSelectorSingle::ExportTimespanSelectorSingle (ARDOUR::Session * session, ProfileManagerPtr manager, std::string range_id) :
387         ExportTimespanSelector (session, manager, false),
388         range_id (range_id)
389 {
390         range_scroller.set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_NEVER);
391         range_view.append_column_editable (_("RT"), range_cols.realtime);
392         range_view.append_column_editable (_("Range"), range_cols.name);
393
394         if (Gtk::CellRendererToggle * renderer = dynamic_cast<Gtk::CellRendererToggle *> (range_view.get_column_cell_renderer (0))) {
395                 renderer->signal_toggled().connect (sigc::hide (sigc::mem_fun (*this, &ExportTimespanSelectorSingle::update_timespans)));
396         }
397
398         if (Gtk::CellRendererText * renderer = dynamic_cast<Gtk::CellRendererText *> (range_view.get_column_cell_renderer (1))) {
399                 renderer->signal_edited().connect (sigc::mem_fun (*this, &ExportTimespanSelectorSingle::update_range_name));
400         }
401
402         Gtk::CellRendererText * label_render = Gtk::manage (new Gtk::CellRendererText());
403         Gtk::TreeView::Column * label_col = Gtk::manage (new Gtk::TreeView::Column (_("Time Span"), *label_render));
404         label_col->add_attribute (label_render->property_markup(), range_cols.label);
405         range_view.append_column (*label_col);
406
407         range_view.append_column (_("Length"), range_cols.length);
408         range_view.append_column (_("Creation Date"), range_cols.date);
409 }
410
411 void
412 ExportTimespanSelectorSingle::allow_realtime_export (bool yn)
413 {
414         ExportTimespanSelector::allow_realtime_export (yn);
415         range_view.get_column (0)->set_visible (_realtime_available);
416 }
417
418 void
419 ExportTimespanSelectorSingle::fill_range_list ()
420 {
421         if (!state) { return; }
422         const bool realtime = _session->config.get_realtime_export ();
423
424         std::string id;
425         if (!range_id.compare (X_("selection"))) {
426                 id = state->selection_range->id().to_s();
427         } else {
428                 id = range_id;
429         }
430
431         range_list->clear();
432         state->timespans->clear();
433
434         Gtk::TreeModel::iterator iter;
435         Gtk::TreeModel::Row row;
436         for (LocationList::const_iterator it = state->ranges->begin(); it != state->ranges->end(); ++it) {
437
438                 if (!(*it)->id().to_s().compare (id)) {
439                         iter = range_list->append();
440                         row = *iter;
441
442                         row[range_cols.location] = *it;
443                         row[range_cols.selected] = true;
444                         row[range_cols.realtime] = realtime;
445                         row[range_cols.name] = (*it)->name();
446                         row[range_cols.label] = construct_label (*it);
447                         row[range_cols.length] = construct_length (*it);
448
449                         Glib::DateTime gdt(Glib::DateTime::create_now_local ((*it)->timestamp()));
450                         row[range_cols.timestamp] = (*it)->timestamp();
451                         row[range_cols.date] = gdt.format ("%F %H:%M");;
452
453
454                         add_range_to_selection (*it, false);
455
456                         break;
457                 }
458         }
459
460         set_time_format_from_state();
461 }
462
463 void
464 ExportTimespanSelectorSingle::update_timespans ()
465 {
466         state->timespans->clear();
467         const bool realtime = _session->config.get_realtime_export ();
468         bool inconsistent = false;
469
470         for (Gtk::TreeStore::Children::iterator it = range_list->children().begin(); it != range_list->children().end(); ++it) {
471                 add_range_to_selection (it->get_value (range_cols.location), it->get_value (range_cols.realtime) && _realtime_available);
472                 if (it->get_value (range_cols.realtime) != realtime) {
473                         inconsistent = true;
474                 }
475         }
476         realtime_checkbutton.set_inconsistent (inconsistent);
477 }
478
479 /*** ExportTimespanSelectorMultiple ***/
480
481 ExportTimespanSelectorMultiple::ExportTimespanSelectorMultiple (ARDOUR::Session * session, ProfileManagerPtr manager) :
482   ExportTimespanSelector (session, manager, true)
483 {
484         range_scroller.set_policy (Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
485         range_view.append_column_editable ("", range_cols.selected);
486         range_view.append_column_editable (_("RT"), range_cols.realtime);
487         range_view.append_column_editable (_("Range"), range_cols.name);
488
489         if (Gtk::CellRendererToggle * renderer = dynamic_cast<Gtk::CellRendererToggle *> (range_view.get_column_cell_renderer (0))) {
490                 renderer->signal_toggled().connect (sigc::hide (sigc::mem_fun (*this, &ExportTimespanSelectorMultiple::update_selection)));
491         }
492         if (Gtk::CellRendererToggle * renderer = dynamic_cast<Gtk::CellRendererToggle *> (range_view.get_column_cell_renderer (1))) {
493                 renderer->signal_toggled().connect (sigc::hide (sigc::mem_fun (*this, &ExportTimespanSelectorMultiple::update_selection)));
494         }
495         if (Gtk::CellRendererText * renderer = dynamic_cast<Gtk::CellRendererText *> (range_view.get_column_cell_renderer (2))) {
496                 renderer->signal_edited().connect (sigc::mem_fun (*this, &ExportTimespanSelectorMultiple::update_range_name));
497         }
498
499         Gtk::CellRendererText * label_render = Gtk::manage (new Gtk::CellRendererText());
500         Gtk::TreeView::Column * label_col = Gtk::manage (new Gtk::TreeView::Column (_("Time Span"), *label_render));
501         label_col->add_attribute (label_render->property_markup(), range_cols.label);
502         range_view.append_column (*label_col);
503
504         range_view.append_column (_("Length"), range_cols.length);
505         range_view.append_column (_("Creation Date"), range_cols.date);
506
507         range_list->set_sort_column(5, Gtk::SORT_DESCENDING);
508         Gtk::TreeViewColumn* date_col = range_view.get_column(5); // date column
509         date_col->set_sort_column(7); // set sort as the timestamp
510
511 }
512
513 void
514 ExportTimespanSelectorMultiple::allow_realtime_export (bool yn)
515 {
516         ExportTimespanSelector::allow_realtime_export (yn);
517         range_view.get_column (1)->set_visible (_realtime_available);
518 }
519
520 void
521 ExportTimespanSelectorMultiple::fill_range_list ()
522 {
523         if (!state) { return; }
524         const bool realtime = _session->config.get_realtime_export ();
525
526         range_list->clear();
527
528         Gtk::TreeModel::iterator iter;
529         Gtk::TreeModel::Row row;
530         for (LocationList::const_iterator it = state->ranges->begin(); it != state->ranges->end(); ++it) {
531
532                 iter = range_list->append();
533                 row = *iter;
534
535                 row[range_cols.location] = *it;
536                 row[range_cols.selected] = false;
537                 row[range_cols.realtime] = realtime;
538                 row[range_cols.name] = (*it)->name();
539                 row[range_cols.label] = construct_label (*it);
540                 row[range_cols.length] = construct_length (*it);
541
542                 Glib::DateTime gdt(Glib::DateTime::create_now_local ((*it)->timestamp()));
543                 row[range_cols.timestamp] = (*it)->timestamp();
544                 row[range_cols.date] = gdt.format ("%F %H:%M");;
545
546         }
547
548         set_selection_from_state ();
549 }
550
551 void
552 ExportTimespanSelectorMultiple::set_selection_from_state ()
553 {
554         Gtk::TreeModel::Children::iterator tree_it;
555
556         for (TimespanList::iterator it = state->timespans->begin(); it != state->timespans->end(); ++it) {
557                 string id = (*it)->range_id();
558                 for (tree_it = range_list->children().begin(); tree_it != range_list->children().end(); ++tree_it) {
559                         Location * loc = tree_it->get_value (range_cols.location);
560
561                         if ((id == "selection" && loc == state->selection_range.get()) ||
562                             (id == loc->id().to_s())) {
563                                 tree_it->set_value (range_cols.selected, true);
564                                 tree_it->set_value (range_cols.realtime, (*it)->realtime ());
565                         }
566                 }
567         }
568
569         set_time_format_from_state();
570 }
571
572 void
573 ExportTimespanSelectorMultiple::update_selection ()
574 {
575         update_timespans ();
576         CriticalSelectionChanged ();
577 }
578
579 void
580 ExportTimespanSelectorMultiple::update_timespans ()
581 {
582         state->timespans->clear();
583         const bool realtime = _session->config.get_realtime_export ();
584         bool inconsistent = false;
585
586         for (Gtk::TreeStore::Children::iterator it = range_list->children().begin(); it != range_list->children().end(); ++it) {
587                 if (it->get_value (range_cols.selected)) {
588                         add_range_to_selection (it->get_value (range_cols.location), it->get_value (range_cols.realtime) && _realtime_available);
589                 }
590                 if (it->get_value (range_cols.realtime) != realtime) {
591                         inconsistent = true;
592                 }
593         }
594         realtime_checkbutton.set_inconsistent (inconsistent);
595 }
596