File: confirmationpage.h

package info (click to toggle)
subtitleeditor 0.56.1-2
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 8,908 kB
  • sloc: cpp: 26,446; makefile: 1,713; perl: 434; sh: 259; xml: 149
file content (228 lines) | stat: -rw-r--r-- 7,838 bytes parent folder | download | duplicates (3)
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
218
219
220
221
222
223
224
225
226
227
228
#pragma once

// 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 <gui/cellrenderercustom.h>
#include <gui/textviewcell.h>
#include <widget_config_utility.h>

#include "page.h"
#include "patternmanager.h"

class ComfirmationPage : public AssistantPage {
   class Column : public Gtk::TreeModel::ColumnRecord {
     public:
      Column() {
         add(num);
         add(accept);
         add(original);
         add(corrected);
      }
      Gtk::TreeModelColumn<guint> num;
      Gtk::TreeModelColumn<bool> accept;
      Gtk::TreeModelColumn<Glib::ustring> original;
      Gtk::TreeModelColumn<Glib::ustring> corrected;
   };

  public:
   ComfirmationPage(BaseObjectType* cobject, const Glib::RefPtr<Gtk::Builder>& builder) : AssistantPage(cobject, builder) {
      builder->get_widget("treeview-comfirmation", m_treeview);
      builder->get_widget("button-comfirmation-mark-all", m_buttonMarkAll);
      builder->get_widget("button-comfirmation-unmark-all", m_buttonUnmarkAll);
      builder->get_widget("check-comfirmation-remove-blank", m_checkRemoveBlank);

      create_treeview();
      init_signals();

      widget_config::read_config_and_connect(m_checkRemoveBlank, "comfirmation-page", "remove-blank");
   }

   void create_treeview() {
      m_liststore = Gtk::ListStore::create(m_column);
      m_treeview->set_model(m_liststore);

      // column Num
      {
         Gtk::TreeViewColumn* column = manage(new Gtk::TreeViewColumn(_("Num")));
         m_treeview->append_column(*column);

         Gtk::CellRendererText* label = manage(new Gtk::CellRendererText);
         column->pack_start(*label);
         column->add_attribute(label->property_text(), m_column.num);
      }
      // column Accept
      {
         Gtk::TreeViewColumn* column = manage(new Gtk::TreeViewColumn(_("Accept")));
         m_treeview->append_column(*column);

         Gtk::CellRendererToggle* toggle = manage(new Gtk::CellRendererToggle);
         column->pack_start(*toggle);
         column->add_attribute(toggle->property_active(), m_column.accept);
         toggle->signal_toggled().connect(sigc::mem_fun(*this, &ComfirmationPage::on_accept_toggled));
      }
      // column Original
      {
         Gtk::TreeViewColumn* column = manage(new Gtk::TreeViewColumn(_("Original Text")));
         m_treeview->append_column(*column);

         CellRendererCustom<TextViewCell>* label = manage(new CellRendererCustom<TextViewCell>);
         column->pack_start(*label);
         column->add_attribute(label->property_text(), m_column.original);
      }
      // column Corrected
      {
         m_column_corrected_text = manage(new Gtk::TreeViewColumn(_("Corrected Text")));
         m_treeview->append_column(*m_column_corrected_text);

         CellRendererCustom<TextViewCell>* renderer = manage(new CellRendererCustom<TextViewCell>);
         m_column_corrected_text->pack_start(*renderer);
         m_column_corrected_text->add_attribute(renderer->property_text(), m_column.corrected);
         renderer->property_editable() = true;
         renderer->signal_edited().connect(sigc::mem_fun(*this, &ComfirmationPage::on_corrected_edited));
      }
      m_treeview->signal_row_activated().connect(sigc::mem_fun(*this, &ComfirmationPage::on_row_activated));
   }

