-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathview.cpp
More file actions
173 lines (148 loc) · 4.52 KB
/
view.cpp
File metadata and controls
173 lines (148 loc) · 4.52 KB
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
#include "view.h"
// #include "delegate.h"
#include <QFileInfo>
#include <QMessageBox>
#include <QScrollBar>
#include <QDebug>
View::View(QWidget* parent)
: QListView(parent)
{
setDragDropMode(QListView::NoDragDrop);
setSelectionMode(QListView::NoSelection);
setResizeMode(QListView::ResizeMode::Adjust);
setUniformItemSizes(false);
setVerticalScrollMode(ScrollMode::ScrollPerPixel);
verticalScrollBar()->setSingleStep(50);
delegate = new Delegate;
setItemDelegate(delegate);
connect(verticalScrollBar(), &QScrollBar::valueChanged, this, &View::scrolled);
resizeTimer = new QTimer;
resizeTimer->setInterval(100);
resizeTimer->setSingleShot(true);
connect(resizeTimer, &QTimer::timeout, this, &View::resizeTimeout);
unloadTimer = new QTimer;
unloadTimer->setInterval(1000);
connect(unloadTimer, &QTimer::timeout, this, &View::unloadTimeout);
}
void View::setItemPixmap(const QModelIndex& index, const QPixmap& pix)
{
itemModel->setData(index, pix, Qt::DecorationRole);
loadedIndexes.append(index);
update(index);
}
void View::onFileListLoaded(const QStringList& fileList)
{
emit fileListLoaded(fileList);
}
void View::onItemLoaded(QStandardItem* item)
{
itemModel->appendRow(item);
}
void View::resizeTimeout()
{
if (nullptr == itemModel) {
return;
}
auto item = new QStandardItem("Refresh");
itemModel->insertRow(0, item);
itemModel->removeRow(0);
if (maxScroll == 0) {
return;
}
double fraction = currentScroll / maxScroll;
maxScroll = verticalScrollBar()->maximum();
currentScroll = fraction * maxScroll;
verticalScrollBar()->setValue(currentScroll);
}
void View::unloadTimeout()
{
auto indexesCopy = loadedIndexes;
QMutableListIterator<QModelIndex> i(indexesCopy);
while (i.hasNext()) {
auto index = i.next();
if (!index.isValid()) {
i.remove();
continue;
}
auto rect = visualRect(index);
if (!rect.isValid() || (rect.y() + rect.height() + height() < 0) || (rect.y() > rect.height() + height())) {
i.remove();
itemModel->setData(index, QPixmap(), Qt::DecorationRole);
}
}
loadedIndexes = indexesCopy;
}
void View::loadFile(const QString& f)
{
unloadTimer->stop();
QFileInfo info(f);
if (!info.exists() && !(info.suffix().toLower() == "zip" || info.suffix().toLower() == "cbz" || info.suffix().toLower() == "7z")) {
QMessageBox::warning(this, "Invalid File", QString("file: %1\n is not valid").arg(f));
return;
}
if (nullptr != itemModel) {
itemModel->deleteLater();
}
itemModel = new QStandardItemModel;
setModel(itemModel);
verticalScrollBar()->setValue(0);
if (nullptr != loader) {
disconnect(delegate, &Delegate::requestImage, loader, &Loader::addToQueue);
disconnect(loader, &Loader::fileListLoaded, this, &View::onFileListLoaded);
disconnect(loader, &Loader::itemLoaded, this, &View::onItemLoaded);
disconnect(loader, &Loader::pixmapLoaded, this, &View::setItemPixmap);
loader->quit();
loader->deleteLater();
}
loader = new Loader;
connect(delegate, &Delegate::requestImage, loader, &Loader::addToQueue);
connect(loader, &Loader::fileListLoaded, this, &View::onFileListLoaded);
connect(loader, &Loader::itemLoaded, this, &View::onItemLoaded);
connect(loader, &Loader::pixmapLoaded, this, &View::setItemPixmap);
loader->setFile(f);
emit fileLoaded(f);
unloadTimer->start();
}
void View::setCurrentFile(int i)
{
auto index = itemModel->index(i, 0);
if (index.isValid()) {
scrollTo(index);
}
}
void View::scrolled(int value)
{
auto index = indexAt(QPoint(width() / 2, height() / 2));
if (!index.isValid()) {
return;
}
emit currentFileChanged(index.data(Qt::DisplayRole).toString());
QModelIndex next;
// down
if (value > currentScroll) {
next = itemModel->index(index.row() + 1, 0);
}
// up
else {
next = itemModel->index(index.row() - 1, 0);
}
if (next.isValid()
&& next.data(Qt::DecorationRole).value<QPixmap>().isNull()
&& !next.data(Qt::DisplayRole).toString().isEmpty()) {
loader->addToQueue(next);
}
currentScroll = value;
}
void View::mousePressEvent(QMouseEvent* e)
{
e->ignore();
}
void View::mouseReleaseEvent(QMouseEvent* e)
{
e->ignore();
}
void View::resizeEvent(QResizeEvent* e)
{
QListView::resizeEvent(e);
resizeTimer->start();
}