Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qquicktreeviewdelegate.cpp
Go to the documentation of this file.
1// Copyright (C) 2021 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
5
6#include <QtQuickTemplates2/private/qquickitemdelegate_p_p.h>
7#include <QtQuick/private/qquicktaphandler_p.h>
8#include <QtQuick/private/qquicktreeview_p_p.h>
9
10#include <QtCore/qpointer.h>
11
13
218using namespace Qt::Literals::StringLiterals;
219
221{
222public:
223 Q_DECLARE_PUBLIC(QQuickTreeViewDelegate)
224
227 void toggleExpanded();
228 QPalette defaultPalette() const override;
229
230public:
231 QPointer<QQuickTreeView> m_treeView;
232 QPointer<QQuickTapHandler> m_tapHandlerOnIndicator;
236 bool m_isTreeNode = false;
237 bool m_expanded = false;
238 bool m_current = false;
239 bool m_selected = false;
240 bool m_editing = false;
241 bool m_hasChildren = false;
243 int m_depth = 0;
244};
245
247{
249
250 auto view = q->treeView();
251 if (!view)
252 return;
253 if (!view->pointerNavigationEnabled())
254 return;
255
256 const int row = qmlContext(q)->contextProperty(u"row"_s).toInt();
257 view->toggleExpanded(row);
258}
259
261{
263
264 // Remove the tap handler that was installed
265 // on the previous indicator
267
268 auto indicator = q->indicator();
269 if (!indicator)
270 return;
271
274 // Work-around to block taps from passing through to TreeView.
277}
278
280{
282
283 if (auto indicator = q_func()->indicator()) {
284 const bool insideDelegateBounds = indicator->x() + indicator->width() < q->width();
285 indicator->setVisible(m_isTreeNode && m_hasChildren && insideDelegateBounds);
286 }
287}
288
291{
293
294 auto tapHandler = new QQuickTapHandler(this);
295 tapHandler->setAcceptedModifiers(Qt::NoModifier);
297
298 // Since we override mousePressEvent to avoid QQuickAbstractButton from blocking
299 // pointer handlers, we inform the button about its pressed state from the tap
300 // handler instead. This will ensure that we emit button signals like
301 // pressed, clicked, and doubleClicked.
302 connect(tapHandler, &QQuickTapHandler::pressedChanged, [this, d, tapHandler] {
303 auto view = treeView();
304 if (view && !view->pointerNavigationEnabled())
305 return;
306
307 const QQuickHandlerPoint p = tapHandler->point();
308 if (tapHandler->isPressed())
309 d->handlePress(p.position(), 0);
310 else if (tapHandler->tapCount() > 0)
311 d->handleRelease(p.position(), 0);
312 else
313 d->handleUngrab();
314
315 if (tapHandler->tapCount() > 1 && !tapHandler->isPressed())
317 });
318}
319
320void QQuickTreeViewDelegate::geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry)
321{
323
324 QQuickItemDelegate::geometryChange(newGeometry, oldGeometry);
325 d->updateIndicatorVisibility();
326}
327
329{
331
332 const auto view = d->m_treeView;
333 if (view && view->pointerNavigationEnabled()) {
334 // Ignore mouse events so that we don't block our own pointer handlers, or
335 // pointer handlers in e.g TreeView, TableView, or SelectionRectangle. Instead
336 // we call out to the needed mouse handling functions in QAbstractButton directly
337 // from our pointer handlers, to ensure that continue to work as a button.
338 event->ignore();
339 return;
340 }
341
343}
344
349
354
356{
357 return d_func()->m_indentation;
358}
359
361{
363 if (qFuzzyCompare(d->m_indentation, indentation))
364 return;
365
366 d->m_indentation = indentation;
368}
369
371{
372 return d_func()->m_isTreeNode;
373}
374
376{
378 if (d->m_isTreeNode == isTreeNode)
379 return;
380
381 d->m_isTreeNode = isTreeNode;
382 d->updateIndicatorVisibility();
384}
385
387{
388 return d_func()->m_hasChildren;
389}
390
392{
394 if (d->m_hasChildren == hasChildren)
395 return;
396
397 d->m_hasChildren = hasChildren;
398 d->updateIndicatorVisibility();
400}
401
403{
404 return d_func()->m_expanded;
405}
406
408{
410 if (d->m_expanded == expanded)
411 return;
412
413 d->m_expanded = expanded;
415}
416
418{
419 return d_func()->m_current;
420}
421
423{
425 if (d->m_current == current)
426 return;
427
428 d->m_current = current;
429 emit currentChanged();
430}
431
433{
434 return d_func()->m_selected;
435}
436
438{
440 if (d->m_selected == selected)
441 return;
442
443 d->m_selected = selected;
444 emit selectedChanged();
445}
446
448{
449 return d_func()->m_editing;
450}
451
453{
455 if (d->m_editing == editing)
456 return;
457
458 d->m_editing = editing;
459 emit editingChanged();
460}
461
463{
464 return d_func()->m_depth;
465}
466
468{
470 if (d->m_depth == depth)
471 return;
472
473 d->m_depth = depth;
475}
476
478{
479 return d_func()->m_treeView;
480}
481
483{
485 if (d->m_treeView == treeView)
486 return;
487
488 d->m_treeView = treeView;
490}
491
493{
496 d->updateIndicatorVisibility();
497 d->updateIndicatorPointerHandlers();
498}
499
501{
502 return d_func()->m_leftMargin;
503}
504
506{
508 if (qFuzzyCompare(d->m_leftMargin, leftMargin))
509 return;
510
511 d->m_leftMargin = leftMargin;
513}
514
516{
517 return d_func()->m_rightMargin;
518}
519
521{
523 if (qFuzzyCompare(d->m_rightMargin, rightMargin))
524 return;
525
526 d->m_rightMargin = rightMargin;
528}
529
531
532#include "moc_qquicktreeviewdelegate_p.cpp"
\reentrant
Definition qfont.h:22
\inmodule QtGui
Definition qevent.h:196
static QMetaObject::Connection connect(const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, const typename QtPrivate::FunctionPointer< Func2 >::Object *receiverPrivate, Func2 slot, Qt::ConnectionType type=Qt::AutoConnection)
Definition qobject_p.h:299
The QPalette class contains color groups for each widget state.
Definition qpalette.h:19
T * data() const noexcept
Definition qpointer.h:73
QVariant contextProperty(const QString &) const
Returns the value of the name property for this context as a QVariant.
virtual void componentComplete()=0
Invoked after the root component that caused this instantiation has completed construction.
QQuickDeferredPointer< QQuickItem > indicator
The QQuickItem class provides the most basic of all visual items in \l {Qt Quick}.
Definition qquickitem.h:63
virtual void geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry)
qreal x
\qmlproperty real QtQuick::Item::x \qmlproperty real QtQuick::Item::y \qmlproperty real QtQuick::Item...
Definition qquickitem.h:72
virtual void mousePressEvent(QMouseEvent *event)
This event handler can be reimplemented in a subclass to receive mouse press events for an item.
qreal width
This property holds the width of this item.
Definition qquickitem.h:75
void setVisible(bool)
void setAcceptedModifiers(Qt::KeyboardModifiers acceptedModifiers)
\qmlproperty flags PointerDeviceHandler::acceptedModifiers
void setGesturePolicy(GesturePolicy gesturePolicy)
\qmlproperty enumeration QtQuick::TapHandler::gesturePolicy
void tapped(QEventPoint eventPoint, Qt::MouseButton)
static QPalette palette(Scope scope)
static QFont font(Scope scope)
QPointer< QQuickTapHandler > m_tapHandlerOnIndicator
QPalette defaultPalette() const override
QPointer< QQuickTreeView > m_treeView
void setRightMargin(qreal rightMargin)
void setHasChildren(bool hasChildren)
void setIndentation(qreal indentation)
void setTreeView(QQuickTreeView *treeView)
QFont defaultFont() const override
void geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry) override
void mousePressEvent(QMouseEvent *event) override
This event handler can be reimplemented in a subclass to receive mouse press events for an item.
void componentComplete() override
Invoked after the root component that caused this instantiation has completed construction.
QQuickTreeViewDelegate(QQuickItem *parent=nullptr)
void setIsTreeNode(bool isTreeNode)
void setLeftMargin(qreal leftMargin)
\inmodule QtCore\reentrant
Definition qrect.h:484
int toInt(bool *ok=nullptr) const
Returns the variant as an int if the variant has userType() \l QMetaType::Int, \l QMetaType::Bool,...
Combined button and popup list for selecting options.
@ NoModifier
bool qFuzzyCompare(qfloat16 p1, qfloat16 p2) noexcept
Definition qfloat16.h:333
GLint GLenum GLsizei GLsizei GLsizei depth
struct _cl_event * event
GLdouble GLdouble GLdouble GLdouble q
Definition qopenglext.h:259
GLenum GLenum GLsizei void * row
GLfloat GLfloat p
[1]
QQmlContext * qmlContext(const QObject *obj)
Definition qqml.cpp:75
#define emit
double qreal
Definition qtypes.h:187
connect(quitButton, &QPushButton::clicked, &app, &QCoreApplication::quit, Qt::QueuedConnection)
QQuickView * view
[0]