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
find-files-dialog.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2013-2017 John Donoghue
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 <QPushButton>
28 #include <QDialogButtonBox>
29 #include <QGridLayout>
30 #include <QLabel>
31 #include <QLineEdit>
32 #include <QComboBox>
33 #include <QCheckBox>
34 #include <QHeaderView>
35 #include <QTableView>
36 #include <QFileDialog>
37 #include <QStatusBar>
38 #include <QIcon>
39 #include <QFileInfo>
40 #include <QTimer>
41 #include <QDirIterator>
42 #include <QTextStream>
43 #include <QGroupBox>
44 
45 #include "find-files-dialog.h"
46 #include "find-files-model.h"
47 #include "resource-manager.h"
48 
50  : QDialog (p)
51 {
52  setWindowTitle (tr ("Find Files"));
53  setWindowIcon (resource_manager::icon ("edit-find"));
54 
55  _dir_iterator = 0;
56 
57  _timer = new QTimer (this);
58  connect (_timer, SIGNAL (timeout ()), this, SLOT (look_for_files ()));
59 
60  QSettings *settings = resource_manager::get_settings ();
61 
62  QLabel * file_name_label = new QLabel (tr ("Named:"));
64  _file_name_edit->setToolTip (tr ("Enter the filename search expression"));
65 
66  _file_name_edit->setText (settings->value ("findfiles/file_name",
67  "*").toString ());
68  file_name_label->setBuddy (_file_name_edit);
69 
70  QLabel * start_dir_label = new QLabel (tr ("Start in:"));
71 
73  _start_dir_edit->setText (settings->value ("findfiles/start_dir",
74  QDir::currentPath ()).toString ());
75  _start_dir_edit->setToolTip (tr ("Enter the start directory"));
76  start_dir_label->setBuddy (_start_dir_edit);
77 
78  _browse_button = new QPushButton (tr ("Browse..."));
79  _browse_button->setToolTip (tr ("Browse for start directory"));
80  connect (_browse_button, SIGNAL (clicked ()), this, SLOT (browse_folders ()));
81 
82  _recurse_dirs_check = new QCheckBox (tr ("Search subdirectories"));
83  _recurse_dirs_check->setChecked (settings->value ("findfiles/recurse_dirs",
84  false).toBool ());
85  _recurse_dirs_check->setToolTip (
86  tr ("Search recursively through directories for matching files"));
87 
88  _include_dirs_check = new QCheckBox (tr ("Include directory names"));
89  _include_dirs_check->setChecked (settings->value ("findfiles/include_dirs",
90  false).toBool ());
91  _include_dirs_check->setToolTip (
92  tr ("Include matching directories in search results"));
93 
94  _name_case_check = new QCheckBox (tr ("Name case insensitive"));
95  _name_case_check->setChecked (settings->value ("findfiles/name_case",
96  false).toBool ());
97  _name_case_check->setToolTip (tr ("Set matching name is case insensitive"));
98 
99  _contains_text_check = new QCheckBox (tr ("Contains text:"));
100  _contains_text_check->setToolTip (tr ("Enter the file content search expression"));
101  _contains_text_check->setChecked (settings->value ("findfiles/check_text",
102  false).toBool ());
103 
105  _contains_text_edit->setToolTip (tr ("Text to match"));
106  _contains_text_edit->setText (settings->value ("findfiles/contains_text",
107  "").toString ());
108 
109  _content_case_check = new QCheckBox (tr ("Text case insensitive"));
110  _content_case_check->setChecked (settings->value ("findfiles/content_case",
111  false).toBool ());
112  _content_case_check->setToolTip (tr ("Set text content is case insensitive"));
113 
114  find_files_model * model = new find_files_model (this);
115 
116  _file_list = new QTableView;
117  _file_list->setWordWrap (false);
118  _file_list->setModel (model);
119  _file_list->setShowGrid (false);
120  _file_list->setSelectionBehavior (QAbstractItemView::SelectRows);
121  _file_list->setSelectionMode (QAbstractItemView::SingleSelection);
122  _file_list->setAlternatingRowColors (true);
123  _file_list->setToolTip (tr ("Search results"));
124  _file_list->setSortingEnabled (true);
125  _file_list->horizontalHeader ()->restoreState (
126  settings->value ("findfiles/column_state").toByteArray ());
127  _file_list->horizontalHeader ()->setSortIndicatorShown (true);
128 #if defined (HAVE_QT4)
129  _file_list->horizontalHeader ()->setClickable (true);
130 #else
131  _file_list->horizontalHeader ()->setSectionsClickable (true);
132 #endif
133  _file_list->horizontalHeader ()->setStretchLastSection (true);
134  _file_list->sortByColumn (
135  settings->value ("findfiles/sort_files_by_column",0).toInt (),
136  static_cast<Qt::SortOrder>
137  (settings->value ("findfiles/sort_files_by_order",
138  Qt::AscendingOrder).toUInt ()));
139 
140  connect (_file_list, SIGNAL (doubleClicked (const QModelIndex&)),
141  this, SLOT (item_double_clicked (const QModelIndex &)));
142 
143  _status_bar = new QStatusBar;
144  _status_bar->showMessage (tr ("Idle."));
145 
146  _find_button = new QPushButton (tr ("Find"));
147  _find_button->setToolTip (tr ("Start search for matching files"));
148  connect (_find_button, SIGNAL (clicked ()), this, SLOT (start_find ()));
149 
150  _stop_button = new QPushButton (tr ("Stop"));
151  _stop_button->setToolTip (tr ("Stop searching"));
152  _stop_button->setEnabled (false);
153  connect (_stop_button, SIGNAL (clicked ()), this, SLOT (stop_find ()));
154 
155  // layout everything
156  QDialogButtonBox * button_box = new QDialogButtonBox (Qt::Vertical);
157  button_box->addButton (_find_button, QDialogButtonBox::ActionRole);
158  button_box->addButton (_stop_button, QDialogButtonBox::ActionRole);
159 
160  // add dialog close button
161  _close_button = button_box->addButton (QDialogButtonBox::Close);
162  connect (button_box, SIGNAL (rejected ()),
163  this, SLOT (close ()));
164 
165  // name options
166  QGroupBox * name_group = new QGroupBox (tr ("Filename/location"));
167  QGridLayout * name_layout = new QGridLayout;
168  name_group->setLayout (name_layout);
169 
170  name_layout->addWidget (file_name_label,1,1, 1,1);
171  name_layout->addWidget (_file_name_edit,1,2, 1,-1);
172 
173  name_layout->addWidget (start_dir_label,2,1);
174  name_layout->addWidget (_start_dir_edit,2,2,1,3);
175  name_layout->addWidget (_browse_button,2,5);
176  name_layout->setColumnStretch (2,1);
177 
178  name_layout->addWidget (_recurse_dirs_check,3,1);
179  name_layout->addWidget (_include_dirs_check,3,2);
180  name_layout->addWidget (_name_case_check,3,3);
181 
182  // content options
183  QGroupBox * content_group = new QGroupBox (tr ("File contents"));
184  QGridLayout * content_layout = new QGridLayout;
185  content_group->setLayout (content_layout);
186  content_layout->addWidget (_contains_text_check,4,1);
187  content_layout->addWidget (_contains_text_edit,4,2,1,3);
188  content_layout->setColumnStretch (2,1);
189  content_layout->addWidget (_content_case_check,5,1);
190 
191  QGridLayout *main_layout = new QGridLayout;
192  main_layout->setSizeConstraint (QLayout::SetFixedSize);
193  main_layout->addWidget (name_group, 0, 0);
194  main_layout->addWidget (content_group, 1, 0);
195  main_layout->addWidget (button_box, 0, 1,3,1);
196  main_layout->addWidget (_file_list,2,0);
197  main_layout->setRowStretch (2,1);
198  main_layout->addWidget (_status_bar,3,0,1,-1);
199 
200  setLayout (main_layout);
201 
202  connect (this, SIGNAL (finished (int)), this, SLOT (handle_done (int)));
203 }
204 
205 void
207 {
208  QSettings *settings = resource_manager::get_settings ();
209 
210  if (! settings)
211  return;
212 
213  int sort_column = _file_list->horizontalHeader ()->sortIndicatorSection ();
214  Qt::SortOrder sort_order
215  = _file_list->horizontalHeader ()->sortIndicatorOrder ();
216  settings->setValue ("findfiles/sort_files_by_column", sort_column);
217  settings->setValue ("findfiles/sort_files_by_order", sort_order);
218  settings->setValue ("findfiles/column_state",
219  _file_list->horizontalHeader ()->saveState ());
220 
221  settings->setValue ("findfiles/file_name", _file_name_edit->text ());
222 
223  settings->setValue ("findfiles/start_dir", _start_dir_edit->text ());
224 
225  settings->setValue ("findfiles/recurse_dirs", _recurse_dirs_check->text ());
226  settings->setValue ("findfiles/include_dirs", _include_dirs_check->text ());
227  settings->setValue ("findfiles/name_case", _name_case_check->text ());
228 
229  settings->setValue ("findfiles/contains_text", _contains_text_edit->text ());
230  settings->setValue ("findfiles/check_text",
231  _contains_text_check->isChecked ());
232  settings->setValue ("findfiles/content_case",
233  _content_case_check->isChecked ());
234 
235  settings->sync ();
236 }
237 
239 {
240  if (_dir_iterator)
241  delete _dir_iterator;
242 }
243 
245 {
246  // make sure we stopped processing
247  stop_find ();
248 }
249 
250 void find_files_dialog::set_search_dir (const QString &dir)
251 {
252  stop_find ();
253  _start_dir_edit->setText (dir);
254 }
255 
256 void
258 {
259  stop_find ();
260 
261  find_files_model *m = static_cast<find_files_model *> (_file_list->model ());
262  m->clear ();
263 
264  QDirIterator::IteratorFlags flags = QDirIterator::NoIteratorFlags;
265  if (_recurse_dirs_check->isChecked ())
266  flags |= QDirIterator::Subdirectories;
267 
268  QDir::Filters filters = QDir::Dirs | QDir::NoDotAndDotDot | QDir::Files;
269  if (! _name_case_check->isChecked ())
270  filters |= QDir::CaseSensitive;
271 
272  QStringList nameFilters;
273  nameFilters.append (_file_name_edit->text ());
274 
275  if (_dir_iterator) delete _dir_iterator;
276 
277  _dir_iterator = new QDirIterator (_start_dir_edit->text (), nameFilters,
278  filters, flags);
279 
280  // enable/disable widgets
281  _find_button->setEnabled (false);
282  _stop_button->setEnabled (true);
283  _close_button->setEnabled (false);
284  _browse_button->setEnabled (false);
285  _start_dir_edit->setEnabled (false);
286  _file_name_edit->setEnabled (false);
287  _recurse_dirs_check->setEnabled (false);
288  _include_dirs_check->setEnabled (false);
289  _name_case_check->setEnabled (false);
290  _contains_text_check->setEnabled (false);
291  _content_case_check->setEnabled (false);
292  _contains_text_edit->setEnabled (false);
293 
294  _status_bar->showMessage (tr ("Searching..."));
295  _timer->start (0);
296 }
297 
298 void
300 {
301  _timer->stop ();
302 
303  _find_button->setEnabled (true);
304  _stop_button->setEnabled (false);
305  _close_button->setEnabled (true);
306  _browse_button->setEnabled (true);
307  _start_dir_edit->setEnabled (true);
308  _file_name_edit->setEnabled (true);
309  _recurse_dirs_check->setEnabled (true);
310  _include_dirs_check->setEnabled (true);
311  _name_case_check->setEnabled (true);
312  _contains_text_check->setEnabled (true);
313  _content_case_check->setEnabled (true);
314  _contains_text_edit->setEnabled (true);
315 
316  find_files_model *m = static_cast<find_files_model *> (_file_list->model ());
317  QString res_str = QString (tr("%1 match(es)")).arg (m->rowCount ());
318 
319  _status_bar->showMessage (res_str);
320 }
321 
322 void
324 {
325  QString dir =
326  QFileDialog::getExistingDirectory (this, tr ("Set search directory"),
327  _start_dir_edit->text ());
328 
329  if (! dir.isEmpty ())
330  {
331  _start_dir_edit->setText (dir);
332  }
333 }
334 
335 void
337 {
338  find_files_model *m = static_cast<find_files_model *> (_file_list->model ());
339 
340  QFileInfo info = m->fileInfo (idx);
341 
342  if (idx.column () == 1)
343  {
344  // clicked in directory part
345  emit dir_selected (info.absolutePath ());
346  }
347  else
348  {
349  // clicked in filename part
350  if (info.isDir ())
351  emit dir_selected (info.absoluteFilePath ());
352  else
353  emit file_selected (info.absoluteFilePath ());
354  }
355 }
356 
357 void
359 {
360  if (_dir_iterator && _dir_iterator->hasNext ())
361  {
362  QFileInfo info (_dir_iterator->next ());
363 
365  = static_cast<find_files_model *> (_file_list->model ());
366 
367  if (is_match (info))
368  m->addFile (info);
369  }
370  else
371  {
372  stop_find ();
373  }
374 }
375 
376 bool find_files_dialog::is_match (const QFileInfo &info)
377 {
378  bool match = true;
379  if (info.isDir ())
380  {
381  if (! _include_dirs_check->isChecked ()) match = false;
382  if (_contains_text_check->isChecked ()) match = false;
383  }
384  else
385  {
386  // a file
387  if (_contains_text_check->isChecked ())
388  {
389  match = false;
390 
391  QFile file (info.absoluteFilePath ());
392  if (file.open (QIODevice::ReadOnly))
393  {
394  QTextStream stream (&file);
395 
396  QString line;
397  QString match_str = _contains_text_edit->text ();
398 
399  Qt::CaseSensitivity cs = _content_case_check->isChecked () ?
400  Qt::CaseInsensitive : Qt::CaseSensitive;
401 
402  do
403  {
404  line = stream.readLine ();
405  match = line.contains (match_str, cs);
406  }
407  while (! line.isNull () && match == false);
408  }
409 
410  }
411  }
412 
413  return match;
414 }
QTableView * _file_list
For example cd octave end example noindent changes the current working directory to file
Definition: dirfns.cc:120
QPushButton * _close_button
QStatusBar * _status_bar
QPushButton * _browse_button
void set_search_dir(const QString &dir)
QPushButton * _find_button
void file_selected(const QString &fileName)
QCheckBox * _contains_text_check
bool is_match(const QFileInfo &info)
QPushButton * _stop_button
QLineEdit * _contains_text_edit
void item_double_clicked(const QModelIndex &)
int rowCount(const QModelIndex &p=QModelIndex()) const
nd deftypefn *octave_map m
Definition: ov-struct.cc:2058
double timeout
Definition: graphics.cc:11592
void addFile(const QFileInfo &info)
QDirIterator * _dir_iterator
static QSettings * get_settings(void)
QCheckBox * _content_case_check
QLineEdit * _start_dir_edit
find_files_dialog(QWidget *parent=0)
QCheckBox * _recurse_dirs_check
QCheckBox * _name_case_check
p
Definition: lu.cc:138
QFileInfo fileInfo(const QModelIndex &p) const
static QIcon icon(const QString &icon_name, bool fallback=true)
void dir_selected(const QString &fileName)
QLineEdit * _file_name_edit
QCheckBox * _include_dirs_check