changeset 19597:a402493c0605 gui-release

provide the same filter for history view as for workspace view * history_dock_widget.cc (constructor): combo-box for filter instead of line edit, new check box for enable/disable filter, connect related signals, get last state of filter and check box from settings file; (destructor): new, write state of filter and check box into settings file; (filter_activate): new slot enabling or disabling the filter; (update_filter_history): new slot for saving the filter expression when its edtitign has finished or when it was chosen from the combo box selection list (copyClipboard, pasteClipboard, selectAll): line edit is replaced by a combo box with a line edit * history_dock_widget.h: new destructor, slots filter_activate and update_filter_history, new class variables for check box, filter combo box
author Torsten <ttl@justmail.de>
date Sun, 11 Jan 2015 15:47:37 +0100
parents 010cef260698
children 6a9bde1ba0ea
files libgui/src/history-dock-widget.cc libgui/src/history-dock-widget.h
diffstat 2 files changed, 99 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/libgui/src/history-dock-widget.cc	Sat Jan 10 13:31:24 2015 +0100
+++ b/libgui/src/history-dock-widget.cc	Sun Jan 11 15:47:37 2015 +0100
@@ -30,7 +30,11 @@
 #include <QMenu>
 #include <QScrollBar>
 #include <QDesktopWidget>
+#include <QCompleter>
+#include <QLabel>
+
 #include "error.h"
+#include "resource-manager.h"
 
 #include "cmd-hist.h"
 
@@ -71,29 +75,58 @@
            SIGNAL (customContextMenuRequested (const QPoint &)), this,
            SLOT (ctxMenu (const QPoint &)));
 
-  _filter_line_edit = new QLineEdit (this);
-  _filter_line_edit->setStatusTip (
-    tr ("Enter text to filter the command history."));
-  QVBoxLayout *vbox_layout = new QVBoxLayout ();
+  _filter = new QComboBox (this);
+  _filter->setToolTip (tr ("Enter text to filter the command history"));
+  _filter->setEditable (true);
+  _filter->setMaxCount (MaxFilterHistory);
+  _filter->setInsertPolicy (QComboBox::NoInsert);
+  _filter->setSizeAdjustPolicy (
+            QComboBox::AdjustToMinimumContentsLengthWithIcon);
+  QSizePolicy sizePol (QSizePolicy::Expanding, QSizePolicy::Maximum);
+  _filter->setSizePolicy (sizePol);
+  _filter->completer ()->setCaseSensitivity (Qt::CaseSensitive);
+
+  QLabel *filter_label = new QLabel (tr ("Filter"));
+
+  _filter_checkbox = new QCheckBox ();
 
   setWindowIcon (QIcon (":/actions/icons/logo.png"));
   set_title (tr ("Command History"));
   setWidget (new QWidget ());
 
+  QVBoxLayout *vbox_layout = new QVBoxLayout ();
+  QHBoxLayout *hbox_layout = new QHBoxLayout ();
+  hbox_layout->addWidget (filter_label);
+  hbox_layout->addWidget (_filter_checkbox);
+  hbox_layout->addWidget (_filter);
+  vbox_layout->addLayout (hbox_layout);
   vbox_layout->addWidget (_history_list_view);
-  vbox_layout->addWidget (_filter_line_edit);
   vbox_layout->setMargin (2);
 
   widget ()->setLayout (vbox_layout);
 
-  connect (_filter_line_edit, SIGNAL (textEdited (QString)),
-           &_sort_filter_proxy_model, SLOT (setFilterWildcard (QString)));
+  setFocusProxy (_filter->lineEdit ());
+
+  // Init state of the filter
+  QSettings *settings = resource_manager::get_settings ();
+  _filter->addItems (settings->value ("history_dock_widget/mru_list").toStringList ());
+
+  bool filter_state =
+            settings->value ("history_dock_widget/filter_active", false).toBool ();
+  _filter_checkbox->setChecked (filter_state);
+  filter_activate (filter_state);
+
+  // Connect signals and slots
+  connect (_filter, SIGNAL (editTextChanged (const QString&)),
+           &_sort_filter_proxy_model, SLOT (setFilterWildcard (const QString&)));
+  connect (_filter_checkbox, SIGNAL (toggled (bool)),
+           this, SLOT (filter_activate (bool)));
+  connect (_filter->lineEdit (), SIGNAL (editingFinished ()),
+           this, SLOT (update_filter_history ()));
 
   connect (_history_list_view, SIGNAL (doubleClicked (QModelIndex)),
            this, SLOT (handle_double_click (QModelIndex)));
 
