forked from apsystems/GrblHoming
-
Notifications
You must be signed in to change notification settings - Fork 190
/
grbldialog.cpp
154 lines (132 loc) · 4.39 KB
/
grbldialog.cpp
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
/****************************************************************
* grbldialog.cpp
* GrblHoming - zapmaker fork on github
*
* 15 Nov 2012
* GPL License (see LICENSE file)
* Software is provided AS-IS
****************************************************************/
#include "grbldialog.h"
#include "ui_grbldialog.h"
GrblDialog::GrblDialog(QWidget *parent, GCode *gc) :
QDialog(parent),
gcode(gc),
ui(new Ui::GrblDialog),
tableRowCount(0)
{
ui->setupUi(this);
connect(ui->btnCancel,SIGNAL(clicked()),this,SLOT(Cancel()));
connect(ui->btnOk,SIGNAL(clicked()),this,SLOT(Ok()));
connect(this, SIGNAL(sendGcodeAndGetResult(int, QString)), gcode, SLOT(sendGcodeAndGetResult(int, QString)));
connect(gcode, SIGNAL(gcodeResult(int, QString)), this, SLOT(gcodeResult(int, QString)));
ui->btnCancel->setEnabled(false);
ui->btnOk->setEnabled(false);
QStringList labels;
labels << tr("Value") << tr("Item");
ui->table->setHorizontalHeaderLabels(labels);
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
ui->table->horizontalHeader()->setResizeMode(0, QHeaderView::Stretch);
#else
ui->table->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch);
#endif
ui->table->verticalHeader()->hide();
ui->table->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
ui->table->setShowGrid(true);
}
GrblDialog::~GrblDialog()
{
delete ui;
}
void GrblDialog::getSettings()
{
emit sendGcodeAndGetResult(GDLG_CMD_ID_GET, SETTINGS_COMMAND_V08a);
}
void GrblDialog::gcodeResult(int id, QString result)
{
switch (id)
{
case GDLG_CMD_ID_GET:
{
originalValues.clear();
changeFlags.clear();
if (result.size() > 0)
{
QRegExp rx(QString("\\$") + REGEXP_SETTINGS_LINE);
int pos = 0;
QStringList fieldValues;
QStringList descriptions;
while ((pos = rx.indexIn(result, pos)) != -1)
{
int matched = rx.matchedLength();
//QString extracted = received.mid(pos, matched);
pos += matched;
if (rx.captureCount() > 0)
{
QStringList list = rx.capturedTexts();
if (list.size() == 4)
{
fieldValues.append(list.at(2));
descriptions.append(list.at(3));
}
}
}
tableRowCount = fieldValues.size();
ui->table->setRowCount(0);
ui->table->setColumnCount(2);
int i;
for (i = 0; i < tableRowCount; i++)
{
ui->table->insertRow(i);
ui->table->setItem(i, 0, new QTableWidgetItem(fieldValues.at(i)));
QTableWidgetItem *descWidget = new QTableWidgetItem(descriptions.at(i));
descWidget->setFlags(Qt::NoItemFlags);
ui->table->setItem(i, 1, descWidget);
ui->table->item(i, 0)->setFont(QFont("Tahoma",10,87,false));
ui->table->item(i, 1)->setFont(QFont("Tahoma",10,-1,false));
originalValues.append(fieldValues.at(i));
changeFlags.append(false);
}
ui->table->resizeColumnsToContents();
int colWidthValues = ui->table->columnWidth(0);
ui->table->setColumnWidth(0, colWidthValues + 10);
connect(ui->table,SIGNAL(cellChanged(int,int)),this,SLOT(changeValues(int,int)));
}
else
{
ui->table->setRowCount(0);
ui->table->setColumnCount(2);
}
ui->btnCancel->setEnabled(true);
ui->btnOk->setEnabled(true);
break;
}
case GDLG_CMD_ID_SET:
break;
}
}
void GrblDialog::Cancel()
{
this->close();
}
void GrblDialog::changeValues(int row, int col)
{
Q_UNUSED(col);
if ((ui->table->item(row,0)->text() != originalValues.at(row))
&& ui->table->item(row,0)->text().length() > 0)
{
changeFlags.replace(row, true);
}
}
void GrblDialog::Ok()
{
for(int i = 0; i < tableRowCount; i++)
{
if (changeFlags.at(i) == true)
{
QString strline = "$";
strline.append(QString::number(i)).append("=").append(ui->table->item(i,0)->text()).append('\r');
emit sendGcodeAndGetResult(GDLG_CMD_ID_SET, strline);
}
}
this->close();
}