From e6389d9be9135d5648033abea8bdb75959f64070 Mon Sep 17 00:00:00 2001
From: Mateusz Piekos <mateuszpiekos@gmail.com>
Date: Wed, 18 Jul 2012 12:36:42 +0200
Subject: Modified code to send TextItem updates


diff --git a/Swift/QtUI/Whiteboard/GView.cpp b/Swift/QtUI/Whiteboard/GView.cpp
index 1e77b62..eead3c7 100644
--- a/Swift/QtUI/Whiteboard/GView.cpp
+++ b/Swift/QtUI/Whiteboard/GView.cpp
@@ -48,33 +48,21 @@ namespace Swift {
 	}
 
 	void GView::addItem(QGraphicsItem* item, QString id, int pos) {
-//		QList<QGraphicsItem*> items = scene()->items();
-//		std::cout << "pos: " << pos << " size: " << items.size() << std::endl;
-//		if (mousePressed && pos >= items_.size()) {
-//			pos++;
-//		}
 		itemsMap_.insert(id, item);
 		if (pos > items_.size()) {
 			item->setZValue(zValue++);
 			scene()->addItem(item);
 			items_.append(item);
 		} else {
-			QList<QGraphicsItem*>::iterator it = items_.begin();
-			for (int i = 1; i < pos; i++) {
-				++it;
-			}
-			//	std::cout << id.toStdString() << " before: " << (*it)->data(100).toString().toStdString() << std::endl;
-			item->setZValue((*it)->zValue());
+			QGraphicsItem* temp = items_.at(pos-1);
+			item->setZValue(temp->zValue());
 			scene()->addItem(item);
-			item->stackBefore(*it);
-			//		--it;
-			items_.insert(it, item);
+			item->stackBefore(temp);
+			items_.insert(pos-1, item);
 		}
-
-//		items = scene()->items();
-		std::cout << "items:" << std::endl;
+		std::cout << "items in:" << std::endl;
 		for (QList<QGraphicsItem*>::const_iterator it = items_.begin(); it != items_.end(); ++it) {
-			std::cout << (*it)->data(100).toString().toStdString() << std::endl;
+			std::cout << "item: " << (*it)->data(100).toString().toStdString() << std::endl;
 		}
 		std::cout << std::endl;
 	}
