OSDN Git Service

It's 2011 now.
[qt-creator-jp/qt-creator-jp.git] / src / shared / designerintegrationv2 / sizehandlerect.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
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
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
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.
24 **
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.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33 #include "sizehandlerect.h"
34 #include "widgethostconstants.h"
35
36 #include <QtDesigner/QDesignerFormWindowInterface>
37
38 #include <QtGui/QMouseEvent>
39 #include <QtGui/QPainter>
40 #include <QtGui/QFrame>
41 #include <QtCore/QDebug>
42
43 enum { debugSizeHandle = 0 };
44
45 using namespace SharedTools::Internal;
46
47 SizeHandleRect::SizeHandleRect(QWidget *parent, Direction d, QWidget *resizable) :
48     QWidget(parent),
49     m_dir(d),
50     m_resizable(resizable),
51     m_state(SelectionHandleOff)
52 {
53     setBackgroundRole(QPalette::Text);
54     setAutoFillBackground(true);
55
56     setFixedSize(SELECTION_HANDLE_SIZE, SELECTION_HANDLE_SIZE);
57     setMouseTracking(false);
58     updateCursor();
59 }
60
61 void SizeHandleRect::updateCursor()
62 {
63     switch (m_dir) {
64     case Right:
65     case RightTop:
66         setCursor(Qt::SizeHorCursor);
67         return;
68     case RightBottom:
69         setCursor(Qt::SizeFDiagCursor);
70         return;
71     case LeftBottom:
72     case Bottom:
73         setCursor(Qt::SizeVerCursor);
74         return;
75     default:
76         break;
77     }
78
79     setCursor(Qt::ArrowCursor);
80 }
81
82 void SizeHandleRect::paintEvent(QPaintEvent *)
83 {
84     switch (m_state) {
85     case SelectionHandleOff:
86         break;
87     case SelectionHandleInactive: {
88         QPainter p(this);
89         p.setPen(Qt::red);
90         p.drawRect(0, 0, width() - 1, height() - 1);
91     }
92         break;
93     case SelectionHandleActive: {
94         QPainter p(this);
95         p.setPen(Qt::blue);
96         p.drawRect(0, 0, width() - 1, height() - 1);
97     }
98         break;
99     }
100 }
101
102 void SizeHandleRect::mousePressEvent(QMouseEvent *e)
103 {
104     e->accept();
105
106     if (e->button() != Qt::LeftButton)
107         return;
108
109     m_startSize = m_curSize = m_resizable->size();
110     m_startPos = m_curPos = m_resizable->mapFromGlobal(e->globalPos());
111     if (debugSizeHandle)
112         qDebug() << "SizeHandleRect::mousePressEvent" << m_startSize << m_startPos << m_curPos;
113
114 }
115
116 void SizeHandleRect::mouseMoveEvent(QMouseEvent *e)
117 {
118     if (!(e->buttons() & Qt::LeftButton))
119         return;
120
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());
128     switch (m_dir) {
129     case Right:
130     case RightTop: // Only width
131         delta.setHeight(0);
132         break;
133     case RightBottom: // All dimensions
134         break;
135     case LeftBottom:
136     case Bottom: // Only height
137         delta.setWidth(0);
138         break;
139     default:
140         delta = QSize(0, 0);
141         break;
142     }
143     if (delta != QSize(0, 0))
144         tryResize(delta);
145 }
146
147 void SizeHandleRect::mouseReleaseEvent(QMouseEvent *e)
148 {
149     if (e->button() != Qt::LeftButton)
150         return;
151
152     e->accept();
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());
156         if (debugSizeHandle)
157             qDebug() << "SizeHandleRect::mouseReleaseEvent" << startRect << newRect;
158         emit mouseButtonReleased(startRect, newRect);
159     }
160 }
161
162 void SizeHandleRect::tryResize(const QSize &delta)
163 {
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())
170         return;
171     if (debugSizeHandle)
172         qDebug() << "SizeHandleRect::tryResize by (" << m_startSize << '+' <<  delta << ')' << newSize;
173     m_resizable->resize(newSize);
174     m_curSize = m_resizable->size();
175 }
176
177 void SizeHandleRect::setState(SelectionHandleState st)
178 {
179     if (st == m_state)
180         return;
181     switch (st) {
182     case SelectionHandleOff:
183         hide();
184         break;
185     case SelectionHandleInactive:
186     case SelectionHandleActive:
187         show();
188         raise();
189         break;
190     }
191     m_state = st;
192 }