searchresultwindow.cpp 25.1 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
2
3
4
**
** This file is part of Qt Creator
**
con's avatar
con committed
5
** Copyright (c) 2011 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
**
con's avatar
con committed
9
** No Commercial Usage
10
**
con's avatar
con committed
11
12
13
14
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
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
**
con's avatar
con committed
25
26
27
28
29
30
** In addition, as a special exception, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
con's avatar
con committed
31
**
32
**************************************************************************/
hjk's avatar
hjk committed
33

con's avatar
con committed
34
35
#include "searchresultwindow.h"
#include "searchresulttreemodel.h"
36
#include "searchresulttreeitems.h"
37
#include "searchresulttreeview.h"
con's avatar
con committed
38
#include "ifindsupport.h"
con's avatar
con committed
39

con's avatar
con committed
40
#include <aggregation/aggregate.h>
41
#include <coreplugin/icore.h>
42
43
44
45
#include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/actionmanager/command.h>
#include <coreplugin/coreconstants.h>
#include <coreplugin/icontext.h>
hjk's avatar
hjk committed
46
#include <coreplugin/uniqueidmanager.h>
47
#include <utils/qtcassert.h>
48

con's avatar
con committed
49
50
51
#include <QtCore/QFile>
#include <QtCore/QTextStream>
#include <QtCore/QSettings>
52
#include <QtCore/QDebug>
53
#include <QtCore/QDir>
con's avatar
con committed
54
55
#include <QtGui/QListWidget>
#include <QtGui/QToolButton>
56
#include <QtGui/QLineEdit>
57
58
#include <QtGui/QStackedWidget>
#include <QtGui/QLabel>
59
#include <QtGui/QFont>
60
#include <QtGui/QAction>
con's avatar
con committed
61

62
63
static const char SETTINGSKEYSECTIONNAME[] = "SearchResults";
static const char SETTINGSKEYEXPANDRESULTS[] = "ExpandResults";
con's avatar
con committed
64

65
namespace Find {
con's avatar
con committed
66

con's avatar
con committed
67
68
namespace Internal {

mae's avatar
mae committed
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
    class WideEnoughLineEdit : public QLineEdit {
        Q_OBJECT
    public:
        WideEnoughLineEdit(QWidget *parent):QLineEdit(parent){
            connect(this, SIGNAL(textChanged(QString)),
                    this, SLOT(updateGeometry()));
        }
        ~WideEnoughLineEdit(){}
        QSize sizeHint() const {
            QSize sh = QLineEdit::minimumSizeHint();
            sh.rwidth() += qMax(25 * fontMetrics().width(QLatin1Char('x')),
                                fontMetrics().width(text()));
            return sh;
        }
    public slots:
        void updateGeometry() { QLineEdit::updateGeometry(); }
    };

con's avatar
con committed
87
88
89
90
91
    class SearchResultFindSupport : public IFindSupport
    {
        Q_OBJECT
    public:
        SearchResultFindSupport(SearchResultTreeView *view)
92
93
            : m_view(view),
              m_incrementalWrappedState(false)
con's avatar
con committed
94
95
        {
        }
mae's avatar
mae committed
96

con's avatar
con committed
97
98
        bool supportsReplace() const { return false; }

99
        Find::FindFlags supportedFindFlags() const
con's avatar
con committed
100
        {
101
102
            return Find::FindBackward | Find::FindCaseSensitively
                    | Find::FindRegularExpression | Find::FindWholeWords;
con's avatar
con committed
103
104
105
106
107
        }

        void resetIncrementalSearch()
        {
            m_incrementalFindStart = QModelIndex();
108
            m_incrementalWrappedState = false;
con's avatar
con committed
109
110
111
112
113
114
115
116
117
118
119
120
121
122
        }

        void clearResults() { }

        QString currentFindString() const
        {
            return QString();
        }

