GNU Octave  4.0.0
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-2015 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 #ifdef 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  QVBoxLayout *vbox_layout = new QVBoxLayout ();
98  QHBoxLayout *hbox_layout = new QHBoxLayout ();
99  hbox_layout->addWidget (filter_label);
100  hbox_layout->addWidget (_filter_checkbox);
101  hbox_layout->addWidget (_filter);
102  vbox_layout->addLayout (hbox_layout);
103  vbox_layout->addWidget (_history_list_view);
104  vbox_layout->setMargin (2);
105 
106  widget ()->setLayout (vbox_layout);
107 
108  setFocusProxy (_filter->lineEdit ());
109 
110  // Init state of the filter
111  QSettings *settings = resource_manager::get_settings ();
112  _filter->addItems (settings->value ("history_dock_widget/mru_list").toStringList ());
113 
114  bool filter_state =
115  settings->value ("history_dock_widget/filter_active", false).toBool ();
116  _filter_checkbox->setChecked (filter_state);
117  filter_activate (filter_state);
118 
119  // Connect signals and slots
120  connect (_filter, SIGNAL (editTextChanged (const QString&)),
121  &_sort_filter_proxy_model, SLOT (setFilterWildcard (const QString&)));
122  connect (_filter_checkbox, SIGNAL (toggled (bool)),
123  this, SLOT (filter_activate (bool)));
124  connect (_filter->lineEdit (), SIGNAL (editingFinished ()),
125  this, SLOT (update_filter_history ()));
126 
127  connect (_history_list_view, SIGNAL (doubleClicked (QModelIndex)),
128  this, SLOT (handle_double_click (QModelIndex)));
129 
130  // shrink max. displayed entry size to desktop width
131  QSize screen = QDesktopWidget ().screenGeometry ().size ();
132  int w = screen.width ();
133  QFontMetrics fm = _history_list_view->fontMetrics ();
134  int h = fm.height ();
135  _history_list_view->setGridSize (QSize (w,h));
136  _history_list_view->setTextElideMode (Qt::ElideRight);
137 }
138 
140 {
141  QSettings *settings = resource_manager::get_settings ();
142 
143  settings->setValue ("history_dock_widget/filter_active",
144  _filter_checkbox->isChecked ());
145 
146  QStringList mru;
147  for (int i = 0; i < _filter->count (); i++)
148  mru.append (_filter->itemText (i));
149  settings->setValue ("history_dock_widget/mru_list", mru);
150 
151  settings->sync ();
152 }
153 
154 void
156 {
157  _filter->setEnabled (state);
158  _sort_filter_proxy_model.setDynamicSortFilter (state);
159 
160  if (state)
161  _sort_filter_proxy_model.setFilterWildcard (_filter->currentText ());
162  else
163  _sort_filter_proxy_model.setFilterWildcard (QString ());
164 }
165 
166 void
168 {
169  QString text = _filter->currentText (); // get current text
170  int index = _filter->findText (text); // and its actual index
171 
172  if (index > -1)
173  _filter->removeItem (index); // remove if already existing
174 
175  _filter->insertItem (0, text); // (re)insert at beginning
176  _filter->setCurrentIndex (0);
177 }
178 
179 void history_dock_widget::ctxMenu (const QPoint &xpos)
180 {
181  QMenu menu (this);
182  menu.addAction (resource_manager::icon ("edit-copy"),
183  tr ("Copy"), this, SLOT (handle_contextmenu_copy (bool)));
184  menu.addAction (tr ("Evaluate"), this,
185  SLOT (handle_contextmenu_evaluate (bool)));
186  menu.addAction (resource_manager::icon ("document-new"),
187  tr ("Create script"), this,
188  SLOT (handle_contextmenu_create_script (bool)));
189  menu.exec (_history_list_view->mapToGlobal (xpos));
190 }
191 
193 {
194  QString text;
195  QItemSelectionModel *selectionModel = _history_list_view->selectionModel ();
196  QModelIndexList rows = selectionModel->selectedRows ();
197  QModelIndexList::iterator it;
198  bool prev_valid_row = false;
199  for (it = rows.begin (); it != rows.end (); it++)
200  {
201  if ((*it).isValid ())
202  {
203  if (prev_valid_row)
204  text += "\n";
205  text += (*it).data ().toString ();
206  prev_valid_row = true;
207  }
208  }
209  QApplication::clipboard ()->setText (text);
210 }
211 
213 {
214  QItemSelectionModel *selectionModel = _history_list_view->selectionModel ();
215  QModelIndexList rows = selectionModel->selectedRows ();
216  QModelIndexList::iterator it;
217  for (it = rows.begin () ; it != rows.end (); it++)
218  {
219  if ((*it).isValid ())
220  emit command_double_clicked ((*it).data ().toString ());
221  }
222 }
223 
224 void
226 {
227  QString text;
228  QItemSelectionModel *selectionModel = _history_list_view->selectionModel ();
229  QModelIndexList rows = selectionModel->selectedRows ();
230 
231  bool prev_valid_row = false;
232  for (QModelIndexList::iterator it = rows.begin (); it != rows.end (); it++)
233  {
234  if ((*it).isValid ())
235  {
236  if (prev_valid_row)
237  text += "\n";
238  text += (*it).data ().toString ();
239  prev_valid_row = true;
240  }
241  }
242 
243  if (text.length () > 0)
244  emit command_create_script (text);
245 }
246 
247 
248 void
250 {
251  emit command_double_clicked (modelIndex.data ().toString ());
252 }
253 
254 void
255 history_dock_widget::set_history (const QStringList& hist)
256 {
257  _history_model->setStringList (hist);
258  _history_list_view->scrollToBottom ();
259 }
260 
261 void
262 history_dock_widget::append_history (const QString& hist_entry)
263 {
264  QStringList lst = _history_model->stringList ();
265  lst.append (hist_entry);
266 
267  QScrollBar *scroll_bar = _history_list_view->verticalScrollBar ();
268 
269  bool at_bottom = scroll_bar->maximum () - scroll_bar->value () < 1;
270 
271  _history_model->setStringList (lst);
272 
273  // Scroll if slider position at bottom.
274  if (at_bottom)
275  _history_list_view->scrollToBottom ();
276 }
277 
278 void
280 {
281  _history_model->setStringList (QStringList ());
282 }
283 
284 void
286 {
287  if (_history_list_view->hasFocus ())
289  if (_filter->lineEdit ()->hasFocus ()
290  && _filter->lineEdit ()->hasSelectedText ())
291  {
292  QClipboard *clipboard = QApplication::clipboard ();
293  clipboard->setText ( _filter->lineEdit ()->selectedText ());
294  }
295 }
296 
297 void
299 {
300  if (_filter->lineEdit ()->hasFocus ())
301  {
302  QClipboard *clipboard = QApplication::clipboard ();
303  QString str = clipboard->text ();
304  if (str.length () > 0)
305  _filter->lineEdit ()->insert (str);
306  }
307 }
308 
309 void
311 {
312  if (_filter->lineEdit ()->hasFocus ())
313  {
314  _filter->lineEdit ()->selectAll ();
315  }
316  if (_history_list_view->hasFocus ())
317  {
318  _history_list_view->selectAll ();
319  }
320 }
static uint32_t state[624]
Definition: randmtzig.c:188
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)
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)
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)
void filter_activate(bool enable)
static QIcon icon(const QString &icon_name, bool fallback=true)
void handle_contextmenu_create_script(bool flag)
QSortFilterProxyModel _sort_filter_proxy_model
void handle_contextmenu_evaluate(bool flag)