findincurrentfile.cpp 4.09 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
con's avatar
con committed
2
**
3
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
hjk's avatar
hjk committed
4
** Contact: http://www.qt-project.org/legal
con's avatar
con committed
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
con's avatar
con committed
7
**
hjk's avatar
hjk committed
8
9
10
11
12
13
14
** 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
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
con's avatar
con committed
15
16
**
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17
18
19
20
21
22
23
24
25
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
con's avatar
con committed
26
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
28
****************************************************************************/
con's avatar
con committed
29
30

#include "findincurrentfile.h"
31
#include "basetexteditor.h"
con's avatar
con committed
32

33
#include <utils/filesearch.h>
con's avatar
con committed
34
#include <coreplugin/icore.h>
35
#include <coreplugin/editormanager/ieditor.h>
con's avatar
con committed
36
37
#include <coreplugin/editormanager/editormanager.h>

38
#include <QSettings>
con's avatar
con committed
39

40
using namespace TextEditor;
con's avatar
con committed
41
42
using namespace TextEditor::Internal;

43
FindInCurrentFile::FindInCurrentFile()
44
  : m_currentDocument(0)
con's avatar
con committed
45
{
46
    connect(Core::EditorManager::instance(), SIGNAL(currentEditorChanged(Core::IEditor*)),
47
            this, SLOT(handleFileChange(Core::IEditor*)));
hjk's avatar
hjk committed
48
    handleFileChange(Core::EditorManager::currentEditor());
con's avatar
con committed
49
50
51
52
}

QString FindInCurrentFile::id() const
{
53
    return QLatin1String("Current File");
con's avatar
con committed
54
55
}

56
QString FindInCurrentFile::displayName() const
con's avatar
con committed
57
58
59
60
{
    return tr("Current File");
}

61
62
Utils::FileIterator *FindInCurrentFile::files(const QStringList &nameFilters,
                                              const QVariant &additionalParameters) const
con's avatar
con committed
63
{
64
65
    Q_UNUSED(nameFilters)
    QString fileName = additionalParameters.toString();
66
    QMap<QString, QTextCodec *> openEditorEncodings = BaseTextDocument::openedTextDocumentEncodings();
67
68
    QTextCodec *codec = openEditorEncodings.value(fileName);
    if (!codec)
69
        codec = Core::EditorManager::defaultTextCodec();
70
    return new Utils::FileIterator(QStringList() << fileName, QList<QTextCodec *>() << codec);
con's avatar
con committed
71
72
}

73
74
QVariant FindInCurrentFile::additionalParameters() const
{
75
    return qVariantFromValue(m_currentDocument->filePath());
76
77
}

78
79
QString FindInCurrentFile::label() const
{
80
    return tr("File \"%1\":").arg(QFileInfo(m_currentDocument->filePath()).fileName());
81
82
83
84
}

QString FindInCurrentFile::toolTip() const
{
85
    // %2 is filled by BaseFileFind::runNewSearch
86
    return tr("File path: %1\n%2").arg(QDir::toNativeSeparators(m_currentDocument->filePath()));
87
88
}

con's avatar
con committed
89
90
bool FindInCurrentFile::isEnabled() const
{
91
    return m_currentDocument && !m_currentDocument->filePath().isEmpty();
92
93
94
95
96
}

void FindInCurrentFile::handleFileChange(Core::IEditor *editor)
{
    if (!editor) {
97
98
        if (m_currentDocument) {
            m_currentDocument = 0;
99
            emit enabledChanged(isEnabled());
100
101
        }
    } else {
102
103
104
        Core::IDocument *document = editor->document();
        if (document != m_currentDocument) {
            m_currentDocument = document;
105
            emit enabledChanged(isEnabled());
106
107
        }
    }
con's avatar
con committed
108
109
}

110

con's avatar
con committed
111
112
void FindInCurrentFile::writeSettings(QSettings *settings)
{
113
    settings->beginGroup(QLatin1String("FindInCurrentFile"));
con's avatar
con committed
114
115
116
117
118
119
    writeCommonSettings(settings);
    settings->endGroup();
}

void FindInCurrentFile::readSettings(QSettings *settings)
{
120
    settings->beginGroup(QLatin1String("FindInCurrentFile"));
Eike Ziller's avatar
Eike Ziller committed
121
    readCommonSettings(settings, QLatin1String("*"));
con's avatar
con committed
122
123
    settings->endGroup();
}