changeset 16446:4b3a4bf8569b

improve encapsulation of command window object * terminal-dockwidget.h, terminal-dockwidget.cc (terminal_dock_widget::terminal): New data member. (terminal_dock_widget::terminal_dock_widget): Delete QTerminal argument. Initialize QTerminal object here. Connect terminal_dock_widget::notice_settings_signal to QTerminal::notice_settings. Connect terminal_dock_widget::relay_command_signal to Qterminal::relay_command. Connect terminal_dock_widget::copyClipboard_signal to Qterminal::copyClipboard. Connect terminal_dock_widget::pasteClipboard_signal to Qterminal::pasteClipboard. (terminal_dock_widget::connect_visibility_changed, terminal_dock_widget::focus, terminal_dock_widget::handle_visibility, terminal_dock_widget::notice_settings, terminal_dock_widget::relay_command, terminal_dock_widget::copyClipboard, terminal_dock_widget::pasteClipboard): New functions. terminal_dock_widget::notice_settings_signal, terminal_dock_widget::relay_command_signal, terminal_dock_widget::copyClipboard_signal, terminal_dock_widget::pasteClipboard_signal): New signals. * main-window.h, main-window.cc (main_window::command_window): Rename from main_window::_terminal_dock_widget. Don't use a pointer. Change all uses. (main_window::main_window): Initialize it here. (main_window::~main_window): Don't delete _terminal_dock_widget. (main_window::relay_command_signal main_window::focus_command_window_signal): New signals. (main_window::handle_command_double_clicked): Emit relay_command_signal, and focus_command_window_signal signals instead of calling focus_command_window. (main_window::focus_command_window): Emit focus_command_window_signal singal instead of performing actions on _terminal_dock_widget object directly. (main_window::handle_command_window_visible): Delete. (main_window::connect_visibility_changed): Call command_window.connect_visibility_changed instead of performing actions here. (main_window::construct): Don't create _terminal_dock_widget. Adapt signal/slot connections for new command_window object.
author John W. Eaton <jwe@octave.org>
date Sat, 06 Apr 2013 16:14:32 -0400
parents 3f8d3fc907af
children e3b33a7530bc
files libgui/src/main-window.cc libgui/src/main-window.h libgui/src/terminal-dockwidget.cc libgui/src/terminal-dockwidget.h
diffstat 4 files changed, 151 insertions(+), 62 deletions(-) [+]
line wrap: on
line diff
--- a/libgui/src/main-window.cc	Sat Apr 06 14:23:52 2013 -0400
+++ b/libgui/src/main-window.cc	Sat Apr 06 16:14:32 2013 -0400
@@ -55,7 +55,7 @@
 #include "oct-env.h"
 
 main_window::main_window (QWidget *p)
-  : QMainWindow (p)
+  : QMainWindow (p), command_window (this)
 {
   // We have to set up all our windows, before we finally launch octave.
   construct ();
@@ -78,9 +78,6 @@
     delete _file_editor;
 #endif
 
-  if (_terminal_dock_widget)
-    delete _terminal_dock_widget;
-
   if (_status_bar)
     delete _status_bar;
 
@@ -162,9 +159,10 @@
 }
 
 void
-main_window::handle_command_double_clicked (const QString&)
+main_window::handle_command_double_clicked (const QString& command)
 {
-  focus_command_window ();
+  emit relay_command_signal (command);
+  emit focus_command_window_signal ();
 }
 
 void
@@ -347,20 +345,9 @@
 }
 
 void
-main_window::focus_command_window ()
+main_window::focus_command_window (void)
 {
-  if (!_terminal_dock_widget->isVisible ())
-    {
-      _terminal_dock_widget->setVisible (true);
-    }
-
-  _terminal_dock_widget->setFocus ();
-  _terminal_dock_widget->activateWindow ();
-  _terminal_dock_widget->raise ();
-
-  _terminal_dock_widget->widget ()->setFocus ();
-  _terminal_dock_widget->widget ()->activateWindow ();
-  _terminal_dock_widget->widget ()->raise ();
+  emit focus_command_window_signal ();
 }
 
 void
@@ -426,14 +413,6 @@
 }
 
 void
