Mantid
Loading...
Searching...
No Matches
FindReplaceDialog.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
9
10#include <QButtonGroup>
11#include <QCheckBox>
12#include <QComboBox>
13#include <QCoreApplication>
14#include <QGridLayout>
15#include <QGroupBox>
16#include <QLabel>
17#include <QMessageBox>
18#include <QPushButton>
19#include <QRegExp>
20#include <QVBoxLayout>
21
22//------------------------------------------------------
23// Public member functions
24//------------------------------------------------------
29 : MantidDialog(editor), m_editor(editor), m_findInProgress(false) {
30 initLayout();
31 setSizeGripEnabled(true);
32}
33
38 auto *gb1 = new QGroupBox();
39 m_topLayout = new QGridLayout(gb1);
40
41 m_topLayout->addWidget(new QLabel(tr("Find")), 0, 0);
42 boxFind = new QComboBox();
43 boxFind->setEditable(true);
44 boxFind->setDuplicatesEnabled(false);
45 boxFind->setInsertPolicy(QComboBox::InsertAtTop);
46 boxFind->setMaxCount(10);
47 boxFind->setMaxVisibleItems(10);
48 boxFind->setMinimumWidth(250);
49 boxFind->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed));
50 connect(boxFind, SIGNAL(editTextChanged(const QString &)), this, SLOT(resetSearchFlags()));
51
52 m_topLayout->addWidget(boxFind, 0, 1);
53
55
56 auto *gb2 = new QGroupBox();
57 auto *bottomLayout = new QGridLayout(gb2);
58 auto *find_options = new QButtonGroup(this);
59 find_options->setExclusive(false);
60
61 boxCaseSensitive = new QCheckBox(tr("&Match case"));
62 boxCaseSensitive->setChecked(false);
63 bottomLayout->addWidget(boxCaseSensitive, 0, 0);
64 find_options->addButton(boxCaseSensitive);
65
66 boxWholeWords = new QCheckBox(tr("&Whole word"));
67 boxWholeWords->setChecked(false);
68 bottomLayout->addWidget(boxWholeWords, 1, 0);
69 find_options->addButton(boxWholeWords);
70
71 boxRegex = new QCheckBox(tr("&Regular expression"));
72 boxRegex->setChecked(false);
73 bottomLayout->addWidget(boxRegex, 2, 0);
74 find_options->addButton(boxRegex);
75
76 boxSearchBackwards = new QCheckBox(tr("&Search backwards"));
77 boxSearchBackwards->setChecked(false);
78 bottomLayout->addWidget(boxSearchBackwards, 0, 1);
79 find_options->addButton(boxSearchBackwards);
80
81 boxWrapAround = new QCheckBox(tr("&Wrap around"));
82 boxWrapAround->setChecked(true);
83 bottomLayout->addWidget(boxWrapAround, 1, 1);
84 find_options->addButton(boxWrapAround);
85 connect(find_options, SIGNAL(buttonClicked(int)), this, SLOT(resetSearchFlags()));
86
87 auto *vb1 = new QVBoxLayout();
88 vb1->addWidget(gb1);
89 vb1->addWidget(gb2);
90
91 m_vb2 = new QVBoxLayout();
92
93 buttonNext = new QPushButton(tr("&Next"));
94 buttonNext->setShortcut(tr("Ctrl+F"));
95 buttonNext->setDefault(true);
96 m_vb2->addWidget(buttonNext);
97 connect(buttonNext, SIGNAL(clicked()), this, SLOT(findClicked()));
98
100
101 buttonCancel = new QPushButton(tr("&Close"));
102 m_vb2->addWidget(buttonCancel);
103 m_vb2->addStretch();
104 connect(buttonCancel, SIGNAL(clicked()), this, SLOT(reject()));
105
106 auto *hb = new QHBoxLayout(this);
107 hb->addLayout(vb1);
108 hb->addLayout(m_vb2);
109}
110
113 setWindowTitle(QCoreApplication::applicationName() + " - Find and Replace");
114 m_topLayout->addWidget(new QLabel(tr("Replace with")), 1, 0);
115 boxReplace = new QComboBox();
116 boxReplace->setEditable(true);
117 boxReplace->setDuplicatesEnabled(false);
118 boxReplace->setInsertPolicy(QComboBox::InsertAtTop);
119 boxReplace->setMaxCount(10);
120 boxReplace->setMaxVisibleItems(10);
121 boxReplace->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed));
122 m_topLayout->addWidget(boxReplace, 1, 1);
123 m_topLayout->setColumnStretch(1, 10);
124}
125
128 buttonReplace = new QPushButton(tr("&Replace"));
129 connect(buttonReplace, SIGNAL(clicked()), this, SLOT(replace()));
130 m_vb2->addWidget(buttonReplace);
131
132 buttonReplaceAll = new QPushButton(tr("Replace &all"));
133 connect(buttonReplaceAll, SIGNAL(clicked()), this, SLOT(replaceAll()));
134 m_vb2->addWidget(buttonReplaceAll);
135}
136
137//------------------------------------------------------
138// Protected slot member functions
139//------------------------------------------------------
146bool FindReplaceDialog::find(bool backwards) {
147 QString searchString = boxFind->currentText();
148 if (searchString.isEmpty()) {
149 QMessageBox::warning(this, tr("Empty Search Field"),
150 tr("The search field is empty. Please enter some text and try again."));
151 boxFind->setFocus();
152 return false;
153 }
154
155 if (boxFind->findText(searchString) == -1) {
156 boxFind->addItem(searchString);
157 }
158
159 if (m_findInProgress) {
160 m_findInProgress = m_editor->findNext();
161 } else {
162 bool cs = boxCaseSensitive->isChecked();
163 bool whole = boxWholeWords->isChecked();
164 bool wrap = boxWrapAround->isChecked();
165 bool regex = boxRegex->isChecked();
166 m_findInProgress = m_editor->findFirst(searchString, regex, cs, whole, wrap, !backwards);
167 }
168 return m_findInProgress;
169}
170
175 QString searchString = boxFind->currentText();
176 if (searchString.isEmpty()) {
177 QMessageBox::warning(this, tr("Empty Search Field"),
178 tr("The search field is empty. Please enter some text and try again."));
179 boxFind->setFocus();
180 return;
181 }
182
183 if (!m_editor->hasSelectedText() || m_editor->selectedText() != searchString) {
184 find(); // find and select next match
185 return;
186 }
187
188 QString replaceString = boxReplace->currentText();
189 m_editor->replace(replaceString);
190 find(); // find and select next match
191
192 if (boxReplace->findText(replaceString) == -1) {
193 boxReplace->addItem(replaceString);
194 }
195}
196
201 QString searchString = boxFind->currentText();
202 if (searchString.isEmpty()) {
203 QMessageBox::warning(this, tr("Empty Search Field"),
204 tr("The search field is empty. Please enter some text and try again."));
205 boxFind->setFocus();
206 return;
207 }
208
209 if (boxFind->findText(searchString) == -1) {
210 boxFind->addItem(searchString);
211 }
212
213 QString replaceString = boxReplace->currentText();
214 if (boxReplace->findText(replaceString) == -1) {
215 boxReplace->addItem(replaceString);
216 }
217
218 bool regex = boxRegex->isChecked();
219 bool cs = boxCaseSensitive->isChecked();
220 bool whole = boxWholeWords->isChecked();
221 bool wrap = boxWrapAround->isChecked();
222 bool backward = boxSearchBackwards->isChecked();
223 m_editor->replaceAll(searchString, replaceString, regex, cs, whole, wrap, !backward);
224}
225
230 // Forward to worker function
231 find(boxSearchBackwards->isChecked());
232}
233
239
244
248void FindReplaceDialog::clearEditorSelection() { m_editor->setSelection(-1, -1, -1, -1); }
249
254void FindReplaceDialog::showEvent(QShowEvent *event) {
255 Q_UNUSED(event);
256 if (m_editor->hasSelectedText()) {
257 QString text = m_editor->selectedText();
258 boxFind->setEditText(text);
259 boxFind->addItem(text);
260 }
261}
void resetSearchFlags()
Reset the search flags due to changes.
QCheckBox * boxCaseSensitive
Case-sensitive check box.
QComboBox * boxFind
Find box.
bool find(bool backwards=false)
Find.
void clearEditorSelection()
Clear the editor selection.
void showEvent(QShowEvent *event) override
Called when the widget is shown.
QCheckBox * boxWholeWords
Whole words check box.
QCheckBox * boxWrapAround
Wrap around.
void findClicked()
A slot for the findClicked button.
QComboBox * boxReplace
Replace box.
QPushButton * buttonReplace
Replace text button.
void addReplaceButtons()
Add the replace buttons.
FindReplaceDialog(ScriptEditor *editor)
Constructor.
void initLayout()
Create the layout.
QPushButton * buttonReplaceAll
Replace all text button.
void findNotInProgress()
Set the flag about whether we are currently finding.
QGridLayout * m_topLayout
void replace()
Replace slot.
QCheckBox * boxSearchBackwards
Search backwards.
QCheckBox * boxRegex
Treat as regular expressions.
bool m_findInProgress
If a find is in progress.
void replaceAll()
Replace all slot.
QPushButton * buttonNext
Find next match button.
ScriptEditor * m_editor
The text editor we are working on.
QPushButton * buttonCancel
Cancel dialog button.
void addReplaceBox()
Add replace box.
This class provides an area to write scripts.
void replaceAll(const QString &search, const QString &replace, bool regex, bool caseSensitive, bool matchWords, bool wrap, bool forward=true)
Replace all occurences of a string.