From 60076b8e5dc557716e82d7588ff9a99247e485bf Mon Sep 17 00:00:00 2001 From: Mateusz Piekos Date: Tue, 26 Jun 2012 14:23:47 +0200 Subject: Added handling of text elements diff --git a/Swift/QtUI/Whiteboard/GView.cpp b/Swift/QtUI/Whiteboard/GView.cpp index badbb8e..a450808 100644 --- a/Swift/QtUI/Whiteboard/GView.cpp +++ b/Swift/QtUI/Whiteboard/GView.cpp @@ -5,6 +5,7 @@ */ #include "GView.h" +#include namespace Swift { GView::GView(QGraphicsScene* scene, QWidget* parent) : QGraphicsView(scene, parent), brush(QColor(Qt::white)) @@ -162,7 +163,7 @@ 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()); + QString id = P2QSTRING(idGenerator.generateID()); items_.insert(id, item); item->setZValue(zValue++); item->setData(0, id); @@ -171,7 +172,7 @@ namespace Swift { else if (mode == Rect) { QPointF point = this->mapToScene(event->pos()); QGraphicsRectItem* item = scene()->addRect(point.x(), point.y(), 0, 0, pen, brush); - QString id = QString::fromStdString(idGenerator.generateID()); + QString id = P2QSTRING(idGenerator.generateID()); items_.insert(id, item); item->setZValue(zValue++); item->setData(0, point); @@ -193,7 +194,7 @@ namespace Swift { else if (mode == Circle) { QPointF point = this->mapToScene(event->pos()); QGraphicsEllipseItem* item = scene()->addEllipse(point.x(), point.y(), 0, 0, pen, brush); - QString id = QString::fromStdString(idGenerator.generateID()); + QString id = P2QSTRING(idGenerator.generateID()); items_.insert(id, item); item->setZValue(zValue++); item->setData(0, point); @@ -203,7 +204,7 @@ namespace Swift { else if (mode == HandLine) { QPointF point = this->mapToScene(event->pos()); FreehandLineItem* item = new FreehandLineItem; - QString id = QString::fromStdString(idGenerator.generateID()); + QString id = P2QSTRING(idGenerator.generateID()); items_.insert(id, item); item->setPen(pen); item->setStartPoint(point); @@ -216,7 +217,7 @@ namespace Swift { QPointF point = this->mapToScene(event->pos()); QPainterPath path; QGraphicsPathItem* item = scene()->addPath(path, pen, brush); - QString id = QString::fromStdString(idGenerator.generateID()); + QString id = P2QSTRING(idGenerator.generateID()); items_.insert(id, item); item->setZValue(zValue++); item->setData(0, point); @@ -226,13 +227,13 @@ namespace Swift { else if (mode == Text) { QPointF point = this->mapToScene(event->pos()); QGraphicsTextItem* item = scene()->addText(""); - QString id = QString::fromStdString(idGenerator.generateID()); + QString id = P2QSTRING(idGenerator.generateID()); items_.insert(id, item); item->setZValue(zValue++); item->setData(0, id); item->setDefaultTextColor(pen.color()); textDialog = new TextDialog(item, this); - connect(textDialog, SIGNAL(accepted()), this, SIGNAL(lastItemChanged(textDialog))); + connect(textDialog, SIGNAL(accepted(QGraphicsItem*)), this, SIGNAL(lastItemChanged(QGraphicsItem*))); textDialog->setAttribute(Qt::WA_DeleteOnClose); textDialog->show(); item->setPos(point); @@ -246,7 +247,7 @@ namespace Swift { polygon.append(point); polygon.append(point); item = scene()->addPolygon(polygon, pen, brush); - QString id = QString::fromStdString(idGenerator.generateID()); + QString id = P2QSTRING(idGenerator.generateID()); items_.insert(id, item); item->setZValue(zValue++); item->setData(0, id); diff --git a/Swift/QtUI/Whiteboard/QtWhiteboardWindow.cpp b/Swift/QtUI/Whiteboard/QtWhiteboardWindow.cpp index 55d3479..6e259a2 100644 --- a/Swift/QtUI/Whiteboard/QtWhiteboardWindow.cpp +++ b/Swift/QtUI/Whiteboard/QtWhiteboardWindow.cpp @@ -266,7 +266,7 @@ namespace Swift { QPointF point = textItem->pos(); WhiteboardTextElement::ref element = boost::make_shared(point.x(), point.y()); element->setText(textItem->toPlainText().toStdString()); - + element->setSize(textItem->font().pointSize()); QColor color = textItem->defaultTextColor(); element->setColor(Color(color.red(), color.green(), color.blue(), color.alpha())); diff --git a/Swift/QtUI/Whiteboard/TextDialog.cpp b/Swift/QtUI/Whiteboard/TextDialog.cpp index caa78ce..021895a 100644 --- a/Swift/QtUI/Whiteboard/TextDialog.cpp +++ b/Swift/QtUI/Whiteboard/TextDialog.cpp @@ -44,4 +44,9 @@ namespace Swift { font.setPointSize(i); item->setFont(font); } + + void TextDialog::accept() { + emit accepted(item); + done(QDialog::Accepted); + } } diff --git a/Swift/QtUI/Whiteboard/TextDialog.h b/Swift/QtUI/Whiteboard/TextDialog.h index 8b31d5b..7f0e56a 100644 --- a/Swift/QtUI/Whiteboard/TextDialog.h +++ b/Swift/QtUI/Whiteboard/TextDialog.h @@ -33,7 +33,11 @@ namespace Swift { QHBoxLayout* hLayout; QSpinBox* fontSizeBox; + signals: + void accepted(QGraphicsItem* item); + private slots: + void accept(); void changeItemText(const QString &text); void changeItemFontSize(int i); }; diff --git a/Swift/QtUI/Whiteboard/WhiteboardElementDrawingVisitor.h b/Swift/QtUI/Whiteboard/WhiteboardElementDrawingVisitor.h index 4b2f7ed..214ea10 100644 --- a/Swift/QtUI/Whiteboard/WhiteboardElementDrawingVisitor.h +++ b/Swift/QtUI/Whiteboard/WhiteboardElementDrawingVisitor.h @@ -85,7 +85,14 @@ namespace Swift { } void visit(WhiteboardTextElement& element) { - + QGraphicsTextItem* item = new QGraphicsTextItem(P2QSTRING(element.getText())); + item->setPos(QPointF(element.getX(), element.getY())); + QFont font = item->font(); + font.setPointSize(element.getSize()); + item->setFont(font); + Color color = element.getColor(); + item->setDefaultTextColor(QColor(color.getRed(), color.getGreen(), color.getBlue(), color.getAlpha())); + graphicsView_->scene()->addItem(item); } void visit(WhiteboardEllipseElement& element) { diff --git a/Swiften/Parser/PayloadParsers/WhiteboardParser.cpp b/Swiften/Parser/PayloadParsers/WhiteboardParser.cpp index b5cfa7b..aca3103 100644 --- a/Swiften/Parser/PayloadParsers/WhiteboardParser.cpp +++ b/Swiften/Parser/PayloadParsers/WhiteboardParser.cpp @@ -17,7 +17,7 @@ #include namespace Swift { - WhiteboardParser::WhiteboardParser() : level_(0) { + WhiteboardParser::WhiteboardParser() : actualIsText(false), level_(0) { } void WhiteboardParser::handleStartElement(const std::string& element, const std::string&, const AttributeMap& attributes) { @@ -167,7 +167,28 @@ namespace Swift { getPayloadInternal()->setElement(whiteboardElement); } else if (element == "text") { - WhiteboardTextElement::ref whiteboardElement = boost::make_shared(0,0); + int x = 0; + int y = 0; + try { + x = boost::lexical_cast(attributes.getAttributeValue("x").get_value_or("0")); + y = boost::lexical_cast(attributes.getAttributeValue("y").get_value_or("0")); + } catch (boost::bad_lexical_cast&) { + } + + WhiteboardTextElement::ref whiteboardElement = boost::make_shared(x, y); + + actualIsText = true; + Color color(attributes.getAttributeValue("fill").get_value_or("#000000")); + color.setAlpha(opacityToAlpha(attributes.getAttributeValue("opacity").get_value_or("1"))); + whiteboardElement->setColor(color); + + int fontSize = 1; + try { + fontSize = boost::lexical_cast(attributes.getAttributeValue("font-size").get_value_or("12")); + } catch (boost::bad_lexical_cast&) { + } + whiteboardElement->setSize(fontSize); + getPayloadInternal()->setElement(whiteboardElement); } else if (element == "ellipse") { int cx = 0; @@ -208,11 +229,18 @@ namespace Swift { --level_; if (level_ == 0) { getPayloadInternal()->setData(data_); + } else if (level_ == 1) { + if (element == "text") { + actualIsText = false; + } } } void WhiteboardParser::handleCharacterData(const std::string& data) { - data_ += data; + if (level_ == 2 && actualIsText) { + WhiteboardTextElement::ref element = boost::dynamic_pointer_cast(getPayloadInternal()->getElement()); + element->setText(data); + } } WhiteboardPayload::Type WhiteboardParser::stringToType(const std::string& type) const { diff --git a/Swiften/Parser/PayloadParsers/WhiteboardParser.h b/Swiften/Parser/PayloadParsers/WhiteboardParser.h index dfb2236..7c93de3 100644 --- a/Swiften/Parser/PayloadParsers/WhiteboardParser.h +++ b/Swiften/Parser/PayloadParsers/WhiteboardParser.h @@ -24,6 +24,7 @@ namespace Swift { int opacityToAlpha(std::string opacity) const; private: + bool actualIsText; int level_; std::string data_; }; -- cgit v0.10.2-6-g49f6