~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to tools/assistant/finddialog.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
 
4
**
 
5
** This file is part of the assistant application of the Qt Toolkit.
 
6
**
 
7
** This file may be distributed under the terms of the Q Public License
 
8
** as defined by Trolltech AS of Norway and appearing in the file
 
9
** LICENSE.QPL included in the packaging of this file.
 
10
**
 
11
** This file may be distributed and/or modified under the terms of the
 
12
** GNU General Public License version 2 as published by the Free Software
 
13
** Foundation and appearing in the file LICENSE.GPL included in the
 
14
** packaging of this file.
 
15
**
 
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
 
17
**   information about Qt Commercial License Agreements.
 
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
 
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
 
20
**
 
21
** Contact info@trolltech.com if any conditions of this licensing are
 
22
** not clear to you.
 
23
**
 
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 
26
**
 
27
****************************************************************************/
 
28
 
 
29
 
 
30
#include "finddialog.h"
 
31
#include "mainwindow.h"
 
32
#include "tabbedbrowser.h"
 
33
#include "helpwindow.h"
 
34
 
 
35
#include <qtextbrowser.h>
 
36
#include <qtextcursor.h>
 
37
#include <qstatusbar.h>
 
38
#include <qlineedit.h>
 
39
#include <qdatetime.h>
 
40
#include <qgridlayout.h>
 
41
#include <qdebug.h>
 
42
 
 
43
FindDialog::FindDialog(MainWindow *parent)
 
44
    : QDialog(parent)
 
45
{
 
46
    contentsWidget = new QWidget(this);
 
47
    ui.setupUi(contentsWidget);
 
48
 
 
49
    QVBoxLayout *l = new QVBoxLayout(this);
 
50
    l->setMargin(0);
 
51
    l->setSpacing(0);
 
52
    l->addWidget(contentsWidget);
 
53
 
 
54
    lastBrowser = 0;
 
55
    onceFound = false;
 
56
    findExpr.clear();
 
57
 
 
58
    sb = new QStatusBar(this);
 
59
    l->addWidget(sb);
 
60
    
 
61
    sb->showMessage(tr("Enter the text you are looking for."));
 
62
 
 
63
    connect(ui.findButton, SIGNAL(clicked()), this, SLOT(findButtonClicked()));
 
64
    connect(ui.closeButton, SIGNAL(clicked()), this, SLOT(reject()));
 
65
}
 
66
 
 
67
FindDialog::~FindDialog()
 
68
{
 
69
}
 
70
 
 
71
void FindDialog::findButtonClicked()
 
72
{
 
73
    doFind(ui.radioForward->isChecked());
 
74
}
 
75
 
 
76
void FindDialog::doFind(bool forward)
 
77
{
 
78
    QTextBrowser *browser = static_cast<QTextBrowser*>(mainWindow()->browsers()->currentBrowser());
 
79
    sb->clearMessage();
 
80
 
 
81
    if (ui.comboFind->currentText() != findExpr || lastBrowser != browser)
 
82
        onceFound = false;
 
83
    findExpr = ui.comboFind->currentText();
 
84
 
 
85
    QTextDocument::FindFlags flags = 0;
 
86
 
 
87
    if (ui.checkCase->isChecked())
 
88
        flags |= QTextDocument::FindCaseSensitively;
 
89
 
 
90
    if (ui.checkWords->isChecked())
 
91
        flags |= QTextDocument::FindWholeWords;
 
92
 
 
93
    QTextCursor c = browser->textCursor();
 
94
    if (!c.hasSelection()) {
 
95
        if (forward)
 
96
            c.movePosition(QTextCursor::Start);
 
97
        else
 
98
            c.movePosition(QTextCursor::End);
 
99
 
 
100
        browser->setTextCursor(c);
 
101
    }
 
102
 
 
103
    QTextDocument::FindFlags options;
 
104
    if (forward == false)
 
105
        flags |= QTextDocument::FindBackward;
 
106
 
 
107
    QTextCursor found = browser->document()->find(findExpr, c, flags);
 
108
    if (found.isNull()) {
 
109
        if (onceFound) {
 
110
            if (forward)
 
111
                statusMessage(tr("Search reached end of the document"));
 
112
            else
 
113
                statusMessage(tr("Search reached start of the document"));
 
114
        } else {
 
115
            statusMessage(tr( "Text not found" ));
 
116
        }
 
117
    } else {
 
118
        browser->setTextCursor(found);
 
119
    }
 
120
    onceFound |= !found.isNull();
 
121
    lastBrowser = browser;
 
122
}
 
123
 
 
124
bool FindDialog::hasFindExpression() const
 
125
{
 
126
    return !findExpr.isEmpty();
 
127
}
 
128
 
 
129
void FindDialog::statusMessage(const QString &message)
 
130
{
 
131
    if (isVisible())
 
132
        sb->showMessage(message);
 
133
    else
 
134
        static_cast<MainWindow*>(parent())->statusBar()->showMessage(message, 2000);
 
135
}
 
136
 
 
137
MainWindow *FindDialog::mainWindow() const
 
138
{
 
139
    return static_cast<MainWindow*>(parentWidget());
 
140
}
 
141
 
 
142
void FindDialog::reset()
 
143
{
 
144
    ui.comboFind->setFocus();
 
145
    ui.comboFind->lineEdit()->setSelection(
 
146
        0, ui.comboFind->lineEdit()->text().length());
 
147
}
 
148