graphicsobjectnodeinstance.cpp 9.25 KB
Newer Older
1 2 3 4
/**************************************************************************
**
** This file is part of Qt Creator
**
con's avatar
con committed
5
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6
**
hjk's avatar
hjk committed
7
** Contact: Nokia Corporation (info@qt.nokia.com)
8 9 10 11
**
**
** GNU Lesser General Public License Usage
**
hjk's avatar
hjk committed
12 13 14 15 16 17
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this file.
** Please review the following information to ensure the GNU Lesser General
** Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18
**
con's avatar
con committed
19
** In addition, as a special exception, Nokia gives you certain additional
hjk's avatar
hjk committed
20
** rights. These rights are described in the Nokia Qt LGPL Exception
con's avatar
con committed
21 22
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
23 24 25 26 27
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
con's avatar
con committed
28
** If you have questions regarding the use of this file, please contact
Tobias Hunger's avatar
Tobias Hunger committed
29
** Nokia at info@qt.nokia.com.
30 31 32 33 34 35
**
**************************************************************************/

#include "graphicsobjectnodeinstance.h"

#include <QGraphicsObject>
36 37
#include "private/qgraphicsitem_p.h"
#include <QStyleOptionGraphicsItem>
38 39
#include <QPixmap>
#include <QSizeF>
40 41 42 43

