Qt 6.x
The Qt SDK
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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
11
216using namespace Qt::Literals::StringLiterals;
217
219{
220public:
221 Q_DECLARE_PUBLIC(QQuickTreeViewDelegate)
222
225 void toggleExpanded();
226 QPalette defaultPalette() const override;
227
228public:
234 bool m_isTreeNode = false;
235 bool m_expanded = false;
236 bool m_current = false;
237 bool m_selected = false;
238 bool m_editing = false;
239 bool m_hasChildren = false;
241 int m_depth = 0;
242};
243
245{
247
248 auto view = q->treeView();
249 if (!view)
250 return;
251 if (!view->pointerNavigationEnabled())
252 return;
253
254 const int row = qmlContext(q)->contextProperty(u"row"_s).toInt();
255 view->toggleExpanded(row);
256}
257
259{
261
262 // Remove the tap handler that was installed
263 // on the previous indicator
265
266 auto indicator = q->indicator();
267 if (!indicator)
268 return;
269
272 // Work-around to block taps from passing through to TreeView.
275}
276
278{
280
281 if (auto indicator = q_func()->indicator()) {
282 const bool insideDelegateBounds = indicator->x() + indicator->width() < q->width();
283 indicator->setVisible(m_isTreeNode && m_hasChildren && insideDelegateBounds);
284 }
285}
286
289{
291
292 auto tapHandler = new QQuickTapHandler(this);
293 tapHandler->setAcceptedModifiers(Qt::NoModifier);
295
296 // Since we override mousePressEvent to avoid QQuickAbstractButton from blocking
297 // pointer handlers, we inform the button about its pressed state from the tap
298 // handler instead. This will ensure that we emit button signals like
299 // pressed, clicked, and doubleClicked.
300 connect(tapHandler, &QQuickTapHandler::pressedChanged, [this, d, tapHandler] {
301 auto view = treeView();
302 if (view && !view->pointerNavigationEnabled())
303 return;
304
305 const QQuickHandlerPoint p = tapHandler->point();
306 if (tapHandler->isPressed())
307 d->handlePress(p.position(), 0);
308 else if (tapHandler->tapCount() > 0)
309 d->handleRelease(p.position(), 0);
310 else
311 d->handleUngrab();
312
313 if (tapHandler->tapCount() > 1 && !tapHandler->isPressed())
315 });
316}
317
318void QQuickTreeViewDelegate::geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry)
319{
321
322 QQuickItemDelegate::geometryChange(newGeometry, oldGeometry);
323 d->updateIndicatorVisibility();
324}
325
327{
329
330 const auto view = d->m_treeView;
331 if (view && view->pointerNavigationEnabled()) {
332 // Ignore mouse events so that we don't block our own pointer handlers, or
333 // pointer handlers in e.g TreeView, TableView, or SelectionRectangle. Instead
334 // we call out to the needed mouse handling functions in QAbstractButton directly
335 // from our pointer handlers, to ensure that continue to work as a button.
336 event->ignore();
337 return;
338 }
339
341}
342
344{
346}
347
349{
351}
352
354{
355 return d_func()->m_indentation;
356}
357
359{
361 if (qFuzzyCompare(d->m_indentation, indentation))
362 return;
363
364 d->m_indentation = indentation;
366}
367
369{
370 return d_func()->m_isTreeNode;
371}
372
374{
376 if (d->m_isTreeNode == isTreeNode)
377 return;
378
379 d->m_isTreeNode = isTreeNode;
380 d->updateIndicatorVisibility();
382}
383
385{
386 return d_func()->m_hasChildren;
387}
388
390{
392 if (d->m_hasChildren == hasChildren)
393 return;
394
395 d->m_hasChildren = hasChildren;
396 d->updateIndicatorVisibility();
398}
399
401{
402 return d_func()->m_expanded;
403}
404
406{
408 if (d->m_expanded == expanded)
409 return;
410
411 d->m_expanded = expanded;
413}
414
416{
417 return d_func()->m_current;
418}
419
421{
423 if (d->m_current == current)
424 return;
425
426 d->m_current = current;
427 emit currentChanged();
428}
429
431{
432 return d_func()->m_selected;
433}
434
436{
438 if (d->m_selected == selected)
439 return;
440
441 d->m_selected = selected;
442 emit selectedChanged();
443}
444
446{
447 return d_func()->m_editing;
448}
449
451{
453 if (d->m_editing == editing)
454 return;
455
456 d->m_editing = editing;
457 emit editingChanged();
458}
459
461{
462 return d_func()->m_depth;
463}
464
466{
468 if (d->m_depth == depth)
469 return;
470
471 d->m_depth = depth;
473}
474
476{
477 return d_func()->m_treeView;
478}
479
481{
483 if (d->m_treeView == treeView)
484 return;
485
486 d->m_treeView = treeView;
488}
489
491{
494 d->updateIndicatorVisibility();
495 d->updateIndicatorPointerHandlers();
496}
497
499{
500 return d_func()->m_leftMargin;
501}
502
504{
506 if (qFuzzyCompare(d->m_leftMargin, leftMargin))
507 return;
508
509 d->m_leftMargin = leftMargin;
511}
512
514{
515 return d_func()->m_rightMargin;
516}
517
519{
521 if (qFuzzyCompare(d->m_rightMargin, rightMargin))
522 return;
523
524 d->m_rightMargin = rightMargin;
526}
527
529
530#include "moc_qquicktreeviewdelegate_p.cpp"
\reentrant
Definition qfont.h:20
\inmodule QtGui
Definition qevent.h:195
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:298
The QPalette class contains color groups for each widget state.
Definition qpalette.h:19
\inmodule QtCore
Definition qpointer.h:18
T * data() const
Definition qpointer.h:56
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:64
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:73
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:76
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:483
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:287
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:71
#define emit
double qreal
Definition qtypes.h:92
connect(quitButton, &QPushButton::clicked, &app, &QCoreApplication::quit, Qt::QueuedConnection)
QQuickView * view
[0]
IUIAutomationTreeWalker __RPC__deref_out_opt IUIAutomationElement ** parent