/
board.cpp
448 lines (401 loc) · 9.84 KB
/
board.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/*
DrawPile - a collaborative drawing program.
Copyright (C) 2006-2008 Calle Laakkonen
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <QDebug>
#include "board.h"
#include "boarditem.h"
#include "annotationitem.h"
#include "user.h"
#include "boardeditor.h"
#include "preview.h"
#include "interfaces.h"
#include "core/layerstack.h"
#include "../shared/net/annotation.h"
#include "../shared/net/message.h"
namespace drawingboard {
Board::Board(QObject *parent, interface::BrushSource *brush, interface::ColorSource *color)
: QGraphicsScene(parent), image_(0),localuser_(-1), toolpreview_(0), brushsrc_(brush), colorsrc_(color), hla_(false), layerwidget_(0)
{
setItemIndexMethod(NoIndex);
}
Board::~Board()
{
delete image_;
clearUsers();
}
/**
* A new image is created with the given size and initialized to a solid color
* @param size size of the drawing board
* @param background background color
*/
void Board::initBoard(const QSize& size, const QColor& background)
{
QImage image(size, QImage::Format_RGB32);
image.fill(background.rgb());
initBoard(image);
}
/**
* An existing image is used as a base.
* @param image image to use
*/
void Board::initBoard(QImage image)
{
setSceneRect(0,0,image.width(), image.height());
delete image_;
image_ = new BoardItem(image.convertToFormat(QImage::Format_RGB32));
addItem(image_);
foreach(User *u, users_)
u->setBoard(image_);
QList<QRectF> regions;
regions.append(sceneRect());
emit changed(regions);
previewstarted_ = false;
}
/**
* A single (local) user is needed to modify the drawing board, be sure
* to create one after deleting everyone.
*/
void Board::clearUsers()
{
commitPreviews();
foreach(User *u, users_) {
delete u;
}
users_.clear();
localuser_ = -1;
}
/**
* @param zeroid if true, set the ID of each annotation to zero
* @return list of ANNOTATE messages
*/
QStringList Board::getAnnotations(bool zeroid) const
{
QStringList messages;
if(image_)
foreach(QGraphicsItem *item, image_->children())
if(item->type() == AnnotationItem::Type) {
protocol::Annotation a;
static_cast<AnnotationItem*>(item)->getOptions(a);
if(zeroid)
a.id = 0;
messages << protocol::Message::quote(a.tokens());
}
return messages;
}
void Board::clearAnnotations()
{
if(image_)
foreach(QGraphicsItem *item, image_->children())
if(item->type() == AnnotationItem::Type) {
emit annotationDeleted(static_cast<AnnotationItem*>(item));
delete item;
}
}
void Board::showAnnotations(bool show)
{
if(image_)
foreach(QGraphicsItem *item, image_->children())
if(item->type() == AnnotationItem::Type)
item->setVisible(show);
}
void Board::highlightAnnotations(bool hl)
{
hla_ = hl;
if(image_)
foreach(QGraphicsItem *item, image_->children())
if(item->type() == AnnotationItem::Type)
static_cast<AnnotationItem*>(item)->forceBorder(hl);
}
/**
* @param id user id
*/
void Board::addUser(int id)
{
if(users_.contains(id)) {
// This is not necessarily an error condition. The server will not
// ghost users that haven't drawn anything. In those cases, it is
// safe to delete the user as it's not being used anywhere.
// Depending on the server, we might get info about ourselves twice.
// First to tell us about ourselves (the user ID), and a second
// time when joining a session. We have already given out a pointer
// to the local user, so don't delete it.
if(id==localuser_)
return;
qDebug() << "Reusing board user" << id;
delete users_.take(id);
}
User *user = new User(image_, id);
users_[id] = user;
}
/**
* Designates one user as the local user
* @param id user id
* @pre id must have been added with addUser
*/
void Board::setLocalUser(int id)
{
Q_ASSERT(users_.contains(id));
localuser_ = id;
// Set the layer list if we know it already
if(layerwidget_)
users_[localuser_]->setLayerList(layerwidget_);
}
/**
* @param llist layer list widget
*/
void Board::setLayerList(widgets::LayerList *llist)
{
layerwidget_ = llist;
// Update local user if it exists
if(localuser_ != -1)
users_[localuser_]->setLayerList(layerwidget_);
}
/**
* @return board contents
*/
QImage Board::image() const
{
if(image_)
return image_->image()->toFlatImage();
else
return QImage();
}
/**
* @retval true if the board contains an image
*/
bool Board::hasImage() const {
return image_!=0;
}
bool Board::hasAnnotations() const
{
if(image_)
foreach(QGraphicsItem *i, image_->children())
if(i->type() == AnnotationItem::Type)
return true;
return false;
}
/**
* @return board width
*/
int Board::width() const {
if(image_)
return int(image_->boundingRect().width());
else
return -1;
}
/**
* @return board height
*/
int Board::height() const {
if(image_)
return int(image_->boundingRect().height());
else
return -1;
}
/**
* @return layer stack
*/
dpcore::LayerStack *Board::layers()
{
if(image_)
return image_->image();
return 0;
}
/**
* Returns a BoardEditor for modifying the drawing board either
* directly or over the network.
* @param session which network session the editor works over. If 0, a local editor is returned
*/
BoardEditor *Board::getEditor(network::SessionState *session)
{
Q_ASSERT(localuser_ != -1);
User *user = users_.value(localuser_);
Q_ASSERT(user);
if(session)
return new RemoteBoardEditor(this, user, session, brushsrc_, colorsrc_);
else
return new LocalBoardEditor(this, user, brushsrc_, colorsrc_);
}
/**
* Preview strokes are used to give immediate feedback to the user,
* before the stroke info messages have completed their roundtrip
* through the server.
* @param point stroke point
*/
void Board::addPreview(const dpcore::Point& point)
{
Q_ASSERT(localuser_ != -1);
User *user = users_.value(localuser_);
Preview *pre;
if(previewcache_.isEmpty()) {
pre = new StrokePreview(user->board());
} else
pre = previewcache_.dequeue();
if(previewstarted_) {
pre->preview(lastpreview_, point, brushsrc_->getBrush());
} else {
pre->preview(point, point, brushsrc_->getBrush());
previewstarted_ = true;
}
lastpreview_ = point;
previews_.enqueue(pre);
}
/**
*/
void Board::endPreview()
{
previewstarted_ = false;
}
/**
* This is called when leaving a session. All pending preview strokes
* are immediately drawn on the board.
*/
void Board::commitPreviews()
{
dpcore::Point lastpoint(-1,-1,0);
while(previews_.isEmpty()==false) {
qreal distance;
Preview *p = previews_.dequeue();
if(p->from() != lastpoint) // TODO
image_->drawPoint(0, p->from(), p->brush());
else // TODO
image_->drawLine(0, p->from(), p->to(), p->brush(), distance);
lastpoint = p->to();
delete p;
}
while(previewcache_.isEmpty()==false)
delete previewcache_.dequeue();
}
/**
* Remove all preview strokes from the board
*/
void Board::flushPreviews()
{
while(previews_.isEmpty()==false) {
Preview *p = previews_.dequeue();
p->hidePreview();
previewcache_.enqueue(p);
}
}
/**
* @param user user id
* @param brush brush to use
* @pre user must exist
*/
void Board::userSetTool(int user, const dpcore::Brush& brush)
{
Q_ASSERT(users_.contains(user));
users_.value(user)->setBrush(brush);
}
/**
* @param user user id
* @param point coordinates
* @pre user must exist
*/
void Board::userStroke(int user, const dpcore::Point& point)
{
Q_ASSERT(users_.contains(user));
users_.value(user)->addStroke(point);
if(user == localuser_ && previews_.isEmpty() == false) {
Q_ASSERT(!previews_.isEmpty());
Preview *pre = previews_.dequeue();
pre->hidePreview();
previewcache_.enqueue(pre);
}
}
/**
* @param user user id
* @pre user must exist
*/
void Board::userEndStroke(int user)
{
Q_ASSERT(users_.contains(user));
users_.value(user)->endStroke();
}
/**
*/
void Board::annotate(const protocol::Annotation& annotation)
{
if(!image_) return;
AnnotationItem *item=0;
bool newitem = true;
// Find existing annotation
foreach(QGraphicsItem *i, image_->children()) {
if(i->type() == AnnotationItem::Type) {
AnnotationItem *ai = static_cast<AnnotationItem*>(i);
if(ai->id() == annotation.id) {
item = ai;
newitem = false;
break;
}
}
}
if(item==0) {
item = new AnnotationItem(annotation.id, image_);
item->forceBorder(hla_);
}
item->setOptions(annotation);
if(newitem && annotation.user == localuser_)
emit newLocalAnnotation(item);
}
void Board::unannotate(int id)
{
if(!image_) return;
AnnotationItem *item=0;
foreach(QGraphicsItem *i, image_->children()) {
if(i->type() == AnnotationItem::Type) {
AnnotationItem *ai = static_cast<AnnotationItem*>(i);
if(ai->id() == id) {
item = ai;
break;
}
}
}
if(item) {
emit annotationDeleted(item);
delete item;
} else {
qWarning() << "Can't delete annotation" << id << "because it doesn't exist!";
}
}
void Board::addLayer(const QString& name)
{
layers()->addLayer(name, layers()->size());
}
/**
* The layer is removed and all users active layers are changed to point
* to something else.
* @param layer id
*/
void Board::deleteLayer(int id)
{
const int index = layers()->id2index(id);
if(index<0) {
qWarning() << "Tried to delete nonexistent layer";
}
// Fix user layers
foreach(User *u, users_) {
if(u->layer() == index) {
if(index==0)
u->setLayerId(1);
else
u->setLayerId(index-1);
}
}
// Delete the layer
layers()->deleteLayer(id);
update();
}
}