UBGraphicsWebView.cpp 3.79 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * 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
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * 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/>.
 */

#include <QtGui>
#include <QtWebKit>

19
#include "UBGraphicsWebView.h"
20 21 22 23 24 25
#include "UBGraphicsScene.h"
#include "UBGraphicsItemDelegate.h"
#include "UBGraphicsDelegateFrame.h"

#include "core/memcheck.h"

26 27
UBGraphicsWebView::UBGraphicsWebView(QGraphicsItem* parent)
    : QGraphicsWebView(parent)
28 29 30
{
    setData(UBGraphicsItemData::ItemLayerType, UBItemLayerType::Object);

31
    mDelegate = new UBGraphicsItemDelegate(this, 0, true);
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
    mDelegate->init();

    setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);

    QGraphicsWebView::setAcceptHoverEvents(true);
}


UBGraphicsWebView::~UBGraphicsWebView()
{
    if (mDelegate)
        delete mDelegate;
}


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

    QVariant newValue = mDelegate->itemChange(change, value);
    return QGraphicsWebView::itemChange(change, newValue);
}

void UBGraphicsWebView::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
}

void UBGraphicsWebView::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
69 70
    if (!mDelegate->mousePressEvent(event))
        setSelected(true); /* forcing selection */
71 72 73 74 75 76 77

    QGraphicsWebView::mousePressEvent(event);
}


void UBGraphicsWebView::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
78
    if (!mDelegate->mouseMoveEvent(event))
79 80 81 82 83 84 85 86 87 88 89 90
        QGraphicsWebView::mouseMoveEvent(event);
}


void UBGraphicsWebView::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
    mDelegate->mouseReleaseEvent(event);
    QGraphicsWebView::mouseReleaseEvent(event);
}

void UBGraphicsWebView::wheelEvent(QGraphicsSceneWheelEvent *event)
{
91
    if (mDelegate->weelEvent(event))
92 93 94 95 96 97 98 99 100
    {
        QGraphicsWebView::wheelEvent(event);
        event->accept();
    }
}

void UBGraphicsWebView::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
{
    Q_UNUSED(event)
101
    /* NOOP */
102 103 104 105
}
void UBGraphicsWebView::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
{
    Q_UNUSED(event)
106
    /* NOOP */
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
}

void UBGraphicsWebView::setDelegate(UBGraphicsItemDelegate* pDelegate)
{
    if (mDelegate)
        delete mDelegate;

    mDelegate = pDelegate;
}


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


void UBGraphicsWebView::resize(const QSizeF & pSize)
{
126
    if (pSize != size()) {
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
        QGraphicsWebView::setMaximumSize(pSize.width(), pSize.height());
        QGraphicsWebView::resize(pSize.width(), pSize.height());
        if (mDelegate)
            mDelegate->positionHandles();
        if (scene())
            scene()->setModified(true);
    }
}

QSizeF UBGraphicsWebView::size() const
{
    return QGraphicsWebView::size();
}


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


void UBGraphicsWebView::remove()
{
    if (mDelegate)
        mDelegate->remove(true);
}