GNU Octave  3.8.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 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 <ttl@justmail.de>
24 
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28 
29 #ifdef HAVE_QSCINTILLA
30 
31 #include <Qsci/qscilexer.h>
32 
33 #include "octave-qscintilla.h"
34 #include "file-editor-tab.h"
35 
37  : QsciScintilla (p)
38 { }
39 
41 { }
42 
43 void
45  QPoint *local_pos)
46 {
47  long position = SendScintilla (QsciScintillaBase::SCI_GETCURRENTPOS);
48  long point_x = SendScintilla
49  (QsciScintillaBase::SCI_POINTXFROMPOSITION,0,position);
50  long point_y = SendScintilla
51  (QsciScintillaBase::SCI_POINTYFROMPOSITION,0,position);
52  *local_pos = QPoint (point_x,point_y); // local cursor position
53  *global_pos = mapToGlobal (*local_pos); // global position of cursor
54 }
55 
56 // determine the actual word and whether we are in an octave or matlab script
57 bool
59 {
60  QPoint global_pos, local_pos;
61  get_global_textcursor_pos (&global_pos, &local_pos);
62  _word_at_cursor = wordAtPoint (local_pos);
63  QString lexer_name = lexer ()->lexer ();
64  return ((lexer_name == "octave" || lexer_name == "matlab")
65  && !_word_at_cursor.isEmpty ());
66 }
67 
68 // call documentation or help on the current word
69 void
70 octave_qscintilla::context_help_doc (bool documentation)
71 {
72  if (get_actual_word ())
73  contextmenu_help_doc (documentation);
74 }
75 
76 // call edit the function related to the current word
77 void
79 {
80  if (get_actual_word ())
81  contextmenu_edit (true);
82 }
83 
84 // call edit the function related to the current word
85 void
87 {
88  if (hasSelectedText ())
89  contextmenu_run (true);
90 }
91 
92 #ifdef HAVE_QSCI_VERSION_2_6_0
93 // context menu requested
94 void
95 octave_qscintilla::contextMenuEvent (QContextMenuEvent *e)
96 {
97  QMenu *context_menu = createStandardContextMenu ( ); // standard menu
98 
99  // the menu's position
100  QPoint global_pos, local_pos;
101 
102  if (e->reason () == QContextMenuEvent::Mouse)
103  {
104  // context menu by mouse
105  global_pos = e->globalPos (); // global mouse position
106  local_pos = e->pos (); // local mouse position
107  }
108  else
109  {
110  // context menu by keyboard or other: get point of text cursor
111  get_global_textcursor_pos (&global_pos, &local_pos);
112  QRect editor_rect = geometry (); // editor rect mapped to global
113  editor_rect.moveTopLeft
114  (parentWidget ()->mapToGlobal (editor_rect.topLeft ()));
115  if (!editor_rect.contains (global_pos)) // is cursor outside editor?
116  global_pos = editor_rect.topLeft (); // yes, take top left corner
117  }
118 
119  // additional custom entries of the context menu
120  context_menu->addSeparator (); // separator before custom entries
121 
122  // help menu: get the position of the mouse or the text cursor
123  // (only for octave files)
124  QString lexer_name = lexer ()->lexer ();
125  if (lexer_name == "octave" || lexer_name == "matlab")
126  {
127  _word_at_cursor = wordAtPoint (local_pos);
128  if (!_word_at_cursor.isEmpty ())
129  {
130  context_menu->addAction (tr ("Help on") + " " + _word_at_cursor,
131  this, SLOT (contextmenu_help (bool)));
132  context_menu->addAction (tr ("Documentation on")
133  + " " + _word_at_cursor,
134  this, SLOT (contextmenu_doc (bool)));
135  context_menu->addAction (tr ("Edit") + " " + _word_at_cursor,
136  this, SLOT (contextmenu_edit (bool)));
137  }
138  context_menu->addSeparator (); // separator before custom entries
139  if (hasSelectedText ())
140  context_menu->addAction (tr ("&Run Selection"),
141  this, SLOT (contextmenu_run (bool)));
142  }
143 
144  // finaly show the menu
145  context_menu->exec (global_pos);
146 }
147 #endif
148 
149 
150 // handle the menu entry for calling help or doc
151 void
153 {
154  contextmenu_help_doc (true);
155 }
156 void
158 {
159  contextmenu_help_doc (false);
160 }
161 
162 // common function with flag for documentation
163 void
164 octave_qscintilla::contextmenu_help_doc (bool documentation)
165 {
166  QString command;
167  if (documentation)
168  command = "doc ";
169  else
170  command = "help ";
172 }
173 
174 void
176 {
177  emit execute_command_in_terminal_signal (QString("edit ") + _word_at_cursor);
178 }
179 
180 void
182 {
183  QStringList commands = selectedText ().split (QRegExp("[\r\n]"),
184  QString::SkipEmptyParts);
185  for (int i = 0; i < commands.size (); i++ )
186  emit execute_command_in_terminal_signal (commands.at (i));
187 }
188 
189 #endif