| 344 |
Kevin |
1 |
#include "MainWindow.h"
|
|
|
2 |
|
|
|
3 |
MainWindow::MainWindow(QWidget *parent) :
|
|
|
4 |
QMainWindow(parent)
|
|
|
5 |
{
|
|
|
6 |
// Set central widget
|
|
|
7 |
centralWidget = new QWidget();
|
|
|
8 |
setCentralWidget(centralWidget);
|
|
|
9 |
|
|
|
10 |
// Serial connection UI
|
|
|
11 |
groupSerialInit = new QGroupBox("Initialization");
|
| 345 |
Kevin |
12 |
btnSerialConnect = new QPushButton("&Connect");
|
|
|
13 |
btnSerialRefresh = new QPushButton("&Refresh");
|
| 348 |
Kevin |
14 |
labelSerialPort = new QLabel("Serial Port:");
|
| 344 |
Kevin |
15 |
labelSerialSpeed = new QLabel("Baud Rate:");
|
| 348 |
Kevin |
16 |
labelSerialDataBits = new QLabel("Data:");
|
|
|
17 |
labelSerialStopBits = new QLabel("Stop:");
|
|
|
18 |
labelSerialParity = new QLabel("Parity Bit:");
|
|
|
19 |
labelSerialFlowControl = new QLabel("Flow Control:");
|
| 344 |
Kevin |
20 |
cboxSerialPort = new QComboBox();
|
| 348 |
Kevin |
21 |
cboxSerialPort->setMinimumWidth(80);
|
| 344 |
Kevin |
22 |
cboxSerialSpeed = new QComboBox();
|
| 346 |
Kevin |
23 |
cboxSerialSpeed->setEditable(true);
|
| 348 |
Kevin |
24 |
cboxSerialSpeed->setValidator(new QIntValidator(0, 100000000, this));
|
|
|
25 |
cboxSerialDataBits = new QComboBox();
|
|
|
26 |
cboxSerialDataBits->setMaximumWidth(60);
|
|
|
27 |
cboxSerialStopBits = new QComboBox();
|
|
|
28 |
cboxSerialStopBits->setMaximumWidth(60);
|
|
|
29 |
cboxSerialParity = new QComboBox();
|
|
|
30 |
cboxSerialFlowControl = new QComboBox();
|
|
|
31 |
cboxSerialFlowControl->setMinimumWidth(70);
|
| 344 |
Kevin |
32 |
|
| 348 |
Kevin |
33 |
QGridLayout *serialSettingsLayout = new QGridLayout();
|
|
|
34 |
serialSettingsLayout->addWidget(btnSerialConnect, 0, 0);
|
|
|
35 |
serialSettingsLayout->addWidget(labelSerialPort, 0, 1);
|
|
|
36 |
serialSettingsLayout->addWidget(cboxSerialPort, 0, 2);
|
|
|
37 |
serialSettingsLayout->addWidget(labelSerialDataBits, 0, 3);
|
|
|
38 |
serialSettingsLayout->addWidget(cboxSerialDataBits, 0, 4);
|
|
|
39 |
serialSettingsLayout->addWidget(labelSerialParity, 0, 5);
|
|
|
40 |
serialSettingsLayout->addWidget(cboxSerialParity, 0, 6);
|
|
|
41 |
|
|
|
42 |
serialSettingsLayout->addWidget(btnSerialRefresh, 1, 0);
|
|
|
43 |
serialSettingsLayout->addWidget(labelSerialSpeed, 1, 1);
|
|
|
44 |
serialSettingsLayout->addWidget(cboxSerialSpeed, 1, 2);
|
|
|
45 |
serialSettingsLayout->addWidget(labelSerialStopBits, 1, 3);
|
|
|
46 |
serialSettingsLayout->addWidget(cboxSerialStopBits, 1, 4);
|
|
|
47 |
serialSettingsLayout->addWidget(labelSerialFlowControl, 1, 5);
|
|
|
48 |
serialSettingsLayout->addWidget(cboxSerialFlowControl, 1, 6);
|
|
|
49 |
|
|
|
50 |
groupSerialInit->setLayout(serialSettingsLayout);
|
| 344 |
Kevin |
51 |
groupSerialInit->setFixedSize(groupSerialInit->sizeHint());
|
|
|
52 |
|
|
|
53 |
// Serial data UI
|
|
|
54 |
groupSerialData = new QGroupBox("Data");
|
|
|
55 |
textSerialData = new QTextEdit();
|
|
|
56 |
textSerialData->setCurrentFont(QFont("Consolas", 8));
|
|
|
57 |
textSerialData->append("Waiting for serial connection...");
|
|
|
58 |
textSerialData->setMinimumHeight(100);
|
|
|
59 |
textSerialData->setReadOnly(true);
|
|
|
60 |
textSerialTransmit = new QLineEdit();
|
|
|
61 |
textSerialTransmit->setFont(QFont("Consolas", 8));
|
| 345 |
Kevin |
62 |
btnSerialTransmit = new QPushButton("&Send");
|
| 344 |
Kevin |
63 |
btnSerialClear = new QPushButton("Clear");
|
|
|
64 |
|
|
|
65 |
QHBoxLayout *serialTransmitLayout = new QHBoxLayout();
|
|
|
66 |
serialTransmitLayout->addWidget(textSerialTransmit);
|
|
|
67 |
serialTransmitLayout->addWidget(btnSerialTransmit);
|
|
|
68 |
serialTransmitLayout->addWidget(btnSerialClear);
|
|
|
69 |
QVBoxLayout *serialDataLayout = new QVBoxLayout();
|
|
|
70 |
serialDataLayout->addWidget(textSerialData);
|
|
|
71 |
serialDataLayout->addLayout(serialTransmitLayout);
|
|
|
72 |
groupSerialData->setLayout(serialDataLayout);
|
|
|
73 |
|
|
|
74 |
|
| 345 |
Kevin |
75 |
// Macro controller
|
|
|
76 |
groupMacro = new QGroupBox("Other");
|
|
|
77 |
btnMacro = new QPushButton("&Macros");
|
|
|
78 |
QHBoxLayout *macroLayout = new QHBoxLayout();
|
|
|
79 |
macroLayout->addWidget(btnMacro);
|
|
|
80 |
groupMacro->setLayout(macroLayout);
|
| 348 |
Kevin |
81 |
// groupMacro->setFixedSize(groupMacro->sizeHint());
|
| 345 |
Kevin |
82 |
|
|
|
83 |
|
| 344 |
Kevin |
84 |
// Main layout
|
|
|
85 |
QGridLayout *mainLayout = new QGridLayout();
|
| 345 |
Kevin |
86 |
mainLayout->addWidget(groupSerialInit, 0, 0, 1, 1, Qt::AlignLeft);
|
|
|
87 |
mainLayout->addWidget(groupMacro, 0, 1, 1, 1, Qt::AlignLeft);
|
|
|
88 |
mainLayout->addWidget(groupSerialData, 1, 0, 1, 2);
|
|
|
89 |
mainLayout->setColumnStretch(0, 0);
|
|
|
90 |
mainLayout->setColumnStretch(1, 1);
|
| 344 |
Kevin |
91 |
centralWidget->setLayout(mainLayout);
|
|
|
92 |
|
| 345 |
Kevin |
93 |
// Initialize thread for connection to serial port
|
| 344 |
Kevin |
94 |
serialThread = new QThread();
|
|
|
95 |
serialController = new SerialController();
|
|
|
96 |
serialController->moveToThread(serialThread);
|
|
|
97 |
connect(this, SIGNAL(Serial_QueryParameters()), serialController, SLOT(Serial_QueryParameters()));
|
| 348 |
Kevin |
98 |
connect(this, SIGNAL(Serial_Connect(QString,QString,QString,QString,QString,QString)),
|
|
|
99 |
serialController, SLOT(Serial_Connect(QString,QString,QString,QString,QString,QString)));
|
| 344 |
Kevin |
100 |
connect(this, SIGNAL(Serial_Disconnect()), serialController, SLOT(Serial_Disconnect()));
|
|
|
101 |
connect(this, SIGNAL(Serial_TransmitString(QString)), serialController, SLOT(Serial_TransmitString(QString)));
|
| 348 |
Kevin |
102 |
connect(serialController, SIGNAL(Serial_UpdateParameters(QStringList,QStringList,QStringList,QStringList,QStringList,QStringList)),
|
|
|
103 |
this, SLOT(Serial_UpdateParameters(QStringList,QStringList,QStringList,QStringList,QStringList,QStringList)));
|
| 344 |
Kevin |
104 |
connect(serialController, SIGNAL(Serial_ReceivedString(QString)), this, SLOT(Serial_ReceivedString(QString)));
|
|
|
105 |
connect(serialController, SIGNAL(Serial_Connected()), this, SLOT(Serial_Connected()));
|
|
|
106 |
connect(serialController, SIGNAL(Serial_Disconnected()), this, SLOT(Serial_Disconnected()));
|
|
|
107 |
connect(serialThread, SIGNAL(finished()), serialController, SLOT(deleteLater()));
|
|
|
108 |
serialThread->start();
|
|
|
109 |
|
| 345 |
Kevin |
110 |
// Initialize macro widget
|
| 348 |
Kevin |
111 |
macroDockWidget = new QDockWidget("Macro Controller", this);
|
|
|
112 |
macroController = new MacroController(macroDockWidget);
|
|
|
113 |
macroDockWidget->setWidget(macroController);
|
|
|
114 |
macroDockWidget->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
|
|
|
115 |
macroDockWidget->hide();
|
|
|
116 |
addDockWidget(Qt::RightDockWidgetArea, macroDockWidget);
|
| 345 |
Kevin |
117 |
connect(macroController, SIGNAL(Macro_TransmitText(QString)), this, SLOT(Serial_PrepareTransmit(QString)));
|
|
|
118 |
connect(serialController, SIGNAL(Serial_Connected()), macroController, SLOT(Macro_EnableTransmit()));
|
|
|
119 |
connect(serialController, SIGNAL(Serial_Disconnected()), macroController, SLOT(Macro_DisableTransmit()));
|
|
|
120 |
|
|
|
121 |
// Connect local widgets
|
|
|
122 |
connect(btnSerialConnect, SIGNAL(clicked()), this, SLOT(Serial_ConnectToggleBtn()));
|
|
|
123 |
connect(btnSerialRefresh, SIGNAL(clicked()), this, SIGNAL(Serial_QueryParameters()));
|
|
|
124 |
connect(btnSerialTransmit, SIGNAL(clicked()), this, SLOT(Serial_PrepareTransmit()));
|
|
|
125 |
connect(textSerialTransmit, SIGNAL(returnPressed()), btnSerialTransmit, SIGNAL(clicked()));
|
|
|
126 |
connect(btnSerialClear, SIGNAL(clicked()), this, SLOT(Serial_ClearBtn()));
|
| 348 |
Kevin |
127 |
connect(btnMacro, SIGNAL(clicked()), macroDockWidget->toggleViewAction(), SLOT(trigger()));
|
| 345 |
Kevin |
128 |
|
| 344 |
Kevin |
129 |
emit Serial_QueryParameters();
|
|
|
130 |
Serial_Disconnected();
|
| 345 |
Kevin |
131 |
|
|
|
132 |
setWindowTitle("Marlin Controller");
|
| 347 |
Kevin |
133 |
setWindowIcon(QIcon(":/External/Resources/Icon_16.bmp"));
|
| 344 |
Kevin |
134 |
}
|
|
|
135 |
|
|
|
136 |
MainWindow::~MainWindow()
|
|
|
137 |
{
|
|
|
138 |
serialThread->quit();
|
|
|
139 |
}
|
|
|
140 |
|
|
|
141 |
void MainWindow::Serial_ConnectToggleBtn()
|
|
|
142 |
{
|
|
|
143 |
if (serialController->connected) {
|
|
|
144 |
emit Serial_Disconnect();
|
|
|
145 |
} else {
|
|
|
146 |
if (cboxSerialPort->currentText() != "" && cboxSerialSpeed->currentText() != "") {
|
| 348 |
Kevin |
147 |
emit Serial_Connect(cboxSerialPort->currentText(), cboxSerialSpeed->currentText(), cboxSerialDataBits->currentText(),
|
|
|
148 |
cboxSerialStopBits->currentText(), cboxSerialParity->currentText(), cboxSerialFlowControl->currentText());
|
| 344 |
Kevin |
149 |
}
|
|
|
150 |
}
|
|
|
151 |
}
|
|
|
152 |
|
| 345 |
Kevin |
153 |
void MainWindow::Serial_PrepareTransmit(QString string)
|
| 344 |
Kevin |
154 |
{
|
| 345 |
Kevin |
155 |
if (string == "") {
|
|
|
156 |
string = textSerialTransmit->text();
|
|
|
157 |
textSerialTransmit->setText("");
|
|
|
158 |
}
|
|
|
159 |
emit Serial_TransmitString(string);
|
| 344 |
Kevin |
160 |
textSerialData->setTextColor(Qt::darkBlue);
|
| 345 |
Kevin |
161 |
|
|
|
162 |
QStringList cmds = string.split('\n');
|
|
|
163 |
for (int i = 0; i < cmds.size(); i++) {
|
|
|
164 |
textSerialData->append("TX: " + cmds[i]);
|
|
|
165 |
}
|
| 344 |
Kevin |
166 |
}
|
|
|
167 |
|
|
|
168 |
void MainWindow::Serial_ClearBtn()
|
|
|
169 |
{
|
|
|
170 |
textSerialData->clear();
|
|
|
171 |
}
|
|
|
172 |
|
| 348 |
Kevin |
173 |
void MainWindow::Serial_UpdateParameters(QStringList ports, QStringList speeds, QStringList dataBits,
|
|
|
174 |
QStringList stopBits, QStringList parity, QStringList flowControl)
|
| 345 |
Kevin |
175 |
{
|
| 348 |
Kevin |
176 |
QString currPort = cboxSerialPort->currentText();
|
| 344 |
Kevin |
177 |
cboxSerialPort->clear();
|
|
|
178 |
cboxSerialPort->addItems(ports);
|
| 348 |
Kevin |
179 |
if (currPort != "" && ports.contains(currPort))
|
|
|
180 |
cboxSerialPort->setCurrentText(currPort);
|
| 344 |
Kevin |
181 |
|
| 348 |
Kevin |
182 |
QString currSpeed = cboxSerialSpeed->currentText();
|
| 344 |
Kevin |
183 |
cboxSerialSpeed->clear();
|
|
|
184 |
cboxSerialSpeed->addItems(speeds);
|
| 348 |
Kevin |
185 |
if (currSpeed != "") cboxSerialSpeed->setCurrentText(currSpeed);
|
|
|
186 |
|
|
|
187 |
int currData = cboxSerialDataBits->currentIndex();
|
|
|
188 |
cboxSerialDataBits->clear();
|
|
|
189 |
cboxSerialDataBits->addItems(dataBits);
|
|
|
190 |
if (currData >= 0) cboxSerialDataBits->setCurrentIndex(currData);
|
|
|
191 |
|
|
|
192 |
int currStop = cboxSerialStopBits->currentIndex();
|
|
|
193 |
cboxSerialStopBits->clear();
|
|
|
194 |
cboxSerialStopBits->addItems(stopBits);
|
|
|
195 |
if (currStop >= 0) cboxSerialStopBits->setCurrentIndex(currStop);
|
|
|
196 |
|
|
|
197 |
int currParity = cboxSerialParity->currentIndex();
|
|
|
198 |
cboxSerialParity->clear();
|
|
|
199 |
cboxSerialParity->addItems(parity);
|
|
|
200 |
if (currParity >= 0) cboxSerialParity->setCurrentIndex(currParity);
|
|
|
201 |
|
|
|
202 |
int currFlow = cboxSerialFlowControl->currentIndex();
|
|
|
203 |
cboxSerialFlowControl->clear();
|
|
|
204 |
cboxSerialFlowControl->addItems(flowControl);
|
|
|
205 |
if (currFlow >= 0) cboxSerialFlowControl->setCurrentIndex(currFlow);
|
| 344 |
Kevin |
206 |
}
|
|
|
207 |
|
|
|
208 |
void MainWindow::Serial_ReceivedString(QString string)
|
|
|
209 |
{
|
|
|
210 |
textSerialData->setTextColor(Qt::darkRed);
|
|
|
211 |
textSerialData->append("RX: " + string);
|
|
|
212 |
}
|
|
|
213 |
|
|
|
214 |
void MainWindow::Serial_Connected()
|
|
|
215 |
{
|
| 345 |
Kevin |
216 |
btnSerialConnect->setText("&Disconnect");
|
| 344 |
Kevin |
217 |
cboxSerialPort->setEnabled(false);
|
|
|
218 |
cboxSerialSpeed->setEnabled(false);
|
|
|
219 |
btnSerialRefresh->setEnabled(false);
|
|
|
220 |
textSerialTransmit->setEnabled(true);
|
|
|
221 |
btnSerialTransmit->setEnabled(true);
|
|
|
222 |
}
|
|
|
223 |
|
|
|
224 |
void MainWindow::Serial_Disconnected()
|
|
|
225 |
{
|
| 345 |
Kevin |
226 |
btnSerialConnect->setText("&Connect");
|
| 344 |
Kevin |
227 |
cboxSerialPort->setEnabled(true);
|
|
|
228 |
cboxSerialSpeed->setEnabled(true);
|
|
|
229 |
btnSerialRefresh->setEnabled(true);
|
|
|
230 |
textSerialTransmit->setEnabled(false);
|
|
|
231 |
btnSerialTransmit->setEnabled(false);
|
|
|
232 |
}
|