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