UBCoreGraphicsScene.cpp 2.38 KB
Newer Older
Claudio Valerio's avatar
Claudio Valerio committed
1
/*
Claudio Valerio's avatar
Claudio Valerio committed
2 3 4 5
 * 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
 * the Free Software Foundation, either version 3 of the License, or
 * (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 "core/memcheck.h"
19 20 21
#include "domain/UBGraphicsAudioItem.h"
#include "domain/UBGraphicsVideoItem.h"
#include "domain/UBGraphicsMediaItem.h"
22
#include "domain/UBGraphicsWidgetItem.h"
23

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

UBCoreGraphicsScene::~UBCoreGraphicsScene()
{
    //we must delete removed items that are no more in any scene
    foreach (const QGraphicsItem* item, mItemsToDelete)
    {
35
        if (item->scene()==NULL || item->scene() == this)
Claudio Valerio's avatar
Claudio Valerio committed
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
        {
            delete item;
        }
    }
}

void UBCoreGraphicsScene::addItem(QGraphicsItem* item)
{
    mItemsToDelete << item;

    if (item->scene() != this)
        QGraphicsScene::addItem(item);
}


void UBCoreGraphicsScene::removeItem(QGraphicsItem* item, bool forceDelete)
{
    QGraphicsScene::removeItem(item);
    if (forceDelete)
    {
        mItemsToDelete.remove(item);
        delete item;
    }
}
60 61 62 63 64

bool UBCoreGraphicsScene::deleteItem(QGraphicsItem* item)
{
    if(mItemsToDelete.contains(item))
    {
65 66 67 68 69 70 71 72 73
        UBGraphicsItem* item_casted = 0;
        switch (item->type())
        {
        case UBGraphicsAudioItem::Type:
                item_casted = dynamic_cast<UBGraphicsAudioItem*>(item);
                break;
        case UBGraphicsVideoItem::Type:
                item_casted = dynamic_cast<UBGraphicsVideoItem*>(item);
                break;
74 75 76
        case UBGraphicsW3CWidgetItem::Type:
                item_casted = dynamic_cast<UBGraphicsWidgetItem*>(item);
                break;
77 78 79 80 81
        }

        if (0 != item_casted)
            item_casted->clearSource();

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