1
0
Fork 0

Orderly destruction of canvas elements

Driven from the object destroyed signal, ensure children and 
corresponding QQ items are also destroyed when the property is removed.
This commit is contained in:
James Turner 2017-11-28 22:54:12 +00:00
parent e4b5a699d8
commit 8e6500ae47
10 changed files with 62 additions and 0 deletions

View file

@ -89,6 +89,7 @@ FGCanvasElement::FGCanvasElement(FGCanvasGroup* pr, LocalProp* prop) :
this, &FGCanvasElement::onVisibleChanged);
connect(prop, &LocalProp::childAdded, this, &FGCanvasElement::onChildAdded);
connect(prop, &LocalProp::childRemoved, this, &FGCanvasElement::onChildRemoved);
connect(prop, &LocalProp::destroyed, this, &FGCanvasElement::onPropDestroyed);
if (pr) {
pr->markChildZIndicesDirty();
@ -97,6 +98,12 @@ FGCanvasElement::FGCanvasElement(FGCanvasGroup* pr, LocalProp* prop) :
requestPolish();
}
void FGCanvasElement::onPropDestroyed()
{
doDestroy();
const_cast<FGCanvasGroup*>(_parent)->removeChild(this);
deleteLater();
}
void FGCanvasElement::requestPolish()
{
@ -304,6 +311,10 @@ bool FGCanvasElement::onChildRemoved(LocalProp *prop)
return false;
}
void FGCanvasElement::doDestroy()
{
}
QColor FGCanvasElement::fillColor() const
{
return _fillColor;

View file

@ -73,6 +73,8 @@ protected:
virtual bool onChildAdded(LocalProp* prop);
virtual bool onChildRemoved(LocalProp* prop);
virtual void doDestroy();
const LocalProp* _propertyRoot;
const FGCanvasGroup* _parent;
@ -83,6 +85,10 @@ protected:
virtual void markStyleDirty();
QVariant getCascadedStyle(const char* name, QVariant defaultValue = QVariant()) const;
private slots:
void onPropDestroyed();
private:
void onCenterChanged(QVariant value);

View file

@ -190,6 +190,16 @@ bool FGCanvasGroup::onChildRemoved(LocalProp *prop)
return false;
}
void FGCanvasGroup::removeChild(FGCanvasElement *child)
{
auto it = std::find(_children.begin(), _children.end(), child);
if (it != _children.end()) {
int index = std::distance(_children.begin(), it);
_children.erase(it);
emit childRemoved(index);
}
}
int FGCanvasGroup::indexOfChildWithProp(LocalProp* prop) const
{
auto it = std::find_if(_children.begin(), _children.end(), [prop](FGCanvasElement* child)
@ -211,6 +221,17 @@ void FGCanvasGroup::markStyleDirty()
}
}
void FGCanvasGroup::doDestroy()
{
delete _quick;
FGCanvasElementVec children = std::move(_children);
_children.clear();
for (auto c : children) {
delete c;
}
}
void FGCanvasGroup::markCachedSymbolDirty()
{
_cachedSymbolDirty = true;

View file

@ -25,6 +25,7 @@ public:
CanvasItem* quickItem() const override
{ return _quick; }
void removeChild(FGCanvasElement* child);
signals:
void childAdded();
void childRemoved(int index);
@ -39,6 +40,8 @@ protected:
bool onChildRemoved(LocalProp *prop) override;
virtual void markStyleDirty() override;
void doDestroy() override;
private:
void markCachedSymbolDirty();
int indexOfChildWithProp(LocalProp *prop) const;

View file

@ -466,6 +466,11 @@ CanvasItem *FGCanvasPath::quickItem() const
return _quickPath;
}
void FGCanvasPath::doDestroy()
{
delete _quickPath;
}
void FGCanvasPath::markPathDirty()
{
_pathDirty = true;

View file

@ -40,6 +40,8 @@ protected:
CanvasItem* createQuickItem(QQuickItem *parent) override;
CanvasItem* quickItem() const override;
void doDestroy() override;
private:
void markPathDirty();
void markStrokeDirty();

View file

@ -252,6 +252,12 @@ void FGCanvasText::markStyleDirty()
markFontDirty();
}
void FGCanvasText::doDestroy()
{
qDebug() << Q_FUNC_INFO;
delete _quickItem;
}
bool FGCanvasText::onChildAdded(LocalProp *prop)
{
if (FGCanvasElement::onChildAdded(prop)) {

View file

@ -38,6 +38,9 @@ protected:
void doPolish() override;
virtual void markStyleDirty() override;
void doDestroy() override;
private:
bool onChildAdded(LocalProp *prop) override;

View file

@ -242,6 +242,10 @@ void FGQCanvasImage::markStyleDirty()
{
}
void FGQCanvasImage::doDestroy()
{
delete _quickItem;
}
CanvasItem *FGQCanvasImage::createQuickItem(QQuickItem *parent)
{

View file

@ -38,6 +38,7 @@ protected:
virtual void markStyleDirty() override;
void doDestroy() override;
void doPolish() override;
private slots: