Skip to content

Commit 7565afc

Browse files
committed
geometry storage added
1 parent 6588d74 commit 7565afc

File tree

5 files changed

+204
-151
lines changed

5 files changed

+204
-151
lines changed

meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ src = [
88
'src/FloatingSettings.cpp',
99
'src/WhiteBoard.cpp',
1010
'src/Button.cpp',
11+
'src/Geometry.cpp',
1112
'src/SetupWidgets.cpp',
1213
'src/settings.c',
1314
'src/OverView.cpp',

src/DrawingWidget.cpp

Lines changed: 15 additions & 145 deletions
Original file line numberDiff line numberDiff line change
@@ -44,26 +44,6 @@ void removeDirectory(const QString &path);
4444
#define HISTORY 15
4545
#endif
4646

47-
class ValueStorage {
48-
public:
49-
void saveValue(qint64 id, QPointF data) {
50-
values[id] = data;
51-
}
52-
53-
QPointF loadValue(qint64 id) {
54-
if (values.contains(id)) {
55-
return values[id];
56-
} else {
57-
// -1 -1 means disable drawing
58-
return QPointF(-1,-1);
59-
}
60-
}
61-
62-
private:
63-
QMap<qint64, QPointF> values;
64-
};
65-
ValueStorage storage;
66-
6747
class CursorStorage {
6848
public:
6949
void init(qint64 id){
@@ -275,7 +255,6 @@ PageStorage pages;
275255

276256
int curEventButtons = 0;
277257
bool isMoved = 0;
278-
float fpressure = 0;
279258

280259
DrawingWidget::DrawingWidget(QWidget *parent): QWidget(parent) {
281260
initializeImage(size());
@@ -300,8 +279,6 @@ DrawingWidget::~DrawingWidget() {}
300279

301280
void DrawingWidget::mousePressEvent(QMouseEvent *event) {
302281
drawing = true;
303-
lastPoint = event->position();
304-
firstPoint = event->position();
305282
mergeSelection();
306283
imageBackup = image;
307284
if(floatingSettings->isVisible()){
@@ -319,6 +296,8 @@ void DrawingWidget::mousePressEvent(QMouseEvent *event) {
319296
}
320297
curs.setCursor(-1, penSize[ev_pen]);
321298
curEventButtons = event->buttons();
299+
geo.clear(-1);
300+
geo.addValue(-1,event->position());
322301
isMoved = false;
323302

324303
}
@@ -345,10 +324,11 @@ void DrawingWidget::mouseMoveEvent(QMouseEvent *event) {
345324
switch(penMode) {
346325
case DRAW:
347326
updateCursorMouse(-1, event->position());
348-
drawLineTo(event->position());
327+
geo.addValue(-1, event->position());
328+
drawLineToFunc(-1, 1.0);
349329
break;
350330
case SELECTION:
351-
selectionDraw(firstPoint, event->position());
331+
selectionDraw(geo.first(-1), event->position());
352332
break;
353333
}
354334
} else {
@@ -366,7 +346,8 @@ void DrawingWidget::addImage(QImage img){
366346

367347
void DrawingWidget::mouseReleaseEvent(QMouseEvent *event) {
368348
if(curEventButtons & Qt::LeftButton && !isMoved) {
369-
drawLineTo(event->position()+QPointF(0,1));
349+
geo.addValue(-1, event->position()+QPointF(0,1));
350+
drawLineToFunc(-1, 1.0);
370351
}
371352
if (drawing) {
372353
drawing = false;
@@ -410,21 +391,11 @@ void DrawingWidget::clear() {
410391
}
411392

412393

413-
int rad = 0;
414-
int frad = 0;
415-
416-
void DrawingWidget::drawLineTo(const QPointF &endPoint) {
417-
drawLineToFunc(lastPoint, endPoint, 1.0);
418-
lastPoint = endPoint;
419-
}
420-
static QPointF last_end = QPointF(0,0);
421-
static QPointF last_begin = QPointF(0,0);
422394

423395
void DrawingWidget::selectionDraw(QPointF startPoint, QPointF endPoint) {
424396
image = imageBackup;
425397
update();
426398
painter.begin(&image);
427-
lastPoint = endPoint;
428399
painter.setPen(Qt::NoPen);
429400
penColor.setAlpha(127);
430401
painter.setBrush(QBrush(penColor));
@@ -433,105 +404,6 @@ void DrawingWidget::selectionDraw(QPointF startPoint, QPointF endPoint) {
433404
painter.end();
434405
}
435406

436-
437-
void DrawingWidget::drawLineToFunc(QPointF startPoint, QPointF endPoint, qreal pressure) {
438-
if(startPoint.x() < 0 || startPoint.y() < 0){
439-
return;
440-
}
441-
if (fpressure > 0){
442-
pressure = fpressure;
443-
}
444-
int fpenStyle = penStyle;
445-
if (penType == ERASER) {
446-
fpenStyle = SPLINE;
447-
}
448-
449-
switch(fpenStyle){
450-
case SPLINE:
451-
painter.begin(&image);
452-
break;
453-
case LINE:
454-
case CIRCLE:
455-
case RECTANGLE:
456-
case TRIANGLE:
457-
startPoint = firstPoint;
458-
image = imageBackup;
459-
painter.begin(&image);
460-
break;
461-
}
462-
penColor.setAlpha(255);
463-
painter.setCompositionMode(QPainter::CompositionMode_Source);
464-
switch(penType){
465-
case PEN:
466-
break;
467-
case ERASER:
468-
painter.setCompositionMode(QPainter::CompositionMode_Clear);
469-
break;
470-
case MARKER:
471-
penColor.setAlpha(127);
472-
break;
473-
}
474-
475-
painter.setPen(QPen(penColor, penSize[penType]*pressure, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
476-
painter.setRenderHint(QPainter::Antialiasing, true);
477-
painter.setRenderHint(QPainter::SmoothPixmapTransform, true);
478-
479-
switch(fpenStyle){
480-
case SPLINE:
481-
painter.drawLine(startPoint, endPoint);
482-
break;
483-
case LINE:
484-
painter.drawLine(startPoint, endPoint);
485-
break;
486-
case CIRCLE:
487-
rad = QLineF(startPoint, endPoint).length();
488-
painter.drawEllipse(startPoint, rad, rad);
489-
break;
490-
case RECTANGLE:
491-
painter.drawRect(QRectF(startPoint,endPoint));
492-
break;
493-
case TRIANGLE:
494-
painter.drawLine(startPoint, endPoint);
495-
painter.drawLine(startPoint, QPointF(startPoint.x(), endPoint.y()));
496-
painter.drawLine(QPointF(startPoint.x(), endPoint.y()), endPoint);
497-
break;
498-
}
499-
switch(fpenStyle){
500-
case SPLINE:
501-
rad = penSize[penType];
502-
update(QRectF(
503-
startPoint, endPoint
504-
).toRect().normalized().adjusted(-rad, -rad, +rad, +rad));
505-
break;
506-
case LINE:
507-
case TRIANGLE:
508-
case RECTANGLE:
509-
rad = penSize[penType];
510-
update(QRectF(
511-
last_begin, last_end
512-
).toRect().normalized().adjusted(-rad, -rad, +rad, +rad));
513-
update(QRectF(
514-
startPoint, endPoint
515-
).toRect().normalized().adjusted(-rad, -rad, +rad, +rad));
516-
break;
517-
case CIRCLE:
518-
rad = QLineF(startPoint, endPoint).length() + penSize[penType];
519-
frad = QLineF(last_begin, last_end).length() + penSize[penType];
520-
update(QRectF(
521-
startPoint,startPoint
522-
).toRect().normalized().adjusted(-rad, -rad, +rad, +rad));
523-
update(QRectF(
524-
startPoint,startPoint
525-
).toRect().normalized().adjusted(-frad, -frad, +frad, +frad));
526-
break;
527-
}
528-
529-
last_begin = startPoint;
530-
last_end = endPoint;
531-
532-
painter.end();
533-
534-
}
535407
#ifdef LIBARCHIVE
536408
void DrawingWidget::saveAll(QString file){
537409
if (!file.isEmpty()) {
@@ -612,25 +484,24 @@ bool DrawingWidget::event(QEvent *ev) {
612484
foreach(const QTouchEvent::TouchPoint &touchPoint, touchPoints) {
613485
QPointF pos = touchPoint.position();
614486
if ((Qt::TouchPointState)touchPoint.state() == Qt::TouchPointPressed) {
615-
storage.saveValue(touchPoint.id(), pos);
487+
geo.addValue(touchPoint.id(), pos);
616488
}
617489
else if ((Qt::TouchPointState)touchPoint.state() == Qt::TouchPointReleased) {
618490
curs.hide(touchPoint.id());
619-
storage.saveValue(touchPoint.id(), QPointF(-1,-1));
491+
geo.load(touchPoint.id()).clear();
620492
continue;
621493
}
622-
QPointF oldPos = storage.loadValue(touchPoint.id());
623-
drawLineToFunc(oldPos.toPoint(), pos.toPoint(), touchPoint.pressure());
624-
storage.saveValue(touchPoint.id(), pos);
494+
drawLineToFunc(touchPoint.id(), touchPoint.pressure());
495+
geo.addValue(touchPoint.id(), pos);
625496
updateCursorMouse(touchPoint.id(), pos.toPoint());
626497
curs.setCursor(touchPoint.id(), penSize[penType]);
627498
}
628499
break;
629500
}
630501
case QEvent::TabletPress: {
631502
QTabletEvent *tabletEvent = static_cast<QTabletEvent*>(ev);
632-
lastPoint = tabletEvent->position();
633-
firstPoint = tabletEvent->position();
503+
geo.clear(-1);
504+
geo.addValue(-1, tabletEvent->position());
634505
imageBackup = image;
635506
tabletActive = true;
636507
break;
@@ -648,9 +519,8 @@ bool DrawingWidget::event(QEvent *ev) {
648519
if(tabletEvent->buttons() & Qt::RightButton) {
649520
penType = ERASER;
650521
}
651-
QPointF pos = tabletEvent->position();
652-
drawLineToFunc(lastPoint, pos.toPoint(), tabletEvent->pressure());
653-
lastPoint = tabletEvent->position();
522+
geo.addValue(-1, tabletEvent->position());
523+
drawLineToFunc(-1, tabletEvent->pressure());
654524
penType = penTypeBak;
655525
}
656526

src/DrawingWidget.h

Lines changed: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,81 @@
3131
#define DRAW 0
3232
#define SELECTION 1
3333

34+
class ValueStorage {
35+
public:
36+
int size() {
37+
return values.size();
38+
}
39+
40+
void clear() {
41+
return values.clear();
42+
}
43+
44+
void saveValue(qint64 id, QPointF data) {
45+
values[id] = data;
46+
}
47+
QPointF last() {
48+
return loadValue(values.size()-1);
49+
}
50+
51+
QPointF first() {
52+
return loadValue(0);
53+
}
54+
55+
QPointF loadValue(qint64 id) {
56+
if (values.contains(id)) {
57+
return values[id];
58+
} else {
59+
// -1 -1 means disable drawing
60+
return QPointF(-1,-1);
61+
}
62+
}
63+
QMap<qint64, QPointF> values;
64+
};
65+
66+
class GeometryStorage {
67+
public:
68+
void addValue(qint64 id, QPointF data) {
69+
values[id].saveValue(values[id].size(), data);
70+
}
71+
72+
QPointF last(qint64 id){
73+
return load(id).last();
74+
}
75+
76+
QPointF first(qint64 id){
77+
return load(id).first();
78+
}
79+
80+
ValueStorage load(qint64 id) {
81+
if (values.contains(id)) {
82+
return values[id];
83+
} else {
84+
// -1 -1 means disable drawing
85+
ValueStorage v;
86+
return v;
87+
}
88+
}
89+
void clear(qint64 id) {
90+
return values[id].clear();
91+
}
92+
93+
private:
94+
QMap<qint64, ValueStorage> values;
95+
};
96+
3497
class DrawingWidget : public QWidget {
3598
public:
3699
explicit DrawingWidget(QWidget *parent = nullptr);
37100
~DrawingWidget(); // Destructor
38101

39102
QImage image;
40-
QPointF lastPoint;
41-
QPointF firstPoint;
42103
QColor penColor;
43104
QWidget* floatingSettings;
44105
MovableWidget* cropWidget;
45106
int penSize[3];
46107
bool reset;
47108
void initializeImage(const QSize &size);
48-
void drawLineTo(const QPointF &endPoint);
49109
void goPrevious();
50110
void goNext();
51111
void goPreviousPage();
@@ -68,6 +128,7 @@ class DrawingWidget : public QWidget {
68128

69129
protected:
70130
bool drawing = false;
131+
float fpressure;
71132
QImage imageBackup;
72133
bool eraser;
73134
void mousePressEvent(QMouseEvent *event) override;
@@ -78,9 +139,10 @@ class DrawingWidget : public QWidget {
78139
void updateCursorMouse(qint64 i, QPoint pos);
79140
void updateCursorMouse(qint64 i, QPointF pos);
80141
void createSelection();
81-
void drawLineToFunc(const QPointF startPoint, const QPointF endPoint, qreal pressure);
142+
void drawLineToFunc(qint64 id, qreal pressure);
82143
void selectionDraw(QPointF startPoint, QPointF endPoint);
83144
bool event(QEvent * ev);
145+
GeometryStorage geo;
84146
QPainter painter;
85147
};
86148

0 commit comments

Comments
 (0)