summaryrefslogtreecommitdiffstats
path: root/src/client/toolbar/toolbar.cpp
blob: bfe4e24ecd48826983210a5ee0e825f8535d11eb (plain) (blame)
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
 * toolbar.cpp
 *
 *  Created on: 21.01.2013
 *      Author: sr
 */

#include "../../shared/settings.h"
#include "../net/serverconnection.h"
#include "../vnc/vncwindow.h"
#include "../vnc/vncserver.h"

#include "toolbar.h"
#include "ui_toolbar.h"

/***************************************************************************//**
 * Constructor of the Toolbar. Constructs a widget which is a child of parent.
 * Initialize the GUI and sets up the menu, sets window properties, create the
 * VNC- and connect-window, connects the necessary signals, sets the UI's
 * position and configuires the timer for the UI to be hidden.
 * @param parent If parent is 0, the new widget becomes a window. If parent is
 * another widget, this widget becomes a child window inside parent. The new
 * widget is deleted when its parent is deleted.
 */
Toolbar::Toolbar(QWidget *parent) :
	QWidget(parent), _ui(new Ui::Toolbar), _hideTimer(this), _connection(NULL),
	_blinkTimer(this),_cam32(":cam32.svg"),	_beWatchedEye(":eye")
{
	/* Initialize the GUI */
	_ui->setupUi(this);

	/* Set window properties */
	setWindowFlags(Qt::WindowStaysOnTopHint | Qt::X11BypassWindowManagerHint | Qt::FramelessWindowHint);
	setAttribute(Qt::WA_AlwaysShowToolTips);
	setAttribute(Qt::WA_QuitOnClose);
	setVisible(true);

	/* Create the VNC Window */
	_vnc = new VncWindow(NULL);

	/* Create the connect window */
	_connectWindow = new ConnectWindow(NULL);
	// Connect the signals
	connect(_connectWindow, SIGNAL(disconnect()), this, SLOT(onDoDisconnect()));
	connect(_connectWindow, SIGNAL(connected(ServerConnection*)), this, SLOT(onConnected(ServerConnection*)));

	/* Setup menu */
	_menu = new QMenu(this);
	_acnDisconnect = new QAction(tr("Set &session ID"), this);
	_acnQuit = new QAction(tr("&Quit"), this);
	_menu->addAction(_acnDisconnect);
	_menu->addSeparator();
	_menu->addAction(_acnQuit);
	_ui->cmdMenu->setMenu(_menu);
	// Connect the signals
	connect(_menu, SIGNAL(aboutToHide()), this, SLOT(hideBar()));
	connect(_acnDisconnect, SIGNAL(triggered()), _connectWindow, SLOT(show()));
	connect(_acnQuit, SIGNAL(triggered()), qApp, SLOT(quit()));

	/* Connect the signals from vnc server */
	connect(VncServer::instance(), SIGNAL(started(int, QString&, QString&)), this, SLOT(onVncServerIsRunning(int)));

	/* Set position */
	const QDesktopWidget desktop;
	const QRect primaryScreen = desktop.screenGeometry();
	move(primaryScreen.left() + (primaryScreen.width() - this->width())/2 , primaryScreen.top());
	qDebug() << primaryScreen.left() << primaryScreen.top() << primaryScreen.right() << primaryScreen.bottom();

	/* Setup hide timer */
	_hideTimer.setInterval(500);
	_hideTimer.setSingleShot(true);
	connect(&_hideTimer, SIGNAL(timeout()), this, SLOT(hideBar()));
	_hideTimer.start(); // initially show PVS and hide later

	/* Setup blink timer */
	_blinkTimer.setInterval(500);
	connect(&_blinkTimer, SIGNAL(timeout()), this, SLOT(cameraBlink()));
}


/***************************************************************************//**
 * Destructor of the Toolbar. Destroys the widget. All this widget's children
 * are deleted first.
 */
Toolbar::~Toolbar()
{
	VncServer::instance()->stop();
	_vnc->deleteLater();
	_connectWindow->deleteLater();
	delete _ui;
}

/*
 * Override
 */

/***************************************************************************//**
 * This event is reimplemented to receive widget leave events. When the mouse
 * cursor leaves the widget, a timer gets started which, when timed out, hides
 * the Toolbar.
 * @param e The leave event (Mouse leaves widget's boundaries.)
 */
