-
Notifications
You must be signed in to change notification settings - Fork 3
/
WaterFallItem.cpp
118 lines (105 loc) · 2.67 KB
/
WaterFallItem.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
#include "WaterFallItem.h"
#include "ImageItem.h"
#include "ImageSaveThread.h"
#include "ImageLoadThread.h"
CWaterFallItem::CWaterFallItem(const QPixmap& thumb, const QString& path, QWidget* parent /*= 0*/)
: QWidget(parent)
, m_isHovering(false)
, m_isDumped(false)
{
setThumb(thumb, path);
setCursor(Qt::PointingHandCursor);
/*
const QStringList listName = path.split("/");
if (listName.size() >= 2)
{
setToolTip(listName[listName.size() - 2]);
}*/
QFileInfo fileInfo(path);
setToolTip(QString::fromLocal8Bit("·¾¶: %1\nÃû³Æ: %2").arg(fileInfo.absolutePath()).arg(fileInfo.fileName()));
setMouseTracking(true);
}
void CWaterFallItem::setThumb(const QPixmap& thumb, const QString& path)
{
m_originImagePath = path;
m_thumb = thumb;
m_size = m_thumb.size();
update();
}
void CWaterFallItem::paintEvent(QPaintEvent *event)
{
if (m_isDumped)
{
QEventLoop loop;
/*CImageLoadThread loadThread;
loadThread.setImagePath(m_dumpedPath);
connect(&loadThread, &CImageLoadThread::finished, &loop, &QEventLoop::quit);
loadThread.start();
loop.exec();
Q_ASSERT(loadThread.isSuccessed());
m_thumb = QPixmap::fromImage(loadThread.image());*/
m_thumb.load(m_dumpedPath);
qDebug() << "Reload image --> " << m_dumpedPath << m_thumb.width() << " " << m_thumb.height();
m_isDumped = false;
removeDumpFile();
}
if (!m_thumb.isNull())
{
QPainter painter(this);
QRect r = m_isHovering ? rect() : rect().adjusted(3, 3, -3, -3);
painter.drawPixmap(r, m_thumb);
}
}
void CWaterFallItem::enterEvent(QEvent *event)
{
m_isHovering = true;
update();
}
void CWaterFallItem::leaveEvent(QEvent *event)
{
m_isHovering = false;
update();
}
void CWaterFallItem::mousePressEvent(QMouseEvent *event)
{
CImageItem imageItem(m_originImagePath);
imageItem.exec();
}
void CWaterFallItem::dump()
{
if (!m_isDumped)
{
const QString dir = QStandardPaths::standardLocations(QStandardPaths::CacheLocation)[0];
if (!QDir().exists(dir))
{
QDir().mkpath(dir);
}
m_dumpedPath = dir + "/" + QUuid::createUuid().toString() + ".jpg";
QEventLoop loop;
CImageSaveThread saveThread;
connect(&saveThread, &CImageSaveThread::finished, &loop, &QEventLoop::quit);
QImage tempImage = m_thumb.toImage();
saveThread.setImagePath(m_dumpedPath, &tempImage);
saveThread.start();
loop.exec();
Q_ASSERT(saveThread.isSuccessed());
m_thumb = QPixmap();
m_isDumped = true;
qDebug() << "Dump item ---> " << m_dumpedPath;
}
}
CWaterFallItem::~CWaterFallItem()
{
removeDumpFile();
}
void CWaterFallItem::removeDumpFile()
{
if (!m_dumpedPath.isEmpty())
{
if (!QFile::remove(m_dumpedPath))
{
qDebug() << "Failed to remove dump file! " << m_dumpedPath;
}
m_dumpedPath.clear();
}
}