forked from foldynl/QLog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProfileImageWidget.cpp
151 lines (118 loc) · 3.53 KB
/
ProfileImageWidget.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
#include <QVBoxLayout>
#include "ProfileImageWidget.h"
#include "ui_ProfileImageWidget.h"
#include "core/debug.h"
MODULE_IDENTIFICATION("qlog.ui.profileimagewidget");
ProfileImageWidget::ProfileImageWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::ProfileImageWidget),
imageLabel(new AspectRatioLabel(this)),
nam(new QNetworkAccessManager(this)),
currentReply(nullptr)
{
FCT_IDENTIFICATION;
ui->setupUi(this);
QVBoxLayout *layout = new QVBoxLayout(this);
imageLabel->setAlignment(Qt::AlignCenter);
imageLabel->setMinimumSize(sizeHint());
layout->addWidget(imageLabel);
connect(nam, &QNetworkAccessManager::finished,
this, &ProfileImageWidget::processReply);
}
ProfileImageWidget::~ProfileImageWidget()
{
FCT_IDENTIFICATION;
if ( currentReply )
currentReply->abort();
nam->deleteLater();
imageLabel->deleteLater();
delete ui;
}
void ProfileImageWidget::loadImageFromUrl(const QString &urlAddress)
{
FCT_IDENTIFICATION;
qCDebug(function_parameters) << urlAddress;
if ( urlAddress.isEmpty() )
{
imageLabel->clear();
imageLabel->setToolTip("");
return;
}
if ( currentReply )
{
qCDebug(runtime) << "Previous request is still running";
currentReply->abort();
}
QUrl url(urlAddress);
currentReply = nam->get(QNetworkRequest(url));
}
void ProfileImageWidget::processReply(QNetworkReply *reply)
{
FCT_IDENTIFICATION;
/* always process one requests per class */
currentReply = nullptr;
int replyStatusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
if ( reply->error() != QNetworkReply::NoError
|| replyStatusCode < 200
|| replyStatusCode >= 300)
{
qCDebug(runtime) << "Download Image error: URL " << reply->request().url().toString();
qCDebug(runtime) << "Download Image error:" << reply->errorString();
qCDebug(runtime) << "HTTP Status Code" << replyStatusCode;
if ( reply->error() != QNetworkReply::OperationCanceledError )
{
reply->deleteLater();
}
return;
}
QByteArray imageData = reply->readAll();
QPixmap pixmap;
pixmap.loadFromData(imageData);
imageLabel->setPixmap(pixmap);
imageLabel->setScaledContents(true);
mimeType = mimeDatabase.mimeTypeForData(imageData);
if ( mimeType.name().contains("image", Qt::CaseInsensitive))
{
// QByteArray->QString in arg is due to QT5.12
imageLabel->setToolTip(QString("<img src='data:%0;base64, %1'>").arg(mimeType.name(), QString(imageData.toBase64())));
}
reply->deleteLater();
}
AspectRatioLabel::AspectRatioLabel(QWidget* parent, Qt::WindowFlags f) :
QLabel(parent, f)
{
}
AspectRatioLabel::~AspectRatioLabel()
{
}
void AspectRatioLabel::setPixmap(const QPixmap& pm)
{
pixmapWidth = pm.width();
pixmapHeight = pm.height();
updateMargins();
QLabel::setPixmap(pm);
}
void AspectRatioLabel::resizeEvent(QResizeEvent* event)
{
updateMargins();
QLabel::resizeEvent(event);
}
void AspectRatioLabel::updateMargins()
{
if ( pixmapWidth <= 0 || pixmapHeight <= 0 )
return;
int w = width();
int h = height();
if ( w <= 0 || h <= 0 )
return;
if ( w * pixmapHeight > h * pixmapWidth )
{
int m = (w - (pixmapWidth * h / pixmapHeight)) / 2;
setContentsMargins(m, 0, m, 0);
}
else
{
int m = (h - (pixmapHeight * w / pixmapWidth)) / 2;
setContentsMargins(0, m, 0, m);
}
}