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 <QGridLayout>
14#include <QGroupBox>
15#include <QLabel>
16#include <QMessageBox>
17#include <QPushButton>
18#include <QRegExp>
19#include <QVBoxLayout>
20
21//------------------------------------------------------
22// Public member functions
23//------------------------------------------------------
28 : MantidDialog(editor), m_editor(editor), m_findInProgress(false) {
29 initLayout();
30 setSizeGripEnabled(true);
31}
32
37 auto *gb1 = new QGroupBox();
38 m_topLayout = new QGridLayout(gb1);
39
40 m_topLayout->addWidget(new QLabel(tr("Find")), 0, 0);
41 boxFind = new QComboBox();
42 boxFind->setEditable(true);
43 boxFind->setDuplicatesEnabled(false);
44 boxFind->setInsertPolicy(QComboBox::InsertAtTop);
45 boxFind->setMaxCount(10);
46 boxFind->setMaxVisibleItems(10);
47 boxFind->setMinimumWidth(250);
48 boxFind->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed));
49 connect(boxFind, SIGNAL(editTextChanged(const QString &)), this, SLOT(resetSearchFlags()));
50
51 m_topLayout->addWidget(boxFind, 0, 1);
52
54
55 auto *gb2 = new QGroupBox();
56 auto *bottomLayout = new QGridLayout(gb2);
57 auto *find_options = new QButtonGroup(this);
58 find_options->setExclusive(false);
59
60 boxCaseSensitive = new QCheckBox(tr("&Match case"));
61 boxCaseSensitive->setChecked(false);
62 bottomLayout->addWidget(boxCaseSensitive, 0, 0);
63 find_options->addButton(boxCaseSensitive);
64
65 boxWholeWords = new QCheckBox(tr("&Whole word"));
66 boxWholeWords->setChecked(false);
67 bottomLayout->addWidget(boxWholeWords, 1, 0);
68 find_options->addButton(boxWholeWords);
69
70 boxRegex = new QCheckBox(tr("&Regular expression"));
71 boxRegex->setChecked(false);
72 bottomLayout->addWidget(boxRegex, 2, 0);
73 find_options->addButton(boxRegex);
74
75 boxSearchBackwards = new QCheckBox(tr("&Search backwards"));
76 boxSearchBackwards->setChecked(false);
77 bottomLayout->addWidget(boxSearchBackwards, 0, 1);
78 find_options->addButton(boxSearchBackwards);
79
80 boxWrapAround = new QCheckBox(tr("&Wrap around"));
81 boxWrapAround->setChecked(true);
82 bottomLayout->addWidget(boxWrapAround, 1, 1);
83 find_options->addButton(boxWrapAround);
84 connect(find_options, SIGNAL(buttonClicked(int)), this, SLOT(resetSearchFlags()));
85
86 auto *vb1 = new QVBoxLayout();
87 vb1->addWidget(gb1);
88 vb1->addWidget(gb2);
89
90 m_vb2 = new QVBoxLayout();
91
92 buttonNext = new QPushButton(tr("&Next"));
93 buttonNext->setShortcut(tr("Ctrl+F"));
94 buttonNext->setDefault(true);
95 m_vb2->addWidget(buttonNext);
96 connect(buttonNext, SIGNAL(clicked()), this, SLOT(findClicked()));
97
99
100 buttonCancel = new QPushButton(tr("&Close"));
101 m_vb2->addWidget(buttonCancel);
102 m_vb2->addStretch();
103 connect(buttonCancel, SIGNAL(clicked()), this, SLOT(reject()));
104
105 auto *hb = new QHBoxLayout(this);
106 hb->addLayout(vb1);
107 hb->addLayout(m_vb2);
108}
109
112 setWindowTitle(tr("MantidPlot") + " - " + tr("Find and Replace"));
113 m_topLayout->addWidget(new QLabel(tr("Replace with")), 1, 0);
114 boxReplace = new QComboBox();
115 boxReplace->setEditable(true);
116 boxReplace->setDuplicatesEnabled(false);
117 boxReplace->setInsertPolicy(QComboBox::InsertAtTop);
118 boxReplace->setMaxCount(10);
119 boxReplace->setMaxVisibleItems(10);
120 boxReplace->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed));
121 m_topLayout->addWidget(boxReplace, 1, 1);
122 m_topLayout->setColumnStretch(1, 10);
123}
124
127 buttonReplace = new QPushButton(tr("&Replace"));
128 connect(buttonReplace, SIGNAL(clicked()), this, SLOT(replace()));
129 m_vb2->addWidget(buttonReplace);
130
131 buttonReplaceAll = new QPushButton(tr("Replace &all"));
132 connect(buttonReplaceAll, SIGNAL(clicked()), this, SLOT(replaceAll()));
133 m_vb2->addWidget(buttonReplaceAll);
134}
135
136//------------------------------------------------------
137// Protected slot member functions
138//------------------------------------------------------
145bool FindReplaceDialog::find(bool backwards) {
146 QString searchString = boxFind->currentText();
147 if (searchString.isEmpty()) {
148 QMessageBox::warning(this, tr("Empty Search Field"),
149 tr("The search field is empty. Please enter some text and try again."));
150 boxFind->setFocus();
151 return false;
152 }
153
154 if (boxFind->findText(searchString) == -1) {
155 boxFind->addItem(searchString);
156 }
157
158 if (m_findInProgress) {
159 m_findInProgress = m_editor->findNext();
160 } else {
161 bool cs = boxCaseSensitive->isChecked();
162 bool whole = boxWholeWords->isChecked();
163 bool wrap = boxWrapAround->isChecked();
164 bool regex = boxRegex->isChecked();
165 m_findInProgress = m_editor->findFirst(searchString, regex, cs, whole, wrap, !backwards);
166 }
167 return m_findInProgress;
168}
169
174 QString searchString = boxFind->currentText();
175 if (searchString.isEmpty()) {
176 QMessageBox::warning(this, tr("Empty Search Field"),
177 tr("The search field is empty. Please enter some text and try again."));
178 boxFind->setFocus();
179 return;
180 }
181
182 if (!m_editor->hasSelectedText() || m_editor->selectedText() != searchString) {
183 find(); // find and select next match
184 return;
185 }
186
187 QString replaceString = boxReplace->currentText();
188 m_editor->replace(replaceString);
189 find(); // find and select next match
190
191 if (boxReplace->findText(replaceString) == -1) {
192 boxReplace->addItem(replaceString);
193 }
194}
195
200 QString searchString = boxFind->currentText();
201 if (searchString.isEmpty()) {
202 QMessageBox::warning(this, tr("Empty Search Field"),
203 tr("The search field is empty. Please enter some text and try again."));
204 boxFind->setFocus();
205 return;
206 }
207
208 if (boxFind->findText(searchString) == -1) {
209 boxFind->addItem(searchString);
210 }
211
212 QString replaceString = boxReplace->currentText();
213 if (boxReplace->findText(replaceString) == -1) {
214 boxReplace->addItem(replaceString);
215 }
216
217 bool regex = boxRegex->isChecked();
218 bool cs = boxCaseSensitive->isChecked();
219 bool whole = boxWholeWords->isChecked();
220 bool wrap = boxWrapAround->isChecked();
221 bool backward = boxSearchBackwards->isChecked();
222 m_editor->replaceAll(searchString, replaceString, regex, cs, whole, wrap, !backward);
223}
224
229 // Forward to worker function
230 find(boxSearchBackwards->isChecked());
231}
232
237}
238
243
247void FindReplaceDialog::clearEditorSelection() { m_editor->setSelection(-1, -1, -1, -1); }
248
253void FindReplaceDialog::showEvent(QShowEvent *event) {
254 Q_UNUSED(event);
255 if (m_editor->hasSelectedText()) {
256 QString text = m_editor->selectedText();
257 boxFind->setEditText(text);
258 boxFind->addItem(text);
259 }
260}
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.
QVBoxLayout * m_vb2
QPushButton * buttonCancel
Cancel dialog button.
void addReplaceBox()
Add replace box.
This class provides an area to write scripts.
Definition: ScriptEditor.h:37
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.