KTextEditor

katesearchbar.h
1/*
2 SPDX-FileCopyrightText: 2009-2010 Bernhard Beschow <bbeschow@cs.tu-berlin.de>
3 SPDX-FileCopyrightText: 2007 Sebastian Pipping <webmaster@hartwork.org>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6*/
7
8#ifndef KATE_SEARCH_BAR_H
9#define KATE_SEARCH_BAR_H 1
10
11#include "kateviewhelpers.h"
12#include <ktexteditor_export.h>
13
14#include <ktexteditor/attribute.h>
15#include <ktexteditor/document.h>
16
17namespace KTextEditor
18{
19class ViewPrivate;
20}
21class KateViewConfig;
22class QVBoxLayout;
23class QComboBox;
24
25namespace Ui
26{
27class IncrementalSearchBar;
28class PowerSearchBar;
29}
30
31namespace KTextEditor
32{
33class MovingRange;
34class Message;
35}
36
37class KTEXTEDITOR_EXPORT KateSearchBar : public KateViewBarWidget
38{
39 Q_OBJECT
40
41 friend class SearchBarTest;
42
43public:
44 enum SearchMode {
45 // NOTE: Concrete values are important here
46 // to work with the combobox index!
47 MODE_PLAIN_TEXT = 0,
48 MODE_WHOLE_WORDS = 1,
49 MODE_ESCAPE_SEQUENCES = 2,
50 MODE_REGEX = 3
51 };
52
53 enum MatchResult {
54 MatchFound,
55 MatchWrappedForward,
56 MatchWrappedBackward,
57 MatchMismatch,
58 MatchNothing,
59 MatchNeutral
60 };
61
62 enum SearchDirection {
63 SearchForward,
64 SearchBackward
65 };
66
67public:
68 explicit KateSearchBar(bool initAsPower, KTextEditor::ViewPrivate *view, KateViewConfig *config);
69 ~KateSearchBar() override;
70
71 void closed() override;
72
73 bool isPower() const;
74
75 QString searchPattern() const;
76 QString replacementPattern() const;
77
78 bool selectionOnly() const;
79 bool matchCase() const;
80
81 void nextMatchForSelection(KTextEditor::ViewPrivate *view, SearchDirection searchDirection);
82
83public Q_SLOTS:
84 /**
85 * Set the current search pattern.
86 * @param searchPattern the search pattern
87 */
88 void setSearchPattern(const QString &searchPattern);
89
90 /**
91 * Set the current replacement pattern.
92 * @param replacementPattern the replacement pattern
93 */
94 void setReplacementPattern(const QString &replacementPattern);
95
96 void setSearchMode(SearchMode mode);
97 void setSelectionOnly(bool selectionOnly);
98 void setMatchCase(bool matchCase);
99
100 // Called by buttons and typically <F3>/<Shift>+<F3> shortcuts
101 void findNext();
102 void findPrevious();
103
104 // PowerMode stuff
105 void findAll();
106 void replaceNext();
107 void replaceAll();
108
109 // Also used by KTextEditor::ViewPrivate
110 void enterPowerMode();
111 void enterIncrementalMode();
112
113 bool clearHighlights();
114 void updateHighlightColors();
115
116 // read write status of document changed
117 void slotReadWriteChanged();
118
119protected:
120 // Overridden
121 void showEvent(QShowEvent *event) override;
122 bool eventFilter(QObject *obj, QEvent *event) override;
123
124private Q_SLOTS:
125 void onIncPatternChanged(const QString &pattern);
126 void onMatchCaseToggled(bool matchCase);
127
128 void onReturnPressed();
129 void updateSelectionOnly();
130 void updateIncInitCursor();
131
132 void onPowerPatternChanged(const QString &pattern);
133 void onPowerModeChanged(int index);
134 void onPowerPatternContextMenuRequest();
135 void onPowerPatternContextMenuRequest(const QPoint &);
136 void onPowerReplacmentContextMenuRequest();
137 void onPowerReplacmentContextMenuRequest(const QPoint &);
138 void onPowerCancelFindOrReplace();
139
140 /**
141 * This function do the hard search & replace work in time slice steps.
142 * When all is done @ref m_matchCounter is set and the signal
143 * @ref findOrReplaceAllFinished() is emitted.
144 */
145 void findOrReplaceAll();
146
147 /**
148 * Restore needed settings when signal @ref findOrReplaceAllFinished()
149 * was received.
150 */
151 void endFindOrReplaceAll();
152
154 /**
155 * Will emitted by @ref findOrReplaceAll() when all is done.
156 */
157 void findOrReplaceAllFinished();
158
159private:
160 // Helpers
161 bool find(SearchDirection searchDirection = SearchForward)
162 {
163 return findOrReplace(searchDirection, nullptr);
164 };
165 KTEXTEDITOR_NO_EXPORT
166 bool findOrReplace(SearchDirection searchDirection, const QString *replacement);
167
168 /**
169 * The entry point to start a search & replace task.
170 * Set needed member variables and call @ref findOrReplaceAll() to do the work.
171 */
172 KTEXTEDITOR_NO_EXPORT
173 void beginFindOrReplaceAll(KTextEditor::Range inputRange, const QString &replacement, bool replaceMode = true);
174 KTEXTEDITOR_NO_EXPORT
175 void beginFindAll(KTextEditor::Range inputRange)
176 {
177 beginFindOrReplaceAll(inputRange, QString(), false);
178 };
179
180 KTEXTEDITOR_NO_EXPORT
181 bool isPatternValid() const;
182
183 KTEXTEDITOR_NO_EXPORT
184 KTextEditor::SearchOptions searchOptions(SearchDirection searchDirection = SearchForward) const;
185
186 KTEXTEDITOR_NO_EXPORT
187 void highlightMatch(KTextEditor::Range range);
188 KTEXTEDITOR_NO_EXPORT
189 void highlightReplacement(KTextEditor::Range range);
190 KTEXTEDITOR_NO_EXPORT
191 void indicateMatch(MatchResult matchResult);
192 KTEXTEDITOR_NO_EXPORT
193 static void selectRange(KTextEditor::ViewPrivate *view, KTextEditor::Range range);
194 KTEXTEDITOR_NO_EXPORT
195 void selectRange2(KTextEditor::Range range);
196
197 KTEXTEDITOR_NO_EXPORT
198 QList<QString> getCapturePatterns(const QString &pattern) const;
199 KTEXTEDITOR_NO_EXPORT
200 void showExtendedContextMenu(bool forPattern, const QPoint &pos);
201
202 KTEXTEDITOR_NO_EXPORT
203 void givePatternFeedback();
204 KTEXTEDITOR_NO_EXPORT
205 void addCurrentTextToHistory(QComboBox *combo);
206 KTEXTEDITOR_NO_EXPORT
207 void backupConfig(bool ofPower);
208 KTEXTEDITOR_NO_EXPORT
209 void sendConfig();
210
211 KTEXTEDITOR_NO_EXPORT
212 void showResultMessage();
213
214private:
215 KTextEditor::ViewPrivate *const m_view;
216 KateViewConfig *const m_config;
218 QPointer<KTextEditor::Message> m_infoMessage;
219
220 // Shared by both dialogs
221 QVBoxLayout *const m_layout;
222 QWidget *m_widget;
223 QString m_unfinishedSearchText;
224
225 // Incremental search related
226 Ui::IncrementalSearchBar *m_incUi;
227 KTextEditor::Cursor m_incInitCursor;
228
229 // Power search related
230 Ui::PowerSearchBar *m_powerUi = nullptr;
231 KTextEditor::MovingRange *m_workingRange = nullptr;
232 KTextEditor::Range m_inputRange;
233 QString m_replacement;
234 uint m_matchCounter = 0;
235 bool m_replaceMode = false;
236 bool m_cancelFindOrReplace = true;
237 bool m_selectionChangedByUndoRedo = false;
238 std::vector<KTextEditor::Range> m_highlightRanges;
239
240 // attribute to highlight matches with
241 KTextEditor::Attribute::Ptr highlightMatchAttribute;
242 KTextEditor::Attribute::Ptr highlightReplacementAttribute;
243
244 // Status backup
245 bool m_incHighlightAll : 1;
246 bool m_incFromCursor : 1;
247 bool m_incMatchCase : 1;
248 bool m_powerMatchCase : 1;
249 bool m_powerFromCursor : 1;
250 bool m_powerHighlightAll : 1;
251 unsigned int m_powerMode : 2;
252};
253
254#endif // KATE_SEARCH_BAR_H
The Cursor represents a position in a Document.
Definition cursor.h:75
A range that is bound to a specific Document, and maintains its position.
An object representing a section of text, from one Cursor to another.
QAction * findNext(const QObject *recvr, const char *slot, QObject *parent)
The KTextEditor namespace contains all the public API that is required to use the KTextEditor compone...
Q_SIGNALSQ_SIGNALS
Q_SLOTSQ_SLOTS
virtual bool eventFilter(QObject *watched, QEvent *event)
QWidget * find(WId id)
virtual void showEvent(QShowEvent *event)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 29 2024 11:53:44 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.