Skip to content

Commit fbe57ee

Browse files
author
etet100
committed
Ui state refactor
1 parent 74f2278 commit fbe57ee

14 files changed

Lines changed: 223 additions & 151 deletions

src/gpilot/gpilot.pro

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,7 @@ HEADERS += ui/forms/frmmain.h \
472472
ui/utils/shortcutsmanager.h \
473473
ui/utils/statecolors.h \
474474
ui/utils/uipermissions.h \
475+
ui/utils/uistate.h \
475476
ui/utils/syntaxhighlighter.h \
476477
ui/utils/thememanager.h \
477478
ui/utils/windowstaskbar.h \

src/gpilot/ui/forms/frmmain.cpp

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ FrmMain::FrmMain(QWidget *parent) :
108108
ui->fraDropUser->setVisible(false);
109109

110110
connectWindowTitleUpdater();
111+
connectFilesManagerToUpdateControls();
111112

112113
// Initialize OpenAI key
113114
if (ConfigurationAI::instance().openAIKey() != "") {
@@ -532,6 +533,17 @@ void FrmMain::initializeLogMenu()
532533
});
533534
}
534535

536+
void FrmMain::connectFilesManagerToUpdateControls()
537+
{
538+
FilesManager& fm = FilesManager::instance();
539+
connect(&fm, &FilesManager::gcodeFileStateChanged, this, [this](bool, const QString&, bool) {
540+
updateControlsState(currentUiState());
541+
});
542+
connect(&fm, &FilesManager::heightmapFileStateChanged, this, [this](bool, const QString&, bool) {
543+
updateControlsState(currentUiState());
544+
});
545+
}
546+
535547
void FrmMain::connectWindowTitleUpdater()
536548
{
537549
FilesManager& fm = FilesManager::instance();
@@ -1826,9 +1838,10 @@ void FrmMain::onMachineStateChanged(MachineState state)
18261838
{
18271839
ui->state->setState(state);
18281840

1829-
ui->control->updateControlsState(communicator()->stateBehavior());
1841+
ui->control->updateControlsState(currentUiState());
18301842

18311843
// ui->spindle->...
1844+
18321845
// ui->cmdSpindle->setEnabled(state == DeviceHold0 || ((communicator()->senderState() != SenderTransferring) && (communicator()->senderState() != SenderStopping)));
18331846
}
18341847

@@ -1950,7 +1963,8 @@ void FrmMain::updateOnStateBehaviorChanged(AbstractStateBehavior *sb)
19501963
ThemeManager::instance().dark() ? "black" : "white"
19511964
);
19521965
ui->console->appendSystem(QString("State: %1").arg(sb->description()));
1953-
updateControlsState();
1966+
FilesManager& fm = FilesManager::instance();
1967+
updateControlsState({ sb, { fm.gcodeOpened(), fm.heightmapOpened() } });
19541968

19551969
// Auto-dismiss the prompt dialog when the user-prompt state goes away
19561970
// (e.g. user resolved the prompt via a side-channel like the toolbar
@@ -3014,10 +3028,23 @@ void FrmMain::newHeightmap()
30143028
updateControlsState();
30153029
}
30163030