-main_window::handle_command_window_visible (bool visible)
-{
-  // if widget is changed to visible and is not floating
-  if (visible && !_terminal_dock_widget->isFloating ())
-    focus_command_window ();
-}
-
-void
 main_window::handle_command_history_visible (bool visible)
 {
   // if changed to visible and widget is not floating
@@ -637,8 +616,8 @@
 void
 main_window::connect_visibility_changed ()
 {
-  connect (_terminal_dock_widget, SIGNAL (visibilityChanged (bool)),
-           this,                  SLOT (handle_command_window_visible (bool)));
+  command_window.connect_visibility_changed ();
+
   connect (_workspace_view,       SIGNAL (visibilityChanged (bool)),
            this,                  SLOT (handle_workspace_visible (bool)));
   connect (_history_dock_widget,  SIGNAL (visibilityChanged (bool)),
@@ -697,12 +676,6 @@
   QToolButton *current_directory_up_tool_button = new QToolButton (this);
   current_directory_up_tool_button->setIcon (QIcon(":/actions/icons/up.png"));
 
-  // Octave Terminal subwindow.
-  QTerminal *terminal = new QTerminal (this);
-  terminal->setObjectName ("OctaveTerminal");
-  terminal->setFocusPolicy (Qt::StrongFocus);
-  _terminal_dock_widget = new terminal_dock_widget (terminal, this);
-
   // Create and set the central widget.  QMainWindow takes ownership of
   // the widget (pointer) so there is no need to delete the object upon
   // destroying this main_window.
@@ -1036,8 +1009,8 @@
   connect (about_octave_action,         SIGNAL (triggered ()),
            this,                        SLOT   (show_about_octave ()));
   connect (show_command_window_action,  SIGNAL (toggled (bool)),
-           _terminal_dock_widget,       SLOT   (setVisible (bool)));
-  connect (_terminal_dock_widget,       SIGNAL (active_changed (bool)),
+           &command_window,             SLOT   (setVisible (bool)));
+  connect (&command_window,             SIGNAL (active_changed (bool)),
            show_command_window_action,  SLOT   (setChecked (bool)));
   connect (show_workspace_action,       SIGNAL (toggled (bool)),
            _workspace_view,             SLOT   (setVisible (bool)));
@@ -1063,7 +1036,11 @@
            show_documentation_action,   SLOT   (setChecked (bool)));
 
   connect (command_window_action,       SIGNAL (triggered ()),
-           this,                        SLOT (focus_command_window ()));
+           &command_window,             SLOT (focus ()));
+
+  connect (this, SIGNAL (focus_command_window_signal ()),
+           &command_window, SLOT (focus ()));
+
   connect (workspace_action,            SIGNAL (triggered ()),
            this,                        SLOT (focus_workspace ()));
   connect (history_action,              SIGNAL (triggered ()),
@@ -1084,7 +1061,7 @@
            _file_editor,                SLOT   (notice_settings (const QSettings *)));
 #endif
   connect (this,                        SIGNAL (settings_changed (const QSettings *)),
-           terminal,                    SLOT   (notice_settings (const QSettings *)));
+           &command_window,             SLOT   (notice_settings (const QSettings *)));
   connect (this,                        SIGNAL (settings_changed (const QSettings *)),
            _files_dock_widget,          SLOT   (notice_settings (const QSettings *)));
   connect (this,                        SIGNAL (settings_changed (const QSettings *)),
@@ -1097,8 +1074,8 @@
            this,                        SLOT   (report_status_message (QString)));
   connect (_history_dock_widget,        SIGNAL (command_double_clicked (const QString&)),
            this,                        SLOT   (handle_command_double_clicked (const QString&)));
