Skip to content

Commit bf14e2f

Browse files
committed
Separate new file for program/heighmap, separate save changes, isModified
1 parent 745432d commit bf14e2f

7 files changed

Lines changed: 89 additions & 47 deletions

File tree

src/gpilot/core/gcode/gcode.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,11 @@ bool GCode::isModified() const
153153
return m_contentHash != calculateHash();
154154
}
155155

156+
void GCode::markAsNotModified()
157+
{
158+
m_contentHash = calculateHash();
159+
}
160+
156161
void GCode::setCommandSent()
157162
{
158163
GCodeItem& item = m_data[m_commandIndex];

src/gpilot/core/gcode/gcode.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ class GCode : public QObject
206206
void updateHash();
207207
QString calculateHash() const;
208208
bool isModified() const;
209+
void markAsNotModified();
209210

210211
signals:
211212
void progressChanged(int progress);

src/gpilot/core/heightmap/heightmap.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ void Heightmap::assign(QSize size, QPointF startPos, QSizeF stepSize, Interpolat
5353
updateEndPos();
5454
updateMinMax();
5555
endUpdate();
56+
markAsNotModified();
5657
}
5758

5859
void Heightmap::setInterpolationMode(InterpolationMode mode)
@@ -89,13 +90,19 @@ bool Heightmap::isInside(QPointF ptMm) const
8990

9091
void Heightmap::notifyChanged()
9192
{
93+
m_modified = true;
9294
if (m_updateDepth > 0) {
9395
m_pendingChange = true;
9496
return;
9597
}
9698
emit changed();
9799
}
98100

101+
void Heightmap::markAsNotModified()
102+
{
103+
m_modified = false;
104+
}
105+
99106
void Heightmap::beginUpdate()
100107
{
101108
m_updateDepth++;

src/gpilot/core/heightmap/heightmap.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,12 @@ class Heightmap : public QObject
8686

8787
void minMax(double value);
8888

89-
// Suppress changed() signal until endUpdate() is called. Nested calls are counted.
9089
void beginUpdate();
91-
// Resume signal emission; emits changed() once if anything changed inside the block.
9290
void endUpdate();
9391

92+
bool isModified() const { return m_modified; }
93+
void markAsNotModified();
94+
9495
signals:
9596
void changed();
9697

@@ -110,6 +111,7 @@ class Heightmap : public QObject
110111
QList<double> m_data;
111112
int m_updateDepth = 0;
112113
bool m_pendingChange = false;
114+
bool m_modified = false;
113115
void notifyChanged();
114116
void setSize(QSize size);
115117
void updateEndPos();

src/gpilot/ui/forms/frmmain.cpp

Lines changed: 67 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ void FrmMain::initializeHeightmapPanel()
517517
connect(ui->heightmap, &PartMainHeightmap::extremesRequired, this, [this]() {
518518
ui->heightmap->setHeightmapAreaRect(ui->visualizer->getCodeDrawerBounds());
519519
});
520-
connect(ui->heightmap, &PartMainHeightmap::newHeightmapRequested, this, &FrmMain::fileNew);
520+
connect(ui->heightmap, &PartMainHeightmap::newHeightmapRequested, this, &FrmMain::newHeightmap);
521521
connect(ui->heightmap, &PartMainHeightmap::loadHeightmapRequested, this, &FrmMain::onLoadHeightmapRequested);
522522
connect(ui->heightmap, &PartMainHeightmap::useHeightmapToggled, this, &FrmMain::useHeightmapToggled);
523523
connect(ui->heightmap, &PartMainHeightmap::heightmapModeToggled, this, &FrmMain::heightmapModeToggled);
@@ -722,7 +722,7 @@ void FrmMain::initializeEventFilter()
722722

723723
void FrmMain::initializeMainMenu()
724724
{
725-
connect(ui->actFileNew, &QAction::triggered, this, &FrmMain::fileNew);
725+
connect(ui->actFileNew, &QAction::triggered, this, &FrmMain::newProgram);
726726
connect(ui->actFileOpen, &QAction::triggered, this, &FrmMain::fileOpen);
727727
connect(ui->actFileSave, &QAction::triggered, this, &FrmMain::fileSave);
728728
connect(ui->actFileSaveAs, &QAction::triggered, this, &FrmMain::fileSaveAs);
@@ -796,7 +796,7 @@ void FrmMain::closeEvent(QCloseEvent *ce)
796796
bool mode = m_heightmapMode;
797797
m_heightmapMode = false;
798798

799-
if (!saveChanges(false) || !saveChanges(true)) {
799+
if (!saveProgramChanges() || !saveHeightmapChanges()) {
800800
ce->ignore();
801801
m_heightmapMode = mode;
802802
return;
@@ -889,7 +889,9 @@ void FrmMain::dropEvent(QDropEvent *de)
889889
QString fileName = de->mimeData()->urls().at(0).toLocalFile();
890890

891891
if (!m_heightmapMode) {
892-
if (!saveChanges(false)) return;
892+
if (!saveProgramChanges()) {
893+
return;
894+
}
893895

894896
// Load dropped g-code file
895897
if (!fileName.isEmpty()) {
@@ -903,7 +905,9 @@ void FrmMain::dropEvent(QDropEvent *de)
903905
//loadFile(de->mimeData()->text().split("\n"));
904906
}
905907
} else {
906-
if (!saveChanges(true)) return;
908+
if (!saveHeightmapChanges()) {
909+
return;
910+
}
907911

908912
// Load dropped heightmap file
909913
Core::instance().addRecentHeightmap(fileName);
@@ -940,17 +944,6 @@ QMenu *FrmMain::createPopupMenu()
940944
return menu;
941945
}
942946

943-
void FrmMain::fileNew()
944-
{
945-
if (!saveChanges(m_heightmapMode)) return;
946-
947-
if (!m_heightmapMode) {
948-
newFile();
949-
} else {
950-
newHeightmap();
951-
}
952-
}
953-
954947
void FrmMain::fileOpen()
955948
{
956949
onFileOpen();
@@ -1184,7 +1177,9 @@ void FrmMain::viewCentralVisualizerToggled(bool checked)
11841177
void FrmMain::onFileOpen(QString filePath)
11851178
{
11861179
if (!m_heightmapMode) {
1187-
if (!saveChanges(false)) return;
1180+
if (!saveProgramChanges()) {
1181+
return;
1182+
}
11881183

11891184
if (filePath.isEmpty()) {
11901185
filePath = QFileDialog::getOpenFileName(this, tr("Open Heightmap"), lastUsedDirectory(),
@@ -1200,7 +1195,9 @@ void FrmMain::onFileOpen(QString filePath)
12001195

12011196
loadFile(filePath);
12021197
} else {
1203-
if (!saveChanges(true)) return;
1198+
if (!saveHeightmapChanges()) {
1199+
return;
1200+
}
12041201

12051202
if (filePath.isEmpty()) {
12061203
QString filePath = QFileDialog::getOpenFileName(this, tr("Open G-Code"), lastUsedDirectory(), tr("Heightmap files (*.map)"));
@@ -1768,7 +1765,7 @@ void FrmMain::heightmapModeToggled(bool checked)
17681765

17691766
void FrmMain::onLoadHeightmapRequested()
17701767
{
1771-
if (!saveChanges(true)) {
1768+
if (!saveHeightmapChanges()) {
17721769
return;
17731770
}
17741771

@@ -2219,10 +2216,15 @@ void FrmMain::onActRecentFileTriggered()
22192216
QString filePath = action->text();
22202217

22212218
if (action != NULL) {
2222-
if (!saveChanges(m_heightmapMode)) return;
22232219
if (!m_heightmapMode) {
2220+
if (!saveProgramChanges()) {
2221+
return;
2222+
}
22242223
loadFile(filePath);
22252224
} else {
2225+
if (!saveHeightmapChanges()) {
2226+
return;
2227+
}
22262228
HeightmapLoader loader;
22272229
loader.loadFromFile(filePath, heightmap());
22282230
}
@@ -3029,33 +3031,50 @@ void FrmMain::testConverter(int converterIndex)
30293031
updateParser();
30303032
}
30313033

3032-
bool FrmMain::saveChanges(bool heightMapMode)
3034+
bool FrmMain::saveProgramChanges()
30333035
{
30343036
FilesManager& fm = FilesManager::instance();
3037+
if (!fm.gcodeModified()) {
3038+
return true;
3039+
}
30353040

3036-
if (!heightMapMode && fm.gcodeModified()) {
3037-
int res = QMessageBox::warning(this, this->windowTitle(), tr("G-code program file was changed. Save?"),
3038-
QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel);
3039-
if (res == QMessageBox::Cancel) return false;
3040-
else if (res == QMessageBox::Yes) fileSave();
3041-
3042-
fm.setGcodeModified(false);
3041+
int res = QMessageBox::warning(this, this->windowTitle(),
3042+
tr("G-code program file was changed. Save?"),
3043+
QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel);
3044+
if (res == QMessageBox::Cancel) {
3045+
return false;
3046+
}
3047+
if (res == QMessageBox::Yes) {
3048+
fileSave();
30433049
}
30443050

3045-
if (heightMapMode && fm.heightmapModified()) {
3046-
int res = QMessageBox::warning(this, this->windowTitle(), tr("Heightmap file was changed. Save?"),
3047-
QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel);
3048-
if (res == QMessageBox::Cancel) return false;
3049-
else if (res == QMessageBox::Yes) {
3050-
m_heightmapMode = true;
3051-
fileSave();
3052-
m_heightmapMode = heightMapMode;
3053-
updateRecentFilesMenus(); // Restore g-code files recent menu
3054-
}
3051+
fm.setGcodeModified(false);
3052+
return true;
3053+
}
30553054

3056-
fm.setHeightmapModified(false);
3055+
bool FrmMain::saveHeightmapChanges()
3056+
{
3057+
FilesManager& fm = FilesManager::instance();
3058+
if (!fm.heightmapModified()) {
3059+
return true;
30573060
}
30583061

3062+
int res = QMessageBox::warning(this, this->windowTitle(),
3063+
tr("Heightmap file was changed. Save?"),
3064+
QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel);
3065+
if (res == QMessageBox::Cancel) {
3066+
return false;
3067+
}
3068+
if (res == QMessageBox::Yes) {
3069+
// fileSave() reads m_heightmapMode to pick the exporter; force it for the duration of the save.
3070+
bool prevMode = m_heightmapMode;
3071+
m_heightmapMode = true;
3072+
fileSave();
3073+
m_heightmapMode = prevMode;
3074+
updateRecentFilesMenus();
3075+
}
3076+
3077+
fm.setHeightmapModified(false);
30593078
return true;
30603079
}
30613080

@@ -3080,8 +3099,12 @@ void FrmMain::resetHeightmap()
30803099
// 4. Zresetować estymację czasu
30813100
// ...
30823101
// 7.
3083-
void FrmMain::newFile()
3102+
void FrmMain::newProgram()
30843103
{
3104+
if (!saveProgramChanges()) {
3105+
return;
3106+
}
3107+
30853108
ui->program->close();
30863109
ui->visualizer->close();
30873110

@@ -3112,6 +3135,10 @@ void FrmMain::newFile()
31123135

31133136
void FrmMain::newHeightmap()
31143137
{
3138+
if (!saveHeightmapChanges()) {
3139+
return;
3140+
}
3141+
31153142
ui->program->clearHeightmapModel();
31163143
onFileReset();
31173144
ui->heightmap->setOpenFile(tr("Untitled"));

src/gpilot/ui/forms/frmmain.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ class FrmMain : public QMainWindow
105105
void settingsSetToDefault();
106106

107107
private slots:
108-
void fileNew();
108+
void newProgram();
109+
void newHeightmap();
109110
void fileOpen();
110111
void fileSave();
111112
void fileSaveAs();
@@ -277,12 +278,11 @@ private slots:
277278
void updateProgramTitle();
278279
bool programIsCentralWidget() const;
279280
QPointer<GCode> m_activeProgram;
280-
bool saveChanges(bool heightmapMode);
281+
bool saveProgramChanges();
282+
bool saveHeightmapChanges();
281283
void testConverter(int converterIndex = 0);
282284
// void clearTable();
283285
void resetHeightmap();
284-
void newFile();
285-
void newHeightmap();
286286

287287
// Ui
288288
UiState currentUiState() const;

src/gpilot/ui/forms/frmsettings.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#include <QListWidgetItem>
1111
#include <QSettings>
1212
#include <QGroupBox>
13-
#include <QVector3D>
13+
#include <QVector3D
1414
#include "core/config/configuration.h"
1515
#include "utils/validators.h"
1616
#include "colorpicker.h"

0 commit comments

Comments
 (0)