UBGraphicsProxyWidget.cpp 4.62 KB
Newer Older
Claudio Valerio's avatar
Claudio Valerio committed
1
/*
Claudio Valerio's avatar
Claudio Valerio committed
2 3
 * 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
Claudio Valerio's avatar
Claudio Valerio committed
4
 * the Free Software Foundation, either version 2 of the License, or
Claudio Valerio's avatar
Claudio Valerio committed
5
 * (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 18 19 20 21 22 23 24
 */

#include "UBGraphicsProxyWidget.h"

#include <QtGui>

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

#include "UBGraphicsDelegateFrame.h"

25
#include "core/memcheck.h"
Claudio Valerio's avatar
Claudio Valerio committed
26 27 28 29 30 31

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

32 33 34
    //UBGraphicsItemDelegate* delegate = new UBGraphicsItemDelegate(this,0, true, false, false);
    //delegate->init();
    //setDelegate(delegate);
Claudio Valerio's avatar
Claudio Valerio committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48

    setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);

    QGraphicsProxyWidget::setAcceptHoverEvents(true);
}


UBGraphicsProxyWidget::~UBGraphicsProxyWidget()
{
}


QVariant UBGraphicsProxyWidget::itemChange(GraphicsItemChange change, const QVariant &value)
{
49 50 51 52
    if (change == QGraphicsItem::ItemCursorHasChanged &&  scene())
    {
        unsetCursor();
    }
Claudio Valerio's avatar
Claudio Valerio committed
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
    if ((change == QGraphicsItem::ItemSelectedHasChanged)
              &&  scene())
    {
        if (isSelected())
        {
            scene()->setActiveWindow(this);
        }
        else
        {
            if(scene()->activeWindow() == this)
            {
                scene()->setActiveWindow(0);
            }
        }
    }

69
    QVariant newValue = Delegate()->itemChange(change, value);
Claudio Valerio's avatar
Claudio Valerio committed
70 71 72
    return QGraphicsProxyWidget::itemChange(change, newValue);
}

root's avatar
root committed
73 74 75 76 77
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
78 79 80

void UBGraphicsProxyWidget::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
81
    if (Delegate()->mousePressEvent(event))
Claudio Valerio's avatar
Claudio Valerio committed
82 83 84 85 86 87
    {
        //NOOP
    }
    else
    {
        // QT Proxy Widget is a bit lazy, we force the selection ...
88

Claudio Valerio's avatar
Claudio Valerio committed
89 90
        setSelected(true);
    }
91
    QGraphicsProxyWidget::mousePressEvent(event);
Claudio Valerio's avatar
Claudio Valerio committed
92 93 94 95 96
}


void UBGraphicsProxyWidget::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
97
    if (Delegate()->mouseMoveEvent(event))
Claudio Valerio's avatar
Claudio Valerio committed
98 99 100 101 102 103 104 105 106 107 108 109
    {
        // NOOP;
    }
    else
    {
        QGraphicsProxyWidget::mouseMoveEvent(event);
    }
}


void UBGraphicsProxyWidget::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
110
    Delegate()->mouseReleaseEvent(event);
Claudio Valerio's avatar
Claudio Valerio committed
111 112 113
    QGraphicsProxyWidget::mouseReleaseEvent(event);
}

114 115
void UBGraphicsProxyWidget::wheelEvent(QGraphicsSceneWheelEvent *event)
{
116
    if( Delegate()->weelEvent(event) )
117
    {
118
        QGraphicsProxyWidget::wheelEvent(event);
119 120
        event->accept();
    }
121 122
}

123 124 125 126 127 128 129 130 131 132
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
133 134 135 136 137 138 139 140 141 142 143

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


void UBGraphicsProxyWidget::resize(const QSizeF & pSize)
{
    if (pSize != size())
    {
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
        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
166
        if (widget())
167
            widget()->resize(size.width(), size.height());
168 169
        if (Delegate())
            Delegate()->positionHandles();
Claudio Valerio's avatar
Claudio Valerio committed
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
        if (scene())
            scene()->setModified(true);
    }
}


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


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