From e0c79b3b885f126a2a2a34cb0d5df90796821130 Mon Sep 17 00:00:00 2001 From: Mateusz Piekos Date: Mon, 18 Jun 2012 12:55:55 +0200 Subject: Completed serialization and parsing of line element diff --git a/Swift/QtUI/Whiteboard/GView.cpp b/Swift/QtUI/Whiteboard/GView.cpp index 634ff75..478dee0 100644 --- a/Swift/QtUI/Whiteboard/GView.cpp +++ b/Swift/QtUI/Whiteboard/GView.cpp @@ -57,6 +57,7 @@ namespace Swift { QLineF line = item->line(); line.setP1(this->mapToScene(event->pos())); item->setLine(line); + } } else if (mode == Rect) { @@ -142,7 +143,10 @@ namespace Swift { if (mode == Line) { QPointF point = this->mapToScene(event->pos()); QGraphicsItem* item = scene()->addLine(point.x(), point.y(), point.x(), point.y(), pen); + QString id = QString::fromStdString(idGenerator.generateID()); item->setZValue(zValue++); + item->setData(0, id); + items_.insert(id, item); lastItem = item; } diff --git a/Swift/QtUI/Whiteboard/GView.h b/Swift/QtUI/Whiteboard/GView.h index 6a141ee..74da7a4 100644 --- a/Swift/QtUI/Whiteboard/GView.h +++ b/Swift/QtUI/Whiteboard/GView.h @@ -11,12 +11,11 @@ #include #include #include +#include #include "TextDialog.h" #include "FreehandLineItem.h" -using namespace std; - namespace Swift { class GView : public QGraphicsView { Q_OBJECT; @@ -47,6 +46,7 @@ namespace Swift { QGraphicsRectItem* selectionRect; TextDialog* textDialog; QHash items_; + SimpleIDGenerator idGenerator; signals: void lastItemChanged(QGraphicsItem* item); diff --git a/Swift/QtUI/Whiteboard/QtWhiteboardWindow.cpp b/Swift/QtUI/Whiteboard/QtWhiteboardWindow.cpp index 24eb6cf..612e467 100644 --- a/Swift/QtUI/Whiteboard/QtWhiteboardWindow.cpp +++ b/Swift/QtUI/Whiteboard/QtWhiteboardWindow.cpp @@ -14,7 +14,6 @@ #include #include #include -#include #include #include @@ -235,7 +234,12 @@ namespace Swift { QGraphicsLineItem* lineItem = qgraphicsitem_cast(item); if (lineItem != 0) { QLine line = lineItem->line().toLine(); + QColor color = lineItem->pen().color(); WhiteboardLineElement::ref element = boost::make_shared(line.x1(), line.y1(), line.x2(), line.y2()); + element->setColor(Color(color.red(), color.green(), color.blue(), color.alpha())); + element->setPenWidth(lineItem->pen().width()); + + element->setID(lineItem->data(0).toString().toStdString()); whiteboardSession_->sendElement(element); } diff --git a/Swift/QtUI/Whiteboard/WhiteboardElementDrawingVisitor.h b/Swift/QtUI/Whiteboard/WhiteboardElementDrawingVisitor.h index 962fd79..a1715bc 100644 --- a/Swift/QtUI/Whiteboard/WhiteboardElementDrawingVisitor.h +++ b/Swift/QtUI/Whiteboard/WhiteboardElementDrawingVisitor.h @@ -17,6 +17,11 @@ namespace Swift { void visit(const WhiteboardLineElement* element) { QGraphicsLineItem *item = new QGraphicsLineItem(element->x1(), element->y1(), element->x2(), element->y2()); + QPen pen; + Color color = element->getColor(); + pen.setColor(QColor(color.getRed(), color.getGreen(), color.getBlue(), color.getAlpha())); + pen.setWidth(element->getPenWidth()); + item->setPen(pen); graphicsView_->scene()->addItem(item); } diff --git a/Swiften/Parser/PayloadParsers/WhiteboardParser.cpp b/Swiften/Parser/PayloadParsers/WhiteboardParser.cpp index ef3c119..d0fa9f9 100644 --- a/Swiften/Parser/PayloadParsers/WhiteboardParser.cpp +++ b/Swiften/Parser/PayloadParsers/WhiteboardParser.cpp @@ -6,6 +6,7 @@ #include #include +#include #include #include @@ -23,6 +24,19 @@ namespace Swift { int x2 = std::atoi(attributes.getAttributeValue("x2").get_value_or("0").c_str()); int y2 = std::atoi(attributes.getAttributeValue("y2").get_value_or("0").c_str()); WhiteboardLineElement::ref whiteboardElement = boost::make_shared(x1, y1, x2, y2); + Color color(attributes.getAttributeValue("stroke").get_value_or("#00000")); + + std::string opacity = attributes.getAttributeValue("opacity").get_value_or("1"); + if (opacity.find('.') != std::string::npos) { + opacity = opacity.substr(opacity.find('.')+1, 2); + color.setAlpha(std::atoi(opacity.c_str())*255/100); + } + + whiteboardElement->setColor(color); + + int penWidth = std::atoi(attributes.getAttributeValue("stroke-width").get_value_or("1").c_str()); + whiteboardElement->setPenWidth(penWidth); + getPayloadInternal()->setElement(whiteboardElement); } } diff --git a/Swiften/SConscript b/Swiften/SConscript index 1fa47ca..140f696 100644 --- a/Swiften/SConscript +++ b/Swiften/SConscript @@ -206,6 +206,7 @@ if env["SCONS_STAGE"] == "build" : "Whiteboard/IncomingWhiteboardSession.cpp", "Whiteboard/OutgoingWhiteboardSession.cpp", "Whiteboard/WhiteboardSessionManager.cpp", + "Whiteboard/Elements/Color.cpp", ] SConscript(dirs = [ diff --git a/Swiften/Serializer/PayloadSerializers/WhiteboardSerializer.cpp b/Swiften/Serializer/PayloadSerializers/WhiteboardSerializer.cpp index def21b1..f02791c 100644 --- a/Swiften/Serializer/PayloadSerializers/WhiteboardSerializer.cpp +++ b/Swiften/Serializer/PayloadSerializers/WhiteboardSerializer.cpp @@ -16,9 +16,19 @@ namespace Swift { element->setAttribute("y1", intToStr(line->y1())); element->setAttribute("x2", intToStr(line->x2())); element->setAttribute("y2", intToStr(line->y2())); + element->setAttribute("id", line->getID()); + element->setAttribute("stroke", line->getColor().toHex()); + element->setAttribute("stroke-width", intToStr(line->getPenWidth())); + int alpha = line->getColor().getAlpha(); + int opacity = 100*alpha/254; + if (opacity == 100) { + element->setAttribute("opacity", "1"); + } else { + element->setAttribute("opacity", "."+intToStr(opacity)); + } } - std::string WhiteboardElementSerializingVisitor::intToStr(const int t) { + std::string WhiteboardElementSerializingVisitor::intToStr(const int t) const { std::stringstream ss; ss << t; return ss.str(); diff --git a/Swiften/Serializer/PayloadSerializers/WhiteboardSerializer.h b/Swiften/Serializer/PayloadSerializers/WhiteboardSerializer.h index 219ebcc..50cf252 100644 --- a/Swiften/Serializer/PayloadSerializers/WhiteboardSerializer.h +++ b/Swiften/Serializer/PayloadSerializers/WhiteboardSerializer.h @@ -19,8 +19,8 @@ namespace Swift { XMLElement::ref getResult() const; private: - std::string intToStr(const int t); - + std::string intToStr(const int t) const; + XMLElement::ref element; }; diff --git a/Swiften/Whiteboard/Elements/Color.cpp b/Swiften/Whiteboard/Elements/Color.cpp new file mode 100644 index 0000000..b614a2a --- /dev/null +++ b/Swiften/Whiteboard/Elements/Color.cpp @@ -0,0 +1,82 @@ +/* + * Copyright (c) 2012 Mateusz Piękos + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +#include +#include +#include +#include +#include + +namespace Swift { + Color::Color() : red_(0), green_(0), blue_(0), alpha_(255) { + } + + Color::Color(int red, int green, int blue, int alpha) : red_(red), green_(green), blue_(blue), alpha_(alpha) { + } + + Color::Color(const std::string& hex) : alpha_(255) { + if (hex.size() == 7) { + char temp[3]; + hex.copy(temp, 2, 1); + temp[2] = 0; + std::sscanf(temp, "%x", &red_); + hex.copy(temp, 2, 3); + temp[2] = 0; + std::sscanf(temp, "%x", &green_); + hex.copy(temp, 2, 5); + temp[2] = 0; + std::sscanf(temp, "%x", &blue_); + } + } + + std::string Color::toHex() const { + std::string result = "#"; + std::string hex; + hex = intToStr(red_, 16); + if (hex.size() == 1) { + result += "0"; + } + result += hex; + hex = intToStr(green_, 16); + if (hex.size() == 1) { + result += "0"; + } + result += hex; + hex = intToStr(blue_, 16); + if (hex.size() == 1) { + result += "0"; + } + result += hex; + return result; + } + + int Color::getRed() const { + return red_; + } + + int Color::getGreen() const { + return green_; + } + + int Color::getBlue() const { + return blue_; + } + + int Color::getAlpha() const { + return alpha_; + } + + void Color::setAlpha(int alpha) { + alpha_ = alpha; + } + + std::string Color::intToStr(int t, int base) const { + std::stringstream ss; + ss << std::setbase(base); + ss << t; + return ss.str(); + } +} diff --git a/Swiften/Whiteboard/Elements/Color.h b/Swiften/Whiteboard/Elements/Color.h new file mode 100644 index 0000000..b7cca5a --- /dev/null +++ b/Swiften/Whiteboard/Elements/Color.h @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2012 Mateusz Piękos + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +#pragma once + +#include + +namespace Swift { + class Color { + public: + Color(); + Color(int red, int green, int blue, int alpha = 255); + Color(const std::string& hex); + std::string toHex() const; + int getRed() const; + int getGreen() const; + int getBlue() const; + int getAlpha() const; + void setAlpha(int alpha); + + private: + std::string intToStr(int t, int base = 10) const; + + int red_, green_, blue_; + int alpha_; + }; +} diff --git a/Swiften/Whiteboard/Elements/WhiteboardLineElement.h b/Swiften/Whiteboard/Elements/WhiteboardLineElement.h index b64e397..0088189 100644 --- a/Swiften/Whiteboard/Elements/WhiteboardLineElement.h +++ b/Swiften/Whiteboard/Elements/WhiteboardLineElement.h @@ -7,6 +7,7 @@ #pragma once #include +#include namespace Swift { class WhiteboardLineElement : public WhiteboardElement { @@ -36,12 +37,38 @@ namespace Swift { return y2_; } + const Color& getColor() const { + return color_; + } + + void setColor(const Color& color) { + color_ = color; + } + + std::string getID() const { + return id_; + } + + void setID(const std::string& id) { + id_ = id; + } + + int getPenWidth() const { + return penWidth_; + } + + void setPenWidth(const int penWidth) { + penWidth_ = penWidth; + } + void accept(WhiteboardElementVisitor& visitor) { visitor.visit(this); } private: int x1_, y1_, x2_, y2_; - std::string id; + Color color_; + int penWidth_; + std::string id_; }; } -- cgit v0.10.2-6-g49f6