changeset 16731:01d523d5f796

add help for word at mouse or text cursor to the editors context menu * octave-qscintilla.h: new class derived from qsciscintilla to get acces to the editors context menu * octave-qscxintilla.cc(contextMenuEvent): create standard context menu and add entry for calling help on current word at cursor (contextmenu_help): handler for the new menu entry * libgui/src/module.mk: add new files octave_qscintilla.cc/.h * file-editor-tab.cc(constructor): _edit_area is of class octave_qscintilla, connect the signal of this class for command execution (execute_command_in_terminal): slot for signal connected in contructor * file-editor-tab.h: new slot for executing a command in terminal, _edit_area is of new class octave_qscintilla
author Torsten <ttl@justmail.de>
date Fri, 07 Jun 2013 23:13:48 +0200
parents 4da837c4902f
children 3806afcf974a
files libgui/src/m-editor/file-editor-tab.cc libgui/src/m-editor/file-editor-tab.h libgui/src/m-editor/file-editor.cc libgui/src/m-editor/octave-qscintilla.cc libgui/src/m-editor/octave-qscintilla.h libgui/src/module.mk
diffstat 6 files changed, 168 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/libgui/src/m-editor/file-editor-tab.cc	Thu Jun 06 20:19:48 2013 -0700
+++ b/libgui/src/m-editor/file-editor-tab.cc	Fri Jun 07 23:13:48 2013 +0200
@@ -70,7 +70,14 @@
 
   _file_name = directory;
 
-  _edit_area = new QsciScintilla (this);
+  _edit_area = new octave_qscintilla (this);
+  // Connect signal for command execution to a slot of this tab which in turn
+  // emits a signal connected to the main window.
+  // Direct connection is not possible because tab's parent is null.
+  connect (_edit_area,
+           SIGNAL (execute_command_in_terminal_signal (const QString&)),
+           this,
+           SLOT (execute_command_in_terminal (const QString&)));
 
   // Leave the find dialog box out of memory until requested.
   _find_dialog = 0;
@@ -162,6 +169,12 @@
 }
 
 void
