1
0
mirror of https://github.com/binary-kitchen/doorlockd synced 2024-12-22 18:34:25 +01:00

Improved GUI

This commit is contained in:
Ralf Ramsauer 2015-09-29 02:13:48 +02:00
parent a45fc4ca33
commit 4366e117d1
2 changed files with 41 additions and 66 deletions

View File

@ -36,33 +36,18 @@ static volatile bool run = true;
std::unique_ptr<MainWindow> mainWindow = nullptr; std::unique_ptr<MainWindow> mainWindow = nullptr;
static void doorlock_update(const Clientmessage &msg) static void onDoorlockUpdate(const Clientmessage &msg)
{ {
l("Updated to current token: " + msg.token(), LogLevel::info);
if (mainWindow) { if (mainWindow) {
mainWindow->setQRCode(QString::fromStdString(msg.token())); mainWindow->setQRCode(msg.token());
mainWindow->setLabel(QString::fromStdString(msg.token()));
} }
} }
static int startGui(int argc, char** argv)
{
QApplication app(argc, argv);
app.setOrganizationName("Binary Kitchen");
app.setApplicationName("doorlock-client");
mainWindow = std::unique_ptr<MainWindow>(new MainWindow);
int retval = app.exec();
run = false;
return retval;
}
static int doorlock_client(const std::string &hostname, static int doorlock_client(const std::string &hostname,
const unsigned short port) const unsigned short port)
{ {
int retval = 0; int retval = 0;
std::thread guiThread;
try { try {
tcp::resolver resolver(io_service); tcp::resolver resolver(io_service);
@ -87,28 +72,13 @@ static int doorlock_client(const std::string &hostname,
throw boost::system::system_error(error); throw boost::system::system_error(error);
data.resize(SOCKET_BUFFERLENGTH); data.resize(SOCKET_BUFFERLENGTH);
length = socket.read_some(boost::asio::buffer(data), error);
if (error)
throw boost::system::system_error(error);
const auto response = Response::fromJson(std::string(data.begin(), data.begin()+length));
if (!response)
throw std::runtime_error("Invalid response from server");
guiThread = std::thread([] () {
startGui(0, nullptr);
});
for (;run;) { for (;run;) {
length = socket.read_some(boost::asio::buffer(data), error); length = socket.read_some(boost::asio::buffer(data), error);
if (error) if (error)
throw boost::system::system_error(error); throw boost::system::system_error(error);
const auto message = Clientmessage::fromJson(std::string(data.begin(), data.begin()+length)); const auto message = Clientmessage::fromJson(std::string(data.begin(), data.begin()+length));
if (!response) onDoorlockUpdate(message);
throw std::runtime_error("Invalid response from server");
doorlock_update(message);
} }
} }
catch(const std::exception &e) catch(const std::exception &e)
@ -118,15 +88,11 @@ static int doorlock_client(const std::string &hostname,
} }
out: out:
if (guiThread.joinable()) {
guiThread.join();
}
return retval; return retval;
} }
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
int retval = -1;
std::string hostname; std::string hostname;
unsigned short port; unsigned short port;
@ -151,8 +117,7 @@ int main(int argc, char** argv)
if (vm.count("help")) if (vm.count("help"))
{ {
std::cout << desc << std::endl; std::cout << desc << std::endl;
retval = 0; exit(-1);
goto out;
} }
po::notify(vm); po::notify(vm);
@ -160,14 +125,33 @@ int main(int argc, char** argv)
catch(const std::exception &e) catch(const std::exception &e)
{ {
l(LogLevel::error, e.what()); l(LogLevel::error, e.what());
goto out; exit(-1);
} }
l(LogLevel::notice, "Starting doorlock-client"); l(LogLevel::notice, "Starting doorlock-client");
doorlock_client(hostname, port); QApplication app(argc, argv);
app.setOrganizationName("Binary Kitchen");
app.setApplicationName("doorlock-client");
mainWindow = std::unique_ptr<MainWindow>(new MainWindow);
mainWindow->showFullScreen();
// Start the TCP client as thread
std::thread clientThread = std::thread([&] () {
// If the TCP client returns, an error has occured
// In normal operation, it never returns
doorlock_client(hostname, port);
// Stop the QT application
app.quit();
});
// This routine will never return in normal operation
app.exec();
clientThread.join();
out:
l(LogLevel::notice, "Stopping doorlock-client"); l(LogLevel::notice, "Stopping doorlock-client");
return retval; return 0;
} }

View File

@ -2,52 +2,43 @@
#define MAINWINDOW_H #define MAINWINDOW_H
#include <QApplication> #include <QApplication>
#include <QVBoxLayout> #include <QMainWindow>
#include <QLabel> #include <QStatusBar>
#include <QHBoxLayout>
#include "qrwidget.h" #include "qrwidget.h"
class MainWindow : public QWidget class MainWindow : public QMainWindow
{ {
public: public:
MainWindow() : MainWindow() :
QWidget() QMainWindow()
{ {
_qrWidget = new QRWidget; _qrWidget = new QRWidget;
_layout = new QVBoxLayout; statusBar()->showMessage(tr("Boofar"));
_layout->addWidget(_qrWidget);
_label = new QLabel; QHBoxLayout *layout = new QHBoxLayout;
_label->setText("Hello, world!");
QFont font("Times", 25, QFont::Bold); layout->addWidget(_qrWidget);
_label->setFont(font);
_layout->addWidget(_label);
setLayout(_layout); QWidget* window = new QWidget;
showFullScreen(); window->setLayout(layout);
setCentralWidget(window);
} }
MainWindow(const MainWindow&); MainWindow(const MainWindow&);
MainWindow& operator =(const MainWindow&); MainWindow& operator =(const MainWindow&);
void setQRCode(const QString &str) void setQRCode(const std::string &str)
{ {
_qrWidget->setQRData(str); _qrWidget->setQRData(str);
} }
void setLabel(const QString &str)
{
_label->setText(str);
}
private: private:
QRWidget* _qrWidget = { nullptr }; QRWidget* _qrWidget = { nullptr };
QVBoxLayout* _layout = { nullptr };
QLabel* _label = { nullptr };
}; };
#endif #endif