Skip to content

Commit b5d5d03

Browse files
committed
Begin to add Replace functionality
1 parent 9e5d9db commit b5d5d03

File tree

7 files changed

+192
-65
lines changed

7 files changed

+192
-65
lines changed

src/server/gui/dialogs/server_dialogfindreplace.cpp

Lines changed: 80 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22
#include "ui_server_dialogfindreplace.h"
33

44
#include <gui/server_ui.h>
5-
#include <gui/server_windowmanager.h>
5+
#include <gui/script/server_scriptmanager.h>
6+
#include <gui/script/server_basescripteditor.h>
67

78
#define MAX_FIND_COMBO_SZ 20
89

910
mbServerDialogFindReplace::Strings::Strings() :
10-
cachePrefix (QStringLiteral("Ui.Dialogs.FindReplace.")),
11-
findComboBox(QStringLiteral("findComboBox"))
11+
cachePrefix (QStringLiteral("Ui.Dialogs.FindReplace.")),
12+
findComboBox (QStringLiteral("findComboBox")),
13+
replaceComboBox(QStringLiteral("replaceComboBox"))
1214
{
1315
}
1416

@@ -26,6 +28,8 @@ mbServerDialogFindReplace::mbServerDialogFindReplace(QWidget *parent) :
2628

2729
connect(ui->btnFindNext , &QPushButton::clicked, this, &mbServerDialogFindReplace::findNext );
2830
connect(ui->btnFindPrevious, &QPushButton::clicked, this, &mbServerDialogFindReplace::findPrevious);
31+
connect(ui->btnReplace , &QPushButton::clicked, this, &mbServerDialogFindReplace::replace );
32+
connect(ui->btnReplaceAll , &QPushButton::clicked, this, &mbServerDialogFindReplace::replaceAll );
2933
connect(ui->btnClose , &QPushButton::clicked, this, &mbServerDialogFindReplace::close );
3034
}
3135

@@ -39,11 +43,20 @@ MBSETTINGS mbServerDialogFindReplace::cachedSettings() const
3943
const Strings &ds = Strings::instance();
4044
const QString &prefix = ds.cachePrefix;
4145

46+
QComboBox *cmb;
4247
QStringList lsFindComboBox;
43-
for (int i = 0; i < ui->cmbFind->count(); ++i)
44-
lsFindComboBox.append(ui->cmbFind->itemText(i));
48+
cmb = ui->cmbFind;
49+
for (int i = 0; i < cmb->count(); ++i)
50+
lsFindComboBox.append(cmb->itemText(i));
51+
52+
QStringList lsReplaceComboBox;
53+
cmb = ui->cmbReplace;
54+
for (int i = 0; i < cmb->count(); ++i)
55+
lsReplaceComboBox.append(cmb->itemText(i));
56+
4557
MBSETTINGS m = mbCoreDialogBase::cachedSettings();
46-
m[prefix+ds.findComboBox] = lsFindComboBox;
58+
m[prefix+ds.findComboBox ] = lsFindComboBox ;
59+
m[prefix+ds.replaceComboBox] = lsReplaceComboBox;
4760
return m;
4861
}
4962

@@ -55,6 +68,7 @@ void mbServerDialogFindReplace::setCachedSettings(const MBSETTINGS &settings)
5568

5669
MBSETTINGS::const_iterator it;
5770
MBSETTINGS::const_iterator end = settings.end();
71+
5872
it = settings.find(prefix+ds.findComboBox);
5973
if (it != end)
6074
{
@@ -66,38 +80,87 @@ void mbServerDialogFindReplace::setCachedSettings(const MBSETTINGS &settings)
6680
++i;
6781
}
6882
}
83+
84+
it = settings.find(prefix+ds.replaceComboBox);
85+
if (it != end)
86+
{
87+
QStringList ls = it.value().toStringList();
88+
int i = 0;
89+
Q_FOREACH(const QString &s, ls)
90+
{
91+
ui->cmbReplace->insertItem(i, s);
92+
++i;
93+
}
94+
}
6995
}
7096

7197
void mbServerDialogFindReplace::execFindReplace(bool replace)
7298
{
7399
if (replace)
100+
{
74101
this->setWindowTitle("Replace");
102+
}
75103
else
104+
{
76105
this->setWindowTitle("Find");
106+
}
107+
ui->lblReplace ->setVisible(replace);
108+
ui->cmbReplace ->setVisible(replace);
109+
ui->btnReplace ->setVisible(replace);
110+
ui->btnReplaceAll->setVisible(replace);
77111
this->show();
78-
QString text = mbServer::global()->ui()->windowManager()->selectedText();
79-
if (text.size())
80-
ui->cmbFind->setCurrentText(text);
112+
mbServerBaseScriptEditor *se = mbServer::global()->ui()->scriptManager()->activeScriptEditor();
113+
if (se)
114+
{
115+
QString text = se->textCursor().selectedText();
116+
if (text.size())
117+
ui->cmbFind->setCurrentText(text);
118+
}
81119
}
82120

