Root/Software/sie_cg/diagramtextitem.cpp

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
47DiagramTextItem::DiagramTextItem(QGraphicsItem *parent, QGraphicsScene *scene,
48                 bool editable, DiagramItem *ownerItem, unsigned char styleIO,
49                 unsigned char ID, QString defaultText, QPointF offset)
50    : QGraphicsTextItem(parent, scene)
51{
52    myOwnerScene = scene;
53    myOwnerItem = ownerItem;
54    //currentOwnerItem=0;
55    myStyleIO = styleIO;
56    myID=ID;
57    editableItem=editable;
58    if(myOwnerItem==0)
59        setZValue(1000.0);
60    else
61        setZValue(myOwnerItem->zValue());
62
63    setPlainText(defaultText);
64    posOffset=offset;
65
66    if(editable && myOwnerItem==0)
67    {
68        setFlag(QGraphicsItem::ItemIsMovable,true);
69        setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
70        setFlag(QGraphicsItem::ItemSendsScenePositionChanges, true);
71    }
72
73    setFlag(QGraphicsItem::ItemIsSelectable,true);
74    onlyOneUndo=1;
75    editorOpened=0;
76    updatePosition();
77}
78
79void DiagramTextItem::updatePosition()
80{
81        QPointF myOwnerPos;
82
83
84        if(myOwnerItem!=0)
85        {
86            setZValue(myOwnerItem->zValue());
87            myOwnerPos= myOwnerItem->pos();
88        }
89        else
90        {
91            setZValue(1000.0);
92            myOwnerPos=pos();
93        }
94
95        if(myStyleIO==0)
96            setPos(myOwnerPos+posOffset+QPointF(-boundingRect().width()/2,
97                                                -boundingRect().height()/2));
98        else if(myStyleIO & 0b10000000)
99            setPos(myOwnerPos+posOffset+QPointF(0,-boundingRect().height()/2));
100        else //OUT
101            setPos(myOwnerPos+posOffset+QPointF(-boundingRect().width(),
102                                                -boundingRect().height()/2));
103}
104
105QVariant DiagramTextItem::itemChange(GraphicsItemChange change,
106                     const QVariant &value)
107{
108    if (change == QGraphicsItem::ItemPositionChange)
109        positionChanged=1;
110
111    return value;
112}
113
114void DiagramTextItem::focusOutEvent(QFocusEvent *event)
115{
116    if(editableItem)
117    {
118        if(toPlainText()=="") setPlainText("?");
119        if(myOwnerItem!=0) updatePosition();
120        //Close editor
121        setTextInteractionFlags(Qt::NoTextInteraction);
122        QTextCursor cursor = textCursor();
123        cursor.clearSelection();
124        setTextCursor(cursor);
125        QGraphicsTextItem::focusOutEvent(event);
126        //Determine undo state
127        if(myOwnerItem==0) setFlag(QGraphicsItem::ItemIsMovable,true);
128        if(toPlainText()==currentText)
129            qobject_cast<DiagramScene*>(myOwnerScene)->removeLastUndo();
130        else
131            qobject_cast<DiagramScene*>(myOwnerScene)->clearRedo();
132        onlyOneUndo=1;
133        editorOpened=0;
134        //Allow deletions when editor is closed
135        qobject_cast<DiagramScene *>(myOwnerScene)->myOwnerWindow
136                ->dontDelete =0;
137    }
138}
139
140void DiagramTextItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
141{
142    if(editableItem)
143    {
144        if(onlyOneUndo)
145        {
146            //Save current state to undo list
147            currentText = toPlainText();
148            qobject_cast<DiagramScene *>(myOwnerScene)->saveUndo();
149            //Open editor
150            if (textInteractionFlags() == Qt::NoTextInteraction)
151                setTextInteractionFlags(Qt::TextEditorInteraction);
152            setSelected(1);
153            setFocus(Qt::MouseFocusReason);
154            setFlag(QGraphicsItem::ItemIsMovable,false);
155            editorOpened=1;
156            //Prevent deletions when editor is opened
157            qobject_cast<DiagramScene *>(myOwnerScene)->myOwnerWindow
158                    ->dontDelete = 1;
159        }
160        doubleClicked=1;
161        onlyOneUndo=0;
162    }
163}
164
165void DiagramTextItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
166{
167    if (myOwnerItem!=0 && editorOpened==0)
168    {
169        setSelected(0);
170        myOwnerItem->mousePressEvent(event);
171    }
172    else
173    {
174        positionChanged=0;
175        doubleClicked=0;
176        qobject_cast<DiagramScene *>(myOwnerScene)->saveUndo();
177        QGraphicsTextItem::mousePressEvent(event);
178    }
179}
180
181void DiagramTextItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
182{
183        if (myOwnerItem!=0 && editorOpened==0)
184        {
185            myOwnerItem->mouseReleaseEvent(event);
186        }
187        else
188        {
189            if(!doubleClicked)
190            {
191                if(!positionChanged)
192                    qobject_cast<DiagramScene*>(myOwnerScene)->removeLastUndo();
193                else
194                    qobject_cast<DiagramScene*>(myOwnerScene)->clearRedo();
195            }
196            QGraphicsTextItem::mouseReleaseEvent(event);
197        }
198}
199
200void DiagramTextItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
201{
202        if (myOwnerItem!=0 && editorOpened==0)
203        {
204            myOwnerItem->mouseMoveEvent(event);
205        }
206        else
207            QGraphicsTextItem::mouseMoveEvent(event);
208}
209
210QDomElement DiagramTextItem::toXml(QDomDocument &document) const
211{
212        QDomElement textItem = document.createElement("TextItem");
213
214        //Set attibutes; Text and Font
215        textItem.setAttribute("Text",toPlainText());
216        textItem.setAttribute("Family",myFont.family());
217        textItem.setAttribute("PointSize",myFont.pointSize());
218        textItem.setAttribute("Bold",myFont.bold());
219        textItem.setAttribute("Italic",myFont.italic());
220        textItem.setAttribute("Underline",myFont.underline());
221        textItem.setAttribute("Color",defaultTextColor().name());
222        textItem.setAttribute("x",pos().x());
223        textItem.setAttribute("y",pos().y());
224
225        return (textItem);
226}
227

Archive Download this file

Branches:
master



interactive