-
Notifications
You must be signed in to change notification settings - Fork 0
/
form.cpp
185 lines (150 loc) · 5.56 KB
/
form.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include <QApplication>
#include <QWindow>
#include <QScreen>
#include <QDateTime>
#include <QMessageBox>
#include <QDir>
#include <QKeyEvent>
#include <QStandardPaths>
#include <QFileDialog>
#include <QImageWriter>
#include <QRegExp>
#include "form.h"
#include "ui_form.h"
#include "successdialog.h"
#include "utils.h"
Form::Form(QWidget *parent) :
QWidget(parent),
ui(new Ui::Form)
{
ui->setupUi(this);
this->ui->listWidget->addItems(Utils::loadPaths());
this->ui->listWidget->setCurrentRow(0);
shootScreen();
capturedDate = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
const QString format = ".png";//TODO: move to configuration
targetFilename = generateFilename(capturedDate)+format;
this->ui->screenshotFileName->setText(targetFilename);
connect(this->ui->cancelButton,&QPushButton::clicked,this,&Form::closeAllWindows);
}
Form::~Form()
{
delete ui;
}
QString Form::generateFilename(QString capturedDate){
return "Screenshot at " + capturedDate;
}
void Form::shootScreen()
{
QScreen *screen = QGuiApplication::primaryScreen();
if (const QWindow *window = windowHandle())
screen = window->screen();
if (!screen)
return;
originalPixmap = screen->grabWindow(0);
updateScreenshotLabel();
QApplication::beep();
}
void Form::saveScreenshot(QString fullFileName)
{
if (!originalPixmap.save(fullFileName)) {
QMessageBox::warning(this, tr("Save Error"), tr("The image could not be saved to \"%1\".")
.arg(QDir::toNativeSeparators(fullFileName)));
}
}
void Form::saveToCustomPath(){
QString initialPath = QStandardPaths::writableLocation(QStandardPaths::PicturesLocation);
if (initialPath.isEmpty())
initialPath = QDir::currentPath();
initialPath += "/"+ targetFilename;
QFileDialog fileDialog(this, tr("Save As"), initialPath);
fileDialog.setAcceptMode(QFileDialog::AcceptSave);
fileDialog.setFileMode(QFileDialog::AnyFile);
fileDialog.setDirectory(initialPath);
// QStringList mimeTypes;
// foreach (const QByteArray &bf, QImageWriter::supportedMimeTypes())
// mimeTypes.append(QLatin1String(bf));
// fileDialog.setMimeTypeFilters(mimeTypes);
// fileDialog.selectMimeTypeFilter("image/" + format);
// fileDialog.setDefaultSuffix(format);
if (fileDialog.exec() != QDialog::Accepted)
return;
const QString outFile = fileDialog.selectedFiles().first();
qDebug() << "Custom path:" << outFile;
saveScreenshot(outFile);
closeAllWindows();
}
void Form::updateScreenshotLabel()
{
this->ui->screenshotLabel->setPixmap(originalPixmap.scaled(this->ui->screenshotLabel->size(),
Qt::KeepAspectRatio,
Qt::SmoothTransformation));
}
void Form::on_saveScreenshot_clicked()
{
QString path = this->ui->listWidget->currentItem()->text();
QRegExp rx("(http|ftp|https)://?");
rx.indexIn(path);
qDebug() << "Detecting upload/save mode " << path << rx.cap(0);
if(rx.cap(0)!=""){
qDebug() << "URL mode";
qDebug() << "/tmp/" << targetFilename;
saveScreenshot("/tmp/"+targetFilename);
uploadScreenshot(path, targetFilename,"/tmp/"+targetFilename);
} else{
qDebug() << "FILE mode";
qDebug() << path << targetFilename;
saveScreenshot(path+"/"+targetFilename);
closeAllWindows();
}
}
void Form::on_pushButton_clicked()
{
saveToCustomPath();
}
void Form::uploadScreenshot(QString urlPath, QString fileName, QString fullPath){
qDebug() << QSslSocket::sslLibraryVersionString();
qDebug() << "uploadScreenshot start from" << fileName << fullPath;
QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
QHttpPart imagePart;
imagePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("image/png"));
imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"userfile\"; filename=\""+ fileName+"\""));
QFile *file = new QFile(fullPath);
file->open(QIODevice::ReadOnly);
imagePart.setBodyDevice(file);
file->setParent(multiPart); // we cannot delete the file now, so delete it with the multiPart
multiPart->append(imagePart);
QUrl url(urlPath);
QNetworkRequest request(url);
QNetworkAccessManager *manager = new QNetworkAccessManager(this);
connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(imageLoaded(QNetworkReply*)));
QNetworkReply *reply = manager->post(request, multiPart);
multiPart->setParent(reply);
qDebug() << "uploadScreenshot done";
}
void Form::imageLoaded(QNetworkReply *reply)
{
QByteArray data = reply->readAll();
reply->deleteLater();
QRegExp rx("(http|ftp|https)://([\\w_-]+(?:(?:\\.[\\w_-]+)+))([\\w.,@?^=%&:/~+#-]*[\\w@?^=%&/~+#-])?");
rx.indexIn(data);
qDebug() << "imageLoaded responce" << data << rx.cap(0);
SuccessDialog *dlg=new SuccessDialog(rx.cap(0));
connect(dlg, SIGNAL(close()),this, SLOT (closeAllWindows()));
dlg->setModal(true);
dlg->show();
}
void Form::closeAllWindows()
{
// connect(this->ui->cancelButton, SIGNAL(clicked()),
// this, SLOT (closeAllWindows()));
// connect(exitAction, SIGNAL(triggered()), qApp, SLOT(closeAllWindows()));
QApplication::exit();
}
void Form::keyPressEvent(QKeyEvent *e)
{
if(e->key() == Qt::Key_Escape) {
qDebug() << "escape key has pressed";
closeAllWindows();
}
}