helpfindsupport.cpp 4.26 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
2
3
4
**
** This file is part of Qt Creator
**
5
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
6
7
8
**
** Contact:  Qt Software Information (qt-info@nokia.com)
**
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
26
** If you are unsure which license is appropriate for your use, please
** contact the sales department at qt-sales@nokia.com.
con's avatar
con committed
27
**
28
**************************************************************************/
hjk's avatar
hjk committed
29

con's avatar
con committed
30
31
32
#include "helpfindsupport.h"
#include "helpviewer.h"

hjk's avatar
hjk committed
33
34
#include <utils/qtcassert.h>

con's avatar
con committed
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
using namespace Help::Internal;

HelpFindSupport::HelpFindSupport(CentralWidget *centralWidget)
    : m_centralWidget(centralWidget)
{
}

HelpFindSupport::~HelpFindSupport()
{
}

bool HelpFindSupport::isEnabled() const
{
    return true;
}
hjk's avatar
hjk committed
50

con's avatar
con committed
51
52
QString HelpFindSupport::currentFindString() const
{
hjk's avatar
hjk committed
53
54
    QTC_ASSERT(m_centralWidget, return QString());
    HelpViewer *viewer = m_centralWidget->currentHelpViewer();
con's avatar
con committed
55
56
    if (!viewer)
        return QString();
57
#if !defined(QT_NO_WEBKIT)
con's avatar
con committed
58
59
60
61
62
63
    return viewer->selectedText();
#else
    return viewer->textCursor().selectedText();
#endif
}

hjk's avatar
hjk committed
64
65
66
67
QString HelpFindSupport::completedFindString() const
{
    return QString();
}
con's avatar
con committed
68
69
70

bool HelpFindSupport::findIncremental(const QString &txt, QTextDocument::FindFlags findFlags)
{
hjk's avatar
hjk committed
71
    QTC_ASSERT(m_centralWidget, return false);
con's avatar
con committed
72
73
74
75
76
77
    findFlags &= ~QTextDocument::FindBackward;
    return m_centralWidget->find(txt, findFlags, true);
}

bool HelpFindSupport::findStep(const QString &txt, QTextDocument::FindFlags findFlags)
{
hjk's avatar
hjk committed
78
    QTC_ASSERT(m_centralWidget, return false);
con's avatar
con committed
79
80
    return m_centralWidget->find(txt, findFlags, false);
}
con's avatar
con committed
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

HelpViewerFindSupport::HelpViewerFindSupport(HelpViewer *viewer)
        : m_viewer(viewer)
{
}

QString HelpViewerFindSupport::currentFindString() const
{
    QTC_ASSERT(m_viewer, return QString());
    return m_viewer->selectedText();
}

bool HelpViewerFindSupport::findIncremental(const QString &txt, QTextDocument::FindFlags findFlags)
{
    QTC_ASSERT(m_viewer, return false);
    findFlags &= ~QTextDocument::FindBackward;
    return find(txt, findFlags, true);
}

bool HelpViewerFindSupport::findStep(const QString &txt, QTextDocument::FindFlags findFlags)
{
    QTC_ASSERT(m_viewer, return false);
    return find(txt, findFlags, false);
}

bool HelpViewerFindSupport::find(const QString &txt, QTextDocument::FindFlags findFlags, bool incremental)
{
    QTC_ASSERT(m_viewer, return false);
#if !defined(QT_NO_WEBKIT)
    Q_UNUSED(incremental);
    QWebPage::FindFlags options = QWebPage::FindWrapsAroundDocument;
    if (findFlags & QTextDocument::FindBackward)
        options |= QWebPage::FindBackward;
    if (findFlags & QTextDocument::FindCaseSensitively)
        options |= QWebPage::FindCaseSensitively;

    return m_viewer->findText(txt, options);
#else
    QTextCursor cursor = viewer->textCursor();
    QTextDocument *doc = viewer->document();
    QTextBrowser *browser = qobject_cast<QTextBrowser*>(viewer);

    if (!browser || !doc || cursor.isNull())
        return false;
    if (incremental)
        cursor.setPosition(cursor.selectionStart());

    QTextCursor found = doc->find(txt, cursor, findFlags);
    if (found.isNull()) {
        if ((findFlags&QTextDocument::FindBackward) == 0)
            cursor.movePosition(QTextCursor::Start);
        else
            cursor.movePosition(QTextCursor::End);
        found = doc->find(txt, cursor, findFlags);
        if (found.isNull()) {
            return false;
        }
    }
    if (!found.isNull()) {
        viewer->setTextCursor(found);
    }
    return true;
#endif
}