1 | /* |
2 | This file is part of the KDE libraries |
3 | SPDX-FileCopyrightText: 2000 Carsten Pfeiffer <pfeiffer@kde.org> |
4 | SPDX-FileCopyrightText: 2012 Kevin Ottens <ervin+bluesystems@kde.org> |
5 | |
6 | SPDX-License-Identifier: LGPL-2.0-or-later |
7 | */ |
8 | |
9 | #ifndef KDRAGWIDGETDECORATOR_H |
10 | #define KDRAGWIDGETDECORATOR_H |
11 | |
12 | #include <QObject> |
13 | |
14 | #include <kwidgetsaddons_export.h> |
15 | |
16 | #include <memory> |
17 | |
18 | class QDrag; |
19 | |
20 | /*! |
21 | * \class KDragWidgetDecorator |
22 | * \inmodule KWidgetsAddons |
23 | * \inheaderfile KDragWidgetDecorator |
24 | * |
25 | * \brief A decorator which adds drag-support to widgets. |
26 | * |
27 | * This is a decorator using an event filter to implement drag-support |
28 | * in widgets. |
29 | * |
30 | * You must override the virtual method dragObject() to specify the |
31 | * QDrag to be used. |
32 | */ |
33 | class KWIDGETSADDONS_EXPORT KDragWidgetDecoratorBase : public QObject |
34 | { |
35 | Q_OBJECT |
36 | |
37 | /*! |
38 | * \property KDragWidgetDecoratorBase::isDragEnabled |
39 | */ |
40 | Q_PROPERTY(bool isDragEnabled READ isDragEnabled WRITE setDragEnabled) |
41 | |
42 | public: |
43 | /*! |
44 | * Default constructor. |
45 | */ |
46 | explicit KDragWidgetDecoratorBase(QWidget *parent = nullptr); |
47 | |
48 | ~KDragWidgetDecoratorBase() override; |
49 | |
50 | /*! |
51 | * Enables/disables drag-support. Default is enabled. |
52 | */ |
53 | void setDragEnabled(bool enable); |
54 | |
55 | /*! |
56 | * Returns if drag support is enabled or not. |
57 | */ |
58 | bool isDragEnabled() const; |
59 | |
60 | protected: |
61 | /*! |
62 | * Returns the widget this decorator is attached to |
63 | */ |
64 | QWidget *decoratedWidget() const; |
65 | |
66 | /*! |
67 | * Reimplement this and return the QDrag object that should be used |
68 | * for the drag. Remember to give it "decoratedWidget()" as parent. |
69 | * |
70 | * Default implementation returns a null pointer, so that no drag is initiated. |
71 | */ |
72 | virtual QDrag *dragObject(); |
73 | |
74 | bool eventFilter(QObject *watched, QEvent *event) override; |
75 | |
76 | /*! |
77 | * Starts a drag (Copy by default) using dragObject() |
78 | */ |
79 | virtual void startDrag(); |
80 | |
81 | private: |
82 | std::unique_ptr<class KDragWidgetDecoratorBasePrivate> const d; |
83 | }; |
84 | |
85 | /*! |
86 | * \class KDragWidgetDecorator |
87 | * \inmodule KWidgetsAddons |
88 | * |
89 | * \brief A decorator which adds drag-support to widgets. |
90 | * |
91 | * This is a decorator using an event filter to implement drag-support |
92 | * in widgets. |
93 | * |
94 | * You must set the dragObjectFactory to specify the QDrag to be used. |
95 | */ |
96 | template<class Widget> |
97 | class KDragWidgetDecorator : public KDragWidgetDecoratorBase |
98 | { |
99 | public: |
100 | /*! |
101 | * \typedef KDragWidgetDecorator::DragObjectFactory |
102 | */ |
103 | typedef QDrag *(*DragObjectFactory)(Widget *); |
104 | |
105 | KDragWidgetDecorator(Widget *parent = nullptr) |
106 | : KDragWidgetDecoratorBase(parent) |
107 | , m_factory(nullptr) |
108 | { |
109 | } |
110 | |
111 | /*! |
112 | * Returns the QDrag factory used by this decorator |
113 | */ |
114 | DragObjectFactory dragObjectFactory() const |
115 | { |
116 | return m_factory; |
117 | } |
118 | |
119 | /*! |
120 | * Set a factory to be used by this decorator |
121 | * |
122 | * \a factory the new QDrag factory to use |
123 | */ |
124 | void setDragObjectFactory(DragObjectFactory factory) |
125 | { |
126 | m_factory = factory; |
127 | } |
128 | |
129 | private: |
130 | QDrag *dragObject() override |
131 | { |
132 | if (m_factory) { |
133 | Widget *w = static_cast<Widget *>(decoratedWidget()); |
134 | return m_factory(w); |
135 | } else { |
136 | return KDragWidgetDecoratorBase::dragObject(); |
137 | } |
138 | } |
139 | |
140 | DragObjectFactory m_factory; |
141 | }; |
142 | |
143 | #endif // KDRAGWIDGETDECORATOR_H |
144 | |