-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScribbleArea.cpp
executable file
·529 lines (404 loc) · 10.6 KB
/
ScribbleArea.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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
/*
* File: ScribbleArea.cpp
* Author: scribble
*
* Created on October 25, 2012, 2:17 PM
*/
#include "ScribbleArea.h"
#include "Sender.h"
ScribbleArea::ScribbleArea() : networkPathPage(-1)
{
penColor = Color();
penSize = 2.0;
enable = 0;
mTempPath = NULL;
pathsOnPage.resize(1);
Paths_IDs.resize(1);
redoVector.resize(1);
networkActivity = NetworkActivity::NONE;
}
ScribbleArea::ScribbleArea(int x_, int y_, int w_, int h_) : networkPathPage(-1)
{
network = NETWORK;
enable = 0;
mMode = WRITE;
x = x_;
y = y_;
width = w_;
height = h_;
penColor = Color();
penSize = 2.0;
mTempPath = NULL;
document = new Document(x, y, width, height);
//std::string fileName = "./Files/test.pdf";
//loadFile(fileName);
}
ScribbleArea::ScribbleArea(const ScribbleArea& orig)
{
}
ScribbleArea::~ScribbleArea()
{
delete mTempPath;
}
Color ScribbleArea::getPenColor()
{
return penColor;
}
float ScribbleArea::getPenSize()
{
return penSize;
}
void ScribbleArea::setPenColor(Color newColor)
{
penColor = newColor;
}
/*! Set Pen Width
*
* \param newWidth An integer representing the new width of the pen
*
* This function set the width of the pen to be used.
*/
void ScribbleArea::setPenWidth(int newWidth)
{
penSize = newWidth;
}
int ScribbleArea::getMode()
{
return mMode;
}
void ScribbleArea::setMode(int mode)
{
mMode = mode;
}
std::vector<std::vector<Path*> > ScribbleArea::getPathsOnPage()
{
return pathsOnPage;
}
int ScribbleArea::getCurrentPage()
{
return document->getCurrentPage();
}
void ScribbleArea::setLockForPath(bool lock)
{
if ( lock == 1 )
{
pathsLock.lock();
}
else
{
pathsLock.unlock();
}
}
void ScribbleArea::setLockForNetworkPath(bool lock)
{
if ( lock == 1 )
{
lockForNetworkPath.lock();
}
else
{
lockForNetworkPath.unlock();
}
}
void ScribbleArea::setLockForTempPath(bool lock)
{
if ( lock == 1 )
{
lockForTempPath.lock();
}
else
{
lockForTempPath.unlock();
}
}
Path* ScribbleArea::getTempPath()
{
return mTempPath;
}
bool ScribbleArea::pointInsideArea(Point * point)
{
//point has to be inside frame. could be changed but overlaps may occur
if ( ( point->getX() > x ) && ( point->getX() < width + x ) && ( point->getY() > y ) && ( point->getY() < height + y ) )
{
return true;
}
return false;
}
/*! Screen Press Event
*
* \param *point A pointer to a Point object
*
* This function initializes the lastPoint and enables scribbling in Write mode, or tries to delete a path on which the point passes through in Erase mode
*/
void ScribbleArea::screenPressEvent(Point* point)
{
//if point is NULL return, nothing to do
if ( point == NULL )
{
return;
}
//If mode is write, initialize the writing sequence
if ((this->mMode == WRITE || this->mMode == ERASE) && enable == 1)
{
//cleanRedoVector();
lastPoint.setX(point->getX());
lastPoint.setY(point->getY());
scribbling = true;
lockForTempPath.lock();
mTempPath = new Path(point, this->mMode, this->penColor, this->penSize, Paths_IDs[document->getCurrentPage()]++);
//TOTEST
if ( network == 1 )
{
sender->sendNewPath(mTempPath->getPathID(), mMode, mTempPath->getPenColorInt()/*, mTempPath->isEnabled()*/, document->getCurrentPage(), penSize);
}
lockForTempPath.unlock();
} //any other point needs to be delete
else
{
delete point;
point = NULL;
}
}
/*! Screen Move Event
*
* \param *point A pointer to a Point object
*
* This function draws a line between a last point and the new point in Write mode or tries to delete a path on which the point passes through in Erase mode
*/
void ScribbleArea::screenMoveEvent(Point* point)
{
if ( point == NULL )
{
return;
}
if (scribbling == true)
{
pathsLock.lock();
lockForTempPath.lock();
mTempPath->addPoint(point);
if ( network == 1 )
{
sender->sendPoints(point);
}
lockForTempPath.unlock();
pathsLock.unlock();
}//Here we can add more else if to enhance user experience by changing the color of the pressed button.
else {
delete point;
point = NULL;
}
}
/*! Screen Release Event
*
* This function disables scribbling and informs the ScribbleArea that nothing is touching the screen anymore
*/
void ScribbleArea::screenReleaseEvent(/*Points *point*/) {
if (scribbling == true) {
scribbling = false;
pathsLock.lock();
lockForTempPath.lock();
if (mTempPath != NULL) {
pathsOnPage.at(document->getCurrentPage()).push_back(mTempPath);
if (network == 1) {
sender->sendEndPath();
}
mTempPath = NULL;
}
lockForTempPath.unlock();
pathsLock.unlock();
} else {
}
}
/*! Undo
*
* This function allows the user to undo the last actions. Presently, there is no limit of now many undo can be performed, meaning the user can press undo until there is nothing present on the screen
*/
void ScribbleArea::undo(int page) {
pathsLock.lock();
if (!pathsOnPage.at(page).empty()) {
if (!redoVector.at(page).empty()) {
if (pathsOnPage.at(page).back()->getPathID() > redoVector.at(page).back()->getPathID()) {
for (uint i = 0; i < redoVector.at(page).size(); i++) {
delete redoVector.at(page).at(i);
}
redoVector.at(page).clear();
}
}
redoVector.at(page).push_back(pathsOnPage.at(page).back());
pathsOnPage.at(page).pop_back();
//updatePageContent();
}
pathsLock.unlock();
}
/*! Redo
*
* This function allows the user to redo the last undone actions. This action is only available if the last action(s) is an undo, otherwise this function will have no effect
*/
void ScribbleArea::redo(int page) {
pathsLock.lock();
if (!redoVector.at(page).empty()) {
if (!pathsOnPage.at(page).empty()) {
if (pathsOnPage.at(page).back()->getPathID() > redoVector.at(page).back()->getPathID()) {
for (uint i = 0; i < redoVector.at(page).size(); i++) {
delete redoVector.at(page).at(i);
}
redoVector.at(page).clear();
}
}
if (!redoVector.at(page).empty()) {
pathsOnPage.at(page).push_back(redoVector.at(page).back());
redoVector.at(page).pop_back();
}
}
pathsLock.unlock();
}
/*! Clear all
*
* This function clears the current page from all writing. This action <b>cannot</b> be undone.
*/
void ScribbleArea::clearAll(int page) {
pathsLock.lock();
if (!pathsOnPage.at(page).empty()) {
for (uint j = 0; j < pathsOnPage.at(page).size(); j++) {
delete pathsOnPage.at(page).at(j);
}
pathsOnPage.at(page).clear();
}
if (!redoVector.at(page).empty()) {
for (uint i = 0; i < redoVector.at(page).size(); i++) {
delete redoVector.at(page).at(i);
}
redoVector.at(page).clear();
}
pathsLock.unlock();
}
/*! Set write mode
*
* This function set the mode to write, allowing the user to write on top of the PDF
*/
void ScribbleArea::write() {
mMode = WRITE;
}
/*! Set erase mode
*
* This function set the mode to erase, allowing the user to erase anything that has been written on top of the PDF, leaving the PDF intact
*/
void ScribbleArea::erase() {
mMode = ERASE;
}
bool ScribbleArea::getScribbling() {
return scribbling;
}
void ScribbleArea::setNetworkPage(int p) {
lockForNetworkPath.lock();
//std::cout << "Setting network page: " << p << std::endl;
networkPathPage = p;
lockForNetworkPath.unlock();
}
Path* ScribbleArea::getNetworkPath() {
return mNetworkPath;
}
int ScribbleArea::getNetworkPage() {
return networkPathPage;
}
void ScribbleArea::setNetworkPath(Path* p) {
lockForNetworkPath.lock();
mNetworkPath = p;
lockForNetworkPath.unlock();
}
void ScribbleArea::addNetworkPoint(Point * p) {
lockForNetworkPath.lock();
mNetworkPath->addPoint(p);
lockForNetworkPath.unlock();
}
void ScribbleArea::endNetworkPath() {
pathsLock.lock();
lockForNetworkPath.lock();
pathsOnPage.at(networkPathPage).push_back(mNetworkPath);
mNetworkPath = NULL;
networkPathPage = -1;
lockForNetworkPath.unlock();
pathsLock.unlock();
}
void ScribbleArea::setSender(Sender* sender) {
this->sender = sender;
}
Sender* ScribbleArea::getSender() {
return sender;
}
Document * ScribbleArea::getDocument() {
return document;
}
void ScribbleArea::setFilesOnServer(std::vector<std::string> filesOnServer) {
this->filesOnServer = filesOnServer;
}
void ScribbleArea::addFileOnServer(std::string file)
{
filesOnServer.push_back(file);
}
void ScribbleArea::clearFilesOnServer()
{
filesOnServer.clear();
}
std::vector<std::string> ScribbleArea::getFilesOnServer() {
return filesOnServer;
}
int ScribbleArea::getX() {
return x;
}
int ScribbleArea::getY() {
return y;
}
int ScribbleArea::getWidth() {
return width;
}
int ScribbleArea::getHeight() {
return height;
}
void ScribbleArea::previousPage() {
if (document->getCurrentPage() != 0) {
document->changePage(document->getCurrentPage() - 1);
}
}
void ScribbleArea::nextPage() {
if (document->getCurrentPage() != document->getNumberOfPages() - 1) {
document->changePage(document->getCurrentPage() + 1);
}
}
void ScribbleArea::loadFile(std::string fileName) {
document->load(fileName);
document->getCurrentPage();
pathsOnPage.clear();
Paths_IDs.clear();
redoVector.clear();
pathsOnPage.resize(document->getNumberOfPages());
Paths_IDs.resize(document->getNumberOfPages());
redoVector.resize(document->getNumberOfPages());
}
void ScribbleArea::setOwnershipMe() {
enableScribbleArea(1);
ownership = ME;
}
void ScribbleArea::setOwnershipFree() {
enableScribbleArea(0);
ownership = FREE;
}
void ScribbleArea::setOwnershipTaken() {
enableScribbleArea(0);
ownership = TAKEN;
}
void ScribbleArea::setNetworkActivity(ScribbleArea::NetworkActivity n) {
networkActivity = n;
}
ScribbleArea::NetworkActivity ScribbleArea::getNetworkActivity() {
return networkActivity;
}
int ScribbleArea::getOwnershipValue() {
return ownership;
}
void ScribbleArea::enableScribbleArea(bool en) {
enable = en;
}
bool ScribbleArea::getEnabled() {
return enable;
}