-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtab.cpp
More file actions
82 lines (70 loc) · 2.03 KB
/
Copy pathtab.cpp
File metadata and controls
82 lines (70 loc) · 2.03 KB
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
#include "tab.h"
#include "ui_tab.h"
#include "highlighter.h"
#include <string>
#include <QMessageBox>
#ifdef QT_DEBUG
#include <QDebug>
#endif
Tab::Tab(QWidget *parent) :
QWidget(parent),
ui(new Ui::Tab)
{
ui->setupUi(this);
ui->method->addItems(methods);
model = new QJsonModel;
manager = new QNetworkAccessManager(this);
ui->btnSend->setShortcut(tr("ctrl+e"));
ui->btnFormatter->setShortcut(tr("ctrl+f"));
ui->result->setModel(model);
connect(ui->btnSend, &QPushButton::clicked, this, &Tab::clickedSend);
connect(manager, &QNetworkAccessManager::finished, this, &Tab::finished);
connect(ui->btnFormatter, &QPushButton::clicked, this, &Tab::formatter);
}
Tab::~Tab()
{
delete ui;
}
void Tab::clickedSend(bool)
{
auto request = QNetworkRequest(QUrl(ui->lineURL->text()));
request.setHeader(QNetworkRequest::KnownHeaders::ContentTypeHeader, "application/json");
switch (ui->method->currentIndex()) {
case Tab::METHOD_GET:
manager->get(request);
break;
case Tab::METHOD_POST:
manager->post(request, ui->query->toPlainText().toUtf8());
break;
case Tab::METHOD_PUT:
manager->put(request, ui->query->toPlainText().toUtf8());
break;
case Tab::METHOD_DELETE:
auto reply = QMessageBox::question(
this,
"Do you want call delete on this URL?",
ui->lineURL->text(),
QMessageBox::Yes | QMessageBox::No,
QMessageBox::No
);
if (reply == QMessageBox::Yes) {
manager->deleteResource(request);
}
break;
}
}
void Tab::formatter(bool)
{
auto tmp = new QJsonModel;
tmp->loadJson(ui->query->toPlainText().toUtf8());
ui->query->setText(tmp->json().toJson());
}
void Tab::finished(QNetworkReply *reply)
{
if (reply->error()) {
QMessageBox::warning(this, "Error", reply->errorString());
return;
}
model->load(reply);
ui->result->expandToDepth(3);
}