Undo-redo stack for eraising operations and Sankore 834

preferencesAboutTextFull
Ilia Ryabokon 12 years ago
parent 8b0e41ea91
commit f6693f4e85
  1. 2
      src/adaptors/UBSvgSubsetAdaptor.cpp
  2. 6
      src/domain/UBGraphicsGroupContainerItem.cpp
  3. 2
      src/domain/UBGraphicsGroupContainerItem.h
  4. 4
      src/domain/UBGraphicsItemGroupUndoCommand.cpp
  5. 66
      src/domain/UBGraphicsItemUndoCommand.cpp
  6. 4
      src/domain/UBGraphicsItemUndoCommand.h
  7. 15
      src/domain/UBGraphicsScene.cpp
  8. 2
      src/domain/UBGraphicsScene.h
  9. 6
      src/domain/UBItem.cpp
  10. 1
      src/domain/UBItem.h

@ -1084,7 +1084,7 @@ QGraphicsItem *UBSvgSubsetAdaptor::UBSvgSubsetReader::readElementFromGroup()
{
QGraphicsItem *result = 0;
result = mScene->itemByUuid(QUuid(mXmlReader.attributes().value(aId).toString()));
result = mScene->itemForUuid(QUuid(mXmlReader.attributes().value(aId).toString()));
mXmlReader.skipCurrentElement();
mXmlReader.readNext();

@ -125,6 +125,8 @@ void UBGraphicsGroupContainerItem::removeFromGroup(QGraphicsItem *item)
pRemoveFromGroup(item);
item->setFlags(ItemIsSelectable | ItemIsFocusable);
}
void UBGraphicsGroupContainerItem::deselectCurrentItem()
@ -219,7 +221,7 @@ void UBGraphicsGroupContainerItem::setUuid(const QUuid &pUuid)
setData(UBGraphicsItemData::ItemUuid, QVariant(pUuid)); //store item uuid inside the QGraphicsItem to fast operations with Items on the scene
}
void UBGraphicsGroupContainerItem::destroy() {
void UBGraphicsGroupContainerItem::destroy(bool canUndo) {
foreach (QGraphicsItem *item, childItems()) {
pRemoveFromGroup(item);
@ -227,7 +229,7 @@ void UBGraphicsGroupContainerItem::destroy() {
item->setFlag(QGraphicsItem::ItemIsFocusable, true);
}
remove();
mDelegate->remove(canUndo);
}
void UBGraphicsGroupContainerItem::clearSource()

@ -37,7 +37,7 @@ public:
}
virtual void setUuid(const QUuid &pUuid);
void destroy();
void destroy(bool canUndo = true);
virtual void clearSource();

@ -22,7 +22,7 @@ UBGraphicsItemGroupUndoCommand::~UBGraphicsItemGroupUndoCommand()
void UBGraphicsItemGroupUndoCommand::undo()
{
mGroup->destroy();
mGroup->destroy(false);
foreach(QGraphicsItem *item, mItems) {
item->setSelected(true);
}
@ -41,7 +41,7 @@ void UBGraphicsItemGroupUndoCommand::redo()
QList<QGraphicsItem*> childItems = item->childItems();
UBGraphicsGroupContainerItem *currentGroup = dynamic_cast<UBGraphicsGroupContainerItem*>(item);
if (currentGroup) {
currentGroup->destroy();
currentGroup->destroy(false);
}
foreach (QGraphicsItem *chItem, childItems) {
mGroup->addToGroup(chItem);

@ -27,10 +27,11 @@
#include "domain/UBGraphicsGroupContainerItem.h"
UBGraphicsItemUndoCommand::UBGraphicsItemUndoCommand(UBGraphicsScene* pScene, const QSet<QGraphicsItem*>& pRemovedItems,
const QSet<QGraphicsItem*>& pAddedItems)
const QSet<QGraphicsItem*>& pAddedItems, const QMultiMap<UBGraphicsGroupContainerItem*, QUuid> &groupsMap)
: mScene(pScene)
, mRemovedItems(pRemovedItems - pAddedItems)
, mAddedItems(pAddedItems - pRemovedItems)
, mRemovedItems(pRemovedItems - pAddedItems)
, mAddedItems(pAddedItems - pRemovedItems)
, mExcludedFromGroup(groupsMap)
{
mFirstRedo = true;
@ -110,6 +111,36 @@ void UBGraphicsItemUndoCommand::undo()
}
}
QMapIterator<UBGraphicsGroupContainerItem*, QUuid> curMapElement(mExcludedFromGroup);
UBGraphicsGroupContainerItem *nextGroup = 0;
UBGraphicsGroupContainerItem *previousGroupItem;
bool groupChanged = false;
while (curMapElement.hasNext()) {
curMapElement.next();
groupChanged = previousGroupItem != curMapElement.key();
//trying to find the group on the scene;
if (!nextGroup || groupChanged) {
UBGraphicsGroupContainerItem *groupCandidate = curMapElement.key();
if (groupCandidate) {
nextGroup = groupCandidate;
if(!mScene->items().contains(nextGroup)) {
mScene->addItem(nextGroup);
}
nextGroup->setVisible(true);
}
}
QGraphicsItem *groupedItem = mScene->itemForUuid(curMapElement.value());
if (groupedItem) {
nextGroup->addToGroup(groupedItem);
}
previousGroupItem = curMapElement.key();
UBGraphicsItem::Delegate(nextGroup)->update();
}
// force refresh, QT is a bit lazy and take a lot of time (nb item ^2 ?) to trigger repaint
mScene->update(mScene->sceneRect());
@ -125,6 +156,35 @@ void UBGraphicsItemUndoCommand::redo()
return;
}
QMapIterator<UBGraphicsGroupContainerItem*, QUuid> curMapElement(mExcludedFromGroup);
UBGraphicsGroupContainerItem *nextGroup = 0;
UBGraphicsGroupContainerItem *previousGroupItem;
bool groupChanged = false;
while (curMapElement.hasNext()) {
curMapElement.next();
groupChanged = previousGroupItem != curMapElement.key();
//trying to find the group on the scene;
if (!nextGroup || groupChanged) {
UBGraphicsGroupContainerItem *groupCandidate = curMapElement.key();
if (groupCandidate) {
nextGroup = groupCandidate;
}
}
QGraphicsItem *groupedItem = mScene->itemForUuid(curMapElement.value());
if (groupedItem) {
if (nextGroup->childItems().count() == 1) {
nextGroup->destroy(false);
break;
}
nextGroup->removeFromGroup(groupedItem);
}
previousGroupItem = curMapElement.key();
UBGraphicsItem::Delegate(nextGroup)->update();
}
QSetIterator<QGraphicsItem*> itRemoved(mRemovedItems);
while (itRemoved.hasNext())
{

@ -18,6 +18,7 @@
#include <QtGui>
#include "UBAbstractUndoCommand.h"
#include "UBGraphicsGroupContainerItem.h"
class UBGraphicsScene;
@ -27,7 +28,7 @@ class UBGraphicsItemUndoCommand : public UBAbstractUndoCommand
{
public:
UBGraphicsItemUndoCommand(UBGraphicsScene* pScene, const QSet<QGraphicsItem*>& pRemovedItems,
const QSet<QGraphicsItem*>& pAddedItems);
const QSet<QGraphicsItem*>& pAddedItems, const QMultiMap<UBGraphicsGroupContainerItem*, QUuid> &groupsMap = QMultiMap<UBGraphicsGroupContainerItem*, QUuid>());
UBGraphicsItemUndoCommand(UBGraphicsScene* pScene, QGraphicsItem* pRemovedItem,
QGraphicsItem* pAddedItem);
@ -47,6 +48,7 @@ class UBGraphicsItemUndoCommand : public UBAbstractUndoCommand
UBGraphicsScene* mScene;
QSet<QGraphicsItem*> mRemovedItems;
QSet<QGraphicsItem*> mAddedItems;
QMultiMap<UBGraphicsGroupContainerItem*, QUuid> mExcludedFromGroup;
bool mFirstRedo;
};

