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) { Client::Client(QObject *parent) : QObject(parent) {
socket = new QTcpSocket(nullptr); socket = new QTcpSocket(nullptr);
socket->connectToHost("localhost", 65432);
socket->waitForConnected();
std::cout << "Connected" << std::endl;
} }
Client::~Client() { Client::~Client() {
socket->close(); 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) { void Client::send(QString data) {
std::cout << "SENDING MESSAGE: \"" << data.toStdString() << "\"" << std::endl; std::cout << "SENDING MESSAGE: \"" << data.toStdString() << "\"" << std::endl;
QByteArray dataStream = data.toUtf8(); QByteArray dataStream = data.toUtf8();

View file

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

119
main.qml
View file

@ -9,19 +9,21 @@ Window {
property alias freqText: freqText property alias freqText: freqText
property alias dial: dial property alias dial: dial
title: qsTr("Radio Tuner") title: qsTr("Radio Tuner")
property var initialFreq : { property var initialFreq : 0
return client.getFrequency() / 100000;
}
Dial { Connections {
id: dial target: dial
x: 74 onMoved: {
y: 248 var dialNumber = (dial.value / 10)
font.pointSize: 10 var dialString = (dialNumber < 100) ? dialNumber.toString().substring(0, 4) : dialNumber.toString().substring(0, 5)
stepSize: 1 if (client.isConnected()) {
to: 1080 freqText.text = qsTr("Tuned To: " + dialString)
from: 875 dialNumber = parseFloat(dialString)
value: initialFreq console.log("The radio then dials to: " + dialNumber + "MHz")
client.setFrequency((dialNumber * 1000000))
}
}
} }
Text { Text {
@ -32,28 +34,87 @@ Window {
font.pixelSize: 32 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 { Text {
id: freqText id: freqText
x: 108 x: 108
y: 197 y: 197
text: { text: ""
var dialNumber = initialFreq / 10;
var dialString = (dialNumber < 100) ? dialNumber.toString().substring(0, 4) : dialNumber.toString().substring(0, 5)
return "Tuned To: " + dialString
}
font.pixelSize: 24 font.pixelSize: 24
visible: false
} }
Connections {
target: dial
onMoved: {
var dialNumber = (dial.value / 10) Dial {
var dialString = (dialNumber < 100) ? dialNumber.toString().substring(0, 4) : dialNumber.toString().substring(0, 5) id: dial
freqText.text = qsTr("Tuned To: " + dialString) x: 74
dialNumber = parseFloat(dialString) y: 248
console.log("The radio then dials to: " + dialNumber + "MHz") font.pointSize: 10
client.setFrequency((dialNumber * 1000000)) stepSize: 1
to: 1080
from: 875
value: initialFreq
visible: false
}
} }
}
}