Skip to content

Commit 78db7e7

Browse files
committed
Addons: Create the CleanUp addon
1 parent ea9c845 commit 78db7e7

12 files changed

Lines changed: 209 additions & 75 deletions

addons/addons.pro

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ SOURCES += \
2222
include(src/bhc/bhc.pri)
2323
include(src/blockskip/blockskip.pri)
2424
include(src/chamfer/chamfer.pri)
25+
include(src/cleanup/cleanup.pri)

addons/include/addons-actions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,23 @@ public slots:
4545
QAction *blockSkipIncrement() {return m_blockSkipIncrement;}
4646
QAction *blockSkipRemove() {return m_blockSkipRemove;}
4747
QAction *chamfer() {return m_chamfer;}
48+
QAction *cleanUp() {return m_cleanUp;}
4849

4950
protected:
5051
QAction *m_bhc;
5152
QAction *m_blockSkipDecrement;
5253
QAction *m_blockSkipIncrement;
5354
QAction *m_blockSkipRemove;
5455
QAction *m_chamfer;
56+
QAction *m_cleanUp;
5557

5658
protected slots:
5759
void doBhc();
5860
void doBlockSkipDecrement();
5961
void doBlockSkipIncrement();
6062
void doBlockSkipRemove();
6163
void doChamfer();
64+
void doCleanUp();
6265

6366
protected:
6467
void doBlockSkip(bool remove, bool inc);

addons/src/addons-actions.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,23 @@
2929
#include "bhc/addons-bhc.h"
3030
#include "blockskip/utils-blockskip.h"
3131
#include "chamfer/addons-chamfer.h"
32+
#include "cleanup/addons-cleanup.h"
3233

3334

3435
Addons::Actions::Actions(QObject *parent) : QObject(parent),
3536
m_bhc(new QAction(this)),
3637
m_blockSkipDecrement(new QAction(this)),
3738
m_blockSkipIncrement(new QAction(this)),
3839
m_blockSkipRemove(new QAction(this)),
39-
m_chamfer(new QAction(this))
40+
m_chamfer(new QAction(this)),
41+
m_cleanUp(new QAction(this))
4042
{
4143
connect(m_bhc, SIGNAL(triggered()), this, SLOT(doBhc()));
4244
connect(m_blockSkipDecrement, SIGNAL(triggered()), this, SLOT(doBlockSkipDecrement()));
4345
connect(m_blockSkipIncrement, SIGNAL(triggered()), this, SLOT(doBlockSkipIncrement()));
4446
connect(m_blockSkipRemove, SIGNAL(triggered()), this, SLOT(doBlockSkipRemove()));
4547
connect(m_chamfer, SIGNAL(triggered()), this, SLOT(doChamfer()));
48+
connect(m_cleanUp, SIGNAL(triggered()), this, SLOT(doCleanUp()));
4649

4750
loadIcons();
4851
loadTranslations();
@@ -60,6 +63,8 @@ void Addons::Actions::loadTranslations()
6063
m_blockSkipRemove->setToolTip(tr("Remove Block Skip /"));
6164
m_chamfer->setText(tr("Chamfer"));
6265
m_chamfer->setToolTip(tr("Calculate chamfer"));
66+
m_cleanUp->setText(tr("Clean &up"));
67+
m_cleanUp->setToolTip(tr("Remove text using regular expressions"));
6368
}
6469

6570
void Addons::Actions::loadIcons()
@@ -69,6 +74,7 @@ void Addons::Actions::loadIcons()
6974
m_blockSkipIncrement->setIcon(QIcon(":/images/blockskip+.png"));
7075
m_blockSkipRemove->setIcon(QIcon(":/images/blockskipr.png"));
7176
m_chamfer->setIcon(QIcon(":/images/chamfer.png"));
77+
m_cleanUp->setIcon(QIcon(":/images/cleanup.png"));
7278
}
7379