+file_editor_tab::execute_command_in_terminal (const QString& command)
+{
+  emit execute_command_in_terminal_signal (command); // connected to main window
+}
+
+void
 file_editor_tab::set_file_name (const QString& fileName)
 {
   // update tracked file if we really have a file on disk
--- a/libgui/src/m-editor/file-editor-tab.h	Thu Jun 06 20:19:48 2013 -0700
+++ b/libgui/src/m-editor/file-editor-tab.h	Fri Jun 07 23:13:48 2013 +0200
@@ -28,10 +28,10 @@
 #include <QFileSystemWatcher>
 #include <QSettings>
 #include <QFileInfo>
-#include <Qsci/qsciscintilla.h>
 #include <Qsci/qsciapis.h>
 
 #include "find-dialog.h"
+#include "octave-qscintilla.h"
 
 class file_editor;
 
@@ -103,6 +103,8 @@
 
   void file_has_changed (const QString& fileName);
 
+  void execute_command_in_terminal (const QString& command);
+
 signals:
 
   void file_name_changed (const QString& fileName, const QString& toolTip);
@@ -113,6 +115,7 @@
   void editor_check_conflict_save (const QString& saveFileName,
                                    bool remove_on_success);
   void run_file_signal (const QFileInfo& info);
+  void execute_command_in_terminal_signal (const QString&);
 
 protected:
 
@@ -183,7 +186,7 @@
   void remove_all_breakpoints_callback (const bp_info& info);
   void center_current_line ();
 
-  QsciScintilla *_edit_area;
+  octave_qscintilla *_edit_area;
 
   QString _file_name;
   QString _file_name_short;
--- a/libgui/src/m-editor/file-editor.cc	Thu Jun 06 20:19:48 2013 -0700
+++ b/libgui/src/m-editor/file-editor.cc	Fri Jun 07 23:13:48 2013 +0200
@@ -1082,6 +1082,9 @@
   connect (f, SIGNAL (run_file_signal (const QFileInfo&)),
            parent (), SLOT (run_file_in_terminal (const QFileInfo&)));
   
+  connect (f, SIGNAL (execute_command_in_terminal_signal (const QString&)),
+           parent (), SLOT (execute_command_in_terminal (const QString&)));
+
   // Signals from the file_editor non-trivial operations
   connect (this, SIGNAL (fetab_settings_changed (const QSettings *)),
            f, SLOT (notice_settings (const QSettings *)));
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libgui/src/m-editor/octave-qscintilla.cc	Fri Jun 07 23:13:48 2013 +0200
@@ -0,0 +1,87 @@
+/*
+
+Copyright (C) 2013 Torsten <ttl@justmail.de>
+
+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
+<http://www.gnu.org/licenses/>.
+
+*/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#ifdef HAVE_QSCINTILLA
+
+#include "octave-qscintilla.h"
+#include "file-editor-tab.h"
+
+octave_qscintilla::octave_qscintilla (QWidget *p)
+    : QsciScintilla (p)
+{ }
+
+octave_qscintilla::~octave_qscintilla ()
+{ }
+
+
+// context menu requested
+void
+octave_qscintilla::contextMenuEvent (QContextMenuEvent *e)
+{
+  QMenu *context_menu = createStandardContextMenu ( );  // standard menu
+
+  context_menu->addSeparator ();   // separator before custom entries
+
+  // help menu: get the position of the mouse or the text cursor
+  _word_at_cursor = "";
+  QPoint global_pos = e->globalPos ();            // global mouse position
+
+  if (e->reason () == QContextMenuEvent::Mouse)   // context menu by mouse
+    _word_at_cursor = wordAtPoint (e->pos ());
+  else
+    { // context menu by keyboard or other: get point of text cursor
+      long position = SendScintilla (QsciScintillaBase::SCI_GETCURRENTPOS);
+      long point_x  = SendScintilla
+                        (QsciScintillaBase::SCI_POINTXFROMPOSITION,0,position);
+      long point_y  = SendScintilla
+                        (QsciScintillaBase::SCI_POINTYFROMPOSITION,0,position);
+      QPoint local_pos = QPoint (point_x,point_y);  // local position of cursor
+      global_pos = mapToGlobal (local_pos); // global position of cursor
+      QRect editor_rect = geometry ();      // get editor rect and map to global
+      editor_rect.moveTopLeft(parentWidget()->mapToGlobal(editor_rect.topLeft()));
+      if (editor_rect.contains (global_pos))  // is cursor within editor?
+        _word_at_cursor = wordAtPoint (local_pos);
+      else
+        global_pos = editor_rect.topLeft ();
+    }
+  // finally create the menu entry if a word at cursor was found
+  if (!_word_at_cursor.isEmpty ())
+    context_menu->addAction (tr ("help") + " " + _word_at_cursor,
+                             this, SLOT (contextmenu_help (bool)));
+
+  context_menu->exec (global_pos);
+}
+
+
+// handle the menu entry for calling help
+void
+octave_qscintilla::contextmenu_help (bool)
+{
+  QString command = "help " + _word_at_cursor;
+  emit execute_command_in_terminal_signal (command);
+}
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libgui/src/m-editor/octave-qscintilla.h	Fri Jun 07 23:13:48 2013 +0200
@@ -0,0 +1,55 @@
+/*
+
+Copyright (C) 2013 Torsten <ttl@justmail.de>
+
+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
+<http://www.gnu.org/licenses/>.
+
+*/
+
+#if !defined (octave_qscintilla_h)
+#define octave_qscintilla_h 1
+
+#include <Qsci/qsciscintilla.h>
+#include <QMenu>
+#include <QContextMenuEvent>
+
+class octave_qscintilla : public QsciScintilla
+{
+  Q_OBJECT
+
+public:
+
+  octave_qscintilla (QWidget *p);
+  ~octave_qscintilla ();
+
+  virtual void contextMenuEvent (QContextMenuEvent *e);
+
+signals:
+
+  void execute_command_in_terminal_signal (const QString&);
+
+private slots:
+
+  void contextmenu_help (bool);
+
+private:
+
+  QString _word_at_cursor;
+
+};
+
+#endif
--- a/libgui/src/module.mk	Thu Jun 06 20:19:48 2013 -0700
+++ b/libgui/src/module.mk	Fri Jun 07 23:13:48 2013 +0200
@@ -69,7 +69,8 @@
   src/m-editor/moc-file-editor-interface.cc \
   src/m-editor/moc-file-editor-tab.cc \
   src/m-editor/moc-file-editor.cc \
-  src/m-editor/moc-find-dialog.cc
+  src/m-editor/moc-find-dialog.cc \
+  src/m-editor/moc-octave-qscintilla.cc
 endif
 
 octave_gui_MOC += \
@@ -110,6 +111,7 @@
   src/m-editor/file-editor-tab.h \
   src/m-editor/file-editor.h \
   src/m-editor/find-dialog.h \
+  src/m-editor/octave-qscintilla.h \
   src/main-window.h \
   src/octave-gui.h \
   src/octave-main-thread.h \
@@ -134,6 +136,7 @@
   src/m-editor/file-editor-tab.cc \
   src/m-editor/file-editor.cc \
   src/m-editor/find-dialog.cc \
+  src/m-editor/octave-qscintilla.cc \
   src/main-window.cc \
   src/octave-gui.cc \
   src/octave-main-thread.cc \