cornergrabber.cpp 3.37 KB
Newer Older
1
2
3
4
5
6
#include "cornergrabber.h"

CornerGrabber::CornerGrabber(QGraphicsItem *parent,  int corner) :
    QGraphicsItem(parent),
    _outterborderColor(Qt::black),
    _outterborderPen(),
7
8
    _width(GRABBER_WIDTH),
    _height(GRABBER_HEIGHT),
9
10
11
12
13
14
    _corner(corner),
    _is_active(false)
{
    setParentItem(parent);
    _outterborderPen.setWidth(2);
    _outterborderPen.setColor(_outterborderColor);
15
16
    this->setAcceptHoverEvents(true);
    this->setFlag(QGraphicsItem::ItemIgnoresTransformations);
17
18
}

19

20
21
22
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
qreal CornerGrabber::getHeight()
{
    return _height;
}

qreal CornerGrabber::getWidth()
{
    return _width;
}

int CornerGrabber::getCorner()
{
    return _corner;
}

bool CornerGrabber::isActive()
{
    return _is_active;
}


// we have to implement the mouse events to keep the linker happy,
// but just set accepted to false since are not actually handling them

void CornerGrabber::mouseMoveEvent(QGraphicsSceneDragDropEvent *event)
{
    event->setAccepted(false);
}

void CornerGrabber::mousePressEvent(QGraphicsSceneDragDropEvent *event)
{
    event->setAccepted(false);
}

void CornerGrabber::mouseReleaseEvent ( QGraphicsSceneMouseEvent * event )
{
    event->setAccepted(false);
}

void CornerGrabber::mousePressEvent ( QGraphicsSceneMouseEvent * event )
{
    event->setAccepted(false);
}

void CornerGrabber::mouseMoveEvent ( QGraphicsSceneMouseEvent * event )
{
    event->setAccepted(false);
}


// change the color on hover events to indicate to the use the object has
// been captured by the mouse

void CornerGrabber::hoverLeaveEvent ( QGraphicsSceneHoverEvent * )
{
    _outterborderColor = Qt::black;
    _is_active = false;
    this->update(0,0,_width,_height);
}

void CornerGrabber::hoverEnterEvent ( QGraphicsSceneHoverEvent * )
{
    _outterborderColor = Qt::red;
    _is_active = true;
    this->update(0,0,_width,_height);
}

QRectF CornerGrabber::boundingRect() const
{
89
90
    QRectF bounding_rect = this->rect();
    return bounding_rect;
91
92
}

93
94
95
96
97
98
99
100
101
QRectF CornerGrabber::rect() const
{
    return _rect;
}

void CornerGrabber::setRect(const QRectF & rectangle)
{
    _rect = rectangle;
}
102
103
104
105
106

void CornerGrabber::paint (QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{

    // fill the box with solid color, use sharp corners
107
    prepareGeometryChange();
108
109
110
    _outterborderPen.setCapStyle(Qt::SquareCap);
    _outterborderPen.setStyle(Qt::SolidLine);
    painter->setPen(_outterborderPen);
111
112
    QRectF rect = createRect();
    this->setRect(rect);
113
114
    QBrush brush (Qt::SolidPattern);
    brush.setColor (_outterborderColor);
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
    painter->fillRect(_rect,brush);

}

QRectF CornerGrabber::createRect()
{
    QPointF topLeft(0,0);
    QPointF bottomRight(_width, _height);
    QRectF rect(topLeft, bottomRight);

    switch(_corner)
    {
        case CornerGrabber::bottomLeft:
        {
            QPointF move_to(0,0);
            rect.moveBottomLeft(move_to);
            return rect;
            break;
        }
        case CornerGrabber::topLeft:
        {
            return rect;
            break;
        }
        case CornerGrabber::topRight:
        {
            QPointF move_to(0,0);
            rect.moveTopRight(move_to);
            return rect;
            break;
        }
        case CornerGrabber::bottomRight:
        {
            QPointF move_to(0,0);
            rect.moveBottomRight(move_to);
            return rect;
            break;
        }
        default:
            break;
    }
156
}