void Toolbar::leaveEvent(QEvent* e)
{
	_hideTimer.start();
	QWidget::leaveEvent(e);
}


/***************************************************************************//**
 * This event is reimplemented to receive widget enter events. When the mouse
 * cursor enters the widget, the timer  which, when timed out, hides
 * the Toolbar, gets stopped.
 * @param e The enter event (Mouse enters widget's boundaries.)
 */
void Toolbar::enterEvent(QEvent* e)
{
	_hideTimer.stop();
	showBar();
	QWidget::enterEvent(e);
}

/*
 * Slots
 */

/***************************************************************************//**
 * A slot for changing the camera icon. This slot should be called permanently
 * if the vnc server is recording  the screen.
 */
void Toolbar::cameraBlink()
{
	static bool showEye = false;
	if (!showEye)
	{
		_ui->icon_cam->setPixmap(_beWatchedEye);
		showEye = true;
	}
	else
	{
		_ui->icon_cam->setPixmap(QPixmap()); // set empty pixmap for blinking effect
		showEye = false;
	}
}

/***************************************************************************//**
 * A slot for the VncServerIsRunning signal. This slot will change the UI
 * according to the state fo the VncServer.
 * @param[in] port Indicates the state of the VncServer.
 */
void Toolbar::onVncServerIsRunning(int port)
{
	if (port > 0) {
		_blinkTimer.start();
		_ui->lblStatus->setStyleSheet("color:red");
		_ui->lblStatus->setText(tr("Streaming"));
		showBar();
	}	else {
		_blinkTimer.stop();
		_ui->icon_cam->setPixmap(_cam32);
		_ui->lblStatus->setStyleSheet("color:green");
		_ui->lblStatus->setText(tr("Online"));
		hideBar();
	}
}

/***************************************************************************//**
 * A slot for the onDisconnected signal of the ConnectWindow. This slot will
 * change the UI according to the state fo the connection.
 */
void Toolbar::onDisconnected()
{
	_connectWindow->setConnected(false);
		if (_connection != NULL)
		_connection->blockSignals(true);
	_connection = NULL;
	_ui->lblStatus->setStyleSheet("color:red");
	_ui->lblStatus->setText(tr("Offline"));
}

/***************************************************************************//**
 * A slot for the onConnected signal of the ConnectWindow. This slot will
 * change the UI according to the state of the connection and connect the
 * relevant signals and slots.
 * @param connection Pointer to the ServerConnection
 */
void Toolbar::onConnected(ServerConnection* connection)
{
	_ui->lblStatus->setStyleSheet("color:green");
	_ui->lblStatus->setText(tr("Online"));
	//
	if (_connection != NULL)
	{
		disconnect(_connection, SIGNAL(disconnected()), this, SLOT(onDisconnected()));
		_connection->blockSignals(true);
		_connection->disconnectFromServer();
	}
	_connection = connection;
	connect(_connection, SIGNAL(disconnected()), this, SLOT(onDisconnected()));
	connect(_connection, SIGNAL(openVnc(const QString&, int, const QString&, bool, bool, const QString&, const int)),
		_vnc, SLOT(open(const QString&, int, const QString&, bool, bool, const QString&, const int)));
	connect(_connection, SIGNAL(closeVnc()), _vnc, SLOT(close()));
	connect(_vnc, SIGNAL(running(const bool, const int)), _connection, SLOT(onVncViewerStartStop(const bool, const int)));
	_connectWindow->setConnected(true);
}

/***************************************************************************//**
 *
 */
void Toolbar::onDoDisconnect()
{
	if (_connection != NULL)
		_connection->disconnectFromServer();
}

/***************************************************************************//**
 * This slot hides the toolbar. Places the toolbar hidden behind the edge of the
 * screen just showing 2 pixels.
 */
void Toolbar::hideBar()
{
	// Don't hide window if any menu is open or VNC Server is running from this client.
	if (_menu->isVisible() || VncServer::instance()->isVncServerRunning())
		return;
	const QDesktopWidget desktop;
	const QRect primaryScreen = desktop.screenGeometry();
	move(x(), primaryScreen.top() + 2 - height());
}

/***************************************************************************//**
 * This slot shows the toolbar. Used after a hideBar().
 */
void Toolbar::showBar()
{
	const QDesktopWidget desktop;
	const QRect primaryScreen = desktop.screenGeometry();
	move(x(), primaryScreen.top());
}