summaryrefslogtreecommitdiffstats
path: root/resizeuihelper.cpp
blob: a458fd44e0575c886c923c485fb720408024e432 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/** This file is part of WebScraps **
 *
 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).*
 * All rights reserved.

 * Contact:  Nokia Corporation (qt-info@nokia.com)**

 * You may use this file under the terms of the BSD license as follows:
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *   1. Redistributions of source code must retain the above copyright notice, this list
 *      of conditions and the following disclaimer.
 *   2. Redistributions in binary form must reproduce the above copyright notice, this
 *      list of conditions and the following disclaimer in the documentation and/or
 *      other materials provided with the distribution.
 *   3. Neither the name of Nokia Corporation and its Subsidiary(-ies) nor the names of 
 *      its contributors may be used to endorse or promote products derived from this software 
 *      without specific prior written permission.

 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS 
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY 
 * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "resizeuihelper.h"
#include <QGraphicsSceneHoverEvent>
#include <QGraphicsSceneMouseEvent>
#include <QHoverEvent>
#include <QMouseEvent>
#include <QEvent>
#include <QGraphicsScene>
#include <QCursor>
#include <QWidget>

ResizeUiHelper::ResizeUiHelper(QGraphicsItem *item, QObject *parent)
        : QObject(parent)
        , m_item(0)
        , m_widget(0)
{
    item->scene()->addItem(this);
    item->installSceneEventFilter(this);
    item->setAcceptHoverEvents(true);
    m_item = item;
    m_rect = item->boundingRect();
    reset();
    setResizeEnabled(true);
}

ResizeUiHelper::ResizeUiHelper(QWidget *widget, QObject *parent)
        : QObject(parent)
        , m_item(0)
        , m_widget(0)
{
    widget->installEventFilter(this);
    widget->setMouseTracking(true);
    m_widget = widget;
    m_rect = widget->geometry();
    reset();
}

void ResizeUiHelper::reset()
{
    m_gripsEnabled.fill(true, int(ResizeUiHelper::Left) + 1);
    m_gripSize = QSizeF(5, 5);
    m_isResizeInProgress = false;
}

void ResizeUiHelper::setRect(const QRectF &rect)
{
    m_rect = rect;
}

QRectF ResizeUiHelper::rect() const
{
    return m_rect;
}

void ResizeUiHelper::setResizeEnabled(bool enabled)
{
    m_isResizeEnabled = enabled;
}

bool ResizeUiHelper::resizeEnabled() const
{
    return m_isResizeEnabled;
}

void ResizeUiHelper::setResizeGripEnabled(GripId grip, bool enabled)
{
    Q_ASSERT(grip <= ResizeUiHelper::Left);
    m_gripsEnabled[int(grip)] = enabled;
}

bool ResizeUiHelper::resizeGripEnabled(GripId grip) const
{
    Q_ASSERT(grip <= ResizeUiHelper::Left);
    return m_gripsEnabled[int(grip)];
}

void ResizeUiHelper::setAllResizeGripsEnabled(bool enabled)
{
    for (int i = int(ResizeUiHelper::TopLeft); i <= int(ResizeUiHelper::Left); i++)
        setResizeGripEnabled(GripId(i), enabled);
}

void ResizeUiHelper::setGripSize(QSizeF sz)
{
    m_gripSize = sz;
}

QSizeF ResizeUiHelper::gripSize() const
{
    return m_gripSize;
}

QRectF ResizeUiHelper::gripRect(GripId grip) const
{
    QPointF pt;
    switch (grip) {
    case ResizeUiHelper::TopLeft:
        pt = m_rect.topLeft(); break;
    case ResizeUiHelper::TopRight:
        pt = m_rect.topRight(); break;
    case ResizeUiHelper::BottomLeft:
        pt = m_rect.bottomLeft(); break;
    case ResizeUiHelper::BottomRight:
        pt = m_rect.bottomRight(); break;
    case ResizeUiHelper::Top:
        pt = QPointF(m_rect.center().x(), m_rect.top()); break;
    case ResizeUiHelper::Right:
        pt = QPointF(m_rect.right(), m_rect.center().y()); break;
    case ResizeUiHelper::Bottom:
        pt = QPointF(m_rect.center().x(), m_rect.bottom()); break;
    case ResizeUiHelper::Left:
        pt = QPointF(m_rect.left(), m_rect.center().y()); break;
    default:
        pt = QPointF();
    }
    QPointF tl = pt - QPointF(m_gripSize.width() / 2, m_gripSize.height() / 2);
    return QRectF(tl, m_gripSize);
}

QCursor ResizeUiHelper::gripCursor(GripId grip) const
{
    switch (grip) {
    case ResizeUiHelper::TopLeft:
    case ResizeUiHelper::BottomRight:
        return Qt::SizeFDiagCursor;
    case ResizeUiHelper::Top:
    case ResizeUiHelper::Bottom:
        return Qt::SizeVerCursor;
    case ResizeUiHelper::TopRight:
    case ResizeUiHelper::BottomLeft:
        return Qt::SizeBDiagCursor;
    case ResizeUiHelper::Right:
    case ResizeUiHelper::Left:
        return Qt::SizeHorCursor;
    default:
        return Qt::ArrowCursor;
    }
}

bool ResizeUiHelper::eventFilter(QObject *watched, QEvent * event)
{
    Q_UNUSED(watched);
    QMouseEvent *mouseEvent = dynamic_cast<QMouseEvent*>(event);
    QPointF eventPos;
    if (mouseEvent)
        eventPos = mouseEvent->pos();
    else
        return false;
    QEvent::Type eventType;
    switch (event->type()) {
    case QEvent::MouseMove:
        if ((mouseEvent->buttons() & Qt::LeftButton) == Qt::LeftButton)
            eventType = QEvent::GraphicsSceneMouseMove;
        else
            eventType = QEvent::GraphicsSceneHoverMove;
        break;
    case QEvent::MouseButtonPress:
        eventType = QEvent::GraphicsSceneMousePress;
        break;
    case QEvent::MouseButtonRelease:
        eventType = QEvent::GraphicsSceneMouseMove;
    default:
        return false;
    }
    return genericEventFilter(eventType, eventPos);
}

bool ResizeUiHelper::sceneEventFilter(QGraphicsItem *watched, QEvent* event)
{
    Q_UNUSED(watched);
    QGraphicsSceneHoverEvent *hoverEvent = dynamic_cast<QGraphicsSceneHoverEvent*>(event);
    QGraphicsSceneMouseEvent *mouseEvent = dynamic_cast<QGraphicsSceneMouseEvent*>(event);
    if (!hoverEvent && !mouseEvent)
        return false;
    QPointF eventPos;
    if (hoverEvent)
        eventPos = hoverEvent->pos();
    if (mouseEvent)
        eventPos = mouseEvent->pos();
    return genericEventFilter(event->type(), eventPos);
}

bool ResizeUiHelper::genericEventFilter(QEvent::Type eventType, QPointF eventPos)
{
    if (!resizeEnabled())
        return false;
    if (eventType == QEvent::GraphicsSceneMouseRelease) {
        m_isResizeInProgress = false;
        return false;
    }
    bool gripSectionMatched = false;
    for (int i = int(ResizeUiHelper::TopLeft); i <= int(ResizeUiHelper::Left); i++) {
        GripId section = GripId(i);
        if (m_isResizeInProgress && eventType == QEvent::GraphicsSceneMouseMove) {
            rectResize(m_buttonDownRect, m_activeResizeGrip, m_buttonDownPos, eventPos);
            return true; // event handled
        }
        if (gripRect(section).contains(eventPos)) {
            if (!resizeGripEnabled(section)) {
                return false;
            }
            if (eventType == QEvent::GraphicsSceneHoverMove) {
                setWatchedCursor(gripCursor(section));
                return true;
            } else if (eventType == QEvent::GraphicsSceneMousePress) {
                m_buttonDownPos = eventPos;
                m_buttonDownRect = m_rect;
                m_activeResizeGrip = section;
                m_isResizeInProgress = true;
                return true;
            }
            gripSectionMatched = true;
        }
    }
    if (!gripSectionMatched && eventType == QEvent::GraphicsSceneHoverMove)
        setWatchedCursor(gripCursor(ResizeUiHelper::NoGrip));
    return false;
}

QRectF ResizeUiHelper::rectResize(QRectF originalRect, GripId activeSection, QPointF originalPoint, QPointF newPoint)
{
    qreal dx1 = 0;
    qreal dy1 = 0;
    qreal dx2 = 0;
    qreal dy2 = 0;
    switch (activeSection) {
    case ResizeUiHelper::Left:
        dx1 = newPoint.x() - originalPoint.x();
        break;
    case ResizeUiHelper::Right:
        dx2 = newPoint.x() - originalPoint.x();
        break;
    case ResizeUiHelper::Top:
        dy1 = newPoint.y() - originalPoint.y();
        break;
    case ResizeUiHelper::Bottom:
        dy2 = newPoint.y() - originalPoint.y();
        break;
    case ResizeUiHelper::TopLeft:
        dy1 = newPoint.y() - originalPoint.y();
        dx1 = newPoint.x() - originalPoint.x();
        break;
    case ResizeUiHelper::BottomRight:
        dy2 = newPoint.y() - originalPoint.y();
        dx2 = newPoint.x() - originalPoint.x();
        break;
    case ResizeUiHelper::TopRight:
        dy1 = newPoint.y() - originalPoint.y();
        dx2 = newPoint.x() - originalPoint.x();
        break;
    case ResizeUiHelper::BottomLeft:
        dy2 = newPoint.y() - originalPoint.y();
        dx1 = newPoint.x() - originalPoint.x();
        break;
    default:
        break;
    }
    QRectF newRect = originalRect.adjusted(dx1, dy1, dx2, dy2);
    if (m_rect != newRect)
        emit rectResized(newRect);
    return (m_rect = newRect);
}

void ResizeUiHelper::setWatchedCursor(QCursor cursor)
{
    if (m_item)
        m_item->setCursor(cursor);
    if (m_widget)
        m_widget->setCursor(cursor);
}

QRectF ResizeUiHelper::boundingRect() const   // dummy
{
    return QRectF();
}

void ResizeUiHelper::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)   // dummy
{
    Q_UNUSED(painter);
    Q_UNUSED(option);
    Q_UNUSED(widget);
}