   void init_signals() {
      m_buttonMarkAll->signal_clicked().connect(sigc::mem_fun(*this, &ComfirmationPage::on_mark_all));
      m_buttonUnmarkAll->signal_clicked().connect(sigc::mem_fun(*this, &ComfirmationPage::on_unmark_all));
   }

   bool comfirme(Document* doc, const std::list<Pattern*>& patterns) {
      m_liststore->clear();

      Subtitles subs = doc->subtitles();

      Glib::ustring text, previous;
      for (Subtitle sub = subs.get_first(); sub; ++sub) {
         text = sub.get_text();

         for (const auto& pattern : patterns) {
            pattern->execute(text, previous);
         }

         if (sub.get_text() != text) {
            Gtk::TreeIter it = m_liststore->append();
            (*it)[m_column.num] = sub.get_num();
            (*it)[m_column.accept] = true;
            (*it)[m_column.original] = sub.get_text();
            (*it)[m_column.corrected] = text;
         }

         previous = text;
      }
      return !m_liststore->children().empty();
   }

   Glib::ustring get_page_title() {
      unsigned int size = m_liststore->children().size();
      if (size == 0)
         return _("There Is No Change");

      return Glib::ustring::compose(ngettext("Confirm %1 Change", "Confirm %1 Changes", size), size);
   }

   // Apply the accepted change to the document.
   void apply(Document* doc) {
      g_return_if_fail(doc);

      bool remove_blank = m_checkRemoveBlank->get_active();
      std::vector<Subtitle> blank_subs, selection;

      doc->start_command(_("Text Correction"));
      Subtitles subtitles = doc->subtitles();

      for (Gtk::TreeIter it = m_liststore->children().begin(); it; ++it) {
         bool accept = (*it)[m_column.accept];
         if (accept == false)
            continue;

         unsigned int num = (*it)[m_column.num];
         Glib::ustring corrected = (*it)[m_column.corrected];

         Subtitle sub = subtitles.get(num);
         if (sub.get_text() != corrected) {
            sub.set_text(corrected);
            selection.push_back(sub);
         }
         if (remove_blank)
            if (sub.get_text().empty())
               blank_subs.push_back(sub);
      }
      // Select the modified subtitles
      subtitles.select(selection);
      // Remove the blank subtitles
      if (remove_blank && blank_subs.empty() == false)
         subtitles.remove(blank_subs);

      doc->finish_command();
   }

  protected:
   // Mark all items.
   void on_mark_all() {
      Gtk::TreeIter it = m_liststore->children().begin();
      while (it) {
         (*it)[m_column.accept] = true;
         ++it;
      }
   }

   // Unmark all items.
   void on_unmark_all() {
      Gtk::TreeIter it = m_liststore->children().begin();
      while (it) {
         (*it)[m_column.accept] = false;
         ++it;
      }
   }

   // Toggle the state of accept value.
   void on_accept_toggled(const Glib::ustring& path) {
      Gtk::TreeIter it = m_liststore->get_iter(path);
      if (it) {
         (*it)[m_column.accept] = !static_cast<bool>((*it)[m_column.accept]);
      }
   }

   void on_row_activated(const Gtk::TreeModel::Path& path, Gtk::TreeViewColumn* column) {
      if (column == m_column_corrected_text)
         return;
      on_accept_toggled(path.to_string());
   }

   // Update the item text.
   void on_corrected_edited(const Glib::ustring& path, const Glib::ustring& text) {
      Gtk::TreeIter it = m_liststore->get_iter(path);
      if (it) {
         (*it)[m_column.corrected] = text;
      }
   }

  protected:
   Column m_column;
   Glib::RefPtr<Gtk::ListStore> m_liststore;
   Gtk::TreeView* m_treeview;
   Gtk::TreeViewColumn* m_column_corrected_text;
   Gtk::Button* m_buttonMarkAll;
   Gtk::Button* m_buttonUnmarkAll;
   Gtk::CheckButton* m_checkRemoveBlank;
};