Skip to content

Commit 91ae698

Browse files
committed
Add LongJobHelper to the Addons::Renumber
1 parent 9401e01 commit 91ae698

3 files changed

Lines changed: 149 additions & 135 deletions

File tree

addons/src/renumber/addons-renumber.cpp

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* Copyright (C) 2006-2018 by Artur Kozioł, artkoz78@gmail.com
3-
* Copyright (C) 2023 Nick Egorrov, nicegorov@yandex.ru
3+
* Copyright (C) 2023-2025 Nick Egorrov, nicegorov@yandex.ru
44
*
55
* This file is part of GCodeWorkShop.
66
*
@@ -18,11 +18,14 @@
1818
* along with this program. If not, see <http://www.gnu.org/licenses/>.
1919
*/
2020

21-
#include <QApplication> // for QApplication
22-
#include <QDialog> // for QDialog
23-
#include <QString> // for QString
24-
#include <QWidget> // for QWidget
25-
#include <Qt> // for BusyCursor
21+
#include <functional> // for function
22+
23+
#include <QCoreApplication> // for translate
24+
#include <QDialog> // for QDialog
25+
#include <QString> // for QString
26+
#include <QWidget> // for QWidget
27+
28+
#include <ui/longjobhelper.h> // for LongJobHelper, LongJobHelper::CANCEL
2629

2730
#include "addons-renumber.h"
2831
#include "renumberdialog.h" // for RenumberDialog
@@ -46,10 +49,14 @@ bool Addons::doRenumber(QWidget* parent, QSettings* settings, QString& tx)
4649
return false;
4750
}
4851

49-
const RenumberOptions& opt = dlg->options();
50-
QApplication::setOverrideCursor(Qt::BusyCursor);
51-
Utils::renumber(opt, tx);
52-
QApplication::restoreOverrideCursor();
53-
return true;
52+
LongJobHelper helper{parent};
53+
helper.begin(tx.length(), QCoreApplication::translate("Addons::Actions", "Renumbering"), 20);
54+
55+
bool changed = Utils::renumber(tx, dlg->options(), [&helper](int pos) -> bool{
56+
return helper.check(pos) == LongJobHelper::CANCEL;
57+
});
58+
59+
helper.end();
60+
return changed;
5461
}
5562

Lines changed: 120 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2023 Nick Egorrov, nicegorov@yandex.ru
2+
* Copyright (C) 2023-2025 Nick Egorrov, nicegorov@yandex.ru
33
*
44
* This file is part of GCodeWorkShop.
55
*
@@ -28,80 +28,98 @@
2828
#include "utils-renumber.h"
2929

3030

31-
void Utils::renumber(const RenumberOptions& opt, QString& tx)
31+
bool Utils::renumber(QString& text, const RenumberOptions& opt, const std::function<bool(int)>& interrupt)
3232
{
33-
int lineCount = tx.count("\n");
33+
bool changed = false;
34+
QString result;
35+
int num = opt.startAt;
36+
int substr_start = 0;
37+
int substr_end = 0;
38+
39+
while (substr_end >= 0) {
40+
substr_end = text.indexOf('\n', substr_start);
41+
QString line = text.mid(substr_start, substr_end - substr_start + 1);
42+
substr_start = substr_end + 1;
43+
bool localChange = false;
3444

35-
switch (opt.mode) {
36-
case RenumberOptions::RenumberWithN:
37-
renumberWithN(opt, tx);
38-
break;
45+
if (interrupt(substr_start)) {
46+
return false;
47+
}
3948

40-
case RenumberOptions::RenumberAll:
41-
renumberAll(opt, tx, lineCount);
42-
break;
49+
switch (opt.mode) {
50+
case RenumberOptions::RenumberWithN:
51+
localChange = renumberWithN(line, num, opt);
52+
break;
4353

44-
case RenumberOptions::RemoveAll:
45-
removeAll(tx);
46-
break;
54+
case RenumberOptions::RenumberAll:
55+
localChange = renumberAll(line, num, opt);
56+
break;
57+
58+
case RenumberOptions::RemoveAll:
59+
localChange = renumberRemoveAll(line);
60+
break;
4761

48-
case RenumberOptions::RenumberWithoutN:
49-
renumberWithoutN(opt, tx, lineCount);
50-
break;
62+
case RenumberOptions::RenumberWithoutN:
63+
localChange = renumberWithoutN(line, num, opt);
64+
break;
65+
66+
default:
67+
;
68+
}
69+
70+
if (localChange) {
71+
changed = true;
72+
num += opt.inc;
73+
}
5174

52-
default:
53-
;
75+
result += line;
5476
}
55-
}
5677

78+
text = result;
79+
return changed;
80+
}
5781

