1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights. These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
32 **************************************************************************/
33 #include "sizehandlerect.h"
34 #include "widgethostconstants.h"
36 #include <QtDesigner/QDesignerFormWindowInterface>
38 #include <QtGui/QMouseEvent>
39 #include <QtGui/QPainter>
40 #include <QtGui/QFrame>
41 #include <QtCore/QDebug>
43 enum { debugSizeHandle = 0 };
45 using namespace SharedTools::Internal;
47 SizeHandleRect::SizeHandleRect(QWidget *parent, Direction d, QWidget *resizable) :
50 m_resizable(resizable),
51 m_state(SelectionHandleOff)
53 setBackgroundRole(QPalette::Text);
54 setAutoFillBackground(true);
56 setFixedSize(SELECTION_HANDLE_SIZE, SELECTION_HANDLE_SIZE);
57 setMouseTracking(false);
61 void SizeHandleRect::updateCursor()
66 setCursor(Qt::SizeHorCursor);
69 setCursor(Qt::SizeFDiagCursor);
73 setCursor(Qt::SizeVerCursor);
79 setCursor(Qt::ArrowCursor);
82 void SizeHandleRect::paintEvent(QPaintEvent *)
85 case SelectionHandleOff:
87 case SelectionHandleInactive: {
90 p.drawRect(0, 0, width() - 1, height() - 1);
93 case SelectionHandleActive: {
96 p.drawRect(0, 0, width() - 1, height() - 1);
102 void SizeHandleRect::mousePressEvent(QMouseEvent *e)
106 if (e->button() != Qt::LeftButton)
109 m_startSize = m_curSize = m_resizable->size();
110 m_startPos = m_curPos = m_resizable->mapFromGlobal(e->globalPos());
112 qDebug() << "SizeHandleRect::mousePressEvent" << m_startSize << m_startPos << m_curPos;
116 void SizeHandleRect::mouseMoveEvent(QMouseEvent *e)
118 if (!(e->buttons() & Qt::LeftButton))
121 // Try resize with delta against start position.
122 // We don't take little deltas in consecutive move events as this
123 // causes the handle and the mouse cursor to become out of sync
124 // once a min/maxSize limit is hit. When the cursor reenters the valid
125 // areas, it will now snap to it.
126 m_curPos = m_resizable->mapFromGlobal(e->globalPos());
127 QSize delta = QSize(m_curPos.x() - m_startPos.x(), m_curPos.y() - m_startPos.y());
130 case RightTop: // Only width
133 case RightBottom: // All dimensions
136 case Bottom: // Only height
143 if (delta != QSize(0, 0))
147 void SizeHandleRect::mouseReleaseEvent(QMouseEvent *e)
149 if (e->button() != Qt::LeftButton)
153 if (m_startSize != m_curSize) {
154 const QRect startRect = QRect(0, 0, m_startPos.x(), m_startPos.y());
155 const QRect newRect = QRect(0, 0, m_curPos.x(), m_curPos.y());
157 qDebug() << "SizeHandleRect::mouseReleaseEvent" << startRect << newRect;
158 emit mouseButtonReleased(startRect, newRect);
162 void SizeHandleRect::tryResize(const QSize &delta)
164 // Try resize with delta against start position
165 QSize newSize = m_startSize + delta;
166 newSize = newSize.expandedTo(m_resizable->minimumSizeHint());
167 newSize = newSize.expandedTo(m_resizable->minimumSize());
168 newSize = newSize.boundedTo(m_resizable->maximumSize());
169 if (newSize == m_resizable->size())
172 qDebug() << "SizeHandleRect::tryResize by (" << m_startSize << '+' << delta << ')' << newSize;
173 m_resizable->resize(newSize);
174 m_curSize = m_resizable->size();
177 void SizeHandleRect::setState(SelectionHandleState st)
182 case SelectionHandleOff:
185 case SelectionHandleInactive:
186 case SelectionHandleActive: