UBGraphicsPixmapItem.cpp 5.75 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 32 33 34 35 36 37 38 39
#include "UBGraphicsPixmapItem.h"

#include <QtGui>
#include <QMimeData>
#include <QDrag>

#include "UBGraphicsScene.h"

#include "UBGraphicsItemDelegate.h"

40 41 42 43 44 45 46
#include "frameworks/UBFileSystemUtils.h"

#include "core/UBApplication.h"
#include "core/UBPersistenceManager.h"

#include "board/UBBoardController.h"

47 48
#include "core/memcheck.h"

49
UBGraphicsPixmapItem::UBGraphicsPixmapItem(QGraphicsItem* parent)
Claudio Valerio's avatar
Claudio Valerio committed
50 51
    : QGraphicsPixmapItem(parent)
{
52 53 54 55
    setDelegate(new UBGraphicsItemDelegate(this, 0, GF_COMMON
                                           | GF_FLIPPABLE_ALL_AXIS
                                           | GF_REVOLVABLE
                                           | GF_RESPECT_RATIO
56
                                           | GF_TOOLBAR_USED));
57

Claudio Valerio's avatar
Claudio Valerio committed
58 59 60
    setData(UBGraphicsItemData::ItemLayerType, UBItemLayerType::Object);
    setTransformationMode(Qt::SmoothTransformation);

shibakaneki's avatar
shibakaneki committed
61
    setData(UBGraphicsItemData::itemLayerType, QVariant(itemLayerType::ObjectItem)); //Necessary to set if we want z value to be assigned correctly
Claudio Valerio's avatar
Claudio Valerio committed
62
    setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
63

64 65
    setData(UBGraphicsItemData::ItemCanBeSetAsBackground, true);

66
    setUuid(QUuid::createUuid()); //more logical solution is in creating uuid for element in element's constructor
Claudio Valerio's avatar
Claudio Valerio committed
67 68 69 70 71 72 73 74
}

UBGraphicsPixmapItem::~UBGraphicsPixmapItem()
{
}

QVariant UBGraphicsPixmapItem::itemChange(GraphicsItemChange change, const QVariant &value)
{
75
    QVariant newValue = Delegate()->itemChange(change, value);
Claudio Valerio's avatar
Claudio Valerio committed
76 77 78
    return QGraphicsPixmapItem::itemChange(change, newValue);
}

root's avatar
root committed
79 80 81 82 83 84
void UBGraphicsPixmapItem::setUuid(const QUuid &pUuid)
{
    UBItem::setUuid(pUuid);
    setData(UBGraphicsItemData::ItemUuid, QVariant(pUuid));
}

Claudio Valerio's avatar
Claudio Valerio committed
85 86 87 88
void UBGraphicsPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    QMimeData* pMime = new QMimeData();
    pMime->setImageData(pixmap().toImage());
89
    Delegate()->setMimeData(pMime);
90 91 92
    qreal k = (qreal)pixmap().width() / 100.0;

    QSize newSize((int)(pixmap().width() / k), (int)(pixmap().height() / k));
93

94
    Delegate()->setDragPixmap(pixmap().scaled(newSize, Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
95

96
    if (Delegate()->mousePressEvent(event))
Claudio Valerio's avatar
Claudio Valerio committed
97 98 99 100 101
    {
        //NOOP
    }
    else
    {
102
//        QGraphicsPixmapItem::mousePressEvent(event);
Claudio Valerio's avatar
Claudio Valerio committed
103 104 105 106 107
    }
}

void UBGraphicsPixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
108
    if (Delegate()->mouseMoveEvent(event))
Claudio Valerio's avatar
Claudio Valerio committed
109 110 111 112 113 114 115 116 117 118 119
    {
        // NOOP;
    }
    else
    {
        QGraphicsPixmapItem::mouseMoveEvent(event);
    }
}

void UBGraphicsPixmapItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
120
    Delegate()->mouseReleaseEvent(event);
Claudio Valerio's avatar
Claudio Valerio committed
121 122 123 124 125 126
    QGraphicsPixmapItem::mouseReleaseEvent(event);
}


void UBGraphicsPixmapItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
127 128
    painter->setRenderHint(QPainter::Antialiasing, false);

Claudio Valerio's avatar
Claudio Valerio committed
129 130 131
    // Never draw the rubber band, we draw our custom selection with the DelegateFrame
    QStyleOptionGraphicsItem styleOption = QStyleOptionGraphicsItem(*option);

132
    styleOption.state &= ~QStyle::State_Selected;
Claudio Valerio's avatar
Claudio Valerio committed
133
    QGraphicsPixmapItem::paint(painter, &styleOption, widget);
134
    Delegate()->postpaint(painter, option, widget);
135 136

    painter->setRenderHint(QPainter::Antialiasing, true);
Claudio Valerio's avatar
Claudio Valerio committed
137 138 139 140 141 142 143
}


UBItem* UBGraphicsPixmapItem::deepCopy() const
{
   UBGraphicsPixmapItem* copy = new UBGraphicsPixmapItem();

144
   copy->setUuid(this->uuid()); // this is OK for now as long as Widgets are imutable
Claudio Valerio's avatar
Claudio Valerio committed
145

146
   copyItemParameters(copy);
Claudio Valerio's avatar
Claudio Valerio committed
147 148 149 150 151 152

   // TODO UB 4.7 ... complete all members ?

   return copy;
}

153 154 155 156 157 158 159 160 161 162 163 164 165
void UBGraphicsPixmapItem::copyItemParameters(UBItem *copy) const
{
    UBGraphicsPixmapItem *cp = dynamic_cast<UBGraphicsPixmapItem*>(copy);
    if (cp)
    {
        cp->setPixmap(this->pixmap());
        cp->setPos(this->pos());
        cp->setTransform(this->transform());
        cp->setFlag(QGraphicsItem::ItemIsMovable, true);
        cp->setFlag(QGraphicsItem::ItemIsSelectable, true);
        cp->setData(UBGraphicsItemData::ItemLayerType, this->data(UBGraphicsItemData::ItemLayerType));
        cp->setData(UBGraphicsItemData::ItemLocked, this->data(UBGraphicsItemData::ItemLocked));
        cp->setSourceUrl(this->sourceUrl());
166 167

        cp->setZValue(this->zValue());
168 169
    }
}
Claudio Valerio's avatar
Claudio Valerio committed
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186

UBGraphicsScene* UBGraphicsPixmapItem::scene()
{
    return qobject_cast<UBGraphicsScene*>(QGraphicsItem::scene());
}


void UBGraphicsPixmapItem::setOpacity(qreal op)
{
    QGraphicsPixmapItem::setOpacity(op);
}


qreal UBGraphicsPixmapItem::opacity() const
{
    return QGraphicsPixmapItem::opacity();
}
187 188 189 190 191 192 193 194


void UBGraphicsPixmapItem::clearSource()
{
    QString fileName = UBPersistenceManager::imageDirectory + "/" + uuid().toString() + ".png";
    QString diskPath =  UBApplication::boardController->selectedDocument()->persistencePath() + "/" + fileName;
    UBFileSystemUtils::deleteFile(diskPath);
}