UBCoreGraphicsScene.cpp 2.67 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
    //at groups deleting some items can be added to mItemsToDelete, so we need to use iterators.
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
35
    foreach(QGraphicsItem* item, mItemsToDelete)
Claudio Valerio's avatar
Claudio Valerio committed
36
    {
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
37
        if (item)
Claudio Valerio's avatar
Claudio Valerio committed
38
        {
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
39
            if (item->scene() == NULL || item->scene() == this)
40 41 42
            {
                delete item;
            }
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
43
        }
Claudio Valerio's avatar
Claudio Valerio committed
44
    }
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
45
    mItemsToDelete.clear();
Claudio Valerio's avatar
Claudio Valerio committed
46 47 48 49
}

void UBCoreGraphicsScene::addItem(QGraphicsItem* item)
{
50 51
    addItemToDeletion(item);

Ilia Ryabokon's avatar
Ilia Ryabokon committed
52 53 54 55 56
    if (item->type() == UBGraphicsGroupContainerItem::Type && item->childItems().count()) {
        foreach (QGraphicsItem *curItem, item->childItems()) {
            removeItemFromDeletion(curItem);
        }
    }
57
 
Claudio Valerio's avatar
Claudio Valerio committed
58 59
    if (item->scene() != this)
        QGraphicsScene::addItem(item);
60 61

    setModified(true);
Claudio Valerio's avatar
Claudio Valerio committed
62 63 64 65 66 67 68 69
}


void UBCoreGraphicsScene::removeItem(QGraphicsItem* item, bool forceDelete)
{
    QGraphicsScene::removeItem(item);
    if (forceDelete)
    {
70
        qDebug() << "force delete is " << forceDelete;
71
        deleteItem(item);
Claudio Valerio's avatar
Claudio Valerio committed
72
    }
73
    setModified(true);
Claudio Valerio's avatar
Claudio Valerio committed
74
}
75 76 77 78 79

bool UBCoreGraphicsScene::deleteItem(QGraphicsItem* item)
{
    if(mItemsToDelete.contains(item))
    {
80
        UBGraphicsItem *item_casted = dynamic_cast<UBGraphicsItem *>(item);
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
81
        if (item_casted != NULL)
82 83
            item_casted->clearSource();

84 85 86 87 88 89 90 91
        mItemsToDelete.remove(item);
        delete item;
        return true;
    }
    else
        return false;
}

92 93 94 95 96 97
void UBCoreGraphicsScene::removeItemFromDeletion(QGraphicsItem *item)
{
    if(NULL != item){
        mItemsToDelete.remove(item);
    }
}
Ilia Ryabokon's avatar
Ilia Ryabokon committed
98 99 100 101 102 103 104

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