Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A few miscellaneous features #187

Merged
merged 5 commits into from
Dec 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/auto4_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ namespace Automation4 {
wxWindow *ww = config_dialog->CreateWindow(&w); // generate actual dialog contents
s->Add(ww, 0, wxALL, 5); // add contents to dialog
w.SetSizerAndFit(s);
w.SetLayoutAdaptationMode(wxDIALOG_ADAPTATION_MODE_ENABLED);
w.CenterOnParent();
w.ShowModal();
});
Expand Down
4 changes: 3 additions & 1 deletion src/base_grid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,9 @@ void BaseGrid::OnMouseEvent(wxMouseEvent &event) {
if (event.GetWheelRotation() != 0) {
if (ForwardMouseWheelEvent(this, event)) {
int step = shift ? h / lineHeight - 2 : 3;
ScrollTo(yPos - step * event.GetWheelRotation() / event.GetWheelDelta());
scrollWheelProgress += event.GetWheelRotation();
ScrollTo(yPos - step * (scrollWheelProgress / event.GetWheelDelta()));
scrollWheelProgress %= event.GetWheelDelta();
}
return;
}
Expand Down
1 change: 1 addition & 0 deletions src/base_grid.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class BaseGrid final : public wxWindow {
std::vector<agi::signal::Connection> connections;
int lineHeight = 1; ///< Height of a line in pixels in the current font
bool holding = false; ///< Is a drag selection in process?
int scrollWheelProgress = 0; ///< How close we are to reaching a full mouse wheel step
wxFont font; ///< Current grid font
wxScrollBar *scrollBar; ///< The grid's scrollbar
bool byFrame = false; ///< Should times be displayed as frame numbers
Expand Down
11 changes: 6 additions & 5 deletions src/dialog_selection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class DialogSelection final : public wxDialog {
wxRadioBox *dialogue_field; ///< Which dialogue field to look at
wxRadioBox *match_mode;

void Process(wxCommandEvent&);
void Process(wxCommandEvent& event);

/// Dialogue/Comment check handler to ensure at least one is always checked
/// @param chk The checkbox to check if both are clear
Expand Down Expand Up @@ -150,7 +150,7 @@ wxDialog (c->parent, -1, _("Select"), wxDefaultPosition, wxDefaultSize, wxCAPTIO
main_sizer->Add(selection_change_type = new wxRadioBox(this, -1, _("Action"), wxDefaultPosition, wxDefaultSize, 4, actions, 1), main_flags);
}

main_sizer->Add(CreateButtonSizer(wxOK | wxCANCEL | wxHELP), main_flags);
main_sizer->Add(CreateButtonSizer(wxOK | wxCANCEL | wxAPPLY | wxHELP), main_flags);

SetSizerAndFit(main_sizer);
CenterOnParent();
Expand All @@ -165,6 +165,7 @@ wxDialog (c->parent, -1, _("Select"), wxDefaultPosition, wxDefaultSize, wxCAPTIO
match_mode->SetSelection(OPT_GET("Tool/Select Lines/Mode")->GetInt());

Bind(wxEVT_BUTTON, &DialogSelection::Process, this, wxID_OK);
Bind(wxEVT_BUTTON, &DialogSelection::Process, this, wxID_APPLY);
Bind(wxEVT_BUTTON, std::bind(&HelpButton::OpenPage, "Select Lines"), wxID_HELP);
apply_to_comments->Bind(wxEVT_CHECKBOX, std::bind(&DialogSelection::OnDialogueCheckbox, this, apply_to_dialogue));
apply_to_dialogue->Bind(wxEVT_CHECKBOX, std::bind(&DialogSelection::OnDialogueCheckbox, this, apply_to_comments));
Expand All @@ -181,7 +182,7 @@ DialogSelection::~DialogSelection() {
OPT_SET("Tool/Select Lines/Match/Comment")->SetBool(apply_to_comments->IsChecked());
}

void DialogSelection::Process(wxCommandEvent&) {
void DialogSelection::Process(wxCommandEvent& event) {
std::set<AssDialogue*> matches;

try {
Expand All @@ -192,7 +193,7 @@ void DialogSelection::Process(wxCommandEvent&) {
dialogue_field->GetSelection(), con->ass.get());
}
catch (agi::Exception const&) {
Close();
if (event.GetId() == wxID_OK) Close();
return;
}

Expand Down Expand Up @@ -242,7 +243,7 @@ void DialogSelection::Process(wxCommandEvent&) {
new_active = *new_sel.begin();
con->selectionController->SetSelectionAndActive(std::move(new_sel), new_active);

Close();
if (event.GetId() == wxID_OK) Close();
}

void DialogSelection::OnDialogueCheckbox(wxCheckBox *chk) {
Expand Down
1 change: 1 addition & 0 deletions src/libresrc/default_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,7 @@
"Syntax" : true
},
"Provider" : "libass",
"Show Original": false,
"Time Edit" : {
"Insert Mode" : true
}
Expand Down
1 change: 1 addition & 0 deletions src/libresrc/osx/default_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,7 @@
"Syntax" : true
},
"Provider" : "libass",
"Show Original": false,
"Time Edit" : {
"Insert Mode" : true
}
Expand Down
51 changes: 45 additions & 6 deletions src/mkv_wrap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,18 +114,45 @@ struct MkvStdIO final : InputStream {
}
};

static void read_subtitles(agi::ProgressSink *ps, MatroskaFile *file, MkvStdIO *input, bool srt, double totalTime, AssParser *parser) {
static bool read_subtitles(agi::ProgressSink *ps, MatroskaFile *file, MkvStdIO *input, bool srt, double totalTime, AssParser *parser, CompressedStream *cs) {
std::vector<std::pair<int, std::string>> subList;

// Load blocks
uint64_t startTime, endTime, filePos;
unsigned int rt, frameSize, frameFlags;

std::vector<char> uncompBuf(cs ? 256 : 0);

while (mkv_ReadFrame(file, 0, &rt, &startTime, &endTime, &filePos, &frameSize, &frameFlags) == 0) {
if (ps->IsCancelled()) return;
if (ps->IsCancelled()) return true;
if (frameSize == 0) continue;

const std::string_view readBuf(input->file.read(filePos, frameSize), frameSize);
std::string_view readBuf;

if (cs) {
cs_NextFrame(cs, filePos, frameSize);
int bytesRead = 0;

int res;
do {
res = cs_ReadData(cs, &uncompBuf[bytesRead], uncompBuf.size() - bytesRead);
if (res == -1) {
const char *err = cs_GetLastError(cs);
if (!err) err = "Unknown error";
ps->Log("Failed to decompress subtitles: " + std::string(err));
return false;
}

bytesRead += res;

if (bytesRead >= uncompBuf.size())
uncompBuf.resize(2 * uncompBuf.size());
} while (res != 0);

readBuf = std::string_view(&uncompBuf[0], bytesRead);
} else {
readBuf = std::string_view(input->file.read(filePos, frameSize), frameSize);
}

// Get start and end times
int64_t timecodeScaleLow = 1000000;
Expand Down Expand Up @@ -167,6 +194,7 @@ static void read_subtitles(agi::ProgressSink *ps, MatroskaFile *file, MkvStdIO *
sort(begin(subList), end(subList));
for (auto order_value_pair : subList)
parser->AddLine(order_value_pair.second);
return true;
}

void MatroskaWrapper::GetSubtitles(std::filesystem::path const& filename, AssFile *target) {
Expand All @@ -183,7 +211,7 @@ void MatroskaWrapper::GetSubtitles(std::filesystem::path const& filename, AssFil
// Find tracks
for (auto track : boost::irange(0u, tracks)) {
auto trackInfo = mkv_GetTrackInfo(file, track);
if (trackInfo->Type != 0x11 || trackInfo->CompEnabled) continue;
if (trackInfo->Type != 0x11) continue;

// Known subtitle format
std::string CodecID(trackInfo->CodecID);
Expand Down Expand Up @@ -239,14 +267,25 @@ void MatroskaWrapper::GetSubtitles(std::filesystem::path const& filename, AssFil

parser.AddLine("[Events]");

agi::scoped_holder<CompressedStream *, decltype(&cs_Destroy)> cs(nullptr, cs_Destroy);
if (trackInfo->CompEnabled) {
cs = cs_Create(file, trackToRead, err, sizeof(err));
if (!cs)
throw MatroskaException(err);
}

// Read timecode scale
auto segInfo = mkv_GetFileInfo(file);
int64_t timecodeScale = mkv_TruncFloat(trackInfo->TimecodeScale) * segInfo->TimecodeScale;

// Progress bar
auto totalTime = double(segInfo->Duration) / timecodeScale;
DialogProgress progress(nullptr, _("Parsing Matroska"), _("Reading subtitles from Matroska file."));
progress.Run([&](agi::ProgressSink *ps) { read_subtitles(ps, file, &input, srt, totalTime, &parser); });
bool result;
progress.Run([&](agi::ProgressSink *ps) { result = read_subtitles(ps, file, &input, srt, totalTime, &parser, cs); });

if (!result)
throw MatroskaException("Failed to read subtitles");
}

bool MatroskaWrapper::HasSubtitles(std::filesystem::path const& filename) {
Expand All @@ -261,7 +300,7 @@ bool MatroskaWrapper::HasSubtitles(std::filesystem::path const& filename) {
for (auto track : boost::irange(0u, tracks)) {
auto trackInfo = mkv_GetTrackInfo(file, track);

if (trackInfo->Type == 0x11 && !trackInfo->CompEnabled) {
if (trackInfo->Type == 0x11) {
std::string CodecID(trackInfo->CodecID);
if (CodecID == "S_TEXT/SSA" || CodecID == "S_TEXT/ASS" || CodecID == "S_TEXT/UTF8")
return true;
Expand Down
21 changes: 17 additions & 4 deletions src/subs_edit_box.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,12 @@ SubsEditBox::SubsEditBox(wxWindow *parent, agi::Context *context)

context->textSelectionController->SetControl(edit_ctrl);
edit_ctrl->SetFocus();

bool show_original = OPT_GET("Subtitle/Show Original")->GetBool();
if (show_original) {
split_box->SetValue(true);
DoOnSplit(true);
}
}

SubsEditBox::~SubsEditBox() {
Expand Down Expand Up @@ -574,15 +580,22 @@ void SubsEditBox::SetControlsState(bool state) {
}

void SubsEditBox::OnSplit(wxCommandEvent&) {
bool show_original = split_box->IsChecked();
DoOnSplit(show_original);
OPT_SET("Subtitle/Show Original")->SetBool(show_original);
}

void SubsEditBox::DoOnSplit(bool show_original) {
Freeze();
GetSizer()->Show(secondary_editor, split_box->IsChecked());
GetSizer()->Show(bottom_sizer, split_box->IsChecked());
GetSizer()->Show(secondary_editor, show_original);
GetSizer()->Show(bottom_sizer, show_original);
Fit();
SetMinSize(GetSize());
GetParent()->GetSizer()->Layout();
wxSizer* parent_sizer = GetParent()->GetSizer();
if (parent_sizer) parent_sizer->Layout();
Thaw();

if (split_box->IsChecked())
if (show_original)
secondary_editor->SetValue(to_wx(c->initialLineState->GetInitialText()));
}

Expand Down
1 change: 1 addition & 0 deletions src/subs_edit_box.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ class SubsEditBox final : public wxPanel {
void OnEffectChange(wxCommandEvent &);
void OnSize(wxSizeEvent &event);
void OnSplit(wxCommandEvent&);
void DoOnSplit(bool show_original);

void SetPlaceholderCtrl(wxControl *ctrl, wxString const& value);

Expand Down
Loading