Commit 923c20bd authored by Eike Ziller's avatar Eike Ziller

Locator: Move filter action creation out of input field widget

Change-Id: I380e28444b33caa2c3c2724ad2933a13fdcb312a
Reviewed-by: David Schulz's avatarDavid Schulz <david.schulz@qt.io>
parent 93f4fb77
......@@ -324,6 +324,11 @@ Id ILocatorFilter::id() const
return m_id;
}
Id ILocatorFilter::actionId() const
{
return m_id.withPrefix("Locator.");
}
/*!
Returns the filter's translated display name.
......
......@@ -105,6 +105,7 @@ public:
virtual ~ILocatorFilter() {}
Id id() const;
Id actionId() const;
QString displayName() const;
......
......@@ -62,8 +62,11 @@
namespace Core {
namespace Internal {
static Locator *m_instance = nullptr;
Locator::Locator()
{
m_instance = this;
m_refreshTimer.setSingleShot(false);
connect(&m_refreshTimer, &QTimer::timeout, this, [this]() { refresh(); });
}
......@@ -121,7 +124,7 @@ void Locator::initialize(CorePlugin *corePlugin, const QStringList &, QString *)
m_externalToolsFilter = new ExternalToolsFilter;
m_corePlugin->addObject(m_externalToolsFilter);
m_corePlugin->addAutoReleasedObject(new LocatorFiltersFilter(this));
m_corePlugin->addAutoReleasedObject(new LocatorFiltersFilter);
#ifdef Q_OS_OSX
m_corePlugin->addAutoReleasedObject(new SpotlightLocatorFilter);
#endif
......@@ -175,7 +178,6 @@ void Locator::loadSettings()
foreach (const QString &key, keys) {
ILocatorFilter *filter = new DirectoryFilter(baseId.withSuffix(++count));
filter->restoreState(settings->value(key).toByteArray());
m_filters.append(filter);
customFilters.append(filter);
}
setCustomFilters(customFilters);
......@@ -185,7 +187,41 @@ void Locator::loadSettings()
if (m_refreshTimer.interval() > 0)
m_refreshTimer.start();
m_settingsInitialized = true;
emit filtersChanged();
setFilters(m_filters + customFilters);
}
void Locator::updateFilterActions()
{
QMap<Id, QAction *> actionCopy = m_filterActionMap;
m_filterActionMap.clear();
// register new actions, update existent
for (ILocatorFilter *filter : m_filters) {
if (filter->shortcutString().isEmpty() || filter->isHidden())
continue;
Id filterId = filter->id();
Id actionId = filter->actionId();
QAction *action = 0;
if (!actionCopy.contains(filterId)) {
// register new action
action = new QAction(filter->displayName(), this);
Command *cmd = ActionManager::registerAction(action, actionId);
cmd->setAttribute(Command::CA_UpdateText);
connect(action, &QAction::triggered, this, [filter] {
LocatorManager::showFilter(filter);
});
} else {
action = actionCopy.take(filterId);
action->setText(filter->displayName());
}
m_filterActionMap.insert(filterId, action);
}
// unregister actions that are deleted now
const auto end = actionCopy.end();
for (auto it = actionCopy.begin(); it != end; ++it) {
ActionManager::unregisterAction(it.value(), it.key().withPrefix("Locator."));
delete it.value();
}
}
void Locator::updateEditorManagerPlaceholderText()
......@@ -263,7 +299,7 @@ void Locator::saveSettings()
*/
QList<ILocatorFilter *> Locator::filters()
{
return m_filters;
return m_instance->m_filters;
}
/*!
......@@ -278,6 +314,7 @@ QList<ILocatorFilter *> Locator::customFilters()
void Locator::setFilters(QList<ILocatorFilter *> f)
{
m_filters = f;
updateFilterActions();
updateEditorManagerPlaceholderText(); // possibly some shortcut changed
emit filtersChanged();
}
......
......@@ -57,7 +57,7 @@ public:
void extensionsInitialized();
bool delayedInitialize();
QList<ILocatorFilter *> filters();
static QList<ILocatorFilter *> filters();
QList<ILocatorFilter *> customFilters();
void setFilters(QList<ILocatorFilter *> f);
void setCustomFilters(QList<ILocatorFilter *> f);
......@@ -73,6 +73,7 @@ public slots:
private:
void loadSettings();
void updateFilterActions();
void updateEditorManagerPlaceholderText();
LocatorSettingsPage *m_settingsPage;
......@@ -80,6 +81,7 @@ private:
bool m_settingsInitialized = false;
QList<ILocatorFilter *> m_filters;
QList<ILocatorFilter *> m_customFilters;
QMap<Id, QAction *> m_filterActionMap;
int m_refreshInterval;
QTimer m_refreshTimer;
OpenDocumentsFilter *m_openDocumentsFilter;
......
......@@ -35,8 +35,7 @@ using namespace Core::Internal;
Q_DECLARE_METATYPE(ILocatorFilter*)
LocatorFiltersFilter::LocatorFiltersFilter(Locator *plugin):
m_plugin(plugin),
LocatorFiltersFilter::LocatorFiltersFilter():
m_icon(Utils::Icons::NEXT.icon())
{
setId("FiltersFilter");
......@@ -55,7 +54,7 @@ void LocatorFiltersFilter::prepareSearch(const QString &entry)
return;
QMap<QString, ILocatorFilter *> uniqueFilters;
foreach (ILocatorFilter *filter, m_plugin->filters()) {
foreach (ILocatorFilter *filter, Locator::filters()) {
const QString filterId = filter->shortcutString() + QLatin1Char(',') + filter->displayName();
uniqueFilters.insert(filterId, filter);
}
......
......@@ -43,7 +43,7 @@ class LocatorFiltersFilter : public ILocatorFilter
Q_OBJECT
public:
LocatorFiltersFilter(Locator *plugin);
LocatorFiltersFilter();
// ILocatorFilter
void prepareSearch(const QString &entry) override;
......@@ -54,7 +54,6 @@ public:
void refresh(QFutureInterface<void> &future) override;
private:
Locator *m_plugin;
QStringList m_filterShortcutStrings;
QStringList m_filterDisplayNames;
QIcon m_icon;
......
......@@ -24,6 +24,9 @@
****************************************************************************/
#include "locatormanager.h"
#include "ilocatorfilter.h"
#include "locator.h"
#include "locatorwidget.h"
#include <extensionsystem/pluginmanager.h>
......@@ -39,6 +42,27 @@ LocatorManager::LocatorManager(Internal::LocatorWidget *locatorWidget)
m_locatorWidget = locatorWidget;
}
void LocatorManager::showFilter(ILocatorFilter *filter)
{
QTC_ASSERT(filter, return);
QTC_ASSERT(m_locatorWidget, return);
QString searchText = tr("<type here>");
const QString currentText = m_locatorWidget->currentText().trimmed();
// add shortcut string at front or replace existing shortcut string
if (!currentText.isEmpty()) {
searchText = currentText;
foreach (ILocatorFilter *otherfilter, Internal::Locator::filters()) {
if (currentText.startsWith(otherfilter->shortcutString() + QLatin1Char(' '))) {
searchText = currentText.mid(otherfilter->shortcutString().length() + 1);
break;
}
}
}
show(filter->shortcutString() + QLatin1Char(' ') + searchText,
filter->shortcutString().length() + 1,
searchText.length());
}
void LocatorManager::show(const QString &text,
int selectionStart, int selectionLength)
{
......
......@@ -31,6 +31,8 @@
namespace Core {
class ILocatorFilter;
namespace Internal { class LocatorWidget; }
class CORE_EXPORT LocatorManager : public QObject
......@@ -40,6 +42,7 @@ class CORE_EXPORT LocatorManager : public QObject
public:
LocatorManager(Internal::LocatorWidget *locatorWidget);
static void showFilter(ILocatorFilter *filter);
static void show(const QString &text, int selectionStart = -1, int selectionLength = 0);
};
......
......@@ -173,7 +173,7 @@ LocatorSettingsPage::LocatorSettingsPage(Locator *plugin)
QWidget *LocatorSettingsPage::widget()
{
if (!m_widget) {
m_filters = m_plugin->filters();
m_filters = Locator::filters();
m_customFilters = m_plugin->customFilters();
m_widget = new QWidget;
......
......@@ -39,6 +39,7 @@
#include <coreplugin/mainwindow.h>
#include <utils/algorithm.h>
#include <utils/appmainwindow.h>
#include <utils/asconst.h>
#include <utils/fancylineedit.h>
#include <utils/hostosinfo.h>
#include <utils/progressindicator.h>
......@@ -61,7 +62,6 @@
#include <QTreeView>
#include <QToolTip>
Q_DECLARE_METATYPE(Core::ILocatorFilter*)
Q_DECLARE_METATYPE(Core::LocatorFilterEntry)
namespace Core {
......@@ -269,8 +269,7 @@ void CompletionList::resizeHeaders()
// =========== LocatorWidget ===========
LocatorWidget::LocatorWidget(Locator *qop) :
m_locatorPlugin(qop),
LocatorWidget::LocatorWidget(Locator *locator) :
m_locatorModel(new LocatorModel(this)),
m_completionList(new CompletionList(this)),
m_filterMenu(new QMenu(this)),
......@@ -319,7 +318,7 @@ LocatorWidget::LocatorWidget(Locator *qop) :
m_fileLineEdit->setButtonMenu(Utils::FancyLineEdit::Left, m_filterMenu);
connect(m_refreshAction, &QAction::triggered,
m_locatorPlugin, [this]() { m_locatorPlugin->refresh(); });
locator, [locator]() { locator->refresh(); });
connect(m_configureAction, &QAction::triggered, this, &LocatorWidget::showConfigureDialog);
connect(m_fileLineEdit, &QLineEdit::textChanged,
this, &LocatorWidget::showPopup);
......@@ -352,7 +351,7 @@ LocatorWidget::LocatorWidget(Locator *qop) :
updatePlaceholderText(locateCmd);
}
connect(m_locatorPlugin, &Locator::filtersChanged, this, &LocatorWidget::updateFilterList);
connect(locator, &Locator::filtersChanged, this, &LocatorWidget::updateFilterList);
updateFilterList();
}
......@@ -368,44 +367,13 @@ void LocatorWidget::updatePlaceholderText(Command *command)
void LocatorWidget::updateFilterList()
{
typedef QMap<Id, QAction *> IdActionMap;
m_filterMenu->clear();
// update actions and menu
IdActionMap actionCopy = m_filterActionMap;
m_filterActionMap.clear();
// register new actions, update existent
foreach (ILocatorFilter *filter, m_locatorPlugin->filters()) {
if (filter->shortcutString().isEmpty() || filter->isHidden())
continue;
Id filterId = filter->id();
Id locatorId = filterId.withPrefix("Locator.");
QAction *action = 0;
Command *cmd = 0;
if (!actionCopy.contains(filterId)) {
// register new action
action = new QAction(filter->displayName(), this);
cmd = ActionManager::registerAction(action, locatorId);
cmd->setAttribute(Command::CA_UpdateText);
connect(action, &QAction::triggered, this, &LocatorWidget::filterSelected);
action->setData(qVariantFromValue(filter));
} else {
action = actionCopy.take(filterId);
action->setText(filter->displayName());
cmd = ActionManager::command(locatorId);
}
m_filterActionMap.insert(filterId, action);
m_filterMenu->addAction(cmd->action());
const QList<ILocatorFilter *> filters = Locator::filters();
for (ILocatorFilter *filter : filters) {
Command *cmd = ActionManager::command(filter->actionId());
if (cmd)
m_filterMenu->addAction(cmd->action());
}
// unregister actions that are deleted now
const IdActionMap::Iterator end = actionCopy.end();
for (IdActionMap::Iterator it = actionCopy.begin(); it != end; ++it) {
ActionManager::unregisterAction(it.value(), it.key().withPrefix("Locator."));
delete it.value();
}
m_filterMenu->addSeparator();
m_filterMenu->addAction(m_refreshAction);
m_filterMenu->addAction(m_configureAction);
......@@ -572,7 +540,7 @@ QList<ILocatorFilter *> LocatorWidget::filtersFor(const QString &text, QString &
break;
}
const int whiteSpace = text.indexOf(QChar::Space, firstNonSpace);
const QList<ILocatorFilter *> filters = m_locatorPlugin->filters();
const QList<ILocatorFilter *> filters = Locator::filters();
if (whiteSpace >= 0) {
const QString prefix = text.mid(firstNonSpace, whiteSpace - firstNonSpace).toLower();
QList<ILocatorFilter *> prefixFilters;
......@@ -702,29 +670,9 @@ void LocatorWidget::show(const QString &text, int selectionStart, int selectionL
}
}
void LocatorWidget::filterSelected()
QString LocatorWidget::currentText() const
{
QString searchText = tr("<type here>");
QAction *action = qobject_cast<QAction *>(sender());
QTC_ASSERT(action, return);
ILocatorFilter *filter = action->data().value<ILocatorFilter *>();
QTC_ASSERT(filter, return);
QString currentText = m_fileLineEdit->text().trimmed();
// add shortcut string at front or replace existing shortcut string
if (!currentText.isEmpty()) {
searchText = currentText;
foreach (ILocatorFilter *otherfilter, m_locatorPlugin->filters()) {
if (currentText.startsWith(otherfilter->shortcutString() + QLatin1Char(' '))) {
searchText = currentText.mid(otherfilter->shortcutString().length() + 1);
break;
}
}
}
show(filter->shortcutString() + QLatin1Char(' ') + searchText,
filter->shortcutString().length() + 1,
searchText.length());
updateCompletionList(m_fileLineEdit->text());
m_fileLineEdit->setFocus();
return m_fileLineEdit->text();
}
void LocatorWidget::showConfigureDialog()
......
......@@ -52,11 +52,12 @@ class LocatorWidget
Q_OBJECT
public:
explicit LocatorWidget(Locator *qop);
explicit LocatorWidget(Locator *locator);
void updateFilterList();
void show(const QString &text, int selectionStart = -1, int selectionLength = 0);
QString currentText() const;
void updatePlaceholderText(Command *command);
......@@ -64,7 +65,6 @@ private:
void showPopup();
void showPopupNow();
void acceptCurrentEntry();
void filterSelected();
void showConfigureDialog();
void addSearchResults(int firstIndex, int endIndex);
void handleSearchFinished();
......@@ -78,7 +78,6 @@ private:
QList<ILocatorFilter*> filtersFor(const QString &text, QString &searchText);
void setProgressIndicatorVisible(bool visible);
Locator *m_locatorPlugin;
LocatorModel *m_locatorModel;
CompletionList *m_completionList;
......@@ -88,7 +87,6 @@ private:
Utils::FancyLineEdit *m_fileLineEdit;
QTimer m_showPopupTimer;
QFutureWatcher<LocatorFilterEntry> *m_entriesWatcher;
QMap<Id, QAction *> m_filterActionMap;
QString m_requestedCompletionText;
bool m_needsClearResult = true;
bool m_updateRequested = false;
......
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