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 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
|
// 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 <gtkmm_utility.h>
#include <utility.h>
#include <memory>
class DialogViewEdit : public Gtk::Dialog {
class ColumnRecord : public Gtk::TreeModel::ColumnRecord {
public:
ColumnRecord() {
add(display);
add(name);
add(label);
}
Gtk::TreeModelColumn<bool> display;
Gtk::TreeModelColumn<Glib::ustring> name;
Gtk::TreeModelColumn<Glib::ustring> label;
};
public:
DialogViewEdit(BaseObjectType* cobject, const Glib::RefPtr<Gtk::Builder>& builder) : Gtk::Dialog(cobject) {
utility::set_transient_parent(*this);
builder->get_widget("treeview-columns", m_treeview);
create_treeview();
}
// Update the treeview with the columns displayed.
// Add remaining columns that are not displayed.
// Run the dialog and update columns_displayed.
void execute(Glib::ustring& columns_displayed) {
std::vector<std::string> array;
utility::split(columns_displayed, ';', array);
for (const auto& s : array) {
Gtk::TreeIter iter = m_liststore->append();
(*iter)[m_column_record.name] = s;
(*iter)[m_column_record.label] = SubtitleView::get_column_label_by_name(s);
(*iter)[m_column_record.display] = true;
}
// add other columns
auto all_columns = cfg::get_string_list("subtitle-view", "columns-list");
for (const auto& col : all_columns) {
if (std::find(array.begin(), array.end(), col) == array.end()) {
Gtk::TreeIter it = m_liststore->append();
(*it)[m_column_record.name] = col;
(*it)[m_column_record.label] = SubtitleView::get_column_label_by_name(col);
(*it)[m_column_record.display] = false;
}
}
run();
// get the new columns order
Glib::ustring columns_updated;
Gtk::TreeNodeChildren rows = m_liststore->children();
if (!rows.empty()) {
for (Gtk::TreeIter it = rows.begin(); it; ++it) {
if ((*it)[m_column_record.display] == true)
columns_updated += (*it)[m_column_record.name] + ";";
}
}
columns_displayed = columns_updated;
}
protected:
// Create the treeview with two columns : Display and Name
// Support DND .ui).
void create_treeview() {
m_liststore = Gtk::ListStore::create(m_column_record);
m_treeview->set_model(m_liststore);
// column display
{
Gtk::TreeViewColumn* column = manage(new Gtk::TreeViewColumn(_("Display")));
m_treeview->append_column(*column);
Gtk::CellRendererToggle* toggle = manage(new Gtk::CellRendererToggle);
column->pack_start(*toggle);
column->add_attribute(toggle->property_active(), m_column_record.display);
toggle->signal_toggled().connect(sigc::mem_fun(*this, &DialogViewEdit::on_display_toggled));
}
// column label
{
Gtk::TreeViewColumn* column = manage(new Gtk::TreeViewColumn(_("Name")));
m_treeview->append_column(*column);
Gtk::CellRendererText* label = manage(new Gtk::CellRendererText);
column->pack_start(*label);
column->add_attribute(label->property_text(), m_column_record.label);
}
}
// Toggle the state of the displayed column
void on_display_toggled(const Glib::ustring& path) {
Gtk::TreeIter iter = m_liststore->get_iter(path);
if (iter) {
bool state = (*iter)[m_column_record.display];
(*iter)[m_column_record.display] = !state;
}
}
protected:
ColumnRecord m_column_record;
Gtk::TreeView* m_treeview;
Glib::RefPtr<Gtk::ListStore> m_liststore;
};
class DialogViewManager : public Gtk::Dialog {
class ColumnRecord : public Gtk::TreeModel::ColumnRecord {
public:
ColumnRecord() {
add(name);
add(columns);
}
Gtk::TreeModelColumn<Glib::ustring> name;
Gtk::TreeModelColumn<Glib::ustring> columns;
};
public:
DialogViewManager(BaseObjectType* cobject, const Glib::RefPtr<Gtk::Builder>& builder) : Gtk::Dialog(cobject) {
utility::set_transient_parent(*this);
builder->get_widget("treeview", m_treeview);
builder->get_widget("button-add", m_buttonAdd);
builder->get_widget("button-remove", m_buttonRemove);
builder->get_widget("button-edit", m_buttonEdit);
m_buttonAdd->signal_clicked().connect(sigc::mem_fun(*this, &DialogViewManager::on_add));
m_buttonRemove->signal_clicked().connect(sigc::mem_fun(*this, &DialogViewManager::on_remove));
m_buttonEdit->signal_clicked().connect(sigc::mem_fun(*this, &DialogViewManager::on_edit));
create_treeview();
init_treeview();
}
void execute() {
run();
// save to the configuration
save_to_config();
}
protected:
void create_treeview() {
m_liststore = Gtk::ListStore::create(m_column_record);
m_treeview->set_model(m_liststore);
Gtk::TreeViewColumn* column = manage(new Gtk::TreeViewColumn(_("Name")));
m_treeview->append_column(*column);
Gtk::CellRendererText* name = manage(new Gtk::CellRendererText);
column->pack_start(*name);
column->add_attribute(name->property_text(), m_column_record.name);
name->property_editable() = true;
name->signal_edited().connect(sigc::mem_fun(*this, &DialogViewManager::on_name_edited));
m_treeview->get_selection()->signal_changed().connect(sigc::mem_fun(*this, &DialogViewManager::on_selection_changed));
}
void init_treeview() {
auto keys = cfg::get_keys("view-manager");
for (const auto& name : keys) {
auto cols = cfg::get_string("view-manager", name);
Gtk::TreeIter iter = m_liststore->append();
(*iter)[m_column_record.name] = name;
(*iter)[m_column_record.columns] = cols;
}
Gtk::TreeIter iter = m_liststore->get_iter("0");
if (iter)
m_treeview->get_selection()->select(iter);
else
on_selection_changed();
}
void on_name_edited(const Glib::ustring& path, const Glib::ustring& text) {
Gtk::TreeIter iter = m_liststore->get_iter(path);
(*iter)[m_column_record.name] = text;
}
void on_selection_changed() {
bool state = m_treeview->get_selection()->get_selected();
m_buttonRemove->set_sensitive(state);
m_buttonEdit->set_sensitive(state);
}
void on_add() {
Gtk::TreeIter iter = m_liststore->append();
(*iter)[m_column_record.name] = _("Untitled");
m_treeview->set_cursor(m_liststore->get_path(*iter), *m_treeview->get_column(0), true);
}
void on_remove() {
Gtk::TreeIter selected = m_treeview->get_selection()->get_selected();
if (selected) {
Glib::ustring name = (*selected)[m_column_record.name];
selected = m_liststore->erase(selected);
if (selected)
m_treeview->get_selection()->select(selected);
}
}
// Edit the selected item, launch the dialog edit
void on_edit() {
Gtk::TreeIter selected = m_treeview->get_selection()->get_selected();
if (selected) {
std::unique_ptr<DialogViewEdit> dialog(gtkmm_utility::get_widget_derived<DialogViewEdit>(
SE_DEV_VALUE(SE_PLUGIN_PATH_UI, SE_PLUGIN_PATH_DEV), "dialog-view-manager.ui", "dialog-view-edit"));
Glib::ustring columns = (*selected)[m_column_record.columns];
dialog->execute(columns);
// updated with the new columns displayed
(*selected)[m_column_record.columns] = columns;
}
}
// Delete the group "view-manager" and create with the new values
void save_to_config() {
cfg::remove_group("view-manager");
Gtk::TreeNodeChildren rows = m_liststore->children();
if (!rows.empty()) {
for (Gtk::TreeIter it = rows.begin(); it; ++it) {
Glib::ustring name = (*it)[m_column_record.name];
Glib::ustring columns = (*it)[m_column_record.columns];
cfg::set_string("view-manager", name, columns);
}
}
}
protected:
ColumnRecord m_column_record;
Gtk::TreeView* m_treeview;
Glib::RefPtr<Gtk::ListStore> m_liststore;
Gtk::Button* m_buttonAdd;
Gtk::Button* m_buttonRemove;
Gtk::Button* m_buttonEdit;
};
class ViewManagerPlugin : public Action {
public:
ViewManagerPlugin() {
activate();
update_ui();
}
~ViewManagerPlugin() {
deactivate();
}
// First check if the user has any preferences
void check_config() {
Glib::ustring group = "view-manager";
if (cfg::has_group(group) && !cfg::get_keys(group).empty())
return;
cfg::set_string(group, _("Simple"), "number;start;end;duration;text");
cfg::set_string(group, _("Advanced"), "number;start;end;duration;style;name;text");
cfg::set_string(group, _("Translation"), "number;text;translation");
cfg::set_string(group, _("Timing"), "number;start;end;duration;cps;text");
}
// Gets current view, returns empty when unsuccesfull
Glib::ustring get_current_view() {
se_dbg(SE_DBG_VIEW);
// Get current column configuration
Glib::ustring current_columns = cfg::get_string("subtitle-view", "columns-displayed");
Glib::ustring current_view = "";
// Find which view matches the current columns
auto keys = cfg::get_keys("view-manager");
for (const auto& name : keys) {
Glib::ustring view_columns = cfg::get_string("view-manager", name);
if (view_columns == current_columns)
current_view = name;
}
return current_view;
}
void activate() {
se_dbg(SE_DBG_VIEW);
if (m_current_view_name.empty()) {
m_current_view_name = get_current_view();
}
check_config();
// Set up menu items for views
action_group = Gtk::ActionGroup::create("ViewManagerPlugin");
Gtk::RadioAction::Group view_group;
auto keys = cfg::get_keys("view-manager");
for (const auto& name : keys) {
if (!m_current_view_name.empty()) {
// If the current displayed columns are from a view, display
// radiobuttons
// Note that on X11 with appmentu-gtk3 (aka global menus), this will
// not work if we first load from config column configuration that does
// not correspond to any view, then until restarted, no radio buttons
// will show even if we later set up a menu
Glib::RefPtr<Gtk::RadioAction> radio_action = Gtk::RadioAction::create(view_group, name, name, _("Switches to this view"));
action_group->add(radio_action, sigc::bind(sigc::mem_fun(*this, &ViewManagerPlugin::on_set_view), name));
} else {
// Create as regular menu item if no defined view is set
Glib::RefPtr<Gtk::Action> regular_action = Gtk::Action::create(name, name, _("Switches to this view"));
action_group->add(regular_action, sigc::bind(sigc::mem_fun(*this, &ViewManagerPlugin::on_set_view), name));
}
}
// Set View preferences menu item
action_group->add(Gtk::Action::create("view-manager-preferences", Gtk::Stock::PREFERENCES, _("View _Manager"), _("Manage the views")),
sigc::mem_fun(*this, &ViewManagerPlugin::on_view_manager));
// ui
Glib::RefPtr<Gtk::UIManager> ui = get_ui_manager();
ui->insert_action_group(action_group);
Glib::ustring submenu = R"(
<ui>
<menubar name='menubar'>
<menu name='menu-view' action='menu-view'>
<placeholder name='view-manager'>
<placeholder name='view-manager-placeholder'/>
<separator/>
<menuitem action='view-manager-preferences'/>
</placeholder>
</menu>
</menubar>
</ui>
)";
ui_id = ui->add_ui_from_string(submenu);
for (const auto& name : keys) {
ui->add_ui(ui_id, "/menubar/menu-view/view-manager/view-manager-placeholder", name, name, Gtk::UI_MANAGER_AUTO, false);
}
update_ui();
ui->ensure_update();
}
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);
ui->ensure_update();
}
// Updates the configuration with the columns to display.
void on_set_view(const Glib::ustring& name) {
se_dbg(SE_DBG_VIEW);
bool was_empty = m_current_view_name.empty();
m_current_view_name = name;
Glib::ustring columns = cfg::get_string("view-manager", name);
cfg::set_string("subtitle-view", "columns-displayed", columns);
// if we do not do this check, we enter an endless loop
// we only do this when previously empty to display radio buttons
// this is a niche thing when in config there is different column
// configuration not corresponding to a view
if (was_empty) {
deactivate();
activate();
}
}
void on_view_manager() {
se_dbg(SE_DBG_VIEW);
std::unique_ptr<DialogViewManager> dialog(gtkmm_utility::get_widget_derived<DialogViewManager>(
SE_DEV_VALUE(SE_PLUGIN_PATH_UI, SE_PLUGIN_PATH_DEV), "dialog-view-manager.ui", "dialog-view-manager"));
dialog->execute();
on_set_view(m_current_view_name);
deactivate();
activate();
}
void update_ui() {
se_dbg(SE_DBG_VIEW);
// Find which view matches the current view
if (!m_current_view_name.empty()) {
auto keys = cfg::get_keys("view-manager");
for (const auto& view_name : keys) {
if (m_current_view_name == view_name) {
// Found matching view, activate its radio button
Glib::RefPtr<Gtk::Action> action = action_group->get_action(view_name);
if (action) {
Glib::RefPtr<Gtk::RadioAction> radio = Glib::RefPtr<Gtk::RadioAction>::cast_static(action);
if (radio)
radio->set_active(true);
}
break;
}
}
}
}
protected:
Gtk::UIManager::ui_merge_id ui_id;
Glib::RefPtr<Gtk::ActionGroup> action_group;
Glib::ustring m_current_view_name;
};
REGISTER_EXTENSION(ViewManagerPlugin)
|