locator.cpp 11.2 KB
Newer Older
1 2
/****************************************************************************
**
Eike Ziller's avatar
Eike Ziller committed
3 4
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing
5 6 7 8 9 10 11
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
Eike Ziller's avatar
Eike Ziller committed
12 13
** a written agreement between you and The Qt Company.  For licensing terms and
** conditions see http://www.qt.io/terms-conditions.  For further information
Eike Ziller's avatar
Eike Ziller committed
14
** use the contact form at http://www.qt.io/contact-us.
15 16 17
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
Eike Ziller's avatar
Eike Ziller committed
18 19 20 21 22 23
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file.  Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24
**
Eike Ziller's avatar
Eike Ziller committed
25 26
** In addition, as a special exception, The Qt Company gives you certain additional
** rights.  These rights are described in The Qt Company LGPL Exception
27 28 29 30
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/

hjk's avatar
hjk committed
31
#include "locator.h"
32 33 34 35 36 37
#include "locatorconstants.h"
#include "locatorfiltersfilter.h"
#include "locatormanager.h"
#include "locatorwidget.h"
#include "opendocumentsfilter.h"
#include "filesystemfilter.h"
38
#include "locatorsettingspage.h"
39 40 41 42 43 44 45 46

#include <coreplugin/coreplugin.h>
#include <coreplugin/statusbarwidget.h>
#include <coreplugin/coreconstants.h>
#include <coreplugin/settingsdatabase.h>
#include <coreplugin/icore.h>
#include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/actionmanager/actioncontainer.h>
47
#include <coreplugin/editormanager/editormanager.h>
48
#include <coreplugin/editormanager/editormanager_p.h>
49 50 51
#include <coreplugin/progressmanager/progressmanager.h>
#include <coreplugin/progressmanager/futureprogress.h>
#include <extensionsystem/pluginmanager.h>
52
#include <utils/algorithm.h>
53 54 55 56 57 58 59 60 61 62 63
#include <utils/QtConcurrentTools>
#include <utils/qtcassert.h>

#include <QSettings>
#include <QtPlugin>
#include <QFuture>
#include <QAction>

namespace Core {
namespace Internal {

hjk's avatar
hjk committed
64
Locator::Locator()
65 66 67 68 69 70 71
    : m_settingsInitialized(false)
{
    m_corePlugin = 0;
    m_refreshTimer.setSingleShot(false);
    connect(&m_refreshTimer, SIGNAL(timeout()), this, SLOT(refresh()));
}

hjk's avatar
hjk committed
72
Locator::~Locator()
73 74 75 76 77 78 79 80 81 82 83 84
{
    m_corePlugin->removeObject(m_openDocumentsFilter);
    m_corePlugin->removeObject(m_fileSystemFilter);
    m_corePlugin->removeObject(m_executeFilter);
    m_corePlugin->removeObject(m_settingsPage);
    delete m_openDocumentsFilter;
    delete m_fileSystemFilter;
    delete m_executeFilter;
    delete m_settingsPage;
    qDeleteAll(m_customFilters);
}

hjk's avatar
hjk committed
85
void Locator::initialize(CorePlugin *corePlugin, const QStringList &, QString *)
86 87 88
{
    m_corePlugin = corePlugin;

89
    m_settingsPage = new LocatorSettingsPage(this);
90 91 92 93 94 95 96 97 98 99 100
    m_corePlugin->addObject(m_settingsPage);

    m_locatorWidget = new LocatorWidget(this);
    m_locatorWidget->setEnabled(false);
    StatusBarWidget *view = new StatusBarWidget;
    view->setWidget(m_locatorWidget);
    view->setContext(Context("LocatorWidget"));
    view->setPosition(StatusBarWidget::First);
    m_corePlugin->addAutoReleasedObject(view);

    QAction *action = new QAction(m_locatorWidget->windowIcon(), m_locatorWidget->windowTitle(), this);
101
    Command *cmd = ActionManager::registerAction(action, Constants::LOCATE);
102
    cmd->setDefaultKeySequence(QKeySequence(tr("Ctrl+K")));
Montel Laurent's avatar
Montel Laurent committed
103
    connect(action, &QAction::triggered, this, &Locator::openLocator);
104 105 106
    connect(cmd, SIGNAL(keySequenceChanged()), this, SLOT(updatePlaceholderText()));
    updatePlaceholderText(cmd);

Orgad Shaneh's avatar
Orgad Shaneh committed
107
    ActionContainer *mtools = ActionManager::actionContainer(Constants::M_TOOLS);
108 109 110 111 112 113 114 115 116 117 118 119 120 121
    mtools->addAction(cmd);

    m_corePlugin->addObject(new LocatorManager(m_locatorWidget));

    m_openDocumentsFilter = new OpenDocumentsFilter;
    m_corePlugin->addObject(m_openDocumentsFilter);

    m_fileSystemFilter = new FileSystemFilter(m_locatorWidget);
    m_corePlugin->addObject(m_fileSystemFilter);

    m_executeFilter = new ExecuteFilter();
    m_corePlugin->addObject(m_executeFilter);

    m_corePlugin->addAutoReleasedObject(new LocatorFiltersFilter(this, m_locatorWidget));
122 123

    connect(ICore::instance(), SIGNAL(saveSettingsRequested()), this, SLOT(saveSettings()));
124 125
}

hjk's avatar
hjk committed
126
void Locator::updatePlaceholderText(Command *command)
127 128 129 130 131 132 133 134 135 136 137
{
    if (!command)
        command = qobject_cast<Command *>(sender());
    QTC_ASSERT(command, return);
    if (command->keySequence().isEmpty())
        m_locatorWidget->setPlaceholderText(tr("Type to locate"));
    else
        m_locatorWidget->setPlaceholderText(tr("Type to locate (%1)").arg(
                                                command->keySequence().toString(QKeySequence::NativeText)));
}

hjk's avatar
hjk committed
138
void Locator::openLocator()
139 140 141 142
{
    m_locatorWidget->show(QString());
}

hjk's avatar
hjk committed
143
void Locator::extensionsInitialized()
144 145
{
    m_filters = ExtensionSystem::PluginManager::getObjects<ILocatorFilter>();
146 147 148 149 150
    Utils::sort(m_filters, [](const ILocatorFilter *first, const ILocatorFilter *second) -> bool {
        if (first->priority() != second->priority())
            return first->priority() < second->priority();
        return first->id().alphabeticallyBefore(second->id());
    });
151
    setFilters(m_filters);
152 153 154 155 156 157 158 159

    Command *openCommand = ActionManager::command(Constants::OPEN);
    Command *locateCommand = ActionManager::command(Constants::LOCATE);
    connect(openCommand, &Command::keySequenceChanged,
            this, &Locator::updateEditorManagerPlaceholderText);
    connect(locateCommand, &Command::keySequenceChanged,
            this, &Locator::updateEditorManagerPlaceholderText);
    updateEditorManagerPlaceholderText();
160 161
}

hjk's avatar
hjk committed
162
bool Locator::delayedInitialize()
163 164 165 166 167
{
    loadSettings();
    return true;
}

hjk's avatar
hjk committed
168
void Locator::loadSettings()
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
{
    QSettings *qs = ICore::settings();

    // Backwards compatibility to old settings location
    if (qs->contains(QLatin1String("QuickOpen/FiltersFilter"))) {
        loadSettingsHelper(qs);
    } else {
        SettingsDatabase *settings = ICore::settingsDatabase();
        loadSettingsHelper(settings);
    }

    qs->remove(QLatin1String("QuickOpen"));

    m_locatorWidget->updateFilterList();
    m_locatorWidget->setEnabled(true);
    if (m_refreshTimer.interval() > 0)
        m_refreshTimer.start();
    m_settingsInitialized = true;
}

189 190 191 192
void Locator::updateEditorManagerPlaceholderText()
{
    Command *openCommand = ActionManager::command(Constants::OPEN);
    Command *locateCommand = ActionManager::command(Constants::LOCATE);
193
    const QString placeholderText = tr("<html><body style=\"color:#909090; font-size:14px\">"
194
          "<div align='center'>"
195
          "<div style=\"font-size:20px\">Open a document</div>"
196 197 198 199
          "<table><tr><td>"
          "<hr/>"
          "<div style=\"margin-top: 5px\">&bull; File > Open File or Project (%1)</div>"
          "<div style=\"margin-top: 5px\">&bull; File > Recent Files</div>"
Eike Ziller's avatar
Eike Ziller committed
200
          "<div style=\"margin-top: 5px\">&bull; Tools > Locate (%2) and</div>"
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
          "<div style=\"margin-left: 1em\">- type to open file from any open project</div>"
          "%4"
          "%5"
          "<div style=\"margin-left: 1em\">- type <code>%3&lt;space&gt;&lt;filename&gt;</code> to open file from file system</div>"
          "<div style=\"margin-left: 1em\">- select one of the other filters for jumping to a location</div>"
          "<div style=\"margin-top: 5px\">&bull; Drag and drop files here</div>"
          "</td></tr></table>"
          "</div>"
          "</body></html>")
         .arg(openCommand->keySequence().toString(QKeySequence::NativeText))
         .arg(locateCommand->keySequence().toString(QKeySequence::NativeText))
         .arg(m_fileSystemFilter->shortcutString());

    QString classes;
    ILocatorFilter *classesFilter = Utils::findOrDefault(m_filters, [](const ILocatorFilter *filter) {
        return filter->id() == Id("Classes"); // not nice, but anyhow
    });
    if (classesFilter)
        classes = tr("<div style=\"margin-left: 1em\">- type <code>%1&lt;space&gt;&lt;pattern&gt;</code>"
                     " to jump to a class definition</div>").arg(classesFilter->shortcutString());

    QString methods;
    ILocatorFilter *methodsFilter = Utils::findOrDefault(m_filters, [](const ILocatorFilter *filter) {
        return filter->id() == Id("Methods"); // not nice, but anyhow
    });
    if (methodsFilter)
        methods = tr("<div style=\"margin-left: 1em\">- type <code>%1&lt;space&gt;&lt;pattern&gt;</code>"
                     " to jump to a function definition</div>").arg(methodsFilter->shortcutString());

230
    EditorManagerPrivate::setPlaceholderText(placeholderText.arg(classes, methods));
231 232
}

hjk's avatar
hjk committed
233
void Locator::saveSettings()
234 235 236
{
    if (m_settingsInitialized) {
        SettingsDatabase *s = ICore::settingsDatabase();
237
        s->beginTransaction();
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
        s->beginGroup(QLatin1String("QuickOpen"));
        s->remove(QString());
        s->setValue(QLatin1String("RefreshInterval"), refreshInterval());
        foreach (ILocatorFilter *filter, m_filters) {
            if (!m_customFilters.contains(filter))
                s->setValue(filter->id().toString(), filter->saveState());
        }
        s->beginGroup(QLatin1String("CustomFilters"));
        int i = 0;
        foreach (ILocatorFilter *filter, m_customFilters) {
            s->setValue(QLatin1String("directory") + QString::number(i),
                        filter->saveState());
            ++i;
        }
        s->endGroup();
        s->endGroup();
254
        s->endTransaction();
255 256 257 258 259 260
    }
}

/*!
    Return all filters, including the ones created by the user.
*/
hjk's avatar
hjk committed
261
QList<ILocatorFilter *> Locator::filters()
262 263 264 265 266 267 268 269
{
    return m_filters;
}

