# HG changeset patch # User Torsten # Date 1396380588 -7200 # Node ID 086093fbdc1a31619e2e96d474518101cebfbde7 # Parent 6e81b59d657c8e875357f1c173ad73fd79adb0be first implementation of a shortcut manager (bug #41217) * file-editor.cc (set_shortcuts): use shortcut-manager to set some shortcuts * main_window.cc (set_global_shortcuts): use shortcut-manager to set some shortcuts; (construct_file_menu): all actions as class variables; (construct_edit_menu): all actions as class variables; * main_window.h: actions as class variables * module.mk: new files shortcut_manager.cc, shortcut_manager.h * octave-gui.cc (octave_start_gui): initialize the shortcut_manager * settings-dialog.cc (constructor): call shortcut-manager for shortcut table (write_changed_settings): call shortcut-manager for writing shortcuts * settings-dialog.ui: new tab with a tree widget for the shortcuts * shortcut_manager.cc (constructor, destructor): new class; (instance_ok): checks if instance is valid, creates a new one otherwise; (do_init_data): initialize the list with all shortcut's data; (init): internal function for initializing the data list; (do_fill_treewidget): fills the tree widget in the settings dialog; (do_write_shortcuts): writes shortcuts from settings dialog into file; (do_set_shortcut): setting the shortcut for an action; (handle_double_clicked): slot for double clicking into the tree widget; (shortcut_dialog): dialog for entering a new shortcut; (shortcut_dialog_finished): processing the dialog's result; (shortcut_dialog_set_default): setting the shortcut to it's default; (enter_shortcut::enter_shortcut): new class derived from QLineEdit; (enter_shortcut::handle_direct_shortcut): switch between normal editing and directly entering a shortcut; (enter_shortcut::keyPressEvent): event handler filtering the shortcuts; * shortcut_manager.h (init_data): static function calling do_init_data; (write_shortcuts): static function calling do_write_shortcuts; (fill_treewidget): static function calling do_fill_treewidget; (set_shortcut): static function calling do_set_shortcut; diff -r 6e81b59d657c -r 086093fbdc1a libgui/src/m-editor/file-editor.cc --- a/libgui/src/m-editor/file-editor.cc Thu Mar 06 03:56:59 2014 +0500 +++ b/libgui/src/m-editor/file-editor.cc Tue Apr 01 21:29:48 2014 +0200 @@ -28,6 +28,8 @@ #include "file-editor.h" #include "resource-manager.h" +#include "shortcut-manager.h" + #include #include #include @@ -1552,6 +1554,10 @@ { if (set) { + + shortcut_manager::set_shortcut (_save_action, "editor_file:save"); + shortcut_manager::set_shortcut (_save_as_action, "editor_file:save_as"); + _comment_selection_action->setShortcut (Qt::ControlModifier + Qt::Key_R); _uncomment_selection_action->setShortcut (Qt::SHIFT + Qt::ControlModifier @@ -1586,12 +1592,11 @@ _context_run_action->setShortcut (Qt::Key_F9); _context_edit_action->setShortcut (Qt::ControlModifier + Qt::Key_E); - _save_action->setShortcut (QKeySequence::Save); - _save_as_action->setShortcut (QKeySequence::SaveAs); _close_action->setShortcut (QKeySequence::Close); _redo_action->setShortcut (QKeySequence::Redo); _undo_action->setShortcut (QKeySequence::Undo); + } else { diff -r 6e81b59d657c -r 086093fbdc1a libgui/src/main-window.cc --- a/libgui/src/main-window.cc Thu Mar 06 03:56:59 2014 +0500 +++ b/libgui/src/main-window.cc Tue Apr 01 21:29:48 2014 +0200 @@ -51,6 +51,7 @@ #endif #include "main-window.h" #include "settings-dialog.h" +#include "shortcut-manager.h" #include "__init_qt__.h" @@ -680,6 +681,9 @@ int icon_size = settings->value ("toolbar_icon_size",16).toInt (); _main_tool_bar->setIconSize (QSize (icon_size,icon_size)); + set_global_shortcuts (true); + set_global_shortcuts (command_window_has_focus ()); + resource_manager::update_network_settings (); } @@ -1442,15 +1446,15 @@ file_menu->addSeparator (); - QAction *load_workspace_action + _load_workspace_action = file_menu->addAction (tr ("Load Workspace...")); - QAction *save_workspace_action + _save_workspace_action = file_menu->addAction (tr ("Save Workspace As...")); file_menu->addSeparator (); - QAction *preferences_action + _preferences_action = file_menu->addAction (QIcon (":/actions/icons/configure.png"), tr ("Preferences...")); @@ -1459,7 +1463,7 @@ _exit_action = file_menu->addAction (tr ("Exit")); _exit_action->setShortcutContext (Qt::ApplicationShortcut); - connect (preferences_action, SIGNAL (triggered ()), + connect (_preferences_action, SIGNAL (triggered ()), this, SLOT (process_settings_dialog_request ())); #ifdef HAVE_QSCINTILLA @@ -1467,10 +1471,10 @@ editor_window, SLOT (request_open_file ())); #endif - connect (load_workspace_action, SIGNAL (triggered ()), + connect (_load_workspace_action, SIGNAL (triggered ()), this, SLOT (handle_load_workspace_request ())); - connect (save_workspace_action, SIGNAL (triggered ()), + connect (_save_workspace_action, SIGNAL (triggered ()), this, SLOT (handle_save_workspace_request ())); connect (_exit_action, SIGNAL (triggered ()), @@ -1491,8 +1495,8 @@ _new_function_action->setEnabled (true); _new_function_action->setShortcutContext (Qt::ApplicationShortcut); - QAction *new_figure_action = new_menu->addAction (tr ("Figure")); - new_figure_action->setEnabled (true); + _new_figure_action = new_menu->addAction (tr ("Figure")); + _new_figure_action->setEnabled (true); #ifdef HAVE_QSCINTILLA connect (_new_script_action, SIGNAL (triggered ()), @@ -1502,7 +1506,7 @@ editor_window, SLOT (request_new_function ())); #endif - connect (new_figure_action, SIGNAL (triggered ()), + connect (_new_figure_action, SIGNAL (triggered ()), this, SLOT (handle_new_figure_request ())); } @@ -1515,22 +1519,18 @@ _undo_action = edit_menu->addAction (QIcon (":/actions/icons/undo.png"), tr ("Undo")); - _undo_action->setShortcut (QKeySequence::Undo); edit_menu->addSeparator (); _copy_action = edit_menu->addAction (QIcon (":/actions/icons/editcopy.png"), tr ("Copy"), this, SLOT (copyClipboard ())); - _copy_action->setShortcut (QKeySequence::Copy); - _paste_action = edit_menu->addAction (QIcon (":/actions/icons/editpaste.png"), tr ("Paste"), this, SLOT (pasteClipboard ())); - _paste_action->setShortcut (QKeySequence::Paste); - - QAction * select_all_action + + _select_all_action = edit_menu->addAction (tr ("Select All"), this, SLOT (selectAll ())); _clear_clipboard_action @@ -1543,25 +1543,25 @@ edit_menu->addSeparator (); - QAction *clear_command_window_action + _clear_command_window_action = edit_menu->addAction (tr ("Clear Command Window")); - QAction *clear_command_history + _clear_command_history_action = edit_menu->addAction (tr ("Clear Command History")); - QAction *clear_workspace_action + _clear_workspace_action = edit_menu->addAction (tr ("Clear Workspace")); connect (_find_files_action, SIGNAL (triggered ()), this, SLOT (find_files ())); - connect (clear_command_window_action, SIGNAL (triggered ()), + connect (_clear_command_window_action, SIGNAL (triggered ()), this, SLOT (handle_clear_command_window_request ())); - connect (clear_command_history, SIGNAL (triggered ()), + connect (_clear_command_history_action, SIGNAL (triggered ()), this, SLOT (handle_clear_history_request ())); - connect (clear_workspace_action, SIGNAL (triggered ()), + connect (_clear_workspace_action, SIGNAL (triggered ()), this, SLOT (handle_clear_workspace_request ())); connect (_clipboard, SIGNAL (changed (QClipboard::Mode)), @@ -2282,17 +2282,26 @@ if (set_shortcuts) { - _open_action->setShortcut (QKeySequence::Open); - _new_script_action->setShortcut (QKeySequence::New); - _new_function_action->setShortcut (Qt::ControlModifier - + Qt::ShiftModifier - + Qt::Key_N); - - _exit_action->setShortcut (QKeySequence::Quit); - - _find_files_action->setShortcut (Qt::ControlModifier - + Qt::ShiftModifier - + Qt::Key_F); + // file menu + shortcut_manager::set_shortcut (_open_action, "main_file:open_file"); + shortcut_manager::set_shortcut (_new_script_action, "main_file:new_file"); + shortcut_manager::set_shortcut (_new_function_action, "main_file:new_function"); + shortcut_manager::set_shortcut (_new_function_action, "main_file:new_figure"); + shortcut_manager::set_shortcut (_load_workspace_action, "main_file:load_workspace"); + shortcut_manager::set_shortcut (_save_workspace_action, "main_file:save_workspace"); + shortcut_manager::set_shortcut (_preferences_action, "main_file:preferences"); + shortcut_manager::set_shortcut (_exit_action,"main_file:exit"); + + // edit menu + shortcut_manager::set_shortcut (_copy_action, "main_edit:copy"); + shortcut_manager::set_shortcut (_paste_action, "main_edit:paste"); + shortcut_manager::set_shortcut (_undo_action, "main_edit:undo"); + shortcut_manager::set_shortcut (_select_all_action, "main_edit:select_all"); + shortcut_manager::set_shortcut (_clear_clipboard_action, "main_edit:clear_clipboard"); + shortcut_manager::set_shortcut (_find_files_action, "main_edit:find_in_files"); + shortcut_manager::set_shortcut (_clear_command_history_action, "main_edit:clear_history"); + shortcut_manager::set_shortcut (_clear_command_window_action, "main_edit:clear_command_window"); + shortcut_manager::set_shortcut (_clear_workspace_action, "main_edit:clear_workspace"); } else @@ -2300,13 +2309,26 @@ QKeySequence no_key = QKeySequence (); + // file menu _open_action->setShortcut (no_key); _new_script_action->setShortcut (no_key); _new_function_action->setShortcut (no_key); - + _new_function_action->setShortcut (no_key); + _load_workspace_action->setShortcut (no_key); + _save_workspace_action->setShortcut (no_key); + _preferences_action->setShortcut (no_key); _exit_action->setShortcut (no_key); + // edit menu + //_copy_action->setShortcut (no_key); + //_paste_action->setShortcut (no_key); + //_undo_action->setShortcut (no_key); + _select_all_action->setShortcut (no_key); + _clear_clipboard_action->setShortcut (no_key); _find_files_action->setShortcut (no_key); + _clear_command_history_action->setShortcut (no_key); + _clear_command_window_action->setShortcut (no_key); + _clear_workspace_action->setShortcut (no_key); } diff -r 6e81b59d657c -r 086093fbdc1a libgui/src/main-window.h --- a/libgui/src/main-window.h Thu Mar 06 03:56:59 2014 +0500 +++ b/libgui/src/main-window.h Tue Apr 01 21:29:48 2014 +0200 @@ -315,14 +315,21 @@ QAction *_new_script_action; QAction *_new_function_action; QAction *_open_action; + QAction *_new_figure_action; + QAction *_load_workspace_action; + QAction *_save_workspace_action; + QAction *_preferences_action; + QAction *_exit_action; QAction *_copy_action; QAction *_paste_action; QAction *_clear_clipboard_action; QAction *_undo_action; - + QAction *_clear_command_window_action; + QAction *_clear_command_history_action; + QAction *_clear_workspace_action; QAction *_find_files_action; - QAction *_exit_action; + QAction *_select_all_action; // Toolbars. QComboBox *_current_directory_combo_box; diff -r 6e81b59d657c -r 086093fbdc1a libgui/src/module.mk --- a/libgui/src/module.mk Thu Mar 06 03:56:59 2014 +0500 +++ b/libgui/src/module.mk Tue Apr 01 21:29:48 2014 +0200 @@ -96,6 +96,7 @@ src/moc-terminal-dock-widget.cc \ src/moc-color-picker.cc \ src/moc-resource-manager.cc \ + src/moc-shortcut-manager.cc \ src/moc-welcome-wizard.cc \ src/moc-workspace-model.cc \ src/moc-workspace-view.cc \ @@ -133,6 +134,7 @@ src/qtinfo/webinfo.h \ src/resource-manager.h \ src/settings-dialog.h \ + src/shortcut-manager.h \ src/thread-manager.h \ src/terminal-dock-widget.h \ src/color-picker.h \ @@ -160,6 +162,7 @@ src/qtinfo/webinfo.cc \ src/resource-manager.cc \ src/settings-dialog.cc \ + src/shortcut-manager.cc \ src/thread-manager.cc \ src/terminal-dock-widget.cc \ src/color-picker.cc \ diff -r 6e81b59d657c -r 086093fbdc1a libgui/src/octave-gui.cc --- a/libgui/src/octave-gui.cc Thu Mar 06 03:56:59 2014 +0500 +++ b/libgui/src/octave-gui.cc Tue Apr 01 21:29:48 2014 +0200 @@ -48,6 +48,7 @@ #include "welcome-wizard.h" #include "resource-manager.h" +#include "shortcut-manager.h" #include "main-window.h" #include "octave-gui.h" #include "thread-manager.h" @@ -192,6 +193,9 @@ octave_env::putenv ("TERM", "cygwin"); #endif + // shortcut manager + shortcut_manager::init_data (); + // Create and show main window. main_window w; diff -r 6e81b59d657c -r 086093fbdc1a libgui/src/settings-dialog.cc --- a/libgui/src/settings-dialog.cc Thu Mar 06 03:56:59 2014 +0500 +++ b/libgui/src/settings-dialog.cc Tue Apr 01 21:29:48 2014 +0200 @@ -25,6 +25,7 @@ #endif #include "resource-manager.h" +#include "shortcut-manager.h" #include "workspace-model.h" #include "settings-dialog.h" #include "ui-settings-dialog.h" @@ -57,6 +58,8 @@ { ui->setupUi (this); + shortcut_manager::fill_treewidget (ui->shortcuts_treewidget); + QSettings *settings = resource_manager::get_settings (); // restore last geometry @@ -675,6 +678,8 @@ write_terminal_colors (settings); + shortcut_manager::write_shortcuts (); + settings->setValue ("settings/last_tab",ui->tabWidget->currentIndex ()); settings->setValue ("settings/geometry",saveGeometry ()); settings->sync (); diff -r 6e81b59d657c -r 086093fbdc1a libgui/src/settings-dialog.ui --- a/libgui/src/settings-dialog.ui Thu Mar 06 03:56:59 2014 +0500 +++ b/libgui/src/settings-dialog.ui Tue Apr 01 21:29:48 2014 +0200 @@ -32,7 +32,7 @@ - 3 + 6 @@ -1165,6 +1165,12 @@ + + + 40 + 20 + + @@ -1255,11 +1261,11 @@ - + 0 - + @@ -1274,7 +1280,7 @@ 1000 - + @@ -1327,12 +1333,6 @@ - - - 40 - 20 - - @@ -1648,6 +1648,132 @@ + + + Shortcuts + + + + + + true + + + + + 0 + 0 + 678 + 378 + + + + + + + QLayout::SetDefaultConstraint + + + 0 + + + + + Enter a new shortcut by double clicking on the related row. + + + + + + + 0 + + + + + + 0 + 0 + + + + + 0 + 0 + + + + + 0 + 0 + + + + 0 + + + true + + + false + + + false + + + false + + + 3 + + + false + + + 120 + + + false + + + 64 + + + true + + + + Action + + + + + Default Shortcut + + + AlignLeft|AlignVCenter + + + + + Actual Shortcut + + + AlignLeft|AlignVCenter + + + + + + + + + + + + + + Network diff -r 6e81b59d657c -r 086093fbdc1a libgui/src/shortcut-manager.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libgui/src/shortcut-manager.cc Tue Apr 01 21:29:48 2014 +0200 @@ -0,0 +1,402 @@ +/* + +Copyright (C) 2014 Torsten + +This file is part of Octave. + +Octave 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. + +Octave 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 Octave; see the file COPYING. If not, see +. + +*/ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "error.h" +#include "resource-manager.h" +#include "shortcut-manager.h" +#include "singleton-cleanup.h" + +shortcut_manager *shortcut_manager::instance = 0; + +shortcut_manager::shortcut_manager () +{ + setObjectName ("Shortcut_Manager"); +} + +shortcut_manager::~shortcut_manager () +{ +} + +bool +shortcut_manager::instance_ok (void) +{ + bool retval = true; + + if (! instance) + { + instance = new shortcut_manager (); + + if (instance) + singleton_cleanup_list::add (cleanup_instance); + } + + if (! instance) + { + ::error ("unable to create shortcut_manager object!"); + + retval = false; + } + + return retval; +} + +void +shortcut_manager::do_init_data () +{ + // actions of the main window + init (tr ("New File"), "main_file:new_file", QKeySequence::New ); + init (tr ("New Function"), "main_file:new_function", QKeySequence ("Ctrl+Shift+N") ); + init (tr ("New Figure"), "main_file:new_figure", QKeySequence () ); + init (tr ("Open File"), "main_file:open_file", QKeySequence::Open ); + init (tr ("Load Workspace"), "main_file:load_workspace", QKeySequence () ); + init (tr ("Save Workspace As"), "main_file:save_workspace", QKeySequence () ); + init (tr ("Preferences"), "main_file:preferences", QKeySequence () ); + init (tr ("Exit Octave"), "main_file:exit", QKeySequence::Quit ); + + init (tr ("Copy"), "main_edit:copy", QKeySequence::Copy); + init (tr ("Paste"), "main_edit:paste", QKeySequence::Paste); + init (tr ("Undo"), "main_edit:undo", QKeySequence::Undo); + init (tr ("Select All"), "main_edit:select_all", QKeySequence () ); + init (tr ("Clear Clipboard"), "main_edit:clear_clipboard", QKeySequence () ); + init (tr ("Find in Files"), "main_edit:find_in_files", QKeySequence (Qt::ControlModifier + Qt::ShiftModifier + Qt::Key_F) ); + init (tr ("Clear Command Window"), "main_edit:clear_command_window", QKeySequence () ); + init (tr ("Clear Command History"), "main_edit:clear_history", QKeySequence () ); + init (tr ("Clear Workspace"), "main_edit:clear_workspace", QKeySequence () ); + + // actions of the editor + init (tr ("Save File"), "editor_file:save", QKeySequence::Save ); + init (tr ("Save File As"), "editor_file:save_as", QKeySequence::SaveAs ); +} + +void +shortcut_manager::init (QString description, QString key, QKeySequence def_sc) +{ + QSettings *settings = resource_manager::get_settings (); + + settings->beginGroup ("shortcuts"); + QKeySequence actual = QKeySequence (settings->value (key, def_sc).toString ()); + settings->endGroup (); + + shortcut_t shortcut_info; + shortcut_info.description = description; + shortcut_info.settings_key = key; + shortcut_info.actual_sc = actual; + shortcut_info.default_sc = def_sc; + _sc << shortcut_info; + + if (! actual.isEmpty ()) + _shortcut_hash[actual] = _sc.count (); // offset of 1 to avoid 0 + _action_hash[key] = _sc.count (); // offset of 1 to avoid 0 +} + +void +shortcut_manager::do_fill_treewidget (QTreeWidget *tree_view) +{ + _dialog = 0; + _level_hash.clear (); + + tree_view->header ()->setResizeMode (QHeaderView::ResizeToContents); + + QTreeWidgetItem *main = new QTreeWidgetItem (tree_view); + main->setText (0, tr ("Main")); + main->setExpanded (true); + QTreeWidgetItem *main_file = new QTreeWidgetItem (main); + main_file->setText (0, tr ("File")); + QTreeWidgetItem *main_edit = new QTreeWidgetItem (main); + main_edit->setText (0, tr ("Edit")); + QTreeWidgetItem *main_debug = new QTreeWidgetItem (main); + main_debug->setText (0, tr ("Debug")); + QTreeWidgetItem *main_window = new QTreeWidgetItem (main); + main_window->setText (0, tr ("Window")); + QTreeWidgetItem *main_help = new QTreeWidgetItem (main); + main_help->setText (0, tr ("Help")); + QTreeWidgetItem *main_news = new QTreeWidgetItem (main); + main_news->setText (0, tr ("News")); + + _level_hash["main_file"] = main_file; + _level_hash["main_edit"] = main_edit; + _level_hash["main_debug"] = main_debug; + _level_hash["main_window"] = main_window; + _level_hash["main_help"] = main_help; + _level_hash["main_news"] = main_news; + + QTreeWidgetItem *editor = new QTreeWidgetItem (tree_view); + editor->setText (0, tr ("Editor")); + editor->setExpanded (true); + QTreeWidgetItem *editor_file = new QTreeWidgetItem (editor); + editor_file->setText (0, tr ("File")); + QTreeWidgetItem *editor_edit = new QTreeWidgetItem (editor); + editor_edit->setText (0, tr ("Edit")); + QTreeWidgetItem *editor_view = new QTreeWidgetItem (editor); + editor_view->setText (0, tr ("View")); + QTreeWidgetItem *editor_debug = new QTreeWidgetItem (editor); + editor_debug->setText (0, tr ("Debug")); + QTreeWidgetItem *editor_run = new QTreeWidgetItem (editor); + editor_run->setText (0, tr ("Run")); + QTreeWidgetItem *editor_help = new QTreeWidgetItem (editor); + editor_help->setText (0, tr ("Help")); + + _level_hash["editor_file"] = editor_file; + _level_hash["editor_edit"] = editor_edit; + _level_hash["editor_view"] = editor_view; + _level_hash["editor_debug"] = editor_debug; + _level_hash["editor_run"] = editor_run; + _level_hash["editor_help"] = editor_help; + + connect (tree_view, SIGNAL (itemDoubleClicked (QTreeWidgetItem*, int)), + this, SLOT (handle_double_clicked (QTreeWidgetItem*, int))); + + for (int i = 0; i < _sc.count (); i++) + { + shortcut_t shortcut_info = _sc.at (i); + + QTreeWidgetItem* section = _level_hash[shortcut_info.settings_key.section(':',0,0)]; + QTreeWidgetItem* tree_item = new QTreeWidgetItem (section); + + tree_item->setText (0, shortcut_info.description); + tree_item->setText (1, shortcut_info.default_sc); + tree_item->setText (2, shortcut_info.actual_sc); + + _item_index_hash[tree_item] = i + 1; // index+1 to avoid 0 + _index_item_hash[i] = tree_item; + } +} + +void +shortcut_manager::do_write_shortcuts () +{ + QSettings *settings = resource_manager::get_settings (); + + settings->beginGroup ("shortcuts"); + for (int i = 0; i < _sc.count (); i++) + settings->setValue(_sc.at (i).settings_key, _sc.at (i).actual_sc.toString ()); + settings->endGroup (); + + settings->sync (); + + delete _dialog; +} + +void +shortcut_manager::do_set_shortcut (QAction* action, const QString& key) +{ + QSettings *settings = resource_manager::get_settings (); + + int index = _action_hash[key] - 1; + + action->setShortcut ( + settings->value ("shortcuts/" + key, _sc.at (index).default_sc).toString ()); +} + +void +shortcut_manager::handle_double_clicked (QTreeWidgetItem* item, int) +{ + int i = _item_index_hash[item]; + if (i == 0) + return; // top-level-item clicked + + shortcut_dialog (i-1); // correct to index starting at 0 +} + +void +shortcut_manager::shortcut_dialog (int index) +{ + if (! _dialog) + { + _dialog = new QDialog (this); + + _dialog->setWindowTitle (tr ("Enter new Shortcut")); + + QVBoxLayout *box = new QVBoxLayout(_dialog); + + QLabel *help = new QLabel (tr ("Apply the desired shortcut or click " + "on the right button to reset the " + "shortcut to its default.")); + help->setWordWrap (true); + box->addWidget (help); + + QCheckBox *direct = new QCheckBox (tr ("Enter shortcut directly by performing it")); + direct->setCheckState (Qt::Checked); + box->addWidget (direct); + + QGridLayout *grid = new QGridLayout(); + + QLabel *actual = new QLabel (tr ("Actual shortcut")); + _edit_actual = new enter_shortcut (_dialog); + _edit_actual->setAlignment (Qt::AlignHCenter); + grid->addWidget (actual, 0, 0); + grid->addWidget (_edit_actual, 0, 1); + + QLabel *def = new QLabel (tr ("Default shortcut")); + _label_default = new QLabel (_dialog); + _label_default->setAlignment (Qt::AlignHCenter); + grid->addWidget (def, 1, 0); + grid->addWidget (_label_default, 1, 1); + + QPushButton *set_default = new QPushButton (tr ("Set to default")); + grid->addWidget (set_default, 0, 2); + connect (set_default, SIGNAL (clicked ()), + this, SLOT (shortcut_dialog_set_default ())); + + box->addLayout (grid); + + QDialogButtonBox *button_box = new QDialogButtonBox (QDialogButtonBox::Ok + | QDialogButtonBox::Cancel); + QList buttons = button_box->buttons (); + for (int i = 0; i < buttons.count (); i++) + buttons.at (i)->setShortcut (QKeySequence ()); + connect(button_box, SIGNAL (accepted ()), _dialog, SLOT (accept ())); + connect(button_box, SIGNAL (rejected ()), _dialog, SLOT (reject ())); + box->addWidget (button_box); + + _dialog->setLayout (box); + + connect (direct, SIGNAL (stateChanged (int)), + _edit_actual, SLOT (handle_direct_shortcut (int))); + connect (_dialog, SIGNAL (finished (int)), + this, SLOT (shortcut_dialog_finished (int))); + + } + + _edit_actual->setText (_sc.at (index).actual_sc); + _label_default->setText (_sc.at (index).default_sc); + _handled_index = index; + + _edit_actual->setFocus (); + _dialog->setFocusProxy (_edit_actual); + _dialog->exec (); +} + +void +shortcut_manager::shortcut_dialog_finished (int result) +{ + if (result == QDialog::Rejected) + return; + + int double_index = _shortcut_hash[_edit_actual->text()] - 1; + + if (double_index >= 0 && double_index != _handled_index) + { + int ret = QMessageBox::warning(this, tr("Double Shortcut"), + tr ("The chosen shortcut\n \"%1\"\n" + "is already used for the action\n \"%2\".\n" + "Do you want to use the shortcut anyhow removing it " + "from the previous action?") + .arg (_edit_actual->text()) + .arg (_sc.at (double_index).description), + QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes); + + if (ret == QMessageBox::Yes) + { + shortcut_t double_shortcut = _sc.at (double_index); + double_shortcut.actual_sc = QKeySequence (); + _sc.replace (double_index, double_shortcut); + _index_item_hash[double_index]->setText (1, QKeySequence ()); + } + else + return; + } + + shortcut_t shortcut = _sc.at (_handled_index); + if (! shortcut.actual_sc.isEmpty ()) + _shortcut_hash.remove (shortcut.actual_sc); + shortcut.actual_sc = _edit_actual->text(); + _sc.replace (_handled_index, shortcut); + + _index_item_hash[_handled_index]->setText (2, shortcut.actual_sc); + + if (! shortcut.actual_sc.isEmpty ()) + _shortcut_hash[shortcut.actual_sc] = _handled_index + 1; // index+1 to avoid 0 +} + +void +shortcut_manager::shortcut_dialog_set_default () +{ + _edit_actual->setText (_label_default->text ()); +} + + + +enter_shortcut::enter_shortcut (QWidget *p) : QLineEdit (p) +{ + _direct_shortcut = true; +} + +enter_shortcut::~enter_shortcut () +{ +} + +void +enter_shortcut::handle_direct_shortcut (int state) +{ + if (state) + _direct_shortcut = true; + else + _direct_shortcut = false; +} + +void +enter_shortcut::keyPressEvent (QKeyEvent *e) +{ + if (! _direct_shortcut) + { + QLineEdit::keyPressEvent (e); + return; + } + + if (e->type () == QEvent::KeyPress) + { + int key = e->key (); + + if (key == Qt::Key_unknown || key == 0 || key >= 16777248) + return; + + Qt::KeyboardModifiers modifiers = e->modifiers (); + + if(modifiers & Qt::ShiftModifier) + key += Qt::SHIFT; + if(modifiers & Qt::ControlModifier) + key += Qt::CTRL; + if(modifiers & Qt::AltModifier) + key += Qt::ALT; + if(modifiers & Qt::MetaModifier) + key += Qt::META; + + setText (QKeySequence(key)); + } +} + diff -r 6e81b59d657c -r 086093fbdc1a libgui/src/shortcut-manager.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libgui/src/shortcut-manager.h Tue Apr 01 21:29:48 2014 +0200 @@ -0,0 +1,139 @@ +/* + +Copyright (C) 2014 Torsten + +This file is part of Octave. + +Octave 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. + +Octave 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 Octave; see the file COPYING. If not, see +. + +*/ + +#ifndef SHORTCUT_MANAGER_H +#define SHORTCUT_MANAGER_H + +#include +#include +#include +#include +#include + + +class enter_shortcut : public QLineEdit +{ + Q_OBJECT + +public: + enter_shortcut (QWidget *p = 0); + ~enter_shortcut (); + + virtual void keyPressEvent (QKeyEvent *e); + +public slots: + void handle_direct_shortcut (int); + +private: + bool _direct_shortcut; + +}; + + +class shortcut_manager : public QWidget +{ + Q_OBJECT + +public: + shortcut_manager (); + ~shortcut_manager (); + + static void init_data () + { + if (instance_ok ()) + instance->do_init_data (); + } + + static void write_shortcuts () + { + if (instance_ok ()) + instance->do_write_shortcuts (); + } + + static void set_shortcut (QAction *action, const QString& key) + { + if (instance_ok ()) + instance->do_set_shortcut (action, key); + } + + static void fill_treewidget (QTreeWidget *tree_view) + { + if (instance_ok ()) + instance->do_fill_treewidget (tree_view); + } + +public slots: + +signals: + +protected: + +protected slots: + + void handle_double_clicked (QTreeWidgetItem*, int); + void shortcut_dialog_finished (int); + void shortcut_dialog_set_default (); + +private: + + static shortcut_manager *instance; + static void cleanup_instance (void) { delete instance; instance = 0; } + + // No copying! + + shortcut_manager (const shortcut_manager&); + shortcut_manager& operator = (const shortcut_manager&); + + static bool instance_ok (void); + + void init (QString, QString, QKeySequence); + void do_init_data (); + void do_write_shortcuts (); + void do_set_shortcut (QAction *action, const QString& key); + void do_fill_treewidget (QTreeWidget *tree_view); + void shortcut_dialog (int); + + struct shortcut_t + { + QString description; + QString settings_key; + QKeySequence actual_sc; + QKeySequence default_sc; + QTreeWidgetItem *tree_item; + }; + + QList _sc; + QHash _shortcut_hash; + QHash _action_hash; + QHash _level_hash; + QHash _index_item_hash; + QHash _item_index_hash; + + QDialog *_dialog; + enter_shortcut *_edit_actual; + QLabel *_label_default; + int _handled_index; + +}; + + +#endif // SHORTCUT_MANAGER_H