GNU Octave  4.0.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
shortcut-manager.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2014-2015 Torsten <ttl@justmail.de>
4 
5 This file is part of Octave.
6 
7 Octave is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with Octave; see the file COPYING. If not, see
19 <http://www.gnu.org/licenses/>.
20 
21 */
22 
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26 
27 #include <QtCore>
28 #include <QMessageBox>
29 #include <QDebug>
30 #include <QGridLayout>
31 #include <QVBoxLayout>
32 #include <QDialogButtonBox>
33 #include <QPushButton>
34 #include <QLineEdit>
35 #include <QCheckBox>
36 #include <QHeaderView>
37 #include <QAction>
38 #include <QFileDialog>
39 
40 #include "error.h"
41 #include "resource-manager.h"
42 #include "shortcut-manager.h"
43 #include "singleton-cleanup.h"
44 
46 
48 {
49  setObjectName ("Shortcut_Manager");
50 
51  // Mac: don't let Qt interpret CMD key ("Meta" in Qt terminology) as Ctrl
52 #if defined (Q_OS_MAC)
53  QCoreApplication::setAttribute (Qt::AA_MacDontSwapCtrlAndMeta, true);
54 #endif
55 
57 }
58 
60 {
61 }
62 
63 bool
65 {
66  bool retval = true;
67 
68  if (! instance)
69  {
70  instance = new shortcut_manager ();
71 
72  if (instance)
74  }
75 
76  if (! instance)
77  {
78  ::error ("unable to create shortcut_manager object!");
79 
80  retval = false;
81  }
82 
83  return retval;
84 }
85 
86 void
88 {
89  QKeySequence ctrl;
90  int prefix;
91 #if defined (Q_OS_MAC)
92  // Use CMD key as an equivalent of Ctrl key on other platforms
93  ctrl = Qt::MetaModifier;
94  // Some of octave default shortcuts on windows/linux are already defined
95  // as system wide shortcuts on Mac Os X (almost all Function keys).
96  // Prefix those with Option (Alt) modifier to avoid conflicts.
97  prefix = Qt::AltModifier;
98 #else
99  ctrl = Qt::ControlModifier;
100  prefix = Qt::NoModifier;
101 #endif
102 
103  QKeySequence ctrl_shift = ctrl + Qt::ShiftModifier;
104  QKeySequence ctrl_alt = ctrl + Qt::AltModifier;
105 
106  // actions of the main window
107 
108  // file
109  init (tr ("New File"), "main_file:new_file", QKeySequence::New);
110  init (tr ("New Function"), "main_file:new_function",
111  QKeySequence (ctrl_shift + Qt::Key_N));
112  init (tr ("New Figure"), "main_file:new_figure", QKeySequence ());
113  init (tr ("Open File"), "main_file:open_file", QKeySequence::Open);
114  init (tr ("Load Workspace"), "main_file:load_workspace", QKeySequence ());
115  init (tr ("Save Workspace As"), "main_file:save_workspace", QKeySequence ());
116  init (tr ("Exit Octave"), "main_file:exit", QKeySequence::Quit);
117 
118  // edit
119  init (tr ("Copy"), "main_edit:copy", QKeySequence::Copy);
120  init (tr ("Paste"), "main_edit:paste", QKeySequence::Paste);
121  init (tr ("Undo"), "main_edit:undo", QKeySequence::Undo);
122  init (tr ("Select All"), "main_edit:select_all", QKeySequence ());
123  init (tr ("Clear Clipboard"), "main_edit:clear_clipboard", QKeySequence ());
124  init (tr ("Find in Files"), "main_edit:find_in_files",
125  QKeySequence (ctrl_shift + Qt::Key_F));
126  init (tr ("Clear Command Window"), "main_edit:clear_command_window",
127  QKeySequence ());
128  init (tr ("Clear Command History"), "main_edit:clear_history",
129  QKeySequence ());
130  init (tr ("Clear Workspace"), "main_edit:clear_workspace", QKeySequence ());
131  init (tr ("Preferences"), "main_edit:preferences", QKeySequence ());
132 
133  // debug
134  init (tr ("Step"), "main_debug:step_over",
135  QKeySequence (prefix + Qt::Key_F10));
136  init (tr ("Step Into"), "main_debug:step_into",
137  QKeySequence (prefix + Qt::Key_F11));
138  init (tr ("Step Out"), "main_debug:step_out",
139  QKeySequence (prefix + Qt::ShiftModifier + Qt::Key_F11));
140  init (tr ("Continue"), "main_debug:continue",
141  QKeySequence (prefix + Qt::Key_F5));
142  init (tr ("Quit Debug Mode"), "main_debug:quit",
143  QKeySequence (prefix + Qt::ShiftModifier + Qt::Key_F5));
144 
145  // window
146  init (tr ("Show Command Window"), "main_window:show_command",
147  prefix + ctrl_shift + Qt::Key_0);
148  init (tr ("Show Command History"), "main_window:show_history",
149  prefix + ctrl_shift + Qt::Key_1);
150  init (tr ("Show File Browser"), "main_window:show_file_browser",
151  prefix + ctrl_shift + Qt::Key_2);
152  init (tr ("Show Workspace"), "main_window:show_workspace",
153  prefix + ctrl_shift + Qt::Key_3);
154  init (tr ("Show Editor"), "main_window:show_editor",
155  prefix + ctrl_shift + Qt::Key_4);
156  init (tr ("Show Documentation"), "main_window:show_doc",
157  prefix + ctrl_shift + Qt::Key_5);
158  init (tr ("Command Window"), "main_window:command",
159  prefix + ctrl + Qt::Key_0);
160  init (tr ("Command History"), "main_window:history",
161  prefix + ctrl + Qt::Key_1);
162  init (tr ("File Browser"), "main_window:file_browser",
163  prefix + ctrl + Qt::Key_2);
164  init (tr ("Workspace"), "main_window:workspace",
165  prefix + ctrl + Qt::Key_3);
166  init (tr ("Editor"), "main_window:editor",
167  prefix + ctrl + Qt::Key_4);
168  init (tr ("Documentation"), "main_window:doc",
169  prefix + ctrl + Qt::Key_5);
170  init (tr ("Reset Default Window Layout"), "main_window:reset", QKeySequence ());
171 
172  // help
173  init (tr ("Show Ondisk Documentation"), "main_help:ondisk_doc",
174  QKeySequence ());
175  init (tr ("Show Online Documentation"), "main_help:online_doc",
176  QKeySequence ());
177  init (tr ("Report Bug"), "main_help:report_bug", QKeySequence ());
178  init (tr ("Octave Packages"), "main_help:packages", QKeySequence ());
179  init (tr ("Share Code"), "main_help:agora", QKeySequence ());
180  init (tr ("Contribute to Octave"), "main_help:contribute", QKeySequence ());
181  init (tr ("Octave Developer Resources"), "main_help:developer",
182  QKeySequence ());
183  init (tr ("About Octave"), "main_help:about", QKeySequence ());
184 
185  // news
186  init (tr ("Release Notes"), "main_news:release_notes", QKeySequence ());
187  init (tr ("Community News"), "main_news:community_news", QKeySequence ());
188 
189  // actions of the editor
190 
191  // file
192  init (tr ("Edit Function"), "editor_file:edit_function",
193  QKeySequence (ctrl + Qt::Key_E));
194  init (tr ("Save File"), "editor_file:save", QKeySequence::Save);
195  init (tr ("Save File As"), "editor_file:save_as", QKeySequence::SaveAs);
196  init (tr ("Close"), "editor_file:close", QKeySequence::Close);
197  init (tr ("Close All"), "editor_file:close_all", QKeySequence ());
198  init (tr ("Close Other Files"), "editor_file:close_other", QKeySequence ());
199  init (tr ("Print"), "editor_file:print", QKeySequence::Print);
200 
201  // edit
202  init (tr ("Undo"), "editor_edit:undo", QKeySequence::Undo);
203  init (tr ("Redo"), "editor_edit:redo", QKeySequence::Redo);
204  init (tr ("Copy"), "editor_edit:copy", QKeySequence::Copy);
205  init (tr ("Cut"), "editor_edit:cut", QKeySequence::Cut);
206  init (tr ("Paste"), "editor_edit:paste", QKeySequence::Paste);
207  init (tr ("Select All"), "editor_edit:select_all", QKeySequence::SelectAll);
208  init (tr ("Find and Replace"), "editor_edit:find_replace",
209  QKeySequence::Find);
210  init (tr ("Delete to Start of Word"), "editor_edit:delete_start_word",
211  QKeySequence::DeleteStartOfWord);
212  init (tr ("Delete to End of Word"), "editor_edit:delete_end_word",
213  QKeySequence::DeleteEndOfWord);
214  init (tr ("Delete to Start of Line"), "editor_edit:delete_start_line",
215  QKeySequence (ctrl_shift + Qt::Key_Backspace));
216  init (tr ("Delete to End of Line"), "editor_edit:delete_end_line",
217  QKeySequence (ctrl_shift + Qt::Key_Delete));
218  init (tr ("Delete Line"), "editor_edit:delete_line",
219  QKeySequence (ctrl_shift + Qt::Key_L));
220  init (tr ("Copy Line"), "editor_edit:copy_line",
221  QKeySequence (ctrl_shift + Qt::Key_C));
222  init (tr ("Cut Line"), "editor_edit:cut_line",
223  QKeySequence (ctrl_shift + Qt::Key_X));
224  init (tr ("Duplicate Selection/Line"), "editor_edit:duplicate_selection",
225  QKeySequence (ctrl + Qt::Key_D));
226  init (tr ("Transpose Line"), "editor_edit:transpose_line",
227  QKeySequence (ctrl + Qt::Key_T));
228  init (tr ("Show Completion List"), "editor_edit:completion_list",
229  QKeySequence (ctrl + Qt::Key_Space));
230 
231  init (tr ("Comment Selection"), "editor_edit:comment_selection",
232  QKeySequence (ctrl + Qt::Key_R));
233  init (tr ("Uncomment Selection"), "editor_edit:uncomment_selection",
234  QKeySequence (ctrl_shift + Qt::Key_R));
235  init (tr ("Uppercase Selection"), "editor_edit:upper_case",
236  QKeySequence (ctrl + Qt::Key_U));
237  init (tr ("Lowercase Selection"), "editor_edit:lower_case",
238  QKeySequence (ctrl_alt + Qt::Key_U));
239 
240 #if defined (Q_OS_MAC)
241  init (tr ("Indent Selection"), "editor_edit:indent_selection",
242  QKeySequence (prefix + Qt::Key_Tab));
243  init (tr ("Unindent Selection"), "editor_edit:unindent_selection",
244  QKeySequence (prefix + Qt::ShiftModifier + Qt::Key_Tab));
245 #else
246  init (tr ("Indent Selection"), "editor_edit:indent_selection",
247  QKeySequence (ctrl + Qt::Key_Tab));
248  init (tr ("Unindent Selection"), "editor_edit:unindent_selection",
249  QKeySequence (ctrl_shift + Qt::Key_Tab));
250 #endif
251 
252  init (tr ("Convert Line Endings to Windows"), "editor_edit:conv_eol_winows",
253  QKeySequence ());
254  init (tr ("Convert Line Endings to Unix"), "editor_edit:conv_eol_unix",
255  QKeySequence ());
256  init (tr ("Convert Line Endings to Mac"), "editor_edit:conv_eol_mac",
257  QKeySequence ());
258 
259  init (tr ("Goto Line"), "editor_edit:goto_line",
260  QKeySequence (ctrl + Qt::Key_G));
261  init (tr ("Move to Matching Brace"), "editor_edit:move_to_brace",
262  QKeySequence (ctrl + Qt::Key_M));
263  init (tr ("Select to Matching Brace"), "editor_edit:select_to_brace",
264  QKeySequence (ctrl_shift + Qt::Key_M));
265  init (tr ("Toggle Bookmark"), "editor_edit:toggle_bookmark",
266  QKeySequence (prefix + Qt::Key_F7));
267  init (tr ("Next Bookmark"), "editor_edit:next_bookmark",
268  QKeySequence (prefix + Qt::Key_F2));
269  init (tr ("Previous Bookmark"), "editor_edit:previous_bookmark",
270  QKeySequence (prefix + Qt::SHIFT + Qt::Key_F2));
271  init (tr ("Remove All Bookmark"), "editor_edit:remove_bookmark",
272  QKeySequence ());
273 
274  init (tr ("Preferences"), "editor_edit:preferences", QKeySequence ());
275  init (tr ("Styles Preferences"), "editor_edit:styles_preferences",
276  QKeySequence ());
277 
278  // view
279  init (tr ("Show Line Numbers"), "editor_view:show_line_numbers",
280  QKeySequence ());
281  init (tr ("Show Whitespace Characters"), "editor_view:show_white_spaces",
282  QKeySequence ());
283  init (tr ("Show Line Endings"), "editor_view:show_eol_chars", QKeySequence ());
284  init (tr ("Show Indentation Guides"), "editor_view:show_ind_guides",
285  QKeySequence ());
286  init (tr ("Show Long Line Marker"), "editor_view:show_long_line",
287  QKeySequence ());
288  init (tr ("Zoom In"), "editor_view:zoom_in", QKeySequence::ZoomIn);
289  init (tr ("Zoom Out"), "editor_view:zoom_out", QKeySequence::ZoomOut);
290 #if defined (Q_OS_MAC)
291  init (tr ("Zoom Normal"), "editor_view:zoom_normal",
292  QKeySequence (ctrl + Qt::Key_Underscore));
293 #else
294  init (tr ("Zoom Normal"), "editor_view:zoom_normal",
295  QKeySequence (ctrl + Qt::Key_Period));
296 #endif
297 
298  // debug
299  init (tr ("Toggle Breakpoint"), "editor_debug:toggle_breakpoint",
300  QKeySequence ());
301  init (tr ("Next Breakpoint"), "editor_debug:next_breakpoint",
302  QKeySequence ());
303  init (tr ("Previous Breakpoint"), "editor_debug:previous_breakpoint",
304  QKeySequence ());
305  init (tr ("Remove All Breakpoints"), "editor_debug:remove_breakpoints",
306  QKeySequence ());
307 
308  // run
309  init (tr ("Run File"), "editor_run:run_file",
310  QKeySequence (prefix + Qt::Key_F5) );
311  init (tr ("Run Selection"), "editor_run:run_selection",
312  QKeySequence (prefix + Qt::Key_F9) );
313 
314  // help
315  init (tr ("Help on Keyword"), "editor_help:help_keyword",
316  QKeySequence::HelpContents);
317  init (tr ("Document on Keyword"), "editor_help:doc_keyword",
318  QKeySequence (Qt::SHIFT + Qt::Key_F1));
319 }
320 
321 void
322 shortcut_manager::init (QString description, QString key, QKeySequence def_sc)
323 {
324  QKeySequence actual_0
325  = QKeySequence (_settings->value ("shortcuts/"+key, def_sc).toString ());
326  QKeySequence actual_1
327  = QKeySequence (_settings->value ("shortcuts/"+key+"_1", def_sc).toString ());
328 
329  // append the new shortcut to the list
330  shortcut_t shortcut_info;
331  shortcut_info.description = description;
332  shortcut_info.settings_key = key;
333  shortcut_info.actual_sc [0] = actual_0;
334  shortcut_info.actual_sc [1] = actual_1;
335  shortcut_info.default_sc [0] = def_sc;
336  shortcut_info.default_sc [1] = def_sc; // TODO: Different defaults
337  _sc << shortcut_info;
338 
339  // insert shortcut prepended by widget in order check for duplicates later
340  QString widget = key.section ('_',0,0); // get widget that uses the shortcut
341  if (! actual_0.isEmpty ())
342  _shortcut_hash[widget + ":" + actual_0.toString ()] =
343  _sc.count (); // offset of 1 to avoid 0
344  if (! actual_1.isEmpty ())
345  _shortcut_hash[widget + "_1:" + actual_1.toString ()] =
346  _sc.count (); // offset of 1 to avoid 0
347  _action_hash[key] = _sc.count (); // offset of 1 to avoid 0
348 }
349 
350 void
351 shortcut_manager::do_fill_treewidget (QTreeWidget *tree_view)
352 {
353  _dialog = 0;
354  _level_hash.clear ();
355 
356  tree_view->header ()->setResizeMode (QHeaderView::ResizeToContents);
357 
358  QTreeWidgetItem *main = new QTreeWidgetItem (tree_view);
359  main->setText (0, tr ("Main"));
360  main->setExpanded (true);
361  QTreeWidgetItem *main_file = new QTreeWidgetItem (main);
362  main_file->setText (0, tr ("File"));
363  QTreeWidgetItem *main_edit = new QTreeWidgetItem (main);
364  main_edit->setText (0, tr ("Edit"));
365  QTreeWidgetItem *main_debug = new QTreeWidgetItem (main);
366  main_debug->setText (0, tr ("Debug"));
367  QTreeWidgetItem *main_window = new QTreeWidgetItem (main);
368  main_window->setText (0, tr ("Window"));
369  QTreeWidgetItem *main_help = new QTreeWidgetItem (main);
370  main_help->setText (0, tr ("Help"));
371  QTreeWidgetItem *main_news = new QTreeWidgetItem (main);
372  main_news->setText (0, tr ("News"));
373 
374  _level_hash["main_file"] = main_file;
375  _level_hash["main_edit"] = main_edit;
376  _level_hash["main_debug"] = main_debug;
377  _level_hash["main_window"] = main_window;
378  _level_hash["main_help"] = main_help;
379  _level_hash["main_news"] = main_news;
380 
381  QTreeWidgetItem *editor = new QTreeWidgetItem (tree_view);
382  editor->setText (0, tr ("Editor"));
383  editor->setExpanded (true);
384  QTreeWidgetItem *editor_file = new QTreeWidgetItem (editor);
385  editor_file->setText (0, tr ("File"));
386  QTreeWidgetItem *editor_edit = new QTreeWidgetItem (editor);
387  editor_edit->setText (0, tr ("Edit"));
388  QTreeWidgetItem *editor_view = new QTreeWidgetItem (editor);
389  editor_view->setText (0, tr ("View"));
390  QTreeWidgetItem *editor_debug = new QTreeWidgetItem (editor);
391  editor_debug->setText (0, tr ("Debug"));
392  QTreeWidgetItem *editor_run = new QTreeWidgetItem (editor);
393  editor_run->setText (0, tr ("Run"));
394  QTreeWidgetItem *editor_help = new QTreeWidgetItem (editor);
395  editor_help->setText (0, tr ("Help"));
396 
397  _level_hash["editor_file"] = editor_file;
398  _level_hash["editor_edit"] = editor_edit;
399  _level_hash["editor_view"] = editor_view;
400  _level_hash["editor_debug"] = editor_debug;
401  _level_hash["editor_run"] = editor_run;
402  _level_hash["editor_help"] = editor_help;
403 
404  connect (tree_view, SIGNAL (itemDoubleClicked (QTreeWidgetItem*, int)),
405  this, SLOT (handle_double_clicked (QTreeWidgetItem*, int)));
406 
407  for (int i = 0; i < _sc.count (); i++)
408  {
409  shortcut_t sc = _sc.at (i);
410 
411  QTreeWidgetItem* section = _level_hash[sc.settings_key.section(':',0,0)];
412  QTreeWidgetItem* tree_item = new QTreeWidgetItem (section);
413 
414  // set a slightly transparent foreground for default columns
415  QColor fg = QColor (tree_item->foreground (1).color ());
416  fg.setAlpha (128);
417  tree_item->setForeground (1, QBrush (fg));
418  tree_item->setForeground (3, QBrush (fg));
419 
420  // write the shortcuts
421  tree_item->setText (0, sc.description);
422  tree_item->setText (1, sc.default_sc [0]);
423  tree_item->setText (2, sc.actual_sc [0]);
424  tree_item->setText (3, sc.default_sc [1]);
425  tree_item->setText (4, sc.actual_sc [1]);
426 
427  _item_index_hash[tree_item] = i + 1; // index+1 to avoid 0
428  _index_item_hash[i] = tree_item;
429  }
430 
431 }
432 
433 // write one or all actual shortcut set(s) into a settings file
434 void
435 shortcut_manager::do_write_shortcuts (int set, QSettings* settings,
436  bool closing)
437 {
438  if (set)
439  {
440  // set is not zero, only write the desired set (index = set-1)
441  // into the settings file that the user has selected for this export
442  for (int i = 0; i < _sc.count (); i++) // loop over all shortcuts
443  {
444  settings->setValue("shortcuts/"+_sc.at (i).settings_key,
445  _sc.at (i).actual_sc[set-1].toString ());
446  }
447  }
448  else
449  {
450  // set is zero, write all sets into the normal octave settings file
451  // (this is only the case when called from the closing settings dialog)
452  for (int i = 0; i < _sc.count (); i++) // loop over all shortcuts
453  {
454  settings->setValue("shortcuts/"+_sc.at (i).settings_key,
455  _sc.at (i).actual_sc[0].toString ());
456  settings->setValue("shortcuts/"+_sc.at (i).settings_key+"_1",
457  _sc.at (i).actual_sc[1].toString ());
458  }
459 
460  if (closing)
461  {
462  delete _dialog; // the dialog for key sequences can be removed now
463  _dialog = 0; // make sure it is zero again
464  }
465  }
466 
467  settings->sync (); // sync the settings file
468 }
469 
470 void
471 shortcut_manager::do_set_shortcut (QAction* action, const QString& key)
472 {
473  int set = _settings->value ("shortcuts/set",0).toInt ();
474  int index;
475 
476  index = _action_hash[key] - 1;
477 
478  QString key_set = key;
479  if (set == 1)
480  key_set = key+"_1";
481 
482  if (index > -1 && index < _sc.count ())
483  action->setShortcut (QKeySequence (
484  _settings->value ("shortcuts/" + key_set, _sc.at (index).default_sc[set]).toString ()));
485  else
486  qDebug () << "Key: " << key_set << " not found in _action_hash";
487 }
488 
489 void
490 shortcut_manager::handle_double_clicked (QTreeWidgetItem* item, int col)
491 {
492  switch (col)
493  {
494  case 2:
495  case 4:
496  _selected_set = col/2 - 1;
497  break;
498 
499  default:
500  return;
501  }
502 
503  int i = _item_index_hash[item];
504  if (i == 0)
505  return; // top-level-item clicked
506 
507  shortcut_dialog (i-1); // correct to index starting at 0
508 }
509 
510 void
512 {
513  if (! _dialog)
514  {
515  _dialog = new QDialog (this);
516 
517  _dialog->setWindowTitle (tr ("Enter new Shortcut for Set %1")
518  .arg (_selected_set + 1));
519 
520  QVBoxLayout *box = new QVBoxLayout(_dialog);
521 
522  QLabel *help = new QLabel (tr ("Apply the desired shortcut or click "
523  "on the right button to reset the "
524  "shortcut to its default."));
525  help->setWordWrap (true);
526  box->addWidget (help);
527 
528  QCheckBox *direct = new QCheckBox (
529  tr ("Enter shortcut directly by performing it"));
530  direct->setCheckState (Qt::Checked);
531  box->addWidget (direct);
532 
533  QGridLayout *grid = new QGridLayout();
534 
535  QLabel *actual = new QLabel (tr ("Actual shortcut"));
537  _edit_actual->setAlignment (Qt::AlignHCenter);
538  grid->addWidget (actual, 0, 0);
539  grid->addWidget (_edit_actual, 0, 1);
540 
541  QLabel *def = new QLabel (tr ("Default shortcut"));
542  _label_default = new QLabel (_dialog);
543  _label_default->setAlignment (Qt::AlignHCenter);
544  grid->addWidget (def, 1, 0);
545  grid->addWidget (_label_default, 1, 1);
546 
547  QPushButton *set_default = new QPushButton (tr ("Set to default"));
548  grid->addWidget (set_default, 0, 2);
549  connect (set_default, SIGNAL (clicked ()),
550  this, SLOT (shortcut_dialog_set_default ()));
551 
552  box->addLayout (grid);
553 
554  QDialogButtonBox *button_box = new QDialogButtonBox (QDialogButtonBox::Ok
555  | QDialogButtonBox::Cancel);
556  QList<QAbstractButton *> buttons = button_box->buttons ();
557  for (int i = 0; i < buttons.count (); i++)
558  buttons.at (i)->setShortcut (QKeySequence ());
559  connect(button_box, SIGNAL (accepted ()), _dialog, SLOT (accept ()));
560  connect(button_box, SIGNAL (rejected ()), _dialog, SLOT (reject ()));
561  box->addWidget (button_box);
562 
563  _dialog->setLayout (box);
564 
565  connect (direct, SIGNAL (stateChanged (int)),
566  _edit_actual, SLOT (handle_direct_shortcut (int)));
567  connect (_dialog, SIGNAL (finished (int)),
568  this, SLOT (shortcut_dialog_finished (int)));
569 
570  }
571 
572  _edit_actual->setText (_sc.at (index).actual_sc[_selected_set]);
573  _label_default->setText (_sc.at (index).default_sc[_selected_set]);
574  _handled_index = index;
575 
576  _edit_actual->setFocus ();
577  _dialog->setFocusProxy (_edit_actual);
578  _dialog->exec ();
579 }
580 
581 void
583 {
584  if (result == QDialog::Rejected)
585  return;
586 
587  // check for duplicate
588 
589  // get the widget for which this shortcut is defined
590  QString widget = _sc.at (_handled_index).settings_key.section ('_',0,0);
591  // and look for shortcut
592  QString sep = ":";
593  if (_selected_set)
594  sep = "_1:";
595 
596  int double_index = _shortcut_hash[widget + sep + _edit_actual->text()] - 1;
597 
598  if (double_index >= 0 && double_index != _handled_index)
599  {
600  int ret = QMessageBox::warning(this, tr("Double Shortcut"),
601  tr ("The chosen shortcut\n \"%1\"\n"
602  "is already used for the action\n \"%2\".\n"
603  "Do you want to use the shortcut anyhow removing it "
604  "from the previous action?")
605  .arg (_edit_actual->text())
606  .arg (_sc.at (double_index).description),
607  QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
608 
609  if (ret == QMessageBox::Yes)
610  {
611  shortcut_t double_shortcut = _sc.at (double_index);
612  double_shortcut.actual_sc[_selected_set] = QKeySequence ();
613  _sc.replace (double_index, double_shortcut);
614  _index_item_hash[double_index]->setText ((_selected_set + 1)*2, QKeySequence ());
615  }
616  else
617  return;
618  }
619 
620  shortcut_t shortcut = _sc.at (_handled_index);
621  if (! shortcut.actual_sc[_selected_set].isEmpty ())
622  _shortcut_hash.remove (widget + sep +
623  shortcut.actual_sc[_selected_set].toString ());
624  shortcut.actual_sc[_selected_set] = _edit_actual->text();
625  _sc.replace (_handled_index, shortcut);
626 
627  _index_item_hash[_handled_index]->setText ((_selected_set + 1)*2,
628  shortcut.actual_sc[_selected_set]);
629 
630  if (! shortcut.actual_sc[_selected_set].isEmpty ())
631  _shortcut_hash[widget + sep + shortcut.actual_sc[_selected_set].toString ()] =
632  _handled_index + 1;
633 }
634 
635 void
637 {
638  _edit_actual->setText (_label_default->text ());
639 }
640 
641 // import a shortcut set from a given settings file and refresh the tree view
642 void
643 shortcut_manager::import_shortcuts (int set, QSettings *settings)
644 {
645  for (int i = 0; i < _sc.count (); i++)
646  {
647  // update the list of all shortcuts
648  shortcut_t sc = _sc.at (i); // make a copy
649  sc.actual_sc[set-1] = QKeySequence ( // get new shortcut from settings
650  settings->value ("shortcuts/"+sc.settings_key,sc.actual_sc[set-1]).
651  toString ()); // and use the old one as default
652  _sc.replace (i,sc); // replace the old with the new one
653 
654  // update the tree view
655  QTreeWidgetItem* tree_item = _index_item_hash[i]; // get related tree item
656  tree_item->setText (2*set, sc.actual_sc [set-1]); // display new shortcut
657  }
658 }
659 
660 // import or export of shortcut sets,
661 // called from settings dialog when related buttons are clicked
662 void
664 {
665  QString file;
666 
667  // get the file name to read or write the shortcuts,
668  // the default extension is .osc (octave shortcuts)
669  if (import)
670  {
671  file = QFileDialog::getOpenFileName (this,
672  tr ("Import shortcut set %1 from file ...").arg (set), QString (),
673  tr ("Octave Shortcut Files (*.osc);;All Files (*)"));
674  }
675  else
676  {
677  file = QFileDialog::getSaveFileName (this,
678  tr ("Export shortcut set %1 into file ...").arg (set), QString (),
679  tr ("Octave Shortcut Files (*.osc);;All Files (*)"));
680  }
681 
682  // create a settings object related to this file
683  QSettings *osc_settings = new QSettings (file, QSettings::IniFormat);
684  if (osc_settings)
685  {
686  // the settings object was successfully created: carry on
687  if (import)
688  import_shortcuts (set, osc_settings); // import (special action)
689  else
690  do_write_shortcuts (set, osc_settings, false); // export, (saving settings)
691  }
692  else
693  qWarning () << tr ("Failed to open %1 as octave shortcut file"). arg (file);
694 
695 }
696 
697 
698 // enter_shortcut:
699 // class derived from QLineEdit for directly entering key sequences which
701 {
702  _direct_shortcut = true; // the shortcut is directly entered
703 }
704 
706 {
707 }
708 
709 // slot for checkbox whether the shortcut is directly entered or not
710 void
712 {
713  if (state)
714  _direct_shortcut = true; // the shortcut is directly entered
715  else
716  _direct_shortcut = false; // the shortcut has to be written as text
717 }
718 
719 // new keyPressEvent
720 void
722 {
723  if (! _direct_shortcut)
724  {
725  QLineEdit::keyPressEvent (e);
726  return;
727  }
728 
729  if (e->type () == QEvent::KeyPress)
730  {
731  int key = e->key ();
732 
733  if (key == Qt::Key_unknown || key == 0)
734  return;
735 
736  Qt::KeyboardModifiers modifiers = e->modifiers ();
737 
738  if (modifiers & Qt::ShiftModifier)
739  key += Qt::SHIFT;
740  if (modifiers & Qt::ControlModifier)
741  key += Qt::CTRL;
742  if (modifiers & Qt::AltModifier)
743  key += Qt::ALT;
744  if (modifiers & Qt::MetaModifier)
745  key += Qt::META;
746 
747  setText (QKeySequence(key));
748  }
749 }
QHash< int, QTreeWidgetItem * > _index_item_hash
int main(int argc, char **argv)
Definition: main-cli.cc:32
QHash< QTreeWidgetItem *, int > _item_index_hash
static uint32_t state[624]
Definition: randmtzig.c:188
void do_import_export(bool import, int set)
void init(QString, QString, QKeySequence)
enter_shortcut * _edit_actual
OCTINTERP_API octave_value box(JNIEnv *jni_env, jobject jobj, jclass jcls=0)
QSettings * _settings
#define CTRL(x)
Definition: kpty.cpp:143
void shortcut_dialog_set_default()
void error(const char *fmt,...)
Definition: error.cc:476
static shortcut_manager * instance
void handle_double_clicked(QTreeWidgetItem *, int)
QHash< QString, int > _action_hash
void do_write_shortcuts(int set, QSettings *settings, bool closing)
void handle_direct_shortcut(int)
static void cleanup_instance(void)
static void add(fptr f)
enter_shortcut(QWidget *p=0)
virtual void keyPressEvent(QKeyEvent *e)
static QSettings * get_settings(void)
double arg(double x)
Definition: lo-mappers.h:37
void warning(const char *fmt,...)
Definition: error.cc:681
QHash< QString, QTreeWidgetItem * > _level_hash
void do_fill_treewidget(QTreeWidget *tree_view)
QHash< QString, int > _shortcut_hash
static bool instance_ok(void)
void do_set_shortcut(QAction *action, const QString &key)
QList< shortcut_t > _sc
void import_shortcuts(int set, QSettings *settings)
void shortcut_dialog_finished(int)