KDeclarative

DeclarativeDropArea.cpp
1 /*
2  SPDX-FileCopyrightText: 2010 BetterInbox <[email protected]>
3  SPDX-FileContributor: Gregory Schlomoff <[email protected]>
4 
5  SPDX-License-Identifier: MIT
6 */
7 
8 #include "DeclarativeDropArea.h"
9 #include "DeclarativeDragDropEvent.h"
10 
11 
12 DeclarativeDropArea::DeclarativeDropArea(QQuickItem *parent)
13  : QQuickItem(parent),
14  m_enabled(true),
15  m_preventStealing(false),
16  m_temporaryInhibition(false),
17  m_containsDrag(false)
18 {
19  setFlag(ItemAcceptsDrops, m_enabled);
20 }
21 
22 void DeclarativeDropArea::temporaryInhibitParent(bool inhibit)
23 {
24  QQuickItem *candidate = parentItem();
25 
26  while (candidate) {
27  if (DeclarativeDropArea *da = qobject_cast<DeclarativeDropArea *>(candidate)) {
28  da->m_temporaryInhibition = inhibit;
29  if (inhibit) {
30  Q_EMIT da->dragLeaveEvent(nullptr);
31  }
32  }
33  candidate = candidate->parentItem();
34  }
35 }
36 
37 void DeclarativeDropArea::dragEnterEvent(QDragEnterEvent *event)
38 {
39  if (!m_enabled || m_temporaryInhibition) {
40  return;
41  }
42 
43  DeclarativeDragDropEvent dde(event, this);
44  event->accept();
45 
46  Q_EMIT dragEnter(&dde);
47 
48  if (!event->isAccepted()) {
49  return;
50  }
51 
52  if (m_preventStealing) {
53  temporaryInhibitParent(true);
54  }
55 
56  m_oldDragMovePos = event->pos();
57  setContainsDrag(true);
58 }
59 
60 void DeclarativeDropArea::dragLeaveEvent(QDragLeaveEvent *event)
61 {
62  //do it anyways, in the unlikely case m_preventStealing
63  //was changed while drag
64  temporaryInhibitParent(false);
65 
66  m_oldDragMovePos = QPoint(-1,-1);
67  DeclarativeDragDropEvent dde(event, this);
68  Q_EMIT dragLeave(&dde);
69  setContainsDrag(false);
70 }
71 
72 void DeclarativeDropArea::dragMoveEvent(QDragMoveEvent *event)
73 {
74  if (!m_enabled || m_temporaryInhibition) {
75  event->ignore();
76  return;
77  }
78  event->accept();
79  //if the position we export didn't change, don't generate the move event
80  if (event->pos() == m_oldDragMovePos) {
81  return;
82  }
83 
84  m_oldDragMovePos = event->pos();
85  DeclarativeDragDropEvent dde(event, this);
86  Q_EMIT dragMove(&dde);
87 }
88 
89 void DeclarativeDropArea::dropEvent(QDropEvent *event)
90 {
91  //do it anyways, in the unlikely case m_preventStealing
92  //was changed while drag, do it after a loop,
93  //so the parent dropevent doesn't get delivered
94  metaObject()->invokeMethod(this, "temporaryInhibitParent", Qt::QueuedConnection, Q_ARG(bool, false));
95 
96  m_oldDragMovePos = QPoint(-1,-1);
97 
98  if (!m_enabled || m_temporaryInhibition) {
99  return;
100  }
101 
102  DeclarativeDragDropEvent dde(event, this);
103  Q_EMIT drop(&dde);
104  setContainsDrag(false);
105 }
106 
107 bool DeclarativeDropArea::isEnabled() const
108 {
109  return m_enabled;
110 }
111 
112 void DeclarativeDropArea::setEnabled(bool enabled)
113 {
114  if (enabled == m_enabled) {
115  return;
116  }
117 
118  m_enabled = enabled;
119  setFlag(ItemAcceptsDrops, m_enabled);
120  Q_EMIT enabledChanged();
121 }
122 
123 bool DeclarativeDropArea::preventStealing() const
124 {
125  return m_preventStealing;
126 }
127 
128 void DeclarativeDropArea::setPreventStealing(bool prevent)
129 {
130  if (prevent == m_preventStealing) {
131  return;
132  }
133 
134  m_preventStealing = prevent;
135  Q_EMIT preventStealingChanged();
136 }
137 
138 void DeclarativeDropArea::setContainsDrag(bool dragging)
139 {
140  if (m_containsDrag != dragging) {
141  m_containsDrag = dragging;
142  Q_EMIT containsDragChanged(m_containsDrag);
143  }
144 }
145 
146 bool DeclarativeDropArea::containsDrag() const
147 {
148  return m_containsDrag;
149 }
QQuickItem * parentItem() const const
QPoint pos() const const
virtual const QMetaObject * metaObject() const const
virtual bool event(QEvent *e)
bool isAccepted() const const
bool invokeMethod(QObject *obj, const char *member, Qt::ConnectionType type, QGenericReturnArgument ret, QGenericArgument val0, QGenericArgument val1, QGenericArgument val2, QGenericArgument val3, QGenericArgument val4, QGenericArgument val5, QGenericArgument val6, QGenericArgument val7, QGenericArgument val8, QGenericArgument val9)
KIOWIDGETS_EXPORT DropJob * drop(const QDropEvent *dropEvent, const QUrl &destUrl, JobFlags flags=DefaultFlags)
QueuedConnection
Q_EMITQ_EMIT
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Mon Jan 25 2021 22:44:28 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.