view libgui/src/workspace-view.cc @ 16468:0f143f68078d

use signal/slot for updating workspace instead of using event listener * main-window.h, main-window.cc: Clean up list of include files. (main_window::prepare_to_exit): Rename from prepare_to_quit. Change all uses. (main_window::update_workspace): Delete. (main_window::construct): Don't connect _octave_qt_event_listener:update_workspace_signal to main_window::update_workspace. (main_window::construct_octave_qt_link): Connect _octave_qt_link::set_workspace_signal to _workspace_model::set_workspace. Connect _octave_qt_link::clear_workspace_signal to _workspace_model::clear_workspace. * symbol-information.h, symbol-information.cc: Delete * libgui/src/module.mk (noinst_HEADERS, src_libgui_src_la_SOURCES): Remove them from the lists. * octave-qt-link.h, octave-qt-link.cc: Don't use symbol_information to store workspace info. (octave_qt_link::do_update_workspace): Delete. (octave_qt_link::do_set_workspace, octave_qt_link::do_clear_workspace): New functions. (octave_qt_link::do_pre_input_event): Don't call do_update_workspace. (octave_qt_link::set_workspace_signal, octave_qt_link::clear_workspace_signal): New signals. * workspace-model.h, workspace-model.cc: Don't use symbol_information to store workspace info. Accept workspace info through a signal/slot combination, not by asking the symbol table. (workspace_model::request_update_workspace, (workspace_model::update_workspace_callback): Delete. (workspace_model::set_workspace, workspace_model::clear_workspace, workspace_model::clear_data, workspace_model::clear_tree, workspace_model::update_tree, workspace_model::append_tree): New functions. * workspace-view.h, workspace-view.cc (workspace_view::model_changed): Don't call update_workspace_callback. The model now signals the view when it has changed. * input.cc (octave_base_reader::octave_gets, get_debug_input): Call octave_link::set_workspace just prior to prompting for input. * workspace-element.h: New file. * libinterp/interpfcn/module.mk (INTERPFCN_INC): Include it in the list. * octave-link.cc: Don't include symtab.h. * octave-link.h (octave_link::update_workspace): Delete. (octave_link::set_workspace, octave_link::do_set_workspace, * octave_link::clear_workspace, octave_link::do_clear_workspace): New functions. * symtab.h, symtab.cc (symbol_table::workspace_info, symbol_table::do_workspace_info): New functions. * ov.h (octave_value::short_disp): New function. * ov-base.h, ov-base.cc (octave_base_value::short_disp): New function. * ov-base-scalar.h, ov-base-scalar.cc (octave_base_scalar<ST>::short_disp): New function. * ov-range.h, ov-range.cc (octave_range::short_disp): New function.
author John W. Eaton <jwe@octave.org>
date Mon, 08 Apr 2013 12:01:24 -0400
parents 8e2a853cdd7d
children 64727ed135cb
line wrap: on
line source

/*

Copyright (C) 2013 John W. Eaton
Copyright (C) 2011-2012 Jacob Dawid

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

#include "workspace-view.h"
#include "resource-manager.h"
#include <QHeaderView>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QPushButton>
#include <QMenu>

workspace_view::workspace_view (QWidget *p)
  : octave_dock_widget (p)
{
  setObjectName ("WorkspaceView");
  setWindowIcon (QIcon(":/actions/icons/logo.png"));
  setWindowTitle (tr ("Workspace"));
  setStatusTip (tr ("View the variables in the active workspace."));

  view = new QTreeView (this);

  view->setHeaderHidden (false);
  view->setAlternatingRowColors (true);
  view->setAnimated (false);
  view->setTextElideMode (Qt::ElideRight);
  view->setWordWrap (false);
  view->setContextMenuPolicy (Qt::CustomContextMenu);

  // Set an empty widget, so we can assign a layout to it.
  setWidget (new QWidget (this));

  // Create a new layout and add widgets to it.
  QVBoxLayout *vbox_layout = new QVBoxLayout ();
  vbox_layout->addWidget (view);
  vbox_layout->setMargin (2);

  // Set the empty widget to have our layout.
  widget ()->setLayout (vbox_layout);

  // Initialize collapse/expand state of the workspace subcategories.

  QSettings *settings = resource_manager::get_settings ();

  // FIXME -- what should happen if settings is 0?

  _explicit_collapse.local
    = settings->value ("workspaceview/local_collapsed", false).toBool ();

  _explicit_collapse.global
    = settings->value ("workspaceview/global_collapsed", false).toBool ();

  _explicit_collapse.persistent
    = settings->value ("workspaceview/persistent_collapsed", false).toBool ();

  // Initialize column order and width of the workspace
  
  view->header ()->restoreState (settings->value ("workspaceview/column_state").toByteArray ());

  // Connect signals and slots.
  connect (view, SIGNAL (collapsed (QModelIndex)),
           this, SLOT (collapse_requested (QModelIndex)));

  connect (view, SIGNAL (expanded (QModelIndex)),
           this, SLOT (expand_requested (QModelIndex)));

  connect (view, SIGNAL (doubleClicked (QModelIndex)),
           this, SLOT (item_double_clicked (QModelIndex)));

  connect (view, SIGNAL (customContextMenuRequested(const QPoint&)),
           this, SLOT(contextmenu_requested (const QPoint&)));

  connect (this, SIGNAL (command_requested (const QString&)),
           p, SLOT (handle_command_double_clicked(const QString&)));
}

workspace_view::~workspace_view (void)
{
  QSettings *settings = resource_manager::get_settings ();

  settings->setValue ("workspaceview/local_collapsed",
                      _explicit_collapse.local);

  settings->setValue ("workspaceview/global_collapsed",
                      _explicit_collapse.global);

  settings->setValue ("workspaceview/persistent_collapsed",
                      _explicit_collapse.persistent);

  settings->setValue("workspaceview/column_state",
                     view->header ()->saveState ());

  settings->sync ();
}

void
workspace_view::model_changed ()
{
  QAbstractItemModel *m = view->model ();

  // This code is very quirky and requires some explanation.
  // Usually, we should not deal with collapsing or expanding ourselves,
  // because the view itself determines (based on the model) whether it
  // is appropriate to collapse or expand items.
  //
  // Now, the logic requires that we update our model item by item, which
  // would make it work correctly, but this is extremely slow and scales
  // very bad (O(n^2)). That's why we throw away our model and rebuild it
  // completely from scratch (O(n)), which is why the view renders all
  // displayed data as invalid.
  //
  // In order to make collapsing/expanding work again, we need to set
  // flags ourselves here.

  QModelIndex local_model_index = m->index (0, 0);
  QModelIndex global_model_index = m->index (1, 0);
  QModelIndex persistent_model_index = m->index (2, 0);

  if (_explicit_collapse.local)
    view->collapse (local_model_index);
  else
    view->expand (local_model_index);

  if (_explicit_collapse.global)
    view->collapse (global_model_index);
  else
    view->expand (global_model_index);

  if (_explicit_collapse.persistent)
    view->collapse (persistent_model_index);
  else
    view->expand (persistent_model_index);
}

void
workspace_view::collapse_requested (QModelIndex index)
{
  // This code is very quirky and requires some explanation.
  // Usually, we should not deal with collapsing or expanding ourselves,
  // because the view itself determines (based on the model) whether it
  // is appropriate to collapse or expand items.
  //
  // Now, the logic requires that we update our model item by item, which
  // would make it work correctly, but this is extremely slow and scales
  // very bad (O(n^2)). That's why we throw away our model and rebuild it
  // completely from scratch (O(n)), which is why the view renders all
  // displayed data as invalid.
  //
  // In order to make collapsing/expanding work again, we need to set
  // flags ourselves here.
  QAbstractItemModel *m = view->model ();

  QMap<int, QVariant> item_data = m->itemData (index);

  if (item_data[0] == "Local")
    _explicit_collapse.local = true;

  if (item_data[0] == "Global")
    _explicit_collapse.global = true;

  if (item_data[0] == "Persistent")
    _explicit_collapse.persistent = true;
}

void
workspace_view::expand_requested (QModelIndex index)
{
  // This code is very quirky and requires some explanation.
  // Usually, we should not deal with collapsing or expanding ourselves,
  // because the view itself determines (based on the model) whether it
  // is appropriate to collapse or expand items.
  //
  // Now, the logic requires that we update our model item by item, which
  // would make it work correctly, but this is extremely slow and scales
  // very bad (O(n^2)). That's why we throw away our model and rebuild it
  // completely from scratch (O(n)), which is why the view renders all
  // displayed data as invalid.
  //
  // In order to make collapsing/expanding work again, we need to do set
  // flags ourselves here.
  QAbstractItemModel *m = view->model ();

  QMap<int, QVariant> item_data = m->itemData (index);

  if (item_data[0] == "Local")
    _explicit_collapse.local = false;

  if (item_data[0] == "Global")
    _explicit_collapse.global = false;

  if (item_data[0] == "Persistent")
    _explicit_collapse.persistent = false;
}

void
workspace_view::item_double_clicked (QModelIndex)
{
  // TODO: Implement opening a dialog that allows the user to change a
  // variable in the workspace.
}

void
workspace_view::closeEvent (QCloseEvent *e)
{
  emit active_changed (false);
  QDockWidget::closeEvent (e);
}

void
workspace_view::contextmenu_requested (const QPoint& pos)
{
  QMenu menu (this);

  QModelIndex index = view->indexAt (pos);
  QAbstractItemModel *m = view->model ();

  // if it isnt Local, Glocal etc, allow the ctx menu
  if (index.parent() != QModelIndex())
    {
      QMap<int, QVariant> item_data = m->itemData (index);
  
      QString var_name = item_data[0].toString ();

      menu.addAction ("disp(" + var_name + ")", this,
                      SLOT (handle_contextmenu_disp ()));

      menu.addAction ("plot(" + var_name + ")", this,
                      SLOT (handle_contextmenu_plot ()));

      menu.addAction ("stem(" + var_name + ")", this,
                      SLOT (handle_contextmenu_stem ()));

      menu.exec (view->mapToGlobal (pos));
    }
}

void
workspace_view::handle_contextmenu_disp (void)
{
  relay_contextmenu_command ("disp"); 
}

void
workspace_view::handle_contextmenu_plot (void)
{
  relay_contextmenu_command ("figure;\nplot"); 
}

void
workspace_view::handle_contextmenu_stem (void)
{
  relay_contextmenu_command ("figure;\nstem"); 
}

void
workspace_view::relay_contextmenu_command (const QString& cmdname)
{
  QModelIndex index = view->currentIndex ();

  if (index.parent () != QModelIndex ())
    {
      QAbstractItemModel *m = view->model ();

      QMap<int, QVariant> item_data = m->itemData (index);
  
      QString var_name = item_data[0].toString ();

      emit command_requested (cmdname + "(" + var_name + ")\n");
    }
}