/
phonedialog.cpp
161 lines (133 loc) · 4.82 KB
/
phonedialog.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
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
#include "phonedialog.h"
#include "ui_phonedialog.h"
PhoneDialog::PhoneDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::PhoneDialog)
{
ui->setupUi(this);
ui->scrollArea->setVisible(false);
if (QNetworkInterface::allAddresses().count() < 3) {
ui->ipLabel->setText("Connect to the internet.");
} else {
ui->ipLabel->setText(QNetworkInterface::allAddresses().at(2).toString());
server = new QTcpServer(this);
server->listen(QHostAddress::Any, 26157);
connect(server, SIGNAL(newConnection()), this, SLOT(newConnection()));
}
}
PhoneDialog::~PhoneDialog()
{
delete ui;
}
void PhoneDialog::keyPressEvent(QKeyEvent *event) {
if (event->key() == Qt::Key_Escape) {
this->close();
}
}
void PhoneDialog::resizeEvent(QResizeEvent *event) {
}
void PhoneDialog::newConnection() {
QTcpSocket* socket = server->nextPendingConnection();
//server->close();
sockets.append(socket);
buffers.append(QByteArray());
connect(socket, &QTcpSocket::readyRead, [=]() {
int index = sockets.indexOf(socket);
QByteArray buffer = buffers.at(sockets.indexOf(socket));
buffer.append(socket->readAll());
if (buffer.endsWith("\r\n")) {
if (buffer.endsWith("ENDSTREAM\r\n")) {
buffers.removeAt(sockets.indexOf(socket));
sockets.removeOne(socket);
broadcastNumber();
if (sockets.count() == 0) {
this->close();
}
return;
} else {
//Reply with OK
socket->write("OK\r\n");
socket->flush();
ui->scrollArea->setVisible(true);
ui->connectionFrame->setVisible(false);
QDir::home().mkdir("thePhoto");
QFile image(QDir::homePath() + "/thePhoto/Image_" + QDateTime::currentDateTime().toString("ddMMyy-hhmmsszzz") + ".jpg");
image.open(QFile::ReadWrite);
image.write(buffer);
image.flush();
image.close();
loadImage(image.fileName());
buffer.clear();
}
}
buffers.replace(index, buffer);
});
broadcastNumber();
ui->ipLabel->setVisible(false);
ui->connectionStatus->setVisible(false);
ui->trademarkLabel->setVisible(false);
ui->statusLabel->setText("Your phone is now connected to this PC. You can start taking images now.");
}
void PhoneDialog::broadcastNumber() {
for (QTcpSocket* socket : sockets) {
socket->write(QString("CONNECTIONS " + QString::number(sockets.count()) + "\r\n").toUtf8());
socket->flush();
}
}
void PhoneDialog::loadImage(QString path) {
QImageReader reader(path);
reader.setAutoTransform(true);
QImage image = reader.read();
if (image.isNull()) {
ui->imageLabel->setText("Unfortunately, that image didn't send correctly.");
} else {
ui->imageLabel->setPixmap(QPixmap::fromImage(image));
setScaleFactor(calculateScaling(this->size(), image.size()));
}
}
float PhoneDialog::calculateScaling(QSize container, QSize image, bool allowLarger) {
return calculateScaling(container.width(), container.height(), image.width(), image.height(), allowLarger);
}
float PhoneDialog::calculateScaling(int containerWidth, int containerHeight, int imageWidth, int imageHeight, bool allowLarger) {
float heightFactor = 0, widthFactor = 0;
if (imageHeight > containerHeight || allowLarger) {
heightFactor = (float) containerHeight / (float) imageHeight;
}
if (imageWidth > containerWidth || allowLarger) {
widthFactor = (float) containerWidth / (float) containerHeight;
}
if (heightFactor == 0 && widthFactor == 0) {
return 1;
} else if (heightFactor == 0 && widthFactor != 0) {
return widthFactor;
} else if (heightFactor != 0 && widthFactor == 0) {
return heightFactor;
} else if (heightFactor < widthFactor) {
return heightFactor;
} else {
return widthFactor;
}
}
void PhoneDialog::setScaleFactor(float factor) {
scaleFactor = factor;
ui->imageLabel->resize(scaleFactor * ui->imageLabel->pixmap()->size());
recenterImage();
}
void PhoneDialog::recenterImage() {
int newx = 0, newy = 0;
if (ui->imageLabel->width() < ui->scrollArea->width()) {
newx = ui->scrollArea->width() / 2 - ui->imageLabel->width() / 2;
}
if (ui->imageLabel->height() < ui->scrollArea->height()) {
newy = ui->scrollArea->height() / 2 - ui->imageLabel->height() / 2;
}
ui->imageLabel->move(newx, newy);
}
void PhoneDialog::close() {
QDialog::close();
for (QTcpSocket* socket : sockets) {
socket->write(QString("ENDSTREAM\r\n").toUtf8());
socket->flush();
}
this->deleteLater();
}