myGraphicsRectItem.cpp 6.03 KB
Newer Older
1
2
3
4
5
6
7
8
#include "myGraphicsRectItem.h"

#include <QGraphicsSceneMouseEvent>
#include <QGraphicsRectItem>
#include <QPen>
#include <QBrush>
#include <QApplication>
#include <QVariant>
9
#include <string.h>
10

11
myGraphicsRectItem::myGraphicsRectItem(const QRectF & rect,  QGraphicsItem * parent)
12
13
14
15
    : QGraphicsRectItem(rect, parent)
{
    this->setFlag(QGraphicsItem::ItemIsSelectable);
    this->setFlag(QGraphicsItem::ItemIsMovable);
16
    this->setFlag(QGraphicsItem::ItemSendsScenePositionChanges);
17
18
19
20
21
    pen = new QPen(Qt::red);
    brush = new QBrush(Qt::red);

    tmp_rect = 0;
    _grabbers_created = false;
22
    resize_mode = false;
23
24
}

25

26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
void myGraphicsRectItem::deleteGrabbers()
{
    _bottomLeft_corner->setParentItem(NULL);
    _topLeft_corner->setParentItem(NULL);
    _topRight_corner->setParentItem(NULL);
    _bottomRight_corner->setParentItem(NULL);

    delete _bottomLeft_corner;
    delete _topLeft_corner;
    delete _topRight_corner;
    delete _bottomRight_corner;
    _grabbers_created = false;
}

QVariant myGraphicsRectItem::itemChange(GraphicsItemChange change, const QVariant &value)
{
    if (change == QGraphicsItem::ItemSelectedChange)
    {
        if (value == true)
        {
            qDebug("selected!");
            createGrabbers();
        }
        else
        {
            qDebug("unselected!");
            deleteGrabbers();
        }
    }
55
56
57
58
59
60
61
62
63
64
65
66
67
68
    if (change == QGraphicsItem::ItemPositionChange && scene()) // with this, block movement of object when we are hovering through cornergrabbers
    {
        if(checkCornerGrabbers() == CornerGrabber::noCorner)
        {
            qDebug("move now!");
            return QPointF(value.toPointF().x(), value.toPointF().y());
        }
        else
        {
            qDebug("dont move now!");
            return QPointF(pos().x(), pos().y());
        }
    }

69
70
71
72
73
74
75
76
77

    return QGraphicsItem::itemChange(change, value);
}

bool myGraphicsRectItem::grabbersAreCreated()
{
    return _grabbers_created;
}

78
void myGraphicsRectItem::setCornerPositions() //need to call this function whenever we chnge the size of the rectangle
79
80
{
    QRectF rect = this->rect();
81
    _bottomLeft_corner->setPos(rect.bottomLeft().x(), rect.bottomLeft().y());
82
    _topLeft_corner->setPos(rect.topLeft().x(), rect.topLeft().y());
83
84
    _topRight_corner->setPos(rect.topRight().x(), rect.topRight().y());
    _bottomRight_corner->setPos(rect.bottomRight().x(), rect.bottomRight().y());
85
86
87
88
89
90
91
92
93
}

void myGraphicsRectItem::createGrabbers()
{
    if(!grabbersAreCreated())
    {
        _bottomLeft_corner = new CornerGrabber(this, CornerGrabber::bottomLeft);
        _topLeft_corner = new CornerGrabber(this, CornerGrabber::topLeft);
        _topRight_corner = new CornerGrabber(this, CornerGrabber::topRight);
94
        _bottomRight_corner = new CornerGrabber(this, CornerGrabber::bottomRight);
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119


        _bottomLeft_corner->installSceneEventFilter(this);
        _topLeft_corner->installSceneEventFilter(this);
        _topRight_corner->installSceneEventFilter(this);
        _bottomRight_corner->installSceneEventFilter(this);

        _grabbers_created = true;
        setCornerPositions();
    }
}

int myGraphicsRectItem::checkCornerGrabbers()
{

    // we return the first active found. Hopefully we never have two at the same time.
    if(_bottomLeft_corner->isActive())
        return CornerGrabber::bottomLeft;
    else if(_topLeft_corner->isActive())
        return CornerGrabber::topLeft;
    else if (_topRight_corner->isActive())
        return CornerGrabber::topRight;
    else if( _bottomRight_corner->isActive())
        return CornerGrabber::bottomRight;
    else
120
121
122
123
124
125
126
127
128
        return CornerGrabber::noCorner;               //0 is none
}

bool myGraphicsRectItem::anyGrabber()
{
    if(checkCornerGrabbers() != CornerGrabber::noCorner)
        return true;
    else
        return false;
129
130
131
132
133
134
135
}

void myGraphicsRectItem::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
    if (mouseEvent->button() != Qt::LeftButton)
        return;

136
    createGrabbers();           //This is just in case they have not been created by now. We have a creator guardian anyhow
137

138
    if(anyGrabber())
139
    {
140
        resize_mode = true;
141
    }
142
    QGraphicsRectItem::mousePressEvent(mouseEvent);
143
144
145
146
147
}

void myGraphicsRectItem::mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
    this->prepareGeometryChange();
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
    if(resize_mode)
    {
        switch(checkCornerGrabbers())
        {
            case CornerGrabber::bottomLeft:
            {
                qDebug("bottomLeft");
                QRectF resize_rect = this->rect();
                resize_rect.setBottomLeft(mouseEvent->pos());
                this->setRect(resize_rect.normalized());
                break;
            }
            case CornerGrabber::topLeft:
            {
                qDebug("topLeft");
                QRectF resize_rect = this->rect();
                resize_rect.setTopLeft(mouseEvent->pos());
                this->setRect(resize_rect.normalized());
                break;
            }
            case CornerGrabber::topRight:
            {
                qDebug("topRight");
                QRectF resize_rect = this->rect();
                resize_rect.setTopRight(mouseEvent->pos());
                this->setRect(resize_rect.normalized());
                break;
            }
            case CornerGrabber::bottomRight:
            {
                qDebug("bottomRight");
                QRectF resize_rect = this->rect();
                resize_rect.setBottomRight(mouseEvent->pos());
                this->setRect(resize_rect.normalized());
                break;
            }
            case CornerGrabber::noCorner:
                qDebug("No corner. Should never enter here");
            default:
                break;
        }
189
190
        setCornerPositions();
        rectSizeChanged();
191
    }
192
193
194
195
196
197
198
199
200
    QGraphicsRectItem::mouseMoveEvent(mouseEvent);
}

void myGraphicsRectItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent)
{

    if (mouseEvent->button() != Qt::LeftButton)
        return;
    // TODO: stop resize mode
201
202
203
204
    if(resize_mode)
    {
        resize_mode = false;
    }
205
206
    QGraphicsRectItem::mouseReleaseEvent(mouseEvent);
}