OSDN Git Service

Update license.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / qmldesigner / components / formeditor / anchorhandleitem.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
8 **
9 **
10 ** GNU Lesser General Public License Usage
11 **
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.
18 **
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.
22 **
23 ** Other Usage
24 **
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.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **************************************************************************/
32
33 #include "anchorhandleitem.h"
34
35 #include <formeditoritem.h>
36 #include <QPen>
37 #include <QPainterPathStroker>
38 #include <cmath>
39 #include <QtDebug>
40
41 namespace QmlDesigner {
42
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))
50 {
51     addToGroup(m_sourceAnchorLinePathItem);
52     addToGroup(m_arrowPathItem);
53     addToGroup(m_targetAnchorLinePathItem);
54     addToGroup(m_targetNamePathItem);
55
56     setFlag(QGraphicsItem::ItemIsMovable, true);
57 }
58
59 AnchorLine::Type AnchorHandleItem::sourceAnchorLine() const
60 {
61     if (isTopHandle())
62         return AnchorLine::Top;
63     if (isBottomHandle())
64         return AnchorLine::Bottom;
65     if (isLeftHandle())
66         return AnchorLine::Left;
67     if (isRightHandle())
68         return AnchorLine::Right;
69
70     return AnchorLine::Invalid;
71 }
72
73 AnchorLine AnchorHandleItem::targetAnchorLine() const
74 {
75     QmlAnchors anchors(anchorController().formEditorItem()->qmlItemNode().anchors());
76
77     if (isTopHandle())
78         return anchors.instanceAnchor(AnchorLine::Top);
79     if (isBottomHandle())
80         return anchors.instanceAnchor(AnchorLine::Bottom);
81     if (isLeftHandle())
82         return anchors.instanceAnchor(AnchorLine::Left);
83     if (isRightHandle())
84         return anchors.instanceAnchor(AnchorLine::Right);
85
86     return AnchorLine();
87 }
88
89 static QString anchorLineToString(AnchorLine::Type anchorLineType)
90 {
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";
96         default: break;
97     }
98
99     return QString();
100
101 }
102
103 QString AnchorHandleItem::toolTipString() const
104 {
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())));
108
109     AnchorLine toAnchorLine(targetAnchorLine());
110     QmlItemNode toNode(toAnchorLine.qmlItemNode());
111     QString toString;
112     if (toNode.isValid())
113         toString = QString("%3: %1(%2)").arg(toNode.simplifiedTypeName(), toNode.id(), anchorLineToString(toAnchorLine.type()));
114
115     return templateString.arg(fromString).arg(toString);
116 }
117
118 void AnchorHandleItem::setHandlePath(const AnchorHandlePathData  &pathData)
119 {
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);
126
127     setHighlighted(false);
128 }
129
130 AnchorController AnchorHandleItem::anchorController() const
131 {
132     Q_ASSERT(!m_anchorControllerData.isNull());
133     return AnchorController(m_anchorControllerData.toStrongRef());
134 }
135
136 AnchorHandleItem* AnchorHandleItem::fromGraphicsItem(QGraphicsItem *item)
137 {
138     return qgraphicsitem_cast<AnchorHandleItem*>(item);
139 }
140
141 bool AnchorHandleItem::isTopHandle() const
142 {
143     return anchorController().isTopHandle(this);
144 }
145
146 bool AnchorHandleItem::isLeftHandle() const
147 {
148     return anchorController().isLeftHandle(this);
149 }
150
151 bool AnchorHandleItem::isRightHandle() const
152 {
153     return anchorController().isRightHandle(this);
154 }
155
156 bool AnchorHandleItem::isBottomHandle() const
157 {
158     return anchorController().isBottomHandle(this);
159 }
160
161 AnchorLine::Type AnchorHandleItem::anchorType() const
162 {
163     if (isTopHandle())
164         return AnchorLine::Top;
165
166     if (isBottomHandle())
167         return AnchorLine::Bottom;
168
169     if (isLeftHandle())
170         return AnchorLine::Left;
171
172     if (isRightHandle())
173         return AnchorLine::Right;
174
175
176     return AnchorLine::Invalid;
177 }
178
179 void AnchorHandleItem::setHighlighted(bool highlight)
180 {
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));
189
190     if (highlight) {
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();
198
199     } else {
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();
207     }
208 }
209
210 QPointF AnchorHandleItem::itemSpacePosition() const
211 {
212     return parentItem()->mapToItem(anchorController().formEditorItem(), pos());
213 }
214
215 } // namespace QmlDesigner