forked from segfault87/stitchy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselectiongroup.cpp
155 lines (123 loc) · 3.42 KB
/
selectiongroup.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
#include <QDataStream>
#include <QGraphicsScene>
#include "cell.h"
#include "document.h"
#include "globalstate.h"
#include "sparsemap.h"
#include "selectiongroup.h"
const char* SelectionGroup::mimeType()
{
return "application/vnd.kr.influx.stitchy.selection";
}
SelectionGroup::SelectionGroup(Document *doc)
: QGraphicsItemGroup()
{
map_ = new SparseMap(doc);
}
SelectionGroup::SelectionGroup(Document *doc, const QRect ®ion, bool move)
: QGraphicsItemGroup(), region_(region)
{
map_ = new SparseMap(doc);
initialize(doc, region, move);
}
SelectionGroup::SelectionGroup(Document *doc, const QRect ®ion)
: QGraphicsItemGroup(), region_(region)
{
map_ = new SparseMap(doc);
}
SelectionGroup::SelectionGroup(Document *doc, const QByteArray &array)
: QGraphicsItemGroup()
{
map_ = new SparseMap(doc);
deserialize(doc, array);
}
SelectionGroup::~SelectionGroup()
{
delete map_;
}
void SelectionGroup::moveTo(const QPoint &p)
{
region_.moveTopLeft(p);
setPos(QPointF(p.x() * 10.0f, p.y() * 10.0f));
}
void SelectionGroup::moveRel(const QPoint &delta)
{
region_.moveTopLeft(region_.topLeft() + delta);
setPos(QPointF(region_.x() * 10.0f, region_.y() * 10.0f));
}
QByteArray SelectionGroup::serialize() const
{
QByteArray array;
QDataStream stream(&array, QIODevice::WriteOnly);
stream << region_.x() << region_.y() << region_.width() << region_.height();
const CellMap &map = map_->cells();
stream << map.size();
for (CellMap::ConstIterator it = map.begin(); it != map.end(); ++it) {
const Cell *c = it.value();
stream << c->pos().x() << c->pos().y();
stream << c->featureMask();
for (int i = 0; i < CELL_COUNT; ++i) {
if (c->contains(i)) {
const Color *color = c->color(i);
stream << i;
if (color->parent())
stream << color->parent()->id();
else
stream << "";
stream << color->id();
}
}
}
return array;
}
void SelectionGroup::deserialize(Document *doc, const QByteArray &array)
{
QDataStream stream(const_cast<QByteArray *>(&array), QIODevice::ReadOnly);
int x, y, w, h;
stream >> x >> y >> w >> h;
region_ = QRect(x, y, w, h);
map_->clear();
int len;
stream >> len;
for (int i = 0; i < len; ++i) {
int x, y;
stream >> x >> y;
Cell *cell = map_->cellAt(QPoint(x, y));
int features;
stream >> features;
for (int j = 0; j < CELL_COUNT; ++j) {
if (FeatureMaskTest(features, j)) {
QString category, id;
int feature;
stream >> feature;
stream >> category >> id;
const Color *color = GlobalState::self()->colorManager()->get(category, id);
cell->addFeature(j, color);
}
}
cell->createGraphicsItems(this);
}
moveTo(position());
doc->addItem(this);
}
void SelectionGroup::initialize(Document *doc, const QRect ®ion, bool move)
{
SparseMap *map = doc->map();
region_ = region;
for (int y = region.y(); y < region.y() + region.height(); ++y) {
for (int x = region.x() ; x < region.x() + region.width(); ++x) {
QPoint point(x, y);
if (map->contains(point)) {
Cell *o = map->cellAt(point);
QPoint adjusted(x - region.x(), y - region.y());
Cell *c = map_->cellAt(adjusted);
c->merge(*o);
c->createGraphicsItems(this);
if (move)
map->remove(point);
}
}
}
moveTo(position());
doc->addItem(this);
}