GNU Octave  3.8.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-2013 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 
33 #include "error.h"
34 
35 #include "cmd-hist.h"
36 
37 #include "history-dock-widget.h"
38 
40  : octave_dock_widget (p)
41 {
42  setObjectName ("HistoryDockWidget");
43  setStatusTip (tr ("Browse and search the command history."));
44 
45  connect (this, SIGNAL (command_create_script (const QString&)),
46  p, SLOT (new_file (const QString&)));
47 
48  connect (this, SIGNAL (information (const QString&)),
49  p, SLOT (report_status_message (const QString&)));
50 
51  connect (this, SIGNAL (command_double_clicked (const QString&)),
52  p, SLOT (execute_command_in_terminal (const QString&)));
53 
54  construct ();
55 }
56 
57 void
59 {
60  _history_model = new QStringListModel ();
62  _history_list_view = new QListView (this);
64  _history_list_view->setAlternatingRowColors (true);
65  _history_list_view->setEditTriggers (QAbstractItemView::NoEditTriggers);
66  _history_list_view->setStatusTip (
67  tr ("Double-click a command to transfer it to the terminal."));
68  _history_list_view->setSelectionMode (QAbstractItemView::ExtendedSelection);
69  _history_list_view->setContextMenuPolicy (Qt::CustomContextMenu);
70  connect (_history_list_view,
71  SIGNAL (customContextMenuRequested (const QPoint &)), this,
72  SLOT (ctxMenu (const QPoint &)));
73 
74  _filter_line_edit = new QLineEdit (this);
75  _filter_line_edit->setStatusTip (
76  tr ("Enter text to filter the command history."));
77  QVBoxLayout *vbox_layout = new QVBoxLayout ();
78 
79  setWindowIcon (QIcon (":/actions/icons/logo.png"));
80  set_title (tr ("Command History"));
81  setWidget (new QWidget ());
82 
83  vbox_layout->addWidget (_history_list_view);
84  vbox_layout->addWidget (_filter_line_edit);
85  vbox_layout->setMargin (2);
86 
87  widget ()->setLayout (vbox_layout);
88 
89  connect (_filter_line_edit, SIGNAL (textEdited (QString)),
90  &_sort_filter_proxy_model, SLOT (setFilterWildcard (QString)));
91 
92  connect (_history_list_view, SIGNAL (doubleClicked (QModelIndex)),
93  this, SLOT (handle_double_click (QModelIndex)));
94 
95  setFocusProxy (_filter_line_edit);
96 }
97 
98 void history_dock_widget::ctxMenu (const QPoint &xpos)
99 {
100  QMenu menu (this);
101  menu.addAction (tr ("Copy"), this, SLOT (handle_contextmenu_copy (bool)));
102  menu.addAction (tr ("Evaluate"), this,
103  SLOT (handle_contextmenu_evaluate (bool)));
104  menu.addAction (tr ("Create script"), this,
105  SLOT (handle_contextmenu_create_script (bool)));
106  menu.exec (_history_list_view->mapToGlobal (xpos));
107 }
108 
110 {
111  QString text;
112  QItemSelectionModel *selectionModel = _history_list_view->selectionModel ();
113  QModelIndexList rows = selectionModel->selectedRows ();
114  QModelIndexList::iterator it;
115  bool prev_valid_row = false;
116  for (it = rows.begin (); it != rows.end (); it++)
117  {
118  if ((*it).isValid ())
119  {
120  if (prev_valid_row)
121  text += "\n";
122  text += (*it).data ().toString ();
123  prev_valid_row = true;
124  }
125  }
126  QApplication::clipboard ()->setText (text);
127 }
128 
130 {
131  QItemSelectionModel *selectionModel = _history_list_view->selectionModel ();
132  QModelIndexList rows = selectionModel->selectedRows ();
133  QModelIndexList::iterator it;
134  for (it = rows.begin () ; it != rows.end (); it++)
135  {
136  if ((*it).isValid ())
137  emit command_double_clicked ((*it).data ().toString ());
138  }
139 }
140 
141 void
143 {
144  QString text;
145  QItemSelectionModel *selectionModel = _history_list_view->selectionModel ();
146  QModelIndexList rows = selectionModel->selectedRows ();
147 
148  bool prev_valid_row = false;
149  for (QModelIndexList::iterator it = rows.begin (); it != rows.end (); it++)
150  {
151  if ((*it).isValid ())
152  {
153  if (prev_valid_row)
154  text += "\n";
155  text += (*it).data ().toString ();
156  prev_valid_row = true;
157  }
158  }
159 
160  if (text.length () > 0)
161  emit command_create_script (text);
162 }
163 
164 
165 void
167 {
168  emit command_double_clicked (modelIndex.data ().toString ());
169 }
170 
171 void
172 history_dock_widget::set_history (const QStringList& hist)
173 {
174  _history_model->setStringList (hist);
175  _history_list_view->scrollToBottom ();
176 }
177 
178 void
179 history_dock_widget::append_history (const QString& hist_entry)
180 {
181  QStringList lst = _history_model->stringList ();
182  lst.append (hist_entry);
183 
184  QScrollBar *scroll_bar = _history_list_view->verticalScrollBar ();
185 
186  bool at_bottom = scroll_bar->maximum () - scroll_bar->value () < 1;
187 
188  _history_model->setStringList (lst);
189 
190  // Scroll if slider position at bottom.
191  if (at_bottom)
192  _history_list_view->scrollToBottom ();
193 }
194 
195 void
197 {
198  _history_model->setStringList (QStringList ());
199 }
200 
201 void
203 {
204  if (_history_list_view->hasFocus ())
206  if (_filter_line_edit->hasFocus () && _filter_line_edit->hasSelectedText ())
207  {
208  QClipboard *clipboard = QApplication::clipboard ();
209  clipboard->setText ( _filter_line_edit->selectedText ());
210  }
211 }
212 
213 void
215 {
216  if (_filter_line_edit->hasFocus ())
217  {
218  QClipboard *clipboard = QApplication::clipboard ();
219  QString str = clipboard->text ();
220  if (str.length () > 0)
221  _filter_line_edit->insert (str);
222  }
223 }
224