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
|
#include "searchtextlocator.h"
#include "common/unused.h"
#include <QTextCursor>
#include <QTextBlock>
#include <QRegExp>
#include <QDebug>
SearchTextLocator::SearchTextLocator(QTextDocument* document, QObject* parent) :
QObject(parent), document(document)
{
}
QString SearchTextLocator::getLookupString() const
{
return lookupString;
}
void SearchTextLocator::setLookupString(const QString& value)
{
lookupString = value;
lastMatchStart = -1;
lastMatchEnd = -1;
}
QString SearchTextLocator::getReplaceString() const
{
return replaceString;
}
void SearchTextLocator::setReplaceString(const QString& value)
{
replaceString = value;
}
bool SearchTextLocator::getCaseSensitive() const
{
return caseSensitive;
}
void SearchTextLocator::setCaseSensitive(bool value)
{
caseSensitive = value;
}
bool SearchTextLocator::getRegularExpression() const
{
return regularExpression;
}
void SearchTextLocator::setRegularExpression(bool value)
{
regularExpression = value;
}
bool SearchTextLocator::getSearchBackwards() const
{
return searchBackwards;
}
void SearchTextLocator::setSearchBackwards(bool value)
{
searchBackwards = value;
}
int SearchTextLocator::getStartPosition() const
{
return startPosition;
}
void SearchTextLocator::setStartPosition(int value)
{
if (ignoreCursorMovements)
return;
startPosition = value;
initialStartPosition = value;
afterDocPositionSwitch = false;
lastMatchStart = -1;
lastMatchEnd = -1;
emit replaceAvailable(false);
}
QTextDocument::FindFlags SearchTextLocator::getFlags()
{
QTextDocument::FindFlags flags;
if (caseSensitive)
flags |= QTextDocument::FindCaseSensitively;
if (searchBackwards)
flags |= QTextDocument::FindBackward;
return flags;
}
void SearchTextLocator::notFound()
{
startPosition = initialStartPosition;
afterDocPositionSwitch = false;
emit reachedEnd();
emit replaceAvailable(false);
}
QTextCursor SearchTextLocator::findInWholeDoc(QTextDocument::FindFlags flags)
{
// Simply find a match
QTextCursor cursor;
if (regularExpression)
cursor = document->find(QRegExp(lookupString), startPosition, flags);
else
cursor = document->find(lookupString, startPosition, flags);
// If not matched, see if we can find match at the other part of document (before/after init start position)
if (cursor.isNull() && !afterDocPositionSwitch)
{
afterDocPositionSwitch = true;
int start = 0;
if (flags.testFlag(QTextDocument::FindBackward))
start = document->lastBlock().position() + document->lastBlock().length();
if (regularExpression)
cursor = document->find(QRegExp(lookupString), start, flags);
else
cursor = document->find(lookupString, start, flags);
}
// If we found a match after/before start position, but it's already before/after start position, we cannot report it as a match.
if ((afterDocPositionSwitch && !cursor.isNull()) && // we have a match after switching doc position
((!flags.testFlag(QTextDocument::FindBackward) && cursor.selectionStart() >= initialStartPosition) || // it's after init pos
(flags.testFlag(QTextDocument::FindBackward) && cursor.selectionEnd() <= initialStartPosition))) // it's before init pos
{
cursor = QTextCursor(); // exceeded search range
}
// If we do have a match, we need to remember its parameters for the next lookup.
if (!cursor.isNull())
{
if (flags.testFlag(QTextDocument::FindBackward))
startPosition = cursor.selectionStart();
else
startPosition = cursor.selectionEnd();
lastMatchStart = cursor.selectionStart();
lastMatchEnd = cursor.selectionEnd();
}
return cursor;
}
void SearchTextLocator::replaceCurrent()
{
if (lastMatchStart == -1 || lastMatchEnd == -1)
return;
ignoreCursorMovements = true;
QTextCursor cursor(document);
cursor.setPosition(lastMatchStart);
cursor.setPosition(lastMatchEnd, QTextCursor::KeepAnchor);
cursor.removeSelectedText();
cursor.insertText(replaceString);
ignoreCursorMovements = false;
// Adjust further lookups according to replaced lenght change.
startPosition += replaceString.length() - lookupString.length();
}
bool SearchTextLocator::find(QTextDocument::FindFlags flags)
{
if (flags == 0)
flags = getFlags();
QTextCursor cursor = findInWholeDoc(flags);
if (cursor.isNull())
{
notFound();
return false;
}
emit found(cursor.selectionStart(), cursor.selectionEnd());
emit replaceAvailable(true);
return true;
}
void SearchTextLocator::findNext()
{
QTextDocument::FindFlags flags = getFlags();
flags &= !QTextDocument::FindBackward;
find(flags);
}
void SearchTextLocator::findPrev()
{
QTextDocument::FindFlags flags = getFlags();
flags |= QTextDocument::FindBackward;
find(flags);
}
bool SearchTextLocator::replaceAndFind()
{
replaceCurrent();
return find();
}
void SearchTextLocator::replaceAll()
{
QString origContents = document->toPlainText();
QString contents = origContents;
Qt::CaseSensitivity cs = caseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive;
int replLen = replaceString.length();
int diff = 0;
if (regularExpression)
{
QRegExp re(lookupString, cs);
contents.replace(re, replaceString);
int pos = 0;
while ((pos = re.indexIn(origContents, pos)) != -1 && pos < startPosition)
{
int len = re.matchedLength();
pos += len;
diff += (replLen - len);
}
}
else
{
contents.replace(lookupString, replaceString, cs);
int len = lookupString.length();
int singleDiff = (replLen - len);
int pos = 0;
while ((pos = origContents.indexOf(lookupString, pos, cs)) != -1 && pos < startPosition)
{
pos += len;
diff += singleDiff;
}
}
int newPos = startPosition + diff; // calculated before replacing contents to use original startPosition
QTextCursor cursor(document);
cursor.setPosition(0);
cursor.setPosition(origContents.length(), QTextCursor::KeepAnchor);
cursor.insertText(contents);
emit newCursorPositionAfterAllReplaced(newPos);
}
void SearchTextLocator::cursorMoved()
{
emit replaceAvailable(false);
}
|