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
octave-qscintilla.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2013-2015 Torsten
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 // Author: Torsten <[email protected]>
24 
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28 
29 #ifdef HAVE_QSCINTILLA
30 
31 #include <Qsci/qscilexer.h>
32 #include <Qsci/qscicommandset.h>
33 #include <QShortcut>
34 #include <QMessageBox>
35 
36 #include "octave-qscintilla.h"
37 #include "file-editor-tab.h"
38 #include "shortcut-manager.h"
39 
41  : QsciScintilla (p)
42 {
43  connect (this, SIGNAL (textChanged ()), this, SLOT (text_changed ()));
44 
45  // clear scintilla edit shortcuts that are handled by the editor
46  QsciCommandSet *cmd_set = standardCommands ();
47 
48 #ifdef HAVE_QSCI_VERSION_2_6_0
49  // find () was added in QScintilla 2.6
50  cmd_set->find (QsciCommand::SelectionCopy)->setKey (0);
51  cmd_set->find (QsciCommand::SelectionCut)->setKey (0);
52  cmd_set->find (QsciCommand::Paste)->setKey (0);
53  cmd_set->find (QsciCommand::SelectAll)->setKey (0);
54  cmd_set->find (QsciCommand::SelectionDuplicate)->setKey (0);
55  cmd_set->find (QsciCommand::LineTranspose)->setKey (0);
56  cmd_set->find (QsciCommand::Undo)->setKey (0);
57  cmd_set->find (QsciCommand::Redo)->setKey (0);
58  cmd_set->find (QsciCommand::SelectionUpperCase)->setKey (0);
59  cmd_set->find (QsciCommand::SelectionLowerCase)->setKey (0);
60  cmd_set->find (QsciCommand::ZoomIn)->setKey (0);
61  cmd_set->find (QsciCommand::ZoomOut)->setKey (0);
62  cmd_set->find (QsciCommand::DeleteWordLeft)->setKey (0);
63  cmd_set->find (QsciCommand::DeleteWordRight)->setKey (0);
64  cmd_set->find (QsciCommand::DeleteLineLeft)->setKey (0);
65  cmd_set->find (QsciCommand::DeleteLineRight)->setKey (0);
66  cmd_set->find (QsciCommand::LineDelete)->setKey (0);
67  cmd_set->find (QsciCommand::LineCut)->setKey (0);
68  cmd_set->find (QsciCommand::LineCopy)->setKey (0);
69 #else
70  // find commands via its default key (tricky way without find ())
71  QList< QsciCommand * > cmd_list = cmd_set->commands ();
72  for (int i = 0; i < cmd_list.length (); i++)
73  {
74  int cmd_key = cmd_list.at (i)->key ();
75  switch (cmd_key)
76  {
77  case Qt::Key_C | Qt::CTRL : // SelectionCopy
78  case Qt::Key_X | Qt::CTRL : // SelectionCut
79  case Qt::Key_V | Qt::CTRL : // Paste
80  case Qt::Key_A | Qt::CTRL : // SelectAll
81  case Qt::Key_D | Qt::CTRL : // SelectionDuplicate
82  case Qt::Key_T | Qt::CTRL : // LineTranspose
83  case Qt::Key_Z | Qt::CTRL : // Undo
84  case Qt::Key_Y | Qt::CTRL : // Redo
85  case Qt::Key_Z | Qt::CTRL | Qt::SHIFT : // Redo
86  case Qt::Key_U | Qt::CTRL : // SelectionLowerCase
87  case Qt::Key_U | Qt::CTRL | Qt::SHIFT : // SelectionUpperCase
88  case Qt::Key_Plus | Qt::CTRL : // ZoomIn
89  case Qt::Key_Minus | Qt::CTRL : // ZoomOut
90  case Qt::Key_Backspace | Qt::CTRL | Qt::SHIFT : // DeleteLineLeft
91  case Qt::Key_Delete | Qt::CTRL | Qt::SHIFT : // DeleteLineRight
92  case Qt::Key_K | Qt::META : // DeleteLineRight
93  case Qt::Key_Backspace | Qt::CTRL : // DeleteWordLeft
94  case Qt::Key_Delete | Qt::CTRL : // DeleteWordRight
95  case Qt::Key_L | Qt::CTRL | Qt::SHIFT : // LineDelete
96  case Qt::Key_L | Qt::CTRL : // LineCut
97  case Qt::Key_T | Qt::CTRL | Qt::SHIFT : // LineCopy
98  cmd_list.at (i)->setKey (0);
99  }
100  }
101 #endif
102 
103 #if defined (Q_OS_MAC)
104  // Octave interprets Cmd key as Meta whereas Qscintilla interprets it
105  // as Ctrl. We thus invert Meta/Ctrl in Qscintilla's shortcuts list.
106  QList< QsciCommand * > cmd_list_mac = cmd_set->commands ();
107  for (int i = 0; i < cmd_list_mac.length (); i++)
108  {
109  // Primary key
110  int key = cmd_list_mac.at (i)->key ();
111 
112  if (static_cast<int> (key | Qt::META) == key &&
113  static_cast<int> (key | Qt::CTRL) != key)
114  key = (key ^ Qt::META) | Qt::CTRL;
115  else if (static_cast<int> (key | Qt::CTRL) == key)
116  key = (key ^ Qt::CTRL) | Qt::META;
117 
118  cmd_list_mac.at (i)->setKey (key);
119 
120  // Alternate key
121  key = cmd_list_mac.at (i)->alternateKey ();
122 
123  if (static_cast<int> (key | Qt::META) == key &&
124  static_cast<int> (key | Qt::CTRL) != key)
125  key = (key ^ Qt::META) | Qt::CTRL;
126  else if (static_cast<int> (key | Qt::CTRL) == key)
127  key = (key ^ Qt::CTRL) | Qt::META;
128 
129  cmd_list_mac.at (i)->setAlternateKey (key);
130  }
131 #endif
132 }
133 
135 { }
136 
137 void
139  QPoint *local_pos)
140 {
141  long position = SendScintilla (SCI_GETCURRENTPOS);
142  long point_x = SendScintilla
143  (SCI_POINTXFROMPOSITION,0,position);
144  long point_y = SendScintilla
145  (SCI_POINTYFROMPOSITION,0,position);
146  *local_pos = QPoint (point_x,point_y); // local cursor position
147  *global_pos = mapToGlobal (*local_pos); // global position of cursor
148 }
149 
150 // determine the actual word and whether we are in an octave or matlab script
151 bool
153 {
154  QPoint global_pos, local_pos;
155  get_global_textcursor_pos (&global_pos, &local_pos);
156  _word_at_cursor = wordAtPoint (local_pos);
157  QString lexer_name = lexer ()->lexer ();
158  return ((lexer_name == "octave" || lexer_name == "matlab")
159  && !_word_at_cursor.isEmpty ());
160 }
161 
162 // call documentation or help on the current word
163 void
164 octave_qscintilla::context_help_doc (bool documentation)
165 {
166  if (get_actual_word ())
167  contextmenu_help_doc (documentation);
168 }
169 
170 // call edit the function related to the current word
171 void
173 {
174  if (get_actual_word ())
175  contextmenu_edit (true);
176 }
177 
178 // call edit the function related to the current word
179 void
181 {
182  if (hasSelectedText ())
183  contextmenu_run (true);
184 }
185 
186 #ifdef HAVE_QSCI_VERSION_2_6_0
187 // context menu requested
188 void
189 octave_qscintilla::contextMenuEvent (QContextMenuEvent *e)
190 {
191  QPoint global_pos, local_pos; // the menu's position
192  QMenu *context_menu = createStandardContextMenu (); // standard menu
193 
194  // fill context menu with editor's standard actions
195  emit create_context_menu_signal (context_menu);
196 
197  // determine position depending on mouse or keyboard event
198  if (e->reason () == QContextMenuEvent::Mouse)
199  {
200  // context menu by mouse
201  global_pos = e->globalPos (); // global mouse position
202  local_pos = e->pos (); // local mouse position
203  }
204  else
205  {
206  // context menu by keyboard or other: get point of text cursor
207  get_global_textcursor_pos (&global_pos, &local_pos);
208  QRect editor_rect = geometry (); // editor rect mapped to global
209  editor_rect.moveTopLeft
210  (parentWidget ()->mapToGlobal (editor_rect.topLeft ()));
211  if (!editor_rect.contains (global_pos)) // is cursor outside editor?
212  global_pos = editor_rect.topLeft (); // yes, take top left corner
213  }
214 
215  // additional custom entries of the context menu
216  context_menu->addSeparator (); // separator before custom entries
217 
218  // help menu: get the position of the mouse or the text cursor
219  // (only for octave files)
220  QString lexer_name = lexer ()->lexer ();
221  if (lexer_name == "octave" || lexer_name == "matlab")
222  {
223  _word_at_cursor = wordAtPoint (local_pos);
224  if (!_word_at_cursor.isEmpty ())
225  {
226  context_menu->addAction (tr ("Help on") + " " + _word_at_cursor,
227  this, SLOT (contextmenu_help (bool)));
228  context_menu->addAction (tr ("Documentation on")
229  + " " + _word_at_cursor,
230  this, SLOT (contextmenu_doc (bool)));
231  context_menu->addAction (tr ("Edit") + " " + _word_at_cursor,
232  this, SLOT (contextmenu_edit (bool)));
233  }
234  }
235 
236  // finaly show the menu
237  context_menu->exec (global_pos);
238 }
239 #endif
240 
241 
242 // handle the menu entry for calling help or doc
243 void
245 {
246  contextmenu_help_doc (true);
247 }
248 void
250 {
251  contextmenu_help_doc (false);
252 }
253 
254 // common function with flag for documentation
255 void
256 octave_qscintilla::contextmenu_help_doc (bool documentation)
257 {
258  if (documentation)
260  else
262 }
263 
264 void
266 {
268 }
269 
270 void
272 {
273  QStringList commands = selectedText ().split (QRegExp("[\r\n]"),
274  QString::SkipEmptyParts);
275  for (int i = 0; i < commands.size (); i++)
276  emit execute_command_in_terminal_signal (commands.at (i));
277 }
278 
279 void
281 {
282  emit status_update (isUndoAvailable (), isRedoAvailable ());
283 }
284 
285 #endif
void contextmenu_help_doc(bool)
void context_menu_edit_signal(const QString &)
void contextmenu_run(bool)
#define CTRL(x)
Definition: kpty.cpp:143
void contextmenu_edit(bool)
void status_update(bool, bool)
void show_doc_signal(const QString &)
void context_help_doc(bool)
#define lexer
Definition: oct-parse.cc:170
void contextmenu_help(bool)
void create_context_menu_signal(QMenu *)
octave_qscintilla(QWidget *p)
void execute_command_in_terminal_signal(const QString &)
void contextmenu_doc(bool)
void text_changed(void)
void get_global_textcursor_pos(QPoint *global_pos, QPoint *local_pos)