#include <QApplication>
#include "instructiongen.h"
int main(int argc, char *argv[])
{
InstructionGen generator;
generator.show();
return a.exec();
}
#include <QApplication>
#include "instructiongen.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
InstructionGen generator;
generator.show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
#include "instructiongen.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QGridLayout>
#include <QPushButton>
#include <QTextEdit>
#include <QLabel>
#include <QLineEdit>
#include <QTcpSocket>
#include <QBuffer>
#include <QSpinBox>
#include <QErrorMessage>
static const quint16 DEFAULT_PORT = 15001;
InstructionGen
::InstructionGen(QWidget* parent, Qt
::WFlags flags
){
port->setRange(1, 32767);
port->setValue(DEFAULT_PORT);
server->setText("localhost");
top->addWidget(label, 0, 0);
top->addWidget(server, 0, 1);
top->addWidget(port, 0, 2);
label
= new QLabel("Prefix:",
this);
prefix->setText("sim.");
top->addWidget(label, 1, 0);
top->addWidget(prefix, 1, 1);
top->addWidget(conn, 1, 2);
log_stat->setReadOnly(true);
label
= new QLabel("Instruction:",
this);
send->setDefault(true);
bottom->addWidget(label);
bottom->addWidget(message);
bottom->addWidget(send);
main->addLayout(top);
main->addWidget(log_stat);
main->addLayout(bottom);
setLayout(main);
connect(message, SIGNAL(returnPressed()), SLOT(sendInstruction()));
connect(send, SIGNAL(clicked()), SLOT(sendInstruction()));
connect(conn, SIGNAL(clicked()), SLOT(toggleConnection()));
connect(socket, SIGNAL(connected()), SLOT(setConnected()));
connect(socket, SIGNAL(disconnected()), SLOT(setDisconnected()));
connect(socket, SIGNAL(readyRead()), SLOT(receiveMessage()));
setDisconnected();
}
InstructionGen::~InstructionGen()
{
buffer->close();
}
void InstructionGen::setConnected()
{
port->setEnabled(false);
server->setEnabled(false);
prefix->setEnabled(true);
message->setEnabled(true);
log_stat->setEnabled(true);
log_stat->clear();
send->setEnabled(true);
conn->setText("Disconnect");
}
void InstructionGen::setDisconnected()
{
port->setEnabled(true);
server->setEnabled(true);
prefix->setEnabled(false);
message->setEnabled(false);
log_stat->setEnabled(false);
send->setEnabled(false);
conn->setText("Connect");
}
void InstructionGen::toggleConnection()
{
{
socket->connectToHost(server->text(), port->value());
}
else
{
socket->disconnectFromHost();
}
}
void InstructionGen::sendInstruction()
{
socket->write(prefix->text().toLatin1() + message->text().toLatin1() + "\n");
log_stat->append((prefix->text().toLatin1() + message->text().toLatin1() + "\n").simplified());
message->clear();
}
void InstructionGen::receiveMessage()
{
// missing some checks for returns values for the sake of simplicity
qint64 bytes = buffer->write(socket->readAll());
// go back as many bytes as we just wrote so that it can be read
buffer->seek(buffer->pos() - bytes);
// read only full lines, line by line
while (buffer->canReadLine())
{
log_stat->append("received: "+line.simplified());
}
}
#include "instructiongen.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QGridLayout>
#include <QPushButton>
#include <QTextEdit>
#include <QLabel>
#include <QLineEdit>
#include <QTcpSocket>
#include <QBuffer>
#include <QSpinBox>
#include <QErrorMessage>
static const quint16 DEFAULT_PORT = 15001;
InstructionGen::InstructionGen(QWidget* parent, Qt::WFlags flags)
: QWidget(parent, flags)
{
QVBoxLayout* main = new QVBoxLayout(this);
QGridLayout* top = new QGridLayout;
QHBoxLayout* bottom = new QHBoxLayout;
QLabel* label = new QLabel("Server:", this);
server = new QLineEdit(this);
port = new QSpinBox(this);
conn = new QPushButton("Connect", this);
port->setRange(1, 32767);
port->setValue(DEFAULT_PORT);
server->setText("localhost");
top->addWidget(label, 0, 0);
top->addWidget(server, 0, 1);
top->addWidget(port, 0, 2);
label = new QLabel("Prefix:", this);
prefix = new QLineEdit(this);
prefix->setText("sim.");
top->addWidget(label, 1, 0);
top->addWidget(prefix, 1, 1);
top->addWidget(conn, 1, 2);
log_stat = new QTextEdit(this);
log_stat->setReadOnly(true);
label = new QLabel("Instruction:", this);
message = new QLineEdit(this);
send = new QPushButton("Send", this);
send->setDefault(true);
bottom->addWidget(label);
bottom->addWidget(message);
bottom->addWidget(send);
main->addLayout(top);
main->addWidget(log_stat);
main->addLayout(bottom);
setLayout(main);
buffer = new QBuffer(this);
socket = new QTcpSocket(this);
buffer->open(QIODevice::ReadWrite);
connect(message, SIGNAL(returnPressed()), SLOT(sendInstruction()));
connect(send, SIGNAL(clicked()), SLOT(sendInstruction()));
connect(conn, SIGNAL(clicked()), SLOT(toggleConnection()));
connect(socket, SIGNAL(connected()), SLOT(setConnected()));
connect(socket, SIGNAL(disconnected()), SLOT(setDisconnected()));
connect(socket, SIGNAL(readyRead()), SLOT(receiveMessage()));
setDisconnected();
}
InstructionGen::~InstructionGen()
{
buffer->close();
}
void InstructionGen::setConnected()
{
port->setEnabled(false);
server->setEnabled(false);
prefix->setEnabled(true);
message->setEnabled(true);
log_stat->setEnabled(true);
log_stat->clear();
send->setEnabled(true);
conn->setText("Disconnect");
}
void InstructionGen::setDisconnected()
{
port->setEnabled(true);
server->setEnabled(true);
prefix->setEnabled(false);
message->setEnabled(false);
log_stat->setEnabled(false);
send->setEnabled(false);
conn->setText("Connect");
}
void InstructionGen::toggleConnection()
{
if (socket->state() == QAbstractSocket::UnconnectedState)
{
socket->connectToHost(server->text(), port->value());
}
else
{
socket->disconnectFromHost();
}
}
void InstructionGen::sendInstruction()
{
socket->write(prefix->text().toLatin1() + message->text().toLatin1() + "\n");
log_stat->append((prefix->text().toLatin1() + message->text().toLatin1() + "\n").simplified());
message->clear();
}
void InstructionGen::receiveMessage()
{
// missing some checks for returns values for the sake of simplicity
qint64 bytes = buffer->write(socket->readAll());
// go back as many bytes as we just wrote so that it can be read
buffer->seek(buffer->pos() - bytes);
// read only full lines, line by line
while (buffer->canReadLine())
{
QString line = buffer->readLine();
log_stat->append("received: "+line.simplified());
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks