forked from segfault87/stitchy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocument.cpp
223 lines (177 loc) · 4.22 KB
/
document.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#include <QFile>
#include <QGraphicsItem>
#include <QGraphicsScene>
#include <QUndoGroup>
#include "cell.h"
#include "color.h"
#include "editor.h"
#include "globalstate.h"
#include "sparsemap.h"
#include "selection.h"
#include "selectiongroup.h"
#include "stitch.h"
#include "utils.h"
#include "document.h"
Document::Document(QObject *parent)
: QGraphicsScene(parent)
{
selection_ = NULL;
floatingSelection_ = NULL;
changed_ = false;
editor_ = new Editor(this);
map_ = new SparseMap(this);
connect(editor_, SIGNAL(changed()), this, SLOT(documentChanged_()));
grid_ = NULL;
resetGrid();
GlobalState::self()->undoGroup()->addStack(editor_);
}
Document::Document(const QSize &size, QObject *parent)
: QGraphicsScene(parent), size_(size)
{
selection_ = NULL;
floatingSelection_ = NULL;
changed_ = false;
editor_ = new Editor(this);
map_ = new SparseMap(this);
connect(editor_, SIGNAL(changed()), this, SLOT(documentChanged_()));
grid_ = NULL;
resetGrid();
GlobalState::self()->undoGroup()->addStack(editor_);
}
Document::~Document()
{
GlobalState::self()->undoGroup()->removeStack(editor_);
if (selection_)
delete selection_;
if (floatingSelection_)
delete floatingSelection_;
delete map_;
}
QRect Document::boundingRect() const
{
return QRect(QPoint(0, 0), size_);
}
Selection* Document::createSelection()
{
if (selection_)
delete selection_;
selection_ = new Selection();
addItem(selection_);
return selection_;
}
Selection* Document::createSelection(const QRect ®ion)
{
Selection *s = createSelection();
s->set(region);
emit madeSelection(region);
return s;
}
void Document::clearSelection()
{
if (selection_) {
delete selection_;
selection_ = NULL;
}
}
SelectionGroup* Document::createFloatingSelection(const QByteArray &data)
{
if (floatingSelection_)
delete floatingSelection_;
floatingSelection_ = new SelectionGroup(this, data);
return floatingSelection_;
}
void Document::clearFloatingSelection()
{
if (floatingSelection_) {
delete floatingSelection_;
floatingSelection_ = NULL;
}
}
void Document::acquire(StitchItem *item)
{
colors_.acquire(item);
stitches_.insert(item);
}
void Document::release(StitchItem *item)
{
colors_.release(item);
stitches_.remove(item);
}
void Document::setName(const QString &name)
{
name_ = name;
}
void Document::setTitle(const QString &title)
{
title_ = title;
}
void Document::setAuthor(const QString &author)
{
author_ = author;
}
void Document::setSize(const QSize &size)
{
if (size_ == size)
return;
size_ = size;
resetGrid();
}
void Document::setChanged(bool b)
{
changed_ = b;
if (changed_)
emit documentChanged();
else
emit documentSaved();
}
void Document::documentChanged_()
{
setChanged(true);
}
void Document::resetGrid()
{
if (grid_)
delete grid_;
grid_ = grid();
addItem(grid_);
}
QGraphicsItemGroup* Document::grid()
{
QGraphicsItemGroup *group = new QGraphicsItemGroup();
group->setZValue(-1.0);
QPen gridPen(QBrush(QColor("#DDDDDD")), 0.0);
QPen gridPenDec(QBrush(QColor("#AAAAAA")), 0.0);
QPen *activePen;
double margin;
for (int i = 0; i < size_.height() + 1; ++i) {
if (i % 10 == 0) {
margin = 10.0;
activePen = &gridPenDec;
} else {
margin = 5.0;
activePen = &gridPen;
}
QGraphicsLineItem *line = new QGraphicsLineItem(-margin,
10.0 * i,
10.0 * size_.width() + margin,
10.0 * i,
group);
line->setPen(*activePen);
}
for (int i = 0; i < size_.width() + 1; ++i) {
if (i % 10 == 0) {
margin = 10.0;
activePen = &gridPenDec;
} else {
margin = 5.0;
activePen = &gridPen;
}
QGraphicsLineItem *line = new QGraphicsLineItem(10.0 * i,
-margin,
10.0 * i,
10.0 * size_.height() + margin,
group);
line->setPen(*activePen);
}
return group;
}