diff --git a/src/adaptors/UBSvgSubsetAdaptor.cpp b/src/adaptors/UBSvgSubsetAdaptor.cpp index bcfa5199..53868e35 100644 --- a/src/adaptors/UBSvgSubsetAdaptor.cpp +++ b/src/adaptors/UBSvgSubsetAdaptor.cpp @@ -3120,7 +3120,7 @@ UBGraphicsTriangle* UBSvgSubsetAdaptor::UBSvgSubsetReader::triangleFromSvg() UBGraphicsCache* UBSvgSubsetAdaptor::UBSvgSubsetReader::cacheFromSvg() { - UBGraphicsCache* pCache = new UBGraphicsCache(); + UBGraphicsCache* pCache = UBGraphicsCache::instance(mScene); pCache->setData(UBGraphicsItemData::ItemLayerType, QVariant(UBItemLayerType::Tool)); graphicsItemFromSvg(pCache); diff --git a/src/domain/UBGraphicsScene.cpp b/src/domain/UBGraphicsScene.cpp index 22ff3379..6fdd79f6 100644 --- a/src/domain/UBGraphicsScene.cpp +++ b/src/domain/UBGraphicsScene.cpp @@ -1603,6 +1603,9 @@ void UBGraphicsScene::removeItem(QGraphicsItem* item) --mItemCount; mFastAccessItems.removeAll(item); + /* delete the item if it is cache to allow its reinstanciation, because Cache implements design pattern Singleton. */ + if (dynamic_cast(item)) + UBCoreGraphicsScene::deleteItem(item); } void UBGraphicsScene::removeItems(const QSet& items) @@ -1956,17 +1959,17 @@ void UBGraphicsScene::addAristo(QPointF center) void UBGraphicsScene::addCache() { - UBGraphicsCache* cache = new UBGraphicsCache(); - mTools << cache; + UBGraphicsCache* cache = UBGraphicsCache::instance(this); + if (!items().contains(cache)) { + addItem(cache); - addItem(cache); + cache->setData(UBGraphicsItemData::ItemLayerType, QVariant(UBItemLayerType::Tool)); - cache->setData(UBGraphicsItemData::ItemLayerType, QVariant(UBItemLayerType::Tool)); - - cache->setVisible(true); - cache->setSelected(true); - UBApplication::boardController->notifyCache(true); - UBApplication::boardController->notifyPageChanged(); + cache->setVisible(true); + cache->setSelected(true); + UBApplication::boardController->notifyCache(true); + UBApplication::boardController->notifyPageChanged(); + } } void UBGraphicsScene::addMask(const QPointF ¢er) diff --git a/src/tools/UBGraphicsCache.cpp b/src/tools/UBGraphicsCache.cpp index 7a89c534..dfde3b4f 100644 --- a/src/tools/UBGraphicsCache.cpp +++ b/src/tools/UBGraphicsCache.cpp @@ -24,11 +24,21 @@ #include "core/memcheck.h" -UBGraphicsCache::UBGraphicsCache():QGraphicsRectItem() +QMap UBGraphicsCache::sInstances; + +UBGraphicsCache* UBGraphicsCache::instance(UBGraphicsScene *scene) +{ + if (!sInstances.contains(scene)) + sInstances.insert(scene, new UBGraphicsCache(scene)); + return sInstances[scene]; +} + +UBGraphicsCache::UBGraphicsCache(UBGraphicsScene *scene) : QGraphicsRectItem() , mMaskColor(Qt::black) , mMaskShape(eMaskShape_Circle) , mShapeWidth(100) , mDrawMask(false) + , mScene(scene) { // Get the board size and pass it to the shape QRect boardRect = UBApplication::boardController->displayView()->rect(); @@ -39,11 +49,12 @@ UBGraphicsCache::UBGraphicsCache():QGraphicsRectItem() UBGraphicsCache::~UBGraphicsCache() { + sInstances.remove(mScene); } UBItem* UBGraphicsCache::deepCopy() const { - UBGraphicsCache* copy = new UBGraphicsCache(); + UBGraphicsCache* copy = new UBGraphicsCache(mScene); copyItemParameters(copy); diff --git a/src/tools/UBGraphicsCache.h b/src/tools/UBGraphicsCache.h index 9ca07d0d..413aa352 100644 --- a/src/tools/UBGraphicsCache.h +++ b/src/tools/UBGraphicsCache.h @@ -30,7 +30,7 @@ typedef enum class UBGraphicsCache : public QGraphicsRectItem, public UBItem { public: - UBGraphicsCache(); + static UBGraphicsCache* instance(UBGraphicsScene *scene); ~UBGraphicsCache(); enum { Type = UBGraphicsItemType::cacheItemType }; @@ -55,8 +55,7 @@ protected: void mouseReleaseEvent(QGraphicsSceneMouseEvent *event); private: - void init(); - QRectF updateRect(QPointF currentPoint); + static QMap sInstances; QColor mMaskColor; eMaskShape mMaskShape; @@ -65,6 +64,13 @@ private: QPointF mShapePos; int mOldShapeWidth; QPointF mOldShapePos; + UBGraphicsScene* mScene; + + + UBGraphicsCache(UBGraphicsScene *scene); + + void init(); + QRectF updateRect(QPointF currentPoint); }; #endif // UBGRAPHICSCACHE_H