83121
void mbServerDialogFindReplace::findNext()
84122
{
85-
processFindCombo();
86-
int flags = getFindFlags();
87-
mbServer::global()->ui()->windowManager()->find(ui->cmbFind->currentText(), flags);
123+
processCombo(ui->cmbFind);
124+
mbServerBaseScriptEditor *se = mbServer::global()->ui()->scriptManager()->activeScriptEditor();
125+
if (se)
126+
{
127+
int flags = getFindFlags();
128+
se->findText(ui->cmbFind->currentText(), flags);
129+
}
88130
}
89131

90132
void mbServerDialogFindReplace::findPrevious()
91133
{
92-
processFindCombo();
93-
int flags = getFindFlags();
94-
flags |= mb::FindBackward;
95-
mbServer::global()->ui()->windowManager()->find(ui->cmbFind->currentText(), flags);
134+
processCombo(ui->cmbFind);
135+
mbServerBaseScriptEditor *se = mbServer::global()->ui()->scriptManager()->activeScriptEditor();
136+
if (se)
137+
{
138+
int flags = getFindFlags();
139+
flags |= mb::FindBackward;
140+
se->findText(ui->cmbFind->currentText(), flags);
141+
}
96142
}
97143

98144
void mbServerDialogFindReplace::replace()
99145
{
146+
processCombo(ui->cmbFind);
147+
processCombo(ui->cmbReplace);
148+
mbServerBaseScriptEditor *se = mbServer::global()->ui()->scriptManager()->activeScriptEditor();
149+
if (se)
150+
{
151+
se->replaceText(ui->cmbReplace->currentText());
152+
}
153+
}
100154

155+
void mbServerDialogFindReplace::replaceAll()
156+
{
157+
processCombo(ui->cmbFind);
158+
processCombo(ui->cmbReplace);
159+
mbServerBaseScriptEditor *se = mbServer::global()->ui()->scriptManager()->activeScriptEditor();
160+
if (se)
161+
{
162+
;//se->replaceTextAll(ui->cmbReplace->currentText());
163+
}
101164
}
102165

103166
int mbServerDialogFindReplace::getFindFlags()
@@ -108,9 +171,8 @@ int mbServerDialogFindReplace::getFindFlags()
108171
return flags;
109172
}
110173

