KWidgetsAddons

kdragwidgetdecorator.cpp
1/*
2 This file is part of the KDE libraries
3 SPDX-FileCopyrightText: 2000 Carsten Pfeiffer <pfeiffer@kde.org>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6*/
7
8#include "kdragwidgetdecorator.h"
9
10#include <QApplication>
11#include <QDrag>
12#include <QMouseEvent>
13#include <QWidget>
14
15class KDragWidgetDecoratorBasePrivate
16{
17public:
18 bool dragEnabled = true;
19 QWidget *decoratedWidget = nullptr;
20 QPoint startPos;
21};
22
24 : QObject(parent)
25 , d(new KDragWidgetDecoratorBasePrivate)
26{
28 d->decoratedWidget = parent;
29}
30
32
33bool KDragWidgetDecoratorBase::isDragEnabled() const
34{
35 return d->dragEnabled;
36}
37
39{
40 d->dragEnabled = enable;
41}
42
44{
45 Q_UNUSED(watched) // except in Q_ASSERT
46 Q_ASSERT(watched == d->decoratedWidget);
47
48 if (!d->dragEnabled) {
49 return false;
50 }
51
52 if (event->type() == QEvent::MouseButtonPress) {
53 QMouseEvent *e = static_cast<QMouseEvent *>(event);
54 d->startPos = e->pos();
55
56 } else if (event->type() == QEvent::MouseMove) {
57 QMouseEvent *e = static_cast<QMouseEvent *>(event);
58 if ((e->buttons() & Qt::LeftButton) && (e->pos() - d->startPos).manhattanLength() > QApplication::startDragDistance()) {
59 startDrag();
60 d->decoratedWidget->setProperty("down", false);
61 return true;
62 }
63 }
64
65 return false;
66}
67
69{
70 return d->decoratedWidget;
71}
72
74{
75 return nullptr;
76}
77
79{
80 QDrag *drag = dragObject();
81 if (drag) {
82 drag->exec(Qt::CopyAction);
83 }
84}
85
86#include "moc_kdragwidgetdecorator.cpp"
KDragWidgetDecoratorBase(QWidget *parent=nullptr)
Default constructor.
bool eventFilter(QObject *watched, QEvent *event) override
Reimplemented to add drag-support.
~KDragWidgetDecoratorBase() override
Destructs the decorator.
virtual void startDrag()
Starts a drag (Copy by default) using dragObject()
void setDragEnabled(bool enable)
Enables/disables drag-support.
virtual QDrag * dragObject()
Reimplement this and return the QDrag object that should be used for the drag.
Qt::DropAction exec(Qt::DropActions supportedActions)
MouseButtonPress
QPoint pos() const const
virtual bool event(QEvent *e)
void installEventFilter(QObject *filterObj)
QObject * parent() const const
Qt::MouseButtons buttons() const const
CopyAction
LeftButton
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Jul 19 2024 11:51:03 by doxygen 1.11.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.