-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblockarea.cpp
156 lines (133 loc) · 4.32 KB
/
blockarea.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
#include "blockarea.h"
#include <QDebug>
#include <QGridLayout>
#include <QLabel>
#include <QPainter>
#include <QPixmap>
#include <QRandomGenerator>
#include <QScrollBar>
#include <QtMath>
#include <iostream>
BlockArea::BlockArea(QWidget *parent)
: QAbstractScrollArea(parent), blockCount(0), scale(1), selectedFile(nullptr), zoomEnabled(false) {
setAttribute(Qt::WA_StyledBackground);
setVerticalScrollBarPolicy(Qt::ScrollBarPolicy::ScrollBarAlwaysOn);
connect(verticalScrollBar(), &QScrollBar::valueChanged, this, [this](int value) { onStatusBarChanged(value); });
}
void BlockArea::setPhysicalBlockCount(int count) {
physicalBlockCount = count;
blockCount = physicalBlockCount / scale;
updateParams(viewport()->size());
}
void BlockArea::setScale(int s) {
scale = s;
blockCount = physicalBlockCount / scale;
updateParams(viewport()->size());
viewport()->repaint();
}
void BlockArea::scan(QString path) { ExtentInfo::scan(path.toStdString()); }
void BlockArea::setSelectedFile(QString path) {
if (selectedFile) {
delete selectedFile;
}
selectedFile = new FileNode(getFileInfoByPath(path.toStdString()));
viewport()->update();
}
void BlockArea::unsetSelectedFile() {
if (selectedFile) {
delete selectedFile;
selectedFile = nullptr;
}
}
void BlockArea::paintEvent(QPaintEvent *) {
if (blockCount == 0) {
return;
}
QPainter p(viewport());
p.setPen(Qt::GlobalColor::gray);
p.setBrush(Qt::BrushStyle::NoBrush);
QSize size(blockSize, blockSize);
int index = yOffset * xNum;
for (int y = 0; y < yNum; y++) {
for (int x = 0; x < xNum; x++) {
QPoint pos(blockSpace + x * (blockSize + blockSpace), blockSpace + y * (blockSize + blockSpace));
std::size_t count = getExtCountFromBlockRange(index * scale, (index + 1) * scale);
if (count == 0) {
p.setBrush(Qt::GlobalColor::white);
} else {
p.setBrush(Qt::GlobalColor::darkCyan);
}
if (selectedFile) {
std::size_t start = index * scale;
std::size_t end = (index + 1) * scale;
for (const auto &ext : selectedFile->exts) {
if ((start >= ext.first && start < ext.second) || (end > ext.first && end <= ext.second) ||
(start < ext.first && end > ext.second)) {
p.setBrush(Qt::GlobalColor::red);
break;
}
}
}
p.drawRect(QRect(pos, size));
if (++index >= blockCount) {
return;
}
}
}
}
void BlockArea::resizeEvent(QResizeEvent *event)
{
updateParams(event->size());
verticalScrollBar()->setRange(0, yMax - yNum + 1);
}
void BlockArea::mouseReleaseEvent(QMouseEvent *event)
{
auto pos = event->pos();
int mx = (pos.x() - blockSpace) % (blockSize + blockSpace);
int my = (pos.y() - blockSpace) % (blockSize + blockSpace);
if (mx > blockSize || my > blockSize) {
return;
}
int ix = (pos.x() - blockSpace) / (blockSize + blockSpace);
int iy = (pos.y() - blockSpace) / (blockSize + blockSpace);
int index = xNum * (iy + yOffset) + ix;
if (index < blockCount) {
unsetSelectedFile();
emit mouseClicked(index);
}
}
void BlockArea::keyPressEvent(QKeyEvent *event)
{
if (event->key() == Qt::Key::Key_Control) {
zoomEnabled = true;
}
}
void BlockArea::keyReleaseEvent(QKeyEvent *event)
{
if (event->key() == Qt::Key::Key_Control) {
zoomEnabled = false;
}
}
void BlockArea::wheelEvent(QWheelEvent *event)
{
if (zoomEnabled) {
// emit zoomed(event->angleDelta().x(), event->angleDelta().y());
if (event->angleDelta().y() < 0) {
setScale(scale * 2);
} else {
setScale(scale / 2 ? scale / 2 : 1);
}
} else {
QAbstractScrollArea::wheelEvent(event);
}
}
void BlockArea::onStatusBarChanged(int value)
{
yOffset = value;
}
void BlockArea::updateParams(QSize size) {
xNum = (size.width() - 2 * blockSpace - 1) / (blockSize + blockSpace);
yNum = (size.height() - 2 * blockSpace - 1) / (blockSize + blockSpace);
yMax = blockCount / xNum;
verticalScrollBar()->setRange(0, yMax - yNum + 1);
}