58-
void Utils::renumberWithoutN(const RenumberOptions& opt, QString& tx, int lineCount)
82+
bool Utils::renumberWithoutN(QString& line, int num, const RenumberOptions& opt)
5983
{
60-
QString line, i_tx, new_tx;
84+
bool changed = false;
85+
QString i_tx;
6186
QRegularExpression regex;
6287

63-
int num = opt.startAt;
6488
regex.setPatternOptions(QRegularExpression::CaseInsensitiveOption);
6589
regex.setPattern("^[0-9]{1,9}\\s\\s");
6690

67-
for (int i = 0; i < lineCount; i++) {
68-
line = tx.section(QLatin1Char('\n'), i, i);
91+
i_tx = QString("%1").arg(num, opt.width);
92+
i_tx.replace(QLatin1Char(' '), QLatin1Char('0'));
93+
i_tx += " ";
6994

70-
i_tx = QString("%1").arg(num, opt.width);
71-
i_tx.replace(QLatin1Char(' '), QLatin1Char('0'));
72-
i_tx += " ";
95+
auto match = regex.match(line);
7396

74-
auto match = regex.match(line);
75-
76-
if (match.hasMatch()) {
77-
line.replace(match.capturedStart(), match.capturedLength(), i_tx);
78-
num += opt.inc;
79-
} else {
80-
if (opt.renumEmpty) {
81-
line.insert(0, i_tx);
82-
num += opt.inc;
83-
}
97+
if (match.hasMatch()) {
98+
line.replace(match.capturedStart(), match.capturedLength(), i_tx);
99+
changed = true;
100+
} else {
101+
if (opt.renumEmpty) {
102+
line.insert(0, i_tx);
103+
changed = true;
84104
}
85-
86-
new_tx += line + '\n';
87105
}
88106

89-
tx = new_tx;
107+
return changed;
90108
}
91109

92-
void Utils::renumberWithN(const RenumberOptions& opt, QString& tx)
110+
bool Utils::renumberWithN(QString& line, int num, const RenumberOptions& opt)
93111
{
112+
bool changed = false;
94113
int pos;
95-
long int i, num, it;
114+
long int i, it;
96115
QString f_tx;
97116
QRegularExpression regex;
98117
bool ok, insertSpace;
99118

100119
pos = 0;
101-
num = opt.startAt;
102120
regex.setPatternOptions(QRegularExpression::CaseInsensitiveOption);
103121
regex.setPattern("[N]{1,1}[0-9\\s]+|\\([^\\n\\r]*\\)|\'[^\\n\\r]*\'|;[^\\n\\r]*");
104-
auto match = regex.match(tx);
122+
auto match = regex.match(line);
105123

106124
while (match.hasMatch()) {
107125
pos = match.capturedStart();
@@ -110,7 +128,7 @@ void Utils::renumberWithN(const RenumberOptions& opt, QString& tx)
110128
//qDebug() << f_tx;
111129

112130
if (pos > 0)
113-
if (tx[pos - 1].isLetterOrNumber()) {
131+
if (line[pos - 1].isLetterOrNumber()) {
114132
pos = match.capturedEnd();
115133
continue;
116134
}
@@ -146,104 +164,87 @@ void Utils::renumberWithN(const RenumberOptions& opt, QString& tx)
146164
f_tx.append(QLatin1String(" "));
147165
}
148166

149-
tx.replace(pos, i, f_tx);
150-
num += opt.inc;
167+
line.replace(pos, i, f_tx);
168+
changed = true;
151169
}
152170
}
153171

154-
match = regex.match(tx, match.capturedEnd());
172+
match = regex.match(line, match.capturedEnd());
155173
}
174+
175+
return changed;
156176
}
157177