3017-
void FrmMain::updateControlsState()
3031+
UiState FrmMain::currentUiState() const
3032+
{
3033+
FilesManager& fm = FilesManager::instance();
3034+
3035+
return {
3036+
Core::instance().communicator()->stateBehavior(),
3037+
{
3038+
fm.gcodeOpened(),
3039+
fm.heightmapOpened()
3040+
},
3041+
};
3042+
}
3043+
3044+
void FrmMain::updateControlsState(const UiState& state)
30183045
{
30193046
bool portOpened = connection() && connection()->isConnected();
3020-
AbstractStateBehavior *sb = communicator()->stateBehavior();
3047+
AbstractStateBehavior *sb = state.sb;
30213048
bool running = sb->is(AbstractStateBehavior::Type::Running);
30223049
bool paused = sb->is(AbstractStateBehavior::Type::Pause) || sb->is(AbstractStateBehavior::Type::ToolChange);
30233050
bool idle = sb->is(AbstractStateBehavior::Type::Idle);
@@ -3026,22 +3053,23 @@ void FrmMain::updateControlsState()
30263053
// ui->control->setEnabled(portOpened);
30273054
ui->spindle->setEnabled(portOpened);
30283055
// TODO: add Action::Jog to ToolChangeBehavior::availableActions(), then simplify to sb->canExecute(Action::Jog)
3029-
ui->jog->setEnabled(sb->isOneOf(AbstractStateBehavior::Type::Idle, AbstractStateBehavior::Type::GoTo, AbstractStateBehavior::Type::Jogging));
30303056

30313057
ui->console->setEnabled(portOpened && !m_configuration.joggingModule().keyboardControl());
30323058
// ui->cmdCommandSend->setEnabled(portOpened);
30333059

3034-
ui->control->updateControlsState(sb);
3060+
ui->control->updateControlsState(state);
3061+
ui->jog->updateControlsState(state);
30353062

30363063
//ui->spindle->...
30373064
// ui->cmdSpindle->setEnabled(!running);
30383065

30393066
bool canOpenFile = UiPermissions::isAllowed(UiPermission::OpenFile, sb->type());
30403067
ui->actFileNew->setEnabled(idle);
30413068
ui->actFileOpen->setEnabled(canOpenFile);
3042-
ui->program->setOpenButtonEnabled(canOpenFile);
3043-
ui->program->setResetButtonEnabled(idle && !program().empty());
3044-
ui->program->setSendButtonEnabled(sb->canExecute(Action::Type::Run) && !program().empty());
3069+
// ui->program->setOpenButtonEnabled(canOpenFile);
3070+
// ui->program->setResetButtonEnabled(idle && !program().empty());
3071+
// ui->program->setSendButtonEnabled(sb->canExecute(Action::Type::Run) && !program().empty());
3072+
ui->program->updateControlsState(state);
30453073
// switch (senderState) {
30463074
// case SenderState::Pausing:
30473075
// case SenderState::Pausing2:
@@ -3109,10 +3137,7 @@ void FrmMain::updateControlsState()
31093137

31103138
ui->program->setSendButtonText(m_heightmapMode ? tr("Probe") : tr("Send"));
31113139

3112-
ui->heightmap->updateControlsState(
3113-
!program().empty(),
3114-
m_heightmapMode
3115-
);
3140+
ui->heightmap->updateControlsState(state);
31163141

31173142
ui->actFileSaveTransformedAs->setVisible(ui->heightmap->useMap());
31183143

src/gpilot/ui/forms/frmmain.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
#include "core/utils/timer.h"
5353
#include "core/core.h"
5454
#include "core/state_behavior/userpromptspec.h"
55+
#include "ui/utils/uistate.h"
5556

5657
namespace Ui {
5758
class frmMain;
@@ -284,7 +285,9 @@ private slots:
284285
void newHeightmap();
285286

286287
// Ui
287-
void updateControlsState();
288+
UiState currentUiState() const;
289+
void updateControlsState(const UiState& state);
290+
void updateControlsState() { updateControlsState(currentUiState()); }
288291
void updateLayouts();
289292
void updateRecentFilesMenus();
290293
void updateJogTitle();
@@ -324,6 +327,7 @@ private slots:
324327
void initializeVirtualSettingsPanel();
325328
void initializeDockCorners();
326329
void connectWindowTitleUpdater();
330+
void connectFilesManagerToUpdateControls();
327331

328332
void initializeConnection(ConfigurationConnection::ConnectionMode mode);
329333
void initializeDockTitles();

src/gpilot/ui/forms/partials/main/partmaincontrol.cpp

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -78,28 +78,22 @@ void PartMainControl::disable()
7878

7979
}
8080

81-
void PartMainControl::updateControlsState(bool portOpened, bool process)
82-
{
83-
// ui->cmdCheck->setEnabled(portOpened && !process);
84-
// ui->cmdHome->setEnabled(!process);
85-
// ui->cmdCheck->setEnabled(!process);
86-
// ui->cmdUnlock->setEnabled(!process);
87-
// //ui->cmdSpindle->setEnabled(!process);
88-
// ui->cmdSleep->setEnabled(!process);
89-
}
90-
91-
void PartMainControl::updateControlsState(AbstractStateBehavior *sb)
81+
void PartMainControl::updateControlsState(const UiState& state)
9282
{
83+
AbstractStateBehavior *sb = state.sb;
9384
// TODO: replace with sb->canExecute(Action::CheckMode) once Action::CheckMode is added
9485
ui->cmdCheck->setEnabled(sb->isOneOf(AbstractStateBehavior::Type::Idle, AbstractStateBehavior::Type::CheckMode));
9586
ui->cmdCheck->setChecked(sb->is(AbstractStateBehavior::Type::CheckMode));
9687
{
9788
QSignalBlocker blocker(ui->cmdHold);
9889
ui->cmdHold->setChecked(sb->is(AbstractStateBehavior::Type::Hold));
9990
}
91+
ui->cmdHome->setEnabled(sb->canExecute(Action::Type::Home));
10092
ui->cmdProbe->setEnabled(sb->canExecute(Action::Type::Probe));
10193
ui->cmdZeroZ->setEnabled(sb->canExecute(Action::Type::ZeroZ));
10294
ui->cmdZeroXY->setEnabled(sb->canExecute(Action::Type::ZeroXY));
95+
ui->cmdUnlock->setEnabled(sb->canExecute(Action::Type::Unlock));
96+
ui->cmdReset->setEnabled(sb->canExecute(Action::Type::Reset));
10397
bool scanPrompt = false;
10498
if (sb->is(AbstractStateBehavior::Type::UserPrompt)) {
10599
auto *prompt = static_cast<UserPromptBehavior*>(sb);

src/gpilot/ui/forms/partials/main/partmaincontrol.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include "core/globals.h"
99
#include "core/state_behavior/abstractstatebehavior.h"
10+
#include "ui/utils/uistate.h"
1011
#include <QWidget>
1112
#include <QMenu>
1213
#include <QAction>
@@ -24,8 +25,7 @@ class PartMainControl : public QWidget
2425
~PartMainControl();
2526
void enable();
2627
void disable();
27-
void updateControlsState(bool portOpened, bool process);
28-
void updateControlsState(AbstractStateBehavior *sb);
28+
void updateControlsState(const UiState& state);
2929
bool hold();
3030
void setFlood(bool);
3131
void initialize();

src/gpilot/ui/forms/partials/main/partmainheightmap.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,10 @@ void PartMainHeightmap::resetUseHeighmap()
132132
ui->chkUseHeightmap->setChecked(false);
133133
}
134134

135-
void PartMainHeightmap::updateControlsState(bool mainState, bool heightmapMode)
135+
void PartMainHeightmap::updateControlsState(const UiState& state)
136136
{
137-
//setEnabled(mainState);
138-
ui->cmdHeightMapMode->setEnabled(!ui->txtHeightMapName->text().isEmpty());
139-
ui->chkUseHeightmap->setEnabled(!heightmapMode && !ui->txtHeightMapName->text().isEmpty());
137+
ui->cmdHeightMapMode->setEnabled(state.files.heightmapOpened);
138+
ui->chkUseHeightmap->setEnabled(!heightmapMode() && state.files.heightmapOpened);
140139
}
141140

142141
void PartMainHeightmap::setOpenFile(QString filePath)

src/gpilot/ui/forms/partials/main/partmainheightmap.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <QWidget>
99
#include "core/heightmap/configurationheightmap.h"
1010
#include "core/heightmap/interpolator/abstractheightmapinterpolator.h"
11+
#include "ui/utils/uistate.h"
1112

1213
namespace Ui {
1314
class partMainHeightmap;
@@ -28,7 +29,7 @@ class PartMainHeightmap : public QWidget
2829
void setGridUpdateEnabled();
2930
void resetOpenFile();
3031
void resetUseHeighmap();
31-
void updateControlsState(bool mainState, bool heightmapMode);
32+
void updateControlsState(const UiState& state);
3233
void setOpenFile(QString filePath);
3334
QRectF areaRectFromTextboxes();
3435
void setHeightmapAreaRect(QRectF area);

src/gpilot/ui/forms/partials/main/partmainjog.cpp

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,6 @@
11
#include "partmainjog.h"
22
#include "ui_partmainjog.h"
3-
4-
void PartMainJog::updateControls()
5-
{
6-
// ui->cboJogStep->setEditable(!ui->chkKeyboardControl->isChecked());
7-
// ui->cboJogFeed->setEditable(!ui->chkKeyboardControl->isChecked());
8-
// ui->cboJogStep->setEnabled(!ui->chkKeyboardControl->isChecked());
9-
// ui->cboJogFeed->setEnabled(!ui->chkKeyboardControl->isChecked());
10-
// //ui->cboJogStep->setStyleSheet(QString("font-size: %1").arg(m_configuration.uiModule().fontSize()));
11-
// ui->cboJogFeed->setStyleSheet(ui->cboJogStep->styleSheet());
12-
}
3+
#include "core/state_behavior/abstractstatebehavior.h"
134

145
PartMainJog::PartMainJog(QWidget *parent)
156
: QWidget(parent)
@@ -51,6 +42,18 @@ PartMainJog::PartMainJog(QWidget *parent)
5142
}
5243
}
5344

45+
void PartMainJog::updateControlsState(const UiState& state)
46+
{
47+
// ui->jog->setEnabled(sb->isOneOf(AbstractStateBehavior::Type::Idle, AbstractStateBehavior::Type::GoTo, AbstractStateBehavior::Type::Jogging));
48+
// ui->cboJogStep->setEditable(!ui->chkKeyboardControl->isChecked());
49+
// ui->cboJogFeed->setEditable(!ui->chkKeyboardControl->isChecked());
50+
// ui->cboJogStep->setEnabled(!ui->chkKeyboardControl->isChecked());
51+
// ui->cboJogFeed->setEnabled(!ui->chkKeyboardControl->isChecked());
52+
// //ui->cboJogStep->setStyleSheet(QString("font-size: %1").arg(m_configuration.uiModule().fontSize()));
53+
// ui->cboJogFeed->setStyleSheet(ui->cboJogStep->styleSheet());
54+
setEnabled(state.sb->canExecute(Action::Type::Jog));
55+
}
56+
5457
void PartMainJog::configurationUpdated()
5558
{
5659
m_storedKeyboardControl = m_configurationJogging->keyboardControl();

src/gpilot/ui/forms/partials/main/partmainjog.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "core/config/module/configurationjogging.h"
55
#include "core/globals.h"
66
#include <QWidget>
7+
#include "ui/utils/uistate.h"
78

89
namespace Ui {
910
class partMainJog;
@@ -17,20 +18,19 @@ class PartMainJog : public QWidget
1718
explicit PartMainJog(QWidget *parent = nullptr);
1819
void initialize(ConfigurationJogging &configurationJogging);
1920
~PartMainJog();
20-
2121
JoggingVector jogVector() const { return m_jogVector; };
2222
void storeAndResetKeyboardControl();
2323
void setKeyboardControl(bool value);
2424
void configurationUpdated();
2525
void restoreKeyboardControl();
26+
void updateControlsState(const UiState &state);
2627

2728
private:
2829
Ui::partMainJog *ui;
2930
bool m_initialized = false;
3031
bool m_storedKeyboardControl = false;
3132
ConfigurationJogging *m_configurationJogging;
3233
JoggingVector m_jogVector;
33-
void updateControls();
3434
void stopJogging();
3535
void stopJoggingIfContinuous();
3636

src/gpilot/ui/forms/partials/main/partmainprogram.cpp

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "core/heightmap/heightmap.h"
1313
#include "ui/tables/heightmapitemdelegate.h"
1414
#include "ui/utils/thememanager.h"
15+
#include "ui/utils/uipermissions.h"
1516

1617
PartMainProgram::PartMainProgram(QWidget* parent)
1718
: QWidget(parent)
@@ -77,10 +78,8 @@ void PartMainProgram::setupUi()
7778
ui->btnProgram->setChecked(true);
7879
return;
7980
}
80-
// updateGroupsFromUI();
8181
ui->btnHeightmap->setChecked(false);
82-
// m_dark = true;
83-
// updateUIFromGroups();
82+
ui->panProgram->setVisible(true);
8483
showProgramTable();
8584
});
8685
connect(ui->btnHeightmap, &QPushButton::clicked, this, [this]() {
@@ -90,10 +89,8 @@ void PartMainProgram::setupUi()
9089
ui->btnHeightmap->setChecked(true);
9190
return;
9291
}
93-
// updateGroupsFromUI();
9492
ui->btnProgram->setChecked(false);
95-
// m_dark = false;
96-
// updateUIFromGroups();
93+
ui->panProgram->setVisible(false);
9794
showHeightmapTable();
9895
});
9996

