Hardware Design: SIE
Sign in or create your account | Project List | Help
Hardware Design: SIE Git Source Tree
Root/
| 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 "diagramitem.h" |
| 45 | #include "diagramscene.h" |
| 46 | #include "arrow.h" |
| 47 | #include "diagramtextitem.h" |
| 48 | #include "lineitem.h" |
| 49 | |
| 50 | DiagramItem::DiagramItem( |
| 51 | QMenu *contextMenu, |
| 52 | QString diagramType, |
| 53 | QDomElement *domElement, |
| 54 | QGraphicsItem *parent, QGraphicsScene *scene, |
| 55 | QPointF position, double zV) |
| 56 | : QGraphicsPolygonItem(parent, scene) |
| 57 | { |
| 58 | myDiagramType = diagramType; |
| 59 | myContextMenu = contextMenu; |
| 60 | myOwnerScene = scene; |
| 61 | myDomElement=domElement; |
| 62 | setPos(position); |
| 63 | setZValue(zV); |
| 64 | myColor = Qt::white; |
| 65 | |
| 66 | for (QDomNode node = myDomElement->firstChild() ; |
| 67 | !node.isNull() ; |
| 68 | node = node.nextSibling()) |
| 69 | { |
| 70 | QDomElement element = node.toElement(); |
| 71 | if(element.tagName()=="Polygon") |
| 72 | { |
| 73 | //READING POLYGON POINTS |
| 74 | QList<QPointF> points; |
| 75 | for (QDomNode node = element.firstChild() ; |
| 76 | !node.isNull() ; |
| 77 | node = node.nextSibling()) |
| 78 | { |
| 79 | QDomElement point = node.toElement(); |
| 80 | if(point.tagName()=="Point") |
| 81 | points.append(QPointF( |
| 82 | point.attribute("x").toFloat(), |
| 83 | point.attribute("y").toFloat())); |
| 84 | } |
| 85 | //CREATING POLYGON |
| 86 | if(points.count()>0) |
| 87 | foreach(QPointF p, points) |
| 88 | myPolygon << p; |
| 89 | |
| 90 | } |
| 91 | else if(element.tagName()=="TextItems" && myOwnerScene!=0) |
| 92 | { |
| 93 | for (QDomNode node = element.firstChild() ; |
| 94 | !node.isNull() ; |
| 95 | node = node.nextSibling()) |
| 96 | { |
| 97 | QDomElement textItemE = node.toElement(); |
| 98 | if(textItemE.tagName()=="TextItem") |
| 99 | { |
| 100 | int myStyleIO = textItemE.attribute("myStyleIO"). |
| 101 | toInt(); |
| 102 | int myID = textItemE.attribute("ID").toInt(); |
| 103 | bool editableItem = textItemE.attribute("editableItem"). |
| 104 | toInt(); |
| 105 | QPointF posOffset= |
| 106 | QPointF(textItemE.attribute("posOffset-x"). |
| 107 | toFloat(), |
| 108 | -textItemE.attribute("posOffset-y"). |
| 109 | toFloat()); |
| 110 | QString itemString=textItemE.attribute("text"); |
| 111 | |
| 112 | DiagramTextItem * newTextItem = |
| 113 | new DiagramTextItem(0,myOwnerScene,editableItem,this,myStyleIO, |
| 114 | myID,itemString,posOffset); |
| 115 | myOwnerScene->addItem(newTextItem); |
| 116 | newTextItem->setZValue(zValue()); |
| 117 | addText(newTextItem); |
| 118 | } |
| 119 | |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | setPolygon(myPolygon); |
| 125 | setFlag(QGraphicsItem::ItemIsMovable, true); |
| 126 | setFlag(QGraphicsItem::ItemIsSelectable, true); |
| 127 | setFlag(QGraphicsItem::ItemSendsGeometryChanges, true); |
| 128 | setFlag(QGraphicsItem::ItemSendsScenePositionChanges, true); |
| 129 | } |
| 130 | |
| 131 | bool DiagramItem::setValue(unsigned char ID, QString value) |
| 132 | { |
| 133 | foreach(DiagramTextItem *item, textItems) |
| 134 | { |
| 135 | if(item->isEditable()) |
| 136 | { |
| 137 | if(item->textID()==ID) |
| 138 | item->setPlainText(value); |
| 139 | } |
| 140 | } |
| 141 | return 1; |
| 142 | } |
| 143 | |
| 144 | DiagramTextItem *DiagramItem::pointerText(unsigned char ID) |
| 145 | { |
| 146 | foreach(DiagramTextItem *item, textItems) |
| 147 | { |
| 148 | if(item->textID()==ID) |
| 149 | return item; |
| 150 | } |
| 151 | return 0; |
| 152 | } |
| 153 | |
| 154 | bool DiagramItem::textIsIO(unsigned char ID) |
| 155 | { |
| 156 | foreach(DiagramTextItem *item, textItems) |
| 157 | { |
| 158 | if(item->textID()==ID) |
| 159 | if(item->styleIO()!=0) |
| 160 | return 1; |
| 161 | } |
| 162 | return 0; |
| 163 | } |
| 164 | |
| 165 | bool DiagramItem::existText(DiagramTextItem * text) |
| 166 | { |
| 167 | foreach(DiagramTextItem *item, textItems) |
| 168 | { |
| 169 | if(item==text) |
| 170 | return 1; |
| 171 | } |
| 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | unsigned char DiagramItem::existArrow(DiagramTextItem *startItem, |
| 176 | DiagramTextItem *endItem) |
| 177 | { |
| 178 | foreach (Arrow *arrow, arrows) { |
| 179 | if(arrow->startItem() == startItem && |
| 180 | arrow->endItem() == endItem) |
| 181 | return 1; //Already exist |
| 182 | else if(arrow->endItem()== endItem) |
| 183 | return 1; //End item (INPOUT) already connected |
| 184 | } |
| 185 | return 0; |
| 186 | } |
| 187 | |
| 188 | void DiagramItem::removeArrow(Arrow *arrow) |
| 189 | { |
| 190 | int index = arrows.indexOf(arrow); |
| 191 | |
| 192 | if (index != -1) |
| 193 | arrows.removeAt(index); |
| 194 | } |
| 195 | |
| 196 | void DiagramItem::removeArrows() |
| 197 | { |
| 198 | foreach (Arrow *arrow, arrows) { |
| 199 | arrow->startItem()->ownerItem()->removeArrow(arrow); |
| 200 | arrow->endItem()->ownerItem()->removeArrow(arrow); |
| 201 | arrow->removeLines(); |
| 202 | scene()->removeItem(arrow); |
| 203 | delete arrow; |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | void DiagramItem::addArrow(Arrow *arrow) |
| 208 | { |
| 209 | arrows.append(arrow); |
| 210 | } |
| 211 | |
| 212 | void DiagramItem::removeTextItem(DiagramTextItem *textItem) |
| 213 | { |
| 214 | int index = textItems.indexOf(textItem); |
| 215 | |
| 216 | if (index != -1) |
| 217 | textItems.removeAt(index); |
| 218 | } |
| 219 | |
| 220 | void DiagramItem::removeTextItems() |
| 221 | { |
| 222 | foreach (DiagramTextItem *textItem, textItems) { |
| 223 | textItem->ownerItem()->removeTextItem(textItem); |
| 224 | scene()->removeItem(textItem); |
| 225 | delete textItem; |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | QPixmap DiagramItem::image() const |
| 230 | { |
| 231 | QSize mySize=this->boundingRect().size().toSize()*1.4; |
| 232 | QPixmap pixmap(mySize); |
| 233 | pixmap.fill(Qt::transparent); |
| 234 | QPainter painter(&pixmap); |
| 235 | int penWidth; |
| 236 | if(mySize.width()>mySize.height()) |
| 237 | penWidth = int(mySize.width()/32); |
| 238 | else |
| 239 | penWidth = int(mySize.height()/32); |
| 240 | |
| 241 | painter.setPen(QPen(Qt::black, penWidth)); |
| 242 | painter.translate(mySize.width()/2, mySize.height()/2); |
| 243 | QPolygonF Polygon = myPolygon; |
| 244 | Polygon = Polygon << Polygon.first(); //Adjust the last segment |
| 245 | painter.drawPolyline(Polygon); |
| 246 | //TODO text on icon may be interesting |
| 247 | return pixmap; |
| 248 | } |
| 249 | |
| 250 | void DiagramItem::contextMenuEvent(QGraphicsSceneContextMenuEvent *event) |
| 251 | { |
| 252 | scene()->clearSelection(); |
| 253 | setSelected(true); |
| 254 | myContextMenu->exec(event->screenPos()); |
| 255 | } |
| 256 | |
| 257 | QVariant DiagramItem::itemChange(GraphicsItemChange change, |
| 258 | const QVariant &value) |
| 259 | { |
| 260 | if (change == QGraphicsItem::ItemPositionChange) { |
| 261 | positionChanged=1; |
| 262 | } |
| 263 | return value; |
| 264 | } |
| 265 | |
| 266 | void DiagramItem::updateTexts() |
| 267 | { |
| 268 | foreach (DiagramTextItem *texts, textItems) { |
| 269 | texts->updatePosition(); |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | void DiagramItem::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent) |
| 274 | { |
| 275 | positionChanged=0; |
| 276 | qobject_cast<DiagramScene *>(myOwnerScene)->saveUndo(); |
| 277 | QGraphicsPolygonItem::mousePressEvent(mouseEvent); |
| 278 | } |
| 279 | |
| 280 | void DiagramItem::mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent) |
| 281 | { |
| 282 | updateTexts(); |
| 283 | foreach (Arrow *arrow, arrows) { |
| 284 | arrow->updatePosition(); |
| 285 | } |
| 286 | QGraphicsPolygonItem::mouseMoveEvent(mouseEvent); |
| 287 | } |
| 288 | |
| 289 | void DiagramItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent) |
| 290 | { |
| 291 | foreach (DiagramTextItem *texts, textItems) { |
| 292 | texts->updatePosition(); |
| 293 | } |
| 294 | foreach (Arrow *arrow, arrows) { |
| 295 | arrow->updatePosition(); |
| 296 | } |
| 297 | |
| 298 | if(!positionChanged) |
| 299 | qobject_cast<DiagramScene *>(myOwnerScene)->removeLastUndo(); |
| 300 | else |
| 301 | qobject_cast<DiagramScene *>(myOwnerScene)->clearRedo(); |
| 302 | |
| 303 | QGraphicsPolygonItem::mouseReleaseEvent(mouseEvent); |
| 304 | } |
| 305 | |
| 306 | QDomElement DiagramItem::toXml(QDomDocument &document) const |
| 307 | { |
| 308 | QDomElement diagramItem = document.createElement("DiagramItem"); |
| 309 | |
| 310 | //Set attibutes; type and position |
| 311 | diagramItem.setAttribute("type",diagramType()); |
| 312 | diagramItem.setAttribute("x",pos().x()); |
| 313 | diagramItem.setAttribute("y",pos().y()); |
| 314 | diagramItem.setAttribute("z",zValue()); |
| 315 | diagramItem.setAttribute("color",myColor.name()); |
| 316 | |
| 317 | //Lists of values (text on editable labels) |
| 318 | int i=0; |
| 319 | QDomElement diagramValues = document.createElement("diagramValues"); |
| 320 | foreach(DiagramTextItem *item, textItems) |
| 321 | { |
| 322 | if(item->isEditable()) |
| 323 | { |
| 324 | QDomElement diagramValue = document.createElement("diagramValue"); |
| 325 | diagramValue.setAttribute("value", item->toPlainText()); |
| 326 | diagramValue.setAttribute("ID", item->textID()); |
| 327 | i++; |
| 328 | diagramValues.appendChild(diagramValue); |
| 329 | } |
| 330 | } |
| 331 | diagramItem.appendChild(diagramValues); |
| 332 | |
| 333 | return (diagramItem); |
| 334 | } |
| 335 | |
| 336 |
Branches:
master