158-
void Utils::renumberAll(const RenumberOptions& opt, QString& tx, int lineCount)
178+
bool Utils::renumberAll(QString& line, int num, const RenumberOptions& opt)
159179
{
160180
int pos;
161-
long int num;
162-
QString f_tx, line, i_tx, new_tx;
181+
QString f_tx, i_tx;
163182
QRegularExpression regex;
164183

165-
num = opt.startAt;
166184
regex.setPatternOptions(QRegularExpression::CaseInsensitiveOption);
167185
regex.setPattern("[Nn]{1,1}[0-9]+[\\s]{0,}|\\([^\\n\\r]*\\)|\'[^\\n\\r]*\'|;[^\\n\\r]*");
168186

169-
for (int i = 0; i < lineCount; i++) {
170-
line = tx.section(QLatin1Char('\n'), i, i, QString::SectionIncludeTrailingSep);
171-
172-
//qDebug() << line;
173-
174-
pos = 0;
175-
176-
while (1) {
177-
if (line.isEmpty()) {
178-
if (!opt.renumEmpty) {
179-
break;
180-
}
181-
182-
f_tx = QString(QLatin1String("N%1")).arg(num, opt.width);
183-
f_tx.replace(QLatin1Char(' '), QLatin1Char('0'));
184-
num += opt.inc;
185-
line.insert(0, f_tx);
186-
break;
187-
}
188-
189-
auto match = regex.match(line, pos);
190-
191-
if (match.hasMatch() && (line.at(0) != QLatin1Char('$'))) {
192-
pos = match.capturedStart();
193-
i_tx = match.captured();
194-
i_tx.remove('\n');
187+
pos = 0;
195188

196-
if ((!i_tx.contains(QLatin1Char('(')) && !i_tx.contains(QLatin1Char('\''))
197-
&& !i_tx.contains(QLatin1Char(';')))) {
198-
f_tx = QString(QLatin1String("N%1")).arg(num, opt.width);
199-
f_tx.replace(QLatin1Char(' '), QLatin1Char('0'));
200-
num += opt.inc;
201-
f_tx.append(QLatin1String(" "));
202-
line.replace(i_tx, f_tx);
203-
break;
204-
} else if (opt.renumComm) {
205-
break;
206-
}
207-
}
189+
if (line.isEmpty()) {
190+
if (!opt.renumEmpty) {
191+
return false;
192+
}
208193

209-
if ((line.at(0) == QLatin1Char('N')) && (!line.at(1).isLetter())) {
210-
f_tx = QString(QLatin1String("N%1")).arg(num, opt.width);
211-
f_tx.replace(QLatin1Char(' '), QLatin1Char('0'));
212-
num += opt.inc;
213-
f_tx.append(QLatin1String(" "));
214-
line.replace(0, 1, f_tx);
215-
break;
216-
}
194+
f_tx = QString(QLatin1String("N%1")).arg(num, opt.width);
195+
f_tx.replace(QLatin1Char(' '), QLatin1Char('0'));
196+
line.insert(0, f_tx);
197+
return true;
198+
}
217199

218-
if (((line.at(0) != QLatin1Char('%')) && (line.at(0) != QLatin1Char(':'))
219-
&& (line.at(0) != QLatin1Char('O')) && (line.at(0) != QLatin1Char('$')))) {
220-
f_tx = QString(QLatin1String("N%1")).arg(num, opt.width);
221-
f_tx.replace(QLatin1Char(' '), QLatin1Char('0'));
222-
num += opt.inc;
223-
f_tx.append(QLatin1String(" "));
224-
line.insert(0, f_tx);
225-
break;
226-
}
200+
auto match = regex.match(line, pos);
227201

228-
break;
202+
if (match.hasMatch() && (line.at(0) != QLatin1Char('$'))) {
203+
pos = match.capturedStart();
204+
i_tx = match.captured();
205+
i_tx.remove('\n');
206+
207+
if ((!i_tx.contains(QLatin1Char('(')) && !i_tx.contains(QLatin1Char('\''))
208+
&& !i_tx.contains(QLatin1Char(';')))) {
209+
f_tx = QString(QLatin1String("N%1")).arg(num, opt.width);
210+
f_tx.replace(QLatin1Char(' '), QLatin1Char('0'));
211+
f_tx.append(QLatin1String(" "));
212+
line.replace(i_tx, f_tx);
213+
return true;
214+
} else if (opt.renumComm) {
215+
return false;
229216
}
217+
}
230218

231-
new_tx += line; // + '\n';
219+
if ((line.at(0) == QLatin1Char('N')) && (!line.at(1).isLetter())) {
220+
f_tx = QString(QLatin1String("N%1")).arg(num, opt.width);
221+
f_tx.replace(QLatin1Char(' '), QLatin1Char('0'));
222+
f_tx.append(QLatin1String(" "));
223+
line.replace(0, 1, f_tx);
224+
return true;
232225
}
233226

234-
tx = new_tx;
227+
if (((line.at(0) != QLatin1Char('%')) && (line.at(0) != QLatin1Char(':'))
228+
&& (line.at(0) != QLatin1Char('O')) && (line.at(0) != QLatin1Char('$')))) {
229+
f_tx = QString(QLatin1String("N%1")).arg(num, opt.width);
230+
f_tx.replace(QLatin1Char(' '), QLatin1Char('0'));
231+
f_tx.append(QLatin1String(" "));
232+
line.insert(0, f_tx);
233+
return true;
234+
}
235+
236+
return false;
235237
}
236238

237-
void Utils::removeAll(QString& tx)
239+
bool Utils::renumberRemoveAll(QString& line)
238240
{
239-
long int num;
241+
bool changed = false;
240242
QString f_tx;
241243
QRegularExpression regex;
242244

243-
num = 0;
244245
regex.setPatternOptions(QRegularExpression::CaseInsensitiveOption);
245246
regex.setPattern("[N]{1,1}[0-9\\s]+|\\([^\\n\\r]*\\)|\'[^\\n\\r]*\'|;[^\\n\\r]*");
246-
auto match = regex.match(tx);
247+
auto match = regex.match(line);
247248

248249
while (match.hasMatch()) {
249250
int pos = match.capturedStart();
@@ -253,12 +254,14 @@ void Utils::removeAll(QString& tx)
253254

254255
if (!f_tx.contains(QLatin1Char('(')) && !f_tx.contains(QLatin1Char('\''))
255256
&& !f_tx.contains(QLatin1Char(';'))) {
256-
tx.remove(pos, match.capturedLength());
257-
num++;
257+
line.remove(pos, match.capturedLength());
258+
changed = true;
258259
} else {
259260
pos = match.capturedEnd();
260261
}
261262

262-
match = regex.match(tx, pos);
263+
match = regex.match(line, pos);
263264
}
265+
266+
return changed;
264267
}

0 commit comments

Comments
 (0)