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 "lineitem.h" |
46 | #include <math.h> |
47 | |
48 | const qreal Pi = 3.14; |
49 | |
50 | Arrow::Arrow( QPointF startP,QPointF endP, |
51 | QGraphicsItem *parent, QGraphicsScene *scene) |
52 | : QGraphicsPathItem(parent, scene) |
53 | { |
54 | startPoint=startP; |
55 | endPoint=endP; |
56 | arrowSize = 10; |
57 | moving=0; |
58 | //setFlag(QGraphicsItem::ItemIsMovable, true); |
59 | setFlag(QGraphicsItem::ItemIsSelectable, true); |
60 | myColor = Qt::black; |
61 | setPen(QPen(myColor, 2, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin)); |
62 | //setZValue(0.0); |
63 | myOwnerScene = scene; |
64 | lineItem *newLine= new lineItem(QPointF(0,0),QPointF(0,0),this); |
65 | myOwnerScene->addItem(newLine); |
66 | addLine(newLine); |
67 | |
68 | updatePosition(); |
69 | } |
70 | |
71 | void Arrow::removeLine(lineItem *line) |
72 | { |
73 | int index = myLines.indexOf(line); |
74 | |
75 | if (index != -1){ |
76 | //Adjust the corners |
77 | if(myLines.count()>1) |
78 | { |
79 | if(index==0) |
80 | { |
81 | myCorners.removeAt(0); |
82 | myOwnerScene->removeItem(myCLines.at(0)); |
83 | myCLines.removeAt(0); |
84 | } |
85 | else if(index>0) |
86 | { |
87 | myCorners.removeAt(index-1); |
88 | myOwnerScene->removeItem(myCLines.at(index-1)); |
89 | myCLines.removeAt(index-1); |
90 | } |
91 | } |
92 | myLines.removeAt(index); |
93 | } |
94 | |
95 | //If lines is empty the arrow is removed |
96 | if (myLines.count()<3) |
97 | { |
98 | removeLines(); |
99 | myOwnerScene->removeItem(this); |
100 | } |
101 | } |
102 | |
103 | void Arrow::removeLines() |
104 | { |
105 | foreach (lineItem *line, myLines) { |
106 | scene()->removeItem(line); |
107 | delete line; |
108 | } |
109 | foreach (lineItem *line, myCLines) { |
110 | scene()->removeItem(line); |
111 | delete line; |
112 | } |
113 | if(myOwnerScene->items().indexOf(SECLine)!=-1) |
114 | { |
115 | scene()->removeItem(SECLine); |
116 | delete SECLine; |
117 | } |
118 | } |
119 | |
120 | bool Arrow::addLine(lineItem *line) |
121 | { |
122 | myLines.append(line); |
123 | return 1; |
124 | } |
125 | |
126 | QRectF Arrow::boundingRect() const |
127 | { |
128 | qreal extra = (pen().width() + arrowSize*2) / 2.0; |
129 | |
130 | return QGraphicsPathItem::boundingRect() |
131 | .normalized() |
132 | .adjusted(-extra, -extra, extra, extra); |
133 | } |
134 | |
135 | QPainterPath Arrow::shape() const |
136 | { |
137 | QPainterPath path = QGraphicsPathItem::shape(); |
138 | return path; |
139 | } |
140 | |
141 | void Arrow::updatePosition() |
142 | { |
143 | QPainterPath myPath; |
144 | myPath.moveTo(startPoint); |
145 | if(!myCorners.isEmpty()) |
146 | foreach(QPointF corner, myCorners){ |
147 | myPath.lineTo(corner); |
148 | } |
149 | myPath.lineTo(endPoint); |
150 | |
151 | setPath(myPath); |
152 | } |
153 | |
154 | void Arrow::paint(QPainter *painter, const QStyleOptionGraphicsItem *, |
155 | QWidget *) |
156 | { |
157 | //Update line positions |
158 | QPen myPen = pen(); |
159 | myPen.setColor(myColor); |
160 | painter->setPen(myPen); |
161 | painter->setBrush(myColor); |
162 | |
163 | QPointF endPos=endPoint; |
164 | QPointF startPos=startPoint; |
165 | |
166 | if(myCorners.isEmpty()) |
167 | { |
168 | myLines.first()->setLine(QLineF(startPos,endPos)); |
169 | } |
170 | else |
171 | { |
172 | myLines.first()->setLine(QLineF(startPos,myCorners.first())); |
173 | for(int i=0; i<myCorners.count()-1;i++) |
174 | myLines.at(i+1)->setLine(QLineF(myCorners.at(i),myCorners.at(i+1))); |
175 | myLines.last()->setLine(QLineF(myCorners.last(),endPos)); |
176 | } |
177 | |
178 | //Drawing arrow selected |
179 | for(int i=0; i<myLines.count();i++) |
180 | { |
181 | if (isSelected()) |
182 | { |
183 | painter->setPen(QPen(Qt::gray, 2, Qt::SolidLine)); |
184 | QLineF myLine = myLines.at(i)->line(); |
185 | painter->drawLine(myLine); |
186 | foreach (lineItem *line, myLines) { line->setColor(Qt::gray);} |
187 | foreach (lineItem *line, myCLines) { line->setColor(Qt::gray);} |
188 | if(myOwnerScene->items().indexOf(SECLine)!=-1) |
189 | SECLine->setColor(Qt::gray); |
190 | } |
191 | else |
192 | { |
193 | foreach (lineItem *line, myLines) { line->setColor(myColor);} |
194 | foreach (lineItem *line, myCLines) { line->setColor(myColor);} |
195 | if(myOwnerScene->items().indexOf(SECLine)!=-1) |
196 | SECLine->setColor(myColor); |
197 | } |
198 | } |
199 | setSelectedArrows(); |
200 | } |
201 | |
202 | void Arrow::setSelectedArrows() |
203 | { |
204 | bool anySelected = 0; |
205 | |
206 | foreach(lineItem *line,myLines){ if(line->isSelected())anySelected=1;} |
207 | foreach(lineItem *line,myCLines){ if(line->isSelected())anySelected=1;} |
208 | if(isSelected()) anySelected=1; |
209 | |
210 | if(anySelected) |
211 | setVisibleCorners(1); |
212 | else |
213 | setVisibleCorners(0); |
214 | } |
215 | |
216 | void Arrow::createCorner(QPointF cornerPos, lineItem *inLine) |
217 | { |
218 | createCorner(cornerPos,myLines.indexOf(inLine)); |
219 | } |
220 | |
221 | void Arrow::createCorner(QPointF cornerPos, int index) |
222 | { |
223 | if (index != -1) |
224 | { |
225 | myCorners.insert(index,cornerPos); |
226 | lineItem *newLine= new lineItem(cornerPos,cornerPos,this); |
227 | myOwnerScene->addItem(newLine); |
228 | newLine->setZValue(this->zValue()); |
229 | addLine(newLine); |
230 | newLine= new lineItem(cornerPos+QPointF(-0.5,-0.5) , |
231 | cornerPos+QPointF(0.5,0.5),this,6,1); |
232 | myOwnerScene->addItem(newLine); |
233 | newLine->setZValue(this->zValue()+0.1); |
234 | myCLines.insert(index,newLine); |
235 | } |
236 | updatePosition(); |
237 | } |
238 | void Arrow::createFirstCorner() |
239 | { |
240 | if(myOwnerScene->items().indexOf(SECLine)==-1) |
241 | { |
242 | SECLine = new lineItem(startPoint+QPointF(-0.5,-0.5) , |
243 | startPoint+QPointF(0.5,0.5),this,6,1); |
244 | myOwnerScene->addItem(SECLine); |
245 | SECLine->setVisible(1); |
246 | SECLine->setZValue(this->zValue()+0.1); |
247 | } |
248 | updatePosition(); |
249 | } |
250 | |
251 | void Arrow::moveCorner(QPointF cornerPos, lineItem *inLine, bool relative) |
252 | { |
253 | int index = myCLines.indexOf(inLine); |
254 | if(relative) cornerPos=mapFromItem(inLine,cornerPos); |
255 | |
256 | if (index != -1) |
257 | { |
258 | myCorners.replace(index,cornerPos); |
259 | } |
260 | else if (inLine == SECLine) |
261 | { |
262 | startPoint=cornerPos; |
263 | endPoint=cornerPos; |
264 | } |
265 | updatePosition(); |
266 | } |
267 | |
268 | void Arrow::setVisibleCorners(bool visible) |
269 | { |
270 | foreach (lineItem *line, myCLines) { |
271 | line->setVisible(visible); |
272 | } |
273 | if(myOwnerScene->items().indexOf(SECLine)!=-1) |
274 | { |
275 | SECLine->setVisible(1); |
276 | } |
277 | updatePosition(); |
278 | } |
279 | |
280 | void Arrow::snapToGrid(QGraphicsSceneMouseEvent *event) |
281 | { |
282 | //TODO implement polygon drag |
283 | QPointF diffPos= event->scenePos()-previousPos; |
284 | printf("diff[%f|%f]\n",diffPos.x(),diffPos.y()); fflush(stdout); |
285 | //Update corners: |
286 | for(int i=0; i < myCorners.count();i++) |
287 | { |
288 | myCLines.at(i)->setLine(QLineF(myCLines.at(i)->line().p1()+diffPos, |
289 | myCLines.at(i)->line().p2()+diffPos)); |
290 | QPointF itemPos = myCLines.at(i)->line().p1()+QPointF(0.5,0.5); |
291 | moveCorner(itemPos,myCLines.at(i)); |
292 | } |
293 | if(myOwnerScene->items().indexOf(SECLine)!=-1) |
294 | { |
295 | SECLine->setLine(QLineF(SECLine->line().p1()+diffPos, |
296 | SECLine->line().p2()+diffPos)); |
297 | QPointF itemPos = SECLine->line().p1()+QPointF(0.5,0.5); |
298 | moveCorner(itemPos,SECLine); |
299 | } |
300 | updatePosition(); |
301 | } |
302 | |
303 | void Arrow::mousePressEvent(QGraphicsSceneMouseEvent *event) |
304 | { |
305 | //if(this->isSelected()) |
306 | { |
307 | previousPos = event->scenePos(); |
308 | moving=1; |
309 | } |
310 | QGraphicsPathItem::mousePressEvent(event); |
311 | } |
312 | |
313 | void Arrow::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) |
314 | { |
315 | if(moving && isSelected()) |
316 | { |
317 | snapToGrid(event); |
318 | previousPos = event->scenePos(); |
319 | } |
320 | moving=0; |
321 | QGraphicsPathItem::mouseReleaseEvent(event); |
322 | } |
323 | |
324 | void Arrow::mouseMoveEvent(QGraphicsSceneMouseEvent *event) |
325 | { |
326 | if(moving && isSelected()) |
327 | { |
328 | snapToGrid(event); |
329 | previousPos = event->scenePos(); |
330 | } |
331 | QGraphicsPathItem::mouseMoveEvent(event); |
332 | } |
333 | |
334 | QDomElement Arrow::toXml(QDomDocument &document) const |
335 | { |
336 | QDomElement diagramArrow = document.createElement("Polygon"); |
337 | //Point list |
338 | if(!myCorners.isEmpty()) |
339 | { |
340 | QDomElement arrowCorner; |
341 | //start point |
342 | arrowCorner = document.createElement("Point"); |
343 | arrowCorner.setAttribute("x",(startPoint-QPointF(500,500)).x()); |
344 | arrowCorner.setAttribute("y",(startPoint-QPointF(500,500)).y()); |
345 | diagramArrow.appendChild(arrowCorner); |
346 | //corner points |
347 | foreach(QPointF corner, myCorners) |
348 | { |
349 | arrowCorner = document.createElement("Point"); |
350 | arrowCorner.setAttribute("x",(corner-QPointF(500,500)).x()); |
351 | arrowCorner.setAttribute("y",(corner-QPointF(500,500)).y()); |
352 | diagramArrow.appendChild(arrowCorner); |
353 | } |
354 | } |
355 | return (diagramArrow); |
356 | } |
357 |
Branches:
master