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

« back to all changes in this revision

Viewing changes to src/dialog_selection.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:
14
14
//
15
15
// Aegisub Project http://www.aegisub.org/
16
16
 
17
 
/// @file dialog_selection.cpp
18
 
/// @brief Select Lines dialogue box and logic
19
 
/// @ingroup secondary_ui
20
 
///
21
 
 
22
 
#include "config.h"
23
 
 
24
 
#include "dialog_selection.h"
25
 
 
26
17
#include "ass_dialogue.h"
27
18
#include "ass_file.h"
28
19
#include "compat.h"
 
20
#include "dialog_manager.h"
 
21
#include "format.h"
 
22
#include "frame_main.h"
29
23
#include "help_button.h"
30
24
#include "include/aegisub/context.h"
31
25
#include "libresrc/libresrc.h"
32
26
#include "options.h"
33
27
#include "search_replace_engine.h"
34
28
#include "selection_controller.h"
35
 
#include "utils.h"
36
 
 
37
 
#include <libaegisub/of_type_adaptor.h>
38
 
 
39
 
#include <algorithm>
40
 
#include <boost/algorithm/string/find.hpp>
41
 
#include <boost/algorithm/string/predicate.hpp>
 
29
 
 
30
#include <boost/range/algorithm/set_algorithm.hpp>
42
31
 
43
32
#include <wx/checkbox.h>
44
33
#include <wx/combobox.h>
 
34
#include <wx/dialog.h>
45
35
#include <wx/msgdlg.h>
46
36
#include <wx/radiobox.h>
47
37
#include <wx/radiobut.h>
48
38
#include <wx/sizer.h>
49
39
#include <wx/textctrl.h>
50
40
 
51
 
enum {
52
 
        ACTION_SET = 0,
53
 
        ACTION_ADD,
54
 
        ACTION_SUB,
55
 
        ACTION_INTERSECT
56
 
};
57
 
 
58
 
enum {
59
 
        MODE_EXACT = 0,
60
 
        MODE_CONTAINS,
61
 
        MODE_REGEXP
62
 
};
63
 
 
64
 
