-
Notifications
You must be signed in to change notification settings - Fork 15
/
dailypage.cpp
152 lines (125 loc) · 4.47 KB
/
dailypage.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
/*
* Copyright (C) 2017 ~ 2018 Deepin Technology Co., Ltd.
*
* Author: rekols <rekols@foxmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "dailypage.h"
#include "widgets/scrollarea.h"
#include <QVBoxLayout>
#include <QTimer>
#include <QFile>
#include <QDir>
DailyPage::DailyPage(QWidget *parent)
: QWidget(parent),
m_networkManager(new QNetworkAccessManager(this)),
m_imageLabel(new QLabel),
m_contentLabel(new QLabel),
m_api(YoudaoAPI::instance())
{
ScrollArea *scrollArea = new ScrollArea;
QWidget *contentWidget = new QWidget;
scrollArea->setWidget(contentWidget);
QVBoxLayout *mainLayout = new QVBoxLayout(this);
QVBoxLayout *contentLayout = new QVBoxLayout(contentWidget);
QVBoxLayout *textLayout = new QVBoxLayout;
m_imageLabel->setFixedHeight(200);
m_imageLabel->setFixedWidth(546);
m_imageLabel->setScaledContents(true);
m_contentLabel->setStyleSheet("QLabel { font-size: 15px; }");
m_contentLabel->setTextInteractionFlags(Qt::TextSelectableByMouse);
m_contentLabel->setWordWrap(true);
mainLayout->setMargin(0);
mainLayout->setSpacing(0);
mainLayout->addWidget(scrollArea);
textLayout->setContentsMargins(10, 0, 10, 0);
textLayout->addSpacing(8);
textLayout->addWidget(m_contentLabel);
textLayout->addSpacing(10);
contentLayout->setContentsMargins(0, 0, 0, 0);
contentLayout->addWidget(m_imageLabel);
contentLayout->addLayout(textLayout);
contentLayout->addStretch();
QTimer::singleShot(100, m_api, &YoudaoAPI::queryDaily);
connect(m_api, &YoudaoAPI::dailyFinished, this, &DailyPage::handleQueryFinished);
}
DailyPage::~DailyPage()
{
}
void DailyPage::checkDirectory()
{
const QString path = QString("%1/.local/share/redict").arg(QDir::homePath());
if (!QDir(path).exists()) {
QDir dir(path);
dir.mkpath(".");
}
}
void DailyPage::clearImageCache()
{
QDir dir(QString("%1/.local/share/redict").arg(QDir::homePath()));
QFileInfoList fileList = dir.entryInfoList(QDir::Files);
for (const QFileInfo &file : fileList) {
QFile f(file.filePath());
f.remove();
f.close();
}
}
void DailyPage::handleQueryFinished(std::tuple<QString, QString, QString, QString, QString> data)
{
QString dailyText;
dailyText += QString("<p>%1</p>").arg(std::get<0>(data));
dailyText += QString("<p>%1</p>").arg(std::get<1>(data));
dailyText += QString("<p>%1</p>").arg(std::get<2>(data));
m_contentLabel->setText(dailyText);
checkDirectory();
const QString picturePath = QString("%1/.local/share/redict/%2.jpeg").arg(QDir::homePath()).arg(std::get<2>(data));
if (!QFile::exists(picturePath)) {
// auto clear image cache.
clearImageCache();
QNetworkRequest request(QUrl(std::get<4>(data)));
m_networkManager->get(request);
connect(m_networkManager, &QNetworkAccessManager::finished, this,
[=] (QNetworkReply *reply) {
QByteArray imgData = reply->readAll();
loadImage(imgData);
// save image file to local.
QFile file(picturePath);
if (file.open(QIODevice::Append)) {
file.write(imgData);
}
file.close();
m_networkManager->deleteLater();
});
} else {
// open local file.
QFile file(picturePath);
if (file.open(QIODevice::ReadOnly)) {
QByteArray imgData = file.readAll();
if (imgData.isEmpty()) {
clearImageCache();
m_api->queryDaily();
}
loadImage(imgData);
}
file.close();
}
}
void DailyPage::loadImage(const QByteArray &data)
{
QPixmap pixmap;
pixmap.loadFromData(data);
m_imageLabel->setPixmap(pixmap);
emit loadFinished();
}