-
Notifications
You must be signed in to change notification settings - Fork 1
/
iconwidget.cpp
executable file
·100 lines (83 loc) · 2.46 KB
/
iconwidget.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
#include "iconwidget.h"
#include <QPainter>
#include <QMouseEvent>
IconWidget::IconWidget(QWidget *parent) :
QLabel(parent), iconNumber(0), iconPlacingRequested(false), iconsEdited(false), iconsInRow(0)
{
}
void IconWidget::init(int _iconsInRow)
{
iconsInRow = _iconsInRow;
}
void IconWidget::setIconNumber(int icon)
{
iconNumber = icon;
if (iconsInRow != 0)
{
selection = QPoint(icon % iconsInRow, icon / iconsInRow);
}
}
int IconWidget::getIconNumber()
{
return iconNumber;
}
void IconWidget::requestIconAppend(QImage icon)
{
iconPlacingRequested = true;
newIcon = icon;
}
bool IconWidget::iconsWereEdited()
{
return iconsEdited;
}
void IconWidget::paintEvent(QPaintEvent *event)
{
QLabel::paintEvent(event);
QPainter painter(this);
painter.setPen(QPen(Qt::green));
if(selection.x() >= 0 && selection.y() >= 0)
{
drawSelection(painter, selection);
}
}
void IconWidget::mousePressEvent(QMouseEvent *event)
{
if (iconPlacingRequested && event->button() == Qt::LeftButton)
{
QPoint point = event->pos();
if (point.x() >= 0 && point.x() <= this->width() && point.y() >= 0 && point.y() <= this->height())
{
QImage icons = pixmap()->toImage();
QPainter p(&icons);
p.setCompositionMode(QPainter::CompositionMode_Source);
QPoint selection = QPoint((point.x() / ICON_SIZE) * ICON_SIZE,
(point.y() / ICON_SIZE) * ICON_SIZE);
p.drawImage(selection, newIcon);
setPixmap(QPixmap::fromImage(icons));
iconsEdited = true;
iconPlacingRequested = false;
emit iconPlaced();
}
}
else if (event->button() == Qt::RightButton)
{
iconPlacingRequested = false;
emit iconSkipped();
}
if (event->modifiers() & Qt::AltModifier)
{
QPoint point = event->pos();
if (point.x() >= 0 && point.x() <= this->width() && point.y() >= 0 && point.y() <= this->height())
{
selection = QPoint(point.x() / ICON_SIZE, point.y() / ICON_SIZE);
iconNumber = iconsInRow * selection.y() + selection.x();
update();
}
}
QWidget::mousePressEvent(event);
}
void IconWidget::drawSelection(QPainter& painter, QPoint selection)
{
QRect selectionRect = QRect(ICON_SIZE * selection.x(), ICON_SIZE * selection.y(), ICON_SIZE, ICON_SIZE);
painter.drawRect(selectionRect);
}