111-
void mbServerDialogFindReplace::processFindCombo()
174+
void mbServerDialogFindReplace::processCombo(QComboBox *cmb)
112175
{
113-
QComboBox *cmb = ui->cmbFind;
114176
QString text = cmb->currentText();
115177
if (text.size())
116178
{

src/server/gui/dialogs/server_dialogfindreplace.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
#include <gui/dialogs/core_dialogedit.h>
55

6+
class QComboBox;
7+
68
namespace Ui {
79
class mbServerDialogFindReplace;
810
}
@@ -18,6 +20,7 @@ class mbServerDialogFindReplace : public mbCoreDialogBase
1820
{
1921
const QString cachePrefix;
2022
const QString findComboBox;
23+
const QString replaceComboBox;
2124

2225
Strings();
2326
static const Strings &instance();
@@ -38,10 +41,11 @@ public Q_SLOTS:
3841
void findNext();
3942
void findPrevious();
4043
void replace();
44+
void replaceAll();
4145

4246
private:
4347
int getFindFlags();
44-
void processFindCombo();
48+
void processCombo(QComboBox *cmb);
4549

4650
private:
4751
Ui::mbServerDialogFindReplace *ui;

src/server/gui/dialogs/server_dialogfindreplace.ui

Lines changed: 48 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,38 +7,52 @@
77
<x>0</x>
88
<y>0</y>
99
<width>373</width>
10-
<height>129</height>
10+
<height>181</height>
1111
</rect>
1212
</property>
1313
<property name="windowTitle">
1414
<string>Find/Replace</string>
1515
</property>
16-
<layout class="QHBoxLayout" name="horizontalLayout_2">
16+
<layout class="QHBoxLayout" name="horizontalLayout" stretch="1,0">
1717
<item>
1818
<layout class="QVBoxLayout" name="verticalLayout_3">
19-
<item>
20-
<layout class="QHBoxLayout" name="horizontalLayout" stretch="0,1">
21-
<item>
22-
<widget class="QLabel" name="label">
23-
<property name="text">
24-
<string>Find:</string>
25-
</property>
26-
</widget>
27-
</item>
28-
<item>
29-
<widget class="QComboBox" name="cmbFind">
30-
<property name="editable">
31-
<bool>true</bool>
32-
</property>
33-
</widget>
34-
</item>
35-
</layout>
36-
</item>
3719
<item>
3820
<layout class="QVBoxLayout" name="verticalLayout_2">
3921
<property name="spacing">
4022
<number>2</number>
4123
</property>
24+
<item>
25+
<layout class="QFormLayout" name="formLayout">
26+
<item row="0" column="0">
27+
<widget class="QLabel" name="label">
28+
<property name="text">
29+
<string>Find</string>
30+
</property>
31+
</widget>
32+
</item>
33+
<item row="0" column="1">
34+
<widget class="QComboBox" name="cmbFind">
35+
<property name="editable">
36+
<bool>true</bool>
37+
</property>
38+
</widget>
39+
</item>
40+
<item row="1" column="0">
41+
<widget class="QLabel" name="lblReplace">
42+
<property name="text">
43+
<string>Replace</string>
44+
</property>
45+
</widget>
46+
</item>
47+
<item row="1" column="1">
48+
<widget class="QComboBox" name="cmbReplace">
49+
<property name="editable">
50+
<bool>true</bool>
51+
</property>
52+
</widget>
53+
</item>
54+
</layout>
55+
</item>
4256
<item>
4357
<widget class="QCheckBox" name="chbMatchCase">
4458
<property name="text">
@@ -86,6 +100,20 @@
86100
</property>
87101
</widget>
88102
</item>
103+
<item>
104+
<widget class="QPushButton" name="btnReplace">
105+
<property name="text">
106+
<string>Replace</string>
107+
</property>
108+
</widget>
109+
</item>
110+
<item>
111+
<widget class="QPushButton" name="btnReplaceAll">
112+
<property name="text">
113+
<string>Replace All</string>
114+
</property>
115+
</widget>
116+
</item>
89117
<item>
90118
<widget class="QPushButton" name="btnClose">
91119
<property name="text">

src/server/gui/script/editor/server_scripteditor.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <QPainter>
77
#include <QMimeData>
88

9+
#include <server_global.h>
910
#include "server_scripthighlighter.h"
1011

1112
// https://doc.qt.io/qt-5/qtwidgets-widgets-codeeditor-example.html
@@ -137,6 +138,61 @@ int mbServerScriptEditor::lineNumberAreaWidth()
137138
return space;
138139
}
139140

141+
bool mbServerScriptEditor::findText(const QString &text, int findFlags)
142+
{
143+
QTextDocument::FindFlags tFindFlags;
144+
if (findFlags & mb::FindBackward ) tFindFlags |= QTextDocument::FindBackward ;
145+
if (findFlags & mb::FindCaseSensitively) tFindFlags |= QTextDocument::FindCaseSensitively;
146+
if (findFlags & mb::FindWholeWords ) tFindFlags |= QTextDocument::FindWholeWords ;
147+
return this->find(text, tFindFlags);
148+
}
149+
150+
bool mbServerScriptEditor::replaceText(const QString &replacement)
151+
{
152+
QTextCursor cursor = textCursor();
153+
if (!cursor.hasSelection())
154+
return false;
155+
156+
// Store selected text before replacement
157+
QString selectedText = cursor.selectedText();
158+
159+
// Replace current selection
160+
cursor.insertText(replacement);
161+
162+
// Move to the next occurrence
163+
QTextDocument *doc = document();
164+
QTextCursor next = doc->find(selectedText, cursor);
165+
166+
if (next.isNull())
167+
return true;
168+
169+
// Select the next found text
170+
setTextCursor(next);
171+
return true;
172+
}
173+
174+
bool mbServerScriptEditor::replaceTextAll(const QString &replacement)
175+
{
176+
QTextDocument *doc = document();
177+
QTextCursor cursor(doc);
178+
179+
// Get the selected text to replace
180+
QString selectedText = textCursor().selectedText();
181+
if (selectedText.isEmpty())
182+
return false;
183+
184+
// Disable undo compression to group all changes
185+
cursor.beginEditBlock();
186+
while (!cursor.isNull() && !cursor.atEnd())
187+
{
188+
cursor = doc->find(selectedText, cursor);
189+
if (!cursor.isNull())
190+
cursor.insertText(replacement);
191+
}
192+
cursor.endEditBlock();
193+
return true;
194+
}
195+
140196
void mbServerScriptEditor::updateLineNumberAreaWidth(int /* newBlockCount */)
141197
{
142198
setViewportMargins(lineNumberAreaWidth(), 0, 0, 0);

src/server/gui/script/editor/server_scripteditor.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ class mbServerScriptEditor : public QPlainTextEdit
6363
public:
6464
void lineNumberAreaPaintEvent(QPaintEvent *event);
6565
int lineNumberAreaWidth();
66+
bool findText(const QString &text, int findFlags);
67+
bool replaceText(const QString &replacement);
68+
bool replaceTextAll(const QString &replacement);
6669

6770
protected:
6871
void resizeEvent(QResizeEvent *event) override;

0 commit comments

Comments
 (0)