Commit 57ca90b8 authored by Eike Ziller's avatar Eike Ziller
Browse files

BaseFileFind/FindInFiles: Clean up enabled / valid state handling



IFindFilter:  Separate enabledChanged and validChanged
SearchEngine: Add enabledChanged
GitGrep:      Enable widget based on enabled state
FindInFiles:  Update validity on search engine enabledChanged instead of
              hardcoding it to when the search path changed

Change-Id: I0c684423b871a3a4f1f164574f42e23b64cf9da0
Reviewed-by: David Schulz's avatarDavid Schulz <david.schulz@qt.io>
parent 62a842e4
...@@ -196,12 +196,17 @@ void FindToolWindow::setCurrentFilter(int index) ...@@ -196,12 +196,17 @@ void FindToolWindow::setCurrentFilter(int index)
QWidget *configWidget = m_configWidgets.at(i); QWidget *configWidget = m_configWidgets.at(i);
if (i == index) { if (i == index) {
m_configWidget = configWidget; m_configWidget = configWidget;
if (m_currentFilter) if (m_currentFilter) {
disconnect(m_currentFilter, &IFindFilter::enabledChanged, disconnect(m_currentFilter, &IFindFilter::enabledChanged,
this, &FindToolWindow::updateButtonStates); this, &FindToolWindow::updateButtonStates);
disconnect(m_currentFilter, &IFindFilter::validChanged,
this, &FindToolWindow::updateButtonStates);
}
m_currentFilter = m_filters.at(i); m_currentFilter = m_filters.at(i);
connect(m_currentFilter, &IFindFilter::enabledChanged, connect(m_currentFilter, &IFindFilter::enabledChanged,
this, &FindToolWindow::updateButtonStates); this, &FindToolWindow::updateButtonStates);
connect(m_currentFilter, &IFindFilter::validChanged,
this, &FindToolWindow::updateButtonStates);
updateButtonStates(); updateButtonStates();
if (m_configWidget) if (m_configWidget)
m_ui.configWidget->layout()->addWidget(m_configWidget); m_ui.configWidget->layout()->addWidget(m_configWidget);
......
...@@ -64,6 +64,7 @@ public: ...@@ -64,6 +64,7 @@ public:
static QString descriptionForFindFlags(FindFlags flags); static QString descriptionForFindFlags(FindFlags flags);
signals: signals:
void enabledChanged(bool enabled); void enabledChanged(bool enabled);
void validChanged(bool valid);
void displayNameChanged(); void displayNameChanged();
}; };
......
...@@ -198,7 +198,7 @@ private: ...@@ -198,7 +198,7 @@ private:
} // namespace } // namespace
static bool validateDirectory(const QString &path) static bool isGitDirectory(const QString &path)
{ {
static IVersionControl *gitVc = VcsManager::versionControl(VcsBase::Constants::VCS_ID_GIT); static IVersionControl *gitVc = VcsManager::versionControl(VcsBase::Constants::VCS_ID_GIT);
QTC_ASSERT(gitVc, return false); QTC_ASSERT(gitVc, return false);
...@@ -221,8 +221,9 @@ GitGrep::GitGrep() ...@@ -221,8 +221,9 @@ GitGrep::GitGrep()
QTC_ASSERT(findInFiles, return); QTC_ASSERT(findInFiles, return);
connect(findInFiles, &TextEditor::FindInFiles::pathChanged, connect(findInFiles, &TextEditor::FindInFiles::pathChanged,
m_widget, [this](const QString &path) { m_widget, [this](const QString &path) {
m_widget->setEnabled(validateDirectory(path)); setEnabled(isGitDirectory(path));
}); });
connect(this, &SearchEngine::enabledChanged, m_widget, &QWidget::setEnabled);
findInFiles->addSearchEngine(this); findInFiles->addSearchEngine(this);
} }
...@@ -249,11 +250,6 @@ QWidget *GitGrep::widget() const ...@@ -249,11 +250,6 @@ QWidget *GitGrep::widget() const
return m_widget; return m_widget;
} }
bool GitGrep::isEnabled() const
{
return m_widget->isEnabled();
}
QVariant GitGrep::parameters() const QVariant GitGrep::parameters() const
{ {
GitGrepParameters params; GitGrepParameters params;
......
...@@ -44,7 +44,6 @@ public: ...@@ -44,7 +44,6 @@ public:
QString title() const override; QString title() const override;
QString toolTip() const override; QString toolTip() const override;
QWidget *widget() const override; QWidget *widget() const override;
bool isEnabled() const override;
QVariant parameters() const override; QVariant parameters() const override;
void readSettings(QSettings *settings) override; void readSettings(QSettings *settings) override;
void writeSettings(QSettings *settings) const override; void writeSettings(QSettings *settings) const override;
......
...@@ -66,7 +66,6 @@ public: ...@@ -66,7 +66,6 @@ public:
QString title() const override { return tr("Internal"); } QString title() const override { return tr("Internal"); }
QString toolTip() const override { return QString(); } QString toolTip() const override { return QString(); }
QWidget *widget() const override { return m_widget; } QWidget *widget() const override { return m_widget; }
bool isEnabled() const override { return true; }
QVariant parameters() const override { return QVariant(); } QVariant parameters() const override { return QVariant(); }
void readSettings(QSettings * /*settings*/) override {} void readSettings(QSettings * /*settings*/) override {}
void writeSettings(QSettings * /*settings*/) const override {} void writeSettings(QSettings * /*settings*/) const override {}
...@@ -93,8 +92,13 @@ public: ...@@ -93,8 +92,13 @@ public:
private: private:
QWidget *m_widget; QWidget *m_widget;
}; };
} } // namespace
class SearchEnginePrivate
{
public:
bool isEnabled = true;
};
class CountingLabel : public QLabel class CountingLabel : public QLabel
{ {
...@@ -116,13 +120,36 @@ public: ...@@ -116,13 +120,36 @@ public:
QPointer<QComboBox> m_filterCombo; QPointer<QComboBox> m_filterCombo;
QVector<SearchEngine *> m_searchEngines; QVector<SearchEngine *> m_searchEngines;
SearchEngine *m_internalSearchEngine; SearchEngine *m_internalSearchEngine;
int m_currentSearchEngineIndex = 0; int m_currentSearchEngineIndex = -1;
}; };
} // namespace Internal } // namespace Internal
using namespace Internal; using namespace Internal;
SearchEngine::SearchEngine()
: d(new SearchEnginePrivate)
{
}
SearchEngine::~SearchEngine()
{
delete d;
}
bool SearchEngine::isEnabled() const
{
return d->isEnabled;
}
void SearchEngine::setEnabled(bool enabled)
{
if (enabled == d->isEnabled)
return;
d->isEnabled = enabled;
emit enabledChanged(d->isEnabled);
}
BaseFileFind::BaseFileFind() : d(new BaseFileFindPrivate) BaseFileFind::BaseFileFind() : d(new BaseFileFindPrivate)
{ {
d->m_internalSearchEngine = new InternalEngine; d->m_internalSearchEngine = new InternalEngine;
...@@ -186,7 +213,10 @@ QVector<SearchEngine *> BaseFileFind::searchEngines() const ...@@ -186,7 +213,10 @@ QVector<SearchEngine *> BaseFileFind::searchEngines() const
void BaseFileFind::setCurrentSearchEngine(int index) void BaseFileFind::setCurrentSearchEngine(int index)
{ {
if (d->m_currentSearchEngineIndex == index)
return;
d->m_currentSearchEngineIndex = index; d->m_currentSearchEngineIndex = index;
emit currentSearchEngineChanged();
} }
void BaseFileFind::runNewSearch(const QString &txt, FindFlags findFlags, void BaseFileFind::runNewSearch(const QString &txt, FindFlags findFlags,
......
...@@ -48,7 +48,10 @@ class SearchResultItem; ...@@ -48,7 +48,10 @@ class SearchResultItem;
namespace TextEditor { namespace TextEditor {
namespace Internal { class BaseFileFindPrivate; } namespace Internal {
class BaseFileFindPrivate;
class SearchEnginePrivate;
} // Internal
class TEXTEDITOR_EXPORT FileFindParameters class TEXTEDITOR_EXPORT FileFindParameters
{ {
...@@ -65,12 +68,13 @@ class BaseFileFind; ...@@ -65,12 +68,13 @@ class BaseFileFind;
class TEXTEDITOR_EXPORT SearchEngine : public QObject class TEXTEDITOR_EXPORT SearchEngine : public QObject
{ {
Q_OBJECT
public: public:
virtual ~SearchEngine() {} SearchEngine();
~SearchEngine();
virtual QString title() const = 0; virtual QString title() const = 0;
virtual QString toolTip() const = 0; // add %1 placeholder where the find flags should be put virtual QString toolTip() const = 0; // add %1 placeholder where the find flags should be put
virtual QWidget *widget() const = 0; virtual QWidget *widget() const = 0;
virtual bool isEnabled() const = 0;
virtual QVariant parameters() const = 0; virtual QVariant parameters() const = 0;
virtual void readSettings(QSettings *settings) = 0; virtual void readSettings(QSettings *settings) = 0;
virtual void writeSettings(QSettings *settings) const = 0; virtual void writeSettings(QSettings *settings) const = 0;
...@@ -78,7 +82,14 @@ public: ...@@ -78,7 +82,14 @@ public:
const FileFindParameters &parameters, BaseFileFind *baseFileFind) = 0; const FileFindParameters &parameters, BaseFileFind *baseFileFind) = 0;
virtual Core::IEditor *openEditor(const Core::SearchResultItem &item, virtual Core::IEditor *openEditor(const Core::SearchResultItem &item,
const FileFindParameters &parameters) = 0; const FileFindParameters &parameters) = 0;
bool isEnabled() const;
void setEnabled(bool enabled);
signals:
void enabledChanged(bool enabled);
private:
Internal::SearchEnginePrivate *d;
}; };
class TEXTEDITOR_EXPORT BaseFileFind : public Core::IFindFilter class TEXTEDITOR_EXPORT BaseFileFind : public Core::IFindFilter
...@@ -122,6 +133,9 @@ protected: ...@@ -122,6 +133,9 @@ protected:
void setCurrentSearchEngine(int index); void setCurrentSearchEngine(int index);
virtual void syncSearchEngineCombo(int /*selectedSearchEngineIndex*/) {} virtual void syncSearchEngineCombo(int /*selectedSearchEngineIndex*/) {}
signals:
void currentSearchEngineChanged();
private: private:
void displayResult(int index); void displayResult(int index);
void searchFinished(); void searchFinished();
......
...@@ -66,7 +66,7 @@ FindInFiles::~FindInFiles() ...@@ -66,7 +66,7 @@ FindInFiles::~FindInFiles()
bool FindInFiles::isValid() const bool FindInFiles::isValid() const
{ {
return m_directory->isValid() && currentSearchEngine()->isEnabled(); return m_isValid;
} }
QString FindInFiles::id() const QString FindInFiles::id() const
...@@ -124,7 +124,14 @@ void FindInFiles::syncSearchEngineCombo(int selectedSearchEngineIndex) ...@@ -124,7 +124,14 @@ void FindInFiles::syncSearchEngineCombo(int selectedSearchEngineIndex)
&& selectedSearchEngineIndex < searchEngines().size(), return); && selectedSearchEngineIndex < searchEngines().size(), return);
m_searchEngineCombo->setCurrentIndex(selectedSearchEngineIndex); m_searchEngineCombo->setCurrentIndex(selectedSearchEngineIndex);
searchEnginesSelectionChanged(selectedSearchEngineIndex); }
void FindInFiles::setValid(bool valid)
{
if (valid == m_isValid)
return;
m_isValid = valid;
emit validChanged(m_isValid);
} }
void FindInFiles::searchEnginesSelectionChanged(int index) void FindInFiles::searchEnginesSelectionChanged(int index)
...@@ -144,11 +151,9 @@ QWidget *FindInFiles::createConfigWidget() ...@@ -144,11 +151,9 @@ QWidget *FindInFiles::createConfigWidget()
int row = 0; int row = 0;
auto searchEngineLabel = new QLabel(tr("Search engine:")); auto searchEngineLabel = new QLabel(tr("Search engine:"));
gridLayout->addWidget(searchEngineLabel, row, 0, Qt::AlignRight); gridLayout->addWidget(searchEngineLabel, row, 0, Qt::AlignRight);
m_searchEngineCombo = new QComboBox; m_searchEngineCombo = new QComboBox;
auto cc = static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged); auto cc = static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged);
connect(m_searchEngineCombo, cc, this, &FindInFiles::searchEnginesSelectionChanged); connect(m_searchEngineCombo, cc, this, &FindInFiles::searchEnginesSelectionChanged);
connect(m_searchEngineCombo, cc, this, &FindInFiles::enabledChanged);
searchEngineLabel->setBuddy(m_searchEngineCombo); searchEngineLabel->setBuddy(m_searchEngineCombo);
gridLayout->addWidget(m_searchEngineCombo, row, 1); gridLayout->addWidget(m_searchEngineCombo, row, 1);
...@@ -165,10 +170,7 @@ QWidget *FindInFiles::createConfigWidget() ...@@ -165,10 +170,7 @@ QWidget *FindInFiles::createConfigWidget()
m_directory->setExpectedKind(PathChooser::ExistingDirectory); m_directory->setExpectedKind(PathChooser::ExistingDirectory);
m_directory->setPromptDialogTitle(tr("Directory to Search")); m_directory->setPromptDialogTitle(tr("Directory to Search"));
connect(m_directory.data(), &PathChooser::pathChanged, connect(m_directory.data(), &PathChooser::pathChanged,
this, [this](const QString &path) { this, &FindInFiles::pathChanged);
emit FindInFiles::pathChanged(path);
emit FindInFiles::enabledChanged(isEnabled());
});
m_directory->setHistoryCompleter(QLatin1String(HistoryKey), m_directory->setHistoryCompleter(QLatin1String(HistoryKey),
/*restoreLastItemFromHistory=*/ true); /*restoreLastItemFromHistory=*/ true);
if (!HistoryCompleter::historyExistsFor(QLatin1String(HistoryKey))) { if (!HistoryCompleter::historyExistsFor(QLatin1String(HistoryKey))) {
...@@ -190,6 +192,16 @@ QWidget *FindInFiles::createConfigWidget() ...@@ -190,6 +192,16 @@ QWidget *FindInFiles::createConfigWidget()
gridLayout->addWidget(filePatternLabel, row, 0); gridLayout->addWidget(filePatternLabel, row, 0);
gridLayout->addWidget(patternWidget, row++, 1, 1, 2); gridLayout->addWidget(patternWidget, row++, 1, 1, 2);
m_configWidget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred); m_configWidget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
// validity
auto updateValidity = [this]() {
setValid(currentSearchEngine()->isEnabled() && m_directory->isValid());
};
connect(this, &BaseFileFind::currentSearchEngineChanged, this, updateValidity);
foreach (SearchEngine *searchEngine, searchEngines())
connect(searchEngine, &SearchEngine::enabledChanged, this, updateValidity);
connect(m_directory.data(), &PathChooser::validChanged, this, updateValidity);
updateValidity();
} }
return m_configWidget; return m_configWidget;
} }
......
...@@ -73,6 +73,7 @@ protected: ...@@ -73,6 +73,7 @@ protected:
void syncSearchEngineCombo(int selectedSearchEngineIndex) override; void syncSearchEngineCombo(int selectedSearchEngineIndex) override;
private: private:
void setValid(bool valid);
void searchEnginesSelectionChanged(int index); void searchEnginesSelectionChanged(int index);
Utils::FileName path() const; Utils::FileName path() const;
...@@ -80,6 +81,7 @@ private: ...@@ -80,6 +81,7 @@ private:
QPointer<Utils::PathChooser> m_directory; QPointer<Utils::PathChooser> m_directory;
QStackedWidget *m_searchEngineWidget = nullptr; QStackedWidget *m_searchEngineWidget = nullptr;
QComboBox *m_searchEngineCombo = nullptr; QComboBox *m_searchEngineCombo = nullptr;
bool m_isValid = false;
}; };
} // namespace TextEditor } // namespace TextEditor
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment