cornergrabber.cpp 4.17 KB
Newer Older
roangel's avatar
roangel committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//    Cornergrabber that allows to dynamically change the size of tables and crazyflie zones
//
//    Copyright (C) 2017  Angel Romero
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.

18
#include "cornergrabber.h"
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
89
90
91
92
93
94
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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173

CornerGrabber::CornerGrabber(QGraphicsItem *parent,  int corner) :
    QGraphicsItem(parent),
    _outterborderColor(Qt::black),
    _outterborderPen(),
    _width(GRABBER_WIDTH),
    _height(GRABBER_HEIGHT),
    _corner(corner),
    _is_active(false)
{
    setParentItem(parent);
    _outterborderPen.setWidth(2);
    _outterborderPen.setColor(_outterborderColor);
    this->setAcceptHoverEvents(true);
    this->setFlag(QGraphicsItem::ItemIgnoresTransformations);
}


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
{
    QRectF bounding_rect = this->rect();
    return bounding_rect;
}

QRectF CornerGrabber::rect() const
{
    return _rect;
}

void CornerGrabber::setRect(const QRectF & rectangle)
{
    _rect = rectangle;
}

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

    // fill the box with solid color, use sharp corners
    prepareGeometryChange();
    _outterborderPen.setCapStyle(Qt::SquareCap);
    _outterborderPen.setStyle(Qt::SolidLine);
    painter->setPen(_outterborderPen);
    QRectF rect = createRect();
    this->setRect(rect);
    QBrush brush (Qt::SolidPattern);
    brush.setColor (_outterborderColor);
    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;
    }
}