summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMateusz Piekos <mateuszpiekos@gmail.com>2012-07-31 08:31:54 (GMT)
committerMateusz Piekos <mateuszpiekos@gmail.com>2012-07-31 08:31:54 (GMT)
commit4321da432e3332e868abf07678f15d305fec2a4b (patch)
tree72cc36c121448dce907a4ccbc679bc3cad3c07f4
parent5fafa602d859657d6616acdbb63dfcd501aed9a0 (diff)
downloadswift-contrib-4321da432e3332e868abf07678f15d305fec2a4b.zip
swift-contrib-4321da432e3332e868abf07678f15d305fec2a4b.tar.bz2
Upgraded update operation with ability to move elements
-rw-r--r--Swift/QtUI/Whiteboard/GView.cpp43
-rw-r--r--Swift/QtUI/Whiteboard/GView.h3
-rw-r--r--Swift/QtUI/Whiteboard/QtWhiteboardWindow.cpp14
-rw-r--r--Swiften/Parser/PayloadParsers/WhiteboardParser.cpp2
-rw-r--r--Swiften/Serializer/PayloadSerializers/WhiteboardSerializer.cpp1
-rw-r--r--Swiften/Whiteboard/Operations/WhiteboardUpdateOperation.h9
-rw-r--r--Swiften/Whiteboard/WhiteboardTransformer.cpp36
7 files changed, 96 insertions, 12 deletions
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<QGraphicsItem*>();
- item->setZValue(item->zValue()+1);
+ if (selectionRect) {
+ QGraphicsItem* item = selectionRect->data(1).value<QGraphicsItem*>();
+ 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<QGraphicsItem*>();
- item->setZValue(item->zValue()-1);
+ if (selectionRect) {
+ QGraphicsItem* item = selectionRect->data(1).value<QGraphicsItem*>();
+ 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<WhiteboardDeleteOperation>(operation);
@@ -343,7 +346,6 @@ namespace Swift {
if (type == GView::New) {
WhiteboardInsertOperation::ref insertOp = boost::make_shared<WhiteboardInsertOperation>();
-// 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<WhiteboardUpdateOperation>();
-// 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<WhiteboardUpdateOperation>();
+ std::string move = attributes.getAttributeValue("newpos").get_value_or("0");
+ updateOp->setNewPos(boost::lexical_cast<int>(attributes.getAttributeValue("newpos").get_value_or("0")));
operation = updateOp;
} else if (type == "delete") {
WhiteboardDeleteOperation::ref deleteOp = boost::make_shared<WhiteboardDeleteOperation>();
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<std::string>(updateOp->getPos()));
operationNode->setAttribute("id", updateOp->getID());
operationNode->setAttribute("parentid", updateOp->getParentID());
+ operationNode->setAttribute("newpos", boost::lexical_cast<std::string>(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<WhiteboardUpdateOperation>(*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<WhiteboardDeleteOperation::ref, WhiteboardOperation::ref> result;
result.first = boost::make_shared<WhiteboardDeleteOperation>(*serverOp);
result.first->setParentID(clientOp->getID());
- result.second = boost::make_shared<WhiteboardUpdateOperation>(*clientOp);
+ WhiteboardUpdateOperation::ref updateOp = boost::make_shared<WhiteboardUpdateOperation>(*clientOp);
+ result.second = updateOp;
result.second->setParentID(serverOp->getID());
if (clientOp->getPos() == serverOp->getPos()) {
WhiteboardDeleteOperation::ref deleteOp = boost::make_shared<WhiteboardDeleteOperation>();
@@ -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<WhiteboardOperation::ref, WhiteboardOperation::ref> WhiteboardTransformer::transform(WhiteboardDeleteOperation::ref clientOp, WhiteboardUpdateOperation::ref serverOp) {
std::pair<WhiteboardOperation::ref, WhiteboardDeleteOperation::ref> result;
- result.first = boost::make_shared<WhiteboardUpdateOperation>(*serverOp);
+ WhiteboardUpdateOperation::ref updateOp = boost::make_shared<WhiteboardUpdateOperation>(*serverOp);
+ result.first = updateOp;
result.first->setParentID(clientOp->getID());
result.second = boost::make_shared<WhiteboardDeleteOperation>(*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
}
}