From 4321da432e3332e868abf07678f15d305fec2a4b Mon Sep 17 00:00:00 2001 From: Mateusz Piekos Date: Tue, 31 Jul 2012 10:31:54 +0200 Subject: Upgraded update operation with ability to move elements diff --git a/Swift/QtUI/Whiteboard/GView.cpp b/Swift/QtUI/Whiteboard/GView.cpp index a4f000b..0fb42a0 100644 --- a/Swift/QtUI/Whiteboard/GView.cpp +++ b/Swift/QtUI/Whiteboard/GView.cpp @@ -380,14 +380,49 @@ namespace Swift { void GView::moveUpSelectedItem() { - QGraphicsItem* item = selectionRect->data(1).value(); - item->setZValue(item->zValue()+1); + if (selectionRect) { + QGraphicsItem* item = selectionRect->data(1).value(); + int pos = items_.indexOf(item); + if (pos < items_.size()-1) { + lastItemChanged(item, pos+1, MoveUp); + move(item, pos+2); + } + } } void GView::moveDownSelectedItem() { - QGraphicsItem* item = selectionRect->data(1).value(); - item->setZValue(item->zValue()-1); + if (selectionRect) { + QGraphicsItem* item = selectionRect->data(1).value(); + int pos = items_.indexOf(item); + if (pos > 0) { + lastItemChanged(item, pos+1, MoveDown); + move(item, pos); + } + } + } + + void GView::move(QGraphicsItem* item, int npos) { + int pos = items_.indexOf(item); + QGraphicsItem* itemAfter = NULL; + if (npos-1 > pos) { + if (npos == items_.size()) { + item->setZValue(zValue++); + } else { + itemAfter = items_.at(npos); + } + + items_.insert(npos, item); + items_.removeAt(pos); + } else if (npos-1 < pos) { + itemAfter = items_.at(npos-1); + items_.insert(npos-1, item); + items_.removeAt(pos+1); + } + if (itemAfter) { + item->setZValue(itemAfter->zValue()); + item->stackBefore(itemAfter); + } } void GView::changePenAndBrush(QGraphicsItem* item, QPen pen, QBrush brush) { diff --git a/Swift/QtUI/Whiteboard/GView.h b/Swift/QtUI/Whiteboard/GView.h index f219929..f0d4fef 100644 --- a/Swift/QtUI/Whiteboard/GView.h +++ b/Swift/QtUI/Whiteboard/GView.h @@ -21,7 +21,7 @@ namespace Swift { Q_OBJECT; public: enum Mode { Rubber, Line, Rect, Circle, HandLine, FilledHandLine, Text, Polygon, Select }; - enum Type { New, Update }; + enum Type { New, Update, MoveUp, MoveDown }; GView(QGraphicsScene* scene, QWidget* parent = 0); void setLineWidth(int i); void setLineColor(QColor color); @@ -38,6 +38,7 @@ namespace Swift { QGraphicsItem* getItem(QString id); void deleteItem(QString id); QString getNewID(); + void move(QGraphicsItem* item, int npos); public slots: void moveUpSelectedItem(); diff --git a/Swift/QtUI/Whiteboard/QtWhiteboardWindow.cpp b/Swift/QtUI/Whiteboard/QtWhiteboardWindow.cpp index 960254c..5654c06 100644 --- a/Swift/QtUI/Whiteboard/QtWhiteboardWindow.cpp +++ b/Swift/QtUI/Whiteboard/QtWhiteboardWindow.cpp @@ -34,7 +34,7 @@ namespace Swift { scene->setSceneRect(0, 0, 400, 400); //BspTreeIndex is buggy, there are problems after removing items //from scene - scene->setItemIndexMethod(QGraphicsScene::NoIndex); + //scene->setItemIndexMethod(QGraphicsScene::NoIndex); graphicsView = new GView(scene, this); graphicsView->setMode(GView::Line); @@ -147,6 +147,9 @@ namespace Swift { if (updateOp) { WhiteboardElementDrawingVisitor visitor(graphicsView, operation->getPos(), GView::Update); updateOp->getElement()->accept(visitor); + if (updateOp->getPos() != updateOp->getNewPos()) { + graphicsView->move(graphicsView->getItem(P2QSTRING(updateOp->getElement()->getID())), updateOp->getNewPos()); + } } WhiteboardDeleteOperation::ref deleteOp = boost::dynamic_pointer_cast(operation); @@ -343,7 +346,6 @@ namespace Swift { if (type == GView::New) { WhiteboardInsertOperation::ref insertOp = boost::make_shared(); -// insertOp->setID(el->getID()); insertOp->setID(Q2PSTRING(graphicsView->getNewID())); insertOp->setPos(pos); insertOp->setElement(el); @@ -352,9 +354,15 @@ namespace Swift { whiteboardSession_->sendOperation(insertOp); } else { WhiteboardUpdateOperation::ref updateOp = boost::make_shared(); -// updateOp->setID(el->getID()); updateOp->setID(Q2PSTRING(graphicsView->getNewID())); updateOp->setPos(pos); + if (type == GView::Update) { + updateOp->setNewPos(pos); + } else if (type == GView::MoveUp) { + updateOp->setNewPos(pos+1); + } else if (type == GView::MoveDown) { + updateOp->setNewPos(pos-1); + } updateOp->setElement(el); updateOp->setParentID(lastOpID); lastOpID = updateOp->getID(); diff --git a/Swiften/Parser/PayloadParsers/WhiteboardParser.cpp b/Swiften/Parser/PayloadParsers/WhiteboardParser.cpp index 12bed17..7ec181f 100644 --- a/Swiften/Parser/PayloadParsers/WhiteboardParser.cpp +++ b/Swiften/Parser/PayloadParsers/WhiteboardParser.cpp @@ -33,6 +33,8 @@ namespace Swift { operation = insertOp; } else if (type == "update") { WhiteboardUpdateOperation::ref updateOp = boost::make_shared(); + std::string move = attributes.getAttributeValue("newpos").get_value_or("0"); + updateOp->setNewPos(boost::lexical_cast(attributes.getAttributeValue("newpos").get_value_or("0"))); operation = updateOp; } else if (type == "delete") { WhiteboardDeleteOperation::ref deleteOp = boost::make_shared(); diff --git a/Swiften/Serializer/PayloadSerializers/WhiteboardSerializer.cpp b/Swiften/Serializer/PayloadSerializers/WhiteboardSerializer.cpp index a4fd618..e1380b2 100644 --- a/Swiften/Serializer/PayloadSerializers/WhiteboardSerializer.cpp +++ b/Swiften/Serializer/PayloadSerializers/WhiteboardSerializer.cpp @@ -160,6 +160,7 @@ namespace Swift { operationNode->setAttribute("pos", boost::lexical_cast(updateOp->getPos())); operationNode->setAttribute("id", updateOp->getID()); operationNode->setAttribute("parentid", updateOp->getParentID()); + operationNode->setAttribute("newpos", boost::lexical_cast(updateOp->getNewPos())); } catch (boost::bad_lexical_cast&) { } updateOp->getElement()->accept(visitor); diff --git a/Swiften/Whiteboard/Operations/WhiteboardUpdateOperation.h b/Swiften/Whiteboard/Operations/WhiteboardUpdateOperation.h index 7c4b088..1b79043 100644 --- a/Swiften/Whiteboard/Operations/WhiteboardUpdateOperation.h +++ b/Swiften/Whiteboard/Operations/WhiteboardUpdateOperation.h @@ -26,7 +26,16 @@ namespace Swift { element_ = element; } + int getNewPos() const { + return newPos_; + } + + void setNewPos(int newPos) { + newPos_ = newPos; + } + private: WhiteboardElement::ref element_; + int newPos_; }; } diff --git a/Swiften/Whiteboard/WhiteboardTransformer.cpp b/Swiften/Whiteboard/WhiteboardTransformer.cpp index c9dbbbd..88ab7d5 100644 --- a/Swiften/Whiteboard/WhiteboardTransformer.cpp +++ b/Swiften/Whiteboard/WhiteboardTransformer.cpp @@ -61,6 +61,24 @@ namespace Swift { result.second = boost::make_shared(*clientOp); result.second->setParentID(serverOp->getID()); } + + if (clientOp->getPos() < serverOp->getPos() && clientOp->getNewPos() > serverOp->getPos()) { + result.first->setPos(result.first->getPos()-1); + if (clientOp->getNewPos() >= serverOp->getNewPos()) { + result.first->setNewPos(result.first->getNewPos()-1); + } + } else if (clientOp->getNewPos() >= serverOp->getNewPos()) { + result.first->setNewPos(result.first->getNewPos()-1); + } + + if (serverOp->getPos() < clientOp->getPos() && serverOp->getNewPos() > clientOp->getPos()) { + result.second->setPos(result.second->getPos()-1); + if (serverOp->getNewPos() >= clientOp->getNewPos()) { + result.second->setNewPos(result.second->getNewPos()-1); + } + } else if (serverOp->getNewPos() >= clientOp->getNewPos()) { + result.second->setNewPos(result.second->getNewPos()-1); + } return result; } @@ -144,7 +162,8 @@ namespace Swift { std::pair result; result.first = boost::make_shared(*serverOp); result.first->setParentID(clientOp->getID()); - result.second = boost::make_shared(*clientOp); + WhiteboardUpdateOperation::ref updateOp = boost::make_shared(*clientOp); + result.second = updateOp; result.second->setParentID(serverOp->getID()); if (clientOp->getPos() == serverOp->getPos()) { WhiteboardDeleteOperation::ref deleteOp = boost::make_shared(); @@ -153,16 +172,21 @@ namespace Swift { result.second->setID(clientOp->getID()); result.second->setParentID(serverOp->getID()); deleteOp->setElementID(serverOp->getElementID()); + } else if (clientOp->getPos() > serverOp->getPos() && clientOp->getNewPos() <= serverOp->getPos()) { + result.second->setPos(result.second->getPos()-1); + } else if (clientOp->getPos() < serverOp->getPos() && clientOp->getNewPos() >= serverOp->getPos()) { + updateOp->setNewPos(updateOp->getNewPos()-1); } else if (clientOp->getPos() > serverOp->getPos()) { result.second->setPos(result.second->getPos()-1); + updateOp->setNewPos(updateOp->getNewPos()-1); } return result; -//TODO: situation with the same pos } std::pair WhiteboardTransformer::transform(WhiteboardDeleteOperation::ref clientOp, WhiteboardUpdateOperation::ref serverOp) { std::pair result; - result.first = boost::make_shared(*serverOp); + WhiteboardUpdateOperation::ref updateOp = boost::make_shared(*serverOp); + result.first = updateOp; result.first->setParentID(clientOp->getID()); result.second = boost::make_shared(*clientOp); result.second->setParentID(serverOp->getID()); @@ -173,10 +197,14 @@ namespace Swift { result.first->setID(serverOp->getID()); result.first->setParentID(clientOp->getID()); deleteOp->setElementID(clientOp->getElementID()); + } else if (clientOp->getPos() < serverOp->getPos() && clientOp->getPos() >= serverOp->getNewPos()) { + result.first->setPos(result.first->getPos()-1); + } else if (clientOp->getPos() > serverOp->getPos() && clientOp->getPos() <= serverOp->getNewPos()) { + updateOp->setNewPos(updateOp->getNewPos()-1); } else if (clientOp->getPos() < serverOp->getPos()) { result.first->setPos(result.first->getPos()-1); + updateOp->setNewPos(updateOp->getNewPos()-1); } return result; -//TODO: situation with the same pos } } -- cgit v0.10.2-6-g49f6