namespace QmlDesigner {
namespace Internal {

44
GraphicsObjectNodeInstance::GraphicsObjectNodeInstance(QGraphicsObject *graphicsObject)
45
   : ObjectNodeInstance(graphicsObject),
46
   m_isMovable(true)
47
{
48
    QGraphicsItemPrivate::get(graphicsObject)->sendParentChangeNotification = 1;
49 50 51 52
}

QGraphicsObject *GraphicsObjectNodeInstance::graphicsObject() const
{
53 54 55
    if (object() == 0)
        return 0;

56 57 58 59 60 61 62 63 64
    Q_ASSERT(qobject_cast<QGraphicsObject*>(object()));
    return static_cast<QGraphicsObject*>(object());
}

bool GraphicsObjectNodeInstance::hasContent() const
{
    return m_hasContent;
}

65 66 67 68 69 70 71 72
QList<ServerNodeInstance> GraphicsObjectNodeInstance::childItems() const
{
    QList<ServerNodeInstance> instanceList;
    foreach(QGraphicsItem *item, graphicsObject()->childItems())
    {
        QGraphicsObject *childObject = item->toGraphicsObject();
        if (childObject && nodeInstanceServer()->hasInstanceForObject(childObject)) {
            instanceList.append(nodeInstanceServer()->instanceForObject(childObject));
73 74 75 76 77 78
        } else { //there might be an item in between the parent instance
                 //and the child instance.
                 //Popular example is flickable which has a viewport item between
                 //the flickable item and the flickable children
            instanceList.append(childItemsForChild(item)); //In such a case we go deeper inside the item and
                                                           //search for child items with instances.
79 80 81 82 83 84
        }
    }

    return instanceList;
}

85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
QList<ServerNodeInstance> GraphicsObjectNodeInstance::childItemsForChild(QGraphicsItem *childItem) const
{
    QList<ServerNodeInstance> instanceList;
    if (childItem) {

        foreach(QGraphicsItem *item, childItem->childItems())
        {
            QGraphicsObject *childObject = item->toGraphicsObject();
            if (childObject && nodeInstanceServer()->hasInstanceForObject(childObject)) {
                instanceList.append(nodeInstanceServer()->instanceForObject(childObject));
            } else {
                instanceList.append(childItemsForChild(item));
            }
        }
    }
    return instanceList;
}

103 104 105 106 107
void GraphicsObjectNodeInstance::setHasContent(bool hasContent)
{
    m_hasContent = hasContent;
}

108 109 110 111 112 113 114 115 116 117 118 119
QPointF GraphicsObjectNodeInstance::position() const
{
    return graphicsObject()->pos();
}

QSizeF GraphicsObjectNodeInstance::size() const
{
    return graphicsObject()->boundingRect().size();
}

QTransform GraphicsObjectNodeInstance::transform() const
{
120
    if (!nodeInstanceServer()->hasInstanceForObject(object()))
121 122
        return sceneTransform();

123
    ServerNodeInstance nodeInstanceParent = nodeInstanceServer()->instanceForObject(object()).parent();
124 125 126 127 128 129 130

    if (!nodeInstanceParent.isValid())
        return sceneTransform();

    QGraphicsObject *graphicsObjectParent = qobject_cast<QGraphicsObject*>(nodeInstanceParent.internalObject());
    if (graphicsObjectParent)
        return graphicsObject()->itemTransform(graphicsObjectParent);
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
    else
        return sceneTransform();
}

QTransform GraphicsObjectNodeInstance::customTransform() const
{
    return graphicsObject()->transform();
}

QTransform GraphicsObjectNodeInstance::sceneTransform() const
{
    return graphicsObject()->sceneTransform();
}

double GraphicsObjectNodeInstance::rotation() const
{
    return graphicsObject()->rotation();
}

double GraphicsObjectNodeInstance::scale() const
{
    return graphicsObject()->scale();
}

QList<QGraphicsTransform *> GraphicsObjectNodeInstance::transformations() const
{
    return graphicsObject()->transformations();
}

QPointF GraphicsObjectNodeInstance::transformOriginPoint() const
{
    return graphicsObject()->transformOriginPoint();
}

double GraphicsObjectNodeInstance::zValue() const
{
    return graphicsObject()->zValue();
}

double GraphicsObjectNodeInstance::opacity() const
{
    return graphicsObject()->opacity();
}

QObject *GraphicsObjectNodeInstance::parent() const
{
    if (!graphicsObject() || !graphicsObject()->parentItem())
        return 0;

    return graphicsObject()->parentItem()->toGraphicsObject();
}

183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
QRectF GraphicsObjectNodeInstance::boundingRectWithStepChilds(QGraphicsItem *parentItem) const
{
    QRectF boundingRect = parentItem->boundingRect();

    foreach (QGraphicsItem *childItem, parentItem->childItems()) {
        QGraphicsObject *childObject = childItem->toGraphicsObject();
        if (!(childObject && nodeInstanceServer()->hasInstanceForObject(childObject))) {
            QRectF transformedRect = childItem->mapRectToParent(boundingRectWithStepChilds(childItem));
            boundingRect = boundingRect.united(transformedRect);
        }
    }

    return boundingRect;
}

198 199
QRectF GraphicsObjectNodeInstance::boundingRect() const
{
200 201 202 203 204 205 206 207 208
    if (graphicsObject()) {
        if (graphicsObject()->flags() & QGraphicsItem::ItemClipsChildrenToShape) {
            return graphicsObject()->boundingRect();
        } else {
            return boundingRectWithStepChilds(graphicsObject());
        }
    }

    return QRectF();
209 210 211 212 213 214 215 216 217 218 219 220
}

bool GraphicsObjectNodeInstance::isGraphicsObject() const
{
    return true;
}

void GraphicsObjectNodeInstance::setPropertyVariant(const QString &name, const QVariant &value)
{
    ObjectNodeInstance::setPropertyVariant(name, value);
}

221 222 223 224 225
void GraphicsObjectNodeInstance::setPropertyBinding(const QString &name, const QString &expression)
{
    ObjectNodeInstance::setPropertyBinding(name, expression);
}

226 227 228 229 230 231 232 233 234 235
QVariant GraphicsObjectNodeInstance::property(const QString &name) const
{
    return ObjectNodeInstance::property(name);
}

bool GraphicsObjectNodeInstance::equalGraphicsItem(QGraphicsItem *item) const
{
    return item == graphicsObject();
}

236 237 238 239 240 241
void initOption(QGraphicsItem *item, QStyleOptionGraphicsItem *option, const QTransform &transform)
{
    QGraphicsItemPrivate *privateItem = QGraphicsItemPrivate::get(item);
    privateItem->initStyleOption(option, transform, QRegion());
}

242
QImage GraphicsObjectNodeInstance::renderImage() const
243
{
244
    QRectF boundingRect = GraphicsObjectNodeInstance::boundingRect();
245 246
    QSize boundingSize = boundingRect.size().toSize();

247
    QImage image(boundingSize, QImage::Format_ARGB32_Premultiplied);
248

249 250
    if (image.isNull())
        return image;
251

252
    image.fill(0x00000000);
253

254
    QPainter painter(&image);
255 256 257 258 259 260 261 262 263 264
    painter.translate(-boundingRect.topLeft());

    if (hasContent()) {
        QStyleOptionGraphicsItem option;
        initOption(graphicsObject(), &option, painter.transform());
        graphicsObject()->paint(&painter, &option);
    }

    foreach(QGraphicsItem *graphicsItem, graphicsObject()->childItems())
        paintRecursively(graphicsItem, &painter);
265

266
    return image;
267 268
}

269 270
void GraphicsObjectNodeInstance::paintRecursively(QGraphicsItem *graphicsItem, QPainter *painter) const
{
271 272
    QGraphicsObject *graphicsObject = graphicsItem->toGraphicsObject();
    if (graphicsObject) {
273
        if (nodeInstanceServer()->hasInstanceForObject(graphicsObject))
274 275 276
            return; //we already keep track of this object elsewhere
    }

277 278 279 280
    if (graphicsItem->isVisible()) {
        painter->save();
        painter->setTransform(graphicsItem->itemTransform(graphicsItem->parentItem()), true);
        painter->setOpacity(graphicsItem->opacity() * painter->opacity());
281 282 283
        QStyleOptionGraphicsItem option;
        initOption(graphicsItem, &option, painter->transform());
        graphicsItem->paint(painter, &option);
284 285 286 287 288 289 290 291 292 293 294
        foreach(QGraphicsItem *childItem, graphicsItem->childItems()) {
            paintRecursively(childItem, painter);
        }
        painter->restore();
    }
}

void GraphicsObjectNodeInstance::paintUpdate()
{
    graphicsObject()->update();
}
295 296 297

bool GraphicsObjectNodeInstance::isMovable() const
{
298 299 300
    if (isRootNodeInstance())
        return false;

301 302 303 304 305 306 307 308
    return m_isMovable && graphicsObject() && graphicsObject()->parentItem();
}

void GraphicsObjectNodeInstance::setMovable(bool movable)
{
    m_isMovable = movable;
}

309 310
} // namespace Internal
} // namespace QmlDesigner