UBGraphicsProxyWidget.cpp 5.45 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
#include "UBGraphicsProxyWidget.h"

#include <QtGui>

#include "UBGraphicsScene.h"
#include "UBGraphicsItemDelegate.h"

#include "UBGraphicsDelegateFrame.h"

39 40 41 42 43 44 45
#include "core/UBApplication.h"
#include "core/UBPersistenceManager.h"

#include "board/UBBoardController.h"

#include "frameworks/UBFileSystemUtils.h"

46
#include "core/memcheck.h"
Claudio Valerio's avatar
Claudio Valerio committed
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62

UBGraphicsProxyWidget::UBGraphicsProxyWidget(QGraphicsItem* parent)
    : QGraphicsProxyWidget(parent, Qt::FramelessWindowHint)
{
    setData(UBGraphicsItemData::ItemLayerType, UBItemLayerType::Object);

    setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);

    QGraphicsProxyWidget::setAcceptHoverEvents(true);
}


UBGraphicsProxyWidget::~UBGraphicsProxyWidget()
{
}

63 64
void UBGraphicsProxyWidget::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
65 66
    painter->save();
    painter->setCompositionMode(QPainter::CompositionMode_SourceOver);
67
    QGraphicsProxyWidget::paint(painter,option,widget);
68
    Delegate()->postpaint(painter, option, widget);
69
    painter->restore();
70
}
Claudio Valerio's avatar
Claudio Valerio committed
71 72 73

QVariant UBGraphicsProxyWidget::itemChange(GraphicsItemChange change, const QVariant &value)
{
74 75 76 77
    if (change == QGraphicsItem::ItemCursorHasChanged &&  scene())
    {
        unsetCursor();
    }
Claudio Valerio's avatar
Claudio Valerio committed
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
    if ((change == QGraphicsItem::ItemSelectedHasChanged)
              &&  scene())
    {
        if (isSelected())
        {
            scene()->setActiveWindow(this);
        }
        else
        {
            if(scene()->activeWindow() == this)
            {
                scene()->setActiveWindow(0);
            }
        }
    }

94 95 96 97 98 99
    if (Delegate()) {
        QVariant newValue = Delegate()->itemChange(change, value);
        return QGraphicsProxyWidget::itemChange(change, newValue);
    }
    else
        return QGraphicsProxyWidget::itemChange(change, value);
Claudio Valerio's avatar
Claudio Valerio committed
100 101
}

root's avatar
root committed
102 103 104 105 106
void UBGraphicsProxyWidget::setUuid(const QUuid &pUuid)
{
    UBItem::setUuid(pUuid);
    setData(UBGraphicsItemData::ItemUuid, QVariant(pUuid)); //store item uuid inside the QGraphicsItem to fast operations with Items on the scene
}
Claudio Valerio's avatar
Claudio Valerio committed
107 108 109

void UBGraphicsProxyWidget::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
110
    if (Delegate()->mousePressEvent(event))
Claudio Valerio's avatar
Claudio Valerio committed
111 112 113 114 115 116
    {
        //NOOP
    }
    else
    {
        // QT Proxy Widget is a bit lazy, we force the selection ...
117

Claudio Valerio's avatar
Claudio Valerio committed
118 119
        setSelected(true);
    }
120
    QGraphicsProxyWidget::mousePressEvent(event);
Claudio Valerio's avatar
Claudio Valerio committed
121 122 123 124 125
}


void UBGraphicsProxyWidget::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
126
    if (Delegate()->mouseMoveEvent(event))
Claudio Valerio's avatar
Claudio Valerio committed
127 128 129 130 131 132 133 134 135 136 137 138
    {
        // NOOP;
    }
    else
    {
        QGraphicsProxyWidget::mouseMoveEvent(event);
    }
}


void UBGraphicsProxyWidget::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
Claudio Valerio's avatar
Claudio Valerio committed
139 140 141 142 143
    if(Delegate()->mouseReleaseEvent(event)){

    }
    else
        QGraphicsProxyWidget::mouseReleaseEvent(event);
Claudio Valerio's avatar
Claudio Valerio committed
144 145
}

146 147
void UBGraphicsProxyWidget::wheelEvent(QGraphicsSceneWheelEvent *event)
{
Claudio Valerio's avatar
Claudio Valerio committed
148
    if( Delegate()->wheelEvent(event) )
149
    {
150
        QGraphicsProxyWidget::wheelEvent(event);
151 152
        event->accept();
    }
153 154
}

155 156 157 158 159 160 161 162 163 164
void UBGraphicsProxyWidget::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
{
    Q_UNUSED(event)
//    NOOP
}
void UBGraphicsProxyWidget::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
{
    Q_UNUSED(event)
//    NOOP
}
Claudio Valerio's avatar
Claudio Valerio committed
165 166 167 168 169 170 171 172 173 174 175

void UBGraphicsProxyWidget::resize(qreal w, qreal h)
{
    UBGraphicsProxyWidget::resize(QSizeF(w, h));
}


void UBGraphicsProxyWidget::resize(const QSizeF & pSize)
{
    if (pSize != size())
    {
176 177 178 179 180
        qreal sizeX = 0;
        qreal sizeY = 0;

        if (widget())
        {
181

182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
            QSizeF minimumItemSize(widget()->minimumSize());
            if (minimumItemSize.width() > pSize.width())
                sizeX = minimumItemSize.width();
            else
                sizeX = pSize.width();

            if (minimumItemSize.height() > pSize.height())
                sizeY = minimumItemSize.height();
            else
                sizeY = pSize.height();
        }
        QSizeF size(sizeX, sizeY);


        QGraphicsProxyWidget::setMaximumSize(size.width(), size.height());
        QGraphicsProxyWidget::resize(size.width(), size.height());
Claudio Valerio's avatar
Claudio Valerio committed
198
        if (widget())
199
            widget()->resize(size.width(), size.height());
200 201
        if (Delegate())
            Delegate()->positionHandles();
Claudio Valerio's avatar
Claudio Valerio committed
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
        if (scene())
            scene()->setModified(true);
    }
}


QSizeF UBGraphicsProxyWidget::size() const
{
    return QGraphicsProxyWidget::size();
}


UBGraphicsScene* UBGraphicsProxyWidget::scene()
{
    return static_cast<UBGraphicsScene*>(QGraphicsItem::scene());
}