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 "lineitem.h" |
| 45 | #include "diagramscene.h" |
| 46 | #include <math.h> |
| 47 | |
| 48 | lineItem::lineItem(QPointF startPoint, QPointF endItem, Arrow *ownerArrow, |
| 49 | int wt, bool movable, QGraphicsItem *parent, QGraphicsScene *scene) |
| 50 | : QGraphicsLineItem(parent, scene) |
| 51 | { |
| 52 | isMovable=movable; |
| 53 | if(isMovable) |
| 54 | setFlag(QGraphicsItem::ItemIsMovable, true); |
| 55 | |
| 56 | setFlag(QGraphicsItem::ItemIsSelectable, true); |
| 57 | |
| 58 | myColor = Qt::black; |
| 59 | setPen(QPen(myColor, 2, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin)); |
| 60 | setLine(QLineF(startPoint,endItem)); |
| 61 | myOwnerArrow = ownerArrow; |
| 62 | pWidth=wt; |
| 63 | moveItem=0; |
| 64 | } |
| 65 | |
| 66 | QRectF lineItem::boundingRect() const |
| 67 | { |
| 68 | qreal extra = (pen().width() + 20) / 2.0; |
| 69 | |
| 70 | return QRectF(line().p1(), QSizeF(line().p2().x() - line().p1().x(), |
| 71 | line().p2().y() - line().p1().y())) |
| 72 | .normalized() |
| 73 | .adjusted(-extra, -extra, extra, extra); |
| 74 | } |
| 75 | |
| 76 | QPainterPath lineItem::shape() const |
| 77 | { |
| 78 | QPainterPath path = QGraphicsLineItem::shape(); |
| 79 | return path; |
| 80 | } |
| 81 | |
| 82 | void lineItem::updatePos() |
| 83 | { |
| 84 | QPointF itemPos = this->line().p1(); |
| 85 | myOwnerArrow->moveCorner(itemPos,this); |
| 86 | } |
| 87 | |
| 88 | void lineItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *, |
| 89 | QWidget *) |
| 90 | { |
| 91 | QPen myPen = pen(); |
| 92 | myPen.setColor(myColor); |
| 93 | myPen.setWidth(pWidth); |
| 94 | painter->setPen(myPen); |
| 95 | painter->setBrush(myColor); |
| 96 | |
| 97 | setLine(line()); |
| 98 | painter->drawLine(line()); |
| 99 | |
| 100 | //Drawing arrow selected |
| 101 | if (isSelected()) { |
| 102 | painter->setPen(QPen(Qt::gray, pWidth, Qt::SolidLine)); |
| 103 | QLineF myLine = line(); |
| 104 | painter->drawLine(myLine); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | QVariant lineItem::itemChange(GraphicsItemChange change, |
| 109 | const QVariant &value) |
| 110 | { |
| 111 | if (change == QGraphicsItem::ItemPositionChange) |
| 112 | positionChanged=1; |
| 113 | |
| 114 | return value; |
| 115 | } |
| 116 | |
| 117 | void lineItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *mouseEvent) |
| 118 | { |
| 119 | if(!isMovable) |
| 120 | { |
| 121 | qobject_cast<DiagramScene *>(myOwnerArrow->myOwnerScene) |
| 122 | ->saveUndoState(); |
| 123 | myOwnerArrow->createCorner(mouseEvent->pos(),this); |
| 124 | } |
| 125 | QGraphicsLineItem::mouseDoubleClickEvent(mouseEvent); |
| 126 | } |
| 127 | |
| 128 | |
| 129 | void lineItem::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent) |
| 130 | { |
| 131 | positionChanged=0; |
| 132 | qobject_cast<DiagramScene *>(myOwnerArrow->myOwnerScene)->saveUndo(); |
| 133 | myOwner()->setSelectedLines(false); |
| 134 | setSelected(true); |
| 135 | QGraphicsLineItem::mousePressEvent(mouseEvent); |
| 136 | } |
| 137 | |
| 138 | void lineItem::mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent) |
| 139 | { |
| 140 | if(isMovable) |
| 141 | { |
| 142 | updatePos(); |
| 143 | } |
| 144 | QGraphicsLineItem::mouseMoveEvent(mouseEvent); |
| 145 | } |
| 146 | |
| 147 | void lineItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent) |
| 148 | { |
| 149 | if(isMovable) |
| 150 | { |
| 151 | updatePos(); |
| 152 | } |
| 153 | if(!positionChanged) |
| 154 | qobject_cast<DiagramScene *>(myOwnerArrow->myOwnerScene) |
| 155 | ->removeLastUndo(); |
| 156 | else |
| 157 | qobject_cast<DiagramScene *>(myOwnerArrow->myOwnerScene) |
| 158 | ->clearRedo(); |
| 159 | QGraphicsLineItem::mouseReleaseEvent(mouseEvent); |
| 160 | } |
| 161 |
Branches:
master
