UBCoreGraphicsScene.cpp 2.89 KB
Newer Older
Claudio Valerio's avatar
Claudio Valerio committed
1
/*
Claudio Valerio's avatar
Claudio Valerio committed
2 3
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
Claudio Valerio's avatar
Claudio Valerio committed
4
 * the Free Software Foundation, either version 2 of the License, or
Claudio Valerio's avatar
Claudio Valerio committed
5
 * (at your option) any later version.
Claudio Valerio's avatar
Claudio Valerio committed
6
 *
Claudio Valerio's avatar
Claudio Valerio committed
7 8 9 10 11 12 13
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
Claudio Valerio's avatar
Claudio Valerio committed
14 15 16 17
 */

#include "UBCoreGraphicsScene.h"

18
#include "domain/UBGraphicsMediaItem.h"
19
#include "domain/UBGraphicsWidgetItem.h"
Ilia Ryabokon's avatar
Ilia Ryabokon committed
20
#include "domain/UBGraphicsGroupContainerItem.h"
21

22 23
#include "core/memcheck.h"

Claudio Valerio's avatar
Claudio Valerio committed
24 25
UBCoreGraphicsScene::UBCoreGraphicsScene(QObject * parent)
    : QGraphicsScene ( parent  )
26
    , mIsModified(true)
Claudio Valerio's avatar
Claudio Valerio committed
27 28 29 30 31 32 33
{
    //NOOP
}

UBCoreGraphicsScene::~UBCoreGraphicsScene()
{
    //we must delete removed items that are no more in any scene
34 35
    //at groups deleting some items can be added to mItemsToDelete, so we need to use iterators.
    if (mItemsToDelete.count())
Claudio Valerio's avatar
Claudio Valerio committed
36
    {
37 38 39
        QSet<QGraphicsItem *>::iterator it = mItemsToDelete.begin();
        QGraphicsItem* item = *it;
        do 
Claudio Valerio's avatar
Claudio Valerio committed
40
        {
41 42 43 44 45 46 47 48 49 50
            item = *it;
            if (item && (item->scene() == NULL || item->scene() == this))
            {
                mItemsToDelete.remove(*it);
                delete item;
            }

            it = mItemsToDelete.begin();

        }while(mItemsToDelete.count());
Claudio Valerio's avatar
Claudio Valerio committed
51 52 53 54 55
    }
}

void UBCoreGraphicsScene::addItem(QGraphicsItem* item)
{
56 57
    addItemToDeletion(item);

Ilia Ryabokon's avatar
Ilia Ryabokon committed
58 59 60 61 62
    if (item->type() == UBGraphicsGroupContainerItem::Type && item->childItems().count()) {
        foreach (QGraphicsItem *curItem, item->childItems()) {
            removeItemFromDeletion(curItem);
        }
    }
63
 
Claudio Valerio's avatar
Claudio Valerio committed
64 65
    if (item->scene() != this)
        QGraphicsScene::addItem(item);
66 67

    setModified(true);
Claudio Valerio's avatar
Claudio Valerio committed
68 69 70 71 72 73 74 75
}


void UBCoreGraphicsScene::removeItem(QGraphicsItem* item, bool forceDelete)
{
    QGraphicsScene::removeItem(item);
    if (forceDelete)
    {
76
        qDebug() << "force delete is " << forceDelete;
77
        deleteItem(item);
Claudio Valerio's avatar
Claudio Valerio committed
78
    }
79
    setModified(true);
Claudio Valerio's avatar
Claudio Valerio committed
80
}
81 82 83 84 85

bool UBCoreGraphicsScene::deleteItem(QGraphicsItem* item)
{
    if(mItemsToDelete.contains(item))
    {
86
        UBGraphicsItem *item_casted = dynamic_cast<UBGraphicsItem *>(item);
87 88 89
        if (0 != item_casted)
            item_casted->clearSource();

90 91
        mItemsToDelete.remove(item);
        delete item;
92
        item = 0;
93 94 95 96 97 98
        return true;
    }
    else
        return false;
}

99 100 101 102 103 104
void UBCoreGraphicsScene::removeItemFromDeletion(QGraphicsItem *item)
{
    if(NULL != item){
        mItemsToDelete.remove(item);
    }
}
Ilia Ryabokon's avatar
Ilia Ryabokon committed
105 106 107 108 109 110 111

void UBCoreGraphicsScene::addItemToDeletion(QGraphicsItem *item)
{
    if (item) {
        mItemsToDelete.insert(item);
    }
}