-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwidgetswindow.cpp
More file actions
157 lines (143 loc) · 4.78 KB
/
widgetswindow.cpp
File metadata and controls
157 lines (143 loc) · 4.78 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
#include "widgetswindow.h"
#include "ui_widgetswindow.h"
#include "sliderw.h"
#include "dialw.h"
#include "buttonw.h"
#include <QDebug>
#include <QFileDialog>
#include <QMessageBox>
widgetsWindow::widgetsWindow(QSerialPort* p, QWidget *parent) :
QMainWindow(parent),
ui(new Ui::widgetsWindow), port(p) {
ui->setupUi(this);
QWidget* topWidget = new QWidget(this);
widgetsLayout = new WidgetsAreaLayout();
topWidget->setLayout(widgetsLayout);
ui->scrollArea->setWidget(topWidget);
ui->actionSignal_Generator->setVisible(false);
// ui->scrollArea->installEventFilter(this);
}
widgetsWindow::~widgetsWindow() {
delete ui;
}
//bool widgetsWindow::eventFilter(QObject *object, QEvent *event) {
// qDebug() << "widgetsWindow::eventFilter : " << object << " event: " << event->type();
// return false;
//}
void widgetsWindow::sendToPort(QString msg) {
if (msg.isEmpty()) return; // Nothing to send
msg += "\n";
// Send data
qint64 result = port->write(msg.toLocal8Bit());
if (result != -1) {
// If data was sent successfully, clear line edit
//ui->receiveTerminal->appendHtml("→ ");
// qDebug() << "MSG Sent : " << msg;
emit messageSent(msg);
}
}
void widgetsWindow::on_actionSave_triggered() {
QString fileName = QFileDialog::getSaveFileName(this,
tr("Save Widgets File"), "",
tr("Widgets File (*.xml);"));
if (fileName.isEmpty()) {
return;
} else {
QFile file(fileName);
if (!file.open(QIODevice::WriteOnly)) {
QMessageBox::information(this, tr("Unable to open file"),
file.errorString());
return;
}
QTextStream stream( &file );
QDomDocument doc("WIDGETS");
QDomElement root = doc.createElement("WIDGETS");
doc.appendChild(root);
widgetsLayout->saveXml(doc);
doc.save(stream, QDomNode::EncodingFromDocument);
}
}
// XML Section
bool widgetsWindow::openXml() {
QString fileName = QFileDialog::getOpenFileName(this, tr("Open Widgets File"), "", tr("Xml (*.xml)"));
QFile file(fileName);
if (!file.open(QFile::ReadOnly | QFile::Text)) {
QMessageBox::information(this, tr("Unable to read file"),
file.errorString());
return false;
}
bool res = xmlWidgets.setContent(&file);
file.close();
return res;
}
void widgetsWindow::createWidget(QString type, QDomElement* domElt) {
qDebug() << "Add Widget : " << type;
if (type == "Dial") {
dialW* f = new dialW(domElt);
widgetsLayout->addWidget(f);
} else if (type == "Slider") {
SliderW* f = new SliderW(domElt);
widgetsLayout->addWidget(f);
} else if (type == "Button") {
ButtonW* f = new ButtonW(domElt);
widgetsLayout->addWidget(f);
}
// else if (type == "SigGen") {
// siggenW* f = new siggenW(domElt);
// widgetsLayout->addWidget(f);
// }
}
void widgetsWindow::doXml() {
// remove all widgets
widgetsLayout->cleanWidgets();
// _________________________
// ::: Read the root tag :::
// Extract the root markup
QDomElement root = xmlWidgets.documentElement();
// Get root names and attributes
QString Type = root.tagName();
// _________________
// ::: Read data :::
// Get the first child of the root (Markup COMPONENT is expected)
QDomElement Component = root.firstChild().toElement();
// Loop while there is a child
while(!Component.isNull()) {
// Check if the child tag name is WIDGET
if (Component.tagName() == "WIDGET") {
// Read and display the Widget ID
QString type = Component.attribute("TYPE","");
// Get the first child of the component
QDomElement Child = Component.firstChild().toElement();
if (!Child.isNull()) {
createWidget(type, &Child);
}
}
// Next widget
Component = Component.nextSibling().toElement();
}
}
void widgetsWindow::on_actionLoad_triggered() {
if (openXml()) {
doXml();
}
}
void widgetsWindow::on_actionRemove_All_triggered() {
// remove all widgets
widgetsLayout->cleanWidgets();
}
void widgetsWindow::on_actionAdd_SliderWidget_triggered() {
SliderW* f = new SliderW();
widgetsLayout->addWidget(f);
}
void widgetsWindow::on_actionAdd_Dial_Widget_triggered() {
dialW* f = new dialW();
widgetsLayout->addWidget(f);
}
void widgetsWindow::on_actionButton_triggered() {
ButtonW* f = new ButtonW();
widgetsLayout->addWidget(f);
}
//void widgetsWindow::on_actionSignal_Generator_triggered() {
// siggenW* f = new siggenW();
// widgetsLayout->addWidget(f);
//}