Skip to content

Commit

Permalink
find files: allow to simultaneously search for several file patterns
Browse files Browse the repository at this point in the history
* find-files-dialog.cc (find_files_dialog): update tool tip for
  the start dir combobox, do not set the start dir to the current
  directory, add a button for doing this instead, find button gets
  focus
  (start_find): allow several search patterns separated by semicolon,
  disable new button as well;
  (stop_find ): enable new button as well;
  (start_dir_to_cwd): new slot for the button which sets the start
  dir to the current directory

* find-files-dialog.h: new button for setting start dir to current
  dir, new slot for this button

* gui-utils.cc (combobox_insert_current_item): code clean up

* main-window.cc (find_files): only set the start directory of
  the activated find files dialog, if the provided argument is
  not empty (as for calls from the file browser), otherwise the
  last start directory is restored

* main-window.h (find_files): default argument is an empty string
  • Loading branch information
ttl-octave committed Jan 4, 2025
1 parent 8692c49 commit b94ba63
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 16 deletions.
37 changes: 26 additions & 11 deletions libgui/src/find-files-dialog.cc
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ find_files_dialog::find_files_dialog (QWidget *p)

QLabel *file_name_label = new QLabel (tr ("Named:"));
m_file_name_edit = new QComboBox;
m_file_name_edit->setToolTip (tr ("Enter the filename search expression"));
m_file_name_edit->setToolTip (tr ("Enter the filename search patterns.\n"
"Several different patterns can be\n"
"separated by ';', e.g. '*.cc ; *.h'"));
m_file_name_edit->setEditable (true);
m_file_name_edit->setMaxCount (m_mru_length);

Expand All @@ -93,13 +95,17 @@ find_files_dialog::find_files_dialog (QWidget *p)
while (mru.length () > m_mru_length)
mru.removeLast ();
m_start_dir_edit->addItems (mru);
m_start_dir_edit->setItemText (0, QDir::currentPath ());

m_browse_button = new QPushButton (tr ("Browse..."));
m_browse_button->setToolTip (tr ("Browse for start directory"));
connect (m_browse_button, &QPushButton::clicked,
this, &find_files_dialog::browse_folders);

m_current_dir_button = new QPushButton (tr ("Current Dir"));
m_current_dir_button->setToolTip (tr ("Set start directory to current directory"));
connect (m_current_dir_button, &QPushButton::clicked,
this, &find_files_dialog::start_dir_to_cwd);

m_recurse_dirs_check = new QCheckBox (tr ("Search subdirectories"));
m_recurse_dirs_check->setToolTip (tr ("Search recursively through directories for matching files"));
m_recurse_dirs_check->setChecked (settings.bool_value (ff_recurse_dirs));
Expand Down Expand Up @@ -194,13 +200,14 @@ find_files_dialog::find_files_dialog (QWidget *p)
name_layout->addWidget (start_dir_label, 1, 0);
name_layout->addWidget (m_start_dir_edit, 1, 1, 1, 2);
name_layout->addWidget (m_browse_button, 1, 3, 1, 1);
name_layout->addWidget (m_current_dir_button, 2, 3, 1, 1);
name_layout->setColumnStretch (1, 1);

QHBoxLayout *name_options_layout = new QHBoxLayout;
name_options_layout->addWidget (m_recurse_dirs_check);
name_options_layout->addWidget (m_include_dirs_check);
name_options_layout->addWidget (m_name_case_check);
name_layout->addLayout (name_options_layout, 2, 0, 1, 3);
name_layout->addLayout (name_options_layout, 2, 0, 1, 2);

// content options
QGroupBox *content_group = new QGroupBox (tr ("File contents"));
Expand All @@ -210,10 +217,7 @@ find_files_dialog::find_files_dialog (QWidget *p)
content_layout->addWidget (m_contains_text_check, 0, 0, 1, 1);
content_layout->addWidget (m_contains_text_edit, 0, 1, 1, 1);
content_layout->setColumnStretch (1, 1);

QHBoxLayout *content_options_layout = new QHBoxLayout;
content_options_layout->addWidget (m_content_case_check);
content_layout->addLayout (content_options_layout, 1, 0, 1, 2);
content_layout->addWidget (m_content_case_check, 0, 2, 1, 1);

