GNU Octave  4.4.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
history-dock-widget.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2011-2018 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
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License 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 <https://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 <QCompleter>
33 #include <QLabel>
34 
35 #include "error.h"
36 #include "resource-manager.h"
37 
38 #include "cmd-hist.h"
39 
40 #include "history-dock-widget.h"
41 
42 namespace octave
43 {
46  {
47  setObjectName ("HistoryDockWidget");
48  setStatusTip (tr ("Browse and search the command history."));
49 
50  connect (this, SIGNAL (command_create_script (const QString&)),
51  p, SLOT (new_file (const QString&)));
52 
53  connect (this, SIGNAL (information (const QString&)),
54  p, SLOT (report_status_message (const QString&)));
55 
56  connect (this, SIGNAL (command_double_clicked (const QString&)),
57  p, SLOT (execute_command_in_terminal (const QString&)));
58 
59  construct ();
60  }
61 
62  void history_dock_widget::set_history (const QStringList& hist)
63  {
64  m_history_model->setStringList (hist);
65  m_history_list_view->scrollToBottom ();
66  }
67 
68  void history_dock_widget::append_history (const QString& hist_entry)
69  {
70  QStringList lst = m_history_model->stringList ();
71  lst.append (hist_entry);
72 
73  QScrollBar *scroll_bar = m_history_list_view->verticalScrollBar ();
74 
75  bool at_bottom = scroll_bar->maximum () - scroll_bar->value () < 1;
76 
77  m_history_model->setStringList (lst);
78 
79  // Scroll if slider position at bottom.
80  if (at_bottom)
81  m_history_list_view->scrollToBottom ();
82  }
83 
85  {
86  m_history_model->setStringList (QStringList ());
87  }
88 
90  {
91  QSettings *settings = resource_manager::get_settings ();
92 
93  if (! settings)
94  return;
95 
96  settings->setValue ("history_dock_widget/filter_active",
97  m_filter_checkbox->isChecked ());
98  settings->setValue ("history_dock_widget/filter_shown", m_filter_shown);
99 
100  QStringList mru;
101  for (int i = 0; i < m_filter->count (); i++)
102  mru.append (m_filter->itemText (i));
103  settings->setValue ("history_dock_widget/mru_list", mru);
104 
105  settings->sync ();
106 
108  }
109 
111  {
112  QString text = m_filter->currentText (); // get current text
113  int index = m_filter->findText (text); // and its actual index
114 
115  if (index > -1)
116  m_filter->removeItem (index); // remove if already existing
117 
118  m_filter->insertItem (0, text); // (re)insert at beginning
119  m_filter->setCurrentIndex (0);
120  }
121 
123  {
124  m_filter->setEnabled (state);
125  m_sort_filter_proxy_model.setDynamicSortFilter (state);
126 
127  if (state)
128  m_sort_filter_proxy_model.setFilterWildcard (m_filter->currentText ());
129  else
130  m_sort_filter_proxy_model.setFilterWildcard (QString ());
131  }
132 
133  void history_dock_widget::ctxMenu (const QPoint& xpos)
134  {
135  QMenu menu (this);
136 
137  QModelIndex index = m_history_list_view->indexAt (xpos);
138 
139  if (index.isValid () && index.column () == 0)
140  {
141  menu.addAction (resource_manager::icon ("edit-copy"),
142  tr ("Copy"), this, SLOT (handle_contextmenu_copy (bool)));
143  menu.addAction (tr ("Evaluate"), this,
144  SLOT (handle_contextmenu_evaluate (bool)));
145  menu.addAction (resource_manager::icon ("document-new"),
146  tr ("Create script"), this,
147  SLOT (handle_contextmenu_create_script (bool)));
148  }
149  if (m_filter_shown)
150  menu.addAction (tr ("Hide filter"), this,
151  SLOT (handle_contextmenu_filter ()));
152  else
153  menu.addAction (tr ("Show filter"), this,
154  SLOT (handle_contextmenu_filter ()));
155 
156  menu.exec (m_history_list_view->mapToGlobal (xpos));
157  }
158 
159  void history_dock_widget::handle_double_click (QModelIndex modelIndex)
160  {
161  emit command_double_clicked (modelIndex.data ().toString ());
162  }
163 
165  {
166  QString text;
167  QItemSelectionModel *selectionModel = m_history_list_view->selectionModel ();
168  QModelIndexList rows = selectionModel->selectedRows ();
169  QModelIndexList::iterator it;
170  bool prev_valid_row = false;
171  for (it = rows.begin (); it != rows.end (); it++)
172  {
173  if ((*it).isValid ())
174  {
175  if (prev_valid_row)
176  text += '\n';
177  text += (*it).data ().toString ();
178  prev_valid_row = true;
179  }
180  }
181  QApplication::clipboard ()->setText (text);
182  }
183 
185  {
186  QItemSelectionModel *selectionModel = m_history_list_view->selectionModel ();
187  QModelIndexList rows = selectionModel->selectedRows ();
188  QModelIndexList::iterator it;
189  for (it = rows.begin () ; it != rows.end (); it++)
190  {
191  if ((*it).isValid ())
192  emit command_double_clicked ((*it).data ().toString ());
193  }
194  }
195 
197  {
198  QString text;
199  QItemSelectionModel *selectionModel = m_history_list_view->selectionModel ();
200  QModelIndexList rows = selectionModel->selectedRows ();
201 
202  bool prev_valid_row = false;
203  for (QModelIndexList::iterator it = rows.begin (); it != rows.end (); it++)
204  {
205  if ((*it).isValid ())
206  {
207  if (prev_valid_row)
208  text += '\n';
209  text += (*it).data ().toString ();
210  prev_valid_row = true;
211  }
212  }
213 
214  if (text.length () > 0)
216  }
217 
219  {
221  m_filter_widget->setVisible (m_filter_shown);
222  }
223 
225  {
226  if (m_history_list_view->hasFocus ())
228  if (m_filter->lineEdit ()->hasFocus ()
229  && m_filter->lineEdit ()->hasSelectedText ())
230  {
231  QClipboard *clipboard = QApplication::clipboard ();
232  clipboard->setText (m_filter->lineEdit ()->selectedText ());
233  }
234  }
235 
237  {
238  if (m_filter->lineEdit ()->hasFocus ())
239  {
240  QClipboard *clipboard = QApplication::clipboard ();
241  QString str = clipboard->text ();
242  if (str.length () > 0)
243  m_filter->lineEdit ()->insert (str);
244  }
245  }
246 
248  {
249  if (m_filter->lineEdit ()->hasFocus ())
250  m_filter->lineEdit ()->selectAll ();
251 
252  if (m_history_list_view->hasFocus ())
253  m_history_list_view->selectAll ();
254  }
255 
257  {
259 
260  if (visible)
261  {
262  int filter_state = m_filter_checkbox->isChecked ();
263  filter_activate (filter_state);
264  }
265  }
266 
268  {
269  m_history_model = new QStringListModel ();
271  m_history_list_view = new QListView (this);
273  m_history_list_view->setAlternatingRowColors (true);
274  m_history_list_view->setEditTriggers (QAbstractItemView::NoEditTriggers);
275  m_history_list_view->setStatusTip (
276  tr ("Double-click a command to transfer it to the terminal."));
277  m_history_list_view->setSelectionMode (QAbstractItemView::ExtendedSelection);
278  m_history_list_view->setContextMenuPolicy (Qt::CustomContextMenu);
279  connect (m_history_list_view,
280  SIGNAL (customContextMenuRequested (const QPoint &)), this,
281  SLOT (ctxMenu (const QPoint &)));
282 
283  m_filter = new QComboBox (this);
284  m_filter->setToolTip (tr ("Enter text to filter the command history"));
285  m_filter->setEditable (true);
286  m_filter->setMaxCount (MaxFilterHistory);
287  m_filter->setInsertPolicy (QComboBox::NoInsert);
288  m_filter->setSizeAdjustPolicy (
289  QComboBox::AdjustToMinimumContentsLengthWithIcon);
290  QSizePolicy sizePol (QSizePolicy::Expanding, QSizePolicy::Preferred);
291  m_filter->setSizePolicy (sizePol);
292  m_filter->completer ()->setCaseSensitivity (Qt::CaseSensitive);
293 
294  QLabel *filter_label = new QLabel (tr ("Filter"));
295 
296  m_filter_checkbox = new QCheckBox ();
297 
298  setWindowIcon (QIcon (":/actions/icons/logo.png"));
299  set_title (tr ("Command History"));
300  setWidget (new QWidget ());
301 
302  m_filter_widget = new QWidget (this);
303  QHBoxLayout *filter_layout = new QHBoxLayout ();
304  filter_layout->addWidget (filter_label);
305  filter_layout->addWidget (m_filter_checkbox);
306  filter_layout->addWidget (m_filter);
307  filter_layout->setMargin(0);
308  m_filter_widget->setLayout (filter_layout);
309 
310  QVBoxLayout *hist_layout = new QVBoxLayout ();
311  hist_layout->addWidget (m_filter_widget);
312  hist_layout->addWidget (m_history_list_view);
313 
314  hist_layout->setMargin (2);
315  widget ()->setLayout (hist_layout);
316 
317  // Init state of the filter
318  QSettings *settings = resource_manager::get_settings ();
319 
321  = settings->value ("history_dock_widget/filter_shown",true).toBool ();
322  m_filter_widget->setVisible (m_filter_shown);
323 
324  m_filter->addItems (
325  settings->value ("history_dock_widget/mru_list").toStringList ());
326 
327  bool filter_state
328  = settings->value ("history_dock_widget/filter_active", false).toBool ();
329  m_filter_checkbox->setChecked (filter_state);
330  filter_activate (filter_state);
331 
332  // Connect signals and slots
333  connect (m_filter, SIGNAL (editTextChanged (const QString&)),
335  SLOT (setFilterWildcard (const QString&)));
336  connect (m_filter_checkbox, SIGNAL (toggled (bool)),
337  this, SLOT (filter_activate (bool)));
338  connect (m_filter->lineEdit (), SIGNAL (editingFinished (void)),
339  this, SLOT (updatem_filter_history (void)));
340 
341  connect (m_history_list_view, SIGNAL (doubleClicked (QModelIndex)),
342  this, SLOT (handle_double_click (QModelIndex)));
343 
344  m_history_list_view->setTextElideMode (Qt::ElideRight);
345  }
346 }
void handle_contextmenu_evaluate(bool flag)
void command_create_script(const QString &commands)
Signale emitted, whenever the user selects commands and chooses "Create script" from the popup menu...
void information(const QString &message)
virtual void handle_visibility(bool visible)
void command_double_clicked(const QString &command)
Signal emitted, whenever the user double-clicked a command in the history.
virtual void handle_visibility(bool visible)
QSortFilterProxyModel m_sort_filter_proxy_model
history_dock_widget(QWidget *parent=nullptr)
void handle_double_click(QModelIndex modelIndex)
std::string str
Definition: hash.cc:118
void set_title(const QString &)
void ctxMenu(const QPoint &pos)
static uint32_t state[624]
Definition: randmtzig.cc:183
static QIcon icon(const QString &icon_name, bool fallback=true)
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:194
p
Definition: lu.cc:138
void set_history(const QStringList &hist)
static QSettings * get_settings(void)
for i
Definition: data.cc:5264
void append_history(const QString &hist_entry)
QStringListModel * m_history_model
Stores the current history_model.
void handle_contextmenu_create_script(bool flag)