findtoolwindow.cpp 6.7 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
2
3
4
**
** This file is part of Qt Creator
**
hjk's avatar
hjk committed
5
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
6
**
7
** Contact: Nokia Corporation (qt-info@nokia.com)
con's avatar
con committed
8
**
9
** Commercial Usage
10
**
11
12
13
14
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
15
**
16
** GNU Lesser General Public License Usage
17
**
18
19
20
21
22
23
** 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.
24
**
25
** If you are unsure which license is appropriate for your use, please
hjk's avatar
hjk committed
26
** contact the sales department at http://qt.nokia.com/contact.
con's avatar
con committed
27
**
28
**************************************************************************/
hjk's avatar
hjk committed
29

con's avatar
con committed
30
31
32
33
34
#include "findtoolwindow.h"
#include "findplugin.h"

#include <coreplugin/icore.h>

35
#include <QtCore/QSettings>
con's avatar
con committed
36
#include <QtGui/QMainWindow>
37
38
#include <QtGui/QStringListModel>
#include <QtGui/QCompleter>
39
#include <QtGui/QKeyEvent>
con's avatar
con committed
40
41
42
43
44

using namespace Find;
using namespace Find::Internal;

FindToolWindow::FindToolWindow(FindPlugin *plugin)
45
    : QDialog(Core::ICore::instance()->mainWindow()),
con's avatar
con committed
46
    m_plugin(plugin),
47
48
    m_findCompleter(new QCompleter(this)),
    m_currentFilter(0)
con's avatar
con committed
49
50
51
{
    m_ui.setupUi(this);
    connect(m_ui.closeButton, SIGNAL(clicked()), this, SLOT(reject()));
52
53
    connect(m_ui.searchButton, SIGNAL(clicked()), this, SLOT(search()));
    connect(m_ui.replaceButton, SIGNAL(clicked()), this, SLOT(replace()));
con's avatar
con committed
54
55
    connect(m_ui.matchCase, SIGNAL(toggled(bool)), m_plugin, SLOT(setCaseSensitive(bool)));
    connect(m_ui.wholeWords, SIGNAL(toggled(bool)), m_plugin, SLOT(setWholeWord(bool)));
56
    connect(m_ui.filterList, SIGNAL(activated(int)), this, SLOT(setCurrentFilter(int)));
con's avatar
con committed
57
    connect(m_ui.searchTerm, SIGNAL(textChanged(QString)), this, SLOT(updateButtonStates()));
con's avatar
con committed
58
59
    m_findCompleter->setModel(m_plugin->findCompletionModel());
    m_ui.searchTerm->setCompleter(m_findCompleter);
60
    m_ui.searchTerm->installEventFilter(this);
con's avatar
con committed
61
62
63
64
    QVBoxLayout *layout = new QVBoxLayout;
    layout->setMargin(0);
    layout->setSpacing(0);
    m_ui.configWidget->setLayout(layout);
con's avatar
con committed
65
    updateButtonStates();
con's avatar
con committed
66
67
68
69
70
71
72
}

FindToolWindow::~FindToolWindow()
{
    qDeleteAll(m_configWidgets);
}

73
74
75
76
77
78
79
80
81
82
83
bool FindToolWindow::eventFilter(QObject *obj, QEvent *event)
{
    if (obj == m_ui.searchTerm && event->type() == QEvent::KeyPress) {
        QKeyEvent *ke = static_cast<QKeyEvent *>(event);
        if (ke->key() == Qt::Key_Down) {
            m_findCompleter->complete();
        }
    }
    return QDialog::eventFilter(obj, event);
}

con's avatar
con committed
84
85
86
87
88
89
90
91
92
void FindToolWindow::updateButtonStates()
{
    bool enabled = !m_ui.searchTerm->text().isEmpty()
                   && m_currentFilter && m_currentFilter->isEnabled();
    m_ui.searchButton->setEnabled(enabled);
    m_ui.replaceButton->setEnabled(m_currentFilter
                                   && m_currentFilter->isReplaceSupported() && enabled);
}

con's avatar
con committed
93
94
95
96
97
98
99
100
101
102
103
104
void FindToolWindow::setFindFilters(const QList<IFindFilter *> &filters)
{
    qDeleteAll(m_configWidgets);
    m_configWidgets.clear();
    m_filters = filters;
    m_ui.filterList->clear();
    QStringList names;
    foreach (IFindFilter *filter, filters) {
        names << filter->name();
        m_configWidgets.append(filter->createConfigWidget());
    }
    m_ui.filterList->addItems(names);
105
106
    if (m_filters.size() > 0)
        setCurrentFilter(0);
con's avatar
con committed
107
108
109
110
111
112
113
114
115
}

