-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patheightbyeightview.cpp
More file actions
102 lines (90 loc) · 3.25 KB
/
eightbyeightview.cpp
File metadata and controls
102 lines (90 loc) · 3.25 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
#include "eightbyeightview.h"
#include "eightbyeightviewcontainer.h"
PgUpDownEventFilter::PgUpDownEventFilter(QObject* parent) : QObject{parent} {
}
bool PgUpDownEventFilter::eventFilter(QObject* obj, QEvent* event) {
if (event->type() == QEvent::KeyPress) {
QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
if (keyEvent->key() == Qt::Key_PageDown || keyEvent->key() == Qt::Key_PageUp) {
return true;
}
return false;
} else {
return QObject::eventFilter(obj, event);
}
}
void EightByEightView::keyPressEvent(QKeyEvent* event) {
if (event->key() == Qt::Key_PageDown) {
auto new_index = m_paletteComboBox->currentIndex() + 1;
if (new_index < m_paletteComboBox->count()) {
m_paletteComboBox->setCurrentIndex(new_index);
}
event->accept();
} else if (event->key() == Qt::Key_PageUp) {
auto new_index = m_paletteComboBox->currentIndex() - 1;
if (new_index > 0) {
m_paletteComboBox->setCurrentIndex(new_index);
}
event->accept();
}
}
EightByEightView::EightByEightView(QGraphicsScene* ogscene) : QGraphicsView(ogscene) {
setMouseTracking(true);
viewport()->setMouseTracking(true);
setWindowTitle("8x8 Tile Viewer");
verticalScrollBar()->setRange(0, 16);
QObject::connect(verticalScrollBar(), QOverload<int>::of(&QScrollBar::valueChanged), this, [&](int value) {
const int step = 16;
int offset = value % step;
if (offset != 0)
verticalScrollBar()->setValue(value - offset);
});
verticalScrollBar()->installEventFilter(new PgUpDownEventFilter(this));
setFixedSize(275, 256);
}
int EightByEightView::convertPointToTile(const QPointF& point) {
int intx = (int)point.x();
int inty = (int)point.y();
int scrollbary = (int)verticalScrollBar()->sliderPosition();
// this might *seem* like a useless calculation but in reality without this it doesn't work properly
// yay for rounding
return (((inty / 16) * 16) + ((scrollbary / 16) * 16)) + (intx / 16);
}
void EightByEightView::setComboBox(QComboBox* comboBox) {
m_paletteComboBox = comboBox;
}
void EightByEightView::mouseMoveEvent(QMouseEvent *event) {
if (!m_open || !hasFocus()) {
event->ignore();
return;
}
static_cast<EightByEightViewContainer*>(parent())->updateTileLabel(QString::asprintf("Tile: %03X", convertPointToTile(event->position())));
}
void EightByEightView::open() {
m_open = true;
show();
raise();
verticalScrollBar()->setFocusPolicy(Qt::NoFocus);
}
void EightByEightView::close(QCloseEvent *event) {
closeEvent(event);
}
void EightByEightView::updateForChange(QImage* image) {
if (currentItem) {
scene()->removeItem(currentItem);
delete currentItem;
}
currentItem = new QGraphicsPixmapItem(QPixmap::fromImage(*image).scaled(image->size() * 2));
currentItem->setAcceptHoverEvents(true);
scene()->addItem(currentItem);
setFixedSize(256 + 18, 256);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
}
void EightByEightView::closeEvent(QCloseEvent* event) {
m_open = false;
event->accept();
}
EightByEightView::~EightByEightView() {
qDebug() << "EightbyEight destructor called";
delete currentItem;
}