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 "diagramtextitem.h" |
| 45 | #include "diagramscene.h" |
| 46 | |
| 47 | DiagramTextItem::DiagramTextItem( |
| 48 | QGraphicsItem *parent, QGraphicsScene *scene, |
| 49 | bool editable, int styleIO, |
| 50 | unsigned char ID, QString defaultText, QPointF offset) |
| 51 | : QGraphicsTextItem(parent, scene) |
| 52 | { |
| 53 | myOwnerScene=scene; |
| 54 | myStyleIO = styleIO; |
| 55 | myID=ID; |
| 56 | editableItem=editable; |
| 57 | setPlainText(defaultText); |
| 58 | posOffset=offset; |
| 59 | |
| 60 | setFlag(QGraphicsItem::ItemSendsGeometryChanges, true); |
| 61 | setFlag(QGraphicsItem::ItemSendsScenePositionChanges, true); |
| 62 | |
| 63 | if(myStyleIO==0xFFF) |
| 64 | setFlag(QGraphicsItem::ItemIsMovable,false); |
| 65 | else |
| 66 | setFlag(QGraphicsItem::ItemIsMovable,true); |
| 67 | |
| 68 | setFlag(QGraphicsItem::ItemIsSelectable,true); |
| 69 | editorOpened=0; |
| 70 | |
| 71 | updatePosition(); |
| 72 | } |
| 73 | |
| 74 | void DiagramTextItem::updatePosition() |
| 75 | { |
| 76 | if(myStyleIO>255) |
| 77 | setPos(posOffset+QPointF(-boundingRect().width()/2, |
| 78 | -boundingRect().height()/2)); |
| 79 | else if(myStyleIO & 0b10000000) |
| 80 | setPos(posOffset+QPointF(0,-boundingRect().height()/2)); |
| 81 | else //OUT |
| 82 | setPos(posOffset+QPointF(-boundingRect().width(), |
| 83 | -boundingRect().height()/2)); |
| 84 | } |
| 85 | |
| 86 | QVariant DiagramTextItem::itemChange(GraphicsItemChange change, |
| 87 | const QVariant &value) |
| 88 | { |
| 89 | return value; |
| 90 | } |
| 91 | |
| 92 | void DiagramTextItem::focusOutEvent(QFocusEvent *event) |
| 93 | { |
| 94 | setPlainText(toPlainText().trimmed()); |
| 95 | if(toPlainText()=="") setPlainText("?"); |
| 96 | setTextInteractionFlags(Qt::NoTextInteraction); |
| 97 | |
| 98 | if(myStyleIO!=0xFFF) |
| 99 | setFlag(QGraphicsItem::ItemIsMovable,true); |
| 100 | |
| 101 | QTextCursor cursor = textCursor(); |
| 102 | cursor.clearSelection(); |
| 103 | setTextCursor(cursor); |
| 104 | QGraphicsTextItem::focusOutEvent(event); |
| 105 | editorOpened=0; |
| 106 | |
| 107 | updatePosition(); |
| 108 | } |
| 109 | |
| 110 | void DiagramTextItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) |
| 111 | { |
| 112 | if(editableItem) |
| 113 | { |
| 114 | if (textInteractionFlags() == Qt::NoTextInteraction) |
| 115 | setTextInteractionFlags(Qt::TextEditorInteraction); |
| 116 | setSelected(1); |
| 117 | setFocus(Qt::MouseFocusReason); |
| 118 | setFlag(QGraphicsItem::ItemIsMovable,false); |
| 119 | editorOpened=1; |
| 120 | } |
| 121 | QGraphicsTextItem::mouseDoubleClickEvent(event); |
| 122 | } |
| 123 | |
| 124 | void DiagramTextItem::snapToGrid(QGraphicsSceneMouseEvent *event) |
| 125 | { |
| 126 | if(myStyleIO!=0xFFF) |
| 127 | setOffset(event->scenePos()); |
| 128 | } |
| 129 | |
| 130 | void DiagramTextItem::mousePressEvent(QGraphicsSceneMouseEvent *event) |
| 131 | { |
| 132 | if(editorOpened==0) |
| 133 | { |
| 134 | printf("StyleIO:[%x]",myStyleIO); fflush(stdout); |
| 135 | snapToGrid(event); |
| 136 | QGraphicsTextItem::mousePressEvent(event); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | void DiagramTextItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) |
| 141 | { |
| 142 | if(editorOpened==0 && myStyleIO!=0xFFF) |
| 143 | { |
| 144 | snapToGrid(event); |
| 145 | QGraphicsTextItem::mouseReleaseEvent(event); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | void DiagramTextItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) |
| 150 | { |
| 151 | if(editorOpened==0 && myStyleIO!=0xFFF) |
| 152 | { |
| 153 | snapToGrid(event); |
| 154 | QGraphicsTextItem::mouseMoveEvent(event); |
| 155 | updatePosition(); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | QDomElement DiagramTextItem::toXml(QDomDocument &document) const |
| 160 | { |
| 161 | QDomElement textItem = document.createElement("TextItem"); |
| 162 | textItem.setAttribute("text",toPlainText()); |
| 163 | if(myStyleIO<256) |
| 164 | { |
| 165 | textItem.setAttribute("myStyleIO",myStyleIO); |
| 166 | textItem.setAttribute("editableItem",0); |
| 167 | } |
| 168 | else if (myStyleIO==256) |
| 169 | { |
| 170 | textItem.setAttribute("myStyleIO",0); |
| 171 | textItem.setAttribute("editableItem",0); |
| 172 | } |
| 173 | else if (myStyleIO==257) |
| 174 | { |
| 175 | textItem.setAttribute("myStyleIO",0); |
| 176 | textItem.setAttribute("editableItem",1); |
| 177 | } |
| 178 | textItem.setAttribute("posOffset-x", |
| 179 | QPointF(posOffset-QPointF(500,500)).x()); |
| 180 | textItem.setAttribute("posOffset-y", |
| 181 | -QPointF(posOffset-QPointF(500,500)).y()); |
| 182 | return (textItem); |
| 183 | } |
| 184 | |
| 185 |
Branches:
master
