1 Star 0 Fork 2

微积分/HahaCrawler

forked from kimtaikee/HahaCrawler 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
imageitem.cpp 3.53 KB
一键复制 编辑 原始数据 按行查看 历史
kimtaikee 提交于 2016-02-16 09:42 . Final Step
#include <QStackedWidget>
#include <QLabel>
#include <QRoundProgressBar.h>
#include <QUrl>
#include <QVBoxLayout>
#include <QThread>
#include <QFileInfo>
#include <QMovie>
#include <QDebug>
#include <QScrollArea>
#include <QHBoxLayout>
#include "imageitem.h"
#include "filedownloader.h"
class ImageItemPrivate
{
public:
ImageItemPrivate(ImageItem* q);
bool isMovie(const QUrl& url);
QUrl url;
QString description;
QString localPath;
ImageItem* q_ptr;
QScrollArea* scrollArea;
QStackedWidget* stackWidget;
QLabel* imageLabel;
QLabel* descLabel;
QRoundProgressBar* pgsBar;
};
ImageItemPrivate::ImageItemPrivate(ImageItem *q) : q_ptr(q)
{
QVBoxLayout* layout = new QVBoxLayout;
layout->setContentsMargins(0, 0, 0, 0);
scrollArea = new QScrollArea(q_ptr);
scrollArea->setWidgetResizable(true);
stackWidget = new QStackedWidget(q_ptr);
imageLabel = new QLabel(q_ptr);
descLabel = new QLabel(q_ptr);
descLabel->setAlignment(Qt::AlignTop | Qt::AlignLeft);
QWidget* labelWidget = new QWidget(q_ptr);
QHBoxLayout* labelWidgetLayout = new QHBoxLayout;
labelWidgetLayout->addWidget(imageLabel);
labelWidgetLayout->addWidget(descLabel);
labelWidget->setLayout(labelWidgetLayout);
scrollArea->setWidget(labelWidget);
pgsBar = new QRoundProgressBar(q_ptr);
pgsBar->setBarStyle(QRoundProgressBar::StyleLine);
pgsBar->setValue(0);
stackWidget->addWidget(pgsBar);
stackWidget->addWidget(scrollArea);
layout->addWidget(stackWidget);
q_ptr->setLayout(layout);
q_ptr->setMinimumHeight(200);
}
bool ImageItemPrivate::isMovie(const QUrl &url)
{
QFileInfo fi(url.toString());
return fi.suffix().compare("gif", Qt::CaseInsensitive) == 0;
}
ImageItem::ImageItem(QWidget *parent) : QWidget(parent)
{
d_ptr = new ImageItemPrivate(this);
}
ImageItem::~ImageItem()
{
delete d_ptr;
}
void ImageItem::setUrl(const QUrl& url)
{
if (d_ptr->url != url) {
d_ptr->url = url;
// show pgsbar and download image
d_ptr->stackWidget->setCurrentWidget(d_ptr->pgsBar);
d_ptr->pgsBar->setValue(0);
QThread* thread = new QThread();
Jcr::FileDownloader* fd = new Jcr::FileDownloader(url);
fd->setSaveDir("G:/HAHACRAWLERDATA");
connect(thread, SIGNAL(started()), fd, SLOT(start()));
connect(fd, SIGNAL(finished()), thread, SLOT(terminate()));
connect(fd, SIGNAL(finished()), thread, SLOT(deleteLater()));
connect(fd, SIGNAL(percentCompleted(float)), this, SLOT(onPercentCompleted(float)));
thread->start();
}
}
QUrl ImageItem::url() const
{
return d_ptr->url;
}
void ImageItem::setDescription(const QString& desc)
{
d_ptr->descLabel->setText(desc);
}
QString ImageItem::description() const
{
return d_ptr->descLabel->text();
}
QString ImageItem::localPath() const
{
return d_ptr->localPath;
}
void ImageItem::onPercentCompleted(float percent)
{
if (percent == 1.0) {
Jcr::FileDownloader* fd = qobject_cast<Jcr::FileDownloader*>(sender());
d_ptr->stackWidget->setCurrentWidget(d_ptr->scrollArea);
d_ptr->localPath = fd->localPath();
if (d_ptr->isMovie(d_ptr->localPath)) {
QMovie* mv = new QMovie(this);
mv->setFileName(d_ptr->localPath);
d_ptr->imageLabel->setMovie(mv);
mv->start();
} else {
d_ptr->imageLabel->setPixmap(QPixmap(d_ptr->localPath));
}
fd->deleteLater();
} else {
d_ptr->pgsBar->setValue(percent * 100);
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/Jackliulin/HahaCrawler.git
[email protected]:Jackliulin/HahaCrawler.git
Jackliulin
HahaCrawler
HahaCrawler
master

搜索帮助