GNU Octave  8.1.0
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-2023 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING. If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if defined (HAVE_CONFIG_H)
27 # include "config.h"
28 #endif
29 
30 #include <QApplication>
31 #include <QClipboard>
32 #include <QCompleter>
33 #include <QLabel>
34 #include <QMenu>
35 #include <QScrollBar>
36 #include <QVBoxLayout>
37 
38 #include "gui-preferences-cs.h"
39 #include "gui-preferences-global.h"
40 #include "gui-preferences-hw.h"
41 #include "history-dock-widget.h"
42 #include "octave-qobject.h"
43 
44 #include "cmd-hist.h"
45 
46 #include "error.h"
47 
49 
51 : octave_dock_widget ("HistoryDockWidget", p, oct_qobj)
52 {
53  setStatusTip (tr ("Browse and search the command history."));
54 
55  construct ();
56 
57  if (! p)
58  make_window ();
59 }
60 
61 void history_dock_widget::set_history (const QStringList& hist)
62 {
63  m_history_model->setStringList (hist);
64  m_history_list_view->scrollToBottom ();
65 }
66 
67 void history_dock_widget::append_history (const QString& hist_entry)
68 {
69  QStringList lst = m_history_model->stringList ();
70  lst.append (hist_entry);
71 
72  QScrollBar *scroll_bar = m_history_list_view->verticalScrollBar ();
73 
74  bool at_bottom = scroll_bar->maximum () - scroll_bar->value () < 1;
75 
76  m_history_model->setStringList (lst);
77 
78  // Scroll if slider position at bottom.
79  if (at_bottom)
80  m_history_list_view->scrollToBottom ();
81 }
82 
84 {
85  m_history_model->setStringList (QStringList ());
86 }
87 
89 {
92 
93  if (! settings)
94  return;
95 
96  settings->setValue (hw_filter_active.key, m_filter_checkbox->isChecked ());
98 
99  QStringList mru;
100  for (int i = 0; i < m_filter->count (); i++)
101  mru.append (m_filter->itemText (i));
102  settings->setValue (hw_mru_list.key, mru);
103 
104  settings->sync ();
105 
107 }
108 
110 {
111  QString text = m_filter->currentText (); // get current text
112  int index = m_filter->findText (text); // and its actual index
113 
114  if (index > -1)
115  m_filter->removeItem (index); // remove if already existing
116 
117  m_filter->insertItem (0, text); // (re)insert at beginning
118  m_filter->setCurrentIndex (0);
119 }
120 
122 {
123  if (focus)
124  {
125  m_filter->setFocus ();
126  setFocusProxy (m_filter);
127  }
128  else
129  {
130  m_history_list_view->setFocus ();
131  setFocusProxy (m_history_list_view);
132  }
133 }
134 
136 {
137  m_filter->setEnabled (state);
138  m_sort_filter_proxy_model.setDynamicSortFilter (state);
139 
140  if (state)
141  m_sort_filter_proxy_model.setFilterWildcard (m_filter->currentText ());
142  else
143  m_sort_filter_proxy_model.setFilterWildcard (QString ());
144 
146 }
147 
148 void history_dock_widget::ctxMenu (const QPoint& xpos)
149 {
150  QMenu menu (this);
151 
152  QModelIndex index = m_history_list_view->indexAt (xpos);
153 
154  if (index.isValid () && index.column () == 0)
155  {
157 
158  menu.addAction (rmgr.icon ("edit-copy"), tr ("Copy"), this,
160  menu.addAction (tr ("Evaluate"), this,
162  menu.addAction (rmgr.icon ("document-new"), tr ("Create script"), this,
164  }
165  if (m_filter_shown)
166  menu.addAction (tr ("Hide filter"), this,
168  else
169  menu.addAction (tr ("Show filter"), this,
171 
172  menu.exec (m_history_list_view->mapToGlobal (xpos));
173 }
174 
175 void history_dock_widget::handle_double_click (QModelIndex modelIndex)
176 {
177  emit command_double_clicked (modelIndex.data ().toString ());
178 }
179 
181 {
182  QString text;
183  QItemSelectionModel *selectionModel = m_history_list_view->selectionModel ();
184  QModelIndexList rows = selectionModel->selectedRows ();
185  bool prev_valid_row = false;
186  for (auto it = rows.begin (); it != rows.end (); it++)
187  {
188  if ((*it).isValid ())
189  {
190  if (prev_valid_row)
191  text += '\n';
192  text += (*it).data ().toString ();
193  prev_valid_row = true;
194  }
195  }
196  QApplication::clipboard ()->setText (text);
197 }
198 
200 {
201  QItemSelectionModel *selectionModel = m_history_list_view->selectionModel ();
202  QModelIndexList rows = selectionModel->selectedRows ();
203  for (auto it = rows.begin () ; it != rows.end (); it++)
204  {
205  if ((*it).isValid ())
206  emit command_double_clicked ((*it).data ().toString ());
207  }
208 }
209 
211 {
212  QString text;
213  QItemSelectionModel *selectionModel = m_history_list_view->selectionModel ();
214  QModelIndexList rows = selectionModel->selectedRows ();
215 
216  bool prev_valid_row = false;
217  for (auto it = rows.begin (); it != rows.end (); it++)
218  {
219  if ((*it).isValid ())
220  {
221  if (prev_valid_row)
222  text += '\n';
223  text += (*it).data ().toString ();
224  prev_valid_row = true;
225  }
226  }
227 
228  if (text.length () > 0)
229  emit command_create_script (text);
230 }
231 
233 {
235  m_filter_widget->setVisible (m_filter_shown);
236 
238 }
239 
241 {
242  if (m_history_list_view->hasFocus ())
244  if (m_filter->lineEdit ()->hasFocus ()
245  && m_filter->lineEdit ()->hasSelectedText ())
246  {
247  QClipboard *clipboard = QApplication::clipboard ();
248  clipboard->setText (m_filter->lineEdit ()->selectedText ());
249  }
250 }
251 
253 {
254  if (m_filter->lineEdit ()->hasFocus ())
255  {
256  QClipboard *clipboard = QApplication::clipboard ();
257  QString str = clipboard->text ();
258  if (str.length () > 0)
259  m_filter->lineEdit ()->insert (str);
260  }
261 }
262 
264 {
265  if (m_filter->lineEdit ()->hasFocus ())
266  m_filter->lineEdit ()->selectAll ();
267 
268  if (m_history_list_view->hasFocus ())
269  m_history_list_view->selectAll ();
270 }
271 
273 {
275 
276  if (visible)
277  {
278  int filter_state = m_filter_checkbox->isChecked ();
279  filter_activate (filter_state);
280  }
281 }
282 
284 {
285  m_history_model = new QStringListModel ();
287  m_history_list_view = new QListView (this);
289  m_history_list_view->setAlternatingRowColors (true);
290  m_history_list_view->setEditTriggers (QAbstractItemView::NoEditTriggers);
291  m_history_list_view->setStatusTip
292  (tr ("Double-click a command to transfer it to the Command Window."));
293  m_history_list_view->setSelectionMode (QAbstractItemView::ExtendedSelection);
294  m_history_list_view->setContextMenuPolicy (Qt::CustomContextMenu);
295  connect (m_history_list_view, &QListView::customContextMenuRequested,
297 
298  m_filter = new QComboBox (this);
299  m_filter->setToolTip (tr ("Enter text to filter the command history"));
300  m_filter->setEditable (true);
301  m_filter->setMaxCount (MaxFilterHistory);
302  m_filter->setInsertPolicy (QComboBox::NoInsert);
303  m_filter->setSizeAdjustPolicy
304  (QComboBox::AdjustToMinimumContentsLengthWithIcon);
305  QSizePolicy sizePol (QSizePolicy::Expanding, QSizePolicy::Preferred);
306  m_filter->setSizePolicy (sizePol);
307  m_filter->completer ()->setCaseSensitivity (Qt::CaseSensitive);
308 
309  QLabel *filter_label = new QLabel (tr ("Filter"));
310 
311  m_filter_checkbox = new QCheckBox ();
312 
313  set_title (tr ("Command History"));
314  setWidget (new QWidget ());
315 
316  m_filter_widget = new QWidget (this);
317  QHBoxLayout *filter_layout = new QHBoxLayout ();
318  filter_layout->addWidget (filter_label);
319  filter_layout->addWidget (m_filter_checkbox);
320  filter_layout->addWidget (m_filter);
321  filter_layout->setMargin(0);
322  m_filter_widget->setLayout (filter_layout);
323 
324  QVBoxLayout *hist_layout = new QVBoxLayout ();
325  hist_layout->addWidget (m_filter_widget);
326  hist_layout->addWidget (m_history_list_view);
327 
328  hist_layout->setMargin (2);
329  hist_layout->setSpacing (0);
330  widget ()->setLayout (hist_layout);
331 
332  // Init state of the filter
335 
337  = settings->value (hw_filter_shown).toBool ();
338  m_filter_widget->setVisible (m_filter_shown);
339 
340  m_filter->addItems (settings->value (hw_mru_list).toStringList ());
341 
342  bool filter_state
343  = settings->value (hw_filter_active).toBool ();
344  m_filter_checkbox->setChecked (filter_state);
345  filter_activate (filter_state);
346 
347  // Connect signals and slots
348  connect (m_filter, &QComboBox::editTextChanged,
350  &QSortFilterProxyModel::setFilterWildcard);
351  connect (m_filter_checkbox, &QCheckBox::toggled,
353  connect (m_filter->lineEdit (), &QLineEdit::editingFinished,
355 
356  connect (m_history_list_view, &QListView::doubleClicked,
358 
359  m_history_list_view->setTextElideMode (Qt::ElideRight);
360 }
361 
363 {
364  QFont font = QFont ();
365 
366  font.setStyleHint (QFont::TypeWriter);
367  QString default_font = settings->value (global_mono_font).toString ();
368 
369  font.setFamily (settings->value (cs_font.key, default_font).toString ());
370  font.setPointSize (settings->value (cs_font_size).toInt ());
371 
372  m_history_list_view->setFont (font);
373 }
374 
OCTAVE_END_NAMESPACE(octave)
Base class for Octave interfaces that use Qt.
resource_manager & get_resource_manager(void)
void handle_contextmenu_evaluate(bool flag)
QStringListModel * m_history_model
Stores the current history_model.
void set_history(const QStringList &hist)
void handle_contextmenu_filter(void)
void ctxMenu(const QPoint &pos)
void set_filter_focus(bool focus)
void handle_contextmenu_create_script(bool flag)
QSortFilterProxyModel m_sort_filter_proxy_model
void command_double_clicked(const QString &command)
Signal emitted whenever the user double-clicks a command in the history.
void append_history(const QString &hist_entry)
void filter_activate(bool enable)
void command_create_script(const QString &commands)
Signal emitted whenever the user selects commands and chooses "Create script" from the popup menu.
void handle_contextmenu_copy(bool flag)
QListView * m_history_list_view
virtual void handle_visibility(bool visible)
void notice_settings(const gui_settings *)
void handle_double_click(QModelIndex modelIndex)
base_qobject & m_octave_qobj
void set_title(const QString &)
virtual void handle_visibility(bool visible)
virtual void save_settings(void)
gui_settings * get_settings(void) const
QIcon icon(const QString &icon_name, bool octave_only=false, const QString &icon_alt_name=QString())
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
const gui_pref cs_font_size("terminal/fontSize", QVariant(10))
const gui_pref cs_font("terminal/fontName", QVariant())
const gui_pref global_mono_font("monospace_font", global_font_family)
const gui_pref hw_filter_active("history_dock_widget/filter_active", QVariant(false))
const gui_pref hw_filter_shown("history_dock_widget/filter_shown", QVariant(true))
const gui_pref hw_mru_list("history_dock_widget/mru_list", QVariant())
static uint32_t state[624]
Definition: randmtzig.cc:193
const QString key