Hardware Design: SIE
Sign in or create your account | Project List | Help
Hardware Design: SIE Git Source Tree
Root/
Source at commit 32a206d created 13 years 5 months ago. By Juan64Bits, First commit for sie code generator project. | |
---|---|
1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
4 | ** All rights reserved. |
5 | ** Contact: Nokia Corporation (qt-info@nokia.com) |
6 | ** |
7 | ** This file is part of the examples of the Qt Toolkit. |
8 | ** |
9 | ** $QT_BEGIN_LICENSE:LGPL$ |
10 | ** Commercial Usage |
11 | ** Licensees holding valid Qt Commercial licenses may use this file in |
12 | ** accordance with the Qt Commercial License Agreement provided with the |
13 | ** Software or, alternatively, in accordance with the terms contained in |
14 | ** a written agreement between you and Nokia. |
15 | ** |
16 | ** GNU Lesser General Public License Usage |
17 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
18 | ** General Public License version 2.1 as published by the Free Software |
19 | ** Foundation and appearing in the file LICENSE.LGPL included in the |
20 | ** packaging of this file. Please review the following information to |
21 | ** ensure the GNU Lesser General Public License version 2.1 requirements |
22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
23 | ** |
24 | ** In addition, as a special exception, Nokia gives you certain additional |
25 | ** rights. These rights are described in the Nokia Qt LGPL Exception |
26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
27 | ** |
28 | ** GNU General Public License Usage |
29 | ** Alternatively, this file may be used under the terms of the GNU |
30 | ** General Public License version 3.0 as published by the Free Software |
31 | ** Foundation and appearing in the file LICENSE.GPL included in the |
32 | ** packaging of this file. Please review the following information to |
33 | ** ensure the GNU General Public License version 3.0 requirements will be |
34 | ** met: http://www.gnu.org/copyleft/gpl.html. |
35 | ** |
36 | ** If you have questions regarding the use of this file, please contact |
37 | ** Nokia at qt-info@nokia.com. |
38 | ** $QT_END_LICENSE$ |
39 | ** |
40 | ****************************************************************************/ |
41 | |
42 | #include <QtGui> |
43 | |
44 | #include "arrow.h" |
45 | #include <math.h> |
46 | |
47 | const qreal Pi = 3.14; |
48 | |
49 | Arrow::Arrow(DiagramTextItem *startItem, DiagramTextItem *endItem, |
50 | QGraphicsItem *parent, QGraphicsScene *scene) |
51 | : QGraphicsPathItem(parent, scene) |
52 | { |
53 | myStartItem = startItem; |
54 | myEndItem = endItem; |
55 | arrowSize = 10; |
56 | moving=0; |
57 | //setFlag(QGraphicsItem::ItemIsMovable, true); |
58 | setFlag(QGraphicsItem::ItemIsSelectable, true); |
59 | myColor = Qt::black; |
60 | setPen(QPen(myColor, 2, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin)); |
61 | myOwnerScene = scene; |
62 | lineItem *newLine= new lineItem(QPointF(0,0),QPointF(0,0),this); |
63 | myOwnerScene->addItem(newLine); |
64 | addLine(newLine); |
65 | } |
66 | |
67 | void Arrow::removeLine(lineItem *line) |
68 | { |
69 | int index = myLines.indexOf(line); |
70 | int cIndex = myCLines.indexOf(line); |
71 | if(cIndex!=-1) index = cIndex; //Prevent segmentation fault |
72 | |
73 | if (index != -1){ |
74 | //Adjust the corners |
75 | if(myLines.count()>1) |
76 | { |
77 | if(index==0) |
78 | { |
79 | myCorners.removeAt(0); |
80 | myOwnerScene->removeItem(myCLines.at(0)); |
81 | myCLines.removeAt(0); |
82 | } |
83 | else if(index>0) |
84 | { |
85 | myCorners.removeAt(index-1); |
86 | myOwnerScene->removeItem(myCLines.at(index-1)); |
87 | myCLines.removeAt(index-1); |
88 | } |
89 | } |
90 | myLines.removeAt(index); |
91 | } |
92 | |
93 | //If lines is empty the arrow is removed |
94 | if (myLines.isEmpty()) |
95 | { |
96 | this->startItem()->ownerItem()->removeArrow(this); |
97 | this->endItem()->ownerItem()->removeArrow(this); |
98 | this->myOwnerScene->removeItem(this); |
99 | } |
100 | } |
101 | |
102 | void Arrow::removeLines() |
103 | { |
104 | foreach (lineItem *line, myLines) { |
105 | scene()->removeItem(line); |
106 | delete line; |
107 | } |
108 | foreach (lineItem *line, myCLines) { |
109 | scene()->removeItem(line); |
110 | delete line; |
111 | } |
112 | } |
113 | |
114 | bool Arrow::addLine(lineItem *line) |
115 | { |
116 | myLines.append(line); |
117 | return 1; |
118 | } |
119 | |
120 | QRectF Arrow::boundingRect() const |
121 | { |
122 | qreal extra = (pen().width() + arrowSize*2) / 2.0; |
123 | |
124 | return QGraphicsPathItem::boundingRect() |
125 | .normalized() |
126 | .adjusted(-extra, -extra, extra, extra); |
127 | } |
128 | |
129 | QPainterPath Arrow::shape() const |
130 | { |
131 | QPainterPath path = QGraphicsPathItem::shape(); |
132 | return path; //FIXME, Arrow heads must to be include |
133 | } |
134 | |
135 | void Arrow::updatePosition() |
136 | { |
137 | QPainterPath myPath; |
138 | myPath.moveTo(myStartItem->ownerItem()->pos()+myStartItem->offset()+ |
139 | QPointF(arrowSize,0)); |
140 | if(!myCorners.isEmpty()) |
141 | foreach(QPointF corner, myCorners){ |
142 | myPath.lineTo(corner); |
143 | } |
144 | myPath.lineTo(myEndItem->ownerItem()->pos()+myEndItem->offset()- |
145 | QPointF(arrowSize,0)); |
146 | |
147 | setPath(myPath); |
148 | |
149 | } |
150 | //! [3] |
151 | |
152 | void Arrow::paint(QPainter *painter, const QStyleOptionGraphicsItem *, |
153 | QWidget *) |
154 | { |
155 | QPen myPen = pen(); |
156 | myPen.setColor(myColor); |
157 | painter->setPen(myPen); |
158 | painter->setBrush(myColor); |
159 | |
160 | QPointF endPos=myEndItem->ownerItem()->pos()+myEndItem->offset()- |
161 | QPointF(arrowSize,0); |
162 | QPointF startPos=myStartItem->ownerItem()->pos()+myStartItem->offset()+ |
163 | QPointF(arrowSize,0); |
164 | |
165 | if(myCorners.isEmpty()) |
166 | { |
167 | myLines.first()->setLine(QLineF(startPos,endPos)); |
168 | } |
169 | else |
170 | { |
171 | myLines.first()->setLine(QLineF(startPos,myCorners.first())); |
172 | for(int i=0; i<myCorners.count()-1;i++) |
173 | myLines.at(i+1)->setLine(QLineF(myCorners.at(i),myCorners.at(i+1))); |
174 | myLines.last()->setLine(QLineF(myCorners.last(),endPos)); |
175 | } |
176 | |
177 | //Drawing arrow selected |
178 | for(int i=0; i<myLines.count();i++) |
179 | if (isSelected()) |
180 | { |
181 | painter->setPen(QPen(Qt::gray, 2, Qt::SolidLine)); |
182 | QLineF myLine = myLines.at(i)->line(); |
183 | painter->drawLine(myLine); |
184 | foreach (lineItem *line, myLines) { line->setColor(Qt::gray);} |
185 | foreach (lineItem *line, myCLines) { line->setColor(Qt::gray);} |
186 | } |
187 | else |
188 | { |
189 | foreach (lineItem *line, myLines) { line->setColor(myColor);} |
190 | foreach (lineItem *line, myCLines) { line->setColor(myColor);} |
191 | } |
192 | |
193 | //End arrow head |
194 | QPointF arrowP1=QPointF(endPos+QPointF(0,arrowSize)); |
195 | QPointF arrowP2=QPointF(endPos+QPointF(0,-arrowSize)); |
196 | |
197 | arrowHeadEnd.clear(); |
198 | arrowHeadEnd << endPos+QPointF(arrowSize,0) << arrowP1 << arrowP2; |
199 | |
200 | painter->drawPolygon(arrowHeadEnd); |
201 | //Start arrow head |
202 | arrowP1=QPointF(startPos+QPointF(-arrowSize,arrowSize)); |
203 | arrowP2=QPointF(startPos+QPointF(-arrowSize,-arrowSize)); |
204 | |
205 | arrowHeadStart.clear(); |
206 | arrowHeadStart << startPos << arrowP1 << arrowP2; |
207 | painter->drawPolygon(arrowHeadStart); |
208 | |
209 | //Set visible corners |
210 | bool anySelected = 0; |
211 | |
212 | foreach(lineItem *line,myLines){ if(line->isSelected())anySelected=1;} |
213 | foreach(lineItem *line,myCLines){ if(line->isSelected())anySelected=1;} |
214 | if(isSelected()) anySelected=1; |
215 | |
216 | if(anySelected) |
217 | setVisibleCorners(1); |
218 | else |
219 | setVisibleCorners(0); |
220 | } |
221 | |
222 | void Arrow::createCorner(QPointF cornerPos, lineItem *inLine) |
223 | { |
224 | createCorner(cornerPos,myLines.indexOf(inLine)); |
225 | } |
226 | |
227 | void Arrow::createCorner(QPointF cornerPos, int index) |
228 | { |
229 | if (index != -1) |
230 | { |
231 | myCorners.insert(index,cornerPos); |
232 | lineItem *newLine= new lineItem(QPointF(0,0),QPointF(0,0),this); |
233 | myOwnerScene->addItem(newLine); |
234 | newLine->setZValue(this->zValue()); |
235 | addLine(newLine); |
236 | newLine= new lineItem(cornerPos+QPointF(-0.5,-0.5) , |
237 | cornerPos+QPointF(0.5,0.5),this,6,1); |
238 | myOwnerScene->addItem(newLine); |
239 | newLine->setZValue(this->zValue()+0.1); |
240 | myCLines.insert(index,newLine); |
241 | } |
242 | } |
243 | |
244 | void Arrow::moveCorner(QPointF cornerPos, lineItem *inLine) |
245 | { |
246 | int index = myCLines.indexOf(inLine); |
247 | |
248 | if (index != -1) |
249 | { |
250 | cornerPos=mapFromItem(inLine,cornerPos); |
251 | myCorners.replace(index,cornerPos); |
252 | updatePosition(); |
253 | } |
254 | } |
255 | |
256 | void Arrow::setVisibleCorners(bool visible) |
257 | { |
258 | foreach (lineItem *line, myCLines) { |
259 | line->setVisible(visible); |
260 | } |
261 | } |
262 | |
263 | QDomElement Arrow::toXml(QDomDocument &document, |
264 | QList<DiagramItem*> sceneItems) const |
265 | { |
266 | QDomElement diagramArrow = document.createElement("Arrow"); |
267 | |
268 | //Set attibutes; startItem |
269 | diagramArrow.setAttribute("start-x",startOffset().x()); |
270 | diagramArrow.setAttribute("start-y",startOffset().y()); |
271 | diagramArrow.setAttribute("start-Owner", |
272 | sceneItems.indexOf(startOwner())); |
273 | diagramArrow.setAttribute("start-ID",this->startItem()->textID()); |
274 | |
275 | //Set attibutes; endItem |
276 | diagramArrow.setAttribute("end-x",endOffset().x()); |
277 | diagramArrow.setAttribute("end-y",endOffset().y()); |
278 | diagramArrow.setAttribute("end-Owner", |
279 | sceneItems.indexOf(endOwner())); |
280 | diagramArrow.setAttribute("end-ID",this->endItem()->textID()); |
281 | |
282 | diagramArrow.setAttribute("color",myColor.name()); |
283 | |
284 | //Corners list |
285 | int i=0; |
286 | if(!myCorners.isEmpty()) |
287 | { |
288 | QDomElement arrowCorners = document.createElement("arrowCorners"); |
289 | foreach(QPointF corner, myCorners) |
290 | { |
291 | QDomElement arrowCorner = document.createElement("arrowCorner"); |
292 | arrowCorner.setAttribute("x",corner.x()); |
293 | arrowCorner.setAttribute("y",corner.y()); |
294 | i++; |
295 | arrowCorners.appendChild(arrowCorner); |
296 | } |
297 | |
298 | diagramArrow.appendChild(arrowCorners); |
299 | } |
300 | return (diagramArrow); |
301 | } |
302 | |
303 | void Arrow::snapToGrid(QGraphicsSceneMouseEvent *event) |
304 | { |
305 | //TODO implement polygon drag |
306 | QPointF diffPos= event->scenePos()-previousPos; |
307 | //Update corners: |
308 | for(int i=0; i < myCorners.count();i++) |
309 | { |
310 | myCLines.at(i)->setLine(QLineF(myCLines.at(i)->line().p1()+diffPos, |
311 | myCLines.at(i)->line().p2()+diffPos)); |
312 | QPointF itemPos = myCLines.at(i)->line().p1()+QPointF(0.5,0.5); |
313 | moveCorner(itemPos,myCLines.at(i)); |
314 | } |
315 | updatePosition(); |
316 | } |
317 | |
318 | void Arrow::mousePressEvent(QGraphicsSceneMouseEvent *event) |
319 | { |
320 | //if(this->isSelected()) |
321 | { |
322 | previousPos = event->scenePos(); |
323 | moving=1; |
324 | } |
325 | QGraphicsPathItem::mousePressEvent(event); |
326 | } |
327 | |
328 | void Arrow::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) |
329 | { |
330 | if(moving && isSelected()) |
331 | { |
332 | snapToGrid(event); |
333 | previousPos = event->scenePos(); |
334 | } |
335 | moving=0; |
336 | QGraphicsPathItem::mouseReleaseEvent(event); |
337 | } |
338 | |
339 | void Arrow::mouseMoveEvent(QGraphicsSceneMouseEvent *event) |
340 | { |
341 | if(moving && isSelected()) |
342 | { |
343 | snapToGrid(event); |
344 | previousPos = event->scenePos(); |
345 | } |
346 | QGraphicsPathItem::mouseMoveEvent(event); |
347 | } |
348 |
Branches:
master