void FindToolWindow::setFindText(const QString &text)
{
    m_ui.searchTerm->setText(text);
}

void FindToolWindow::open(IFindFilter *filter)
{
116
117
    if (!filter)
        filter = m_currentFilter;
con's avatar
con committed
118
119
    int index = m_filters.indexOf(filter);
    if (index >= 0) {
120
        setCurrentFilter(index);
con's avatar
con committed
121
122
123
124
125
126
127
128
129
130
    }
    m_ui.matchCase->setChecked(m_plugin->findFlags() & QTextDocument::FindCaseSensitively);
    m_ui.wholeWords->setChecked(m_plugin->findFlags() & QTextDocument::FindWholeWords);
    m_ui.searchTerm->setFocus();
    m_ui.searchTerm->selectAll();
    exec();
}

void FindToolWindow::setCurrentFilter(int index)
{
131
    m_ui.filterList->setCurrentIndex(index);
con's avatar
con committed
132
133
    for (int i = 0; i < m_configWidgets.size(); ++i) {
        QWidget *configWidget = m_configWidgets.at(i);
134
        if (i == index) {
con's avatar
con committed
135
136
            m_currentFilter = m_filters.at(i);
            bool enabled = m_currentFilter->isEnabled();
137
138
139
            m_ui.matchCase->setEnabled(enabled);
            m_ui.wholeWords->setEnabled(enabled);
            m_ui.searchTerm->setEnabled(enabled);
con's avatar
con committed
140
141
142
143
144
            updateButtonStates();
            if (configWidget) {
                configWidget->setEnabled(enabled);
                m_ui.configWidget->layout()->addWidget(configWidget);
            }
145
        } else {
con's avatar
con committed
146
147
            if (configWidget)
                configWidget->setParent(0);
148
        }
con's avatar
con committed
149
150
151
    }
}

152
void FindToolWindow::acceptAndGetParameters(QString *term, IFindFilter **filter)
con's avatar
con committed
153
{
154
155
156
    if (filter)
        *filter = 0;
    accept();
con's avatar
con committed
157
158
    m_plugin->updateFindCompletion(m_ui.searchTerm->text());
    int index = m_ui.filterList->currentIndex();
159
160
161
162
    QString searchTerm = m_ui.searchTerm->text();
    if (term)
        *term = searchTerm;
    if (searchTerm.isEmpty() || index < 0)
con's avatar
con committed
163
        return;
164
165
166
167
168
169
170
171
172
    if (filter)
        *filter = m_filters.at(index);
}

void FindToolWindow::search()
{
    QString term;
    IFindFilter *filter;
    acceptAndGetParameters(&term, &filter);
con's avatar
con committed
173
174
    if (filter)
        filter->findAll(term, m_plugin->findFlags());
con's avatar
con committed
175
176
}

177
178
179
180
181
182
183
184
void FindToolWindow::replace()
{
    QString term;
    IFindFilter *filter;
    acceptAndGetParameters(&term, &filter);
    filter->replaceAll(term, m_plugin->findFlags());
}

con's avatar
con committed
185
186
void FindToolWindow::writeSettings()
{
187
    QSettings *settings = Core::ICore::instance()->settings();
con's avatar
con committed
188
    settings->beginGroup("Find");
189
    settings->setValue("CurrentFilter", m_currentFilter ? m_currentFilter->id() : 0);
con's avatar
con committed
190
191
192
193
194
195
196
    foreach (IFindFilter *filter, m_filters)
        filter->writeSettings(settings);
    settings->endGroup();
}

void FindToolWindow::readSettings()
{
197
    QSettings *settings = Core::ICore::instance()->settings();
con's avatar
con committed
198
    settings->beginGroup("Find");
199
200
201
    const QString currentFilter = settings->value("CurrentFilter").toString();
    for (int i = 0; i < m_filters.size(); ++i) {
        IFindFilter *filter = m_filters.at(i);
con's avatar
con committed
202
        filter->readSettings(settings);
203
204
205
206
        if (filter->id() == currentFilter) {
            setCurrentFilter(i);
        }
    }
con's avatar
con committed
207
208
    settings->endGroup();
}