@@ -86,6 +74,7 @@ namespace Swift {
 	void GView::clear() {
 		scene()->clear();
 		items_.clear();
+		itemsMap_.clear();
 	}
 
 	QGraphicsItem* GView::getItem(QString id) {
@@ -247,15 +236,15 @@ namespace Swift {
 				QGraphicsItem* item = scene()->items(rect).first();
 				QString id = item->data(100).toString();
 				deleteItem(id);
-				int i = 1;
+/*				int i = 1;
 				QList<QGraphicsItem*>::const_iterator it;
 				for (it = items_.begin(); it != items_.end(); ++it) {
 					if (*it == item) {
 						break;
 					}
 					i++;
-				}
-				itemDeleted(id, i);
+					}*/
+				itemDeleted(id, items_.indexOf(item)+1);
 			}
 		}
 		else if (mode == Circle) {
@@ -301,13 +290,13 @@ namespace Swift {
 			QPointF point = this->mapToScene(event->pos());
 			QGraphicsTextItem* item = scene()->addText("");
 			QString id = getNewID();
-			items_.append(item);
-			item->setZValue(zValue++);
+//			items_.append(item);
+//			item->setZValue(zValue++);
 			item->setData(100, id);
 			item->setData(101, items_.size());
 			item->setDefaultTextColor(pen.color());
 			textDialog = new TextDialog(item, this);
-			connect(textDialog, SIGNAL(accepted(QGraphicsItem*)), this, SIGNAL(lastItemChanged(QGraphicsItem*)));
+			connect(textDialog, SIGNAL(accepted(QGraphicsTextItem*)), this, SLOT(handleTextItemModified(QGraphicsTextItem*)));
 			textDialog->setAttribute(Qt::WA_DeleteOnClose);
 			textDialog->show();
 			item->setPos(point);
@@ -362,20 +351,13 @@ namespace Swift {
 		mousePressed = false;
 		QGraphicsPolygonItem* polygon = dynamic_cast<QGraphicsPolygonItem*>(lastItem);
 		if (polygon && polygon->polygon().size() >= 3) {
-			int i = 1;
-			for (QList<QGraphicsItem*>::const_iterator it = items_.begin(); it != items_.end(); ++it) {
-				if (*it == polygon) {
-					break;
-				}
-				i++;
-			}
-			lastItemChanged(polygon, i, Update);
+			lastItemChanged(polygon, items_.indexOf(polygon)+1, Update);
 		} else if (lastItem) {
 			zValue++;
 			lastItem->setZValue(zValue++);
 			items_.append(lastItem);
 			itemsMap_.insert(lastItem->data(100).toString(), lastItem);
-			std::cout << "items:" << std::endl;
+			std::cout << "items out:" << std::endl;
 			for (QList<QGraphicsItem*>::const_iterator it = items_.begin(); it != items_.end(); ++it) {
 				std::cout << (*it)->data(100).toString().toStdString() << std::endl;
 			}
@@ -384,19 +366,15 @@ namespace Swift {
 			lastItemChanged(lastItem, items_.size(), New);
 		} else if (selectionRect){
 			QGraphicsItem* item = selectionRect->data(1).value<QGraphicsItem*>();
-			int i = 1;
-			for (QList<QGraphicsItem*>::const_iterator it = items_.begin(); it != items_.end(); ++it) {
-				if (*it == item) {
-					break;
-				}
-				i++;
-			}
-			lastItemChanged(item, i, Update);
+			lastItemChanged(item, items_.indexOf(item)+1, Update);
 		}
-//		lastItem = 0;
 	}
 
 
+	void GView::handleTextItemModified(QGraphicsTextItem* item) {
+		lastItemChanged(item, item->data(101).toInt(), Update);
+	}
+
 	void GView::moveUpSelectedItem()
 	{
 		QGraphicsItem* item = selectionRect->data(1).value<QGraphicsItem*>();
diff --git a/Swift/QtUI/Whiteboard/GView.h b/Swift/QtUI/Whiteboard/GView.h
index 9a26b01..e18cb74 100644
--- a/Swift/QtUI/Whiteboard/GView.h
+++ b/Swift/QtUI/Whiteboard/GView.h
@@ -42,6 +42,8 @@ namespace Swift {
 		void moveUpSelectedItem();
 		void moveDownSelectedItem();
 
+	private slots:
+		void handleTextItemModified(QGraphicsTextItem*);
 	private:
 		QString getNewID();
 
diff --git a/Swift/QtUI/Whiteboard/QtWhiteboardWindow.cpp b/Swift/QtUI/Whiteboard/QtWhiteboardWindow.cpp
index 6df68f3..d89a7f6 100644
--- a/Swift/QtUI/Whiteboard/QtWhiteboardWindow.cpp
+++ b/Swift/QtUI/Whiteboard/QtWhiteboardWindow.cpp
@@ -298,7 +298,7 @@ namespace Swift {
 			QColor color = textItem->defaultTextColor();
 			element->setColor(Color(color.red(), color.green(), color.blue(), color.alpha()));
 
-			element->setID(rectItem->data(100).toString().toStdString());
+			element->setID(textItem->data(100).toString().toStdString());
 //			whiteboardSession_->sendElement(element);
 			el = element;
 		}
diff --git a/Swift/QtUI/Whiteboard/TextDialog.h b/Swift/QtUI/Whiteboard/TextDialog.h
index 7f0e56a..f4d9a13 100644
--- a/Swift/QtUI/Whiteboard/TextDialog.h
+++ b/Swift/QtUI/Whiteboard/TextDialog.h
@@ -34,7 +34,7 @@ namespace Swift {
 		QSpinBox* fontSizeBox;
 
 	signals:
-		void accepted(QGraphicsItem* item);
+		void accepted(QGraphicsTextItem* item);
 
 	private slots:
 		void accept();
diff --git a/Swift/QtUI/Whiteboard/WhiteboardElementDrawingVisitor.h b/Swift/QtUI/Whiteboard/WhiteboardElementDrawingVisitor.h
index 4ed1af5..528c72a 100644
--- a/Swift/QtUI/Whiteboard/WhiteboardElementDrawingVisitor.h
+++ b/Swift/QtUI/Whiteboard/WhiteboardElementDrawingVisitor.h
@@ -126,17 +126,24 @@ 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()));
-
+			QGraphicsTextItem* item;
 			QString id = P2QSTRING(element.getID());
-			item->setData(100, id);
-			graphicsView_->addItem(item, id, pos_);
+			if (type_ == GView::New) {
+				item = new QGraphicsTextItem;
+				graphicsView_->addItem(item, id, pos_);
+			} else {
+				item = qgraphicsitem_cast<QGraphicsTextItem*>(graphicsView_->getItem(id));
+			}
+			if (item) {
+				item->setPlainText(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()));
+				item->setData(100, id);
+			}
 		}
 
 		void visit(WhiteboardEllipseElement& element) {
diff --git a/Swiften/Parser/PayloadParsers/WhiteboardParser.cpp b/Swiften/Parser/PayloadParsers/WhiteboardParser.cpp
index ea00335..cdcbb8c 100644
--- a/Swiften/Parser/PayloadParsers/WhiteboardParser.cpp
+++ b/Swiften/Parser/PayloadParsers/WhiteboardParser.cpp
@@ -21,6 +21,7 @@
 
 namespace Swift {
 	WhiteboardParser::WhiteboardParser() : actualIsText(false), level_(0) {
+		operation = NULL;
 	}
 
 	void WhiteboardParser::handleStartElement(const std::string& element, const std::string&, const AttributeMap& attributes) {
@@ -30,31 +31,21 @@ namespace Swift {
 			std::string type = attributes.getAttributeValue("type").get_value_or("");
 			if (type == "insert") {
 				WhiteboardInsertOperation::ref insertOp = boost::make_shared<WhiteboardInsertOperation>();
-				try {
-					insertOp->setID(attributes.getAttributeValue("id").get_value_or(""));
-					insertOp->setParentID(attributes.getAttributeValue("parentid").get_value_or(""));
-					insertOp->setPos(boost::lexical_cast<int>(attributes.getAttributeValue("pos").get_value_or("0")));
-				} catch (boost::bad_lexical_cast&) {
-				}
 				operation = insertOp;
 			} else if (type == "update") {
 				WhiteboardUpdateOperation::ref updateOp = boost::make_shared<WhiteboardUpdateOperation>();
-				try {
-					updateOp->setID(attributes.getAttributeValue("id").get_value_or(""));
-					updateOp->setParentID(attributes.getAttributeValue("parentid").get_value_or(""));
-					updateOp->setPos(boost::lexical_cast<int>(attributes.getAttributeValue("pos").get_value_or("0")));
-				} catch (boost::bad_lexical_cast&) {
-				}
 				operation = updateOp;
 			} else if (type == "delete") {
 				WhiteboardDeleteOperation::ref deleteOp = boost::make_shared<WhiteboardDeleteOperation>();
+				operation = deleteOp;
+			}
+			if (operation) {
 				try {
-					deleteOp->setID(attributes.getAttributeValue("id").get_value_or(""));
-					deleteOp->setParentID(attributes.getAttributeValue("parentid").get_value_or(""));
-					deleteOp->setPos(boost::lexical_cast<int>(attributes.getAttributeValue("pos").get_value_or("0")));
+					operation->setID(attributes.getAttributeValue("id").get_value_or(""));
+					operation->setParentID(attributes.getAttributeValue("parentid").get_value_or(""));
+					operation->setPos(boost::lexical_cast<int>(attributes.getAttributeValue("pos").get_value_or("0")));
 				} catch (boost::bad_lexical_cast&) {
 				}
-				operation = deleteOp;
 			}
 
 		} else if (level_ == 2) {
-- 
cgit v0.10.2-6-g49f6