7480
void Addons::Actions::doBhc()
@@ -109,3 +115,16 @@ void Addons::Actions::doChamfer()
109115
{
110116
Addons::doChamfer(EdytorNc::instance(), Medium::instance().settings());
111117
}
118+
119+
void Addons::Actions::doCleanUp()
120+
{
121+
Addons::Context ctx;
122+
123+
if (!ctx.pull(Addons::Context::ALL)) {
124+
return;
125+
}
126+
127+
if (Addons::doCleanUp(EdytorNc::instance(), Medium::instance().settings(), ctx.text())) {
128+
ctx.push();
129+
}
130+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright (C) 2006-2018 by Artur Kozioł, artkoz78@gmail.com
3+
* Copyright (C) 2023 Nick Egorrov, nicegorov@yandex.ru
4+
*
5+
* This file is part of EdytorNC.
6+
*
7+
* EdytorNC is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation, either version 2 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
*/
20+
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
26+
27+
#include "addons-cleanup.h"
28+
#include "cleanupdialog.h" // for CleanUpDialog
29+
#include "cleanupoptions.h" // for CleanUpOptions
30+
#include "utils-removebyregex.h" // for removeTextByRegExp
31+
32+
33+
bool Addons::doCleanUp(QWidget *parent, QSettings *settings, QString &tx)
34+
{
35+
bool result = false;
36+
QString key = "CleanUpDialog";
37+
CleanUpDialog *dlg;
38+
dlg = parent->findChild<CleanUpDialog *>(key);
39+
40+
if (!dlg) {
41+
dlg = new CleanUpDialog(parent, settings);
42+
dlg->setObjectName(key);
43+
CleanUpOptions opt;
44+
opt.expressions
45+
<< "('\\()[\\w,.;:/*+\\\\! $%^&-]{0,}(\\))$"
46+
<< "(\\()[\\w,.;:/*+\\\\! $%^&-]{0,}(\\))"
47+
<< "[\n]{2,}";
48+
opt.comments
49+
<< QObject::tr("Lines with: '(comment)")
50+
<< QObject::tr("Any: (comment)")
51+
<< QObject::tr("Empty lines:");
52+
dlg->loadSettings(opt);
53+
}
54+
55+
dlg->setText(tx);
56+
57+
if (dlg->exec() == QDialog::Accepted) {
58+
result = Utils::removeTextByRegExp(tx, dlg->options().selected);
59+
}
60+
61+
dlg->deleteLater();
62+
return result;
63+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright (C) 2023 Nick Egorrov, nicegorov@yandex.ru
3+
*
4+
* This file is part of EdytorNC.
5+
*
6+
* EdytorNC is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 2 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
20+
#ifndef ADDONS_CLEANUP_H
21+
#define ADDONS_CLEANUP_H
22+
23+
class QSettings;
24+
class QString;
25+
class QWidget;
26+
27+
28+
namespace Addons {
29+
bool doCleanUp(QWidget *parent, QSettings *settings, QString &tx);
30+
}
31+
32+
#endif // ADDONS_CLEANUP_H

addons/src/cleanup/cleanup.pri

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
HEADERS += $$PWD/addons-cleanup.h
2+
SOURCES += $$PWD/addons-cleanup.cpp
3+
4+
HEADERS += $$PWD/cleanupoptions.h
5+
SOURCES += $$PWD/cleanupoptions.cpp
6+
7+
HEADERS += $$PWD/cleanupdialog.h
8+
SOURCES += $$PWD/cleanupdialog.cpp
9+
FORMS += $$PWD/cleanupdialog.ui
10+
11+
HEADERS += $$PWD/utils-removebyregex.h
12+
SOURCES += $$PWD/utils-removebyregex.cpp
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright (C) 2023 Nick Egorrov, nicegorov@yandex.ru
3+
*
4+
* This file is part of EdytorNC.
5+
*
6+
* EdytorNC is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 2 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
20+
#include <QRegularExpression> // for QRegularExpression
21+
#include <QString> // for QString
22+
#include <QStringList> // for QStringList
23+
24+
#include "utils-removebyregex.h"
25+
26+
27+
bool Utils::removeTextByRegExp(QString &tx, QStringList expList, bool replaceDollar)
28+
{
29+
if (expList.isEmpty()) {
30+
return false;
31+
}
32+
33+
for (QString regexp : expList) {
34+
if (replaceDollar && regexp.contains('$') && !regexp.contains("\\$")) {
35+
regexp.replace('$', "\\n");
36+
}
37+
38+
tx.remove(QRegularExpression(regexp));
39+
}
40+
41+
return true;
42+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright (C) 2023 Nick Egorrov, nicegorov@yandex.ru
3+
*
4+
* This file is part of EdytorNC.
5+
*
6+
* EdytorNC is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 2 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
20+
#ifndef UTILS_REMOVEBYREGEX_H
21+
#define UTILS_REMOVEBYREGEX_H
22+
23+
#include <QStringList>
24+
25+
class QString;
26+
27+
28+
namespace Utils {
29+
bool removeTextByRegExp(QString &tx, QStringList expList, bool replaceDollar = true);
30+
}
31+
32+
#endif // UTILSREMOVEBY_REGEX_H

src/edytornc.cpp

Lines changed: 4 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,6 @@ EdytorNc::EdytorNc(Medium *medium)
112112
openExampleAct = nullptr;
113113
commApp = nullptr;
114114

115-
selectedExpressions.clear();
116-
117115
clipboard = QApplication::clipboard();
118116
connect(clipboard, SIGNAL(dataChanged()), this, SLOT(clipboardChanged()));
119117

@@ -1430,7 +1428,7 @@ void EdytorNc::updateMenus()
14301428
splittAct->setEnabled(hasMdiChildNotReadOnly);
14311429
convertProgAct->setEnabled(hasMdiChildNotReadOnly);
14321430
cmpMacroAct->setEnabled(hasMdiChildNotReadOnly);
1433-
cleanUpDialogAct->setEnabled(hasMdiChildNotReadOnly);
1431+
m_addonsActions->cleanUp()->setEnabled(hasMdiChildNotReadOnly);
14341432
swapAxesAct->setEnabled(hasMdiChildNotReadOnly);
14351433
semiCommAct->setEnabled(hasMdiChildNotReadOnly && hasSelection);
14361434
paraCommAct->setEnabled(hasMdiChildNotReadOnly && hasSelection);
@@ -1732,6 +1730,7 @@ void EdytorNc::createActions()
17321730
m_addonsActions->blockSkipIncrement()->setShortcut(tr("Ctrl+2"));
17331731
m_addonsActions->blockSkipDecrement()->setShortcut(tr("Ctrl+3"));
17341732
//m_addonsActions->chamfer()->setShortcut(tr("F9"));
1733+
//m_addonsActions->cleanUp()->setShortcut(QKeySequence::Print);
17351734

17361735
insertSpcAct = new QAction(QIcon(":/images/insertspc.png"), tr("&Insert spaces"), this);
17371736
insertSpcAct->setShortcut(tr("F4"));
@@ -1755,11 +1754,6 @@ void EdytorNc::createActions()
17551754
insertEmptyLinesAct->setToolTip(tr("Insert empty lines"));
17561755
connect(insertEmptyLinesAct, SIGNAL(triggered()), this, SLOT(doInsertEmptyLines()));
17571756

1758-
cleanUpDialogAct = new QAction(QIcon(":/images/cleanup.png"), tr("Clean &up"), this);
1759-
//cleanUpDialogAct->setShortcut(QKeySequence::Print);
1760-
cleanUpDialogAct->setToolTip(tr("Remove text using regular expressions"));
1761-
connect(cleanUpDialogAct, SIGNAL(triggered()), this, SLOT(displayCleanUpDialog()));
1762-
17631757
insertDotAct = new QAction(QIcon(":/images/dots.png"), tr("Insert dots"), this);
17641758
insertDotAct->setShortcut(tr("F6"));
17651759
insertDotAct->setToolTip(tr("Inserts decimal dot"));
@@ -1971,7 +1965,7 @@ void EdytorNc::createMenus()
19711965
toolsMenu->addAction(insertDotAct);
19721966
toolsMenu->addAction(insertEmptyLinesAct);
19731967
toolsMenu->addAction(removeEmptyLinesAct);
1974-
toolsMenu->addAction(cleanUpDialogAct);
1968+
toolsMenu->addAction(m_addonsActions->cleanUp());
19751969
toolsMenu->addAction(swapAxesAct);
19761970
toolsMenu->addAction(splittAct);
19771971
toolsMenu->addAction(renumberAct);
@@ -2051,7 +2045,7 @@ void EdytorNc::createToolBars()
20512045
toolsToolBar->addSeparator();
20522046
toolsToolBar->addAction(insertSpcAct);
20532047
toolsToolBar->addAction(removeSpcAct);
2054-
toolsToolBar->addAction(cleanUpDialogAct);
2048+
toolsToolBar->addAction(m_addonsActions->cleanUp());
20552049
toolsToolBar->addAction(insertDotAct);
20562050
toolsToolBar->addAction(swapAxesAct);
20572051
toolsToolBar->addAction(renumberAct);
@@ -2307,13 +2301,6 @@ void EdytorNc::readSettings()
23072301
ui->currentPathCheckBox->setChecked(settings.value("FileBrowserShowCurrentFileDir",
23082302
false).toBool());
23092303
ui->filePreviewSpinBox->setValue(settings.value("FilePreviewNo", 10).toInt());
2310-
2311-
settings.beginGroup("CleanUpDialog");
2312-
2313-
selectedExpressions = settings.value("SelectedExpressions",
2314-
(QStringList() << "")).toStringList();
2315-
2316-
settings.endGroup();
23172304
}
23182305

23192306
void EdytorNc::writeSettings()
@@ -2422,10 +2409,6 @@ void EdytorNc::writeSettings()
24222409
settings.setValue("CurrentSession", currentSession);
24232410
settings.endGroup();
24242411

2425-
settings.beginGroup("CleanUpDialog");
2426-
settings.setValue("SelectedExpressions", selectedExpressions);
2427-
settings.endGroup();
2428-
24292412
if (!defaultMdiWindowProperites.startEmpty) {
24302413
saveSession(currentSession);
24312414
}
@@ -3846,24 +3829,6 @@ void EdytorNc::doParaComment()
38463829
}
38473830
}
38483831

3849-
void EdytorNc::displayCleanUpDialog()
3850-
{
3851-
MdiChild *editorWindow = activeMdiChild();
3852-
3853-
if (editorWindow) {
3854-
cleanUpDialog *dialog = new cleanUpDialog(this);
3855-
3856-
int result = dialog->exec(selectedExpressions, editorWindow->textEdit()->toPlainText());
3857-
3858-
if (result == QDialog::Accepted) {
3859-
selectedExpressions = dialog->getSelectedExpressions();
3860-
editorWindow->doRemoveTextByRegExp(selectedExpressions);
3861-
}
3862-
3863-
delete (dialog);
3864-
}
3865-
}
3866-
38673832
void EdytorNc::doSwapAxes()
38683833
{
38693834
QString first, second;

0 commit comments

Comments
 (0)