forked from TypesettingTools/Aegisub
-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathdialog_shift_times.cpp
More file actions
434 lines (363 loc) · 14.2 KB
/
Copy pathdialog_shift_times.cpp
File metadata and controls
434 lines (363 loc) · 14.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
// Copyright (c) 2011, Thomas Goyne <plorkyeran@aegisub.org>
//
// Permission to use, copy, modify, and distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
//
// Aegisub Project http://www.aegisub.org/
#include "ass_dialogue.h"
#include "ass_file.h"
#include "compat.h"
#include "dialog_manager.h"
#include "format.h"
#include "include/aegisub/context.h"
#include "help_button.h"
#include "libresrc/libresrc.h"
#include "options.h"
#include "project.h"
#include "selection_controller.h"
#include "subs_controller.h"
#include "timeedit_ctrl.h"
#include <libaegisub/ass/time.h>
#include <libaegisub/fs.h>
#include <libaegisub/io.h>
#include <libaegisub/log.h>
#include <libaegisub/path.h>
#include <libaegisub/signal.h>
#include <libaegisub/vfr.h>
#include <libaegisub/cajun/elements.h>
#include <libaegisub/cajun/reader.h>
#include <libaegisub/cajun/writer.h>
#include <boost/filesystem/path.hpp>
#include <wx/dialog.h>
#include <wx/listbox.h>
#include <wx/radiobox.h>
#include <wx/radiobut.h>
#include <wx/sizer.h>
#include <wx/textctrl.h>
namespace {
class DialogShiftTimes final : public wxDialog {
agi::Context *context;
agi::fs::path history_filename;
json::Array history;
agi::vfr::Framerate fps;
agi::signal::Connection timecodes_loaded_slot;
agi::signal::Connection selected_set_changed_slot;
TimeEdit *shift_time;
wxTextCtrl *shift_frames;
wxRadioButton *shift_by_time;
wxRadioButton *shift_by_frames;
wxRadioButton *shift_forward;
wxRadioButton *shift_backward;
wxRadioBox *selection_mode;
wxRadioBox *time_fields;
wxListBox *history_box;
void SaveHistory(json::Array shifted_blocks);
void LoadHistory();
void Process(wxCommandEvent&);
int Shift(int initial_time, int shift, bool by_time, agi::vfr::Time type);
void OnClear(wxCommandEvent&);
void OnByTime(wxCommandEvent&);
void OnByFrames(wxCommandEvent&);
void OnHistoryClick(wxCommandEvent&);
void OnSelectedSetChanged();
void OnTimecodesLoaded(agi::vfr::Framerate const& new_fps);
public:
DialogShiftTimes(agi::Context *context);
~DialogShiftTimes();
};
static wxString get_history_string(json::Object &obj) {
wxString filename = to_wx(obj["filename"]);
if (filename.empty())
filename = _("unsaved");
wxString shift_amount(to_wx(obj["amount"]));
if (!obj["is by time"])
shift_amount = fmt_tl("%s frames", shift_amount);
wxString shift_direction = obj["is backward"] ? _("backward") : _("forward");
int64_t time_field = obj["fields"];
wxString fields =
time_field == 0 ? _("s+e") :
time_field == 1 ? _("s") :
_("e") ;
json::Array& sel = obj["selection"];
wxString lines;
int64_t sel_mode = obj["mode"];
if (sel_mode == 0)
lines = _("all");
else if (sel_mode == 2) {
if (!sel.empty())
lines = fmt_tl("from %d onward", (int64_t)static_cast<json::Object&>(sel.front())["start"]);
}
else {
lines += _("sel ");
for (auto it = sel.begin(); it != sel.end(); ++it) {
json::Object& range = *it;
int beg = (int64_t)range["start"];
int end = (int64_t)range["end"];
if (beg == end)
lines += std::to_wstring(beg);
else
lines += fmt_wx("%d-%d", beg, end);
if (it + 1 != sel.end())
lines += ";";
}
}
return fmt_wx("%s, %s %s, %s, %s", filename, shift_amount, shift_direction, fields, lines);
}
DialogShiftTimes::DialogShiftTimes(agi::Context *context)
: wxDialog(context->parent, -1, _("Shift Times"))
, context(context)
, history_filename(config::path->Decode("?state/shift_history.json"))
, timecodes_loaded_slot(context->project->AddTimecodesListener(&DialogShiftTimes::OnTimecodesLoaded, this))
, selected_set_changed_slot(context->selectionController->AddSelectionListener(&DialogShiftTimes::OnSelectedSetChanged, this))
{
SetIcon(GETICON(shift_times_toolbutton_16));
// Create controls
shift_by_time = new wxRadioButton(this, -1, _("&Time: "), wxDefaultPosition, wxDefaultSize, wxRB_GROUP);
shift_by_time->SetToolTip(_("Shift by time"));
shift_by_time->Bind(wxEVT_RADIOBUTTON, &DialogShiftTimes::OnByTime, this);
shift_by_frames = new wxRadioButton(this, -1 , _("&Frames: "));
shift_by_frames->SetToolTip(_("Shift by frames"));
shift_by_frames->Bind(wxEVT_RADIOBUTTON, &DialogShiftTimes::OnByFrames, this);
shift_time = new TimeEdit(this, -1, context);
shift_time->SetToolTip(_("Enter time in h:mm:ss.cs notation"));
shift_frames = new wxTextCtrl(this, -1);
shift_frames->SetToolTip(_("Enter number of frames to shift by"));
shift_forward = new wxRadioButton(this, -1, _("For&ward"), wxDefaultPosition, wxDefaultSize, wxRB_GROUP);
shift_forward->SetToolTip(_("Shifts subs forward, making them appear later. Use if they are appearing too soon."));
shift_backward = new wxRadioButton(this, -1, _("&Backward"));
shift_backward->SetToolTip(_("Shifts subs backward, making them appear earlier. Use if they are appearing too late."));
wxString selection_mode_vals[] = { _("&All rows"), _("Selected &rows"), _("Selection &onward") };
selection_mode = new wxRadioBox(this, -1, _("Affect"), wxDefaultPosition, wxDefaultSize, 3, selection_mode_vals, 1);
wxString time_field_vals[] = { _("Start a&nd End times"), _("&Start times only"), _("&End times only") };
time_fields = new wxRadioBox(this, -1, _("Times"), wxDefaultPosition, wxDefaultSize, 3, time_field_vals, 1);
history_box = new wxListBox(this, -1, wxDefaultPosition, wxSize(350, 100), 0, nullptr, wxLB_HSCROLL);
wxButton *clear_button = new wxButton(this, -1, _("&Clear"));
clear_button->Bind(wxEVT_BUTTON, &DialogShiftTimes::OnClear, this);
// Set initial control states
OnTimecodesLoaded(context->project->Timecodes());
OnSelectedSetChanged();
LoadHistory();
shift_time->SetTime(OPT_GET("Tool/Shift Times/Time")->GetInt());
*shift_frames << (int)OPT_GET("Tool/Shift Times/Frames")->GetInt();
shift_by_frames->SetValue(!OPT_GET("Tool/Shift Times/ByTime")->GetBool() && shift_by_frames->IsEnabled());
time_fields->SetSelection(OPT_GET("Tool/Shift Times/Type")->GetInt());
selection_mode->SetSelection(OPT_GET("Tool/Shift Times/Affect")->GetInt());
shift_backward->SetValue(OPT_GET("Tool/Shift Times/Direction")->GetBool());
if (shift_by_frames->GetValue())
shift_time->Disable();
else
shift_frames->Disable();
// Position controls
wxFlexGridSizer* shift_amount_sizer = new wxFlexGridSizer(2, 2, 5, 5);
shift_amount_sizer->AddGrowableCol(1, 1);
shift_amount_sizer->Add(shift_by_time, wxSizerFlags(0).Align(wxALIGN_CENTER_VERTICAL));
shift_amount_sizer->Add(shift_time, wxSizerFlags().Expand());
shift_amount_sizer->Add(shift_by_frames, wxSizerFlags(0).Align(wxALIGN_CENTER_VERTICAL));
shift_amount_sizer->Add(shift_frames, wxSizerFlags().Expand());
wxSizer *shift_direction_sizer = new wxBoxSizer(wxHORIZONTAL);
shift_direction_sizer->Add(shift_forward, wxSizerFlags(1).Expand());
shift_direction_sizer->Add(shift_backward, wxSizerFlags(1).Expand().Border(wxLEFT));
wxSizer *shift_by_sizer = new wxStaticBoxSizer(wxVERTICAL, this, _("Shift by"));
shift_by_sizer->Add(shift_amount_sizer, wxSizerFlags().Expand());
shift_by_sizer->Add(shift_direction_sizer, wxSizerFlags().Expand().Border(wxTOP));
wxSizer *left_sizer = new wxBoxSizer(wxVERTICAL);
left_sizer->Add(shift_by_sizer, wxSizerFlags().Expand().Border(wxBOTTOM));
left_sizer->Add(selection_mode, wxSizerFlags().Expand().Border(wxBOTTOM));
left_sizer->Add(time_fields, wxSizerFlags().Expand());
wxSizer *history_sizer = new wxStaticBoxSizer(wxVERTICAL, this, _("Load from history"));
history_sizer->Add(history_box, wxSizerFlags(1).Expand());
history_sizer->Add(clear_button, wxSizerFlags().Expand().Border(wxTOP));
wxSizer *top_sizer = new wxBoxSizer(wxHORIZONTAL);
top_sizer->Add(left_sizer, wxSizerFlags().Border(wxALL & ~wxRIGHT).Expand());
top_sizer->Add(history_sizer, wxSizerFlags().Border().Expand());
wxSizer *main_sizer = new wxBoxSizer(wxVERTICAL);
main_sizer->Add(top_sizer, wxSizerFlags().Border(wxALL & ~wxBOTTOM));
main_sizer->Add(CreateButtonSizer(wxOK | wxCANCEL | wxHELP), wxSizerFlags().Right().Border());
SetSizerAndFit(main_sizer);
CenterOnParent();
Bind(wxEVT_BUTTON, &DialogShiftTimes::Process, this, wxID_OK);
Bind(wxEVT_BUTTON, std::bind(&HelpButton::OpenPage, "Shift Times"), wxID_HELP);
shift_time->Bind(wxEVT_TEXT_ENTER, &DialogShiftTimes::Process, this);
history_box->Bind(wxEVT_LISTBOX_DCLICK, &DialogShiftTimes::OnHistoryClick, this);
}
DialogShiftTimes::~DialogShiftTimes() {
long shift;
shift_frames->GetValue().ToLong(&shift);
OPT_SET("Tool/Shift Times/Time")->SetInt(shift_time->GetTime());
OPT_SET("Tool/Shift Times/Frames")->SetInt(shift);
OPT_SET("Tool/Shift Times/ByTime")->SetBool(shift_by_time->GetValue());
OPT_SET("Tool/Shift Times/Type")->SetInt(time_fields->GetSelection());
OPT_SET("Tool/Shift Times/Affect")->SetInt(selection_mode->GetSelection());
OPT_SET("Tool/Shift Times/Direction")->SetBool(shift_backward->GetValue());
}
void DialogShiftTimes::OnTimecodesLoaded(agi::vfr::Framerate const& new_fps) {
fps = new_fps;
if (fps.IsLoaded()) {
shift_by_frames->Enable();
}
else {
shift_by_time->SetValue(true);
shift_by_frames->Disable();
shift_time->Enable();
shift_frames->Disable();
}
}
void DialogShiftTimes::OnSelectedSetChanged() {
if (context->selectionController->GetSelectedSet().empty()) {
selection_mode->Enable(1, false);
selection_mode->Enable(2, false);
selection_mode->SetSelection(0);
}
else {
selection_mode->Enable(1, true);
selection_mode->Enable(2, true);
}
}
void DialogShiftTimes::OnClear(wxCommandEvent &) {
agi::fs::Remove(history_filename);
history_box->Clear();
history.clear();
}
void DialogShiftTimes::OnByTime(wxCommandEvent &) {
shift_time->Enable(true);
shift_frames->Enable(false);
}
void DialogShiftTimes::OnByFrames(wxCommandEvent &) {
shift_time->Enable(false);
shift_frames->Enable(true);
}
void DialogShiftTimes::OnHistoryClick(wxCommandEvent &evt) {
size_t entry = evt.GetInt();
if (entry >= history.size()) return;
json::Object& obj = history[entry];
if (obj["is by time"]) {
shift_time->SetTime(agi::Time((std::string)obj["amount"]));
shift_by_time->SetValue(true);
OnByTime(evt);
}
else {
shift_frames->SetValue(to_wx(obj["amount"]));
if (shift_by_frames->IsEnabled()) {
shift_by_frames->SetValue(true);
OnByFrames(evt);
}
}
if (obj["is backward"])
shift_backward->SetValue(true);
else
shift_forward->SetValue(true);
selection_mode->SetSelection((int64_t)obj["mode"]);
time_fields->SetSelection((int64_t)obj["fields"]);
}
void DialogShiftTimes::SaveHistory(json::Array shifted_blocks) {
json::Object new_entry;
new_entry["filename"] = context->subsController->Filename().filename().string();
new_entry["is by time"] = shift_by_time->GetValue();
new_entry["is backward"] = shift_backward->GetValue();
new_entry["amount"] = from_wx(shift_by_time->GetValue() ? shift_time->GetValue() : shift_frames->GetValue());
new_entry["fields"] = time_fields->GetSelection();
new_entry["mode"] = selection_mode->GetSelection();
new_entry["selection"] = std::move(shifted_blocks);
history.insert(history.begin(), std::move(new_entry));
if (history.size() > 50)
history.resize(50);
try {
agi::JsonWriter::Write(history, agi::io::Save(history_filename).Get());
}
catch (agi::fs::FileSystemError const& e) {
LOG_E("dialog_shift_times/save_history") << "Cannot save shift times history: " << e.GetMessage();
}
}
void DialogShiftTimes::LoadHistory() {
history_box->Clear();
history_box->Freeze();
try {
json::UnknownElement root;
json::Reader::Read(root, *agi::io::Open(history_filename));
history = std::move(static_cast<json::Array&>(root));
for (auto& history_entry : history)
history_box->Append(get_history_string(history_entry));
}
catch (agi::fs::FileSystemError const& e) {
LOG_D("dialog_shift_times/load_history") << "Cannot load shift times history: " << e.GetMessage();
}
catch (json::Exception const& e) {
LOG_D("dialog_shift_times/load_history") << "Cannot load shift times history: " << e.what();
}
catch (...) {
history_box->Thaw();
throw;
}
history_box->Thaw();
}
void DialogShiftTimes::Process(wxCommandEvent &) {
int mode = selection_mode->GetSelection();
int type = time_fields->GetSelection();
bool reverse = shift_backward->GetValue();
bool by_time = shift_by_time->GetValue();
bool start = type != 2;
bool end = type != 1;
auto const& sel = context->selectionController->GetSelectedSet();
long shift;
if (by_time) {
shift = shift_time->GetTime();
if (shift == 0) {
Close();
return;
}
}
else
shift_frames->GetValue().ToLong(&shift);
if (reverse)
shift = -shift;
// Track which rows were shifted for the log
int block_start = 0;
json::Array shifted_blocks;
for (auto& line : context->ass->Events) {
if (!sel.count(&line)) {
if (block_start) {
json::Object block;
block["start"] = block_start;
block["end"] = line.Row;
shifted_blocks.push_back(std::move(block));
block_start = 0;
}
if (mode == 1) continue;
if (mode == 2 && shifted_blocks.empty()) continue;
}
else if (!block_start)
block_start = line.Row + 1;
if (start)
line.Start = Shift(line.Start, shift, by_time, agi::vfr::START);
if (end)
line.End = Shift(line.End, shift, by_time, agi::vfr::END);
}
context->ass->Commit(_("shifting"), AssFile::COMMIT_DIAG_TIME);
if (block_start) {
json::Object block;
block["start"] = block_start;
block["end"] = context->ass->Events.back().Row + 1;
shifted_blocks.push_back(std::move(block));
}
SaveHistory(std::move(shifted_blocks));
Close();
}
int DialogShiftTimes::Shift(int initial_time, int shift, bool by_time, agi::vfr::Time type) {
if (by_time)
return initial_time + shift;
else
return fps.TimeAtFrame(shift + fps.FrameAtTime(initial_time, type), type);
}
}
void ShowShiftTimesDialog(agi::Context *c) {
c->dialog->Show<DialogShiftTimes>(c);
}