-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmainwindow.cpp
115 lines (99 loc) · 3.47 KB
/
mainwindow.cpp
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
/*************************************************************************
**
** (C) Copyright 2013 Reach Technology Inc.
**
** This code is protected by international copyright laws. This file may
** only be used in accordance with a license and cannot be used on
** hardware other than supplied by Reach Technology Inc. We appreciate
** your understanding and fairness.
**
*************************************************************************/
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QDeclarativeView(parent), m_socket(new QLocalSocket(this)), m_connectTimer(new QTimer(this))
{
this->setSource(QUrl::fromLocalFile("/home/reach/Projects/reach-apps/demo-qml/content/mainview.qml" ) );
this->setResizeMode(QDeclarativeView::SizeRootObjectToView);
this->rootContext()->setContextProperty("connection", this);
connect(m_socket,SIGNAL(connected()),this,SLOT(onSocketConnected()));
connect(m_socket,SIGNAL(disconnected()),this,SLOT(onSocketDisconnected()));
connect(m_socket,SIGNAL(readyRead()),this,SLOT(onSocketReadyRead()));
connect(m_socket,SIGNAL(stateChanged(QLocalSocket::LocalSocketState)),this,SLOT(onSocketStateChange(QLocalSocket::LocalSocketState)));
connect(m_connectTimer, SIGNAL(timeout()), SLOT(onConnectTimerTimeout()));
}
MainWindow::~MainWindow()
{
qDebug() << "[QML] mainwindow destructor";
if(m_socket->isOpen()) {
qDebug() << "[QML] closing socket";
m_socket->write("goodbye from Qt");
m_socket->flush();
m_socket->close();
}
delete m_socket;
}
void MainWindow::init()
{
tryConnect();
}
void MainWindow::sendMessage(const QString &message)
{
m_socket->write(message.toLatin1());
}
void MainWindow::onSocketConnected()
{
qDebug() << "[QML] socket connected";
m_connectTimer->stop();
m_socket->write("hello from Qt");
}
void MainWindow::onSocketDisconnected()
{
qDebug() << "[QML] socket disconnected";
m_connectTimer->start(5000);
}
void MainWindow::onSocketError(QLocalSocket::LocalSocketError socketError)
{
switch (socketError) {
case QLocalSocket::ServerNotFoundError:
qDebug() << "[QML] QLocalSocket::ServerNotFoundError";
break;
case QLocalSocket::ConnectionRefusedError:
qDebug() << "[QML] QLocalSocket::ConnectionRefusedError";
break;
case QLocalSocket::PeerClosedError:
break;
default:
qDebug() << m_socket->errorString();
}
}
void MainWindow::onSocketReadyRead()
{
qDebug() << "[QML] socket ready read";
}
void MainWindow::onSocketStateChange(QLocalSocket::LocalSocketState socketState)
{
switch(socketState) {
case QLocalSocket::UnconnectedState:
qDebug() << "[QML] socket state UnconnectedState"; break;
case QLocalSocket::ConnectingState:
qDebug() << "[QML] socket state ConnectingState"; break;
case QLocalSocket::ConnectedState:
qDebug() << "[QML] socket state ConnectedState"; break;
case QLocalSocket::ClosingState:
qDebug() << "[QML] socket state ClosingState"; break;
default:
qDebug() << "[QML] unknow state"; break;
}
}
void MainWindow::tryConnect()
{
m_connectTimer->stop();
m_socket->connectToServer("/tmp/demo_socket",QIODevice::ReadWrite);
if (!m_socket->waitForConnected(1000)) {
m_connectTimer->start(5000);
}
}
void MainWindow::onConnectTimerTimeout()
{
tryConnect();
}