Skip to content

Commit 42e80f2

Browse files
committed
Add LongJobHelper to the Addons::CleanUp
1 parent 07a0cdf commit 42e80f2

3 files changed

Lines changed: 71 additions & 19 deletions

File tree

addons/src/cleanup/addons-cleanup.cpp

Lines changed: 21 additions & 8 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,16 @@
1818
* along with this program. If not, see <http://www.gnu.org/licenses/>.
1919
*/
2020

21-
#include <QDialog> // for QDialog, QDialog::Accepted
22-
#include <QObject> // for QObject
23-
#include <QString> // for QString
24-
#include <QStringList> // for QStringList
25-
#include <QWidget> // for QWidget
21+
#include <functional> // for function
22+
23+
#include <QCoreApplication> // for QCoreApplication
24+
#include <QDialog> // for QDialog, QDialog::Accepted
25+
#include <QObject> // for QObject
26+
#include <QString> // for QString
27+
#include <QStringList> // for QStringList
28+
#include <QWidget> // for QWidget
29+
30+
#include <ui/longjobhelper.h> // for LongJobHelper, LongJobHelper::CANCEL
2631

2732
#include "addons-cleanup.h"
2833
#include "cleanupdialog.h" // for CleanUpDialog
@@ -54,10 +59,18 @@ bool Addons::doCleanUp(QWidget* parent, QSettings* settings, QString& tx)
5459

5560
dlg->setText(tx);
5661

57-
if (dlg->exec() == QDialog::Accepted) {
58-
result = Utils::removeTextByRegExp(tx, dlg->options().selected);
62+
if (dlg->exec() != QDialog::Accepted) {
63+
return false;
5964
}
6065

66+
LongJobHelper helper{parent};
67+
helper.begin(tx.length(), QCoreApplication::translate("Addons::Actions", "Cleaning up"), 20);
68+
69+
result = Utils::removeTextByRegExp(tx, dlg->options().selected, true, [&helper](int pos) -> bool{
70+
return helper.check(pos) == LongJobHelper::CANCEL;
71+
});
72+
73+
helper.end();
6174
dlg->deleteLater();
6275
return result;
6376
}

addons/src/cleanup/utils-removebyregex.cpp

Lines changed: 43 additions & 9 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-2023 Nick Egorrov, nicegorov@yandex.ru
33
*
44
* This file is part of GCodeWorkShop.
55
*
@@ -17,26 +17,60 @@
1717
* along with this program. If not, see <http://www.gnu.org/licenses/>.
1818
*/
1919

20-
#include <QRegularExpression> // for QRegularExpression
21-
#include <QString> // for QString
22-
#include <QStringList> // for QStringList
20+
#include <QRegularExpression> // for QRegularExpression
21+
#include <QRegularExpressionMatch> // for QRegularExpressionMatch
22+
#include <QRegularExpressionMatchIterator> // for QRegularExpressionMatchIterator
23+
#include <QString> // for QString
24+
#include <QStringList> // for QStringList
2325

2426
#include "utils-removebyregex.h"
2527

2628

27-
bool Utils::removeTextByRegExp(QString& tx, QStringList expList, bool replaceDollar)
29+
bool Utils::removeTextByRegExp(QString& tx,
30+
const QStringList& expList,
31+
bool replaceDollar,
32+
const std::function<bool (int)>& interrupt)
2833
{
2934
if (expList.isEmpty()) {
3035
return false;
3136
}
3237

33-
for (QString regexp : expList) {
34-
if (replaceDollar && regexp.contains('$') && !regexp.contains("\\$")) {
35-
regexp.replace('$', "\\n");
38+
QString complex;
39+
40+
// Glue multiple regular expressions into one.
41+
for (QString exp : expList) {
42+
if (replaceDollar && exp.contains('$') && !exp.contains("\\$")) {
43+
exp.replace('$', "\\n");
44+
}
45+
46+
if (!complex.isEmpty()) {
47+
complex.append("|");
48+
}
49+
50+
complex.append(exp);
51+
}
52+
53+
QRegularExpression regexpr{
54+
complex,
55+
QRegularExpression::CaseInsensitiveOption
56+
};
57+
QRegularExpressionMatchIterator iterator = regexpr.globalMatch(tx);
58+
59+
int pos = 0;
60+
QString result;
61+
62+
while (iterator.hasNext()) {
63+
if (interrupt(pos)) {
64+
return false;
3665
}
3766

38-
tx.remove(QRegularExpression(regexp));
67+
const QRegularExpressionMatch& match = iterator.next();
68+
const QString& sub = tx.mid(pos, match.capturedStart() - pos);
69+
result.append(sub);
70+
pos = match.capturedEnd();
3971
}
4072

73+
result.append(tx.mid(pos));
74+
tx = result;
4175
return true;
4276
}

addons/src/cleanup/utils-removebyregex.h

Lines changed: 7 additions & 2 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
*
@@ -20,13 +20,18 @@
2020
#ifndef UTILS_REMOVEBYREGEX_H
2121
#define UTILS_REMOVEBYREGEX_H
2222

23+
#include <functional> // for function
24+
2325
#include <QStringList> // for QStringList
2426

2527
class QString;
2628

2729

2830
namespace Utils {
29-
bool removeTextByRegExp(QString& tx, QStringList expList, bool replaceDollar = true);
31+
bool removeTextByRegExp(QString& tx,
32+
const QStringList& expList,
33+
bool replaceDollar,
34+
const std::function<bool(int)>& interrupt);
3035
}
3136

3237
#endif // UTILSREMOVEBY_REGEX_H

0 commit comments

Comments
 (0)