exporting patch: # HG changeset patch # User Daniel J Sebald # Date 1518837245 21600 # Fri Feb 16 21:14:05 2018 -0600 # Node ID 4d649924052d483582d18de2b5f834d3cc98cc72 # Parent b96c76aed31e08e13b1a1a041bb0c45cc033ee64 Make the variable editor multi-panel via a QMainWindow layout (bug #53046) * main-window.h: Remove and from header list. * octave-dock-widget.h (label_dock_widget): Added. New intermediate class that contains the features related only to the thin header for dock/undock and close. Namely m_icon_size, m_title_widget, m_dock_button, m_close_button, m_dock_action and m_close_action and member function set_title(). (octave_dock_widget): Removed some elements for label_dock_widget class and inherit that object. * octave-dock-widget.cc (label_dock_widget::label_dock_widget): Constructor of new class selecting a few initialization from octave_dock_widget related to thin header and no settings code and no connections at this level. Set the style sheet of the m_dock_button and m_close_button to transparent. (label_dock_widget::set_title): Moved from octave_dock_widget without modification. (octave_dock_widget::octave_dock_widget): Select code moved to label_dock_widget constructor. (octave_dock_widget::set_title): Moved to label_dock_widget class. * variable-editor-model.h: (variable_editor_model::edit_variable_signal): New signal for a model to launch a new variable_dock_widget if a sub-element needs it. (variable_editor_model::update_data): Changed from private to public. (variable_editor_model::update_data_cache): Ditto. (variable_editor_model::double_click): Moved here from variable_editor to make more modular. (variable_editor_model::up): Ditto. Rename this "levelUp" for a more uniquely searchable name. * variable-editor-model.cc (variable_editor_model::setData): Change the test on input variable for QString to be anything that can be converted to a QString, e.g., numbers. (variable_editor_model::update_data): Remove obsolete emitted signal maybe_resize_columns_signal(). (variable_editor_model::range_selected): New method that provides a list of range values for the table. (variable_editor_model::double_click): Moved from variable_editor. Remove the indirect reference to table then model. (variable_editor_model::double_click): Ditto. (idx_to_expr): Ditto. (variable_editor_model::selected_to_octave): Ditto. Simplify by calling range_selected(). * variable-editor.h (var_editor_tab): Remove. A tab widget is no longer used. (variable_dock_widget): Added. A new class inherting label_dock_widget that holds the table and data model, comprised of several member functions from variable_editor class, as slots, and a few signals to access those slots. (variable_dock_widget::variable_focused_signal): Added. New signal to let variable_editor know the object or its proxy has come into focus. (variable_dock_widget::edit_variable_signal): Added. New signal to allow opening another variable in variable_editor. (HoverToolButton): Very short class derived from QToolButton whose added feature is to capture the focused variable_dock_widget when the mouse is hovered over the button. (ReturnFocusToolButton): Similarly short class inherting HoverToolButton that adds the feature of restoring the focus before activating its associated action. (ReturnFocusMenu): Analogous to ReturnFocusToolButton, but based on QMenu. This is placed on a HoverToolButton. (variable_editor): Added QStrings m_current_focus_vname and m_hovered_focus_vname to keep track of variable that most recently focused. Removed m_close_action, m_close_others_action and m_close_all_action QActions since tab widget no longer used. Some for m_tab_widget, m_tab_bar. Change QWidget pointer m_container to QMainWindow pointer m_main. (variable_editor::has_focus): Removed. Using a proxy obviates this function. (variable_editor::edit_variable): Change from private to public. (variable_editor::variable_destroyed): New slot. Connect to variable_dock_widget to keep track of when to check if toolbar still valid. (variable_editor::variable_focused): New slot. Connect to variable_dock_widget() to keep track of most recent focused variable window. (variable_editor::record_hovered_focus_variable): New slot. Connect to variable_dock_widget toolbar's ReturnFocusToolButton and ReturnFocusMenu for when mouse cursor hovers. (variable_editor::restore_hovered_focus_variable): New slot. Connect to variable_dock_widget toolbar's ReturnFocusToolButton and ReturnFocusMenu for prior to acting in order to return to active window/focus when hovered. (variable_editor::request_close_tab): Removed. No longer needed. (variable_editor::request_close_other_tabs): Ditto. (variable_editor::request_close_all_tabs): Ditto. (variable_editor::close_tab): Ditto. (variable_editor::contextmenu_requested): Moved to variable_dock_widget class. (variable_editor::columnmenu_requested): Ditto. (variable_editor::rowmenu_requested): Ditto. (variable_editor::double_click): Moved to variable_editor_model class. (variable_editor::up): Ditto. (variable_editor::clearContent): Moved to variable_dock_widget class. (variable_editor::transposeContent): Ditto. (variable_editor::delete_selected): Ditto. (variable_editor::relay_command): Renamed to relay_selected_command() for clarity. (variable_editor::clear_content_signal): Added. New signal to broadcast to the variable_dock_widgets' associated slot. (variable_editor::copy_clipboard_signal): Ditto. (variable_editor::paste_clipboard_signal): Ditto. (variable_editor::paste_table_clipboard_signal): Ditto. (variable_editor::context_menu_signal): Ditto. (variable_editor::column_menu_signal): Ditto. (variable_editor::row_menu_signal): Ditto. (variable_editor::save_signal): Ditto. (variable_editor::delete_selected_signal): Ditto. (variable_editor::selected_command_signal): Ditto. (variable_editor::enable_actions): Removed. Too short of function otherwise. (variable_editor::octave_to_coords): Removed, no longer needed. (variable_editor::real_var_name): Removed, no more KDE problem in tab names because tab widget no longer used. Names stored in variable_dock_widget objectName. (variable_editor::selected_to_octave): Move to variable_editor_model class. (variable_editor::add_tool_bar_button): Convenience function for repetitive tool bar button construction. * variable-editor.cc: Add and to list of headers. Remove from list. (make_plot_mapper): Added. Reusable, list-based convenience function. (variable_dock_widget::add_edit_actions): Ditto. (variable_editor::construct_tool_bar): Utilize make_plot_mapper(). (variable_dock_widget::createContextMenu): Ditto. Also utilize member function add_edit_actions(). (variable_dock_widget::createColumnMenu): Ditto. (variable_dock_widget::createRowMenu): Ditto. (variable_dock_widget::variable_dock_widget): Added. Constructor that sets a couple attributes and connects the header dock actions inherited from label_dock_widget to appropriate slots. (variable_dock_widget::set_editable): Moved from var_editor_tab object and made more generic so that it can be put in any container. Ensure that the container has the active widget as its focus proxy. (variable_dock_widget::selected_command_requested): Added. Like function command_requested, but chooses just the selected data in the table. Adds a plot title that also uses the %1 argument. (variable_dock_widget::handle_focus_change): Added. Connected to QApplication's focusChanged() to set/clear the blue background of the name label and keep track of most recent focused variable_dock_widget. (variable_dock_widget::change_floating): Added. New slot one line long. (variable_dock_widget::change_existence): Added. New slot that calls close() which is configurable by Qt::WA_DeleteOnClose parameter. For now, "close" means "hide". (variable_dock_widget::toplevel_change): Added. Changes the dock/undock icon, but also its tooltip. Also changes the focus to the new window. First time enlarged is very big to simulate "maximize", i.e., better temporary view of this variable. (variable_dock_widget::closeEvent, etc.): See variable-editor.h for list of functions that are moved from variable_editor, often using findChild() or findChildren() for convenience rather than m_widget_stack or m_table_widget. (variable_editor::variable_editor): Call initializer QMainWindow() and QToolBar() similar to what it was once like. Also initialize m_current_focus_vname and m_hovered_focus_vname to "". Add toolbar to m_main. Use an MDI area for central widget of m_main. Remove all tab bar layout. (variable_editor::edit_variable): Use findChild() to look for variable_dock_widget of requested name. If so, bring into focus and return. If continue, create new variable_dock_widget "page" and place in m_main. Connect all new signals/slots. Remove extra layer of QVBoxLayout container. After calling make_edit_view() and make_disp_view(), place them in the widget_stack and make the widget stack variable_dock_widget's main widget. Supply a title to the label_dock_widget and set the text of that title to the variable's name. Also connect a signal/slot to QApplication's focusChanged() so that the text and background can be updated for highlighting which variable has focus. Move other things around a bit, but still pretty much the same. Use findChildren to enable the toolbar on the first variable, similar to the way it was previous. (variable_editor::make_disp_view): Change input variable "page" to a QWidget pointer and rename "parent". Also, do not set model parent at this level, that's done at creation of the model. (variable_editor::make_edit_view): Ditto. Also, move the connections to variable_editor back into the edit_variable() function. (variable_editor::has_focus): Removed. Not needed, setting focus proxy appears to obviate this. (variable_editor::notice_settings): Took out shortcut_manager shortcuts because these tab actions no longer needed. Also, these shortcuts can possibly be handled via the QAction shortcut key. (variable_editor::add_tool_bar_button): Added. Mimic the m_tool_bar->addAction() construct used in construct_tool_bar(). (variable_editor::construct_tool_bar): Utilize add_tool_bar_button() to place ReturnFocusToolButton objects in the QToolbar. Give an example of adding a short-cut key to the Save QAction. Use a HoverToolButton with a ReturnFocusMenu object similar to existing construction. At the end, utilize findChildren() to loop through all the objects needing connections to slots rather than program individually. diff --git a/libgui/src/main-window.h b/libgui/src/main-window.h --- a/libgui/src/main-window.h +++ b/libgui/src/main-window.h @@ -28,11 +28,9 @@ along with Octave; see the file COPYING. #include #include #include -#include #include #include #include -#include #include #include #include diff --git a/libgui/src/octave-dock-widget.cc b/libgui/src/octave-dock-widget.cc --- a/libgui/src/octave-dock-widget.cc +++ b/libgui/src/octave-dock-widget.cc @@ -37,37 +37,16 @@ along with Octave; see the file COPYING. #include "octave-dock-widget.h" -octave_dock_widget::octave_dock_widget (QWidget *p) +label_dock_widget::label_dock_widget (QWidget *p) : QDockWidget (p) { - m_parent = static_cast (p); // store main window - m_floating = false; - m_predecessor_widget = nullptr; - - connect (this, SIGNAL (visibilityChanged (bool)), - this, SLOT (handle_visibility_changed (bool))); - - connect (p, SIGNAL (settings_changed (const QSettings*)), - this, SLOT (handle_settings (const QSettings*))); - - connect (p, SIGNAL (active_dock_changed (octave_dock_widget*, - octave_dock_widget*)), - this, SLOT (handle_active_dock_changed (octave_dock_widget*, - octave_dock_widget*))); - QStyle *st = style (); m_icon_size = 0.75*st->pixelMetric (QStyle::PM_SmallIconSize); - // add an extra title bar that persists when floating - - setFeatures (QDockWidget::DockWidgetMovable); // not floatable or closeable - // the custom (extra) title bar of the widget m_dock_action = new QAction (QIcon (":/actions/icons/widget-undock.png"), "", this); - m_dock_action-> setToolTip (tr ("Undock widget")); - connect (m_dock_action, SIGNAL (triggered (bool)), - this, SLOT (change_floating (bool))); + m_dock_action->setToolTip (tr ("Undock widget")); m_dock_button = new QToolButton (this); m_dock_button->setDefaultAction (m_dock_action); m_dock_button->setFocusPolicy (Qt::NoFocus); @@ -75,16 +54,15 @@ octave_dock_widget::octave_dock_widget ( m_close_action = new QAction (QIcon (":/actions/icons/widget-close.png"), "", this); - m_close_action-> setToolTip (tr ("Hide widget")); - connect (m_close_action, SIGNAL (triggered (bool)), - this, SLOT (change_visibility (bool))); + m_close_action->setToolTip (tr ("Close widget")); m_close_button = new QToolButton (this); m_close_button->setDefaultAction (m_close_action); m_close_button->setFocusPolicy (Qt::NoFocus); m_close_button->setIconSize (QSize (m_icon_size,m_icon_size)); - m_icon_color = ""; - m_title_3d = 50; + QString css_button = QString ("QToolButton {background: transparent; border: 0px;}"); + m_dock_button->setStyleSheet (css_button); + m_close_button->setStyleSheet (css_button); QHBoxLayout *h_layout = new QHBoxLayout (); h_layout->addStretch (100); @@ -106,6 +84,50 @@ octave_dock_widget::octave_dock_widget ( this, SLOT (selectAll ())); // undo handling connect (p, SIGNAL (undo_signal ()), this, SLOT (do_undo ())); +} + +// set the title in the dockwidgets title bar +void +label_dock_widget::set_title (const QString& title) +{ + QHBoxLayout *h_layout + = static_cast (titleBarWidget ()->layout ()); + QLabel *label = new QLabel (title); + label->setStyleSheet ("background: transparent;"); + h_layout->insertWidget (0,label); + setWindowTitle (title); +} + + +octave_dock_widget::octave_dock_widget (QWidget *p) + : label_dock_widget (p) +{ + m_parent = static_cast (p); // store main window + m_floating = false; + m_predecessor_widget = nullptr; + + connect (this, SIGNAL (visibilityChanged (bool)), + this, SLOT (handle_visibility_changed (bool))); + + connect (p, SIGNAL (settings_changed (const QSettings*)), + this, SLOT (handle_settings (const QSettings*))); + + connect (p, SIGNAL (active_dock_changed (octave_dock_widget*, + octave_dock_widget*)), + this, SLOT (handle_active_dock_changed (octave_dock_widget*, + octave_dock_widget*))); + + setFeatures (QDockWidget::DockWidgetMovable); // not floatable or closeable + + connect (m_dock_action, SIGNAL (triggered (bool)), + this, SLOT (change_floating (bool))); + connect (m_close_action, SIGNAL (triggered (bool)), + this, SLOT (change_visibility (bool))); + + m_close_action->setToolTip (tr ("Hide widget")); + + m_icon_color = ""; + m_title_3d = 50; installEventFilter (this); @@ -192,18 +214,6 @@ octave_dock_widget::make_widget (bool do m_floating = false; } -// set the title in the dockwidgets title bar -void -octave_dock_widget::set_title (const QString& title) -{ - QHBoxLayout *h_layout - = static_cast (titleBarWidget ()->layout ()); - QLabel *label = new QLabel (title); - label->setStyleSheet ("background: transparent;"); - h_layout->insertWidget (0,label); - setWindowTitle (title); -} - // set the widget which previously had focus when tabified void octave_dock_widget::set_predecessor_widget (octave_dock_widget *prev_widget) diff --git a/libgui/src/octave-dock-widget.h b/libgui/src/octave-dock-widget.h --- a/libgui/src/octave-dock-widget.h +++ b/libgui/src/octave-dock-widget.h @@ -30,7 +30,41 @@ along with Octave; see the file COPYING. #include #include -class octave_dock_widget : public QDockWidget +// The few decoration items common to both main window and variable editor. +class label_dock_widget : public QDockWidget +{ + Q_OBJECT + +public: + + label_dock_widget (QWidget *p = nullptr); + + void set_title (const QString&); + +protected slots: + + //! Slots to handle copy & paste. + //!@{ + virtual void copyClipboard (void) { } + virtual void pasteClipboard (void) { } + virtual void selectAll (void) { } + //!@} + + //! Slot to handle undo. + + virtual void do_undo (void) { } + +protected: + + int m_icon_size; + QWidget *m_title_widget; + QToolButton *m_dock_button; + QToolButton *m_close_button; + QAction *m_dock_action; + QAction *m_close_action; +}; + +class octave_dock_widget : public label_dock_widget { Q_OBJECT @@ -44,7 +78,6 @@ public: void make_window (void); void make_widget (bool dock=true); - void set_title (const QString&); void set_predecessor_widget (octave_dock_widget *prev_widget); signals: @@ -98,17 +131,6 @@ protected slots: emit active_changed (true); } - //! Slots to handle copy & paste. - //!@{ - virtual void copyClipboard (void) { } - virtual void pasteClipboard (void) { } - virtual void selectAll (void) { } - //!@} - - //! Slot to handle undo. - - virtual void do_undo (void) { } - //! Event filter for double clicks into the window decoration elements. bool eventFilter (QObject *obj, QEvent *e); @@ -130,7 +152,6 @@ private: bool m_floating; bool m_custom_style; int m_title_3d; - int m_icon_size; QColor m_bg_color; QColor m_bg_color_active; QColor m_fg_color; @@ -139,11 +160,6 @@ private: QString m_icon_color_active; octave_dock_widget *m_predecessor_widget; - QWidget *m_title_widget; - QToolButton *m_dock_button; - QToolButton *m_close_button; - QAction *m_dock_action; - QAction *m_close_action; }; #endif diff --git a/libgui/src/variable-editor-model.cc b/libgui/src/variable-editor-model.cc --- a/libgui/src/variable-editor-model.cc +++ b/libgui/src/variable-editor-model.cc @@ -33,6 +33,7 @@ along with Octave; see the file COPYING. #include #include #include +#include #include "octave-qt-link.h" #include "variable-editor-model.h" @@ -981,7 +982,7 @@ bool variable_editor_model::setData (const QModelIndex& idx, const QVariant& v_user_input, int role) { - if (role != Qt::EditRole || v_user_input.type () != QVariant::String + if (role != Qt::EditRole || ! v_user_input.canConvert (QVariant::String) || ! idx.isValid ()) return false; @@ -1374,3 +1375,14 @@ variable_editor_model::update_descriptio emit description_changed (description.isEmpty () ? make_description_text () : description); } + +void +variable_editor_model::double_click (const QModelIndex& idx) +{ + if (requires_sub_editor (idx)) + { + QString name = QString::fromStdString(rep->name ()); + emit edit_variable_signal (name + subscript_expression (idx), + value_at (idx)); + } +} diff --git a/libgui/src/variable-editor-model.h b/libgui/src/variable-editor-model.h --- a/libgui/src/variable-editor-model.h +++ b/libgui/src/variable-editor-model.h @@ -279,16 +279,22 @@ signals: void description_changed (const QString& description); + void edit_variable_signal (const QString& name, const octave_value& val); + +public slots: + + void update_data (const octave_value& val); + + void update_data_cache (void); + + void double_click (const QModelIndex& idx); + private slots: void data_error (const QString& msg); void user_error (const QString& title, const QString& msg); - void update_data_cache (void); - - void update_data (const octave_value& val); - private: base_ve_model *rep; diff --git a/libgui/src/variable-editor.cc b/libgui/src/variable-editor.cc --- a/libgui/src/variable-editor.cc +++ b/libgui/src/variable-editor.cc @@ -33,8 +33,10 @@ along with Octave; see the file COPYING. #include #include #include +#include #include #include +#include #include #include #include @@ -50,216 +52,834 @@ along with Octave; see the file COPYING. #include "variable-editor.h" #include "variable-editor-model.h" +// Code reuse functions + static QString idx_to_expr (int32_t from, int32_t to) { return (from == to - ? QString ("%1").arg (from + 1) - : QString ("%1:%2").arg (from + 1).arg (to + 1)); + ? QString ("%1").arg (from) + : QString ("%1:%2").arg (from).arg (to)); } -QTableView * -var_editor_tab::get_edit_view (void) const +QSignalMapper * +make_plot_mapper (QMenu *menu) { - return (m_edit_view_idx == m_widget_stack->currentIndex () - ? qobject_cast (m_widget_stack->widget (m_edit_view_idx)) - : nullptr); + QList list; + list << "plot" << "bar" << "stem" << "stairs" << "area" << "pie" << "hist"; + + QSignalMapper *plot_mapper = new QSignalMapper (menu); + + for (int i = 0; i < list.size(); ++i) + { + plot_mapper->setMapping + (menu->addAction (list.at (i), plot_mapper, SLOT (map ())), + "figure (); " + list.at (i) + " (%1); title (\"%1\");"); + } + + return plot_mapper; } -QTextEdit * -var_editor_tab::get_disp_view (void) const + +// Variable dock widget + +variable_dock_widget::variable_dock_widget (QWidget *p) + : label_dock_widget (p), m_initial_float (true) { - return (m_disp_view_idx == m_widget_stack->currentIndex () - ? qobject_cast (m_widget_stack->widget (m_disp_view_idx)) - : nullptr); + setFocusPolicy (Qt::StrongFocus); + // This controls whether the variable_dock_widgets are deleted + // or hidden when clicking the upper right X icon. (Look for + // the checkmark of the variable_editor's context menu.) + // Could make this an option, or perhaps add a drop-down menu + // in place of X "close" icon. +#if 0 + setAttribute (Qt::WA_DeleteOnClose); +#endif + + connect (m_dock_action, SIGNAL (triggered (bool)), + this, SLOT (change_floating (bool))); + connect (m_close_action, SIGNAL (triggered (bool)), + this, SLOT (change_existence (bool))); + connect (this, SIGNAL (topLevelChanged(bool)), + this, SLOT (toplevel_change (bool))); } void -var_editor_tab::set_edit_view (QTableView *edit_view) +variable_dock_widget::set_editable (bool editable) { - m_edit_view_idx = m_widget_stack->addWidget (edit_view); + QStackedWidget *stack = findChild (); + if (stack == nullptr) + return; + + // The QTableView is for editable data models + // and the QTextEdit is for non-editable models. + + if (editable) + { + QTableView *table = findChild (); + if (table != nullptr) + { + stack->setCurrentWidget (table); + setFocusProxy (table); + table->setFocusPolicy (Qt::StrongFocus); + } + + QTextEdit *textedit = findChild (); + if (textedit != nullptr) + textedit->setFocusPolicy (Qt::NoFocus); + } + else + { + QTextEdit *textedit = findChild (); + variable_editor_model *model = findChild (); + if (textedit != nullptr && model != nullptr) + { + stack->setCurrentWidget (textedit); + textedit->setPlainText (model->data ().toString ()); + setFocusProxy (textedit); + } + + QTableView *table = findChild (); + if (table != nullptr) + table->setFocusPolicy (Qt::NoFocus); + } +} + +// slot for (un)dock action +void +variable_dock_widget::change_floating (bool) +{ + setFloating (! isFloating ()); +} + +// slot for hiding the widget +void +variable_dock_widget::change_existence (bool) +{ + close (); } void -var_editor_tab::set_disp_view (QTextEdit *disp_view) +variable_dock_widget::toplevel_change (bool toplevel) { - m_disp_view_idx = m_widget_stack->addWidget (disp_view); -} + if (toplevel) + { + m_dock_action->setIcon (QIcon (":/actions/icons/widget-dock.png")); + m_dock_action->setToolTip (tr ("Dock widget")); -bool -var_editor_tab::has_focus (void) const -{ - QTableView *edit_view = get_edit_view (); - QTextEdit *disp_view = get_disp_view (); + // Make initial size expanded very large for "magnified" viewing + if (m_initial_float) + { + QScreen *pscreen = QGuiApplication::primaryScreen (); + QRect rect (0, 0, 0, 0); + rect = pscreen->availableGeometry (); + rect = QRect (rect.x () + 5, rect.y () + 5, + rect.width () - 10, rect.height () - 10); + setGeometry (rect); + m_initial_float = false; + } - return ((disp_view && disp_view->hasFocus ()) - || (edit_view && edit_view->hasFocus ())); + setFocus (Qt::OtherFocusReason); + activateWindow(); + } + else + { + m_dock_action->setIcon (QIcon (":/actions/icons/widget-undock.png")); + m_dock_action->setToolTip (tr ("Undock widget")); + + setFocus (Qt::OtherFocusReason); + } } void -var_editor_tab::keyPressEvent (QKeyEvent *event) +variable_dock_widget::closeEvent (QCloseEvent *e) { - QTableView *edit_view = get_edit_view (); + QDockWidget::closeEvent (e); +} - if (edit_view) +void +variable_dock_widget::clearContent (void) +{ + if (! hasFocus ()) + return; + + QList viewlist = findChildren (); + QList modellist = findChildren (); + if (viewlist.size () != 1 || modellist.size () != 1) + return; + + QTableView *view = viewlist.at (0); + QItemSelectionModel *sel = view->selectionModel (); + QList indices = sel->selectedIndexes (); + + // FIXME: Use [] for empty cells? + + for (const auto& idx : indices) + modellist.at (0)->clear_content (idx); +} + +void +variable_dock_widget::cutClipboard (void) +{ + copyClipboard (); + + clearContent (); +} + + +void +variable_dock_widget::copyClipboard (void) +{ + if (! hasFocus ()) + return; + + QList viewlist = findChildren (); + if (viewlist.size () != 1) + return; + + QTableView *view = viewlist.at (0); + QAbstractItemModel *model = view->model (); + QItemSelectionModel *sel = view->selectionModel (); + QList indices = sel->selectedIndexes (); + + qSort (indices); + if (indices.isEmpty ()) + return; + + // Convert selected items into TSV format and copy that. + // Spreadsheet tools should understand that. + + QModelIndex previous = indices.first (); + QString copy = model->data (previous).toString (); + indices.removeFirst (); + foreach (QModelIndex idx, indices) { - int key = event->key (); + copy.push_back (previous.row () != idx.row () ? '\n' : '\t'); + copy.append (model->data (idx).toString ()); + previous = idx; + } - if (key == Qt::Key_Right || key == Qt::Key_Tab) + QClipboard *clipboard = QApplication::clipboard (); + clipboard->setText (copy); +} + +void +variable_dock_widget::pasteClipboard (void) +{ + if (! hasFocus ()) + return; + + QList viewlist = findChildren (); + QList modellist = findChildren (); + if (viewlist.size () != 1 || modellist.size () != 1) + return; + + QTableView *view = viewlist.at (0); + QItemSelectionModel *sel = view->selectionModel (); + QList indices = sel->selectedIndexes (); + variable_editor_model *model = modellist.at (0); + + QClipboard *clipboard = QApplication::clipboard (); + QString text = clipboard->text (); + + if (indices.isEmpty ()) + { + if (view->size () == QSize (1,1)) + model->setData (view->model ()->index (0,0), text.toDouble ()); + else if (view->size () == QSize (0,0)) { - QModelIndex idx = edit_view->currentIndex (); + model->insertColumn (0); + model->insertRow (0); + model->setData (view->model ()->index (0,0), text.toDouble ()); + } + } + else + { + QStringList cells = text.split(QRegExp("\n|\r\n|\r")); + int clen = cells.size (); + for (int i = 0; i < indices.size (); i++) + model->setData (indices[i], cells.at (i % clen).toDouble ()); + } +} - int curr_row = idx.row (); - int next_col = idx.column () + 1; +void variable_dock_widget::pasteTableClipboard (void) +{ + if (! hasFocus ()) + return; - if (next_col == m_model->display_columns ()) + QList viewlist = findChildren (); + QList modellist = findChildren (); + if (viewlist.size () != 1 || modellist.size () != 1) + return; + + QTableView *view = viewlist.at (0); + QItemSelectionModel *sel = view->selectionModel (); + QList indices = sel->selectedIndexes (); + variable_editor_model *model = modellist.at (0); + + QClipboard *clipboard = QApplication::clipboard (); + QString text = clipboard->text (); + + QPoint start, end; + + QPoint tabsize = QPoint (model->rowCount (), model->columnCount ()); + + if (indices.isEmpty ()) + { + start = QPoint (0,0); + end = tabsize; + } + else if (indices.size () == 1) + { + start = QPoint (indices[0].row (), indices[0].column ()); + end = tabsize; + } + else + { + end = QPoint (0,0); + start = tabsize; + + for (int i = 0; i < indices.size (); i++) + { + if (indices[i].column () < start.y ()) + start.setY (indices[i].column ()); + + if (indices[i].column () > end.y ()) + end.setY (indices[i].column ()); + + if (indices[i].row () < start.x ()) + start.setX (indices[i].column ()); + + if (indices[i].row () > end.x ()) + end.setX (indices[i].column ()); + } + } + + int rownum = 0; + int colnum = 0; + + QStringList rows = text.split ('\n'); + for (const auto& row : rows) + { + if (rownum > end.x () - start.x ()) + continue; + + QStringList cols = row.split ('\t'); + if (cols.isEmpty ()) + continue; + + for (const auto& col : cols) + { + if (col.isEmpty ()) + continue; + if (colnum > end.y () - start.y () ) + continue; + + model->setData (model->index (rownum + start.x (), + colnum + start.y ()), + QVariant (col)); + + colnum++; + } + + colnum = 0; + rownum++; + } +} + +void +variable_dock_widget::levelUp (void) +{ + if (! hasFocus ()) + return; + + QString name = objectName (); + + // FIXME: Is there a better way? + + if (name.endsWith (')') || name.endsWith ('}')) + { + name.remove (QRegExp ("(\\(|\\{)[^({]*(\\)|\\})$")); + emit edit_variable_signal (name, octave_value ()); + } +} + +void +variable_dock_widget::createVariable (void) +{ + // FIXME: Create unnamed1..n if exist ('unnamed', 'var') is true. + + selected_command_requested ("unnamed = %1"); +} + +void +variable_dock_widget::transposeContent (void) +{ + if (! hasFocus ()) + return; + + QString name = objectName (); + emit command_signal (QString ("%1 = %1';").arg (name)); +} + +QList +variable_dock_widget::range_selected (void) +{ + QList viewlist = findChildren (); + if (viewlist.size () != 1) + return QList (); + + QItemSelectionModel *sel = viewlist.at (0)->selectionModel (); + + // Return early if nothing selected. + if (! sel->hasSelection ()) + return QList (); + + QList indices = sel->selectedIndexes (); + + // FIXME: Shouldn't this be keyed to octave_idx_type? + + int32_t from_row = std::numeric_limits::max (); + int32_t to_row = 0; + int32_t from_col = std::numeric_limits::max (); + int32_t to_col = 0; + + for (const auto& idx : indices) + { + from_row = std::min (from_row, idx.row ()); + to_row = std::max (to_row, idx.row ()); + from_col = std::min (from_col, idx.column ()); + to_col = std::max (to_col, idx.column ()); + } + + QVector vect; + vect << from_row + 1 << to_row + 1 << from_col + 1 << to_col + 1; + QList range = QList::fromVector(vect); + + return range; +} + +void +variable_dock_widget::add_edit_actions (QMenu *menu, const QString& qualifier_string) +{ + menu->addAction (resource_manager::icon ("edit-cut"), + tr ("Cut") + qualifier_string, + this, SLOT (cutClipboard ())); + + menu->addAction (resource_manager::icon ("edit-copy"), + tr ("Copy") + qualifier_string, + this, SLOT (copyClipboard ())); + + menu->addAction (resource_manager::icon ("edit-paste"), + tr ("Paste"), + this, SLOT (pasteClipboard ())); + + // FIXME: Different icon for Paste Table? + + menu->addAction (resource_manager::icon ("edit-paste"), + tr ("Paste Table"), + this, SLOT (pasteTableClipboard ())); + + menu->addSeparator (); + + menu->addAction (resource_manager::icon ("edit-delete"), + tr ("Clear") + qualifier_string, + this, SLOT (clearContent ())); + + menu->addAction (resource_manager::icon ("edit-delete"), + tr ("Delete") + qualifier_string, + this, SLOT (delete_selected ())); + + menu->addAction (resource_manager::icon ("document-new"), + tr ("Variable from Selection"), + this, SLOT (createVariable ())); +} + +void +variable_dock_widget::createContextMenu (const QPoint& qpos) +{ + QList viewlist = findChildren (); + QList modellist = findChildren (); + if (viewlist.size () != 1 || modellist.size () != 1) + return; + + QTableView *view = viewlist.at (0); + QModelIndex index = view->indexAt (qpos); + + if (index.isValid ()) + { + QMenu *menu = new QMenu (this); + + add_edit_actions (menu, tr ("")); + + // FIXME: addAction for sort? + // FIXME: Add icon for transpose. + + menu->addAction (tr ("Transpose"), this, SLOT (transposeContent ())); + + QItemSelectionModel *sel = view->selectionModel (); + + QList indices = sel->selectedIndexes (); + + if (! indices.isEmpty ()) + { + menu->addSeparator (); + + QSignalMapper *plot_mapper = make_plot_mapper (menu); + + connect (plot_mapper, SIGNAL (mapped (const QString&)), + this, SLOT (selected_command_requested (const QString&))); + } + + menu->exec (view->mapToGlobal (qpos)); + } +} + +void +variable_dock_widget::createColumnMenu (const QPoint& pt) +{ + QList viewlist = findChildren (); + if (viewlist.size () != 1) + return; + + QTableView *view = viewlist.at (0); + + int index = view->horizontalHeader ()->logicalIndexAt (pt); + + if (index < 0 || index > view->model ()->columnCount ()) + return; + + QList coords = range_selected (); + + bool nothingSelected = coords.isEmpty (); + + bool whole_columns_selected + = (nothingSelected + ? false + : (coords[0] == 1 && coords[1] == view->model ()->rowCount ())); + + bool current_column_selected + = nothingSelected ? false : (coords[2] <= index+1 && coords[3] > index); + + int column_selection_count + = nothingSelected ? 0 : (coords[3] - coords[2] + 1); + + if (! whole_columns_selected || ! current_column_selected) + { + view->selectColumn (index); + column_selection_count = 1; + current_column_selected = true; + whole_columns_selected = true; + } + + QString column_string + = tr (column_selection_count > 1 ? " columns" : " column"); + + QMenu *menu = new QMenu (this); + + add_edit_actions (menu, column_string); + + menu->addSeparator (); + + QSignalMapper *plot_mapper = make_plot_mapper (menu); + + connect (plot_mapper, SIGNAL (mapped (const QString&)), + this, SLOT (selected_command_requested (const QString&))); + + QPoint menupos = pt; + menupos.setY (view->horizontalHeader ()->height ()); + + menu->exec (view->mapToGlobal (menupos)); +} + +void +variable_dock_widget::createRowMenu (const QPoint& pt) +{ + QList viewlist = findChildren (); + if (viewlist.size () != 1) + return; + + QTableView *view = viewlist.at (0); + + int index = view->verticalHeader ()->logicalIndexAt (pt); + + if (index < 0 || index > view->model ()->columnCount ()) + return; + + QList coords = range_selected (); + + bool nothingSelected = coords.isEmpty (); + + bool whole_rows_selected + = (nothingSelected + ? false + : (coords[2] == 1 && coords[3] == view->model ()->columnCount ())); + + bool current_row_selected + = (nothingSelected ? false : (coords[0] <= index+1 && coords[1] > index)); + + int rowselection_count = nothingSelected ? 0 : (coords[3] - coords[2] + 1); + + if (! whole_rows_selected || ! current_row_selected) + { + view->selectRow (index); + rowselection_count = 1; + current_row_selected = true; + whole_rows_selected = true; + } + + QString row_string = tr (rowselection_count > 1 ? " rows" : " row"); + + QMenu *menu = new QMenu (this); + + add_edit_actions (menu, row_string); + + menu->addSeparator (); + + QSignalMapper *plot_mapper = make_plot_mapper (menu); + + connect (plot_mapper, SIGNAL (mapped (const QString&)), + this, SLOT (selected_command_requested (const QString&))); + + QPoint menupos = pt; + menupos.setX (view->verticalHeader ()->width ()); + + // FIXME: What was the intent here? + // setY (view->verticalHeader ()->sectionPosition (index+1) + + // view->verticalHeader ()->sectionSize (index)); + + menu->exec (view->mapToGlobal (menupos)); +} + +void +variable_dock_widget::handle_horizontal_scroll_action (int action) +{ + if (action == QAbstractSlider::SliderSingleStepAdd + || action == QAbstractSlider::SliderPageStepAdd + || action == QAbstractSlider::SliderToMaximum + || action == QAbstractSlider::SliderMove) + { + QList viewlist = findChildren (); + QList modellist = findChildren (); + + if (viewlist.size () == 1 && modellist.size() == 1) + { + QTableView *edit_view = viewlist.at (0); + variable_editor_model *model = modellist.at (0); + + QScrollBar *sb = edit_view->horizontalScrollBar (); + + if (sb && sb->value () == sb->maximum ()) { - m_model->maybe_resize_columns (next_col + 16); + int new_cols = model->display_columns () + 16; - edit_view->setCurrentIndex (m_model->index (curr_row, next_col)); - } - } - else if (key == Qt::Key_Down || key == Qt::Key_PageDown) - { - QModelIndex idx = edit_view->currentIndex (); - - int next_row = idx.row () + 1; - int curr_col = idx.column (); - - if (next_row == m_model->display_rows ()) - { - m_model->maybe_resize_rows (next_row + 16); - - edit_view->setCurrentIndex (m_model->index (next_row, curr_col)); + model->maybe_resize_columns (new_cols); } } } } void -var_editor_tab::set_editable (bool editable) -{ - int idx = (editable ? m_edit_view_idx : m_disp_view_idx); - - m_widget_stack->setCurrentIndex (idx); - - if (! editable) - { - QTextEdit *viewer = get_disp_view (); - - QVariant v_data = m_model->data (); - - QString str = v_data.toString (); - - viewer->setPlainText (str); - } -} - -void -var_editor_tab::handle_horizontal_scroll_action (int action) +variable_dock_widget::handle_vertical_scroll_action (int action) { if (action == QAbstractSlider::SliderSingleStepAdd || action == QAbstractSlider::SliderPageStepAdd || action == QAbstractSlider::SliderToMaximum || action == QAbstractSlider::SliderMove) { - QTableView *edit_view = get_edit_view (); + QList viewlist = findChildren (); + QList modellist = findChildren (); - if (edit_view) + if (viewlist.size () == 1 && modellist.size() == 1) { - QScrollBar *sb = edit_view->horizontalScrollBar (); + QTableView *edit_view = viewlist.at (0); + variable_editor_model *model = modellist.at (0); + + QScrollBar *sb = edit_view->verticalScrollBar (); if (sb && sb->value () == sb->maximum ()) { - int new_cols = m_model->display_columns () + 16; + int new_rows = model->display_rows () + 16; - m_model->maybe_resize_columns (new_cols); + model->maybe_resize_rows (new_rows); } } } } void -var_editor_tab::handle_vertical_scroll_action (int action) +variable_dock_widget::save (void) { - if (action == QAbstractSlider::SliderSingleStepAdd - || action == QAbstractSlider::SliderPageStepAdd - || action == QAbstractSlider::SliderToMaximum - || action == QAbstractSlider::SliderMove) + if (! hasFocus ()) + return; + + QString name = objectName (); + QString file + = QFileDialog::getSaveFileName (this, + tr ("Save Variable %1 As").arg (name), + /* Should determine extension from save_default_options */ + tr ("./%1.txt").arg (name), + 0, 0, + QFileDialog::DontUseNativeDialog); + + // FIXME: Type? binary, float-binary, ascii, text, hdf5, matlab format? + // FIXME: Call octave_value::save_* directly? + + if (! file.isEmpty ()) + emit command_signal (QString ("save (\"%1\", \"%2\");") + .arg (file) + .arg (name)); +} + +void +variable_dock_widget::delete_selected (void) +{ + if (! hasFocus ()) + return; + + QList modellist = findChildren (); + if (modellist.size() != 1) + return; + + variable_editor_model *model = modellist.at (0); + + QList coords = range_selected (); + + if (coords.isEmpty ()) + return; + + bool whole_columns_selected + = coords[0] == 1 && coords[1] == model->rowCount (); + + bool whole_rows_selected + = coords[2] == 1 && coords[3] == model->columnCount (); + + // Must be deleting whole columns or whole rows, and not the whole thing. + + if (whole_columns_selected == whole_rows_selected) + return; + + if (whole_rows_selected) + model->removeRows (coords[0], coords[1] - coords[0]); + + if (whole_columns_selected) + model->removeColumns (coords[2], coords[3] - coords[2]); +} + +QString +variable_dock_widget::selected_to_octave (void) +{ + QList range = range_selected (); + if (range.isEmpty ()) + return objectName (); + + QString rows = idx_to_expr (range.at (0), range.at (1)); + QString cols = idx_to_expr (range.at (2), range.at (3)); + + // FIXME: Does cell need separate handling? Maybe use '{.,.}'? + + return QString ("%1(%2, %3)").arg (objectName ()).arg (rows).arg (cols); +} + +void +variable_dock_widget::selected_command_requested (const QString& cmd) +{ + if (! hasFocus ()) + return; + + QList modellist = findChildren (); + if (modellist.size() != 1) + return; + + emit command_signal (cmd.arg (selected_to_octave ())); +} + +void +variable_dock_widget::handle_focus_change (QWidget *old, QWidget *now) +{ + octave_unused_parameter (now); + + // The is a proxied test + if (hasFocus ()) { - QTableView *edit_view = get_edit_view (); + QLabel *label = titleBarWidget ()->findChild (); + if (label != nullptr) + label->setStyleSheet ("QLabel {background: skyblue;}"); - if (edit_view) - { - QScrollBar *sb = edit_view->verticalScrollBar (); - - if (sb && sb->value () == sb->maximum ()) - { - int new_rows = m_model->display_rows () + 16; - - m_model->maybe_resize_rows (new_rows); - } - } + emit variable_focused_signal (objectName ()); + } + else if (old == focusWidget()) + { + QLabel *label = titleBarWidget ()->findChild (); + if (label != NULL) + label->setStyleSheet ("QLabel {background: transparent;}"); } } -// Functions for reimplemented tab widget. -var_editor_tab_widget::var_editor_tab_widget (QWidget *p) - : QTabWidget (p) +HoverToolButton::HoverToolButton (QWidget *parent) + : QToolButton (parent) { - tab_bar *bar = new tab_bar (this); - - connect (bar, SIGNAL (close_current_tab_signal (bool)), - p->parent (), SLOT (request_close_tab (bool))); - - this->setTabBar (bar); - - setTabsClosable (true); - setMovable (true); + installEventFilter (this); } -tab_bar * -var_editor_tab_widget::get_tab_bar (void) const +bool HoverToolButton::eventFilter (QObject *obj, QEvent *ev) { - return qobject_cast (tabBar ()); + if (ev->type () == QEvent::HoverEnter) + emit hovered_signal (); + else if (ev->type () == QEvent::MouseButtonPress || + ev->type () == QEvent::MouseButtonPress) + emit popup_shown_signal (); + + return QToolButton::eventFilter (obj, ev); } -bool -var_editor_tab_widget::current_tab_has_focus (void) const +ReturnFocusToolButton::ReturnFocusToolButton (QWidget *parent) + : HoverToolButton (parent) { - var_editor_tab *tab - = qobject_cast (widget (currentIndex ())); - - return tab->has_focus (); + installEventFilter (this); } -QTableView * -var_editor_tab_widget::get_edit_view (void) const +bool ReturnFocusToolButton::eventFilter (QObject *obj, QEvent *ev) { - var_editor_tab *tab - = qobject_cast (widget (currentIndex ())); - return tab->get_edit_view (); + if (ev->type () == QEvent::MouseButtonRelease && isDown ()) + { + emit about_to_activate (); + + setDown (false); + QAction *action = defaultAction (); + if (action != nullptr) + action->activate (QAction::Trigger); + + return true; + } + + return HoverToolButton::eventFilter (obj, ev); } -QTextEdit * -var_editor_tab_widget::get_disp_view (void) const +ReturnFocusMenu::ReturnFocusMenu (QWidget *parent) + : QMenu (parent) { - var_editor_tab *tab - = qobject_cast (widget (currentIndex ())); + installEventFilter (this); +} - return tab->get_disp_view (); +bool ReturnFocusMenu::eventFilter (QObject *obj, QEvent *ev) +{ + if (ev->type () == QEvent::MouseButtonRelease && underMouse ()) + { + emit about_to_activate (); + } + + return QMenu::eventFilter (obj, ev); } // Variable editor. variable_editor::variable_editor (QWidget *p) : octave_dock_widget (p), - m_container (new QWidget (this)), - m_tool_bar (new QToolBar ("", m_container)), - m_tab_widget (new var_editor_tab_widget (m_container)), - m_tab_bar (m_tab_widget->get_tab_bar ()), + m_main (new QMainWindow ()), + m_tool_bar (new QToolBar (m_main)), m_default_width (30), m_default_height (100), m_add_font_height (0), @@ -269,63 +889,42 @@ variable_editor::variable_editor (QWidge m_font (), m_sel_font (), m_table_colors (), - m_close_action (nullptr), - m_close_others_action (nullptr), - m_close_all_action (nullptr) + m_current_focus_vname (""), + m_hovered_focus_vname ("") { setObjectName ("variable_editor"); set_title (tr ("Variable Editor")); setStatusTip (tr ("Edit variables.")); setWindowIcon (QIcon (":/actions/icons/logo.png")); + setFocusPolicy (Qt::NoFocus); + setAttribute (Qt::WA_AlwaysShowToolTips); + + // Tool Bar. construct_tool_bar (); + m_main->addToolBar (m_tool_bar); + m_main->setFocusPolicy (Qt::NoFocus); + setFocusPolicy (Qt::NoFocus); // Colors. for (int i = 0; i < resource_manager::varedit_color_chars ().length (); i++) m_table_colors.append (QColor (Qt::white)); - // Tab Widget. + // Use an MDI area that is shrunk to nothing as the central widget. + // Future feature might be to switch to MDI mode in which the dock + // area is shrunk to nothing and the widgets live in the MDI window. - connect (m_tab_widget, SIGNAL (tabCloseRequested (int)), - this, SLOT (closeTab (int))); + QMdiArea *central_mdiarea = new QMdiArea (m_main); + central_mdiarea->setMinimumSize (QSize (0, 0)); + central_mdiarea->setMaximumSize (QSize (0, 0)); + central_mdiarea->resize (QSize (0, 0)); + m_main->setCentralWidget (central_mdiarea); - // Tab bar. + m_main->setParent (this); + setWidget (m_main); - m_close_action - = add_action (m_tab_bar->get_context_menu (), - resource_manager::icon ("window-close",false), - tr ("&Close"), - SLOT (request_close_tab (bool))); - - m_close_others_action - = add_action (m_tab_bar->get_context_menu (), - resource_manager::icon ("window-close",false), - tr ("Close &Other Tabs"), - SLOT (request_close_other_tabs (bool))); - - m_close_all_action - = add_action (m_tab_bar->get_context_menu (), - resource_manager::icon ("window-close",false), - tr ("Close &All Tabs"), - SLOT (request_close_all_tabs (bool))); - - enable_actions (); - - // Layout the widgets vertically with the toolbar on top. - - QVBoxLayout *vbox_layout = new QVBoxLayout (); - - vbox_layout->setSpacing (0); - vbox_layout->addWidget (m_tool_bar); - vbox_layout->addWidget (m_tab_widget); - vbox_layout->setMargin (1); - - m_container->setLayout (vbox_layout); - - setWidget (m_container); - - connect (this, SIGNAL (command_requested (const QString&)), + connect (this, SIGNAL (command_signal (const QString&)), p, SLOT (execute_command_in_terminal (const QString&))); } @@ -351,48 +950,6 @@ variable_editor::add_action (QMenu *menu return a; } -// Slot for the close tab action. - -void -variable_editor::request_close_tab (bool) -{ - closeTab (m_tab_bar->currentIndex ()); -} - -// Slot for the close other tabs action. - -void -variable_editor::request_close_other_tabs (bool) -{ - int current = m_tab_bar->currentIndex (); - - for (int index = m_tab_bar->count ()-1; index >= 0; index--) - { - if (current != index) - closeTab (index); - } -} - -// Slot for closing all tabs. - -void -variable_editor::request_close_all_tabs (bool) -{ - for (int index = m_tab_bar->count ()-1; index >= 0; index--) - closeTab (index); -} - -void -variable_editor::enable_actions (void) -{ - const int count = m_tab_widget->count (); - - m_tool_bar->setEnabled (count > 0); - m_close_action->setEnabled (count > 0); - m_close_all_action->setEnabled (count > 0); - m_close_others_action->setEnabled (count > 1); -} - void variable_editor::edit_variable (const QString& name, const octave_value& val) { @@ -402,70 +959,122 @@ variable_editor::edit_variable (const QS notice_settings (settings); } - const int tab_count = m_tab_widget->count (); - for (int i = 0; i < tab_count; ++i) + QDockWidget *existing_qdw = m_main->findChild (name); + if (existing_qdw != NULL) { - if (real_var_name (i) == name) - { - // Already open. + // Already open. - m_tab_widget->setCurrentIndex (i); - return; - } + // Put current focused variable out of focus + QFocusEvent event (QEvent::FocusOut, Qt::OtherFocusReason); + QApplication::sendEvent (m_main->focusWidget (), &event); + + // Put existing variable in focus and raise + event = QFocusEvent (QEvent::FocusIn, Qt::OtherFocusReason); + QApplication::sendEvent (existing_qdw, &event); + existing_qdw->show (); + existing_qdw->raise (); + existing_qdw->setFocus (); + + return; } - // Do not set parent. + variable_dock_widget *page = new variable_dock_widget (this); + page->setObjectName (name); + m_main->addDockWidget (Qt::TopDockWidgetArea, page); - QStackedWidget *widget_stack = new QStackedWidget (); + connect (page, SIGNAL (command_signal (const QString&)), + this, SIGNAL (command_signal (const QString&))); + connect (page, SIGNAL (destroyed (QObject *)), + this, SLOT (variable_destroyed (QObject *))); + connect (page, SIGNAL (variable_focused_signal (const QString&)), + this, SLOT (variable_focused (const QString&))); - var_editor_tab *page = new var_editor_tab (widget_stack); + connect (this, SIGNAL (clear_content_signal ()), + page, SLOT (clearContent ())); + connect (this, SIGNAL (copy_clipboard_signal ()), + page, SLOT (copyClipboard ())); + connect (this, SIGNAL (paste_clipboard_signal ()), + page, SLOT (pasteClipboard ())); + connect (this, SIGNAL (paste_table_clipboard_signal ()), + page, SLOT (pasteTableClipboard ())); + connect (this, SIGNAL (level_up_signal ()), + page, SLOT (levelUp ())); + connect (this, SIGNAL (context_menu_signal (const QPoint&)), + page, SLOT (createContextMenu (const QPoint&))); + connect (this, SIGNAL (column_menu_signal (const QPoint&)), + page, SLOT (createColumnMenu (const QPoint&))); + connect (this, SIGNAL (row_menu_signal (const QPoint&)), + page, SLOT (createRowMenu (const QPoint&))); + connect (this, SIGNAL (save_signal ()), + page, SLOT (save ())); + connect (this, SIGNAL (delete_selected_signal ()), + page, SLOT (delete_selected ())); + connect (this, SIGNAL (selected_command_signal (const QString&)), + page, SLOT (selected_command_requested (const QString&))); + connect (page, SIGNAL (edit_variable_signal (const QString&, const octave_value&)), + this, SLOT (edit_variable (const QString&, const octave_value&))); + connect (QApplication::instance(), SIGNAL (focusChanged (QWidget *, QWidget *)), + page, SLOT (handle_focus_change (QWidget *, QWidget *))); - QVBoxLayout *vbox = new QVBoxLayout (page); - page->setLayout (vbox); - - QLabel *label = new QLabel (page); - label->setTextFormat (Qt::PlainText); - vbox->addWidget (label, 0, Qt::AlignTop); - - variable_editor_model *model = new variable_editor_model (name, val); + variable_editor_model *model = + new variable_editor_model (name, val, page); QTableView *edit_view = make_edit_view (page, model); + connect (edit_view->horizontalHeader (), + SIGNAL (customContextMenuRequested (const QPoint&)), + page, SLOT (createColumnMenu (const QPoint&))); + connect (edit_view->verticalHeader (), + SIGNAL (customContextMenuRequested (const QPoint&)), + page, SLOT (createRowMenu (const QPoint&))); + connect (edit_view, SIGNAL (customContextMenuRequested (const QPoint&)), + page, SLOT (createContextMenu (const QPoint&))); + connect (edit_view, SIGNAL (doubleClicked (const QModelIndex&)), + model, SLOT (double_click (const QModelIndex&))); + connect (model, SIGNAL (edit_variable_signal (const QString&, const octave_value&)), + this, SLOT (edit_variable (const QString&, const octave_value&))); + connect (edit_view->horizontalScrollBar (), SIGNAL (actionTriggered (int)), + page, SLOT (handle_horizontal_scroll_action (int))); + connect (edit_view->verticalScrollBar (), SIGNAL (actionTriggered (int)), + page, SLOT (handle_vertical_scroll_action (int))); QTextEdit *disp_view = make_disp_view (page, model); - page->set_model (model); + QStackedWidget *widget_stack = new QStackedWidget (page); + widget_stack->addWidget (edit_view); + widget_stack->addWidget (disp_view); + page->setWidget (widget_stack); - page->set_edit_view (edit_view); - page->set_disp_view (disp_view); + // Must supply a title for a QLabel to be created. Calling set_title() + // more than once will add more QLabels. Could change octave_dock_widget + // to always supply a QLabl (initially empty) and then simply update its + // contents. + page->set_title (name); + QLabel *existing_ql = page->titleBarWidget ()->findChild (); + connect (model, SIGNAL (update_label_signal (const QString&)), + existing_ql, SLOT (setText (const QString&))); - vbox->addWidget (widget_stack); - - int tab_idx = m_tab_widget->addTab (page, name); - - m_tab_widget->setCurrentIndex (tab_idx); - - connect (model, SIGNAL (description_changed (const QString&)), - label, SLOT (setText (const QString&))); + connect (this, SIGNAL (refresh_signal ()), + model, SLOT (update_data_cache ())); + connect (this, SIGNAL (edit_variable_signal (const QString&, const octave_value&)), + model, SLOT (edit_variable (const QString&, const octave_value&))); connect (model, SIGNAL (dataChanged (const QModelIndex&, const QModelIndex&)), this, SLOT (callUpdate (const QModelIndex&, const QModelIndex&))); - - connect (this, SIGNAL (refresh_signal (void)), - model, SLOT (update_data_cache (void))); - connect (model, SIGNAL (set_editable_signal (bool)), page, SLOT (set_editable (bool))); - enable_actions (); + model->update_data (val); + + QList viewlist = findChildren (); + if (viewlist.size () == 1) + m_tool_bar->setEnabled (true); } QTableView * -variable_editor::make_edit_view (var_editor_tab *page, - variable_editor_model *model) +variable_editor::make_edit_view (QWidget *parent, + variable_editor_model *model) { - QTableView *table = new QTableView (page); - - model->setParent (table); + QTableView *table = new QTableView (parent); table->setModel (model); table->setWordWrap (false); @@ -475,29 +1084,9 @@ variable_editor::make_edit_view (var_edi table->horizontalHeader ()->setContextMenuPolicy (Qt::CustomContextMenu); table->verticalHeader ()->setContextMenuPolicy (Qt::CustomContextMenu); - connect (table->horizontalHeader (), - SIGNAL (customContextMenuRequested (const QPoint&)), - this, SLOT (columnmenu_requested (const QPoint&))); - - connect (table->verticalHeader (), - SIGNAL (customContextMenuRequested (const QPoint&)), - this, SLOT (rowmenu_requested (const QPoint&))); - - connect (table, SIGNAL (customContextMenuRequested (const QPoint&)), - this, SLOT (contextmenu_requested (const QPoint&))); - - connect (table, SIGNAL (doubleClicked (const QModelIndex&)), - this, SLOT (double_click (const QModelIndex&))); - table->setHorizontalScrollMode (QAbstractItemView::ScrollPerPixel); table->setVerticalScrollMode (QAbstractItemView::ScrollPerPixel); - connect (table->horizontalScrollBar (), SIGNAL (actionTriggered (int)), - page, SLOT (handle_horizontal_scroll_action (int))); - - connect (table->verticalScrollBar (), SIGNAL (actionTriggered (int)), - page, SLOT (handle_vertical_scroll_action (int))); - table->setFont (m_font); table->setStyleSheet (m_stylesheet); table->setAlternatingRowColors (m_alternate_rows); @@ -531,19 +1120,13 @@ variable_editor::make_edit_view (var_edi } QTextEdit * -variable_editor::make_disp_view (var_editor_tab *page, +variable_editor::make_disp_view (QWidget *parent, variable_editor_model *model) { - QTextEdit *viewer = new QTextEdit (page); + QTextEdit *viewer = new QTextEdit (parent); - model->setParent (viewer); - - QVariant v_data = model->data (); - - QString str = v_data.toString (); - + viewer->setPlainText (model->data ().toString ()); viewer->setLineWrapMode (QTextEdit::NoWrap); - viewer->setPlainText (str); viewer->setReadOnly (true); return viewer; @@ -555,27 +1138,6 @@ variable_editor::refresh (void) emit refresh_signal (); } -bool -variable_editor::has_focus (void) -{ - // FIXME: This only generates exceptions in certain circumstances. - // Get a definitive list and eliminate the need to handle exceptions? - - if (m_tab_widget->currentIndex () == -1) - return false; // No tabs. - - try - { - return m_tab_widget->current_tab_has_focus (); - } - catch (...) - { - return false; - } - - return false; -} - QList variable_editor::default_colors (void) { @@ -686,12 +1248,6 @@ variable_editor::notice_settings (const icon_size = st->pixelMetric (QStyle::PM_SmallIconSize); m_tool_bar->setIconSize (QSize (icon_size, icon_size)); - - // Shortcuts. - - shortcut_manager::set_shortcut (m_close_action, "editor_file:close"); - shortcut_manager::set_shortcut (m_close_all_action, "editor_file:close_all"); - shortcut_manager::set_shortcut (m_close_others_action, "editor_file:close_other"); } void @@ -703,759 +1259,88 @@ variable_editor::closeEvent (QCloseEvent } void -variable_editor::closeTab (int idx) +variable_editor::variable_destroyed (QObject *) { - if (idx < 0 || idx > m_tab_widget->count ()) - return; - - QWidget *wdgt = m_tab_widget->widget (idx); - - m_tab_widget->removeTab (idx); - - delete wdgt; - - enable_actions (); + QList vdwlist = findChildren (); + if (vdwlist.isEmpty ()) + m_tool_bar->setEnabled (false); } void -variable_editor::contextmenu_requested (const QPoint& qpos) +variable_editor::variable_focused (const QString &name) { - QTableView *view = m_tab_widget->get_edit_view (); - - if (! view) - return; - - QModelIndex index = view->indexAt (qpos); - - if (index.isValid ()) - { - QMenu *menu = new QMenu (this); - - menu->addAction (resource_manager::icon ("edit-cut"), - tr ("Cut"), this, SLOT (cutClipboard ())); - - menu->addAction (resource_manager::icon ("edit-copy"), - tr ("Copy"), this, SLOT (copyClipboard ())); - - menu->addAction (resource_manager::icon ("edit-paste"), - tr ("Paste"), this, SLOT (pasteClipboard ())); - - // FIXME: Need different icon for paste table separate from paste? - - menu->addAction (resource_manager::icon ("edit-paste"), - tr ("Paste Table"), this, - SLOT (pasteTableClipboard ())); - - menu->addSeparator (); - - menu->addAction (resource_manager::icon ("edit-delete"), - tr ("Clear"), this, SLOT (clearContent ())); - - menu->addAction (resource_manager::icon ("document-new"), - tr ("Variable from Selection"), this, - SLOT (createVariable ())); - - // FIXME: addAction for sort? - // FIXME: Add icon for transpose. - - menu->addAction (tr ("Transpose"), this, SLOT (transposeContent ())); - - QItemSelectionModel *sel = view->selectionModel (); - - QList indices = sel->selectedIndexes (); - - if (! indices.isEmpty ()) - { - menu->addSeparator (); - - QSignalMapper *plot_mapper = new QSignalMapper (menu); - - plot_mapper->setMapping - (menu->addAction ("plot", plot_mapper, SLOT (map ())), - "figure (); plot (%1);"); - - plot_mapper->setMapping - (menu->addAction ("bar", plot_mapper, SLOT (map ())), - "figure (); bar (%1);"); - - plot_mapper->setMapping - (menu->addAction ("stem", plot_mapper, SLOT (map ())), - "figure (); stem (%1);"); - - plot_mapper->setMapping - (menu->addAction ("stairs", plot_mapper, SLOT (map ())), - "figure (); stairs (%1);"); - - plot_mapper->setMapping - (menu->addAction ("area", plot_mapper, SLOT (map ())), - "figure (); area (%1);"); - - plot_mapper->setMapping - (menu->addAction ("pie", plot_mapper, SLOT (map ())), - "figure (); pie (%1);"); - - plot_mapper->setMapping - (menu->addAction ("hist", plot_mapper, SLOT (map ())), - "figure (); hist (%1);"); - - connect (plot_mapper, SIGNAL (mapped (const QString&)), - this, SLOT (relay_command (const QString&))); - } - - menu->exec (view->mapToGlobal (qpos)); - } + m_current_focus_vname = name; } void -variable_editor::columnmenu_requested (const QPoint& pt) +variable_editor::record_hovered_focus_variable (void) { - QTableView *view = m_tab_widget->get_edit_view (); - - if (! view) - return; - - int index = view->horizontalHeader ()->logicalIndexAt (pt); - - if (index < 0 || index > view->model ()->columnCount ()) - return; - - QString selection = selected_to_octave (); - - QList coords = octave_to_coords (selection); - - bool nothingSelected = coords.isEmpty (); - - bool whole_columns_selected - = (nothingSelected - ? false - : (coords[0] == 1 && coords[1] == view->model ()->rowCount ())); - - bool current_column_selected - = nothingSelected ? false : (coords[2] <= index+1 && coords[3] > index); - - int column_selection_count - = nothingSelected ? 0 : (coords[3] - coords[2] + 1); - - if (! whole_columns_selected || ! current_column_selected) - { - view->selectColumn (index); - column_selection_count = 1; - current_column_selected = true; - whole_columns_selected = true; - } - - QString column_string - = tr (column_selection_count > 1 ? " columns" : " column"); - - QMenu *menu = new QMenu (this); - - menu->addAction (resource_manager::icon ("edit-cut"), - tr ("Cut") + column_string, - this, SLOT (cutClipboard ())); - - menu->addAction (resource_manager::icon ("edit-copy"), - tr ("Copy") + column_string, - this, SLOT (copyClipboard ())); - - menu->addAction (resource_manager::icon ("edit-paste"), - tr ("Paste"), - this, SLOT (pasteClipboard ())); - - // FIXME: Different icon for Paste Table? - - menu->addAction (resource_manager::icon ("edit-paste"), - tr ("Paste Table"), - this, SLOT (pasteTableClipboard ())); - - menu->addSeparator (); - - menu->addAction (resource_manager::icon ("edit-delete"), - tr ("Clear") + column_string, - this, SLOT (clearContent ())); - - menu->addAction (resource_manager::icon ("edit-delete"), - tr ("Delete") + column_string, - this, SLOT (delete_selected ())); - - menu->addAction (resource_manager::icon ("document-new"), - tr ("Variable from Selection"), - this, SLOT (createVariable ())); - - menu->addSeparator (); - - QSignalMapper *plot_mapper = new QSignalMapper (menu); - - plot_mapper->setMapping - (menu->addAction ("plot", plot_mapper, SLOT (map ())), - "figure (); plot (%1);"); - - plot_mapper->setMapping - (menu->addAction ("bar", plot_mapper, SLOT (map ())), - "figure (); bar (%1);"); - - plot_mapper->setMapping - (menu->addAction ("stem", plot_mapper, SLOT (map ())), - "figure (); stem (%1);"); - - plot_mapper->setMapping - (menu->addAction ("stairs", plot_mapper, SLOT (map ())), - "figure (); stairs (%1);"); - - plot_mapper->setMapping - (menu->addAction ("area", plot_mapper, SLOT (map ())), - "figure (); area (%1);"); - - plot_mapper->setMapping - (menu->addAction ("pie", plot_mapper, SLOT (map ())), - "figure (); pie (%1);"); - - plot_mapper->setMapping - (menu->addAction ("hist", plot_mapper, SLOT (map ())), - "figure (); hist (%1);"); - - connect (plot_mapper, SIGNAL (mapped (const QString&)), - this, SLOT (relay_command (const QString&))); - - QPoint menupos = pt; - menupos.setY (view->horizontalHeader ()->height ()); - - menu->exec (view->mapToGlobal (menupos)); + m_hovered_focus_vname = m_current_focus_vname; } void -variable_editor::rowmenu_requested (const QPoint& pt) +variable_editor::restore_hovered_focus_variable (void) { - QTableView *view = m_tab_widget->get_edit_view (); - - if (! view) - return; - - int index = view->verticalHeader ()->logicalIndexAt (pt); - - if (index < 0 || index > view->model ()->columnCount ()) - return; - - QString selection = selected_to_octave (); - - QList coords = octave_to_coords (selection); - - bool nothingSelected = coords.isEmpty (); - - bool whole_rows_selected - = (nothingSelected - ? false - : (coords[2] == 1 && coords[3] == view->model ()->columnCount ())); - - bool current_row_selected - = (nothingSelected ? false : (coords[0] <= index+1 && coords[1] > index)); - - int rowselection_count = nothingSelected ? 0 : (coords[3] - coords[2] + 1); - - if (! whole_rows_selected || ! current_row_selected) + variable_dock_widget *tofocus = findChild (m_hovered_focus_vname); + if (tofocus != nullptr) { - view->selectRow (index); - rowselection_count = 1; - current_row_selected = true; - whole_rows_selected = true; + // Note that this may be platform and window system dependent. + // On a particular Linux system, activateWindow() alone didn't + // immediately set the active window and there was a race + // between the window focus and action signal. Setting the + // active window via the QApplication route did work. + QApplication::setActiveWindow(tofocus->window()); + tofocus->activateWindow (); + tofocus->setFocus (Qt::OtherFocusReason); } - - QString row_string = tr (rowselection_count > 1 ? " rows" : " row"); - - QMenu *menu = new QMenu (this); - - menu->addAction (resource_manager::icon ("edit-cut"), - tr ("Cut") + row_string, - this, SLOT (cutClipboard ())); - - menu->addAction (resource_manager::icon ("edit-copy"), - tr ("Copy") + row_string, - this, SLOT (copyClipboard ())); - - menu->addAction (resource_manager::icon ("edit-paste"), - tr ("Paste"), - this, SLOT (pasteClipboard ())); - - // FIXME: Better icon for Paste Table? - - menu->addAction (resource_manager::icon ("edit-paste"), - tr ("Paste Table"), - this, SLOT (pasteTableClipboard ())); - - menu->addSeparator (); - - menu->addAction (resource_manager::icon ("edit-delete"), - tr ("Clear") + row_string, - this, SLOT (clearContent ())); - - menu->addAction (resource_manager::icon ("edit-delete"), - tr ("Delete") + row_string, - this, SLOT (delete_selected ())); - - menu->addAction (resource_manager::icon ("document-new"), - tr ("Variable from Selection"), - this, SLOT (createVariable ())); - - menu->addSeparator (); - - QSignalMapper *plot_mapper = new QSignalMapper (menu); - - plot_mapper->setMapping - (menu->addAction ("plot", plot_mapper, SLOT (map ())), - "figure (); plot (%1);"); - - plot_mapper->setMapping - (menu->addAction ("bar", plot_mapper, SLOT (map ())), - "figure (); bar (%1);"); - - plot_mapper->setMapping - (menu->addAction ("stem", plot_mapper, SLOT (map ())), - "figure (); stem (%1);"); - - plot_mapper->setMapping - (menu->addAction ("stairs", plot_mapper, SLOT (map ())), - "figure (); stairs (%1);"); - - plot_mapper->setMapping - (menu->addAction ("area", plot_mapper, SLOT (map ())), - "figure (); area (%1);"); - - plot_mapper->setMapping - (menu->addAction ("pie", plot_mapper, SLOT (map ())), - "figure (); pie (%1);"); - - plot_mapper->setMapping - (menu->addAction ("hist", plot_mapper, SLOT (map ())), - "figure (); hist (%1);"); - - connect (plot_mapper, SIGNAL (mapped (const QString&)), - this, SLOT (relay_command (const QString&))); - - QPoint menupos = pt; - menupos.setX (view->verticalHeader ()->width ()); - - // FIXME: What was the intent here? - // setY (view->verticalHeader ()->sectionPosition (index+1) + - // view->verticalHeader ()->sectionSize (index)); - - menu->exec (view->mapToGlobal (menupos)); -} - -void -variable_editor::double_click (const QModelIndex& idx) -{ - QString name = real_var_name (m_tab_widget->currentIndex ()); - - QTableView *table = m_tab_widget->get_edit_view (); - - if (! table) - return; - - variable_editor_model *model - = qobject_cast (table->model ()); - - if (model->requires_sub_editor (idx)) - edit_variable (name + model->subscript_expression (idx), - model->value_at (idx)); } void variable_editor::save (void) { - QString name = real_var_name (m_tab_widget->currentIndex ()); - QString file - = QFileDialog::getSaveFileName (this, - tr ("Save Variable %1 As").arg (name), - ".", 0, 0, - QFileDialog::DontUseNativeDialog); - - // FIXME: Type? binary, float-binary, ascii, text, hdf5, matlab format? - // FIXME: Call octave_value::save_* directly? - - if (! file.isEmpty ()) - emit command_requested (QString ("save (\"%1\", \"%2\");") - .arg (file) - .arg (name)); -} - -void -variable_editor::clearContent (void) -{ - // FIXME: Shift? - - QTableView *view = m_tab_widget->get_edit_view (); - - if (! view) - return; - - QAbstractItemModel *model = view->model (); - QItemSelectionModel *sel = view->selectionModel (); - QList indices = sel->selectedIndexes (); - - // FIXME: Use [] for empty cells? - - for (const auto& idx : indices) - qobject_cast (model)->clear_content (idx); + emit save_signal (); } void variable_editor::cutClipboard (void) { - if (! has_focus ()) - return; - copyClipboard (); - clearContent (); + emit clear_content_signal (); } void variable_editor::copyClipboard (void) { - if (! has_focus ()) - return; - - QTableView *view = m_tab_widget->get_edit_view (); - - if (! view) - return; - - QAbstractItemModel *model = view->model (); - QItemSelectionModel *sel = view->selectionModel (); - QList indices = sel->selectedIndexes (); - qSort (indices); - if (indices.isEmpty ()) - return; - - // Convert selected items into TSV format and copy that. - // Spreadsheet tools should understand that. - - QModelIndex previous = indices.first (); - QString copy = model->data (previous).toString (); - indices.removeFirst (); - foreach (QModelIndex idx, indices) - { - copy.push_back (previous.row () != idx.row () ? '\n' : '\t'); - copy.append (model->data (idx).toString ()); - previous = idx; - } - copy.push_back ('\n'); - - QClipboard *clipboard = QApplication::clipboard (); - clipboard->setText (copy); + emit copy_clipboard_signal (); } void variable_editor::pasteClipboard (void) { - if (! has_focus ()) - return; - - QClipboard *clipboard = QApplication::clipboard (); - QString text = clipboard->text (); - - QTableView *view = m_tab_widget->get_edit_view (); - - if (! view) - return; - - QItemSelectionModel *sel = view->selectionModel (); - QList indices = sel->selectedIndexes (); - - variable_editor_model *model - = static_cast (view->model ()); - - if (indices.isEmpty ()) - { - if (view->size () == QSize (1,1)) - model->setData (view->model ()->index (0,0), text.toDouble ()); - else if (view->size () == QSize (0,0)) - { - model->insertColumn (0); - model->insertRow (0); - model->setData (view->model ()->index (0,0), text.toDouble ()); - } - } - else - { - for (int i = 0; i < indices.size (); i++) - view->model ()->setData (indices[i], text.toDouble ()); - } - - emit updated (); -} - -void variable_editor::pasteTableClipboard (void) -{ - if (! has_focus ()) - return; - - QClipboard *clipboard = QApplication::clipboard (); - QString text = clipboard->text (); - - QTableView *view = m_tab_widget->get_edit_view (); - - if (! view) - return; - - QItemSelectionModel *sel = view->selectionModel (); - QList indices = sel->selectedIndexes (); - - variable_editor_model *model - = static_cast (view->model ()); - - QPoint start, end; - - QPoint tabsize = QPoint (model->rowCount (), model->columnCount ()); - - if (indices.isEmpty ()) - { - start = QPoint (0,0); - end = tabsize; - } - else if (indices.size () == 1) - { - start = QPoint (indices[0].row (), indices[0].column ()); - end = tabsize; - } - else - { - end = QPoint (0,0); - start = tabsize; - - for (int i = 0; i < indices.size (); i++) - { - if (indices[i].column () < start.y ()) - start.setY (indices[i].column ()); - - if (indices[i].column () > end.y ()) - end.setY (indices[i].column ()); - - if (indices[i].row () < start.x ()) - start.setX (indices[i].column ()); - - if (indices[i].row () > end.x ()) - end.setX (indices[i].column ()); - } - } - - int rownum = 0; - int colnum = 0; - - QStringList rows = text.split ('\n'); - for (const auto& row : rows) - { - if (rownum > end.x () - start.x ()) - continue; - - QStringList cols = row.split ('\t'); - if (cols.isEmpty ()) - continue; - - for (const auto& col : cols) - { - if (col.isEmpty ()) - continue; - if (colnum > end.y () - start.y () ) - continue; - - model->setData (model->index (rownum + start.x (), - colnum + start.y ()), - QVariant (col)); - - colnum++; - } - - colnum = 0; - rownum++; - } + emit paste_clipboard_signal (); emit updated (); } void -variable_editor::createVariable (void) +variable_editor::pasteTableClipboard (void) { - // FIXME: Create unnamed1..n if exist ('unnamed', 'var') is true. - - relay_command ("unnamed = %1"); -} - -void -variable_editor::transposeContent (void) -{ - QString name = real_var_name (m_tab_widget->currentIndex ()); - - emit command_requested (QString ("%1 = %1';").arg (name)); + emit paste_table_clipboard_signal (); emit updated (); } void -variable_editor::up (void) +variable_editor::levelUp (void) { - QString name = real_var_name (m_tab_widget->currentIndex ()); - - // FIXME: Is there a better way? - - if (name.endsWith (')') || name.endsWith ('}')) - { - name.remove (QRegExp ("(\\(|\\{)[^({]*(\\)|\\})$")); - edit_variable (name, octave_value ()); - } + emit level_up_signal (); } void -variable_editor::delete_selected (void) +variable_editor::relay_selected_command (const QString& cmd) { - QTableView *view = m_tab_widget->get_edit_view (); - - if (! view) - return; - - QString selection = selected_to_octave (); - QList coords = octave_to_coords (selection); - - if (coords.isEmpty ()) - return; - - bool whole_columns_selected - = coords[0] == 1 && coords[1] == view->model ()->rowCount (); - - bool whole_rows_selected - = coords[2] == 1 && coords[3] == view->model ()->columnCount (); - - // Must be deleting whole columns or whole rows, and not the whole thing. - - if (whole_columns_selected == whole_rows_selected) - return; - - if (whole_rows_selected) - view->model ()->removeRows (coords[0], coords[1] - coords[0]); - - if (whole_columns_selected) - view->model ()->removeColumns (coords[2], coords[3] - coords[2]); - - emit updated (); -} - -void -variable_editor::relay_command (const QString& cmd) -{ - emit command_requested (cmd.arg (selected_to_octave ())); -} - -QList -variable_editor::octave_to_coords (QString& selection) -{ - // FIXME: Is this necessary or would it be quicker to clone the function - // that gives us the QString? - - // Sanity check. - - if (selection.count (",") != 1) - return QList (); - - // FIXME: Why clear if object has just been created? - - QList output; - output.clear (); - - // Remove braces. - - int firstbracket = std::max (selection.indexOf ("("), - selection.indexOf ("{")); - - selection = selection.mid (firstbracket + 1, - selection.length () - (firstbracket + 2)); - - QString rows = selection.left (selection.indexOf (",")); - if (! rows.contains (":")) - { - // Only one row. - - output.push_back (rows.toInt ()); - output.push_back (output.last ()); - } - else - { - output.push_back (rows.left (rows.indexOf (":")).toInt ()); - output.push_back (rows.right (rows.length () - (rows.indexOf (":") + 1)) - .toInt ()); - } - - QString cols; - cols = selection.right (selection.length () - (selection.indexOf (",") + 1)); - if (cols.left (1) == " ") - cols = cols.right (cols.length () - 1); - - if (! cols.contains (":")) - { - // Only one row. - - output.push_back (cols.toInt ()); - output.push_back (output.last ()); - } - else - { - output.push_back (cols.left (cols.indexOf (":")).toInt ()); - output.push_back (cols.right (cols.length () - (cols.indexOf (":") + 1)) - .toInt ()); - } - - return output; -} - -// Return the real variable name from the tab addressed by 'index', -// cleaned of any '&' possibly inserted by KDE. - -QString -variable_editor::real_var_name (int index) -{ - QString var_name = m_tab_widget->tabText (index); - var_name.remove (QChar ('&')); - return var_name; -} - -QString -variable_editor::selected_to_octave (void) -{ - QString name = real_var_name (m_tab_widget->currentIndex ()); - - QTableView *view = m_tab_widget->get_edit_view (); - - if (! view) - return name; - - QItemSelectionModel *sel = view->selectionModel (); - - // Return early if nothing selected. - - if (! sel->hasSelection ()) - return name; - - QList indices = sel->selectedIndexes (); - - // FIXME: Shouldn't this be keyed to octave_idx_type? - - int32_t from_row = std::numeric_limits::max (); - int32_t to_row = 0; - int32_t from_col = std::numeric_limits::max (); - int32_t to_col = 0; - - for (const auto& idx : indices) - { - from_row = std::min (from_row, idx.row ()); - to_row = std::max (to_row, idx.row ()); - from_col = std::min (from_col, idx.column ()); - to_col = std::max (to_col, idx.column ()); - } - - QString rows = idx_to_expr (from_row, to_row); - QString cols = idx_to_expr (from_col, to_col); - - // FIXME: Does cell need separate handling? Maybe use '{.,.}'? - - return QString ("%1(%2, %3)").arg (name).arg (rows).arg (cols); + emit selected_command_signal (cmd); } // Also updates the font. @@ -1491,12 +1376,10 @@ void variable_editor::update_colors (voi + m_table_colors[3].name () +" }"; } - if (m_tab_widget->count () < 1) - return; - - for (int i = 0; i < m_tab_widget->count (); i++) + QList viewlist = findChildren (); + for (int i = 0; i < viewlist.size (); i++) { - QTableView *view = m_tab_widget->get_edit_view (); + QTableView *view = viewlist.at (i); if (! view) continue; @@ -1508,36 +1391,59 @@ void variable_editor::update_colors (voi } +QAction * +variable_editor::add_tool_bar_button (const QIcon &icon, + const QString &text, + const QObject *receiver, + const char *member) +{ + QAction *action = new QAction (icon, text, this); + connect(action, SIGNAL (triggered ()), receiver, member); + QToolButton *button = new ReturnFocusToolButton (m_tool_bar); + button->setDefaultAction (action); + button->setText (text); + button->setToolTip (text); + button->setIcon (icon); + m_tool_bar->addWidget (button); + + return action; +} + void variable_editor::construct_tool_bar (void) { m_tool_bar->setAllowedAreas (Qt::TopToolBarArea); - m_tool_bar->setMovable (false); - m_tool_bar->setObjectName ("VariableEditorToolBar"); m_tool_bar->setWindowTitle (tr ("Variable Editor Toolbar")); - m_tool_bar->addAction (resource_manager::icon ("document-save"), - tr ("Save"), this, SLOT (save ())); + QAction *action; + action = add_tool_bar_button (resource_manager::icon ("document-save"), + tr ("Save"), this, SLOT (save ())); + action->setShortcuts (QKeySequence::Save); + action->setStatusTip(tr("Save variable to a file")); m_tool_bar->addSeparator (); - m_tool_bar->addAction (resource_manager::icon ("edit-cut"), - tr ("Cut"), this, SLOT (cutClipboard ())); + action = add_tool_bar_button (resource_manager::icon ("edit-cut"), + tr ("Cut"), this, SLOT (cutClipboard ())); + action->setStatusTip(tr("Cut data to clipboard")); - m_tool_bar->addAction (resource_manager::icon ("edit-copy"), - tr ("Copy"), this, SLOT (copyClipboard ())); + action = add_tool_bar_button (resource_manager::icon ("edit-copy"), + tr ("Copy"), this, SLOT (copyClipboard ())); + action->setStatusTip(tr("Copy data to clipboard")); - m_tool_bar->addAction (resource_manager::icon ("edit-paste"), - tr ("Paste"), this, SLOT (pasteClipboard ())); + action = add_tool_bar_button (resource_manager::icon ("edit-paste"), + tr ("Paste"), this, SLOT (pasteClipboard ())); + action->setStatusTip(tr("Paste clipboard into variable data")); // FIXME: Different icon for Paste Table? - m_tool_bar->addAction (resource_manager::icon ("edit-paste"), - tr ("Paste Table"), - this, SLOT (pasteTableClipboard ())); + action = add_tool_bar_button (resource_manager::icon ("edit-paste"), + tr ("Paste Table"), + this, SLOT (pasteTableClipboard ())); + action->setStatusTip(tr("Another paste clipboard into variable data")); m_tool_bar->addSeparator (); @@ -1545,50 +1451,26 @@ variable_editor::construct_tool_bar (voi // QAction *print_action; /icons/fileprint.png // m_tool_bar->addSeparator (); - QToolButton *plot_tool_button = new QToolButton (m_tool_bar); + action = new QAction (resource_manager::icon ("plot-xy-curve"), + tr ("Plot"), m_tool_bar); + action->setToolTip (tr ("Plot Selected Data")); + QToolButton *plot_tool_button = new HoverToolButton (m_tool_bar); + plot_tool_button->setDefaultAction (action); plot_tool_button->setText (tr ("Plot")); - plot_tool_button->setToolTip (tr ("Plot Selected Data")); + plot_tool_button->setToolTip (tr ("Plot selected data")); plot_tool_button->setIcon (resource_manager::icon ("plot-xy-curve")); plot_tool_button->setPopupMode (QToolButton::InstantPopup); - QMenu *plot_menu = new QMenu (tr ("Plot"), plot_tool_button); - + QMenu *plot_menu = new ReturnFocusMenu (plot_tool_button); + plot_menu->setTitle (tr ("Plot")); plot_menu->setSeparatorsCollapsible (false); - QSignalMapper *plot_mapper = new QSignalMapper (plot_menu); - - plot_mapper->setMapping - (plot_menu->addAction ("plot", plot_mapper, SLOT (map ())), - "figure (); plot (%1);"); - - plot_mapper->setMapping - (plot_menu->addAction ("bar", plot_mapper, SLOT (map ())), - "figure (); bar (%1);"); - - plot_mapper->setMapping - (plot_menu->addAction ("stem", plot_mapper, SLOT (map ())), - "figure (); stem (%1);"); - - plot_mapper->setMapping - (plot_menu->addAction ("stairs", plot_mapper, SLOT (map ())), - "figure (); stairs (%1);"); - - plot_mapper->setMapping - (plot_menu->addAction ("area", plot_mapper, SLOT (map ())), - "figure (); area (%1);"); - - plot_mapper->setMapping - (plot_menu->addAction ("pie", plot_mapper, SLOT (map ())), - "figure (); pie (%1);"); - - plot_mapper->setMapping - (plot_menu->addAction ("hist", plot_mapper, SLOT (map ())), - "figure (); hist (%1);"); + QSignalMapper *plot_mapper = make_plot_mapper (plot_menu); connect (plot_mapper, SIGNAL (mapped (const QString&)), - this, SLOT (relay_command (const QString&))); + this, SLOT (relay_selected_command (const QString&))); plot_tool_button->setMenu (plot_menu); @@ -1596,8 +1478,41 @@ variable_editor::construct_tool_bar (voi m_tool_bar->addSeparator (); - m_tool_bar->addAction (QIcon (resource_manager::icon ("go-up")), tr ("Up"), - this, SLOT (up ())); + action = add_tool_bar_button (resource_manager::icon ("go-up"), + tr ("Up"), this, SLOT (levelUp ())); + action->setStatusTip(tr("Go one level up in variable hierarchy")); + + // The QToolButton mouse-clicks change active window, so connect all + // HoverToolButton and RuternFocusToolButton objects to the mechanism + // that restores active window and focus before acting. + QList hbuttonlist = m_tool_bar-> + findChildren (QString (), Qt::FindDirectChildrenOnly); + for (int i = 0; i < hbuttonlist.size (); i++) + { + connect (hbuttonlist.at (i), SIGNAL (hovered_signal ()), + this, SLOT (record_hovered_focus_variable ())); + connect (hbuttonlist.at (i), SIGNAL (popup_shown_signal ()), + this, SLOT (restore_hovered_focus_variable ())); + } + QList rfbuttonlist = m_tool_bar-> + findChildren (QString (), Qt::FindDirectChildrenOnly); + for (int i = 0; i < rfbuttonlist.size (); i++) + { + connect (rfbuttonlist.at (i), SIGNAL (about_to_activate ()), + this, SLOT (restore_hovered_focus_variable ())); + } + + // Same for QMenu + QList menulist = m_tool_bar-> + findChildren (QString ()); + for (int i = 0; i < menulist.size (); i++) + { + connect (menulist.at (i), SIGNAL (about_to_activate ()), + this, SLOT (restore_hovered_focus_variable ())); + } + + m_tool_bar->setAttribute(Qt::WA_ShowWithoutActivating); + m_tool_bar->setFocusPolicy (Qt::NoFocus); // Disabled when no tab is present. diff --git a/libgui/src/variable-editor.h b/libgui/src/variable-editor.h --- a/libgui/src/variable-editor.h +++ b/libgui/src/variable-editor.h @@ -34,92 +34,148 @@ along with Octave; see the file COPYING. class octave_value; class QModelIndex; -class QStackedWidget; -class QTabWidget; class QTableView; class QTextEdit; class QToolBar; class variable_editor_model; -class var_editor_tab : public QWidget +// The individual variable subwindow class + +class variable_dock_widget : public label_dock_widget { Q_OBJECT public: - var_editor_tab (QStackedWidget *widget_stack, QWidget *p = nullptr) - : QWidget (p), m_model (nullptr), m_widget_stack (widget_stack), - m_edit_view_idx (-1), m_disp_view_idx (-1) - { } + variable_dock_widget (QWidget *p = nullptr); - ~var_editor_tab (void) = default; +signals: - // No copying! + void command_signal (const QString& cmd); - var_editor_tab (const var_editor_tab&) = delete; + void variable_focused_signal (const QString& name); - var_editor_tab& operator = (const var_editor_tab&) = delete; + void edit_variable_signal (const QString& name, const octave_value& val); - QTableView * get_edit_view (void) const; - QTextEdit * get_disp_view (void) const; +protected: - void set_edit_view (QTableView *); - void set_disp_view (QTextEdit *); - - void set_model (variable_editor_model *model) - { - m_model = model; - } - - bool has_focus (void) const; - - void keyPressEvent (QKeyEvent *event); + virtual void closeEvent (QCloseEvent *e); public slots: - void set_editable (bool); + void set_editable (bool editable); + + void cutClipboard (void); + + void copyClipboard (void); + + void pasteClipboard (void); + + void pasteTableClipboard (void); + + void levelUp (void); + + void clearContent (void); + + void delete_selected (void); + + void createVariable (void); + + void transposeContent (void); + + QList range_selected (void); + + void add_edit_actions (QMenu *menu, const QString& qualifier_string); + + void createContextMenu (const QPoint& pt); + + void createColumnMenu (const QPoint& pt); + + void createRowMenu (const QPoint& pt); void handle_horizontal_scroll_action (int action); void handle_vertical_scroll_action (int action); -private: + void save (void); - variable_editor_model *m_model; + // Convert selection to an Octave expression. + QString selected_to_octave (void); - QStackedWidget *m_widget_stack; + void selected_command_requested (const QString& cmd); - int m_edit_view_idx; - int m_disp_view_idx; + void handle_focus_change (QWidget *old, QWidget *now); + +private slots: + + void change_floating (bool); + + void change_existence (bool); + + void toplevel_change (bool); + +protected: + + bool m_initial_float; }; -// Subclassed QTabWidget for using custom tabbar +// Gadgets to keep track and restore what variable window +// was in focus just prior to selecting something on the +// menu bar -class var_editor_tab_widget : public QTabWidget +class HoverToolButton : public QToolButton { Q_OBJECT public: - var_editor_tab_widget (QWidget *p); + HoverToolButton (QWidget *parent = nullptr); - ~var_editor_tab_widget (void) = default; +signals: - // No copying! + void hovered_signal (void); - var_editor_tab_widget (const var_editor_tab_widget&) = delete; + void popup_shown_signal (void); - var_editor_tab_widget& operator = (const var_editor_tab_widget&) = delete; +protected: - tab_bar * get_tab_bar (void) const; - - bool current_tab_has_focus (void) const; - - QTextEdit *get_disp_view (void) const; - QTableView *get_edit_view (void) const; + bool eventFilter (QObject *obj, QEvent *ev); }; +class ReturnFocusToolButton : public HoverToolButton +{ + Q_OBJECT + +public: + + ReturnFocusToolButton (QWidget *parent = nullptr); + +signals: + + void about_to_activate (void); + +protected: + + bool eventFilter (QObject *obj, QEvent *ev); +}; + +class ReturnFocusMenu : public QMenu +{ + Q_OBJECT + +public: + + ReturnFocusMenu (QWidget *parent = nullptr); + +signals: + + void about_to_activate (void); + +protected: + + bool eventFilter (QObject *obj, QEvent *ev); +}; // The variable editor class @@ -139,50 +195,40 @@ public: variable_editor& operator = (const variable_editor&) = delete; - void edit_variable (const QString& name, const octave_value& val); - - QTableView *make_edit_view (var_editor_tab *page, + QTableView *make_edit_view (QWidget *parent, variable_editor_model *model); - QTextEdit *make_disp_view (var_editor_tab *page, + QTextEdit *make_disp_view (QWidget *parent, variable_editor_model *model); void refresh (void); - bool has_focus (void); - static QList default_colors (void); static QStringList color_names (void); public slots: - void callUpdate (const QModelIndex&,const QModelIndex&); + void callUpdate (const QModelIndex&, const QModelIndex&); void notice_settings (const QSettings *); + void edit_variable (const QString& name, const octave_value& val); + + void variable_destroyed (QObject *obj); + + void variable_focused (const QString& name); + + void record_hovered_focus_variable (void); + + void restore_hovered_focus_variable (void); + protected slots: - void request_close_tab (bool); - void request_close_other_tabs (bool); - void request_close_all_tabs (bool); - void closeEvent (QCloseEvent *); - void closeTab (int idx); - - void contextmenu_requested (const QPoint& pt); - - void columnmenu_requested (const QPoint& pt); - - void rowmenu_requested (const QPoint& pt); - - void double_click (const QModelIndex& idx); - void save (void); - void clearContent (void); - void cutClipboard (void); void copyClipboard (void); @@ -191,17 +237,11 @@ protected slots: void pasteTableClipboard (void); - void createVariable (void); - - void transposeContent (void); - - void up (void); - - void delete_selected (void); + void levelUp (void); // Send command to Octave interpreter. // %1 in CMD is replaced with the value of selected_to_octave. - void relay_command (const QString& cmd); + void relay_selected_command (const QString& cmd); signals: @@ -209,25 +249,41 @@ signals: void finished (void); - void command_requested (const QString& cmd); + void command_signal (const QString& cmd); void refresh_signal (void); + void clear_content_signal (void); + + void copy_clipboard_signal (void); + + void paste_clipboard_signal (void); + + void paste_table_clipboard_signal (void); + + void level_up_signal (void); + + void context_menu_signal (const QPoint& pt); + + void column_menu_signal (const QPoint& pt); + + void row_menu_signal (const QPoint& pt); + + void save_signal (void); + + void delete_selected_signal (void); + + void selected_command_signal (const QString& cmd); + private: QAction * add_action (QMenu *menu, const QIcon& icon, const QString& text, const char *member); - void enable_actions (void); - - QWidget *m_container; + QMainWindow *m_main; QToolBar *m_tool_bar; - var_editor_tab_widget *m_tab_widget; - - tab_bar *m_tab_bar; - int m_default_width; int m_default_height; @@ -248,21 +304,16 @@ private: QList m_table_colors; - QAction *m_close_action; - QAction *m_close_others_action; - QAction *m_close_all_action; - - QList octave_to_coords (QString&); - - // Get the real variable name from the tab text - QString real_var_name (int index); - - // Convert selection to an Octave expression. - QString selected_to_octave (void); - void update_colors (void); + QAction *add_tool_bar_button (const QIcon &icon, const QString &text, + const QObject *receiver, const char *member); + void construct_tool_bar (void); + + QString m_current_focus_vname; + + QString m_hovered_focus_vname; }; #endif