@@ -248,20 +245,37 @@ void PartMainProgram::restoreHeaderState(const QByteArray& state)
248245
ui->tblProgram->horizontalHeader()->restoreState(state);
249246
}
250247

251-
void PartMainProgram::setFileButtonsEnabled(bool open, bool reset, bool send, bool pause, bool abort)
248+
void PartMainProgram::updateControlsState(const UiState& state)
252249
{
253-
ui->cmdFileOpen->setEnabled(open);
254-
ui->cmdFileReset->setEnabled(reset);
255-
ui->cmdFileSend->setEnabled(send);
256-
ui->cmdFilePauseResume->setEnabled(pause);
257-
ui->cmdFileAbort->setEnabled(abort);
250+
// ui->program->setOpenButtonEnabled(canOpenFile);
251+
// ui->program->setResetButtonEnabled(idle && !program().empty());
252+
// ui->program->setSendButtonEnabled(sb->canExecute(Action::Type::Run) && !program().empty());
253+
const auto t = state.sb->type();
254+
ui->cmdFileOpen->setEnabled(UiPermissions::isAllowed(UiPermission::OpenFile, t));
255+
ui->cmdFileReset->setEnabled(
256+
state.files.gcodeOpened &&
257+
state.sb->is(AbstractStateBehavior::Type::Idle)
258+
);
259+
ui->cmdFileSend->setEnabled(
260+
state.files.gcodeOpened &&
261+
state.sb->is(AbstractStateBehavior::Type::Idle)
262+
);
258263
}
259264

