1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtWidgets module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** 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 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 #include "qcolumnviewgrip_p.h"
41 #include <qstyleoption.h>
42 #include <qpainter.h>
43 #include <qbrush.h>
44 #include <qevent.h>
45 #include <qdebug.h>
46 
47 QT_BEGIN_NAMESPACE
48 
49 /*
50     \internal
51     class QColumnViewGrip
52 
53     QColumnViewGrip is created to go inside QAbstractScrollArea's corner.
54     When the mouse it moved it will resize the scroll area and emit's a signal.
55  */
56 
57 /*
58     \internal
59     \fn void QColumnViewGrip::gripMoved()
60     Signal that is emitted when the grip moves the parent widget.
61  */
62 
63 /*!
64     Creates a new QColumnViewGrip with the given \a parent to view a model.
65     Use setModel() to set the model.
66 */
QColumnViewGrip(QWidget * parent)67 QColumnViewGrip::QColumnViewGrip(QWidget *parent)
68     :  QWidget(*new QColumnViewGripPrivate, parent, { })
69 {
70 #ifndef QT_NO_CURSOR
71     setCursor(Qt::SplitHCursor);
72 #endif
73 }
74 
75 /*!
76   \internal
77 */
QColumnViewGrip(QColumnViewGripPrivate & dd,QWidget * parent,Qt::WindowFlags f)78 QColumnViewGrip::QColumnViewGrip(QColumnViewGripPrivate & dd, QWidget *parent, Qt::WindowFlags f)
79 :  QWidget(dd, parent, f)
80 {
81 }
82 
83 /*!
84   Destroys the view.
85 */
~QColumnViewGrip()86 QColumnViewGrip::~QColumnViewGrip()
87 {
88 }
89 
90 /*!
91     Attempt to resize the parent object by \a offset
92     returns the amount of offset that it was actually able to resized
93 */
moveGrip(int offset)94 int QColumnViewGrip::moveGrip(int offset)
95 {
96     QWidget *parentWidget = (QWidget*)parent();
97 
98     // first resize the parent
99     int oldWidth = parentWidget->width();
100     int newWidth = oldWidth;
101     if (isRightToLeft())
102        newWidth -= offset;
103     else
104        newWidth += offset;
105     newWidth = qMax(parentWidget->minimumWidth(), newWidth);
106     parentWidget->resize(newWidth, parentWidget->height());
107 
108     // Then have the view move the widget
109     int realOffset = parentWidget->width() - oldWidth;
110     int oldX = parentWidget->x();
111     if (realOffset != 0)
112         emit gripMoved(realOffset);
113     if (isRightToLeft())
114         realOffset = -1 * (oldX - parentWidget->x());
115     return realOffset;
116 }
117 
118 /*!
119     \reimp
120 */
paintEvent(QPaintEvent * event)121 void QColumnViewGrip::paintEvent(QPaintEvent *event)
122 {
123     QPainter painter(this);
124     QStyleOption opt;
125     opt.initFrom(this);
126     style()->drawControl(QStyle::CE_ColumnViewGrip, &opt, &painter, this);
127     event->accept();
128 }
129 
130 /*!
131     \reimp
132     Resize the parent window to the sizeHint
133 */
mouseDoubleClickEvent(QMouseEvent * event)134 void QColumnViewGrip::mouseDoubleClickEvent(QMouseEvent *event)
135 {
136     Q_UNUSED(event);
137     QWidget *parentWidget = (QWidget*)parent();
138     int offset = parentWidget->sizeHint().width() - parentWidget->width();
139     if (isRightToLeft())
140         offset *= -1;
141     moveGrip(offset);
142     event->accept();
143 }
144 
145 /*!
146     \reimp
147     Begin watching for mouse movements
148 */
mousePressEvent(QMouseEvent * event)149 void QColumnViewGrip::mousePressEvent(QMouseEvent *event)
150 {
151     Q_D(QColumnViewGrip);
152     d->originalXLocation = event->globalX();
153     event->accept();
154 }
155 
156 /*!
157     \reimp
158     Calculate the movement of the grip and moveGrip() and emit gripMoved
159 */
mouseMoveEvent(QMouseEvent * event)160 void QColumnViewGrip::mouseMoveEvent(QMouseEvent *event)
161 {
162     Q_D(QColumnViewGrip);
163     int offset = event->globalX() - d->originalXLocation;
164     d->originalXLocation = moveGrip(offset) + d->originalXLocation;
165     event->accept();
166 }
167 
168 /*!
169     \reimp
170     Stop watching for mouse movements
171 */
mouseReleaseEvent(QMouseEvent * event)172 void QColumnViewGrip::mouseReleaseEvent(QMouseEvent *event)
173 {
174     Q_D(QColumnViewGrip);
175     d->originalXLocation = -1;
176     event->accept();
177 }
178 
179 /*
180  * private object implementation
181  */
QColumnViewGripPrivate()182 QColumnViewGripPrivate::QColumnViewGripPrivate()
183 :  QWidgetPrivate(),
184 originalXLocation(-1)
185 {
186 }
187 
188 QT_END_NAMESPACE
189 
190 #include "moc_qcolumnviewgrip_p.cpp"
191