        QString completedFindString() const
        {
            return QString();
        }

123
        void highlightAll(const QString &txt, Find::FindFlags findFlags)
con's avatar
con committed
124
125
126
127
128
129
        {
            Q_UNUSED(txt)
            Q_UNUSED(findFlags)
            return;
        }

130
        IFindSupport::Result findIncremental(const QString &txt, Find::FindFlags findFlags)
con's avatar
con committed
131
        {
132
            if (!m_incrementalFindStart.isValid()) {
con's avatar
con committed
133
                m_incrementalFindStart = m_view->currentIndex();
134
135
                m_incrementalWrappedState = false;
            }
con's avatar
con committed
136
            m_view->setCurrentIndex(m_incrementalFindStart);
137
138
139
140
141
142
143
            bool wrapped = false;
            IFindSupport::Result result = find(txt, findFlags, &wrapped);
            if (wrapped != m_incrementalWrappedState) {
                m_incrementalWrappedState = wrapped;
                showWrapIndicator(m_view);
            }
            return result;
con's avatar
con committed
144
145
        }

146
        IFindSupport::Result findStep(const QString &txt, Find::FindFlags findFlags)
con's avatar
con committed
147
        {
148
149
150
151
152
            bool wrapped = false;
            IFindSupport::Result result = find(txt, findFlags, &wrapped);
            if (wrapped)
                showWrapIndicator(m_view);
            if (result == IFindSupport::Found) {
con's avatar
con committed
153
                m_incrementalFindStart = m_view->currentIndex();
154
155
                m_incrementalWrappedState = false;
            }
con's avatar
con committed
156
157
158
            return result;
        }

159
        IFindSupport::Result find(const QString &txt, Find::FindFlags findFlags, bool *wrapped)
con's avatar
con committed
160
        {
161
162
            if (wrapped)
                *wrapped = false;
con's avatar
con committed
163
164
165
            if (txt.isEmpty())
                return IFindSupport::NotFound;
            QModelIndex index;
166
167
            if (findFlags & Find::FindRegularExpression) {
                bool sensitive = (findFlags & Find::FindCaseSensitively);
con's avatar
con committed
168
                index = m_view->model()->find(QRegExp(txt, (sensitive ? Qt::CaseSensitive : Qt::CaseInsensitive)),
169
170
171
                                              m_view->currentIndex(),
                                              Find::textDocumentFlagsForFindFlags(findFlags),
                                              wrapped);
con's avatar
con committed
172
            } else {
173
174
175
176
                index = m_view->model()->find(txt,
                                              m_view->currentIndex(),
                                              Find::textDocumentFlagsForFindFlags(findFlags),
                                              wrapped);
con's avatar
con committed
177
178
179
180
181
182
183
184
185
186
187
            }
            if (index.isValid()) {
                m_view->setCurrentIndex(index);
                m_view->scrollTo(index);
                if (index.parent().isValid())
                    m_view->expand(index.parent());
                return IFindSupport::Found;
            }
            return IFindSupport::NotFound;
        }

188
        void replace(const QString &before, const QString &after,
189
            Find::FindFlags findFlags)
190
191
192
193
194
195
        {
            Q_UNUSED(before)
            Q_UNUSED(after)
            Q_UNUSED(findFlags)
        }

con's avatar
con committed
196
        bool replaceStep(const QString &before, const QString &after,
197
            Find::FindFlags findFlags)
con's avatar
con committed
198
199
200
201
202
203
204
205
        {
            Q_UNUSED(before)
            Q_UNUSED(after)
            Q_UNUSED(findFlags)
            return false;
        }

        int replaceAll(const QString &before, const QString &after,
206
            Find::FindFlags findFlags)
con's avatar
con committed
207
208
209
210
211
212
213
214
215
216
        {
            Q_UNUSED(before)
            Q_UNUSED(after)
            Q_UNUSED(findFlags)
            return 0;
        }

    private:
        SearchResultTreeView *m_view;
        QModelIndex m_incrementalFindStart;
217
        bool m_incrementalWrappedState;
con's avatar
con committed
218
219
220
221
222
223
224
    };

