UBGraphicsProxyWidget.cpp 5.05 KB
Newer Older
Claudio Valerio's avatar
Claudio Valerio committed
1
/*
2
 * Copyright (C) 2010-2013 Groupement d'Intérêt Public pour l'Education Numérique en Afrique (GIP ENA)
Claudio Valerio's avatar
Claudio Valerio committed
3
 *
4 5
 * This file is part of Open-Sankoré.
 *
Claudio Valerio's avatar
Claudio Valerio committed
6 7 8
 * Open-Sankoré 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, version 3 of the License,
9 10 11 12 13
 * 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).
 *
 * Open-Sankoré is distributed in the hope that it will be useful,
Claudio Valerio's avatar
Claudio Valerio committed
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Claudio Valerio's avatar
Claudio Valerio committed
15 16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
Claudio Valerio's avatar
Claudio Valerio committed
17
 *
Claudio Valerio's avatar
Claudio Valerio committed
18 19
 * You should have received a copy of the GNU General Public License
 * along with Open-Sankoré.  If not, see <http://www.gnu.org/licenses/>.
Claudio Valerio's avatar
Claudio Valerio committed
20 21
 */

22

Claudio Valerio's avatar
Claudio Valerio committed
23

Claudio Valerio's avatar
Claudio Valerio committed
24 25 26 27 28 29 30 31 32
#include "UBGraphicsProxyWidget.h"

#include <QtGui>

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

#include "UBGraphicsDelegateFrame.h"

33
#include "core/memcheck.h"
Claudio Valerio's avatar
Claudio Valerio committed
34 35 36 37 38 39

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

40 41 42
    //UBGraphicsItemDelegate* delegate = new UBGraphicsItemDelegate(this,0, true, false, false);
    //delegate->init();
    //setDelegate(delegate);
Claudio Valerio's avatar
Claudio Valerio committed
43 44 45 46 47 48 49 50 51 52 53

    setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);

    QGraphicsProxyWidget::setAcceptHoverEvents(true);
}


UBGraphicsProxyWidget::~UBGraphicsProxyWidget()
{
}

54 55 56 57
void UBGraphicsProxyWidget::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    Delegate()->postpaint(painter, option, widget);
}
Claudio Valerio's avatar
Claudio Valerio committed
58 59 60

QVariant UBGraphicsProxyWidget::itemChange(GraphicsItemChange change, const QVariant &value)
{
61 62 63 64
    if (change == QGraphicsItem::ItemCursorHasChanged &&  scene())
    {
        unsetCursor();
    }
Claudio Valerio's avatar
Claudio Valerio committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
    if ((change == QGraphicsItem::ItemSelectedHasChanged)
              &&  scene())
    {
        if (isSelected())
        {
            scene()->setActiveWindow(this);
        }
        else
        {
            if(scene()->activeWindow() == this)
            {
                scene()->setActiveWindow(0);
            }
        }
    }

81
    QVariant newValue = Delegate()->itemChange(change, value);
Claudio Valerio's avatar
Claudio Valerio committed
82 83 84
    return QGraphicsProxyWidget::itemChange(change, newValue);
}

root's avatar
root committed
85 86 87 88 89
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
90 91 92

void UBGraphicsProxyWidget::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
93
    if (Delegate()->mousePressEvent(event))
Claudio Valerio's avatar
Claudio Valerio committed
94 95 96 97 98 99
    {
        //NOOP
    }
    else
    {
        // QT Proxy Widget is a bit lazy, we force the selection ...
100

Claudio Valerio's avatar
Claudio Valerio committed
101 102
        setSelected(true);
    }
103
    QGraphicsProxyWidget::mousePressEvent(event);
Claudio Valerio's avatar
Claudio Valerio committed
104 105 106 107 108
}


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


void UBGraphicsProxyWidget::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
122
    Delegate()->mouseReleaseEvent(event);
Claudio Valerio's avatar
Claudio Valerio committed
123 124 125
    QGraphicsProxyWidget::mouseReleaseEvent(event);
}

126 127
void UBGraphicsProxyWidget::wheelEvent(QGraphicsSceneWheelEvent *event)
{
128
    if( Delegate()->weelEvent(event) )
129
    {
130
        QGraphicsProxyWidget::wheelEvent(event);
131 132
        event->accept();
    }
133 134
}

135 136 137 138 139 140 141 142 143 144
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
145 146 147 148 149 150 151 152 153 154 155

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


void UBGraphicsProxyWidget::resize(const QSizeF & pSize)
{
    if (pSize != size())
    {
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
        qreal sizeX = 0;
        qreal sizeY = 0;

        if (widget())
        {
            
            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
178
        if (widget())
179
            widget()->resize(size.width(), size.height());
180 181
        if (Delegate())
            Delegate()->positionHandles();
Claudio Valerio's avatar
Claudio Valerio committed
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
        if (scene())
            scene()->setModified(true);
    }
}


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


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