Added connect screen without error checking

This commit is contained in:
Brandon Rozek 2019-05-15 21:20:40 -04:00
parent 86b0d07829
commit fa05c63324
3 changed files with 105 additions and 31 deletions

View file

@ -6,15 +6,25 @@
Client::Client(QObject *parent) : QObject(parent) {
socket = new QTcpSocket(nullptr);
socket->connectToHost("localhost", 65432);
socket->waitForConnected();
std::cout << "Connected" << std::endl;
}
Client::~Client() {
socket->close();
}
void Client::connect(QString hostname, int port) {
assert (port > 0);
socket->connectToHost(hostname, port);
socket->waitForConnected();
std::cout << "Connected" << std::endl;
this->connected = true;
}
bool Client::isConnected() {
return this->connected;
}
void Client::send(QString data) {
std::cout << "SENDING MESSAGE: \"" << data.toStdString() << "\"" << std::endl;
QByteArray dataStream = data.toUtf8();

View file

@ -13,8 +13,11 @@ public:
QString recv(void);
Q_INVOKABLE void setFrequency(double freq);
Q_INVOKABLE double getFrequency(void);
Q_INVOKABLE void connect(QString host, int port);
Q_INVOKABLE bool isConnected();
private:
QTcpSocket *socket = nullptr;
bool connected = false;
};
#endif // CLIENT_H

117
main.qml
View file

@ -9,19 +9,21 @@ Window {
property alias freqText: freqText
property alias dial: dial
title: qsTr("Radio Tuner")
property var initialFreq : {
return client.getFrequency() / 100000;
}
property var initialFreq : 0
Dial {
id: dial
x: 74
y: 248
font.pointSize: 10
stepSize: 1
to: 1080
from: 875
value: initialFreq
Connections {
target: dial
onMoved: {
var dialNumber = (dial.value / 10)
var dialString = (dialNumber < 100) ? dialNumber.toString().substring(0, 4) : dialNumber.toString().substring(0, 5)
if (client.isConnected()) {
freqText.text = qsTr("Tuned To: " + dialString)
dialNumber = parseFloat(dialString)
console.log("The radio then dials to: " + dialNumber + "MHz")
client.setFrequency((dialNumber * 1000000))
}
}
}
Text {
@ -32,28 +34,87 @@ Window {
font.pixelSize: 32
}
TextField {
id: hostname
x: 66
y: 112
text: qsTr("Host Name / IP Address of Radio")
}
TextField {
id: port
x: 66
y: 158
text: qsTr("Port")
}
Button {
id: connectBtn
x: 66
y: 214
text: qsTr("Connect")
onClicked: {
connDetails.text = "";
connDetails.visible = false;
var hostnameVal = hostname.text;
var portVal = parseInt(port.text);
if (Number.isNaN(portVal) && portVal > 0) {
connDetails.text = "Port value must be a positive number.";
connDetails.visible = true;
return;
}
client.connect(qsTr(hostnameVal), portVal);
// Ideally we would check for success here... But for now assume
hostname.visible = false; port.visible = false; connectBtn.visible = false;
dial.visible = true; freqText.visible = true;
// Display initial frequency
initialFreq = client.getFrequency() / 100000;
var dialNumber = initialFreq / 10;
var dialString = (dialNumber < 100) ? dialNumber.toString().substring(0, 4) : dialNumber.toString().substring(0, 5);
freqText.text = "Tuned To: " + dialString;
dial.value = initialFreq;
}
}
Text {
id: connDetails
x: 66
y: 271
width: 116
height: 27
text: qsTr("Connection Details")
font.pixelSize: 12
visible: false
}
Text {
id: freqText
x: 108
y: 197
text: {
var dialNumber = initialFreq / 10;
var dialString = (dialNumber < 100) ? dialNumber.toString().substring(0, 4) : dialNumber.toString().substring(0, 5)
return "Tuned To: " + dialString
}
text: ""
font.pixelSize: 24
visible: false
}
Connections {
target: dial
onMoved: {
var dialNumber = (dial.value / 10)
var dialString = (dialNumber < 100) ? dialNumber.toString().substring(0, 4) : dialNumber.toString().substring(0, 5)
freqText.text = qsTr("Tuned To: " + dialString)
dialNumber = parseFloat(dialString)
console.log("The radio then dials to: " + dialNumber + "MHz")
client.setFrequency((dialNumber * 1000000))
}
Dial {
id: dial
x: 74
y: 248
font.pointSize: 10
stepSize: 1
to: 1080
from: 875
value: initialFreq
visible: false
}
}