@ -1067,6 +1067,7 @@ UBItem* UBGraphicsScene::deepCopy() const
void UBGraphicsScene::clearContent(clearCase pCase)
{
QSet<QGraphicsItem*> removedItems;
QMultiMap<UBGraphicsGroupContainerItem*, QUuid> groupsMap;
switch (pCase) {
case clearBackground :
@ -1106,8 +1107,13 @@ void UBGraphicsScene::clearContent(clearCase pCase)
if(shouldDelete) {
if (itemGroup) {
itemGroup->removeFromGroup(item);
groupsMap.insert(itemGroup, UBGraphicsItem::getOwnUuid(item));
if (itemGroup->childItems().count() == 1) {
itemGroup->destroy();
groupsMap.insert(itemGroup, UBGraphicsItem::getOwnUuid(itemGroup->childItems().first()));
QGraphicsItem *lastItem = itemGroup->childItems().first();
bool isSelected = itemGroup->isSelected();
itemGroup->destroy(false);
lastItem->setSelected(isSelected);
}
itemGroup->Delegate()->update();
}
@ -1123,7 +1129,7 @@ void UBGraphicsScene::clearContent(clearCase pCase)
update(sceneRect());
if (enableUndoRedoStack) { //should be deleted after scene own undo stack implemented
UBGraphicsItemUndoCommand* uc = new UBGraphicsItemUndoCommand(this, removedItems, QSet<QGraphicsItem*>());
UBGraphicsItemUndoCommand* uc = new UBGraphicsItemUndoCommand(this, removedItems, QSet<QGraphicsItem*>(), groupsMap);
UBApplication::undoStack->push(uc);
}
@ -1571,8 +1577,9 @@ void UBGraphicsScene::removeItem(QGraphicsItem* item)
void UBGraphicsScene::removeItems(const QSet<QGraphicsItem*>& items)
{
foreach(QGraphicsItem* item, items)
foreach(QGraphicsItem* item, items) {
UBCoreGraphicsScene::removeItem(item);
}
mItemCount -= items.size();
@ -1665,7 +1672,7 @@ QRectF UBGraphicsScene::normalizedSceneRect(qreal ratio)
return normalizedRect;
}
QGraphicsItem *UBGraphicsScene::itemByUuid(QUuid uuid)
QGraphicsItem *UBGraphicsScene::itemForUuid(QUuid uuid)
{
QGraphicsItem *result = 0;

@ -174,7 +174,7 @@ class UBGraphicsScene: public UBCoreGraphicsScene, public UBItem
QRectF normalizedSceneRect(qreal ratio = -1.0);
QGraphicsItem *itemByUuid(QUuid uuid);
QGraphicsItem *itemForUuid(QUuid uuid);
void moveTo(const QPointF& pPoint);
void drawLineTo(const QPointF& pEndPoint, const qreal& pWidth, bool bLineStyle);

@ -54,6 +54,12 @@ bool UBGraphicsItem::isRotatable(QGraphicsItem *item)
return item->data(UBGraphicsItemData::ItemRotatable).toBool();
}
QUuid UBGraphicsItem::getOwnUuid(QGraphicsItem *item)
{
QString idCandidate = item->data(UBGraphicsItemData::ItemUuid).toString();
return idCandidate == QUuid().toString() ? QUuid() : QUuid(idCandidate);
}
UBGraphicsItemDelegate *UBGraphicsItem::Delegate(QGraphicsItem *pItem)
{
UBGraphicsItemDelegate *result = 0;

@ -107,6 +107,7 @@ public:
static void assignZValue(QGraphicsItem*, qreal value);
static bool isRotatable(QGraphicsItem *item);
static bool isFlippable(QGraphicsItem *item);
static QUuid getOwnUuid(QGraphicsItem *item);
static UBGraphicsItemDelegate *Delegate(QGraphicsItem *pItem);
virtual UBGraphicsItemDelegate *Delegate() const = 0;

Loading…
Cancel
Save