    struct SearchResultWindowPrivate {
        SearchResultWindowPrivate();

        Internal::SearchResultTreeView *m_searchResultTreeView;
        QListWidget *m_noMatchesFoundDisplay;
225
226
        QToolButton *m_expandCollapseButton;
        QAction *m_expandCollapseAction;
con's avatar
con committed
227
228
229
230
231
232
        QLabel *m_replaceLabel;
        QLineEdit *m_replaceTextEdit;
        QToolButton *m_replaceButton;
        static const bool m_initiallyExpand = false;
        QStackedWidget *m_widget;
        SearchResult *m_currentSearch;
233
        int m_itemCount;
con's avatar
con committed
234
235
236
237
238
239
        bool m_isShowingReplaceUI;
        bool m_focusReplaceEdit;
    };

    SearchResultWindowPrivate::SearchResultWindowPrivate()
        : m_currentSearch(0),
240
        m_itemCount(0),
con's avatar
con committed
241
242
243
244
        m_isShowingReplaceUI(false),
        m_focusReplaceEdit(false)
    {
    }
245
}
246

con's avatar
con committed
247
248
using namespace Find::Internal;

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
/*!
    \enum Find::SearchResultWindow::SearchMode
    Specifies if a search should show the replace UI or not.

    \value SearchOnly
           The search doesn't support replace.
    \value SearchAndReplace
           The search supports replace, so show the UI for it.
*/

/*!
    \class Find::SearchResult
    \brief Reports user interaction like activation of a search result item.

    Whenever a new search is initiated via startNewSearch, an instance of this
    class is returned to provide the initiator with the hooks for handling user
    interaction.
*/

/*!
    \fn void SearchResult::activated(const Find::SearchResultItem &item)
    \brief Sent if the user activated (e.g. double-clicked) a search result
    \a item.
*/

/*!
    \fn void SearchResult::replaceButtonClicked(const QString &replaceText, const QList<Find::SearchResultItem> &checkedItems)
    \brief Sent when the user initiated a replace, e.g. by pressing the replace
    all button.

    The signal reports the text to use for replacement in \a replaceText,
    and the list of search result items that were selected by the user
    in \a checkedItems.
    The handler of this signal should apply the replace only on the selected
    items.
*/

/*!
    \class Find::SearchResultWindow
    \brief The SearchResultWindow class is the implementation of a commonly
    shared \gui{Search Results} output pane. Use it to show search results
    to a user.

    Whenever you want to show the user a list of search results, or want
    to present UI for a global search and replace, use the single instance
    of this class.

    Except for being an implementation of a output pane, the
    SearchResultWindow has a few methods and one enum that allows other
    plugins to show their search results and hook into the user actions for
    selecting an entry and performing a global replace.

    Whenever you start a search, call startNewSearch(SearchMode) to initialize
    the search result window. The parameter determines if the GUI for
    replacing should be shown.
    The method returns a SearchResult object that is your
    hook into the signals from user interaction for this search.
    When you produce search results, call addResults or addResult to add them
    to the search result window.
    After the search has finished call finishSearch to inform the search
    result window about it.

    After that you get activated signals via your SearchResult instance when
    the user selects a search result item, and, if you started the search
    with the SearchAndReplace option, the replaceButtonClicked signal
    when the user requests a replace.
*/

/*!
    \fn QString SearchResultWindow::displayName() const
    \internal
*/

322
323
SearchResultWindow *SearchResultWindow::m_instance = 0;

324
325
326
327
/*!
    \fn SearchResultWindow::SearchResultWindow()
    \internal
*/
328
329
SearchResultWindow::SearchResultWindow() : d(new SearchResultWindowPrivate)
{
330
    m_instance = this;
331
    d->m_widget = new QStackedWidget;
332
    d->m_widget->setWindowTitle(displayName());
333
334
335
336
337

    d->m_searchResultTreeView = new Internal::SearchResultTreeView(d->m_widget);
    d->m_searchResultTreeView->setFrameStyle(QFrame::NoFrame);
    d->m_searchResultTreeView->setAttribute(Qt::WA_MacShowFocusRect, false);
    d->m_widget->addWidget(d->m_searchResultTreeView);
con's avatar
con committed
338
339
340
    Aggregation::Aggregate * agg = new Aggregation::Aggregate;
    agg->add(d->m_searchResultTreeView);
    agg->add(new SearchResultFindSupport(d->m_searchResultTreeView));
341
342
343
344
345
346

    d->m_noMatchesFoundDisplay = new QListWidget(d->m_widget);
    d->m_noMatchesFoundDisplay->addItem(tr("No matches found!"));
    d->m_noMatchesFoundDisplay->setFrameStyle(QFrame::NoFrame);
    d->m_widget->addWidget(d->m_noMatchesFoundDisplay);

347
348
349
350
351
352
353
    d->m_expandCollapseButton = new QToolButton(d->m_widget);
    d->m_expandCollapseButton->setAutoRaise(true);

    d->m_expandCollapseAction = new QAction(tr("Expand All"), this);
    d->m_expandCollapseAction->setCheckable(true);
    d->m_expandCollapseAction->setIcon(QIcon(QLatin1String(":/find/images/expand.png")));
    Core::Command *cmd = Core::ICore::instance()->actionManager()->registerAction(
hjk's avatar
hjk committed
354
            d->m_expandCollapseAction, "Find.ExpandAll",
355
            Core::Context(Core::Constants::C_GLOBAL));
356
    cmd->setAttribute(Core::Command::CA_UpdateText);
357
    d->m_expandCollapseButton->setDefaultAction(cmd->action());
358
359
360

    d->m_replaceLabel = new QLabel(tr("Replace with:"), d->m_widget);
    d->m_replaceLabel->setContentsMargins(12, 0, 5, 0);
mae's avatar
mae committed
361
    d->m_replaceTextEdit = new WideEnoughLineEdit(d->m_widget);
362
363
364
365
366
367
368
    d->m_replaceButton = new QToolButton(d->m_widget);
    d->m_replaceButton->setToolTip(tr("Replace all occurrences"));
    d->m_replaceButton->setText(tr("Replace"));
    d->m_replaceButton->setToolButtonStyle(Qt::ToolButtonTextOnly);
    d->m_replaceButton->setAutoRaise(true);
    d->m_replaceTextEdit->setTabOrder(d->m_replaceTextEdit, d->m_searchResultTreeView);

369
370
    connect(d->m_searchResultTreeView, SIGNAL(jumpToSearchResult(SearchResultItem)),
            this, SLOT(handleJumpToSearchResult(SearchResultItem)));
371
    connect(d->m_expandCollapseAction, SIGNAL(toggled(bool)), this, SLOT(handleExpandCollapseToolButton(bool)));
372
373
    connect(d->m_replaceTextEdit, SIGNAL(returnPressed()), this, SLOT(handleReplaceButton()));
    connect(d->m_replaceButton, SIGNAL(clicked()), this, SLOT(handleReplaceButton()));
con's avatar
con committed
374
375

    readSettings();
376
    setShowReplaceUI(false);
con's avatar
con committed
377
378
}

379
380
381
382
/*!
    \fn SearchResultWindow::~SearchResultWindow()
    \internal
*/
con's avatar
con committed
383
384
385
SearchResultWindow::~SearchResultWindow()
{
    writeSettings();
386
387
388
389
    delete d->m_currentSearch;
    d->m_currentSearch = 0;
    delete d->m_widget;
    d->m_widget = 0;
390
    d->m_itemCount = 0;
391
    delete d;
con's avatar
con committed
392
393
}

394
395
396
397
/*!
    \fn SearchResultWindow *SearchResultWindow::instance()
    \brief Returns the single shared instance of the Search Results window.
*/
398
399
400
401
402
SearchResultWindow *SearchResultWindow::instance()
{
    return m_instance;
}

403
404
405
406
407
/*!
    \fn void SearchResultWindow::setTextToReplace(const QString &textToReplace)
    \brief Sets the value in the UI element that allows the user to type
    the text that should replace text in search results to \a textToReplace.
*/
408
409
void SearchResultWindow::setTextToReplace(const QString &textToReplace)
{
410
    d->m_replaceTextEdit->setText(textToReplace);
411
412
}

413
414
415
416
/*!
    \fn QString SearchResultWindow::textToReplace() const
    \brief Returns the text that should replace the text in search results.
*/
417
418
QString SearchResultWindow::textToReplace() const
{
419
    return d->m_replaceTextEdit->text();
420
421
}

422
423
424
425
/*!
    \fn void SearchResultWindow::setShowReplaceUI(bool show)
    \internal
*/
426
427
void SearchResultWindow::setShowReplaceUI(bool show)
{
428
429
430
431
432
    d->m_searchResultTreeView->model()->setShowReplaceUI(show);
    d->m_replaceLabel->setVisible(show);
    d->m_replaceTextEdit->setVisible(show);
    d->m_replaceButton->setVisible(show);
    d->m_isShowingReplaceUI = show;
433
434
}

435
436
437
438
/*!
    \fn void SearchResultWindow::handleReplaceButton()
    \internal
*/
439
440
void SearchResultWindow::handleReplaceButton()
{
441
    QTC_ASSERT(d->m_currentSearch, return);
442
443
    // check if button is actually enabled, because this is also triggered
    // by pressing return in replace line edit
444
445
    if (d->m_replaceButton->isEnabled())
        d->m_currentSearch->replaceButtonClicked(d->m_replaceTextEdit->text(), checkedItems());
446
447
}

448
449
450
451
/*!
    \fn QList<SearchResultItem> SearchResultWindow::checkedItems() const
    \internal
*/
452
QList<SearchResultItem> SearchResultWindow::checkedItems() const
453
{
454
    QList<SearchResultItem> result;
455
    Internal::SearchResultTreeModel *model = d->m_searchResultTreeView->model();
456
457
458
    const int fileCount = model->rowCount(QModelIndex());
    for (int i = 0; i < fileCount; ++i) {
        QModelIndex fileIndex = model->index(i, 0, QModelIndex());
459
        Internal::SearchResultTreeItem *fileItem = static_cast<Internal::SearchResultTreeItem *>(fileIndex.internalPointer());
460
461
462
        Q_ASSERT(fileItem != 0);
        for (int rowIndex = 0; rowIndex < fileItem->childrenCount(); ++rowIndex) {
            QModelIndex textIndex = model->index(rowIndex, 0, fileIndex);
463
            Internal::SearchResultTreeItem *rowItem = static_cast<Internal::SearchResultTreeItem *>(textIndex.internalPointer());
464
            if (rowItem->checkState())
465
                result << rowItem->item;
466
467
468
        }
    }
    return result;
469
470
}

471
472
473
474
/*!
    \fn void SearchResultWindow::visibilityChanged(bool)
    \internal
*/
con's avatar
con committed
475
476
477
478
void SearchResultWindow::visibilityChanged(bool /*visible*/)
{
}

479
480
481
482
/*!
    \fn QWidget *SearchResultWindow::outputWidget(QWidget *)
    \internal
*/
con's avatar
con committed
483
484
QWidget *SearchResultWindow::outputWidget(QWidget *)
{
485
    return d->m_widget;
con's avatar
con committed
486
487
}

488
489
490
491
/*!
    \fn QList<QWidget*> SearchResultWindow::toolBarWidgets() const
    \internal
*/
Thorbjørn Lindeijer's avatar
Thorbjørn Lindeijer committed
492
QList<QWidget*> SearchResultWindow::toolBarWidgets() const
con's avatar
con committed
493
{
494
    return QList<QWidget*>() << d->m_expandCollapseButton << d->m_replaceLabel << d->m_replaceTextEdit << d->m_replaceButton;
con's avatar
con committed
495
496
}

497
498
499
500
501
502
503
504
505
506
/*!
    \fn SearchResult *SearchResultWindow::startNewSearch(SearchMode searchOrSearchAndReplace)
    \brief Tells the search results window to start a new search.

    This will clear the contents of the previous search and initialize the UI
    with regard to showing the replace UI or not (depending on the search mode
    in \a searchOrSearchAndReplace).
    Returns a SearchResult object that is used for signaling user interaction
    with the results of this search.
*/
507
508
509
510
SearchResult *SearchResultWindow::startNewSearch(SearchMode searchOrSearchAndReplace)
{
    clearContents();
    setShowReplaceUI(searchOrSearchAndReplace != SearchOnly);
511
512
513
    delete d->m_currentSearch;
    d->m_currentSearch = new SearchResult;
    return d->m_currentSearch;
514
515
}

516
517
518
519
520
/*!
    \fn void SearchResultWindow::finishSearch()
    \brief Notifies the search result window that the current search
    has finished, and the UI should reflect that.
*/
521
522
void SearchResultWindow::finishSearch()
{
523
    if (d->m_itemCount > 0) {
524
        d->m_replaceButton->setEnabled(true);
525
526
527
528
529
    } else {
        showNoMatchesFound();
    }
}

530
531
532
533
/*!
    \fn void SearchResultWindow::clearContents()
    \brief Clears the current contents in the search result window.
*/
con's avatar
con committed
534
535
void SearchResultWindow::clearContents()
{
536
537
538
539
    d->m_replaceTextEdit->setEnabled(false);
    d->m_replaceButton->setEnabled(false);
    d->m_replaceTextEdit->clear();
    d->m_searchResultTreeView->clear();
540
    d->m_itemCount = 0;
541
    d->m_widget->setCurrentWidget(d->m_searchResultTreeView);
542
    navigateStateChanged();
con's avatar
con committed
543
544
}

545
546
547
548
/*!
    \fn void SearchResultWindow::showNoMatchesFound()
    \internal
*/
Thorbjørn Lindeijer's avatar
Thorbjørn Lindeijer committed
549
void SearchResultWindow::showNoMatchesFound()
con's avatar
con committed
550
{
551
552
553
    d->m_replaceTextEdit->setEnabled(false);
    d->m_replaceButton->setEnabled(false);
    d->m_widget->setCurrentWidget(d->m_noMatchesFoundDisplay);
con's avatar
con committed
554
555
}

556
557
558
559
/*!
    \fn bool SearchResultWindow::isEmpty() const
    Returns if the search result window currently doesn't show any results.
*/
con's avatar
con committed
560
561
bool SearchResultWindow::isEmpty() const
{
562
    return (d->m_searchResultTreeView->model()->rowCount() < 1);
con's avatar
con committed
563
564
}

565
566
567
568
569
/*!
    \fn int SearchResultWindow::numberOfResults() const
    Returns the number of search results currently shown in the search
    results window.
*/
con's avatar
con committed
570
571
int SearchResultWindow::numberOfResults() const
{
572
    return d->m_itemCount;
con's avatar
con committed
573
574
}

575
576
577
578
/*!
    \fn bool SearchResultWindow::hasFocus()
    \internal
*/
579
580
bool SearchResultWindow::hasFocus()
{
581
    return d->m_searchResultTreeView->hasFocus() || (d->m_isShowingReplaceUI && d->m_replaceTextEdit->hasFocus());
582
583
}

584
585
586
587
/*!
    \fn bool SearchResultWindow::canFocus()
    \internal
*/
588
589
bool SearchResultWindow::canFocus()
{
590
    return d->m_itemCount > 0;
591
592
}

593
594
595
596
/*!
    \fn void SearchResultWindow::setFocus()
    \internal
*/
597
598
void SearchResultWindow::setFocus()
{
599
    if (d->m_itemCount > 0) {
600
601
        if (!d->m_isShowingReplaceUI) {
            d->m_searchResultTreeView->setFocus();
602
        } else {
603
604
605
606
607
            if (!d->m_widget->focusWidget()
                    || d->m_widget->focusWidget() == d->m_replaceTextEdit
                    || d->m_focusReplaceEdit) {
                d->m_replaceTextEdit->setFocus();
                d->m_replaceTextEdit->selectAll();
608
            } else {
609
                d->m_searchResultTreeView->setFocus();
610
611
612
            }
        }
    }
613
614
}

615
616
617
618
/*!
    \fn void SearchResultWindow::setTextEditorFont(const QFont &font)
    \internal
*/
619
620
void SearchResultWindow::setTextEditorFont(const QFont &font)
{
621
    d->m_searchResultTreeView->setTextEditorFont(font);
622
623
}

624
625
626
627
/*!
    \fn void SearchResultWindow::handleJumpToSearchResult(int index, bool)
    \internal
*/
628
void SearchResultWindow::handleJumpToSearchResult(const SearchResultItem &item)
con's avatar
con committed
629
{
630
    QTC_ASSERT(d->m_currentSearch, return);
631
    d->m_currentSearch->activated(item);
con's avatar
con committed
632
633
}

634
635
636
637
638
639
640
641
642
643
644
645
/*!
    \fn void SearchResultWindow::addResult(const QString &fileName, int lineNumber, const QString &rowText, int searchTermStart, int searchTermLength, const QVariant &userData)
    \brief Adds a single result line to the search results.

    The \a fileName, \a lineNumber and \a rowText are shown in the result line.
    \a searchTermStart and \a searchTermLength specify the region that
    should be visually marked (string position and length in \a rowText).
    You can attach arbitrary \a userData to the search result, which can
    be used e.g. when reacting to the signals of the SearchResult for your search.

    \sa addResults()
*/
646
647
void SearchResultWindow::addResult(const QString &fileName, int lineNumber, const QString &rowText,
    int searchTermStart, int searchTermLength, const QVariant &userData)
con's avatar
con committed
648
{
649
    SearchResultItem item;
650
    item.path = QStringList() << QDir::toNativeSeparators(fileName);
651
    item.lineNumber = lineNumber;
652
653
654
655
    item.text = rowText;
    item.textMarkPos = searchTermStart;
    item.textMarkLength = searchTermLength;
    item.useTextEditorFont = true;
656
    item.userData = userData;
657
    addResults(QList<SearchResultItem>() << item, AddOrdered);
658
659
}

660
661
662
663
664
665
666
/*!
    \fn void SearchResultWindow::addResults(QList<SearchResultItem> &items)
    \brief Adds all of the given search result \a items to the search
    results window.

    \sa addResult()
*/
667
void SearchResultWindow::addResults(QList<SearchResultItem> &items, AddMode mode)
668
{
669
670
671
    bool firstItems = (d->m_itemCount == 0);
    d->m_itemCount += items.size();
    d->m_searchResultTreeView->addResults(items, mode);
672
    if (firstItems) {
673
        d->m_replaceTextEdit->setEnabled(true);
674
        // We didn't have an item before, set the focus to the search widget
675
        d->m_focusReplaceEdit = true;
676
        setFocus();
677
678
        d->m_focusReplaceEdit = false;
        d->m_searchResultTreeView->selectionModel()->select(d->m_searchResultTreeView->model()->index(0, 0, QModelIndex()), QItemSelectionModel::Select);
679
        emit navigateStateChanged();
con's avatar
con committed
680
    }
681
682
}

683
684
685
686
/*!
    \fn void SearchResultWindow::handleExpandCollapseToolButton(bool checked)
    \internal
*/
con's avatar
con committed
687
688
void SearchResultWindow::handleExpandCollapseToolButton(bool checked)
{
689
    d->m_searchResultTreeView->setAutoExpandResults(checked);
690
691
    if (checked) {
        d->m_expandCollapseAction->setText(tr("Collapse All"));
692
        d->m_searchResultTreeView->expandAll();
693
694
    } else {
        d->m_expandCollapseAction->setText(tr("Expand All"));
695
        d->m_searchResultTreeView->collapseAll();
696
    }
con's avatar
con committed
697
698
}

699
700
701
702
/*!
    \fn void SearchResultWindow::readSettings()
    \internal
*/
Thorbjørn Lindeijer's avatar
Thorbjørn Lindeijer committed
703
void SearchResultWindow::readSettings()
con's avatar
con committed
704
{
705
706
    QSettings *s = Core::ICore::instance()->settings();
    if (s) {
707
        s->beginGroup(QLatin1String(SETTINGSKEYSECTIONNAME));
708
        d->m_expandCollapseAction->setChecked(s->value(QLatin1String(SETTINGSKEYEXPANDRESULTS), d->m_initiallyExpand).toBool());
con's avatar
con committed
709
710
711
712
        s->endGroup();
    }
}

713
714
715
716
/*!
    \fn void SearchResultWindow::writeSettings()
    \internal
*/
Thorbjørn Lindeijer's avatar
Thorbjørn Lindeijer committed
717
void SearchResultWindow::writeSettings()
con's avatar
con committed
718
{
719
720
    QSettings *s = Core::ICore::instance()->settings();
    if (s) {
721
        s->beginGroup(QLatin1String(SETTINGSKEYSECTIONNAME));
722
        s->setValue(QLatin1String(SETTINGSKEYEXPANDRESULTS), d->m_expandCollapseAction->isChecked());
con's avatar
con committed
723
724
725
726
        s->endGroup();
    }
}

727
728
729
730
/*!
    \fn int SearchResultWindow::priorityInStatusBar() const
    \internal
*/
con's avatar
con committed
731
732
733
734
int SearchResultWindow::priorityInStatusBar() const
{
    return 80;
}
735

736
737
738
739
/*!
    \fn bool SearchResultWindow::canNext()
    \internal
*/
740
741
bool SearchResultWindow::canNext()
{
742
    return d->m_itemCount > 0;
743
744
}

745
746
747
748
/*!
    \fn bool SearchResultWindow::canPrevious()
    \internal
*/
749
750
bool SearchResultWindow::canPrevious()
{
751
    return d->m_itemCount > 0;
752
753
}

754
755
756
757
/*!
    \fn void SearchResultWindow::goToNext()
    \internal
*/
758
759
void SearchResultWindow::goToNext()
{
760
    if (d->m_itemCount == 0)
761
        return;
762
    QModelIndex idx = d->m_searchResultTreeView->model()->next(d->m_searchResultTreeView->currentIndex());
763
    if (idx.isValid()) {
764
765
        d->m_searchResultTreeView->setCurrentIndex(idx);
        d->m_searchResultTreeView->emitJumpToSearchResult(idx);
766
767
    }
}
768
769
770
771
772

/*!
    \fn void SearchResultWindow::goToPrev()
    \internal
*/
773
774
void SearchResultWindow::goToPrev()
{
775
    if (!d->m_searchResultTreeView->model()->rowCount())
776
        return;
777
    QModelIndex idx = d->m_searchResultTreeView->model()->prev(d->m_searchResultTreeView->currentIndex());
778
    if (idx.isValid()) {
779
780
        d->m_searchResultTreeView->setCurrentIndex(idx);
        d->m_searchResultTreeView->emitJumpToSearchResult(idx);
781
782
783
    }
}

784
785
786
787
/*!
    \fn bool SearchResultWindow::canNavigate()
    \internal
*/
788
789
790
791
bool SearchResultWindow::canNavigate()
{
    return true;
}
792
793
794

} // namespace Find

mae's avatar
mae committed
795
796

#include "searchresultwindow.moc"