Clean up a bunch of stuff in DialogTimingProcessor

Factor out most of the duplicated code and fix a lot of ugly stylistic
things.

Originally committed to SVN as r6310.
This commit is contained in:
Thomas Goyne 2012-01-18 22:51:17 +00:00
parent e40e42ff1c
commit f6c3be4776
5 changed files with 266 additions and 347 deletions

View file

@ -191,6 +191,7 @@
#include <wx/txtstrm.h> #include <wx/txtstrm.h>
#include <wx/utils.h> #include <wx/utils.h>
#include <wx/validate.h> #include <wx/validate.h>
#include <wx/valnum.h>
#include <wx/valtext.h> #include <wx/valtext.h>
#include <wx/wfstream.h> #include <wx/wfstream.h>
#include <wx/window.h> #include <wx/window.h>

View file

@ -36,142 +36,173 @@
#include "config.h" #include "config.h"
#include "dialog_timing_processor.h"
#ifndef AGI_PRE #ifndef AGI_PRE
#include <algorithm> #include <algorithm>
#include <inttypes.h> #include <tr1/functional>
#include <wx/button.h>
#include <wx/checkbox.h>
#include <wx/checklst.h>
#include <wx/msgdlg.h> #include <wx/msgdlg.h>
#include <wx/sizer.h>
#include <wx/slider.h>
#include <wx/statbox.h>
#include <wx/stattext.h> #include <wx/stattext.h>
#include <wx/textctrl.h>
#include <wx/valnum.h>
#endif #endif
#include "ass_dialogue.h" #include "ass_dialogue.h"
#include "ass_file.h" #include "ass_file.h"
#include "ass_time.h" #include "ass_time.h"
#include "dialog_timing_processor.h"
#include "help_button.h" #include "help_button.h"
#include "include/aegisub/context.h" #include "include/aegisub/context.h"
#include "libresrc/libresrc.h" #include "libresrc/libresrc.h"
#include "main.h" #include "main.h"
#include "selection_controller.h" #include "selection_controller.h"
#include "utils.h" #include "utils.h"
#include "validators.h"
#include "video_context.h" #include "video_context.h"
/// Window IDs namespace {
enum { using std::tr1::placeholders::_1;
CHECK_ENABLE_LEADIN = 1850,
CHECK_ENABLE_LEADOUT, void set_ctrl_state(wxCommandEvent &evt, wxCheckBox *cb, wxTextCtrl *tc) {
CHECK_ENABLE_KEYFRAME, tc->Enable(cb->IsChecked());
CHECK_ENABLE_ADJASCENT, evt.Skip();
BUTTON_SELECT_ALL, }
BUTTON_SELECT_NONE,
TIMING_STYLE_LIST wxTextCtrl *make_ctrl(wxWindow *parent, wxSizer *sizer, wxString const& desc, int *value, wxCheckBox *cb, wxString const& tooltip) {
}; wxIntegerValidator<int> validator(value);
validator.SetMin(0);
wxTextCtrl *ctrl = new wxTextCtrl(parent, -1, "", wxDefaultPosition, wxSize(60,-1), 0, validator);
ctrl->SetToolTip(tooltip);
if (!desc.empty())
sizer->Add(new wxStaticText(parent, -1, desc), wxSizerFlags().Center().Border(wxRIGHT));
sizer->Add(ctrl, wxSizerFlags().Expand().Border(wxRIGHT));
ctrl->Enable(cb->IsChecked());
cb->Bind(wxEVT_COMMAND_CHECKBOX_CLICKED, bind(set_ctrl_state, _1, cb, ctrl));
return ctrl;
}
inline wxTextCtrl *make_ctrl(wxStaticBoxSizer *sizer, wxString const& desc, int *value, wxCheckBox *cb, wxString const& tooltip) {
return make_ctrl(sizer->GetStaticBox()->GetParent(), sizer, desc, value, cb, tooltip);
}
wxCheckBox *make_check(wxStaticBoxSizer *sizer, wxString const& desc, const char *opt, wxString const& tooltip) {
wxCheckBox *cb = new wxCheckBox(sizer->GetStaticBox()->GetParent(), -1, desc);
cb->SetToolTip(tooltip);
cb->SetValue(OPT_GET(opt)->GetBool());
sizer->Add(cb, wxSizerFlags().Border(wxRIGHT).Expand());
return cb;
}
}
DialogTimingProcessor::DialogTimingProcessor(agi::Context *c) DialogTimingProcessor::DialogTimingProcessor(agi::Context *c)
: wxDialog(c->parent,-1,_("Timing Post-Processor"),wxDefaultPosition,wxSize(400,250),wxDEFAULT_DIALOG_STYLE) : wxDialog(c->parent, -1, _("Timing Post-Processor"))
, c(c) , c(c)
{ {
using std::tr1::bind;
SetIcon(BitmapToIcon(GETIMAGE(timing_processor_toolbutton_24))); SetIcon(BitmapToIcon(GETIMAGE(timing_processor_toolbutton_24)));
// Set variables // Read options
wxString leadInTime(wxString::Format("%" PRId64, OPT_GET("Audio/Lead/IN")->GetInt())); leadIn = OPT_GET("Audio/Lead/IN")->GetInt();
wxString leadOutTime(wxString::Format("%" PRId64, OPT_GET("Audio/Lead/OUT")->GetInt())); leadOut = OPT_GET("Audio/Lead/OUT")->GetInt();
wxString thresStartBefore(wxString::Format("%" PRId64, OPT_GET("Tool/Timing Post Processor/Threshold/Key Start Before")->GetInt())); beforeStart = OPT_GET("Tool/Timing Post Processor/Threshold/Key Start Before")->GetInt();
wxString thresStartAfter(wxString::Format("%" PRId64, OPT_GET("Tool/Timing Post Processor/Threshold/Key Start After")->GetInt())); beforeEnd = OPT_GET("Tool/Timing Post Processor/Threshold/Key End Before")->GetInt();
wxString thresEndBefore(wxString::Format("%" PRId64, OPT_GET("Tool/Timing Post Processor/Threshold/Key End Before")->GetInt())); afterStart = OPT_GET("Tool/Timing Post Processor/Threshold/Key Start After")->GetInt();
wxString thresEndAfter(wxString::Format("%" PRId64, OPT_GET("Tool/Timing Post Processor/Threshold/Key End After")->GetInt())); afterEnd = OPT_GET("Tool/Timing Post Processor/Threshold/Key End After")->GetInt();
wxString adjsThresTime(wxString::Format("%" PRId64, OPT_GET("Tool/Timing Post Processor/Threshold/Adjacent")->GetInt())); adjDistance = OPT_GET("Tool/Timing Post Processor/Threshold/Adjacent")->GetInt();
// Styles box // Styles box
wxSizer *LeftSizer = new wxStaticBoxSizer(wxVERTICAL,this,_("Apply to styles")); wxSizer *LeftSizer = new wxStaticBoxSizer(wxVERTICAL,this,_("Apply to styles"));
wxArrayString styles = c->ass->GetStyles(); StyleList = new wxCheckListBox(this, -1, wxDefaultPosition, wxSize(150,150), c->ass->GetStyles());
StyleList = new wxCheckListBox(this,TIMING_STYLE_LIST,wxDefaultPosition,wxSize(150,150),styles);
StyleList->SetToolTip(_("Select styles to process. Unchecked ones will be ignored.")); StyleList->SetToolTip(_("Select styles to process. Unchecked ones will be ignored."));
wxButton *all = new wxButton(this,BUTTON_SELECT_ALL,_("&All"));
wxButton *all = new wxButton(this,-1,_("&All"));
all->SetToolTip(_("Select all styles.")); all->SetToolTip(_("Select all styles."));
wxButton *none = new wxButton(this,BUTTON_SELECT_NONE,_("&None"));
wxButton *none = new wxButton(this,-1,_("&None"));
none->SetToolTip(_("Deselect all styles.")); none->SetToolTip(_("Deselect all styles."));
// Options box // Options box
wxSizer *optionsSizer = new wxStaticBoxSizer(wxHORIZONTAL,this,_("Options")); wxStaticBoxSizer *optionsSizer = new wxStaticBoxSizer(wxHORIZONTAL,this,_("Options"));
onlySelection = new wxCheckBox(this,-1,_("Affect &selection only")); onlySelection = new wxCheckBox(this,-1,_("Affect &selection only"));
onlySelection->SetValue(OPT_GET("Tool/Timing Post Processor/Only Selection")->GetBool()); onlySelection->SetValue(OPT_GET("Tool/Timing Post Processor/Only Selection")->GetBool());
optionsSizer->Add(onlySelection,1,wxALL,0); optionsSizer->Add(onlySelection,1,wxALL,0);
// Lead-in/out box // Lead-in/out box
wxSizer *LeadSizer = new wxStaticBoxSizer(wxHORIZONTAL,this,_("Lead-in/Lead-out")); wxStaticBoxSizer *LeadSizer = new wxStaticBoxSizer(wxHORIZONTAL, this, _("Lead-in/Lead-out"));
hasLeadIn = new wxCheckBox(this,CHECK_ENABLE_LEADIN,_("Add lead &in:"));
hasLeadIn->SetToolTip(_("Enable adding of lead-ins to lines.")); hasLeadIn = make_check(LeadSizer, _("Add lead &in:"),
hasLeadIn->SetValue(OPT_GET("Tool/Timing Post Processor/Enable/Lead/IN")->GetBool()); "Tool/Timing Post Processor/Enable/Lead/IN",
leadIn = new wxTextCtrl(this,-1,"",wxDefaultPosition,wxSize(80,-1),0,NumValidator(leadInTime)); _("Enable adding of lead-ins to lines."));
leadIn->SetToolTip(_("Lead in to be added, in milliseconds.")); make_ctrl(LeadSizer, "", &leadIn, hasLeadIn, _("Lead in to be added, in milliseconds."));
hasLeadOut = new wxCheckBox(this,CHECK_ENABLE_LEADOUT,_("Add lead &out:"));
hasLeadOut->SetToolTip(_("Enable adding of lead-outs to lines.")); hasLeadOut = make_check(LeadSizer, _("Add lead &out:"),
hasLeadOut->SetValue(OPT_GET("Tool/Timing Post Processor/Enable/Lead/OUT")->GetBool()); "Tool/Timing Post Processor/Enable/Lead/OUT",
leadOut = new wxTextCtrl(this,-1,"",wxDefaultPosition,wxSize(80,-1),0,NumValidator(leadOutTime)); _("Enable adding of lead-outs to lines."));
leadOut->SetToolTip(_("Lead out to be added, in milliseconds.")); make_ctrl(LeadSizer, "", &leadOut, hasLeadOut, _("Lead out to be added, in milliseconds."));
LeadSizer->Add(hasLeadIn,0,wxRIGHT|wxEXPAND,5);
LeadSizer->Add(leadIn,0,wxRIGHT|wxEXPAND,5);
LeadSizer->Add(hasLeadOut,0,wxRIGHT|wxEXPAND,5);
LeadSizer->Add(leadOut,0,wxRIGHT|wxEXPAND,0);
LeadSizer->AddStretchSpacer(1); LeadSizer->AddStretchSpacer(1);
// Adjacent subs sizer // Adjacent subs sizer
wxSizer *AdjacentSizer = new wxStaticBoxSizer(wxHORIZONTAL,this,_("Make adjacent subtitles continuous")); wxStaticBoxSizer *AdjacentSizer = new wxStaticBoxSizer(wxHORIZONTAL, this, _("Make adjacent subtitles continuous"));
adjsEnable = new wxCheckBox(this,CHECK_ENABLE_ADJASCENT,_("&Enable")); adjsEnable = make_check(AdjacentSizer, _("&Enable"),
adjsEnable->SetToolTip(_("Enable snapping of subtitles together if they are within a certain distance of each other.")); "Tool/Timing Post Processor/Enable/Adjacent",
adjsEnable->SetValue(OPT_GET("Tool/Timing Post Processor/Enable/Adjacent")->GetBool()); _("Enable snapping of subtitles together if they are within a certain distance of each other."));
wxStaticText *adjsThresText = new wxStaticText(this,-1,_("Threshold:"),wxDefaultPosition,wxDefaultSize,wxALIGN_CENTRE);
adjacentThres = new wxTextCtrl(this,-1,"",wxDefaultPosition,wxSize(60,-1),0,NumValidator(adjsThresTime)); make_ctrl(AdjacentSizer, _("Threshold:"), &adjDistance, adjsEnable,
adjacentThres->SetToolTip(_("Maximum difference between start and end time for two subtitles to be made continuous, in milliseconds.")); _("Maximum difference between start and end time for two subtitles to be made continuous, in milliseconds."));
adjacentBias = new wxSlider(this,-1,mid(0,int(OPT_GET("Tool/Timing Post Processor/Adjacent Bias")->GetDouble()*100),100),0,100,wxDefaultPosition,wxSize(-1,20));
adjacentBias = new wxSlider(this, -1, mid<int>(0, OPT_GET("Tool/Timing Post Processor/Adjacent Bias")->GetDouble() * 100, 100), 0, 100, wxDefaultPosition, wxSize(-1,20));
adjacentBias->SetToolTip(_("Sets how to set the adjoining of lines. If set totally to left, it will extend start time of the second line; if totally to right, it will extend the end time of the first line.")); adjacentBias->SetToolTip(_("Sets how to set the adjoining of lines. If set totally to left, it will extend start time of the second line; if totally to right, it will extend the end time of the first line."));
AdjacentSizer->Add(adjsEnable,0,wxRIGHT|wxEXPAND,10);
AdjacentSizer->Add(adjsThresText,0,wxRIGHT|wxALIGN_CENTER,5); AdjacentSizer->Add(new wxStaticText(this, -1, _("Bias: Start <- ")), wxSizerFlags().Center());
AdjacentSizer->Add(adjacentThres,0,wxRIGHT|wxEXPAND,5); AdjacentSizer->Add(adjacentBias, wxSizerFlags(1).Expand().Center());
AdjacentSizer->Add(new wxStaticText(this,-1,_("Bias: Start <- "),wxDefaultPosition,wxDefaultSize,wxALIGN_CENTRE),0,wxALIGN_CENTER,0); AdjacentSizer->Add(new wxStaticText(this, -1, _(" -> End")), wxSizerFlags().Center());
AdjacentSizer->Add(adjacentBias,1,wxEXPAND,0);
AdjacentSizer->Add(new wxStaticText(this,-1,_(" -> End"),wxDefaultPosition,wxDefaultSize,wxALIGN_CENTRE),0,wxALIGN_CENTER,0);
// Keyframes sizer // Keyframes sizer
KeyframesSizer = new wxStaticBoxSizer(wxHORIZONTAL,this,_("Keyframe snapping")); wxStaticBoxSizer *KeyframesSizer = new wxStaticBoxSizer(wxHORIZONTAL, this, _("Keyframe snapping"));
wxSizer *KeyframesFlexSizer = new wxFlexGridSizer(2,5,5,0); wxSizer *KeyframesFlexSizer = new wxFlexGridSizer(2,5,5,0);
keysEnable = new wxCheckBox(this,CHECK_ENABLE_KEYFRAME,_("E&nable"));
keysEnable = new wxCheckBox(this, -1, _("E&nable"));
keysEnable->SetToolTip(_("Enable snapping of subtitles to nearest keyframe, if distance is within threshold.")); keysEnable->SetToolTip(_("Enable snapping of subtitles to nearest keyframe, if distance is within threshold."));
keysEnable->SetValue(OPT_GET("Tool/Timing Post Processor/Enable/Keyframe")->GetBool()); keysEnable->SetValue(OPT_GET("Tool/Timing Post Processor/Enable/Keyframe")->GetBool());
wxStaticText *textStartBefore = new wxStaticText(this,-1,_("Starts before thres.:"),wxDefaultPosition,wxDefaultSize,wxALIGN_CENTRE);
keysStartBefore = new wxTextCtrl(this,-1,"",wxDefaultPosition,wxSize(60,-1),0,NumValidator(thresStartBefore));
keysStartBefore->SetToolTip(_("Threshold for 'before start' distance, that is, how many frames a subtitle must start before a keyframe to snap to it."));
wxStaticText *textStartAfter = new wxStaticText(this,-1,_("Starts after thres.:"),wxDefaultPosition,wxDefaultSize,wxALIGN_CENTRE);
keysStartAfter = new wxTextCtrl(this,-1,"",wxDefaultPosition,wxSize(60,-1),0,NumValidator(thresStartAfter));
keysStartAfter->SetToolTip(_("Threshold for 'after start' distance, that is, how many frames a subtitle must start after a keyframe to snap to it."));
wxStaticText *textEndBefore = new wxStaticText(this,-1,_("Ends before thres.:"),wxDefaultPosition,wxDefaultSize,wxALIGN_CENTRE);
keysEndBefore = new wxTextCtrl(this,-1,"",wxDefaultPosition,wxSize(60,-1),0,NumValidator(thresEndBefore));
keysEndBefore->SetToolTip(_("Threshold for 'before end' distance, that is, how many frames a subtitle must end before a keyframe to snap to it."));
wxStaticText *textEndAfter = new wxStaticText(this,-1,_("Ends after thres.:"),wxDefaultPosition,wxDefaultSize,wxALIGN_CENTRE);
keysEndAfter = new wxTextCtrl(this,-1,"",wxDefaultPosition,wxSize(60,-1),0,NumValidator(thresEndAfter));
keysEndAfter->SetToolTip(_("Threshold for 'after end' distance, that is, how many frames a subtitle must end after a keyframe to snap to it."));
KeyframesFlexSizer->Add(keysEnable,0,wxRIGHT|wxEXPAND,10); KeyframesFlexSizer->Add(keysEnable,0,wxRIGHT|wxEXPAND,10);
KeyframesFlexSizer->Add(textStartBefore,0,wxRIGHT|wxALIGN_CENTER,5);
KeyframesFlexSizer->Add(keysStartBefore,0,wxRIGHT|wxEXPAND,5); // Keyframes are only available if timecodes are loaded
KeyframesFlexSizer->Add(textStartAfter,0,wxRIGHT|wxALIGN_CENTER,5); bool keysAvailable = c->videoController->KeyFramesLoaded() && c->videoController->TimecodesLoaded();
KeyframesFlexSizer->Add(keysStartAfter,0,wxRIGHT|wxEXPAND,0); if (!keysAvailable) {
keysEnable->SetValue(false);
keysEnable->Enable(false);
}
make_ctrl(this, KeyframesFlexSizer, _("Starts before thres.:"), &beforeStart, keysEnable,
_("Threshold for 'before start' distance, that is, how many frames a subtitle must start before a keyframe to snap to it."));
make_ctrl(this, KeyframesFlexSizer, _("Starts after thres.:"), &afterStart, keysEnable,
_("Threshold for 'after start' distance, that is, how many frames a subtitle must start after a keyframe to snap to it."));
KeyframesFlexSizer->AddStretchSpacer(1); KeyframesFlexSizer->AddStretchSpacer(1);
KeyframesFlexSizer->Add(textEndBefore,0,wxRIGHT|wxALIGN_CENTER,5);
KeyframesFlexSizer->Add(keysEndBefore,0,wxRIGHT|wxEXPAND,5); make_ctrl(this, KeyframesFlexSizer, _("Ends before thres.:"), &beforeEnd, keysEnable,
KeyframesFlexSizer->Add(textEndAfter,0,wxRIGHT|wxALIGN_CENTER,5); _("Threshold for 'before end' distance, that is, how many frames a subtitle must end before a keyframe to snap to it."));
KeyframesFlexSizer->Add(keysEndAfter,0,wxRIGHT|wxEXPAND,0);
make_ctrl(this, KeyframesFlexSizer, _("Ends after thres.:"), &afterEnd, keysEnable,
_("Threshold for 'after end' distance, that is, how many frames a subtitle must end after a keyframe to snap to it."));
KeyframesSizer->Add(KeyframesFlexSizer,0,wxEXPAND); KeyframesSizer->Add(KeyframesFlexSizer,0,wxEXPAND);
KeyframesSizer->AddStretchSpacer(1); KeyframesSizer->AddStretchSpacer(1);
// Button sizer // Button sizer
wxStdDialogButtonSizer *ButtonSizer = new wxStdDialogButtonSizer(); wxStdDialogButtonSizer *ButtonSizer = CreateStdDialogButtonSizer(wxOK | wxCANCEL | wxHELP);
ApplyButton = new wxButton(this,wxID_OK); ApplyButton = ButtonSizer->GetAffirmativeButton();
ButtonSizer->AddButton(ApplyButton); ButtonSizer->GetHelpButton()->Bind(wxEVT_COMMAND_BUTTON_CLICKED, bind(&HelpButton::OpenPage, "Timing Processor"));
ButtonSizer->AddButton(new wxButton(this,wxID_CANCEL));
ButtonSizer->AddButton(new HelpButton(this,"Timing Processor"));
ButtonSizer->Realize();
// Right Sizer // Right Sizer
wxSizer *RightSizer = new wxBoxSizer(wxVERTICAL); wxSizer *RightSizer = new wxBoxSizer(wxVERTICAL);
@ -188,12 +219,8 @@ DialogTimingProcessor::DialogTimingProcessor(agi::Context *c)
StyleButtonsSizer->Add(none,1,0,0); StyleButtonsSizer->Add(none,1,0,0);
// Left sizer // Left sizer
size_t len = StyleList->GetCount(); LeftSizer->Add(StyleList, wxSizerFlags(1).Border(wxBOTTOM));
for (size_t i=0;i<len;i++) { LeftSizer->Add(StyleButtonsSizer, wxSizerFlags().Expand());
StyleList->Check(i);
}
LeftSizer->Add(StyleList,1,wxBOTTOM|wxEXPAND,0);
LeftSizer->Add(StyleButtonsSizer,0,wxEXPAND,0);
// Top Sizer // Top Sizer
wxSizer *TopSizer = new wxBoxSizer(wxHORIZONTAL); wxSizer *TopSizer = new wxBoxSizer(wxHORIZONTAL);
@ -203,92 +230,46 @@ DialogTimingProcessor::DialogTimingProcessor(agi::Context *c)
// Main Sizer // Main Sizer
wxSizer *MainSizer = new wxBoxSizer(wxVERTICAL); wxSizer *MainSizer = new wxBoxSizer(wxVERTICAL);
MainSizer->Add(TopSizer,1,wxALL|wxEXPAND,5); MainSizer->Add(TopSizer,1,wxALL|wxEXPAND,5);
MainSizer->SetSizeHints(this); SetSizerAndFit(MainSizer);
SetSizer(MainSizer);
CenterOnParent(); CenterOnParent();
Bind(wxEVT_COMMAND_CHECKBOX_CLICKED, bind(&DialogTimingProcessor::UpdateControls, this));
Bind(wxEVT_COMMAND_BUTTON_CLICKED, &DialogTimingProcessor::OnApply, this, wxID_OK);
all->Bind(wxEVT_COMMAND_BUTTON_CLICKED, bind(&DialogTimingProcessor::CheckAll, this, true));
none->Bind(wxEVT_COMMAND_BUTTON_CLICKED, bind(&DialogTimingProcessor::CheckAll, this, false));
CheckAll(true);
}
void DialogTimingProcessor::CheckAll(bool value) {
size_t count = StyleList->GetCount();
for (size_t i = 0; i < count; ++i)
StyleList->Check(i, value);
UpdateControls(); UpdateControls();
} }
/// @brief Update controls
///
void DialogTimingProcessor::UpdateControls() { void DialogTimingProcessor::UpdateControls() {
// Boxes // Only enable the OK button if it'll actually do something
leadIn->Enable(hasLeadIn->IsChecked()); bool any_checked = false;
leadOut->Enable(hasLeadOut->IsChecked());
adjacentThres->Enable(adjsEnable->IsChecked());
adjacentBias->Enable(adjsEnable->IsChecked());
// Keyframes are only available if timecodes are loaded
bool keysAvailable = c->videoController->KeyFramesLoaded();
bool enableKeys = keysEnable->IsChecked() && keysAvailable;
keysStartBefore->Enable(enableKeys);
keysStartAfter->Enable(enableKeys);
keysEndBefore->Enable(enableKeys);
keysEndAfter->Enable(enableKeys);
if (!keysAvailable) {
keysEnable->SetValue(false);
keysEnable->Enable(false);
}
// Apply button
int checked = 0;
size_t len = StyleList->GetCount(); size_t len = StyleList->GetCount();
for (size_t i=0;i<len;i++) { for (size_t i = 0; i < len; ++i) {
if (StyleList->IsChecked(i)) checked++; if (StyleList->IsChecked(i)) {
any_checked = true;
break;
}
} }
ApplyButton->Enable(checked && (hasLeadIn->IsChecked() || hasLeadOut->IsChecked() || keysEnable->IsChecked() || adjsEnable->IsChecked())); ApplyButton->Enable(any_checked && (hasLeadIn->IsChecked() || hasLeadOut->IsChecked() || keysEnable->IsChecked() || adjsEnable->IsChecked()));
}
BEGIN_EVENT_TABLE(DialogTimingProcessor,wxDialog)
EVT_CHECKBOX(CHECK_ENABLE_LEADIN,DialogTimingProcessor::OnCheckBox)
EVT_CHECKBOX(CHECK_ENABLE_LEADOUT,DialogTimingProcessor::OnCheckBox)
EVT_CHECKBOX(CHECK_ENABLE_KEYFRAME,DialogTimingProcessor::OnCheckBox)
EVT_CHECKBOX(CHECK_ENABLE_ADJASCENT,DialogTimingProcessor::OnCheckBox)
EVT_CHECKLISTBOX(TIMING_STYLE_LIST,DialogTimingProcessor::OnCheckBox)
EVT_BUTTON(wxID_OK,DialogTimingProcessor::OnApply)
EVT_BUTTON(BUTTON_SELECT_ALL,DialogTimingProcessor::OnSelectAll)
EVT_BUTTON(BUTTON_SELECT_NONE,DialogTimingProcessor::OnSelectNone)
END_EVENT_TABLE()
void DialogTimingProcessor::OnCheckBox(wxCommandEvent &) {
UpdateControls();
}
void DialogTimingProcessor::OnSelectAll(wxCommandEvent &) {
size_t len = StyleList->GetCount();
for (size_t i=0;i<len;i++) {
StyleList->Check(i);
}
UpdateControls();
}
void DialogTimingProcessor::OnSelectNone(wxCommandEvent &) {
size_t len = StyleList->GetCount();
for (size_t i=0;i<len;i++) {
StyleList->Check(i,false);
}
UpdateControls();
} }
void DialogTimingProcessor::OnApply(wxCommandEvent &) { void DialogTimingProcessor::OnApply(wxCommandEvent &) {
// Save settings // Save settings
long temp = 0; OPT_SET("Audio/Lead/IN")->SetInt(leadIn);
leadIn->GetValue().ToLong(&temp); OPT_SET("Audio/Lead/OUT")->SetInt(leadOut);
OPT_SET("Audio/Lead/IN")->SetInt(temp); OPT_SET("Tool/Timing Post Processor/Threshold/Key Start Before")->SetInt(beforeStart);
leadOut->GetValue().ToLong(&temp); OPT_SET("Tool/Timing Post Processor/Threshold/Key Start After")->SetInt(afterStart);
OPT_SET("Audio/Lead/OUT")->SetInt(temp); OPT_SET("Tool/Timing Post Processor/Threshold/Key End Before")->SetInt(beforeEnd);
keysStartBefore->GetValue().ToLong(&temp); OPT_SET("Tool/Timing Post Processor/Threshold/Key End After")->SetInt(afterEnd);
OPT_SET("Tool/Timing Post Processor/Threshold/Key Start Before")->SetInt(temp); OPT_SET("Tool/Timing Post Processor/Threshold/Adjacent")->SetInt(adjDistance);
keysStartAfter->GetValue().ToLong(&temp);
OPT_SET("Tool/Timing Post Processor/Threshold/Key Start After")->SetInt(temp);
keysEndBefore->GetValue().ToLong(&temp);
OPT_SET("Tool/Timing Post Processor/Threshold/Key End Before")->SetInt(temp);
keysEndAfter->GetValue().ToLong(&temp);
OPT_SET("Tool/Timing Post Processor/Threshold/Key End After")->SetInt(temp);
adjacentThres->GetValue().ToLong(&temp);
OPT_SET("Tool/Timing Post Processor/Threshold/Adjacent")->SetInt(temp);
OPT_SET("Tool/Timing Post Processor/Adjacent Bias")->SetDouble(adjacentBias->GetValue() / 100.0); OPT_SET("Tool/Timing Post Processor/Adjacent Bias")->SetDouble(adjacentBias->GetValue() / 100.0);
OPT_SET("Tool/Timing Post Processor/Enable/Lead/IN")->SetBool(hasLeadIn->IsChecked()); OPT_SET("Tool/Timing Post Processor/Enable/Lead/IN")->SetBool(hasLeadIn->IsChecked());
OPT_SET("Tool/Timing Post Processor/Enable/Lead/OUT")->SetBool(hasLeadOut->IsChecked()); OPT_SET("Tool/Timing Post Processor/Enable/Lead/OUT")->SetBool(hasLeadOut->IsChecked());
@ -296,174 +277,137 @@ void DialogTimingProcessor::OnApply(wxCommandEvent &) {
OPT_SET("Tool/Timing Post Processor/Enable/Adjacent")->SetBool(adjsEnable->IsChecked()); OPT_SET("Tool/Timing Post Processor/Enable/Adjacent")->SetBool(adjsEnable->IsChecked());
OPT_SET("Tool/Timing Post Processor/Only Selection")->SetBool(onlySelection->IsChecked()); OPT_SET("Tool/Timing Post Processor/Only Selection")->SetBool(onlySelection->IsChecked());
// Check if rows are valid
for (entryIter cur = c->ass->Line.begin(); cur != c->ass->Line.end(); ++cur) {
if (AssDialogue *tempDiag = dynamic_cast<AssDialogue*>(*cur)) {
if (tempDiag->Start > tempDiag->End) {
wxMessageBox(
wxString::Format(
_("One of the lines in the file (%i) has negative duration. Aborting."),
std::distance(c->ass->Line.begin(), cur)),
_("Invalid script"),
wxICON_ERROR|wxOK);
EndModal(0);
return;
}
}
}
Process(); Process();
EndModal(0); EndModal(0);
} }
int DialogTimingProcessor::GetClosestKeyFrame(int frame) {
std::vector<int>::iterator pos = lower_bound(KeyFrames.begin(), KeyFrames.end(), frame);
if (distance(pos, KeyFrames.end()) < 2) return KeyFrames.back();
return frame - *pos < *(pos + 1) - frame ? *pos : *(pos + 1);
}
static bool bad_line(std::set<wxString> *styles, AssDialogue *d) { static bool bad_line(std::set<wxString> *styles, AssDialogue *d) {
return !d || d->Comment || styles->find(d->Style) == styles->end(); return !d || d->Comment || styles->find(d->Style) == styles->end();
} }
void DialogTimingProcessor::SortDialogues() { std::vector<AssDialogue*> DialogTimingProcessor::SortDialogues() {
std::set<wxString> styles; std::set<wxString> styles;
for (size_t i = 0; i < StyleList->GetCount(); ++i) { for (size_t i = 0; i < StyleList->GetCount(); ++i) {
if (StyleList->IsChecked(i)) { if (StyleList->IsChecked(i))
styles.insert(StyleList->GetString(i)); styles.insert(StyleList->GetString(i));
}
std::vector<AssDialogue*> sorted;
sorted.reserve(c->ass->Line.size());
if (onlySelection->IsChecked()) {
SelectionController<AssDialogue>::Selection sel = c->selectionController->GetSelectedSet();
remove_copy_if(sel.begin(), sel.end(), back_inserter(sorted),
bind(bad_line, &styles, _1));
}
else {
transform(c->ass->Line.begin(), c->ass->Line.end(), back_inserter(sorted), cast<AssDialogue*>());
sorted.erase(remove_if(sorted.begin(), sorted.end(), bind(bad_line, &styles, _1)), sorted.end());
}
// Check if rows are valid
for (size_t i = 0; i < sorted.size(); ++i) {
if (sorted[i]->Start > sorted[i]->End) {
wxMessageBox(
wxString::Format(_("One of the lines in the file (%i) has negative duration. Aborting."), i),
_("Invalid script"),
wxICON_ERROR|wxOK);
sorted.clear();
break;
} }
} }
Sorted.clear(); sort(sorted.begin(), sorted.end(), AssFile::CompStart);
Sorted.reserve(c->ass->Line.size()); return sorted;
if (onlySelection->IsChecked()) { }
SelectionController<AssDialogue>::Selection sel = c->selectionController->GetSelectedSet();
remove_copy_if(sel.begin(), sel.end(), back_inserter(Sorted), static int get_closest_kf(std::vector<int> const& kf, int frame) {
bind(bad_line, &styles, std::tr1::placeholders::_1)); std::vector<int>::const_iterator pos = lower_bound(kf.begin(), kf.end(), frame);
} // Return last keyframe if this is on or after the last one
else { if (distance(pos, kf.end()) < 2) return kf.back();
std::vector<AssDialogue*> tmp(c->ass->Line.size()); // Check if this one or the one after is closer to the frame
transform(c->ass->Line.begin(), c->ass->Line.end(), back_inserter(tmp), cast<AssDialogue*>()); return frame - *pos < *(pos + 1) - frame ? *pos : *(pos + 1);
remove_copy_if(tmp.begin(), tmp.end(), back_inserter(Sorted), }
bind(bad_line, &styles, std::tr1::placeholders::_1));
} template<class Iter, class Field>
sort(Sorted.begin(), Sorted.end(), AssFile::CompStart); static int safe_time(Iter begin, Iter end, AssDialogue *comp, int initial, Field field, int const& (*cmp)(int const&, int const&)) {
// Compare to every previous line (yay for O(n^2)!) to see if it's OK to add lead-in
for (; begin != end; ++begin) {
// If the line doesn't already collide with this line, extend it only
// to the edge of the line
if (!comp->CollidesWith(*begin))
initial = cmp(initial, (*begin)->*field);
}
return initial;
} }
/// @brief Actually process subtitles
///
void DialogTimingProcessor::Process() { void DialogTimingProcessor::Process() {
SortDialogues(); std::vector<AssDialogue*> sorted = SortDialogues();
int rows = Sorted.size(); if (sorted.empty()) return;
// Options // Options
long inVal = 0; bool addIn = hasLeadIn->IsChecked() && leadIn;
long outVal = 0; bool addOut = hasLeadOut->IsChecked() && leadOut;
leadIn->GetValue().ToLong(&inVal);
leadOut->GetValue().ToLong(&outVal);
bool addIn = hasLeadIn->IsChecked() && inVal;
bool addOut = hasLeadOut->IsChecked() && outVal;
// Add lead-in/out // Add lead-in/out
if (addIn || addOut) { if (addIn || addOut) {
for (int i=0;i<rows;i++) { for (size_t i = 0; i < sorted.size(); ++i) {
AssDialogue *cur = Sorted[i]; AssDialogue *cur = sorted[i];
if (addIn)
cur->Start = safe_time(sorted.begin() + i + 1, sorted.end(), cur, cur->Start - leadIn, &AssDialogue::End, &std::max<int>);
// Compare to every previous line (yay for O(n^2)!) to see if it's OK to add lead-in if (addOut)
if (inVal) { cur->End = safe_time(sorted.rend() - i, sorted.rend(), cur, cur->End + leadOut, &AssDialogue::Start, &std::min<int>);
int startLead = cur->Start - inVal;
for (int j=0;j<i;j++) {
AssDialogue *comp = Sorted[j];
// Check if they don't already collide (ignore it in that case)
if (cur->CollidesWith(comp)) continue;
// Get comparison times
startLead = std::max<int>(startLead, comp->End);
}
cur->Start = startLead;
}
// Compare to every line to see how far can lead-out be extended
if (outVal) {
int endLead = cur->End + outVal;
for (int j=i+1;j<rows;j++) {
AssDialogue *comp = Sorted[j];
// Check if they don't already collide (ignore it in that case)
if (cur->CollidesWith(comp)) continue;
// Get comparison times
endLead = std::min<int>(endLead, comp->Start);
}
cur->End = endLead;
}
} }
} }
// Make adjacent // Make adjacent
if (adjsEnable->IsChecked()) { if (adjsEnable->IsChecked()) {
AssDialogue *prev = Sorted.front(); double bias = adjacentBias->GetValue() / 100.0;
long adjsThres = 0;
adjacentThres->GetValue().ToLong(&adjsThres);
float bias = adjacentBias->GetValue() / 100.0; for (size_t i = 1; i < sorted.size(); ++i) {
AssDialogue *prev = sorted[i - 1];
for (int i=1; i < rows;i++) { AssDialogue *cur = sorted[i];
AssDialogue *cur = Sorted[i];
// Check if they don't collide // Check if they don't collide
if (cur->CollidesWith(prev)) continue; if (cur->CollidesWith(prev)) continue;
// Compare distance // Compare distance
int dist = cur->Start - prev->End; int dist = cur->Start - prev->End;
if (dist > 0 && dist <= adjsThres) { if (dist > 0 && dist <= adjDistance) {
int setPos = prev->End + int(dist*bias); int setPos = prev->End + int(dist * bias);
cur->Start = setPos; cur->Start = setPos;
prev->End = setPos; prev->End = setPos;
} }
prev = cur;
} }
} }
// Keyframe snapping // Keyframe snapping
if (keysEnable->IsChecked()) { if (keysEnable->IsChecked()) {
KeyFrames = c->videoController->GetKeyFrames(); std::vector<int> kf = c->videoController->GetKeyFrames();
if (c->videoController->IsLoaded()) if (c->videoController->IsLoaded())
KeyFrames.push_back(c->videoController->GetLength() - 1); kf.push_back(c->videoController->GetLength() - 1);
long beforeStart = 0; for (size_t i = 0; i < sorted.size(); ++i) {
long afterStart = 0; AssDialogue *cur = sorted[i];
long beforeEnd = 0;
long afterEnd = 0;
keysStartBefore->GetValue().ToLong(&beforeStart);
keysStartAfter->GetValue().ToLong(&afterStart);
keysEndBefore->GetValue().ToLong(&beforeEnd);
keysEndAfter->GetValue().ToLong(&afterEnd);
for (int i=0;i<rows;i++) {
AssDialogue *cur = Sorted[i];
// Get start/end frames // Get start/end frames
int startF = c->videoController->FrameAtTime(cur->Start,agi::vfr::START); int startF = c->videoController->FrameAtTime(cur->Start, agi::vfr::START);
int endF = c->videoController->FrameAtTime(cur->End,agi::vfr::END); int endF = c->videoController->FrameAtTime(cur->End, agi::vfr::END);
// Get closest for start // Get closest for start
int closest = GetClosestKeyFrame(startF); int closest = get_closest_kf(kf, startF);
if ((closest > startF && closest-startF <= beforeStart) || (closest < startF && startF-closest <= afterStart)) { if ((closest > startF && closest-startF <= beforeStart) || (closest < startF && startF-closest <= afterStart)) {
cur->Start = c->videoController->TimeAtFrame(closest,agi::vfr::START); cur->Start = c->videoController->TimeAtFrame(closest, agi::vfr::START);
} }
// Get closest for end // Get closest for end
closest = GetClosestKeyFrame(endF)-1; closest = get_closest_kf(kf, endF) - 1;
if ((closest > endF && closest-endF <= beforeEnd) || (closest < endF && endF-closest <= afterEnd)) { if ((closest > endF && closest-endF <= beforeEnd) || (closest < endF && endF-closest <= afterEnd)) {
cur->End = c->videoController->TimeAtFrame(closest,agi::vfr::END); cur->End = c->videoController->TimeAtFrame(closest, agi::vfr::END);
} }
} }
} }
// Update grid
c->ass->Commit(_("timing processor"), AssFile::COMMIT_DIAG_TIME); c->ass->Commit(_("timing processor"), AssFile::COMMIT_DIAG_TIME);
} }

View file

@ -37,92 +37,54 @@
#ifndef AGI_PRE #ifndef AGI_PRE
#include <vector> #include <vector>
#include <wx/button.h>
#include <wx/checkbox.h>
#include <wx/checklst.h>
#include <wx/dialog.h> #include <wx/dialog.h>
#include <wx/sizer.h>
#include <wx/slider.h>
#include <wx/textctrl.h>
#endif #endif
namespace agi { struct Context; } namespace agi { struct Context; }
class AssDialogue; class AssDialogue;
class wxButton;
class wxCheckBox;
class wxCheckListBox;
class wxSlider;
/// DOCME
/// @class DialogTimingProcessor /// @class DialogTimingProcessor
/// @brief DOCME /// @brief Automatic postprocessor for correcting common timing issues
///
/// DOCME
class DialogTimingProcessor : public wxDialog { class DialogTimingProcessor : public wxDialog {
agi::Context *c; agi::Context *c; ///< Project context
/// DOCME int leadIn; ///< Lead-in to add in milliseconds
wxStaticBoxSizer *KeyframesSizer; int leadOut; ///< Lead-out to add in milliseconds
int beforeStart; ///< Maximum time in milliseconds to move start time of line backwards to land on a keyframe
int afterStart; ///< Maximum time in milliseconds to move start time of line forwards to land on a keyframe
int beforeEnd; ///< Maximum time in milliseconds to move end time of line backwards to land on a keyframe
int afterEnd; ///< Maximum time in milliseconds to move end time of line forwards to land on a keyframe
int adjDistance; ///< Maximum time in milliseconds to snap adjacent lines to each other
/// DOCME wxCheckBox *onlySelection; ///< Only process selected lines of the selected styles
wxCheckBox *onlySelection; wxCheckBox *hasLeadIn; ///< Enable adding lead-in
wxCheckBox *hasLeadOut; ///< Enable adding lead-out
wxCheckBox *keysEnable; ///< Enable snapping to keyframes
wxCheckBox *adjsEnable; ///< Enable snapping adjacent lines to each other
wxSlider *adjacentBias; ///< Bias between shifting start and end times when snapping adjacent lines
wxCheckListBox *StyleList; ///< List of styles to process
wxButton *ApplyButton; ///< Button to apply the processing
/// DOCME
wxTextCtrl *leadIn;
/// DOCME
wxTextCtrl *leadOut;
/// DOCME
wxCheckBox *hasLeadIn;
/// DOCME
wxCheckBox *hasLeadOut;
/// DOCME
wxCheckBox *keysEnable;
/// DOCME
wxTextCtrl *keysStartBefore;
/// DOCME
wxTextCtrl *keysStartAfter;
/// DOCME
wxTextCtrl *keysEndBefore;
/// DOCME
wxTextCtrl *keysEndAfter;
/// DOCME
wxCheckBox *adjsEnable;
/// DOCME
wxTextCtrl *adjacentThres;
/// DOCME
wxSlider *adjacentBias;
/// DOCME
wxCheckListBox *StyleList;
/// DOCME
wxButton *ApplyButton;
/// DOCME
std::vector<int> KeyFrames;
void OnCheckBox(wxCommandEvent &event);
void OnSelectAll(wxCommandEvent &event);
void OnSelectNone(wxCommandEvent &event);
void OnApply(wxCommandEvent &event); void OnApply(wxCommandEvent &event);
void UpdateControls(); /// Check or uncheck all styles
void Process(); void CheckAll(bool check);
int GetClosestKeyFrame(int frame);
/// DOCME /// Enable and disable text boxes based on which checkboxes are checked
std::vector<AssDialogue*> Sorted; void UpdateControls();
void SortDialogues();
/// Process the file
void Process();
/// Get a list of dialogue lines in the file sorted by start time
std::vector<AssDialogue*> SortDialogues();
public: public:
/// Constructor
/// @param c Project context
DialogTimingProcessor(agi::Context *c); DialogTimingProcessor(agi::Context *c);
DECLARE_EVENT_TABLE()
}; };

View file

@ -65,6 +65,13 @@ NumValidator::NumValidator(int val, bool issigned)
{ {
} }
NumValidator::NumValidator(int64_t val, bool issigned)
: iValue((int)val)
, isFloat(false)
, isSigned(issigned)
{
}
NumValidator::NumValidator(double val, bool issigned) NumValidator::NumValidator(double val, bool issigned)
: fValue(val) : fValue(val)
, isFloat(true) , isFloat(true)

View file

@ -82,6 +82,11 @@ public:
/// @param issigned Allow negative numbers? /// @param issigned Allow negative numbers?
explicit NumValidator(int val, bool issigned=false); explicit NumValidator(int val, bool issigned=false);
/// Constructor
/// @param val Initial value to set the associated control to
/// @param issigned Allow negative numbers?
explicit NumValidator(int64_t val, bool issigned=false);
/// Constructor /// Constructor
/// @param val Initial value to set the associated control to /// @param val Initial value to set the associated control to
/// @param issigned Allow negative numbers? /// @param issigned Allow negative numbers?