KTextWidgets 5.109.0
kfind.h
1/*
2 This file is part of the KDE project
3 SPDX-FileCopyrightText: 2001 S.R. Haque <srhaque@iee.org>.
4 SPDX-FileCopyrightText: 2002 David Faure <david@mandrakesoft.com>
5
6 SPDX-License-Identifier: LGPL-2.0-only
7*/
8
9#ifndef KFIND_H
10#define KFIND_H
11
12#include "ktextwidgets_export.h"
13
14#include <QObject>
15#include <memory>
16
17class QDialog;
18class KFindPrivate;
19
93class KTEXTWIDGETS_EXPORT KFind : public QObject
94{
95 Q_OBJECT
96
97public:
101 enum Options {
102 WholeWordsOnly = 1,
103 FromCursor = 2,
104 SelectedText = 4,
105 CaseSensitive = 8,
106 FindBackwards = 16,
107 RegularExpression = 32,
108 FindIncremental = 64,
109 // Note that KReplaceDialog uses 256 and 512
110 // User extensions can use boolean options above this value.
111 MinimumUserOption = 65536,
112 };
116 Q_DECLARE_FLAGS(SearchOptions, Options)
117
118
122 KFind(const QString &pattern, long options, QWidget *parent);
123
130 KFind(const QString &pattern, long options, QWidget *parent, QWidget *findDialog);
131 ~KFind() override;
132
133 enum Result {
134 NoMatch,
135 Match,
136 };
137
144 bool needData() const;
145
155 void setData(const QString &data, int startPos = -1);
156
168 void setData(int id, const QString &data, int startPos = -1);
169
176 Result find();
177
187 long options() const;
188
195 virtual void setOptions(long options);
196
200 QString pattern() const;
201
205 void setPattern(const QString &pattern);
206
213 int numMatches() const;
214
221 virtual void resetCounts();
222
233 virtual bool validateMatch(const QString &text, int index, int matchedlength);
234
248 virtual bool shouldRestart(bool forceAsking = false, bool showNumMatches = true) const;
249
250#if KTEXTWIDGETS_ENABLE_DEPRECATED_SINCE(5, 70)
267 KTEXTWIDGETS_DEPRECATED_VERSION(5,
268 70,
269 "Use find(const QString &text, const QString &pattern, int index, long options, \
270 int *matchedLength, QRegularExpressionMatch *rmatch).")
271 static int find(const QString &text, const QString &pattern, int index, long options, int *matchedlength);
272#endif
273
274#if KTEXTWIDGETS_ENABLE_DEPRECATED_SINCE(5, 70)
278 KTEXTWIDGETS_DEPRECATED_VERSION_BELATED(5,
279 71,
280 5,
281 70,
282 "Use find(const QString &, const QString &, int, long, \
283 int *, QRegularExpressionMatch *).")
284 static int find(const QString &text, const QRegExp &pattern, int index, long options, int *matchedlength);
285#endif
286
309 static int find(const QString &text, const QString &pattern, int index, long options, int *matchedLength, QRegularExpressionMatch *rmatch);
310
315 virtual void displayFinalDialog() const;
316
324 QDialog *findNextDialog(bool create = false);
325
335
342 int index() const;
343
344Q_SIGNALS:
345#if KTEXTWIDGETS_ENABLE_DEPRECATED_SINCE(5, 81)
361 KTEXTWIDGETS_DEPRECATED_VERSION(5, 81, "Use the KFind::textFound(const QString &, int, int) signal instead.")
362 void highlight(const QString &text, int matchingIndex, int matchedLength); // clazy:exclude=overloaded-signal
363#endif
364
379 void textFound(const QString &text, int matchingIndex, int matchedLength);
380
381#if KTEXTWIDGETS_ENABLE_DEPRECATED_SINCE(5, 81)
397 KTEXTWIDGETS_DEPRECATED_VERSION(5, 81, "Use the KFind::textFoundAtId(int id, int matchingIndex, int matchedLength) signal instead.")
398 void highlight(int id, int matchingIndex, int matchedLength); // clazy:exclude=overloaded-signal
399#endif
400
416 void textFoundAtId(int id, int matchingIndex, int matchedLength);
417
418 // ## TODO docu
419 // findprevious will also emit findNext, after temporarily switching the value
420 // of FindBackwards
421 void findNext();
422
429
437
438protected:
439 QWidget *parentWidget() const;
440 QWidget *dialogsParent() const;
441
442protected:
443 KTEXTWIDGETS_NO_EXPORT KFind(KFindPrivate &dd, const QString &pattern, long options, QWidget *parent);
444 KTEXTWIDGETS_NO_EXPORT KFind(KFindPrivate &dd, const QString &pattern, long options, QWidget *parent, QWidget *findDialog);
445
446private:
447 friend class KReplace;
448 Q_DECLARE_PRIVATE_D(d, KFind)
449 std::unique_ptr<class KFindPrivate> const d;
450 // KF6 TODO: change private d to protected d_ptr, use normal Q_DECLARE_PRIVATE, remove friend
451};
452
453Q_DECLARE_OPERATORS_FOR_FLAGS(KFind::SearchOptions)
454
455#endif
A generic implementation of the "find" function.
Definition kfind.h:94
void setData(int id, const QString &data, int startPos=-1)
Call this when needData returns true, before calling find().
virtual void displayFinalDialog() const
Displays the final dialog saying "no match was found", if that was the case.
static int find(const QString &text, const QString &pattern, int index, long options, int *matchedLength, QRegularExpressionMatch *rmatch)
Search text for pattern.
virtual bool validateMatch(const QString &text, int index, int matchedlength)
Virtual method, which allows applications to add extra checks for validating a candidate match.
int numMatches() const
Returns the number of matches found (i.e.
void textFound(const QString &text, int matchingIndex, int matchedLength)
Connect to this signal to implement highlighting of found text during the find operation.
virtual void setOptions(long options)
Set new options.
bool needData() const
void setPattern(const QString &pattern)
Change the pattern we're looking for.
void closeFindNextDialog()
Close the "find next?" dialog.
void dialogClosed()
Emitted when the 'find next' dialog is being closed.
int index() const
QFlags< Options > SearchOptions
Stores a combination of #Options values.
Definition kfind.h:116
void optionsChanged()
Emitted when the options have changed.
virtual bool shouldRestart(bool forceAsking=false, bool showNumMatches=true) const
Returns true if we should restart the search from scratch.
QString pattern() const
Result find()
Walk the text fragment (e.g.
Options
Definition kfind.h:101
void setData(const QString &data, int startPos=-1)
Call this when needData returns true, before calling find().
void textFoundAtId(int id, int matchingIndex, int matchedLength)
Connect to this signal to implement highlighting of found text during the find operation.
long options() const
Return the current options.
virtual void resetCounts()
Call this to reset the numMatches count (and the numReplacements count for a KReplace).
QDialog * findNextDialog(bool create=false)
Return (or create) the dialog that shows the "find next?" prompt.
A generic implementation of the "replace" function.
Definition kreplace.h:90