]> git.zarvox.org Git - shareboard.git/blob - action.cpp
Implement image insertion and placement, tool selection, and eraser.
[shareboard.git] / action.cpp
1 #include "action.h"
2 #include <QDataStream>
3 #include <QByteArray>
4 #include <QBuffer>
5 #include "base64.h"
6
7 Action::Action() {
8         type = INVALID;
9         mesgID = 0;
10         userID = 0;
11         timestamp = QDateTime::currentDateTime();
12 }
13
14 Action::Action(Type t) {
15         type = t;
16         mesgID = 0;
17         userID = 0;
18         timestamp = QDateTime::currentDateTime();
19 }
20
21 Action::~Action() {
22 }
23
24 //
25 UserJoinAction::UserJoinAction() : Action(Action::UserJoin) {
26 }
27 UserJoinAction::~UserJoinAction() {
28 }
29
30 //
31 UserPartAction::UserPartAction() : Action(Action::UserPart) {
32 }
33 UserPartAction::~UserPartAction() {
34 }
35
36 //
37 MouseMoveAction::MouseMoveAction() : Action(Action::MouseMove) {
38 }
39 MouseMoveAction::~MouseMoveAction() {
40 }
41
42 //
43 DrawLineAction::DrawLineAction() : Action(Action::DrawLine) {
44         color = Qt::black;
45         width = 1;
46 }
47 DrawLineAction::~DrawLineAction() {
48 }
49
50 AddImageAction::AddImageAction() : Action(Action::AddImage) {
51 }
52 AddImageAction::~AddImageAction() {
53 }
54
55 UserChatAction::UserChatAction() : Action(Action::UserChat) {
56 }
57 UserChatAction::~UserChatAction() {
58 }
59
60 ClearBoardAction::ClearBoardAction() : Action(Action::ClearBoard) {
61 }
62 ClearBoardAction::~ClearBoardAction() {
63 }
64
65 QTextStream& operator<<(QTextStream& out, const Action& action) {
66         out << "0 " << action.userID << " " << action.timestamp.toString("yyyy-MM-ddThh:mm:ss.zzz") << " " << static_cast<quint16>(action.type) ; // mesgID, userID, timestamp, mesgType
67         switch(action.type) {
68                 case Action::INVALID:
69                 case Action::UserSynced:
70                         break;
71                 case Action::UserJoin:
72                         {
73                                 const UserJoinAction* a = static_cast<const UserJoinAction*>(&action);
74                                 out << " " << a->username;
75                         } break;
76                 case Action::UserPart:
77                         break; // UserPart doesn't have any special data
78                 case Action::MouseMove:
79                         {
80                                 const MouseMoveAction* a = static_cast<const MouseMoveAction*>(&action);
81                                 out << " " << a->pos.x() << " " << a->pos.y();
82                         } break;
83                 case Action::DrawLine:
84                         {
85                                 const DrawLineAction* a = static_cast<const DrawLineAction*>(&action);
86                                 const QColor& c = a->color;
87                                 out << " " << c.red() << " " << c.green() << " " << c.blue() << " " << a->width;
88                                 foreach(QPointF p, a->points) {
89                                         out << " " << p.x() << " " << p.y();
90                                 }
91                         } break;
92                 case Action::AddImage:
93                         {
94                                 const AddImageAction* a = static_cast<const AddImageAction*>(&action);
95                                 out << " " << a->topLeft.x() << " " << a->topLeft.y();
96                                 QByteArray ba;
97                                 QBuffer buffer(&ba);
98                                 buffer.open(QIODevice::WriteOnly);
99                                 a->image.save(&buffer, "PNG");
100                                 buffer.close();
101                                 out << toBase64(ba);
102                         } break;
103                 case Action::UserChat:
104                         {
105                                 const UserChatAction* a = static_cast<const UserChatAction*>(&action);
106                                 out << " " << a->text;
107                         } break;
108                 case Action::ClearBoard:
109                         break; // No special data for ClearBoard
110         }
111         out << "\n";
112         return out;
113 }
114
115 // operator>> can't be implemented in this manner since we need to handle
116 // different types and operator>> works on a reference to the base class.
117 // Thus, I'm pushing this sort of functionality into the connection manager. - Drew
118