1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
|
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** 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.
**
** GNU Lesser General Public License Usage
**
** 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.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/
#include "searchresultwindow.h"
#include "searchresulttreemodel.h"
#include "searchresulttreeitems.h"
#include <coreplugin/icore.h>
#include <utils/qtcassert.h>
#include <QtCore/QFile>
#include <QtCore/QTextStream>
#include <QtCore/QSettings>
#include <QtCore/QDebug>
#include <QtGui/QListWidget>
#include <QtGui/QToolButton>
#include <QtGui/QLineEdit>
#include <QtGui/QStackedWidget>
#include <QtGui/QLabel>
using namespace Find;
using namespace Find::Internal;
static const QString SETTINGSKEYSECTIONNAME("SearchResults");
static const QString SETTINGSKEYEXPANDRESULTS("ExpandResults");
SearchResultWindow::SearchResultWindow()
: m_currentSearch(0),
m_isShowingReplaceUI(false),
m_focusReplaceEdit(false)
{
m_widget = new QStackedWidget;
m_widget->setWindowTitle(name());
m_searchResultTreeView = new SearchResultTreeView(m_widget);
m_searchResultTreeView->setFrameStyle(QFrame::NoFrame);
m_searchResultTreeView->setAttribute(Qt::WA_MacShowFocusRect, false);
m_widget->addWidget(m_searchResultTreeView);
m_noMatchesFoundDisplay = new QListWidget(m_widget);
m_noMatchesFoundDisplay->addItem(tr("No matches found!"));
m_noMatchesFoundDisplay->setFrameStyle(QFrame::NoFrame);
m_widget->addWidget(m_noMatchesFoundDisplay);
m_expandCollapseToolButton = new QToolButton(m_widget);
m_expandCollapseToolButton->setAutoRaise(true);
m_expandCollapseToolButton->setCheckable(true);
m_expandCollapseToolButton->setIcon(QIcon(":/find/images/expand.png"));
m_expandCollapseToolButton->setToolTip(tr("Expand All"));
m_replaceLabel = new QLabel(tr("Replace with:"), m_widget);
m_replaceLabel->setContentsMargins(12, 0, 5, 0);
m_replaceTextEdit = new QLineEdit(m_widget);
m_replaceButton = new QToolButton(m_widget);
m_replaceButton->setToolTip(tr("Replace all occurrences"));
m_replaceButton->setText(tr("Replace"));
m_replaceButton->setToolButtonStyle(Qt::ToolButtonTextOnly);
m_replaceButton->setAutoRaise(true);
m_replaceTextEdit->setTabOrder(m_replaceTextEdit, m_searchResultTreeView);
connect(m_searchResultTreeView, SIGNAL(jumpToSearchResult(int,bool)),
this, SLOT(handleJumpToSearchResult(int,bool)));
connect(m_expandCollapseToolButton, SIGNAL(toggled(bool)), this, SLOT(handleExpandCollapseToolButton(bool)));
connect(m_replaceTextEdit, SIGNAL(returnPressed()), this, SLOT(handleReplaceButton()));
connect(m_replaceButton, SIGNAL(clicked()), this, SLOT(handleReplaceButton()));
readSettings();
setShowReplaceUI(false);
}
SearchResultWindow::~SearchResultWindow()
{
writeSettings();
delete m_currentSearch;
m_currentSearch = 0;
delete m_widget;
m_widget = 0;
m_items.clear();
}
void SearchResultWindow::setTextToReplace(const QString &textToReplace)
{
m_replaceTextEdit->setText(textToReplace);
}
QString SearchResultWindow::textToReplace() const
{
return m_replaceTextEdit->text();
}
void SearchResultWindow::setShowReplaceUI(bool show)
{
m_searchResultTreeView->model()->setShowReplaceUI(show);
m_replaceLabel->setVisible(show);
m_replaceTextEdit->setVisible(show);
m_replaceButton->setVisible(show);
m_isShowingReplaceUI = show;
}
void SearchResultWindow::handleReplaceButton()
{
QTC_ASSERT(m_currentSearch, return);
// check if button is actually enabled, because this is also triggered
// by pressing return in replace line edit
if (m_replaceButton->isEnabled())
m_currentSearch->replaceButtonClicked(m_replaceTextEdit->text(), checkedItems());
}
QList<SearchResultItem> SearchResultWindow::checkedItems() const
{
QList<SearchResultItem> result;
SearchResultTreeModel *model = m_searchResultTreeView->model();
const int fileCount = model->rowCount(QModelIndex());
for (int i = 0; i < fileCount; ++i) {
QModelIndex fileIndex = model->index(i, 0, QModelIndex());
SearchResultFile *fileItem = static_cast<SearchResultFile *>(fileIndex.internalPointer());
Q_ASSERT(fileItem != 0);
for (int rowIndex = 0; rowIndex < fileItem->childrenCount(); ++rowIndex) {
QModelIndex textIndex = model->index(rowIndex, 0, fileIndex);
SearchResultTextRow *rowItem = static_cast<SearchResultTextRow *>(textIndex.internalPointer());
if (rowItem->checkState())
result << m_items.at(rowItem->index());
}
}
return result;
}
void SearchResultWindow::visibilityChanged(bool /*visible*/)
{
}
QWidget *SearchResultWindow::outputWidget(QWidget *)
{
return m_widget;
}
QList<QWidget*> SearchResultWindow::toolBarWidgets() const
{
return QList<QWidget*>() << m_expandCollapseToolButton << m_replaceLabel << m_replaceTextEdit << m_replaceButton;
}
SearchResult *SearchResultWindow::startNewSearch(SearchMode searchOrSearchAndReplace)
{
clearContents();
setShowReplaceUI(searchOrSearchAndReplace != SearchOnly);
delete m_currentSearch;
m_currentSearch = new SearchResult;
return m_currentSearch;
}
void SearchResultWindow::finishSearch()
{
if (m_items.count()) {
m_replaceButton->setEnabled(true);
} else {
showNoMatchesFound();
}
}
void SearchResultWindow::clearContents()
{
m_replaceTextEdit->setEnabled(false);
m_replaceButton->setEnabled(false);
m_replaceTextEdit->clear();
m_searchResultTreeView->clear();
m_items.clear();
m_widget->setCurrentWidget(m_searchResultTreeView);
navigateStateChanged();
}
void SearchResultWindow::showNoMatchesFound()
{
m_replaceTextEdit->setEnabled(false);
m_replaceButton->setEnabled(false);
m_widget->setCurrentWidget(m_noMatchesFoundDisplay);
}
bool SearchResultWindow::isEmpty() const
{
return (m_searchResultTreeView->model()->rowCount() < 1);
}
int SearchResultWindow::numberOfResults() const
{
return m_items.count();
}
bool SearchResultWindow::hasFocus()
{
return m_searchResultTreeView->hasFocus() || (m_isShowingReplaceUI && m_replaceTextEdit->hasFocus());
}
bool SearchResultWindow::canFocus()
{
return !m_items.isEmpty();
}
void SearchResultWindow::setFocus()
{
if (!m_items.isEmpty()) {
if (!m_isShowingReplaceUI) {
m_searchResultTreeView->setFocus();
} else {
if (!m_widget->focusWidget()
|| m_widget->focusWidget() == m_replaceTextEdit
|| m_focusReplaceEdit) {
m_replaceTextEdit->setFocus();
m_replaceTextEdit->selectAll();
} else {
m_searchResultTreeView->setFocus();
}
}
}
}
void SearchResultWindow::setTextEditorFont(const QFont &font)
{
m_searchResultTreeView->setTextEditorFont(font);
}
void SearchResultWindow::handleJumpToSearchResult(int index, bool /* checked */)
{
QTC_ASSERT(m_currentSearch, return);
m_currentSearch->activated(m_items.at(index));
}
void SearchResultWindow::addResult(const QString &fileName, int lineNumber, const QString &rowText,
int searchTermStart, int searchTermLength, const QVariant &userData)
{
//qDebug()<<"###"<<fileName;
m_widget->setCurrentWidget(m_searchResultTreeView);
int index = m_items.size();
SearchResultItem item;
item.fileName = fileName;
item.lineNumber = lineNumber;
item.lineText = rowText;
item.searchTermStart = searchTermStart;
item.searchTermLength = searchTermLength;
item.userData = userData;
item.index = index;
m_items.append(item);
m_searchResultTreeView->appendResultLine(index, fileName, lineNumber, rowText, searchTermStart, searchTermLength);
if (index == 0) {
m_replaceTextEdit->setEnabled(true);
// We didn't have an item before, set the focus to the search widget
m_focusReplaceEdit = true;
setFocus();
m_focusReplaceEdit = false;
m_searchResultTreeView->selectionModel()->select(m_searchResultTreeView->model()->index(0, 0, QModelIndex()), QItemSelectionModel::Select);
emit navigateStateChanged();
}
}
void SearchResultWindow::handleExpandCollapseToolButton(bool checked)
{
m_searchResultTreeView->setAutoExpandResults(checked);
if (checked)
m_searchResultTreeView->expandAll();
else
m_searchResultTreeView->collapseAll();
}
void SearchResultWindow::readSettings()
{
QSettings *s = Core::ICore::instance()->settings();
if (s) {
s->beginGroup(SETTINGSKEYSECTIONNAME);
m_expandCollapseToolButton->setChecked(s->value(SETTINGSKEYEXPANDRESULTS, m_initiallyExpand).toBool());
s->endGroup();
}
}
void SearchResultWindow::writeSettings()
{
QSettings *s = Core::ICore::instance()->settings();
if (s) {
s->beginGroup(SETTINGSKEYSECTIONNAME);
s->setValue(SETTINGSKEYEXPANDRESULTS, m_expandCollapseToolButton->isChecked());
s->endGroup();
}
}
int SearchResultWindow::priorityInStatusBar() const
{
return 80;
}
bool SearchResultWindow::canNext()
{
return m_items.count() > 0;
}
bool SearchResultWindow::canPrevious()
{
return m_items.count() > 0;
}
void SearchResultWindow::goToNext()
{
if (m_items.count() == 0)
return;
QModelIndex idx = m_searchResultTreeView->model()->next(m_searchResultTreeView->currentIndex());
if (idx.isValid()) {
m_searchResultTreeView->setCurrentIndex(idx);
m_searchResultTreeView->emitJumpToSearchResult(idx);
}
}
void SearchResultWindow::goToPrev()
{
if (!m_searchResultTreeView->model()->rowCount())
return;
QModelIndex idx = m_searchResultTreeView->model()->prev(m_searchResultTreeView->currentIndex());
if (idx.isValid()) {
m_searchResultTreeView->setCurrentIndex(idx);
m_searchResultTreeView->emitJumpToSearchResult(idx);
}
}
bool SearchResultWindow::canNavigate()
{
return true;
}
|