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
23KDragWidgetDecoratorBase::KDragWidgetDecoratorBase(QWidget *parent)
24 : QObject(parent)
25 , d(new KDragWidgetDecoratorBasePrivate)
26{
27 parent->installEventFilter(filterObj: this);
28 d->decoratedWidget = parent;
29}
30
31KDragWidgetDecoratorBase::~KDragWidgetDecoratorBase() = default;
32
33bool KDragWidgetDecoratorBase::isDragEnabled() const
34{
35 return d->dragEnabled;
36}
37
38void KDragWidgetDecoratorBase::setDragEnabled(bool enable)
39{
40 d->dragEnabled = enable;
41}
42
43bool KDragWidgetDecoratorBase::eventFilter(QObject *watched, QEvent *event)
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(name: "down", value: false);
61 return true;
62 }
63 }
64
65 return false;
66}
67
68QWidget *KDragWidgetDecoratorBase::decoratedWidget() const
69{
70 return d->decoratedWidget;
71}
72
73QDrag *KDragWidgetDecoratorBase::dragObject()
74{
75 return nullptr;
76}
77
78void KDragWidgetDecoratorBase::startDrag()
79{
80 QDrag *drag = dragObject();
81 if (drag) {
82 drag->exec(supportedActions: Qt::CopyAction);
83 }
84}
85
86#include "moc_kdragwidgetdecorator.cpp"
87

source code of kwidgetsaddons/src/kdragwidgetdecorator.cpp