-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
334 lines (294 loc) · 10.4 KB
/
mainwindow.cpp
File metadata and controls
334 lines (294 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#include "mainwindow.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QFileDialog>
#include <QStackedWidget>
#include <QMessageBox>
#include <QApplication>
#include <QDir>
#include <QLocale>
/**
* @brief Constructs the MainWindow, initializes the backend, and sets up UI connections.
* @param parent The parent widget.
*/
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
backend = new SaturnBackend(this);
setupUi();
retranslateUi();
// --- UI Signal/Slot Connections ---
connect(backend, &SaturnBackend::modelDetected, [this](QString model)
{
QString ip = ipInput->text();
if (!ip.isEmpty()) {
ipToModel.insert(ip, model);
}
QString imagePath = getIconPathForModel(model);
QPixmap pixmap(imagePath);
if (!pixmap.isNull()) {
imgLabel->setPixmap(pixmap.scaled(300, 300, Qt::KeepAspectRatio, Qt::SmoothTransformation));
} });
connect(backend, &SaturnBackend::connectionReady, [this]()
{ qobject_cast<QStackedWidget *>(centralWidget())->setCurrentWidget(controlPage); });
connect(backend, &SaturnBackend::statusUpdate, this, &MainWindow::updateStatus);
connect(backend, &SaturnBackend::remainingTimeUpdate, this, &MainWindow::updateRemainingTime);
connect(backend, &SaturnBackend::statusUpdate, [this](QString status, int, int, QString)
{
if (status.contains(tr("Printing")) || status.contains(tr("Exposing")) || status.contains(tr("Lowering"))) {
btnPrintLast->setVisible(false);
} });
connect(backend, &SaturnBackend::uploadProgress, progressBar, &QProgressBar::setValue);
connect(backend, &SaturnBackend::fileReadyToPrint, this, &MainWindow::showPrintButton);
connect(backend, &SaturnBackend::logMessage, [](QString msg)
{ qDebug() << "LOG:" << msg; });
// Set initial language based on system locale
QString defaultLocale = QLocale::system().name().section('_', 0, 0);
int index = languageComboBox->findData(defaultLocale);
if (index != -1)
{
languageComboBox->setCurrentIndex(index);
}
else
{
languageComboBox->setCurrentIndex(0); // Default to English
}
onLanguageChanged(languageComboBox->currentIndex());
}
/**
* @brief Sets up the entire user interface, including pages, layouts, and widgets.
*/
void MainWindow::setupUi()
{
QStackedWidget *stack = new QStackedWidget;
// --- PAGE 1: SCANNER ---
scanPage = new QWidget;
QVBoxLayout *layout1 = new QVBoxLayout(scanPage);
printerList = new QListWidget;
btnScan = new QPushButton();
ipInput = new QLineEdit;
btnConnect = new QPushButton();
scanPageLabel = new QLabel();
languageComboBox = new QComboBox();
languageComboBox->addItem("English", "en");
languageComboBox->addItem("Español", "es");
layout1->addWidget(languageComboBox);
layout1->addWidget(scanPageLabel);
layout1->addWidget(printerList);
layout1->addWidget(btnScan);
layout1->addWidget(ipInput);
layout1->addWidget(btnConnect);
connect(btnScan, &QPushButton::clicked, this, &MainWindow::onScanClicked);
connect(btnConnect, &QPushButton::clicked, this, &MainWindow::onConnectClicked);
connect(languageComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &MainWindow::onLanguageChanged);
// --- PAGE 2: CONTROL ---
controlPage = new QWidget;
QVBoxLayout *layout2 = new QVBoxLayout(controlPage);
imgLabel = new QLabel();
QPixmap pixmap(":/resources/images/default.png");
if (!pixmap.isNull())
{
imgLabel->setPixmap(pixmap.scaled(300, 300, Qt::KeepAspectRatio, Qt::SmoothTransformation));
}
imgLabel->setAlignment(Qt::AlignCenter);
lblStatus = new QLabel();
lblFile = new QLabel();
lblRemainingTime = new QLabel();
progressBar = new QProgressBar;
btnUpload = new QPushButton();
btnPrintLast = new QPushButton();
btnPrintLast->setStyleSheet("background-color: #dbf0e3; color: #2e5c3e; font-weight: bold;");
btnPrintLast->setVisible(false);
layout2->addWidget(imgLabel);
layout2->addWidget(lblStatus);
layout2->addWidget(lblFile);
layout2->addWidget(lblRemainingTime);
layout2->addWidget(progressBar);
layout2->addWidget(btnPrintLast);
layout2->addWidget(btnUpload);
connect(btnUpload, &QPushButton::clicked, this, &MainWindow::onUploadClicked);
connect(btnPrintLast, &QPushButton::clicked, this, &MainWindow::onPrintLastClicked);
stack->addWidget(scanPage);
stack->addWidget(controlPage);
setCentralWidget(stack);
resize(400, 600);
}
/**
* @brief Handles language change events to re-translate the UI.
* @param event The change event.
*/
void MainWindow::changeEvent(QEvent *event)
{
if (event->type() == QEvent::LanguageChange)
{
retranslateUi();
}
QMainWindow::changeEvent(event);
}
/**
* @brief Re-translates all UI elements to the currently loaded language.
*/
void MainWindow::retranslateUi()
{
setWindowTitle(tr("Elegoo Remote Control"));
scanPageLabel->setText(tr("Select a Elegoo printer:"));
btnScan->setText(tr("Scan for Printers"));
ipInput->setPlaceholderText(tr("Manual IP (e.g., 192.168.1.50)"));
btnConnect->setText(tr("Connect"));
imgLabel->setText(tr("[Image not found]"));
lblStatus->setText(tr("Status: DISCONNECTED"));
lblFile->setText(tr("File: -"));
lblRemainingTime->setText(tr("Remaining time: Calculating..."));
btnUpload->setText(tr("Upload .goo File"));
btnPrintLast->setText(tr("Print Last Uploaded File"));
}
/**
* @brief Slot triggered when the user selects a new language from the combo box.
* @param index The index of the selected language.
*/
void MainWindow::onLanguageChanged(int index)
{
QString langCode = languageComboBox->itemData(index).toString();
qApp->removeTranslator(&translator);
if (langCode != "en")
{
QDir translationsDir(qApp->applicationDirPath());
translationsDir.cd("translations");
if (translator.load(translationsDir.filePath("saturn_" + langCode + ".qm")))
{
qApp->installTranslator(&translator);
}
}
}
/**
* @brief Slot triggered by the 'Scan' button. Clears the list and starts discovery.
*/
void MainWindow::onScanClicked()
{
printerList->clear();
backend->startDiscovery();
}
/**
* @brief Slot triggered by the 'Connect' button. Uses the IP from the input field to connect.
*/
void MainWindow::onConnectClicked()
{
QString ip = ipInput->text();
if (ip.isEmpty())
return;
QString model = ipToModel.value(ip, "Unknown");
QString imagePath = getIconPathForModel(model);
QPixmap pixmap(imagePath);
if (!pixmap.isNull())
{
imgLabel->setPixmap(pixmap.scaled(300, 300, Qt::KeepAspectRatio, Qt::SmoothTransformation));
}
backend->connectToPrinter(ip);
}
/**
* @brief Slot triggered by the 'Upload' button. Opens a file dialog and starts the upload process.
*/
void MainWindow::onUploadClicked()
{
QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"), "", tr("Goo Files (*.goo *.ctb)"));
if (!fileName.isEmpty())
{
QMessageBox::StandardButton reply;
reply = QMessageBox::question(this, tr("Print"), tr("Start printing immediately after upload?"),
QMessageBox::Yes | QMessageBox::No);
btnPrintLast->setVisible(false);
lblStatus->setText(tr("Status: PREPARING UPLOAD..."));
progressBar->setValue(0);
progressBar->setFormat(tr("Calculating MD5..."));
QApplication::processEvents();
backend->uploadAndPrint(fileName, reply == QMessageBox::Yes);
}
}
/**
* @brief Updates the UI with the latest status from the printer.
*/
void MainWindow::updateStatus(QString status, int layer, int total, QString file)
{
if (status.contains(tr("RECEIVING")) || status.contains(tr("Uploading")))
{
lblStatus->setText(tr("Status: ") + status);
lblStatus->setStyleSheet("font-weight: bold; color: orange;");
lblFile->setText(tr("File: ") + file);
}
else if (total > 0)
{
lblStatus->setText(tr("Status: ") + status);
lblStatus->setStyleSheet("font-weight: bold; color: green;");
lblFile->setText(QString(tr("File: %1 (Layer %2/%3)")).arg(file).arg(layer).arg(total));
progressBar->setValue((layer * 100) / total);
progressBar->setFormat(tr("%p% (Printing)"));
}
else
{
lblStatus->setText(tr("Status: ") + status);
lblStatus->setStyleSheet("color: black;");
lblFile->setText(tr("File: ") + file);
if (status.contains(tr("Ready")))
{
progressBar->setValue(0);
progressBar->setFormat("%p%");
}
}
}
/**
* @brief Slot to update the estimated remaining time display.
* @param time The estimated time as a formatted string.
*/
void MainWindow::updateRemainingTime(const QString &time)
{
if (time.isEmpty())
{
lblRemainingTime->setVisible(false);
}
else
{
lblRemainingTime->setText(tr("Remaining time: ") + time);
lblRemainingTime->setVisible(true);
}
}
/**
* @brief Shows the "Print Last" button when a file has been successfully uploaded.
*/
void MainWindow::showPrintButton(QString filename)
{
lastReadyFile = filename;
btnPrintLast->setText(tr("Print: ") + filename);
btnPrintLast->setVisible(true);
}
/**
* @brief Slot triggered by the 'Print Last' button. Confirms with the user and starts the print.
*/
void MainWindow::onPrintLastClicked()
{
if (lastReadyFile.isEmpty())
return;
QMessageBox::StandardButton reply;
reply = QMessageBox::question(this, tr("Confirm Print"),
tr("Is the printer ready (build plate clean, resin filled, etc.)?\n\nFile: ") + lastReadyFile,
QMessageBox::Yes | QMessageBox::No);
if (reply == QMessageBox::Yes)
{
btnPrintLast->setVisible(false);
backend->printExistingFile(lastReadyFile);
}
}
/**
* @brief Returns the resource path for a printer's icon based on its model name.
*/
QString MainWindow::getIconPathForModel(const QString &modelName)
{
QString m = modelName.toLower();
if (m.contains("saturn 3 ultra"))
{
return ":/resources/images/saturn3ultra.png";
}
else if (m.contains("saturn 3"))
{
return ":/resources/images/saturn3.png";
}
return ":/resources/images/default.png";
}