/*!
    This returns a subset of all the filters, that contains only the filters that
    have been created by the user at some point (maybe in a previous session).
 */
hjk's avatar
hjk committed
270
QList<ILocatorFilter *> Locator::customFilters()
271 272 273 274
{
    return m_customFilters;
}

hjk's avatar
hjk committed
275
void Locator::setFilters(QList<ILocatorFilter *> f)
276 277
{
    m_filters = f;
278
    updateEditorManagerPlaceholderText(); // possibly some shortcut changed
279 280 281
    m_locatorWidget->updateFilterList();
}

hjk's avatar
hjk committed
282
void Locator::setCustomFilters(QList<ILocatorFilter *> filters)
283 284 285 286
{
    m_customFilters = filters;
}

hjk's avatar
hjk committed
287
int Locator::refreshInterval()
288 289 290 291
{
    return m_refreshTimer.interval() / 60000;
}

hjk's avatar
hjk committed
292
void Locator::setRefreshInterval(int interval)
293 294 295 296 297 298 299 300 301 302
{
    if (interval < 1) {
        m_refreshTimer.stop();
        m_refreshTimer.setInterval(0);
        return;
    }
    m_refreshTimer.setInterval(interval * 60000);
    m_refreshTimer.start();
}

hjk's avatar
hjk committed
303
void Locator::refresh(QList<ILocatorFilter *> filters)
304 305 306 307 308
{
    if (filters.isEmpty())
        filters = m_filters;
    QFuture<void> task = QtConcurrent::run(&ILocatorFilter::refresh, filters);
    FutureProgress *progress =
Orgad Shaneh's avatar
Orgad Shaneh committed
309
        ProgressManager::addTask(task, tr("Updating Locator Caches"), Constants::TASK_INDEX);
Montel Laurent's avatar
Montel Laurent committed
310
    connect(progress, &FutureProgress::finished, this, &Locator::saveSettings);
311 312 313 314
}

} // namespace Internal
} // namespace Core