diff options
author | Mateusz Piekos <mateuszpiekos@gmail.com> | 2012-06-24 18:37:27 (GMT) |
---|---|---|
committer | Mateusz Piekos <mateuszpiekos@gmail.com> | 2012-06-24 18:37:27 (GMT) |
commit | 06bbc72598ece3e62b82471e474b0753d5439f00 (patch) | |
tree | b1af2cd0717aff4e8d3427e22873b87410375cd2 /Swiften/Parser/PayloadParsers | |
parent | 286a3d119ec95b235b09935296450ec36e640aeb (diff) | |
download | swift-contrib-06bbc72598ece3e62b82471e474b0753d5439f00.zip swift-contrib-06bbc72598ece3e62b82471e474b0753d5439f00.tar.bz2 |
Added handling of rects and basic handling of polygons
Diffstat (limited to 'Swiften/Parser/PayloadParsers')
-rw-r--r-- | Swiften/Parser/PayloadParsers/WhiteboardParser.cpp | 105 | ||||
-rw-r--r-- | Swiften/Parser/PayloadParsers/WhiteboardParser.h | 1 |
2 files changed, 89 insertions, 17 deletions
diff --git a/Swiften/Parser/PayloadParsers/WhiteboardParser.cpp b/Swiften/Parser/PayloadParsers/WhiteboardParser.cpp index 7182a8c..d1d57a0 100644 --- a/Swiften/Parser/PayloadParsers/WhiteboardParser.cpp +++ b/Swiften/Parser/PayloadParsers/WhiteboardParser.cpp @@ -6,6 +6,9 @@ #include <Swiften/Parser/PayloadParsers/WhiteboardParser.h> #include <Swiften/Whiteboard/Elements/WhiteboardLineElement.h> +#include <Swiften/Whiteboard/Elements/WhiteboardRectElement.h> +#include <Swiften/Whiteboard/Elements/WhiteboardTextElement.h> +#include <Swiften/Whiteboard/Elements/WhiteboardPolygonElement.h> #include <Swiften/Whiteboard/Elements/WhiteboardFreehandPathElement.h> #include <Swiften/Whiteboard/Elements/Color.h> #include <boost/optional.hpp> @@ -33,16 +36,9 @@ namespace Swift { } catch (boost::bad_lexical_cast&) { } WhiteboardLineElement::ref whiteboardElement = boost::make_shared<WhiteboardLineElement>(x1, y1, x2, y2); - Color color(attributes.getAttributeValue("stroke").get_value_or("#000000")); - std::string opacity = attributes.getAttributeValue("opacity").get_value_or("1"); - if (opacity.find('.') != std::string::npos) { - opacity = opacity.substr(opacity.find('.')+1, 2); - try { - color.setAlpha(boost::lexical_cast<int>(opacity)*255/100); - } catch (boost::bad_lexical_cast&) { - } - } + Color color(attributes.getAttributeValue("stroke").get_value_or("#000000")); + color.setAlpha(opacityToAlpha(attributes.getAttributeValue("opacity").get_value_or("1"))); whiteboardElement->setColor(color); int penWidth = 1; @@ -97,18 +93,81 @@ namespace Swift { whiteboardElement->setPenWidth(penWidth); Color color(attributes.getAttributeValue("stroke").get_value_or("#000000")); + color.setAlpha(opacityToAlpha(attributes.getAttributeValue("opacity").get_value_or("1"))); + whiteboardElement->setColor(color); - std::string opacity = attributes.getAttributeValue("opacity").get_value_or("1"); - if (opacity.find('.') != std::string::npos) { - opacity = opacity.substr(opacity.find('.')+1, 2); - try { - color.setAlpha(boost::lexical_cast<int>(opacity)*255/100); - } catch (boost::bad_lexical_cast&) { + getPayloadInternal()->setElement(whiteboardElement); + } else if (element == "rect") { + int x = 0; + int y = 0; + int width = 0; + int height = 0; + try { + x = boost::lexical_cast<int>(attributes.getAttributeValue("x").get_value_or("0")); + y = boost::lexical_cast<int>(attributes.getAttributeValue("y").get_value_or("0")); + width = boost::lexical_cast<int>(attributes.getAttributeValue("width").get_value_or("0")); + height = boost::lexical_cast<int>(attributes.getAttributeValue("height").get_value_or("0")); + } catch (boost::bad_lexical_cast&) { + } + + WhiteboardRectElement::ref whiteboardElement = boost::make_shared<WhiteboardRectElement>(x, y, width, height); + + int penWidth = 1; + try { + penWidth = boost::lexical_cast<int>(attributes.getAttributeValue("stroke-width").get_value_or("1")); + } catch (boost::bad_lexical_cast&) { + } + whiteboardElement->setPenWidth(penWidth); + + Color penColor(attributes.getAttributeValue("stroke").get_value_or("#000000")); + Color brushColor(attributes.getAttributeValue("fill").get_value_or("#000000")); + penColor.setAlpha(opacityToAlpha(attributes.getAttributeValue("opacity").get_value_or("1"))); + brushColor.setAlpha(opacityToAlpha(attributes.getAttributeValue("fill-opacity").get_value_or("1"))); + whiteboardElement->setPenColor(penColor); + whiteboardElement->setBrushColor(brushColor); + + getPayloadInternal()->setElement(whiteboardElement); + } else if (element == "polygon") { + WhiteboardPolygonElement::ref whiteboardElement = boost::make_shared<WhiteboardPolygonElement>(); + + std::string pointsData = attributes.getAttributeValue("points").get_value_or(""); + std::vector<std::pair<int, int> > points; + int pos = 0; + int npos; + int x, y; + try { + while (pos < pointsData.size()) { + npos = pointsData.find(',', pos); + x = boost::lexical_cast<int>(pointsData.substr(pos, npos-pos)); + pos = npos+1; + npos = pointsData.find(' ', pos); + y = boost::lexical_cast<int>(pointsData.substr(pos, npos-pos)); + pos = npos+1; + points.push_back(std::pair<int, int>(x, y)); } - } - whiteboardElement->setColor(color); + } catch (boost::bad_lexical_cast&) { + } + + whiteboardElement->setPoints(points); + + int penWidth = 1; + try { + penWidth = boost::lexical_cast<int>(attributes.getAttributeValue("stroke-width").get_value_or("1")); + } catch (boost::bad_lexical_cast&) { + } + whiteboardElement->setPenWidth(penWidth); + + Color penColor(attributes.getAttributeValue("stroke").get_value_or("#000000")); + Color brushColor(attributes.getAttributeValue("fill").get_value_or("#000000")); + penColor.setAlpha(opacityToAlpha(attributes.getAttributeValue("opacity").get_value_or("1"))); + brushColor.setAlpha(opacityToAlpha(attributes.getAttributeValue("fill-opacity").get_value_or("1"))); + whiteboardElement->setPenColor(penColor); + whiteboardElement->setBrushColor(brushColor); getPayloadInternal()->setElement(whiteboardElement); + } else if (element == "text") { + WhiteboardTextElement::ref whiteboardElement = boost::make_shared<WhiteboardTextElement>(0,0); + getPayloadInternal()->setElement(whiteboardElement); } } ++level_; @@ -136,4 +195,16 @@ namespace Swift { return WhiteboardPayload::SessionTerminate; } } + + int WhiteboardParser::opacityToAlpha(std::string opacity) const { + int value = 255; + if (opacity.find('.') != std::string::npos) { + opacity = opacity.substr(opacity.find('.')+1, 2); + try { + value = boost::lexical_cast<int>(opacity)*255/100; + } catch (boost::bad_lexical_cast&) { + } + } + return value; + } } diff --git a/Swiften/Parser/PayloadParsers/WhiteboardParser.h b/Swiften/Parser/PayloadParsers/WhiteboardParser.h index 79d0f27..dfb2236 100644 --- a/Swiften/Parser/PayloadParsers/WhiteboardParser.h +++ b/Swiften/Parser/PayloadParsers/WhiteboardParser.h @@ -21,6 +21,7 @@ namespace Swift { private: WhiteboardPayload::Type stringToType(const std::string& type) const; + int opacityToAlpha(std::string opacity) const; private: int level_; |