-  connect (_history_dock_widget,        SIGNAL (command_double_clicked (const QString&)),
-           terminal,                    SLOT   (relay_command (const QString&)));
+  connect (this,                        SIGNAL (relay_command_signal (const QString&)),
+           &command_window,             SLOT   (relay_command (const QString&)));
   connect (save_workspace_action,       SIGNAL (triggered ()),
            this,                        SLOT   (handle_save_workspace_request ()));
   connect (load_workspace_action,       SIGNAL (triggered ()),
@@ -1110,9 +1087,9 @@
   connect (current_directory_up_tool_button, SIGNAL (clicked ()),
            this,                        SLOT   (change_directory_up ()));
   connect (copy_action,                 SIGNAL (triggered()),
-           terminal,                    SLOT   (copyClipboard ()));
+           &command_window,             SLOT   (copyClipboard ()));
   connect (paste_action,                SIGNAL (triggered()),
-           terminal,                    SLOT   (pasteClipboard ()));
+           &command_window,             SLOT   (pasteClipboard ()));
   connect (_current_directory_combo_box, SIGNAL (activated (QString)),
            this,                        SLOT (set_current_working_directory (QString)));
   connect (_current_directory_line_edit, SIGNAL (returnPressed ()),
@@ -1133,12 +1110,12 @@
 
   setWindowTitle ("Octave");
   setDockOptions(QMainWindow::AnimatedDocks | QMainWindow::AllowNestedDocks | QMainWindow::AllowTabbedDocks);
-  addDockWidget (Qt::RightDockWidgetArea, _terminal_dock_widget);
+  addDockWidget (Qt::RightDockWidgetArea, &command_window);
   addDockWidget (Qt::RightDockWidgetArea, _documentation_dock_widget);
-  tabifyDockWidget(_terminal_dock_widget,_documentation_dock_widget);
+  tabifyDockWidget (&command_window, _documentation_dock_widget);
 #ifdef HAVE_QSCINTILLA
   addDockWidget (Qt::RightDockWidgetArea, _file_editor);
-  tabifyDockWidget(_terminal_dock_widget,_file_editor);
+  tabifyDockWidget (&command_window, _file_editor);
 #endif
   addDockWidget (Qt::LeftDockWidgetArea, _files_dock_widget);
   addDockWidget (Qt::LeftDockWidgetArea, _workspace_view);
--- a/libgui/src/main-window.h	Sat Apr 06 14:23:52 2013 -0400
+++ b/libgui/src/main-window.h	Sat Apr 06 16:14:32 2013 -0400
@@ -60,12 +60,18 @@
  */
 class main_window : public QMainWindow
 {
-  Q_OBJECT public:
-  main_window (QWidget * parent = 0);
+  Q_OBJECT
+
+public:
+
+  main_window (QWidget *parent = 0);
+
   ~main_window ();
 
 signals:
   void settings_changed (const QSettings *);
+  void relay_command_signal (const QString&);
+  void focus_command_window_signal (void);
 
 public slots:
   void report_status_message (const QString& statusMessage);
@@ -73,7 +79,6 @@
   void handle_load_workspace_request ();
   void handle_clear_workspace_request ();
   void handle_clear_history_request (void);
-  void handle_command_double_clicked (const QString& command);
   void new_file ();
   void open_file ();
   void open_file (const QString& file_name);
@@ -94,13 +99,14 @@
   void change_directory_up (void);
   void accept_directory_line_edit (void);
 
-  void focus_command_window ();
+  void handle_command_double_clicked (const QString& command);
+
+  void focus_command_window (void);
   void focus_command_history ();
   void focus_current_directory ();
   void focus_workspace ();
   void focus_editor ();
   void focus_documentation ();
-  void handle_command_window_visible (bool);
   void handle_command_history_visible (bool);
   void handle_current_directory_visible (bool);
   void handle_workspace_visible (bool);
@@ -148,6 +154,8 @@
 
   void exit_callback (void);
 
+  terminal_dock_widget command_window;
+
 #ifdef HAVE_QSCINTILLA
   file_editor_interface *   _file_editor;
 #endif
@@ -166,7 +174,6 @@
   workspace_view *          _workspace_view;
   history_dock_widget *     _history_dock_widget;
   files_dock_widget *       _files_dock_widget;
-  terminal_dock_widget *    _terminal_dock_widget;
   documentation_dock_widget*_documentation_dock_widget;
 
   // Toolbars.
--- a/libgui/src/terminal-dockwidget.cc	Sat Apr 06 14:23:52 2013 -0400
+++ b/libgui/src/terminal-dockwidget.cc	Sat Apr 06 16:14:32 2013 -0400
@@ -1,5 +1,6 @@
 /*
 
+Copyright (C) 2013 John W. Eaton
 Copyright (C) 2011-2012 Jacob Dawid
 
 This file is part of Octave.
@@ -26,16 +27,91 @@
 
 #include "terminal-dockwidget.h"
 
-terminal_dock_widget::terminal_dock_widget (QTerminal *terminal, QWidget *p)
-  : octave_dock_widget (p)
+terminal_dock_widget::terminal_dock_widget (QWidget *p)
+  : octave_dock_widget (p), terminal (p)
 {
+  terminal.setObjectName ("OctaveTerminal");
+  terminal.setFocusPolicy (Qt::StrongFocus);
+
   setObjectName ("TerminalDockWidget");
   setWindowIcon (QIcon(":/actions/icons/logo.png"));
   setWindowTitle (tr ("Command Window"));
-  setWidget (terminal);
+  setWidget (&terminal);
+
+  connect (this, SIGNAL (visibilityChanged (bool)),
+           this, SLOT (handle_visibility (bool)));
+
+  // topLevelChanged is emitted when floating property changes (floating
+  // = true)
+  connect (this, SIGNAL (topLevelChanged (bool)),
+           this, SLOT (top_level_changed (bool)));
+
+  // Forward signals to QTerminal widget.
+
+  connect (this, SIGNAL (notice_settings_signal (const QSettings *)),
+           &terminal, SLOT (notice_settings (const QSettings *)));
 
-  connect (this, SIGNAL (visibilityChanged (bool)), this, SLOT (handle_visibility_changed (bool)));
-  // topLevelChanged is emitted when floating property changes (floating = true)
-  connect (this, SIGNAL (topLevelChanged(bool)), this, SLOT(top_level_changed(bool)));
+  connect (this, SIGNAL (relay_command_signal (const QString&)),
+           &terminal, SLOT (relay_command (const QString&)));
+
+  connect (this, SIGNAL (copyClipboard_signal (void)),
+           &terminal, SLOT (copyClipboard (void)));
+
+  connect (this, SIGNAL (pasteClipboard_signal (void)),
+           &terminal, SLOT (pasteClipboard (void)));
+}
+
+void
+terminal_dock_widget::connect_visibility_changed (void)
+{
+  connect (this, SIGNAL (visibilityChanged (bool)),
+           this, SLOT (handle_visibility_changed (bool)));
 }
 
+void
+terminal_dock_widget::focus (void)
+{
+  if (! isVisible ())
+    setVisible (true);
+
+  setFocus ();
+  activateWindow ();
+  raise ();
+
+  widget ()->setFocus ();
+  widget ()->activateWindow ();
+  widget ()->raise ();
+}
+
+void
+terminal_dock_widget::handle_visibility (bool visible)
+{
+  // if widget is changed to visible and is not floating
+
+  if (visible && ! isFloating ())
+    focus ();
+}
+
+void
+terminal_dock_widget::notice_settings (const QSettings *settings)
+{
+  emit notice_settings_signal (settings);
+}
+
+void
+terminal_dock_widget::relay_command (const QString& command)
+{
+  emit relay_command_signal (command);
+}
+
+void
+terminal_dock_widget::copyClipboard (void)
+{
+  emit copyClipboard_signal ();
+}
+
+void
+terminal_dock_widget::pasteClipboard (void)
+{
+  emit pasteClipboard_signal ();
+}
--- a/libgui/src/terminal-dockwidget.h	Sat Apr 06 14:23:52 2013 -0400
+++ b/libgui/src/terminal-dockwidget.h	Sat Apr 06 16:14:32 2013 -0400
@@ -1,5 +1,6 @@
 /*
 
+Copyright (C) 2013 John W. Eaton
 Copyright (C) 2011-2012 Jacob Dawid
 
 This file is part of Octave.
@@ -23,22 +24,50 @@
 #ifndef TERMINALDOCKWIDGET_H
 #define TERMINALDOCKWIDGET_H
 
+#include <QString>
+
 #include "QTerminal.h"
 #include "octave-dock-widget.h"
 
 class terminal_dock_widget : public octave_dock_widget
 {
   Q_OBJECT
-  public:
-  terminal_dock_widget (QTerminal *terminal, QWidget *parent = 0);
+
+public:
+
+  terminal_dock_widget (QWidget *parent = 0);
+
+  void connect_visibility_changed (void);
 
 signals:
 
 public slots:
 
-protected:
+  void focus (void);
+
+  void handle_visibility (bool visible);
+
+  void notice_settings (const QSettings *settings);
+
+  void relay_command (const QString& command);
+
+  void copyClipboard (void);
+
+  void pasteClipboard (void);
 
+signals:
+
+  void notice_settings_signal (const QSettings *settings); 
+
+  void relay_command_signal (const QString& command);
+
+  void copyClipboard_signal (void);
+
+  void pasteClipboard_signal (void);
+
+private:
+
+  QTerminal terminal;
 };
 
-
 #endif // TERMINALDOCKWIDGET_H