File: textcorrection.cc

package info (click to toggle)
subtitleeditor 0.56.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,880 kB
  • sloc: cpp: 26,446; makefile: 1,713; perl: 434; sh: 259; xml: 149
file content (217 lines) | stat: -rw-r--r-- 6,266 bytes parent folder | download
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
// subtitleeditor -- a tool to create or edit subtitle
//
// https://subtitleeditor.github.io/subtitleeditor/
// https://github.com/subtitleeditor/subtitleeditor/
//
// Copyright @ 2005-2018, kitone
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

#include <extension/action.h>
#include <filereader.h>
#include <gtkmm.h>
#include <gtkmm_utility.h>
#include <utility.h>

#include "capitalizationpage.h"
#include "commonerrorpage.h"
#include "confirmationpage.h"
#include "hearingimpairedpage.h"
#include "patternmanager.h"
#include "taskspage.h"

class AssistantTextCorrection : public Gtk::Assistant {
  public:
   AssistantTextCorrection(BaseObjectType* cobject, const Glib::RefPtr<Gtk::Builder>& builder) : Gtk::Assistant(cobject) {
      se_dbg(SE_DBG_PLUGINS);

      doc = SubtitleEditorWindow::get_instance()->get_current_document();

      builder->get_widget_derived("vbox-tasks", m_tasksPage);
      builder->get_widget_derived("vbox-comfirmation", m_comfirmationPage);

      add_tasks();

      se_dbg_msg(SE_DBG_PLUGINS, "Init tasks pages");
      // Init tasks pages
      for (int i = 0; i < get_n_pages(); ++i) {
         PatternsPage* page = dynamic_cast<PatternsPage*>(get_nth_page(i));
         if (page)
            m_tasksPage->add_task(page);
      }

      set_page_type(*get_nth_page(0), Gtk::ASSISTANT_PAGE_INTRO);
      set_page_type(*get_nth_page(get_n_pages() - 1), Gtk::ASSISTANT_PAGE_CONFIRM);
   }

   ~AssistantTextCorrection() {
      se_dbg(SE_DBG_PLUGINS);
   }

   void add_tasks() {
      se_dbg(SE_DBG_PLUGINS);

      add_page(manage(new HearingImpairedPage), 1);
      add_page(manage(new CommonErrorPage), 2);
      add_page(manage(new CapitalizationPage), 3);
   }

   void add_page(PatternsPage* page, unsigned int pos) {
      se_dbg_msg(SE_DBG_PLUGINS, "new task page '%s' to the position '%d'", page->get_page_title().c_str(), pos);

      insert_page(*page, pos);
      set_page_title(*page, page->get_page_title());
      set_page_type(*page, Gtk::ASSISTANT_PAGE_CONTENT);
   }

   // Catch the comfirmation page and initialize with the current document
   // and patterns available.
   void on_prepare(Gtk::Widget* page) {
      se_dbg(SE_DBG_PLUGINS);

      AssistantPage* ap = dynamic_cast<AssistantPage*>(page);
      if (ap && ap == m_comfirmationPage) {
         bool res = m_comfirmationPage->comfirme(doc, get_patterns());
         set_page_complete(*page, true);
         set_page_title(*page, m_comfirmationPage->get_page_title());
         // No change, only display the close button
         if (!res)
            set_page_type(*m_comfirmationPage, Gtk::ASSISTANT_PAGE_SUMMARY);
      } else {
         set_page_complete(*page, true);
      }
   }

   // Return all patterns activated.
   std::list<Pattern*> get_patterns() {
      se_dbg(SE_DBG_PLUGINS);

      std::list<Pattern*> patterns;

      for (int i = 0; i < get_n_pages(); ++i) {
         PatternsPage* page = dynamic_cast<PatternsPage*>(get_nth_page(i));
         if (page == NULL)
            continue;
         if (page->is_enable() == false)
            continue;

         std::list<Pattern*> p = page->get_patterns();
         patterns.merge(p);
      }
      return patterns;
   }

   // Apply the change and destroy the window.
   void on_apply() {
      se_dbg(SE_DBG_PLUGINS);

      m_comfirmationPage->apply(doc);

      save_cfg();
   }

   // Destroy the window.
   void on_cancel() {
      se_dbg(SE_DBG_PLUGINS);

      save_cfg();

      // destroy_();
      delete this;
   }

   // Close signal, destroy the window.
   void on_close() {
      se_dbg(SE_DBG_PLUGINS);

      save_cfg();

      // destroy_();
      delete this;
   }

   // Save the configuration for each pages.
   void save_cfg() {
      se_dbg(SE_DBG_PLUGINS);

      for (int i = 0; i < get_n_pages(); ++i) {
         PatternsPage* page = dynamic_cast<PatternsPage*>(get_nth_page(i));
         if (page != NULL)
            page->save_cfg();
      }
   }

  protected:
   TasksPage* m_tasksPage;
   ComfirmationPage* m_comfirmationPage;
   Document* doc;
};

class TextCorrectionPlugin : public Action {
  public:
   TextCorrectionPlugin() {
      activate();
      update_ui();
   }

   ~TextCorrectionPlugin() {
      deactivate();
   }

   void activate() {
      se_dbg(SE_DBG_PLUGINS);

      // actions
      action_group = Gtk::ActionGroup::create("TextCorrectionPlugin");
      action_group->add(Gtk::Action::create("text-correction", _("Text _Correction"), _("Launch the text correction dialogue")),
                        sigc::mem_fun(*this, &TextCorrectionPlugin::on_execute));

      // ui
      Glib::RefPtr<Gtk::UIManager> ui = get_ui_manager();

      ui_id = ui->new_merge_id();
      ui->insert_action_group(action_group);
      ui->add_ui(ui_id, "/menubar/menu-tools/checking", "text-correction", "text-correction");
   }

   void deactivate() {
      se_dbg(SE_DBG_PLUGINS);

      Glib::RefPtr<Gtk::UIManager> ui = get_ui_manager();

      ui->remove_ui(ui_id);
      ui->remove_action_group(action_group);
   }

   void update_ui() {
      se_dbg(SE_DBG_PLUGINS);

      bool visible = (get_current_document() != NULL);

      action_group->get_action("text-correction")->set_sensitive(visible);
   }

   void on_execute() {
      // create dialog
      AssistantTextCorrection* assistant = gtkmm_utility::get_widget_derived<AssistantTextCorrection>(
         SE_DEV_VALUE(SE_PLUGIN_PATH_UI, SE_PLUGIN_PATH_DEV), "assistant-text-correction.ui", "assistant");
      assistant->show();
   }

  protected:
   Gtk::UIManager::ui_merge_id ui_id;
   Glib::RefPtr<Gtk::ActionGroup> action_group;
};

REGISTER_EXTENSION(TextCorrectionPlugin)