]> git.zarvox.org Git - shareboard.git/blob - shareboardcanvas.cpp
Implement image insertion and placement, tool selection, and eraser.
[shareboard.git] / shareboardcanvas.cpp
1 #include "shareboardcanvas.h"
2 #include <QMouseEvent>
3 #include <QPaintEvent>
4 #include <QPainter>
5 #include <QPixmap>
6 #include <QPen>
7 #include <QColor>
8 #include <QDebug>
9 #include "shareboard.h"
10
11 ShareboardCanvas::ShareboardCanvas(QWidget* parent) : QWidget(parent) {
12         setMinimumSize(640,480);
13         setMouseTracking(true);
14         mouseDown = false;
15 }
16
17 ShareboardCanvas::~ShareboardCanvas() {
18 }
19
20 void ShareboardCanvas::setModel(Shareboard* b) {
21         board = b;
22 }
23
24 void ShareboardCanvas::mouseMoveEvent(QMouseEvent* event) {
25         // Send mouse move event to network thread
26         emit mouseMovedTo(event->posF());
27         lastPos = event->posF();
28         // If the button is down
29         if(mouseDown) {
30                 emit segmentDrawn(dragPath.last(), event->posF());
31                 dragPath.append(event->posF());
32         }
33         // Trigger a repaint
34         update();
35 }
36
37 void ShareboardCanvas::mousePressEvent(QMouseEvent* event) {
38         if(event->button() & Qt::LeftButton) {
39                 if(board->drawState == 0) {
40                         emit insertImage(event->posF());
41                         return;
42                 }
43                 mouseDown = true;
44                 dragPath.append(event->posF()); // We're starting a new drag path
45         }
46         //
47 }
48
49 void ShareboardCanvas::mouseReleaseEvent(QMouseEvent* event) {
50         if(event->button() & Qt::LeftButton) {
51                 mouseDown = false;
52                 qDebug() << dragPath.size() << "mouse move events counted this drag";
53                 // Depending on the tool, we now dispatch different types of events
54                 dragPath.clear(); // Clear the path; we're no longer tracking a drag
55         }
56 }
57
58 void ShareboardCanvas::paintEvent(QPaintEvent* event) {
59         Q_UNUSED(event)
60         QPainter p(this);
61         // Paint the background from the saved state
62         p.drawImage(0, 0, board->view());
63         // Now paint our local changes on top of it (optional, will make things look snappier)
64         
65         // Now paint the set of mouse cursors on top of that
66         QMap<int, QPointF>::const_iterator i;
67         for (i = board->viewCursors.constBegin(); i!=board->viewCursors.constEnd(); ++i) {
68                 if(i.key() == board->userID) {
69                         if( board->drawState < 0) {// Eraser mode
70                                 p.setPen(QColor(Qt::black));
71                                 p.setBrush(QColor(Qt::white));
72                                 p.drawEllipse(lastPos, qAbs(board->drawState), qAbs(board->drawState));
73                                 //p.drawEllipse(i.value(), qAbs(board->drawState), qAbs(board->drawState));
74                         }
75                         if( board->drawState == 0) { // Image mode
76                                 p.drawImage(lastPos, board->insertImage);
77                         }
78                         continue;
79                 }
80                 QPen pen(QColor(255,0,0,50));
81                 pen.setCapStyle(Qt::RoundCap);
82                 p.setPen(pen);
83                 p.setBrush(QBrush(QColor(255,0,0,50)));
84                 p.drawEllipse(i.value(),4,4);
85                 p.setPen(Qt::black);
86                 p.setBrush(Qt::black);
87                 p.drawText(i.value(),board->viewUsers[i.key()]);
88         }
89 }