GNU Octave  4.2.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
history-dock-widget.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2011-2017 Jacob Dawid
4 
5 This file is part of Octave.
6 
7 Octave is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with Octave; see the file COPYING. If not, see
19 <http://www.gnu.org/licenses/>.
20 
21 */
22 
23 #if defined (HAVE_CONFIG_H)
24 # include "config.h"
25 #endif
26 
27 #include <QApplication>
28 #include <QClipboard>
29 #include <QVBoxLayout>
30 #include <QMenu>
31 #include <QScrollBar>
32 #include <QDesktopWidget>
33 #include <QCompleter>
34 #include <QLabel>
35 
36 #include "error.h"
37 #include "resource-manager.h"
38 
39 #include "cmd-hist.h"
40 
41 #include "history-dock-widget.h"
42 
44  : octave_dock_widget (p)
45 {
46  setObjectName ("HistoryDockWidget");
47  setStatusTip (tr ("Browse and search the command history."));
48 
49  connect (this, SIGNAL (command_create_script (const QString&)),
50  p, SLOT (new_file (const QString&)));
51 
52  connect (this, SIGNAL (information (const QString&)),
53  p, SLOT (report_status_message (const QString&)));
54 
55  connect (this, SIGNAL (command_double_clicked (const QString&)),
56  p, SLOT (execute_command_in_terminal (const QString&)));
57 
58  construct ();
59 }
60 
61 void
63 {
64  _history_model = new QStringListModel ();
66  _history_list_view = new QListView (this);
68  _history_list_view->setAlternatingRowColors (true);
69  _history_list_view->setEditTriggers (QAbstractItemView::NoEditTriggers);
70  _history_list_view->setStatusTip (
71  tr ("Double-click a command to transfer it to the terminal."));
72  _history_list_view->setSelectionMode (QAbstractItemView::ExtendedSelection);
73  _history_list_view->setContextMenuPolicy (Qt::CustomContextMenu);
74  connect (_history_list_view,
75  SIGNAL (customContextMenuRequested (const QPoint &)), this,
76  SLOT (ctxMenu (const QPoint &)));
77 
78  _filter = new QComboBox (this);
79  _filter->setToolTip (tr ("Enter text to filter the command history"));
80  _filter->setEditable (true);
81  _filter->setMaxCount (MaxFilterHistory);
82  _filter->setInsertPolicy (QComboBox::NoInsert);
83  _filter->setSizeAdjustPolicy (
84  QComboBox::AdjustToMinimumContentsLengthWithIcon);
85  QSizePolicy sizePol (QSizePolicy::Expanding, QSizePolicy::Preferred);
86  _filter->setSizePolicy (sizePol);
87  _filter->completer ()->setCaseSensitivity (Qt::CaseSensitive);
88 
89  QLabel *filter_label = new QLabel (tr ("Filter"));
90 
91  _filter_checkbox = new QCheckBox ();
92 
93  setWindowIcon (QIcon (":/actions/icons/logo.png"));
94  set_title (tr ("Command History"));
95  setWidget (new QWidget ());
96 
97  _filter_widget = new QWidget (this);
98  QHBoxLayout *filter_layout = new QHBoxLayout ();
99  filter_layout->addWidget (filter_label);
100  filter_layout->addWidget (_filter_checkbox);
101  filter_layout->addWidget (_filter);
102  filter_layout->setMargin(0);
103  _filter_widget->setLayout (filter_layout);
104 
105  QVBoxLayout *hist_layout = new QVBoxLayout ();
106  hist_layout->addWidget (_filter_widget);
107  hist_layout->addWidget (_history_list_view);
108 
109  hist_layout->setMargin (2);
110  widget ()->setLayout (hist_layout);
111 
112  // Init state of the filter
113  QSettings *settings = resource_manager::get_settings ();
114 
116  = settings->value ("history_dock_widget/filter_shown",true).toBool();
117  _filter_widget->setVisible (_filter_shown);
118 
119  _filter->addItems (settings->value ("history_dock_widget/mru_list").toStringList ());
120 
121  bool filter_state
122  = settings->value ("history_dock_widget/filter_active", false).toBool ();
123  _filter_checkbox->setChecked (filter_state);
124  filter_activate (filter_state);
125 
126  // Connect signals and slots
127  connect (_filter, SIGNAL (editTextChanged (const QString&)),
129  SLOT (setFilterWildcard (const QString&)));
130  connect (_filter_checkbox, SIGNAL (toggled (bool)),
131  this, SLOT (filter_activate (bool)));
132  connect (_filter->lineEdit (), SIGNAL (editingFinished ()),
133  this, SLOT (update_filter_history ()));
134 
135  connect (_history_list_view, SIGNAL (doubleClicked (QModelIndex)),
136  this, SLOT (handle_double_click (QModelIndex)));
137 
138  // shrink max. displayed entry size to desktop width
139  QSize screen = QDesktopWidget ().screenGeometry ().size ();
140  int w = screen.width ();
141  QFontMetrics fm = _history_list_view->fontMetrics ();
142  int h = fm.height ();
143  _history_list_view->setGridSize (QSize (w,h));
144  _history_list_view->setTextElideMode (Qt::ElideRight);
145 }
146 
147 void
149 {
150  QSettings *settings = resource_manager::get_settings ();
151 
152  if (! settings)
153  return;
154 
155  settings->setValue ("history_dock_widget/filter_active",
156  _filter_checkbox->isChecked ());
157  settings->setValue ("history_dock_widget/filter_shown", _filter_shown);
158 
159  QStringList mru;
160  for (int i = 0; i < _filter->count (); i++)
161  mru.append (_filter->itemText (i));
162  settings->setValue ("history_dock_widget/mru_list", mru);
163 
164  settings->sync ();
165 
167 }
168 
169 void
171 {
172  _filter->setEnabled (state);
173  _sort_filter_proxy_model.setDynamicSortFilter (state);
174 
175  if (state)
176  _sort_filter_proxy_model.setFilterWildcard (_filter->currentText ());
177  else
178  _sort_filter_proxy_model.setFilterWildcard (QString ());
179 }
180 
181 void
183 {
184  QString text = _filter->currentText (); // get current text
185  int index = _filter->findText (text); // and its actual index
186 
187  if (index > -1)
188  _filter->removeItem (index); // remove if already existing
189 
190  _filter->insertItem (0, text); // (re)insert at beginning
191  _filter->setCurrentIndex (0);
192 }
193 
194 void history_dock_widget::ctxMenu (const QPoint &xpos)
195 {
196  QMenu menu (this);
197 
198  QModelIndex index = _history_list_view->indexAt (xpos);
199 
200  if (index.isValid () && index.column () == 0)
201  {
202  menu.addAction (resource_manager::icon ("edit-copy"),
203  tr ("Copy"), this, SLOT (handle_contextmenu_copy (bool)));
204  menu.addAction (tr ("Evaluate"), this,
205  SLOT (handle_contextmenu_evaluate (bool)));
206  menu.addAction (resource_manager::icon ("document-new"),
207  tr ("Create script"), this,
208  SLOT (handle_contextmenu_create_script (bool)));
209  }
210  if (_filter_shown)
211  menu.addAction (tr ("Hide filter"), this,
212  SLOT (handle_contextmenu_filter ()));
213  else
214  menu.addAction (tr ("Show filter"), this,
215  SLOT (handle_contextmenu_filter ()));
216 
217  menu.exec (_history_list_view->mapToGlobal (xpos));
218 }
219 
221 {
222  QString text;
223  QItemSelectionModel *selectionModel = _history_list_view->selectionModel ();
224  QModelIndexList rows = selectionModel->selectedRows ();
225  QModelIndexList::iterator it;
226  bool prev_valid_row = false;
227  for (it = rows.begin (); it != rows.end (); it++)
228  {
229  if ((*it).isValid ())
230  {
231  if (prev_valid_row)
232  text += "\n";
233  text += (*it).data ().toString ();
234  prev_valid_row = true;
235  }
236  }
237  QApplication::clipboard ()->setText (text);
238 }
239 
241 {
242  QItemSelectionModel *selectionModel = _history_list_view->selectionModel ();
243  QModelIndexList rows = selectionModel->selectedRows ();
244  QModelIndexList::iterator it;
245  for (it = rows.begin () ; it != rows.end (); it++)
246  {
247  if ((*it).isValid ())
248  emit command_double_clicked ((*it).data ().toString ());
249  }
250 }
251 
252 void
254 {
255  QString text;
256  QItemSelectionModel *selectionModel = _history_list_view->selectionModel ();
257  QModelIndexList rows = selectionModel->selectedRows ();
258 
259  bool prev_valid_row = false;
260  for (QModelIndexList::iterator it = rows.begin (); it != rows.end (); it++)
261  {
262  if ((*it).isValid ())
263  {
264  if (prev_valid_row)
265  text += "\n";
266  text += (*it).data ().toString ();
267  prev_valid_row = true;
268  }
269  }
270 
271  if (text.length () > 0)
272  emit command_create_script (text);
273 }
274 
275 void
277 {
279  _filter_widget->setVisible (_filter_shown);
280 }
281 
282 void
284 {
285  emit command_double_clicked (modelIndex.data ().toString ());
286 }
287 
288 void
289 history_dock_widget::set_history (const QStringList& hist)
290 {
291  _history_model->setStringList (hist);
292  _history_list_view->scrollToBottom ();
293 }
294 
295 void
296 history_dock_widget::append_history (const QString& hist_entry)
297 {
298  QStringList lst = _history_model->stringList ();
299  lst.append (hist_entry);
300 
301  QScrollBar *scroll_bar = _history_list_view->verticalScrollBar ();
302 
303  bool at_bottom = scroll_bar->maximum () - scroll_bar->value () < 1;
304 
305  _history_model->setStringList (lst);
306 
307  // Scroll if slider position at bottom.
308  if (at_bottom)
309  _history_list_view->scrollToBottom ();
310 }
311 
312 void
314 {
315  _history_model->setStringList (QStringList ());
316 }
317 
318 void
320 {
321  if (_history_list_view->hasFocus ())
323  if (_filter->lineEdit ()->hasFocus ()
324  && _filter->lineEdit ()->hasSelectedText ())
325  {
326  QClipboard *clipboard = QApplication::clipboard ();
327  clipboard->setText (_filter->lineEdit ()->selectedText ());
328  }
329 }
330 
331 void
333 {
334  if (_filter->lineEdit ()->hasFocus ())
335  {
336  QClipboard *clipboard = QApplication::clipboard ();
337  QString str = clipboard->text ();
338  if (str.length () > 0)
339  _filter->lineEdit ()->insert (str);
340  }
341 }
342 
343 void
345 {
346  if (_filter->lineEdit ()->hasFocus ())
347  {
348  _filter->lineEdit ()->selectAll ();
349  }
350  if (_history_list_view->hasFocus ())
351  {
352  _history_list_view->selectAll ();
353  }
354 }
355 
357 {
359 
360  if (visible)
361  {
362  int filter_state = _filter_checkbox->isChecked ();
363  filter_activate (filter_state);
364  }
365 }
void set_title(const QString &)
QStringListModel * _history_model
Stores the current history_model.
void set_history(const QStringList &hist)
history_dock_widget(QWidget *parent=0)
void ctxMenu(const QPoint &pos)
void handle_double_click(QModelIndex modelIndex)
virtual void handle_visibility(bool visible)
double h
Definition: graphics.cc:11205
QListView * _history_list_view
void information(const QString &message)
std::complex< double > w(std::complex< double > z, double relerr=0)
void handle_contextmenu_copy(bool flag)
std::string str
Definition: hash.cc:118
static QSettings * get_settings(void)
void command_double_clicked(const QString &command)
Emitted, whenever the user double-clicked a command in the history.
void command_create_script(const QString &commands)
Emitted whenever the user selects command and chooses Create script from popupmenu.
void append_history(const QString &hist_entry)
static uint32_t state[624]
Definition: randmtzig.cc:184
OCTAVE_EXPORT octave_value_list the first data row corresponds to an index of zero The a spreadsheet style form such as the file is read until end of file is reached The such as text
Definition: dlmread.cc:191
=val(i)}if ode{val(i)}occurs in table i
Definition: lookup.cc:239
p
Definition: lu.cc:138
void filter_activate(bool enable)
void handle_contextmenu_filter(void)
static QIcon icon(const QString &icon_name, bool fallback=true)
virtual void handle_visibility(bool visible)
void handle_contextmenu_create_script(bool flag)
QSortFilterProxyModel _sort_filter_proxy_model
void handle_contextmenu_evaluate(bool flag)