1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
10 ** GNU Lesser General Public License Usage
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
31 **************************************************************************/
33 #include "anchorhandleitem.h"
35 #include <formeditoritem.h>
37 #include <QPainterPathStroker>
41 namespace QmlDesigner {
43 AnchorHandleItem::AnchorHandleItem(QGraphicsItem *parent, const AnchorController &anchorController)
44 : QGraphicsItemGroup(parent),
45 m_anchorControllerData(anchorController.weakPointer()),
46 m_sourceAnchorLinePathItem(new QGraphicsPathItem(this)),
47 m_arrowPathItem(new QGraphicsPathItem(this)),
48 m_targetAnchorLinePathItem(new QGraphicsPathItem(this)),
49 m_targetNamePathItem(new QGraphicsPathItem(this))
51 addToGroup(m_sourceAnchorLinePathItem);
52 addToGroup(m_arrowPathItem);
53 addToGroup(m_targetAnchorLinePathItem);
54 addToGroup(m_targetNamePathItem);
56 setFlag(QGraphicsItem::ItemIsMovable, true);
59 AnchorLine::Type AnchorHandleItem::sourceAnchorLine() const
62 return AnchorLine::Top;
64 return AnchorLine::Bottom;
66 return AnchorLine::Left;
68 return AnchorLine::Right;
70 return AnchorLine::Invalid;
73 AnchorLine AnchorHandleItem::targetAnchorLine() const
75 QmlAnchors anchors(anchorController().formEditorItem()->qmlItemNode().anchors());
78 return anchors.instanceAnchor(AnchorLine::Top);
80 return anchors.instanceAnchor(AnchorLine::Bottom);
82 return anchors.instanceAnchor(AnchorLine::Left);
84 return anchors.instanceAnchor(AnchorLine::Right);
89 static QString anchorLineToString(AnchorLine::Type anchorLineType)
91 switch(anchorLineType) {
92 case AnchorLine::Top: return "Top";
93 case AnchorLine::Bottom: return "Bottom";
94 case AnchorLine::Left: return "Left";
95 case AnchorLine::Right: return "Right";
103 QString AnchorHandleItem::toolTipString() const
105 QString templateString("<p>Anchor Handle</p><p>%1</p><p>%2</p>");
106 QmlItemNode fromNode(anchorController().formEditorItem()->qmlItemNode());
107 QString fromString(QString("%3: %1(%2)").arg(fromNode.simplifiedTypeName(), fromNode.id(), anchorLineToString(sourceAnchorLine())));
109 AnchorLine toAnchorLine(targetAnchorLine());
110 QmlItemNode toNode(toAnchorLine.qmlItemNode());
112 if (toNode.isValid())
113 toString = QString("%3: %1(%2)").arg(toNode.simplifiedTypeName(), toNode.id(), anchorLineToString(toAnchorLine.type()));
115 return templateString.arg(fromString).arg(toString);
118 void AnchorHandleItem::setHandlePath(const AnchorHandlePathData &pathData)
120 m_beginArrowPoint = pathData.beginArrowPoint;
121 m_endArrowPoint = pathData.endArrowPoint;
122 m_arrowPathItem->setPath(pathData.arrowPath);
123 m_sourceAnchorLinePathItem->setPath(pathData.sourceAnchorLinePath);
124 m_targetAnchorLinePathItem->setPath(pathData.targetAnchorLinePath);
125 m_targetNamePathItem->setPath(pathData.targetNamePath);
127 setHighlighted(false);
130 AnchorController AnchorHandleItem::anchorController() const
132 Q_ASSERT(!m_anchorControllerData.isNull());
133 return AnchorController(m_anchorControllerData.toStrongRef());
136 AnchorHandleItem* AnchorHandleItem::fromGraphicsItem(QGraphicsItem *item)
138 return qgraphicsitem_cast<AnchorHandleItem*>(item);
141 bool AnchorHandleItem::isTopHandle() const
143 return anchorController().isTopHandle(this);
146 bool AnchorHandleItem::isLeftHandle() const
148 return anchorController().isLeftHandle(this);
151 bool AnchorHandleItem::isRightHandle() const
153 return anchorController().isRightHandle(this);
156 bool AnchorHandleItem::isBottomHandle() const
158 return anchorController().isBottomHandle(this);
161 AnchorLine::Type AnchorHandleItem::anchorType() const
164 return AnchorLine::Top;
166 if (isBottomHandle())
167 return AnchorLine::Bottom;
170 return AnchorLine::Left;
173 return AnchorLine::Right;
176 return AnchorLine::Invalid;
179 void AnchorHandleItem::setHighlighted(bool highlight)
181 QLinearGradient gradient(m_beginArrowPoint, m_endArrowPoint);
182 gradient.setCoordinateMode(QGradient::LogicalMode);
183 m_arrowPathItem->setPen(QPen(QBrush(Qt::gray), 1.0, Qt::SolidLine, Qt::RoundCap, Qt::MiterJoin));
184 m_targetAnchorLinePathItem->setPen(QColor(70, 0, 0, 90));
185 m_targetAnchorLinePathItem->setBrush(QColor(255, 0, 0, 50));
186 m_arrowPathItem->setPen(QPen(QBrush(Qt::gray), 1.0, Qt::SolidLine, Qt::RoundCap, Qt::MiterJoin));
187 m_targetNamePathItem->setPen(QColor(0, 0, 255, 90));
188 m_targetNamePathItem->setBrush(QColor(0, 0, 255, 50));
191 gradient.setColorAt(0.0, QColor(0, 0, 120, 255));
192 gradient.setColorAt(1.0, QColor(120, 0, 0, 255));
193 m_arrowPathItem->setBrush(gradient);
194 m_sourceAnchorLinePathItem->setPen(QColor(0, 0, 70, 255));
195 m_sourceAnchorLinePathItem->setBrush(QColor(0, 0, 70, 255));
196 m_targetAnchorLinePathItem->show();
197 m_targetNamePathItem->show();
200 gradient.setColorAt(0.0, QColor(0, 0, 255, 255));
201 gradient.setColorAt(1.0, QColor(255, 0, 0, 255));
202 m_arrowPathItem->setBrush(gradient);
203 m_sourceAnchorLinePathItem->setPen(QColor(0, 0, 100, 255));
204 m_sourceAnchorLinePathItem->setBrush(QColor(0, 0, 100, 255));
205 m_targetAnchorLinePathItem->hide();
206 m_targetNamePathItem->hide();
210 QPointF AnchorHandleItem::itemSpacePosition() const
212 return parentItem()->mapToItem(anchorController().formEditorItem(), pos());
215 } // namespace QmlDesigner