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
find-files-dialog.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2013 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 #ifdef 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 (QIcon (":/actions/icons/search.png"));
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:"));
63  _file_name_edit = new QLineEdit;
64  _file_name_edit->setToolTip (tr ("Enter the filename 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 
72  _start_dir_edit = new QLineEdit;
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 ("Recurse directories"));
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 directories"));
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 ("Search must match text"));
101  _contains_text_check->setChecked (settings->value ("findfiles/check_text",
102  false).toBool ());
103 
104  _contains_text_edit = new QLineEdit ();
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  _file_list->horizontalHeader ()->setClickable (true);
129  _file_list->horizontalHeader ()->setStretchLastSection (true);
130  _file_list->sortByColumn (
131  settings->value ("findfiles/sort_files_by_column",0).toInt (),
132  static_cast<Qt::SortOrder>
133  (settings->value ("findfiles/sort_files_by_order",
134  Qt::AscendingOrder).toUInt ()));
135 
136  connect (_file_list, SIGNAL (doubleClicked (const QModelIndex&)),
137  this, SLOT (item_double_clicked (const QModelIndex &)));
138 
139  _status_bar = new QStatusBar;
140  _status_bar->showMessage (tr ("Idle."));
141 
142  _find_button = new QPushButton (tr ("Find"));
143  _find_button->setToolTip (tr ("Start search for matching files"));
144  connect (_find_button, SIGNAL (clicked ()), this, SLOT (start_find ()));
145 
146  _stop_button = new QPushButton (tr ("Stop"));
147  _stop_button->setToolTip (tr ("Stop searching"));
148  _stop_button->setEnabled (false);
149  connect (_stop_button, SIGNAL (clicked ()), this, SLOT (stop_find ()));
150 
151  // layout everything
152  QDialogButtonBox * button_box = new QDialogButtonBox (Qt::Vertical);
153  button_box->addButton (_find_button, QDialogButtonBox::ActionRole);
154  button_box->addButton (_stop_button, QDialogButtonBox::ActionRole);
155 
156  // add dialog close button
157  _close_button = button_box->addButton (QDialogButtonBox::Close);
158  connect (button_box, SIGNAL (rejected ()),
159  this, SLOT (close ()));
160 
161  // name options
162  QGroupBox * name_group = new QGroupBox (tr ("File name/location"));
163  QGridLayout * name_layout = new QGridLayout;
164  name_group->setLayout (name_layout);
165 
166  name_layout->addWidget (file_name_label,1,1, 1,1);
167  name_layout->addWidget (_file_name_edit,1,2, 1,-1);
168 
169  name_layout->addWidget (start_dir_label,2,1);
170  name_layout->addWidget (_start_dir_edit,2,2,1,3);
171  name_layout->addWidget (_browse_button,2,5);
172  name_layout->setColumnStretch (2,1);
173 
174  name_layout->addWidget (_recurse_dirs_check,3,1);
175  name_layout->addWidget (_include_dirs_check,3,2);
176  name_layout->addWidget (_name_case_check,3,3);
177 
178  // content options
179  QGroupBox * content_group = new QGroupBox (tr ("File contents"));
180  QGridLayout * content_layout = new QGridLayout;
181  content_group->setLayout (content_layout);
182  content_layout->addWidget (_contains_text_check,4,1);
183  content_layout->addWidget (_contains_text_edit,4,2,1,3);
184  content_layout->setColumnStretch (2,1);
185  content_layout->addWidget (_content_case_check,5,1);
186 
187  QGridLayout *main_layout = new QGridLayout;
188  main_layout->setSizeConstraint (QLayout::SetFixedSize);
189  main_layout->addWidget (name_group, 0, 0);
190  main_layout->addWidget (content_group, 1, 0);
191  main_layout->addWidget (button_box, 0, 1,3,1);
192  main_layout->addWidget (_file_list,2,0);
193  main_layout->setRowStretch (2,1);
194  main_layout->addWidget (_status_bar,3,0,1,-1);
195 
196 
197  setLayout (main_layout);
198 
199  connect (this, SIGNAL (finished (int)), this, SLOT (handle_done (int)));
200 }
201 
203 {
204  QSettings *settings = resource_manager::get_settings ();
205 
206  int sort_column = _file_list->horizontalHeader ()->sortIndicatorSection ();
207  Qt::SortOrder sort_order
208  = _file_list->horizontalHeader ()->sortIndicatorOrder ();
209  settings->setValue ("findfiles/sort_files_by_column", sort_column);
210  settings->setValue ("findfiles/sort_files_by_order", sort_order);
211  settings->setValue ("findfiles/column_state",
212  _file_list->horizontalHeader ()->saveState ());
213 
214  settings->setValue ("findfiles/file_name", _file_name_edit->text ());
215 
216  settings->setValue ("findfiles/start_dir", _start_dir_edit->text ());
217 
218  settings->setValue ("findfiles/recurse_dirs", _recurse_dirs_check->text ());
219  settings->setValue ("findfiles/include_dirs", _include_dirs_check->text ());
220  settings->setValue ("findfiles/name_case", _name_case_check->text ());
221 
222  settings->setValue ("findfiles/contains_text", _contains_text_edit->text ());
223  settings->setValue ("findfiles/check_text",
224  _contains_text_check->isChecked ());
225  settings->setValue ("findfiles/content_case",
226  _content_case_check->isChecked ());
227 
228  settings->sync ();
229 
230  if (_dir_iterator)
231  delete _dir_iterator;
232 }
233 
235 {
236  // make sure we stopped processing
237  stop_find ();
238 }
239 
240 void find_files_dialog::set_search_dir (const QString &dir)
241 {
242  stop_find ();
243  _start_dir_edit->setText (dir);
244 }
245 
246 void
248 {
249  stop_find ();
250 
251  find_files_model *m = static_cast<find_files_model *> (_file_list->model ());
252  m->clear ();
253 
254  QDirIterator::IteratorFlags flags = QDirIterator::NoIteratorFlags;
255  if (_recurse_dirs_check->isChecked ())
256  flags |= QDirIterator::Subdirectories;
257 
258  QDir::Filters filters = QDir::Dirs|QDir::NoDotAndDotDot|QDir::Files;
259  if (!_name_case_check->isChecked ())
260  filters |= QDir::CaseSensitive;
261 
262  QStringList nameFilters;
263  nameFilters.append (_file_name_edit->text ());
264 
265  if (_dir_iterator) delete _dir_iterator;
266 
267  _dir_iterator = new QDirIterator (_start_dir_edit->text (), nameFilters,
268  filters, flags);
269 
270  // enable/disable widgets
271  _find_button->setEnabled (false);
272  _stop_button->setEnabled (true);
273  _close_button->setEnabled (false);
274  _browse_button->setEnabled (false);
275  _start_dir_edit->setEnabled (false);
276  _file_name_edit->setEnabled (false);
277  _recurse_dirs_check->setEnabled (false);
278  _include_dirs_check->setEnabled (false);
279  _name_case_check->setEnabled (false);
280  _contains_text_check->setEnabled (false);
281  _content_case_check->setEnabled (false);
282  _contains_text_edit->setEnabled (false);
283 
284  _status_bar->showMessage (tr ("Searching..."));
285  _timer->start (0);
286 }
287 
288 void
290 {
291  _timer->stop ();
292 
293  _find_button->setEnabled (true);
294  _stop_button->setEnabled (false);
295  _close_button->setEnabled (true);
296  _browse_button->setEnabled (true);
297  _start_dir_edit->setEnabled (true);
298  _file_name_edit->setEnabled (true);
299  _recurse_dirs_check->setEnabled (true);
300  _include_dirs_check->setEnabled (true);
301  _name_case_check->setEnabled (true);
302  _contains_text_check->setEnabled (true);
303  _content_case_check->setEnabled (true);
304  _contains_text_edit->setEnabled (true);
305 
306  find_files_model *m = static_cast<find_files_model *> (_file_list->model ());
307  QString res_str = QString ("%1 matches").arg (m->rowCount ());
308 
309  _status_bar->showMessage (res_str);
310 }
311 
312 void
314 {
315  QString dir =
316  QFileDialog::getExistingDirectory (this, tr ("Set search directory"),
317  _start_dir_edit->text ());
318 
319  if (! dir.isEmpty ())
320  {
321  _start_dir_edit->setText (dir);
322  }
323 }
324 
325 void
327 {
328  find_files_model *m = static_cast<find_files_model *> (_file_list->model ());
329 
330  QFileInfo info = m->fileInfo (idx);
331 
332  if (idx.column () == 1)
333  {
334  // clicked in directory part
335  emit dir_selected (info.absolutePath ());
336  }
337  else
338  {
339  // clicked in filename part
340  if (info.isDir ())
341  emit dir_selected (info.absoluteFilePath ());
342  else
343  emit file_selected (info.absoluteFilePath ());
344  }
345 }
346 
347 void
349 {
350  if (_dir_iterator && _dir_iterator->hasNext ())
351  {
352  QFileInfo info (_dir_iterator->next ());
353 
355  = static_cast<find_files_model *> (_file_list->model ());
356 
357  if (is_match (info))
358  m->addFile (info);
359  }
360  else
361  {
362  stop_find ();
363  }
364 }
365 
366 bool find_files_dialog::is_match (const QFileInfo &info)
367 {
368  bool match = true;
369  if (info.isDir ())
370  {
371  if (!_include_dirs_check->isChecked ()) match = false;
372  if (_contains_text_check->isChecked ()) match = false;
373  }
374  else
375  {
376  // a file
377  if (_contains_text_check->isChecked ())
378  {
379  match = false;
380 
381  QFile file (info.absoluteFilePath ());
382  if (file.open (QIODevice::ReadOnly))
383  {
384  QTextStream stream (&file);
385 
386  QString line;
387  QString match_str = _contains_text_edit->text ();
388 
389  Qt::CaseSensitivity cs = _content_case_check->isChecked () ?
390  Qt::CaseInsensitive : Qt::CaseSensitive;
391 
392  do
393  {
394  line = stream.readLine ();
395  match = line.contains (match_str, cs);
396  }
397  while (!line.isNull () && match == false);
398  }
399 
400  }
401  }
402 
403  return match;
404 }
405