~ubuntu-branches/ubuntu/wily/aegisub/wily-proposed

« back to all changes in this revision

Viewing changes to src/dialog_translation.cpp

  • Committer: Package Import Robot
  • Author(s): Sebastian Reichel, Pascal De Vuyst, Juan Picca, Sebastian Reichel
  • Date: 2015-08-04 21:40:50 UTC
  • mfrom: (5.1.1 sid)
  • Revision ID: package-import@ubuntu.com-20150804214050-y2aghm9vdksoc8t7
Tags: 3.2.2+dfsg-1
[ Pascal De Vuyst ]
* Fix Typo in package description (Closes: #739219)

[ Juan Picca ]
* Add patch to fix reproducible build (Closes: #789728)

[ Sebastian Reichel ]
* New upstream release
 - remove vendor directory from orig tarball
* Update Debian Standards Version to 3.9.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
/// @ingroup tools_ui
20
20
///
21
21
 
22
 
#include "config.h"
23
 
 
24
22
#include "dialog_translation.h"
25
23
 
26
24
#include "include/aegisub/context.h"
28
26
 
29
27
#include "ass_dialogue.h"
30
28
#include "ass_file.h"
31
 
#include "audio_controller.h"
32
29
#include "command/command.h"
33
30
#include "compat.h"
 
31
#include "format.h"
34
32
#include "help_button.h"
35
33
#include "libresrc/libresrc.h"
36
34
#include "persist_location.h"
 
35
#include "project.h"
37
36
#include "subs_edit_ctrl.h"
38
37
#include "selection_controller.h"
39
 
#include "utils.h"
40
 
#include "video_context.h"
 
38
#include "video_controller.h"
41
39
 
42
 
#include <libaegisub/util.h>
 
40
#include <libaegisub/make_unique.h>
43
41
 
44
42
#include <boost/algorithm/string/classification.hpp>
45
43
#include <boost/algorithm/string/predicate.hpp>
 
44
#include <boost/algorithm/string/replace.hpp>
46
45
 
47
46
#include <wx/checkbox.h>
48
47
#include <wx/msgdlg.h>
49
48
#include <wx/sizer.h>
50
49
#include <wx/settings.h>
51
50
#include <wx/stattext.h>
 
51
#include <wx/stc/stc.h>
52
52
 
53
53
static void add_hotkey(wxSizer *sizer, wxWindow *parent, const char *command, wxString const& text) {
54
54
        sizer->Add(new wxStaticText(parent, -1, text));
56
56
}
57
57
 
58
58
// Skip over override blocks, comments, and whitespace between blocks
59
 
static bool bad_block(AssDialogueBlock &block) {
60
 
        return block.GetType() != AssBlockType::PLAIN || boost::all(block.GetText(), boost::is_space());
 
59
static bool bad_block(std::unique_ptr<AssDialogueBlock> &block) {
 
60
        return block->GetType() != AssBlockType::PLAIN || boost::all(block->GetText(), boost::is_space());
61
61
}
62
62
 
63
63
DialogTranslation::DialogTranslation(agi::Context *c)
66
66
, file_change_connection(c->ass->AddCommitListener(&DialogTranslation::OnExternalCommit, this))
67
67
, active_line_connection(c->selectionController->AddActiveLineListener(&DialogTranslation::OnActiveLineChanged, this))
68
68
, active_line(c->selectionController->GetActiveLine())
69
 
, line_count(count_if(c->ass->Line.begin(), c->ass->Line.end(), cast<AssDialogue*>()))
70
 
, line_number(count_if(c->ass->Line.begin(), c->ass->Line.iterator_to(*active_line), cast<AssDialogue*>()) + 1)
 
69
, line_count(c->ass->Events.size())
71
70
{
72
71
        SetIcon(GETICON(translation_toolbutton_16));
73
72
 
80
79
                line_number_display = new wxStaticText(this, -1, "");
81
80
                original_box->Add(line_number_display, 0, wxBOTTOM, 5);
82
81
 
83
 
                original_text = new ScintillaTextCtrl(this, -1, "", wxDefaultPosition, wxSize(320, 80));
 
82
                original_text = new wxStyledTextCtrl(this, -1, wxDefaultPosition, wxSize(320, 80));
84
83
                original_text->SetWrapMode(wxSTC_WRAP_WORD);
85
84
                original_text->SetMarginWidth(1, 0);
86
85
                original_text->StyleSetForeground(1, wxColour(10, 60, 200));
130
129
                wxStaticBoxSizer *actions_box = new wxStaticBoxSizer(wxVERTICAL, this, _("Actions"));
131
130
 
132
131
                wxButton *play_audio = new wxButton(this, -1, _("Play &Audio"));
133
 
                play_audio->Enable(c->audioController->IsAudioOpen());
 
132
                play_audio->Enable(!!c->project->AudioProvider());
134
133
                play_audio->Bind(wxEVT_BUTTON, &DialogTranslation::OnPlayAudioButton, this);
135
134
                actions_box->Add(play_audio, 0, wxALL, 5);
136
135
 
137
136
                wxButton *play_video = new wxButton(this, -1, _("Play &Video"));
138
 
                play_video->Enable(c->videoController->IsLoaded());
 
137
                play_video->Enable(!!c->project->VideoProvider());
139
138
                play_video->Bind(wxEVT_BUTTON, &DialogTranslation::OnPlayVideoButton, this);
140
139
                actions_box->Add(play_video, 0, wxLEFT | wxRIGHT | wxBOTTOM, 5);
141
140
 
153
152
 
154
153
        SetSizerAndFit(main_sizer);
155
154
 
156
 
        persist = agi::util::make_unique<PersistLocation>(this, "Tool/Translation Assistant");
 
155
        persist = agi::make_unique<PersistLocation>(this, "Tool/Translation Assistant");
157
156
 
158
157
        Bind(wxEVT_KEY_DOWN, &DialogTranslation::OnKeyDown, this);
159
158
 
160
159
        blocks = active_line->ParseTags();
161
160
        if (bad_block(blocks[0])) {
162
161
                if (!NextBlock())
163
 
                        throw NothingToTranslate(from_wx(_("There is nothing to translate in the file.")));
 
162
                        throw NothingToTranslate();
164
163
        }
165
164
        else
166
165
                UpdateDisplay();
167
166
}
168
167
 
169
 
DialogTranslation::~DialogTranslation() {
170
 
}
 
168
DialogTranslation::~DialogTranslation() { }
171
169
 
172
170
void DialogTranslation::OnActiveLineChanged(AssDialogue *new_line) {
173
171
        if (switching_lines) return;
175
173
        active_line = new_line;
176
174
        blocks = active_line->ParseTags();
177
175
        cur_block = 0;
178
 
        line_number = count_if(c->ass->Line.begin(), c->ass->Line.iterator_to(*new_line), cast<AssDialogue*>()) + 1;
179
176
 
180
177
        if (bad_block(blocks[cur_block]) && !NextBlock()) {
181
178
                wxMessageBox(_("No more lines to translate."));
185
182
 
186
183
void DialogTranslation::OnExternalCommit(int commit_type) {
187
184
        if (commit_type == AssFile::COMMIT_NEW || commit_type & AssFile::COMMIT_DIAG_ADDREM) {
188
 
                line_count = count_if(c->ass->Line.begin(), c->ass->Line.end(), cast<AssDialogue*>());
189
 
                line_number_display->SetLabel(wxString::Format(_("Current line: %d/%d"), (int)line_number, (int)line_count));
 
185
                line_count = c->ass->Events.size();
 
186
                line_number_display->SetLabel(fmt_tl("Current line: %d/%d", active_line->Row + 1, line_count));
190
187
        }
191
188
 
192
189
        if (commit_type & AssFile::COMMIT_DIAG_TEXT)
204
201
                        active_line = new_line;
205
202
                        blocks = active_line->ParseTags();
206
203
                        cur_block = 0;
207
 
                        ++line_number;
208
204
                }
209
205
                else
210
206
                        ++cur_block;
226
222
                        active_line = new_line;
227
223
                        blocks = active_line->ParseTags();
228
224
                        cur_block = blocks.size() - 1;
229
 
                        --line_number;
230
225
                }
231
226
                else
232
227
                        --cur_block;
238
233
}
239
234
 
240
235
void DialogTranslation::UpdateDisplay() {
241
 
        line_number_display->SetLabel(wxString::Format(_("Current line: %d/%d"), (int)line_number, (int)line_count));
 
236
        line_number_display->SetLabel(fmt_tl("Current line: %d/%d", active_line->Row, line_count));
242
237
 
243
238
        original_text->SetReadOnly(false);
244
239
        original_text->ClearAll();
245
240
 
246
241
        size_t i = 0;
247
242
        for (auto& block : blocks) {
248
 
                if (block.GetType() == AssBlockType::PLAIN) {
 
243
                if (block->GetType() == AssBlockType::PLAIN) {
249
244
                        int initial_pos = original_text->GetLength();
250
 
                        original_text->AppendTextRaw(block.GetText().c_str());
 
245
                        original_text->AppendTextRaw(block->GetText().c_str());
251
246
                        if (i == cur_block) {
252
 
                                int cur_size = original_text->GetReverseUnicodePosition(initial_pos);
253
 
                                original_text->StartUnicodeStyling(cur_size);
254
 
                                original_text->SetUnicodeStyling(cur_size, block.GetText().size(), 1);
 
247
                                original_text->StartStyling(initial_pos, 31);
 
248
                                original_text->SetStyling(block->GetText().size(), 1);
255
249
                        }
256
250
                }
257
251
                else
258
 
                        original_text->AppendTextRaw(block.GetText().c_str());
 
252
                        original_text->AppendTextRaw(block->GetText().c_str());
259
253
                ++i;
260
254
        }
261
255
 
268
262
}
269
263
 
270
264
void DialogTranslation::Commit(bool next) {
271
 
        wxString new_value = translated_text->GetValue();
272
 
        new_value.Replace("\r\n", "\\N");
273
 
        new_value.Replace("\r", "\\N");
274
 
        new_value.Replace("\n", "\\N");
275
 
        blocks[cur_block] = AssDialogueBlockPlain(from_wx(new_value));
 
265
        std::string new_value = translated_text->GetTextRaw().data();
 
266
        boost::replace_all(new_value, "\r\n", "\\N");
 
267
        boost::replace_all(new_value, "\r", "\\N");
 
268
        boost::replace_all(new_value, "\n", "\\N");
 
269
        *blocks[cur_block] = AssDialogueBlockPlain(new_value);
276
270
        active_line->UpdateText(blocks);
277
271
 
278
272
        file_change_connection.Block();
291
285
}
292
286
 
293
287
void DialogTranslation::InsertOriginal() {
294
 
        translated_text->AddText(to_wx(blocks[cur_block].GetText()));
 
288
        auto const& text = blocks[cur_block]->GetText();
 
289
        translated_text->AddTextRaw(text.data(), text.size());
295
290
}
296
291
 
297
 
 
298
292
void DialogTranslation::OnKeyDown(wxKeyEvent &evt) {
299
293
        hotkey::check("Translation Assistant", c, evt);
300
294
}