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
|
/*
* Copyright (c) 2012 Yoann Blein
* Licensed under the simplified BSD license.
* See Documentation/Licenses/BSD-simplified.txt for more information.
*/
#include "RemoteScreenViewerWidget.h"
#include <QResizeEvent>
#include <QPainter>
#include <boost/bind.hpp>
#include <Swiften/ScreenSharing/IncomingScreenSharing.h>
#include <Swiften/ScreenSharing/Image.h>
namespace Swift {
RemoteScreenViewerWidget::RemoteScreenViewerWidget(boost::shared_ptr<IncomingScreenSharing> incScreenSharing, QWidget *parent) :
QWidget(parent), iss(incScreenSharing)
{
setMouseTracking(true);
setFocusPolicy(Qt::StrongFocus);
iss->onNewImageReceived.connect(boost::bind(&RemoteScreenViewerWidget::handleNewImageReceived, this, _1));
}
RemoteScreenViewerWidget::~RemoteScreenViewerWidget()
{
iss->onNewImageReceived.disconnect(boost::bind(&RemoteScreenViewerWidget::handleNewImageReceived, this, _1));
}
void RemoteScreenViewerWidget::paintEvent(QPaintEvent*)
{
if (!pixmap.isNull()) {
QPainter painter(this);
painter.translate(geometry().center());
painter.drawPixmap(-pixmap.rect().center(), pixmap);
}
}
void RemoteScreenViewerWidget::resizeEvent(QResizeEvent* event)
{
if (!pixmap.isNull()) {
pixmap = pixmap.scaled(event->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
}
QWidget::resizeEvent(event);
}
void RemoteScreenViewerWidget::keyPressEvent(QKeyEvent* event)
{
InputEventPayload::Event ev(InputEventPayload::Event::KeyPress, event->key());
iss->sendInputEvent(ev);
QWidget::keyPressEvent(event);
}
void RemoteScreenViewerWidget::keyReleaseEvent(QKeyEvent* event)
{
InputEventPayload::Event ev(InputEventPayload::Event::KeyRelease, event->key());
iss->sendInputEvent(ev);
QWidget::keyReleaseEvent(event);
}
void RemoteScreenViewerWidget::mouseMoveEvent(QMouseEvent* event)
{
float x = static_cast<float>(event->x()) / pixmap.width();
float y = static_cast<float>(event->y()) / pixmap.height();
InputEventPayload::Event ev(InputEventPayload::Event::MouseMove, x, y);
iss->sendInputEvent(ev);
QWidget::mouseMoveEvent(event);
}
void RemoteScreenViewerWidget::mousePressEvent(QMouseEvent* event)
{
int btnId = 0;
switch (event->button()) {
case Qt::LeftButton:
btnId = 3;
break;
case Qt::MidButton:
btnId = 2;
break;
case Qt::RightButton:
btnId = 1;
break;
default:
break;
}
if (btnId) {
InputEventPayload::Event ev(InputEventPayload::Event::MousePress, btnId);
iss->sendInputEvent(ev);
}
QWidget::mousePressEvent(event);
}
void RemoteScreenViewerWidget::mouseReleaseEvent(QMouseEvent* event)
{
int btnId = 0;
switch (event->button()) {
case Qt::LeftButton:
btnId = 3;
break;
case Qt::MidButton:
btnId = 2;
break;
case Qt::RightButton:
btnId = 1;
break;
default:
break;
}
if (btnId) {
InputEventPayload::Event ev(InputEventPayload::Event::MouseRelease, btnId);
iss->sendInputEvent(ev);
}
QWidget::mouseReleaseEvent(event);
}
void RemoteScreenViewerWidget::wheelEvent(QWheelEvent* event)
{
InputEventPayload::Event ev(InputEventPayload::Event::MouseWheel, event->delta());
iss->sendInputEvent(ev);
QWidget::wheelEvent(event);
}
void RemoteScreenViewerWidget::handleNewImageReceived(const Image& image)
{
QImage qImg(image.data.data(), image.width, image.height, QImage::Format_RGB888);
pixmap = QPixmap::fromImage(qImg).scaled(size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
update();
}
}
|