static std::set<AssDialogue*> process(std::string const& match_text, bool match_case, int mode, bool invert, bool comments, bool dialogue, int field_n, AssFile *ass) {
 
41
namespace {
 
42
class DialogSelection final : public wxDialog {
 
43
        agi::Context *con; ///< Project context
 
44
 
 
45
        wxTextCtrl *match_text; ///< Text to search for
 
46
        wxCheckBox *case_sensitive; ///< Should the search be case-sensitive
 
47
        wxCheckBox *apply_to_dialogue; ///< Select/deselect uncommented lines
 
48
        wxCheckBox *apply_to_comments; ///< Select/deselect commented lines
 
49
        wxRadioButton *select_unmatching_lines; ///< Select lines which don't match instead
 
50
        wxRadioBox *selection_change_type; ///< What sort of action to take on the selection
 
51
        wxRadioBox *dialogue_field; ///< Which dialogue field to look at
 
52
        wxRadioBox *match_mode;
 
53
 
 
54
        void Process(wxCommandEvent&);
 
55
 
 
56
        /// Dialogue/Comment check handler to ensure at least one is always checked
 
57
        /// @param chk The checkbox to check if both are clear
 
58
        void OnDialogueCheckbox(wxCheckBox *chk);
 
59
 
 
60
public:
 
61
        DialogSelection(agi::Context *c);
 
62
        ~DialogSelection();
 
63
};
 
64
 
 
65
enum class Action {
 
66
        SET = 0,
 
67
        ADD,
 
68
        SUB,
 
69
        INTERSECT
 
70
};
 
71
 
 
72
enum Mode {
 
73
        EXACT = 0,
 
74
        CONTAINS,
 
75
        REGEXP
 
76
};
 
77
 
 
78
std::set<AssDialogue*> process(std::string const& match_text, bool match_case, Mode mode, bool invert, bool comments, bool dialogue, int field_n, AssFile *ass) {
65
79
        SearchReplaceSettings settings = {
66
80
                match_text,
67
81
                std::string(),
68
82
                static_cast<SearchReplaceSettings::Field>(field_n),
69
83
                SearchReplaceSettings::Limit::ALL,
70
84
                match_case,
71
 
                mode == MODE_REGEXP,
72
 
                false,
73
 
                false,
74
 
                mode == MODE_EXACT
 
85
                mode == Mode::REGEXP,
 
86
                false,
 
87
                false,
 
88
                mode == Mode::EXACT
75
89
        };
76
90
 
77
91
        auto predicate = SearchReplaceEngine::GetMatcher(settings);
78
92
 
79
93
        std::set<AssDialogue*> matches;
80
 
        for (auto diag : ass->Line | agi::of_type<AssDialogue>()) {
81
 
                if (diag->Comment && !comments) continue;
82
 
                if (!diag->Comment && !dialogue) continue;
 
94
        for (auto& diag : ass->Events) {
 
95
                if (diag.Comment && !comments) continue;
 
96
                if (!diag.Comment && !dialogue) continue;
83
97
 
84
 
                if (invert != predicate(diag, 0))
85
 
                        matches.insert(diag);
 
98
                if (invert != predicate(&diag, 0))
 
99
                        matches.insert(&diag);
86
100
        }
87
101
 
88
102
        return matches;
173
187
        try {
174
188
                matches = process(
175
189
                        from_wx(match_text->GetValue()), case_sensitive->IsChecked(),
176
 
                        match_mode->GetSelection(), select_unmatching_lines->GetValue(),
 
190
                        static_cast<Mode>(match_mode->GetSelection()), select_unmatching_lines->GetValue(),
177
191
                        apply_to_comments->IsChecked(), apply_to_dialogue->IsChecked(),
178
 
                        dialogue_field->GetSelection(), con->ass);
 
192
                        dialogue_field->GetSelection(), con->ass.get());
179
193
        }
180
194
        catch (agi::Exception const&) {
181
195
                Close();
182
196
                return;
183
197
        }
184
198
 
185
 
        int action = selection_change_type->GetSelection();
 
199
        auto action = static_cast<Action>(selection_change_type->GetSelection());
186
200
 
187
 
        SubtitleSelection old_sel, new_sel;
188
 
        if (action != ACTION_SET)
189
 
                con->selectionController->GetSelectedSet(old_sel);
 
201
        Selection old_sel, new_sel;
 
202
        if (action != Action::SET)
 
203
                old_sel = con->selectionController->GetSelectedSet();
190
204
 
191
205
        wxString message;
192
206
        size_t count = 0;
193
207
        switch (action) {
194
 
                case ACTION_SET:
195
 
                        new_sel = matches;
196
 
                        switch (count = new_sel.size()) {
197
 
                                case 0:  message = _("Selection was set to no lines"); break;
198
 
                                case 1:  message = _("Selection was set to one line"); break;
199
 
                                default: message = wxString::Format(_("Selection was set to %u lines"), (unsigned)count);
200
 
                        }
201
 
                        break;
202
 
 
203
 
                case ACTION_ADD:
204
 
                        set_union(old_sel.begin(), old_sel.end(), matches.begin(), matches.end(), inserter(new_sel, new_sel.begin()));
205
 
                        switch (count = new_sel.size() - old_sel.size()) {
206
 
                                case 0:  message = _("No lines were added to selection"); break;
207
 
                                case 1:  message = _("One line was added to selection"); break;
208
 
                                default: message = wxString::Format(_("%u lines were added to selection"), (unsigned)count);
209
 
                        }
210
 
                        break;
211
 
 
212
 
                case ACTION_SUB:
213
 
                        set_difference(old_sel.begin(), old_sel.end(), matches.begin(), matches.end(), inserter(new_sel, new_sel.begin()));
214
 
                        switch (count = old_sel.size() - new_sel.size()) {
215
 
                                case 0:  message = _("No lines were removed from selection"); break;
216
 
                                case 1:  message = _("One line was removed from selection"); break;
217
 
                                default: message = wxString::Format(_("%u lines were removed from selection"), (unsigned)count);
218
 
                        }
219
 
                        break;
220
 
 
221
 
                case ACTION_INTERSECT:
222
 
                        set_intersection(old_sel.begin(), old_sel.end(), matches.begin(), matches.end(), inserter(new_sel, new_sel.begin()));
223
 
                        switch (count = old_sel.size() - new_sel.size()) {
224
 
                                case 0:  message = _("No lines were removed from selection"); break;
225
 
                                case 1:  message = _("One line was removed from selection"); break;
226
 
                                default: message = wxString::Format(_("%u lines were removed from selection"), (unsigned)count);
227
 
                        }
 
208
                case Action::SET:
 
209
                        new_sel = std::move(matches);
 
210
                        message = (count = new_sel.size())
 
211
                                ? fmt_plural(count, "Selection was set to one line", "Selection was set to %u lines", count)
 
212
                                : _("Selection was set to no lines");
 
213
                        break;
 
214
 
 
215
                case Action::ADD:
 
216
                        boost::set_union(old_sel, matches, inserter(new_sel, new_sel.begin()));
 
217
                        message = (count = new_sel.size() - old_sel.size())
 
218
                                ? fmt_plural(count, "One line was added to selection", "%u lines were added to selection", count)
 
219
                                : _("No lines were added to selection");
 
220
                        break;
 
221
 
 
222
                case Action::SUB:
 
223
                        boost::set_difference(old_sel, matches, inserter(new_sel, new_sel.begin()));
 
224
                        goto sub_message;
 
225
 
 
226
                case Action::INTERSECT:
 
227
                        boost::set_intersection(old_sel, matches, inserter(new_sel, new_sel.begin()));
 
228
                        sub_message:
 
229
                        message = (count = old_sel.size() - new_sel.size())
 
230
                                ? fmt_plural(count, "One line was removed from selection", "%u lines were removed from selection", count)
 
231
                                : _("No lines were removed from selection");
228
232
                        break;
229
233
        }
230
234
 
231
235
        if (count == 0)
232
236
                wxMessageBox(message, _("Selection"), wxOK | wxCENTER, this);
233
237
        else
234
 
                StatusTimeout(message);
235
 
 
236
 
        if (new_sel.size() && !new_sel.count(con->selectionController->GetActiveLine()))
237
 
                con->selectionController->SetActiveLine(*new_sel.begin());
238
 
        con->selectionController->SetSelectedSet(new_sel);
 
238
                con->frame->StatusTimeout(message);
239
239
 
240
240
        AssDialogue *new_active = con->selectionController->GetActiveLine();
241
241
        if (new_sel.size() && !new_sel.count(new_active))
242
242
                new_active = *new_sel.begin();
243
 
        con->selectionController->SetSelectionAndActive(new_sel, new_active);
 
243
        con->selectionController->SetSelectionAndActive(std::move(new_sel), new_active);
244
244
 
245
245
        Close();
246
246
}
249
249
        if(!apply_to_dialogue->IsChecked() && !apply_to_comments->GetValue())
250
250
                chk->SetValue(true);
251
251
}
 
252
}
 
253
 
 
254
void ShowSelectLinesDialog(agi::Context *c) {
 
255
        c->dialog->Show<DialogSelection>(c);
 
256
}