-  setFocusProxy (_filter_line_edit);
-
   // shrink max. displayed entry size to desktop width
   QSize screen = QDesktopWidget ().screenGeometry ().size ();
   int w = screen.width ();
@@ -103,6 +136,46 @@
   _history_list_view->setTextElideMode (Qt::ElideRight);
 }
 
+history_dock_widget::~history_dock_widget ()
+{
+  QSettings *settings = resource_manager::get_settings ();
+
+  settings->setValue ("history_dock_widget/filter_active",
+                      _filter_checkbox->isChecked ());
+
+  QStringList mru;
+  for (int i = 0; i < _filter->count (); i++)
+    mru.append (_filter->itemText (i));
+  settings->setValue ("history_dock_widget/mru_list", mru);
+
+  settings->sync ();
+}
+
+void
+history_dock_widget::filter_activate (bool state)
+{
+  _filter->setEnabled (state);
+  _sort_filter_proxy_model.setDynamicSortFilter (state);
+
+  if (state)
+    _sort_filter_proxy_model.setFilterWildcard (_filter->currentText ());
+  else
+    _sort_filter_proxy_model.setFilterWildcard (QString ());
+}
+
+void
+history_dock_widget::update_filter_history ()
+{
+  QString text = _filter->currentText ();   // get current text
+  int index = _filter->findText (text);     // and its actual index
+
+  if (index > -1)
+    _filter->removeItem (index);    // remove if already existing
+
+  _filter->insertItem (0, text);    // (re)insert at beginning
+  _filter->setCurrentIndex (0);
+}
+
 void history_dock_widget::ctxMenu (const QPoint &xpos)
 {
   QMenu menu (this);
@@ -211,31 +284,31 @@
 {
   if (_history_list_view->hasFocus ())
     handle_contextmenu_copy (true);
-  if (_filter_line_edit->hasFocus () && _filter_line_edit->hasSelectedText ())
+  if (_filter->lineEdit ()->hasFocus () && _filter->lineEdit ()->hasSelectedText ())
     {
       QClipboard *clipboard = QApplication::clipboard ();
-      clipboard->setText ( _filter_line_edit->selectedText ());
+      clipboard->setText ( _filter->lineEdit ()->selectedText ());
     }
 }
 
 void
 history_dock_widget::pasteClipboard ()
 {
-  if (_filter_line_edit->hasFocus ())
+  if (_filter->lineEdit ()->hasFocus ())
     {
       QClipboard *clipboard = QApplication::clipboard ();
       QString str =  clipboard->text ();
       if (str.length () > 0)
-        _filter_line_edit->insert (str);
+        _filter->lineEdit ()->insert (str);
     }
 }
 
 void
 history_dock_widget::selectAll ()
 {
-  if (_filter_line_edit->hasFocus ())
+  if (_filter->lineEdit ()->hasFocus ())
     {
-        _filter_line_edit->selectAll ();
+        _filter->lineEdit ()->selectAll ();
     }
   if (_history_list_view->hasFocus ())
     {
--- a/libgui/src/history-dock-widget.h	Sat Jan 10 13:31:24 2015 +0100
+++ b/libgui/src/history-dock-widget.h	Sun Jan 11 15:47:37 2015 +0100
@@ -27,6 +27,9 @@
 #include <QListView>
 #include <QSortFilterProxyModel>
 #include <QStringListModel>
+#include <QComboBox>
+#include <QCheckBox>
+
 #include "octave-dock-widget.h"
 
 class history_dock_widget : public octave_dock_widget
@@ -36,6 +39,7 @@
 public:
 
   history_dock_widget (QWidget *parent = 0);
+  ~history_dock_widget ();
 
 public slots:
 
@@ -56,6 +60,9 @@
 
 private slots:
 
+  void update_filter_history ();
+  void filter_activate (bool enable);
+
   void handle_double_click (QModelIndex modelIndex);
   void handle_contextmenu_copy (bool flag);
   void handle_contextmenu_evaluate (bool flag);
@@ -70,11 +77,14 @@
 
   void construct ();
   QListView *_history_list_view;
-  QLineEdit *_filter_line_edit;
   QSortFilterProxyModel _sort_filter_proxy_model;
 
   /** Stores the current history_model. */
   QStringListModel *_history_model;
+
+  QCheckBox *_filter_checkbox;
+  QComboBox *_filter;
+  enum { MaxFilterHistory = 10 };
 };
 
 #endif // HISTORYDOCKWIDGET_H