1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qcolumnviewgrip_p.h"
5#include <qstyleoption.h>
6#include <qpainter.h>
7#include <qbrush.h>
8#include <qevent.h>
9#include <qdebug.h>
10
11QT_BEGIN_NAMESPACE
12
13/*
14 \internal
15 class QColumnViewGrip
16
17 QColumnViewGrip is created to go inside QAbstractScrollArea's corner.
18 When the mouse it moved it will resize the scroll area and emit's a signal.
19 */
20
21/*
22 \internal
23 \fn void QColumnViewGrip::gripMoved()
24 Signal that is emitted when the grip moves the parent widget.
25 */
26
27/*!
28 Creates a new QColumnViewGrip with the given \a parent to view a model.
29 Use setModel() to set the model.
30*/
31QColumnViewGrip::QColumnViewGrip(QWidget *parent)
32 : QWidget(*new QColumnViewGripPrivate, parent, { })
33{
34#ifndef QT_NO_CURSOR
35 setCursor(Qt::SplitHCursor);
36#endif
37}
38
39/*!
40 \internal
41*/
42QColumnViewGrip::QColumnViewGrip(QColumnViewGripPrivate & dd, QWidget *parent, Qt::WindowFlags f)
43: QWidget(dd, parent, f)
44{
45}
46
47/*!
48 Destroys the view.
49*/
50QColumnViewGrip::~QColumnViewGrip()
51{
52}
53
54/*!
55 Attempt to resize the parent object by \a offset
56 returns the amount of offset that it was actually able to resized
57*/
58int QColumnViewGrip::moveGrip(int offset)
59{
60 QWidget *parentWidget = (QWidget*)parent();
61
62 // first resize the parent
63 int oldWidth = parentWidget->width();
64 int newWidth = oldWidth;
65 if (isRightToLeft())
66 newWidth -= offset;
67 else
68 newWidth += offset;
69 newWidth = qMax(a: parentWidget->minimumWidth(), b: newWidth);
70 parentWidget->resize(w: newWidth, h: parentWidget->height());
71
72 // Then have the view move the widget
73 int realOffset = parentWidget->width() - oldWidth;
74 int oldX = parentWidget->x();
75 if (realOffset != 0)
76 emit gripMoved(offset: realOffset);
77 if (isRightToLeft())
78 realOffset = -1 * (oldX - parentWidget->x());
79 return realOffset;
80}
81
82/*!
83 \reimp
84*/
85void QColumnViewGrip::paintEvent(QPaintEvent *event)
86{
87 QPainter painter(this);
88 QStyleOption opt;
89 opt.initFrom(w: this);
90 style()->drawControl(element: QStyle::CE_ColumnViewGrip, opt: &opt, p: &painter, w: this);
91 event->accept();
92}
93
94/*!
95 \reimp
96 Resize the parent window to the sizeHint
97*/
98void QColumnViewGrip::mouseDoubleClickEvent(QMouseEvent *event)
99{
100 Q_UNUSED(event);
101 QWidget *parentWidget = (QWidget*)parent();
102 int offset = parentWidget->sizeHint().width() - parentWidget->width();
103 if (isRightToLeft())
104 offset *= -1;
105 moveGrip(offset);
106 event->accept();
107}
108
109/*!
110 \reimp
111 Begin watching for mouse movements
112*/
113void QColumnViewGrip::mousePressEvent(QMouseEvent *event)
114{
115 Q_D(QColumnViewGrip);
116 d->originalXLocation = event->globalPosition().toPoint().x();
117 event->accept();
118}
119
120/*!
121 \reimp
122 Calculate the movement of the grip and moveGrip() and emit gripMoved
123*/
124void QColumnViewGrip::mouseMoveEvent(QMouseEvent *event)
125{
126 Q_D(QColumnViewGrip);
127 int offset = event->globalPosition().toPoint().x() - d->originalXLocation;
128 d->originalXLocation = moveGrip(offset) + d->originalXLocation;
129 event->accept();
130}
131
132/*!
133 \reimp
134 Stop watching for mouse movements
135*/
136void QColumnViewGrip::mouseReleaseEvent(QMouseEvent *event)
137{
138 Q_D(QColumnViewGrip);
139 d->originalXLocation = -1;
140 event->accept();
141}
142
143/*
144 * private object implementation
145 */
146QColumnViewGripPrivate::QColumnViewGripPrivate()
147: QWidgetPrivate(),
148originalXLocation(-1)
149{
150}
151
152QT_END_NAMESPACE
153
154#include "moc_qcolumnviewgrip_p.cpp"
155

source code of qtbase/src/widgets/itemviews/qcolumnviewgrip.cpp