260-
void PartMainProgram::setOpenButtonEnabled(bool enabled) { ui->cmdFileOpen->setEnabled(enabled); }
261-
void PartMainProgram::setResetButtonEnabled(bool enabled) { ui->cmdFileReset->setEnabled(enabled); }
262-
void PartMainProgram::setSendButtonEnabled(bool enabled) { ui->cmdFileSend->setEnabled(enabled); }
263-
void PartMainProgram::setAbortButtonEnabled(bool enabled) { ui->cmdFileAbort->setEnabled(enabled); }
264-
void PartMainProgram::setPauseButtonEnabled(bool enabled) { ui->cmdFilePauseResume->setEnabled(enabled); }
265+
// void PartMainProgram::setFileButtonsEnabled(bool open, bool reset, bool send, bool pause, bool abort)
266+
// {
267+
// ui->cmdFileOpen->setEnabled(open);
268+
// ui->cmdFileReset->setEnabled(reset);
269+
// ui->cmdFileSend->setEnabled(send);
270+
// ui->cmdFilePauseResume->setEnabled(pause);
271+
// ui->cmdFileAbort->setEnabled(abort);
272+
// }
273+
274+
// void PartMainProgram::setOpenButtonEnabled(bool enabled) { ui->cmdFileOpen->setEnabled(enabled); }
275+
// void PartMainProgram::setResetButtonEnabled(bool enabled) { ui->cmdFileReset->setEnabled(enabled); }
276+
// void PartMainProgram::setSendButtonEnabled(bool enabled) { ui->cmdFileSend->setEnabled(enabled); }
277+
// void PartMainProgram::setAbortButtonEnabled(bool enabled) { ui->cmdFileAbort->setEnabled(enabled); }
278+
// void PartMainProgram::setPauseButtonEnabled(bool enabled) { ui->cmdFilePauseResume->setEnabled(enabled); }
265279

266280
void PartMainProgram::setPauseButtonText(const QString& text)
267281
{

0 commit comments

Comments
 (0)