-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdtksmartisanoswallpaper.cpp
287 lines (255 loc) · 10.6 KB
/
dtksmartisanoswallpaper.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#include "dtksmartisanoswallpaper.h"
#include <DWidgetUtil>
#include <DSearchEdit>
#include <DTitlebar>
DtkSmartisanosWallpaper::DtkSmartisanosWallpaper(DMainWindow *parent)
: DMainWindow(parent)
{
setCentralWidget(s);
moveToCenter(this);
DtkSmartisanosWallpaper::resize(1300,800);
DtkSmartisanosWallpaper::setMinimumSize(1300,800);
titlebar()->setIcon(QIcon(":/images/iconfinder-icon.svg"));
name->setAlignment(Qt::AlignCenter);
auther->setAlignment(Qt::AlignCenter);
source->setAlignment(Qt::AlignCenter);
des->setAlignment(Qt::AlignCenter);
pagelabel->setAlignment(Qt::AlignCenter);
QHBoxLayout *h1layout =new QHBoxLayout(s);
QVBoxLayout *v1layout = new QVBoxLayout(s);
QHBoxLayout *pagelayout =new QHBoxLayout(s);
DComboBox *sortcombobox= new DComboBox;
sortcombobox->addItem("Artand");
sortcombobox->addItem("Smartisan");
sortcombobox->addItem("Unsplash");
sortcombobox->addItem("Minimography");
sortcombobox->addItem("Pexels");
sortcombobox->addItem("Magdeleine");
sortcombobox->addItem("Fancycrave");
sortcombobox->addItem("Snapwiresnaps");
sortcombobox->addItem("Memento");
sortcombobox->addItem("纹理与材质壁纸");
sortcombobox->addItem("壁纸摄影大赛精选");
DPushButton *firstpage = new DPushButton;
firstpage->setText("首页");
DPushButton *uppage = new DPushButton;
uppage->setText("上一页");
DPushButton *downtpage = new DPushButton;
downtpage->setText("下一页");
QVBoxLayout *v2layout = new QVBoxLayout(s);
name->setText(" ");
auther->setText(" ");
source->setText(" ");
des->setText(" ");
DLineEdit *locationLineEdit = new DLineEdit;
DPushButton *download = new DPushButton;
download->setText("下载");
download->setFixedSize(200,50);
DLabel *Smartisan = new DLabel();
Smartisan->setAlignment(Qt::AlignHCenter);
Smartisan ->setText("Smartisan");
QImage *img=new QImage;
img->load(":/images/smartisanos.png");
Smartisan->setPixmap(QPixmap::fromImage(*img));
h1layout->addLayout(v1layout);
h1layout->setStretchFactor(v1layout,10);
v1layout->addLayout(pagelayout);
pagelayout->addWidget(sortcombobox);
pagelayout->addWidget(pagelabel);
pagelayout->addWidget(firstpage);
pagelayout->addWidget(uppage);
pagelayout->addWidget(downtpage);
QHBoxLayout *h2layout =new QHBoxLayout(s);
h2layout->addWidget(pic1);
h2layout->addWidget(pic2);
h2layout->addWidget(pic3);
pic1->setAlignment(Qt::AlignCenter);
pic2->setAlignment(Qt::AlignCenter);
pic3->setAlignment(Qt::AlignCenter);
QImage *default_img=new QImage;
default_img->load(":/images/smartisan.png");
pic1->setPixmap(QPixmap::fromImage(*default_img).scaled(250,250 ,Qt::KeepAspectRatio, Qt::SmoothTransformation));
pic2->setPixmap(QPixmap::fromImage(*default_img).scaled(250,250 ,Qt::KeepAspectRatio, Qt::SmoothTransformation));
pic3->setPixmap(QPixmap::fromImage(*default_img).scaled(250,250 ,Qt::KeepAspectRatio, Qt::SmoothTransformation));
QVBoxLayout *v3layout = new QVBoxLayout(s);
v3layout->addLayout(h2layout);
v1layout->addLayout(v3layout);
h1layout->addLayout(v2layout);
h1layout->setStretchFactor(v2layout,1);
v2layout->addStretch(0);
v2layout->addWidget(name);
v2layout->addWidget(auther);
v2layout->addWidget(source);
v2layout->addWidget(des);
QHBoxLayout *h3layout = new QHBoxLayout(s);
QLabel *locationlabel = new QLabel;
locationlabel->setText("选择位置:");
v2layout->addLayout(h3layout);
h3layout->addWidget(locationlabel);
h3layout->addWidget(locationLineEdit);
h3layout->addSpacing(50);
filelocation->setFixedSize(200,50);
filelocation->setText("选择");
v2layout->addStretch(0);
v2layout->addWidget(filelocation,0,Qt::AlignCenter);
v2layout->addWidget(download,0,Qt::AlignCenter);
v2layout->addStretch(0);
v2layout->addWidget(Smartisan);
// refresh_ui();
connect(this,SIGNAL(refresh_changed()),this,SLOT(refresh_ui()));
// emit refresh_changed();
//wallpaper sorts
connect(sortcombobox, &DComboBox::currentTextChanged, this, [ = ] {
sorts = sortcombobox->currentText();
pagenum = 0;
emit refresh_changed();
});
//firstpage button
connect(firstpage,&DPushButton::clicked,this,[ = ]{
pagenum = 0;
qDebug()<<"pagenum"<<pagenum;
emit refresh_changed();
});
//uppage button
connect(uppage,&DPushButton::clicked,this,[ = ]{
pagenum--;
qDebug()<<"pagenum"<<pagenum;
emit refresh_changed();
});
//downpage button
connect(downtpage,&DPushButton::clicked,this,[ = ]{
pagenum++;
qDebug()<<"pagenum"<<pagenum;
emit refresh_changed();
});
//file location choose button
connect(filelocation, &DPushButton::clicked, this, [ = ] {
QDir dir= QCoreApplication::applicationDirPath();
dir.cdUp();
PathName = dir.path();
dir.mkdir("jpg");
PathName = QFileDialog::getExistingDirectory(this, tr("选择下载路径"),
PathName+"/jpg/",
QFileDialog::ShowDirsOnly
| QFileDialog::DontResolveSymlinks);
locationLineEdit->setText(PathName);
});
//download button
connect(download, &DPushButton::clicked, this, [ = ] {
for (int i = 0;i<3 ;i++ ) {
QString fileName = PathName + '/' + id[3*pagenum+i]+ ".jpg"; ;
QString url_name = url_png[3*pagenum+i];
downIURL_to_picture(url_name,fileName);
}
});
}
void DtkSmartisanosWallpaper::refresh_ui()
{
QNetworkAccessManager manager;
QNetworkRequest request;
QString url ="http://wallpaper-api.smartisan.com/app/index.php?r=paperapi/index/list&client_version=2&source="+sorts+"&limit=20&paper_id=0";
request.setUrl(url);
// request.setRawHeader("Content-Type", "application/json;charset=utf8");
QNetworkReply *reply = manager.get(request);
QEventLoop loop;
QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
loop.exec();
if (reply->error() != QNetworkReply::NoError)
{
// return false;
}
// QFile file(fileName);
QByteArray receive =reply->readAll();
qDebug()<<"receive:"<<receive;
delete reply;
QJsonParseError jsonError;
QJsonDocument doucment = QJsonDocument::fromJson(receive, &jsonError); // 转化为 JSON 文档
if (!doucment.isNull() && (jsonError.error == QJsonParseError::NoError)) { // 解析未发生错误
if (doucment.isObject()) { // JSON 文档为对象
QJsonObject object = doucment.object(); // 转化为对象
if (object.contains("code")) {
QJsonValue value = object.value("code");
if (value.isDouble()) {
int strName = value.toVariant().toInt();
qDebug() << "code : " << strName;
}
}
if (receive.contains("data")) {
QJsonValue value = object.value("data");
if (value.isArray()) {
QJsonArray array = value.toArray();
for (int i = 0;i < 20 ;i++ ) {
QJsonObject obj = array.at(i).toObject();
if (obj.contains("author")) {
QJsonValue value = obj.value("author");
if (value.isString()) {
author[i] = value.toString();
qDebug() << "author : " << author[i];
}
}
if (obj.contains("id")) {
QJsonValue value = obj.value("id");
if (value.isString()) {
id[i] = value.toString();
qDebug() << "id : " << id[i];
}
}
if (obj.contains("url")) {
QJsonValue value = obj.value("url");
if (value.isString()) {
url_png[i] = value.toString();
qDebug() << "url : " << url_png[i];
}
}
}
}
}
}
}
pic1->setPixmap(setpnglabel(url_png[3*pagenum+0]));
pic2->setPixmap(setpnglabel(url_png[3*pagenum+1]));
pic3->setPixmap(setpnglabel(url_png[3*pagenum+2]));
name->setText("name:"+id[0]);
auther->setText("auther:"+author[0]);
source->setText("source:"+sorts);
pagelabel->setText("第"+QString::number(pagenum+1)+"页");
// des->setText("des:"+des[0]);
}
QPixmap DtkSmartisanosWallpaper::setpnglabel(const QString &szUrl)
{
//QString szUrl="http://icon.smartisan.com/drawable/com.sina.weibo/logo.png";
QUrl url(szUrl);
QNetworkAccessManager manager;
QEventLoop loop;
QNetworkReply *replypng = manager.get(QNetworkRequest(url));
QObject::connect(replypng, SIGNAL(finished()), &loop, SLOT(quit()));
loop.exec();
QByteArray jpegData = replypng->readAll();
QPixmap pix;
pix.loadFromData(jpegData);
QSize picSize(600,600);
//将pixmap缩放成picSize大小然后保存在scaledPixmap中
QPixmap scaledPixmap = pix.scaled(picSize, Qt::KeepAspectRatio);
return scaledPixmap;
//logoPng->setPixmap(pix);
}
bool DtkSmartisanosWallpaper::downIURL_to_picture(const QString &url, const QString &fileName)
{
QNetworkAccessManager manager;
QNetworkRequest request;
request.setUrl(url);
QNetworkReply *reply = manager.get(request);
QEventLoop loop;
QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
loop.exec();
if (reply->error() != QNetworkReply::NoError)
{
return false;
}
QFile file(fileName);
if(!file.open(QIODevice::WriteOnly))
return false;
file.write(reply->readAll());
file.close();
delete reply;
}