// results
QLabel *results_hint = new QLabel (tr ("Results: Double click opens the file"
Expand All @@ -239,6 +243,8 @@ find_files_dialog::find_files_dialog (QWidget *p)
connect (this, &find_files_dialog::finished,
this, &find_files_dialog::handle_done);

m_find_button->setFocus(Qt::OtherFocusReason);

if (settings.contains (ff_geometry.settings_key ()))
restoreGeometry (settings.byte_array_value (ff_geometry));
}
Expand Down Expand Up @@ -318,20 +324,22 @@ find_files_dialog::start_find ()
if (! m_name_case_check->isChecked ())
filters |= QDir::CaseSensitive;

QStringList nameFilters;
nameFilters.append (m_file_name_edit->currentText ());
const QStringList nameFilters =
m_file_name_edit->currentText ().split (QRegularExpression("\\s*;\\s*"),
Qt::SkipEmptyParts);

if (m_dir_iterator)
delete m_dir_iterator;

m_dir_iterator = new QDirIterator (m_start_dir_edit->currentText (), nameFilters,
filters, flags);
m_dir_iterator = new QDirIterator (m_start_dir_edit->currentText (),
nameFilters, filters, flags);

// enable/disable widgets
m_find_button->setEnabled (false);
m_stop_button->setEnabled (true);
m_close_button->setEnabled (false);
m_browse_button->setEnabled (false);
m_current_dir_button->setEnabled (false);
m_start_dir_edit->setEnabled (false);
m_file_name_edit->setEnabled (false);
m_recurse_dirs_check->setEnabled (false);
Expand All @@ -354,6 +362,7 @@ find_files_dialog::stop_find ()
m_stop_button->setEnabled (false);
m_close_button->setEnabled (true);
m_browse_button->setEnabled (true);
m_current_dir_button->setEnabled (true);
m_start_dir_edit->setEnabled (true);
m_file_name_edit->setEnabled (true);
m_recurse_dirs_check->setEnabled (true);
Expand Down Expand Up @@ -393,6 +402,12 @@ find_files_dialog::browse_folders ()
combobox_insert_current_item (m_start_dir_edit, dir);
}

void
find_files_dialog::start_dir_to_cwd ()
{
combobox_insert_current_item (m_start_dir_edit, QDir::currentPath ());
}

void
find_files_dialog::item_double_clicked (const QModelIndex& idx)
{
Expand Down
2 changes: 2 additions & 0 deletions libgui/src/find-files-dialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ private Q_SLOTS:
void start_find ();
void stop_find ();
void browse_folders ();
void start_dir_to_cwd ();
void look_for_files ();
void item_double_clicked (const QModelIndex&);
void handle_done (int);
Expand All @@ -85,6 +86,7 @@ private Q_SLOTS:
QPushButton *m_find_button;
QPushButton *m_close_button;
QPushButton *m_browse_button;
QPushButton *m_current_dir_button;
QTableView *m_file_list;
QTimer *m_timer;
QCheckBox *m_recurse_dirs_check;
Expand Down
2 changes: 1 addition & 1 deletion libgui/src/gui-utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ combobox_insert_current_item (QComboBox *cb, const QString &text)
if (item_text.isEmpty ())
item_text = cb->currentText ();

int idx;;
int idx;
while ((idx = cb->findText (item_text)) >= 0)
cb->removeItem (idx);

Expand Down
5 changes: 2 additions & 3 deletions libgui/src/main-window.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1940,7 +1940,6 @@ main_window::handle_set_path_dialog_request ()
void
main_window::find_files (const QString& start_dir)
{

if (! m_find_files_dlg)
{
m_find_files_dlg = new find_files_dialog (this);
Expand All @@ -1962,10 +1961,10 @@ main_window::find_files (const QString& start_dir)
m_find_files_dlg->show ();
}

m_find_files_dlg->set_search_dir (start_dir);
if (! start_dir.isEmpty ())
m_find_files_dlg->set_search_dir (start_dir);

m_find_files_dlg->activateWindow ();

}

void
Expand Down
2 changes: 1 addition & 1 deletion libgui/src/main-window.h
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ public Q_SLOTS:

//! Find files dialog.
//!@{
void find_files (const QString& startdir = QDir::currentPath ());
void find_files (const QString& startdir = QString ());
void find_files_finished (int) { }
//!@}

Expand Down

0 comments on commit b94ba63

Please sign in to comment.