anchorhandleitem.cpp 6.76 KB
Newer Older
1 2 3 4
/**************************************************************************
**
** This file is part of Qt Creator
**
hjk's avatar
hjk committed
5
** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies).
6
**
Eike Ziller's avatar
Eike Ziller committed
7
** Contact: http://www.qt-project.org/
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.
**
28 29 30 31 32 33 34 35 36
**
**************************************************************************/

#include "anchorhandleitem.h"

#include <formeditoritem.h>
#include <QPen>
#include <QPainterPathStroker>
#include <cmath>
hjk's avatar
hjk committed
37
#include <QDebug>
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104

namespace QmlDesigner {

AnchorHandleItem::AnchorHandleItem(QGraphicsItem *parent, const AnchorController &anchorController)
    : QGraphicsItemGroup(parent),
    m_anchorControllerData(anchorController.weakPointer()),
    m_sourceAnchorLinePathItem(new QGraphicsPathItem(this)),
    m_arrowPathItem(new QGraphicsPathItem(this)),
    m_targetAnchorLinePathItem(new QGraphicsPathItem(this)),
    m_targetNamePathItem(new QGraphicsPathItem(this))
{
    addToGroup(m_sourceAnchorLinePathItem);
    addToGroup(m_arrowPathItem);
    addToGroup(m_targetAnchorLinePathItem);
    addToGroup(m_targetNamePathItem);

    setFlag(QGraphicsItem::ItemIsMovable, true);
}

AnchorLine::Type AnchorHandleItem::sourceAnchorLine() const
{
    if (isTopHandle())
        return AnchorLine::Top;
    if (isBottomHandle())
        return AnchorLine::Bottom;
    if (isLeftHandle())
        return AnchorLine::Left;
    if (isRightHandle())
        return AnchorLine::Right;

    return AnchorLine::Invalid;
}

AnchorLine AnchorHandleItem::targetAnchorLine() const
{
    QmlAnchors anchors(anchorController().formEditorItem()->qmlItemNode().anchors());

    if (isTopHandle())
        return anchors.instanceAnchor(AnchorLine::Top);
    if (isBottomHandle())
        return anchors.instanceAnchor(AnchorLine::Bottom);
    if (isLeftHandle())
        return anchors.instanceAnchor(AnchorLine::Left);
    if (isRightHandle())
        return anchors.instanceAnchor(AnchorLine::Right);

    return AnchorLine();
}

static QString anchorLineToString(AnchorLine::Type anchorLineType)
{
    switch(anchorLineType) {
        case AnchorLine::Top: return "Top";
        case AnchorLine::Bottom: return "Bottom";
        case AnchorLine::Left: return "Left";
        case AnchorLine::Right: return "Right";
        default: break;
    }

    return QString();

}

QString AnchorHandleItem::toolTipString() const
{
    QString templateString("<p>Anchor Handle</p><p>%1</p><p>%2</p>");
    QmlItemNode fromNode(anchorController().formEditorItem()->qmlItemNode());
105
    QString fromString(QString("%3: %1(%2)").arg(fromNode.simplifiedTypeName(), fromNode.id(), anchorLineToString(sourceAnchorLine())));
106 107 108 109 110

    AnchorLine toAnchorLine(targetAnchorLine());
    QmlItemNode toNode(toAnchorLine.qmlItemNode());
    QString toString;
    if (toNode.isValid())
111
        toString = QString("%3: %1(%2)").arg(toNode.simplifiedTypeName(), toNode.id(), anchorLineToString(toAnchorLine.type()));
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 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 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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213

    return templateString.arg(fromString).arg(toString);
}

void AnchorHandleItem::setHandlePath(const AnchorHandlePathData  &pathData)
{
    m_beginArrowPoint = pathData.beginArrowPoint;
    m_endArrowPoint = pathData.endArrowPoint;
    m_arrowPathItem->setPath(pathData.arrowPath);
    m_sourceAnchorLinePathItem->setPath(pathData.sourceAnchorLinePath);
    m_targetAnchorLinePathItem->setPath(pathData.targetAnchorLinePath);
    m_targetNamePathItem->setPath(pathData.targetNamePath);

    setHighlighted(false);
}

AnchorController AnchorHandleItem::anchorController() const
{
    Q_ASSERT(!m_anchorControllerData.isNull());
    return AnchorController(m_anchorControllerData.toStrongRef());
}

AnchorHandleItem* AnchorHandleItem::fromGraphicsItem(QGraphicsItem *item)
{
    return qgraphicsitem_cast<AnchorHandleItem*>(item);
}

bool AnchorHandleItem::isTopHandle() const
{
    return anchorController().isTopHandle(this);
}

bool AnchorHandleItem::isLeftHandle() const
{
    return anchorController().isLeftHandle(this);
}

bool AnchorHandleItem::isRightHandle() const
{
    return anchorController().isRightHandle(this);
}

bool AnchorHandleItem::isBottomHandle() const
{
    return anchorController().isBottomHandle(this);
}

AnchorLine::Type AnchorHandleItem::anchorType() const
{
    if (isTopHandle())
        return AnchorLine::Top;

    if (isBottomHandle())
        return AnchorLine::Bottom;

    if (isLeftHandle())
        return AnchorLine::Left;

    if (isRightHandle())
        return AnchorLine::Right;


    return AnchorLine::Invalid;
}

void AnchorHandleItem::setHighlighted(bool highlight)
{
    QLinearGradient gradient(m_beginArrowPoint, m_endArrowPoint);
    gradient.setCoordinateMode(QGradient::LogicalMode);
    m_arrowPathItem->setPen(QPen(QBrush(Qt::gray), 1.0, Qt::SolidLine, Qt::RoundCap, Qt::MiterJoin));
    m_targetAnchorLinePathItem->setPen(QColor(70, 0, 0, 90));
    m_targetAnchorLinePathItem->setBrush(QColor(255, 0, 0, 50));
    m_arrowPathItem->setPen(QPen(QBrush(Qt::gray), 1.0, Qt::SolidLine, Qt::RoundCap, Qt::MiterJoin));
    m_targetNamePathItem->setPen(QColor(0, 0, 255, 90));
    m_targetNamePathItem->setBrush(QColor(0, 0, 255, 50));

    if (highlight) {
        gradient.setColorAt(0.0,  QColor(0, 0, 120, 255));
        gradient.setColorAt(1.0,  QColor(120, 0, 0, 255));
        m_arrowPathItem->setBrush(gradient);
        m_sourceAnchorLinePathItem->setPen(QColor(0, 0, 70, 255));
        m_sourceAnchorLinePathItem->setBrush(QColor(0, 0, 70, 255));
        m_targetAnchorLinePathItem->show();
        m_targetNamePathItem->show();

    } else {
        gradient.setColorAt(0.0,  QColor(0, 0, 255, 255));
        gradient.setColorAt(1.0,  QColor(255, 0, 0, 255));
        m_arrowPathItem->setBrush(gradient);
        m_sourceAnchorLinePathItem->setPen(QColor(0, 0, 100, 255));
        m_sourceAnchorLinePathItem->setBrush(QColor(0, 0, 100, 255));
        m_targetAnchorLinePathItem->hide();
        m_targetNamePathItem->hide();
    }
}

QPointF AnchorHandleItem::itemSpacePosition() const
{
    return parentItem()->mapToItem(anchorController().formEditorItem(), pos());
}

} // namespace QmlDesigner