UBCoreGraphicsScene.cpp 3.05 KB
Newer Older
Claudio Valerio's avatar
Claudio Valerio committed
1
/*
2
 * Copyright (C) 2015-2018 Département de l'Instruction Publique (DIP-SEM)
Craig Watson's avatar
Craig Watson committed
3
 *
Claudio Valerio's avatar
Claudio Valerio committed
4
 * Copyright (C) 2013 Open Education Foundation
Claudio Valerio's avatar
Claudio Valerio committed
5
 *
Claudio Valerio's avatar
Claudio Valerio committed
6 7
 * Copyright (C) 2010-2013 Groupement d'Intérêt Public pour
 * l'Education Numérique en Afrique (GIP ENA)
8
 *
Claudio Valerio's avatar
Claudio Valerio committed
9 10 11
 * This file is part of OpenBoard.
 *
 * OpenBoard is free software: you can redistribute it and/or modify
Claudio Valerio's avatar
Claudio Valerio committed
12 13
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License,
14 15 16 17
 * with a specific linking exception for the OpenSSL project's
 * "OpenSSL" library (or with modified versions of it that use the
 * same license as the "OpenSSL" library).
 *
Claudio Valerio's avatar
Claudio Valerio committed
18
 * OpenBoard is distributed in the hope that it will be useful,
Claudio Valerio's avatar
Claudio Valerio committed
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Claudio Valerio's avatar
Claudio Valerio committed
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Claudio Valerio's avatar
Claudio Valerio committed
21
 * GNU General Public License for more details.
Claudio Valerio's avatar
Claudio Valerio committed
22
 *
Claudio Valerio's avatar
Claudio Valerio committed
23
 * You should have received a copy of the GNU General Public License
Claudio Valerio's avatar
Claudio Valerio committed
24
 * along with OpenBoard. If not, see <http://www.gnu.org/licenses/>.
Claudio Valerio's avatar
Claudio Valerio committed
25 26
 */

27

Claudio Valerio's avatar
Claudio Valerio committed
28

Claudio Valerio's avatar
Claudio Valerio committed
29

Claudio Valerio's avatar
Claudio Valerio committed
30 31
#include "UBCoreGraphicsScene.h"

32
#include "domain/UBGraphicsMediaItem.h"
33
#include "domain/UBGraphicsWidgetItem.h"
Ilia Ryabokon's avatar
Ilia Ryabokon committed
34
#include "domain/UBGraphicsGroupContainerItem.h"
35

36 37
#include "core/memcheck.h"

Claudio Valerio's avatar
Claudio Valerio committed
38 39
UBCoreGraphicsScene::UBCoreGraphicsScene(QObject * parent)
    : QGraphicsScene ( parent  )
40
    , mIsModified(true)
Claudio Valerio's avatar
Claudio Valerio committed
41 42 43 44 45 46 47
{
    //NOOP
}

UBCoreGraphicsScene::~UBCoreGraphicsScene()
{
    //we must delete removed items that are no more in any scene
48
    //at groups deleting some items can be added to mItemsToDelete, so we need to use iterators.
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
49
    foreach(QGraphicsItem* item, mItemsToDelete)
Claudio Valerio's avatar
Claudio Valerio committed
50
    {
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
51
        if (item)
Claudio Valerio's avatar
Claudio Valerio committed
52
        {
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
53
            if (item->scene() == NULL || item->scene() == this)
54 55
            {
                delete item;
56
                item = NULL;
57
            }
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
58
        }
Claudio Valerio's avatar
Claudio Valerio committed
59
    }
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
60
    mItemsToDelete.clear();
Claudio Valerio's avatar
Claudio Valerio committed
61 62 63 64
}

void UBCoreGraphicsScene::addItem(QGraphicsItem* item)
{
65 66
    addItemToDeletion(item);

Ilia Ryabokon's avatar
Ilia Ryabokon committed
67 68 69 70 71
    if (item->type() == UBGraphicsGroupContainerItem::Type && item->childItems().count()) {
        foreach (QGraphicsItem *curItem, item->childItems()) {
            removeItemFromDeletion(curItem);
        }
    }
72

Claudio Valerio's avatar
Claudio Valerio committed
73 74
    if (item->scene() != this)
        QGraphicsScene::addItem(item);
75 76

    setModified(true);
Claudio Valerio's avatar
Claudio Valerio committed
77 78 79 80 81 82 83 84
}


void UBCoreGraphicsScene::removeItem(QGraphicsItem* item, bool forceDelete)
{
    QGraphicsScene::removeItem(item);
    if (forceDelete)
    {
85
        deleteItem(item);
Claudio Valerio's avatar
Claudio Valerio committed
86
    }
87
    setModified(true);
Claudio Valerio's avatar
Claudio Valerio committed
88
}
89 90 91 92 93

bool UBCoreGraphicsScene::deleteItem(QGraphicsItem* item)
{
    if(mItemsToDelete.contains(item))
    {
94
        UBGraphicsItem *item_casted = dynamic_cast<UBGraphicsItem *>(item);
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
95
        if (item_casted != NULL)
96 97
            item_casted->clearSource();

98 99
        mItemsToDelete.remove(item);
        delete item;
100
        item = NULL;
101 102 103 104 105 106
        return true;
    }
    else
        return false;
}

107 108 109 110 111 112
void UBCoreGraphicsScene::removeItemFromDeletion(QGraphicsItem *item)
{
    if(NULL != item){
        mItemsToDelete.remove(item);
    }
}
Ilia Ryabokon's avatar
Ilia Ryabokon committed
113 114 115 116 117 118 119

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