Commit c41c4d97 authored by con's avatar con
Browse files

Show wrap indicator when searching in text edits.

Adds a showWrapIndicator(parent) method to IFindSupport, to be used by
implementations. First implementation is given for the BaseFindSupport
that's used for all searches in Q(Plain)TextEdits.

Task-number: QTCREATORBUG-2753
parent f30c7f37
......@@ -50,26 +50,32 @@ struct BaseTextFindPrivate {
QPointer<QTextEdit> m_editor;
QPointer<QPlainTextEdit> m_plaineditor;
QPointer<QWidget> m_widget;
QTextCursor m_findScopeStart;
QTextCursor m_findScopeEnd;
int m_findScopeVerticalBlockSelectionFirstColumn;
int m_findScopeVerticalBlockSelectionLastColumn;
int m_incrementalStartPos;
bool m_incrementalWrappedState;
};
BaseTextFindPrivate::BaseTextFindPrivate(QTextEdit *editor)
: m_editor(editor)
, m_widget(editor)
, m_findScopeVerticalBlockSelectionFirstColumn(-1)
, m_findScopeVerticalBlockSelectionLastColumn(-1)
, m_incrementalStartPos(-1)
, m_incrementalWrappedState(false)
{
}
BaseTextFindPrivate::BaseTextFindPrivate(QPlainTextEdit *editor)
: m_plaineditor(editor)
, m_widget(editor)
, m_findScopeVerticalBlockSelectionFirstColumn(-1)
, m_findScopeVerticalBlockSelectionLastColumn(-1)
, m_incrementalStartPos(-1)
, m_incrementalWrappedState(false)
{
}
......@@ -127,6 +133,7 @@ Find::FindFlags BaseTextFind::supportedFindFlags() const
void BaseTextFind::resetIncrementalSearch()
{
d->m_incrementalStartPos = -1;
d->m_incrementalWrappedState = false;
}
void BaseTextFind::clearResults()
......@@ -174,7 +181,13 @@ IFindSupport::Result BaseTextFind::findIncremental(const QString &txt, Find::Fin
if (d->m_incrementalStartPos < 0)
d->m_incrementalStartPos = cursor.selectionStart();
cursor.setPosition(d->m_incrementalStartPos);
bool found = find(txt, findFlags, cursor);
bool wrapped = false;
bool found = find(txt, findFlags, cursor, &wrapped);
if (wrapped != d->m_incrementalWrappedState) {
d->m_incrementalWrappedState = wrapped;
if (found)
showWrapIndicator(d->m_widget);
}
if (found)
emit highlightAll(txt, findFlags);
else
......@@ -184,9 +197,14 @@ IFindSupport::Result BaseTextFind::findIncremental(const QString &txt, Find::Fin
IFindSupport::Result BaseTextFind::findStep(const QString &txt, Find::FindFlags findFlags)
{
bool found = find(txt, findFlags, textCursor());
if (found)
bool wrapped = false;
bool found = find(txt, findFlags, textCursor(), &wrapped);
if (wrapped)
showWrapIndicator(d->m_widget);
if (found) {
d->m_incrementalStartPos = textCursor().selectionStart();
d->m_incrementalWrappedState = false;
}
return found ? Found : NotFound;
}
......@@ -220,7 +238,11 @@ bool BaseTextFind::replaceStep(const QString &before, const QString &after,
Find::FindFlags findFlags)
{
QTextCursor cursor = replaceInternal(before, after, findFlags);
return find(before, findFlags, cursor);
bool wrapped = false;
bool found = find(before, findFlags, cursor, &wrapped);
if (wrapped)
showWrapIndicator(d->m_widget);
return found;
}
int BaseTextFind::replaceAll(const QString &before, const QString &after,
......@@ -254,7 +276,7 @@ int BaseTextFind::replaceAll(const QString &before, const QString &after,
bool BaseTextFind::find(const QString &txt,
Find::FindFlags findFlags,
QTextCursor start)
QTextCursor start, bool *wrapped)
{
if (txt.isEmpty()) {
setTextCursor(start);
......@@ -264,11 +286,15 @@ bool BaseTextFind::find(const QString &txt,
regexp.setPatternSyntax((findFlags&Find::FindRegularExpression) ? QRegExp::RegExp : QRegExp::FixedString);
regexp.setCaseSensitivity((findFlags&Find::FindCaseSensitively) ? Qt::CaseSensitive : Qt::CaseInsensitive);
QTextCursor found = findOne(regexp, start, Find::textDocumentFlagsForFindFlags(findFlags));
if (wrapped)
*wrapped = false;
if (!d->m_findScopeStart.isNull()) {
// scoped
if (found.isNull() || !inScope(found.selectionStart(), found.selectionEnd())) {
if (wrapped)
*wrapped = true;
if ((findFlags&Find::FindBackward) == 0)
start.setPosition(d->m_findScopeStart.position());
else
......@@ -281,6 +307,8 @@ bool BaseTextFind::find(const QString &txt,
// entire document
if (found.isNull()) {
if (wrapped)
*wrapped = true;
if ((findFlags&Find::FindBackward) == 0)
start.movePosition(QTextCursor::Start);
else
......
......@@ -85,7 +85,8 @@ signals:
private:
bool find(const QString &txt,
Find::FindFlags findFlags,
QTextCursor start);
QTextCursor start,
bool *wrapped);
QTextCursor replaceInternal(const QString &before, const QString &after,
Find::FindFlags findFlags);
......
......@@ -28,7 +28,8 @@ SOURCES += findtoolwindow.cpp \
searchresulttreemodel.cpp \
searchresulttreeview.cpp \
searchresultwindow.cpp \
ifindfilter.cpp
ifindfilter.cpp \
ifindsupport.cpp
FORMS += findwidget.ui \
finddialog.ui
RESOURCES += find.qrc
<RCC>
<qresource prefix="/find" >
<qresource prefix="/find">
<file>images/casesensitively.png</file>
<file>images/wholewords.png</file>
<file>images/regexp.png</file>
<file>images/expand.png</file>
<file>images/wrapindicator.png</file>
</qresource>
</RCC>
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** No Commercial Usage
**
** 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.
**
** 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.
**
** 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.
**
**************************************************************************/
#include "ifindsupport.h"
#include <QtCore/QTimer>
#include <QtCore/QPropertyAnimation>
#include <QtGui/QWidget>
#include <QtGui/QPaintEvent>
#include <QtGui/QPainter>
namespace Find {
namespace Internal {
class WrapIndicator : public QWidget
{
Q_OBJECT
Q_PROPERTY(qreal opacity READ opacity WRITE setOpacity USER true)
public:
WrapIndicator(QWidget *parent = 0)
: QWidget(parent),
m_opacity(1.0)
{
if (parent)
setGeometry(QRect(parent->rect().center() - QPoint(25, 25),
parent->rect().center() + QPoint(25, 25)));
}
qreal opacity() const { return m_opacity; }
void setOpacity(qreal value) { m_opacity = value; update(); }
void run()
{
show();
QTimer::singleShot(300, this, SLOT(runInternal()));
}
protected:
void paintEvent(QPaintEvent *)
{
static QPixmap foreground(QLatin1String(":/find/images/wrapindicator.png"));
QPainter p(this);
p.setOpacity(m_opacity);
p.drawPixmap(rect(), foreground);
}
private slots:
void runInternal()
{
QPropertyAnimation *anim = new QPropertyAnimation(this, "opacity", this);
anim->setDuration(200);
anim->setEndValue(0.);
connect(anim, SIGNAL(finished()), this, SLOT(deleteLater()));
anim->start(QAbstractAnimation::DeleteWhenStopped);
}
private:
qreal m_opacity;
};
} // Internal
} // Find
using namespace Find;
void IFindSupport::showWrapIndicator(QWidget *parent)
{
(new Internal::WrapIndicator(parent))->run();
}
#include "ifindsupport.moc"
......@@ -73,6 +73,8 @@ public:
virtual void defineFindScope(){}
virtual void clearFindScope(){}
static void showWrapIndicator(QWidget *parent);
signals:
void changed();
};
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment