File indexing completed on 2025-02-16 13:11:41
0001 /* 0002 This file is part of the KDE libraries 0003 SPDX-FileCopyrightText: 2000 Carsten Pfeiffer <pfeiffer@kde.org> 0004 SPDX-FileCopyrightText: 2012 Kevin Ottens <ervin+bluesystems@kde.org> 0005 0006 SPDX-License-Identifier: LGPL-2.0-or-later 0007 */ 0008 0009 #ifndef KDRAGWIDGETDECORATOR_H 0010 #define KDRAGWIDGETDECORATOR_H 0011 0012 #include <QObject> 0013 0014 #include <kwidgetsaddons_export.h> 0015 0016 #include <memory> 0017 0018 class QDrag; 0019 0020 /** 0021 * @brief A decorator which adds drag-support to widgets 0022 * 0023 * This is a decorator using an event filter to implement drag-support 0024 * in widgets. 0025 * You must override the virtual method dragObject() to specify the 0026 * QDrag to be used. 0027 * 0028 * @author Carsten Pfeiffer <pfeiffer@kde.org> 0029 */ 0030 class KWIDGETSADDONS_EXPORT KDragWidgetDecoratorBase : public QObject 0031 { 0032 Q_OBJECT 0033 Q_PROPERTY(bool isDragEnabled READ isDragEnabled WRITE setDragEnabled) 0034 0035 public: 0036 /** 0037 * Default constructor. 0038 */ 0039 explicit KDragWidgetDecoratorBase(QWidget *parent = nullptr); 0040 0041 /** 0042 * Destructs the decorator. 0043 */ 0044 ~KDragWidgetDecoratorBase() override; 0045 0046 /** 0047 * Enables/disables drag-support. Default is enabled. 0048 */ 0049 void setDragEnabled(bool enable); 0050 0051 /** 0052 * @returns if drag support is enabled or not. 0053 */ 0054 bool isDragEnabled() const; 0055 0056 protected: 0057 /** 0058 * @return the widget this decorator is attached to 0059 */ 0060 QWidget *decoratedWidget() const; 0061 0062 /** 0063 * Reimplement this and return the QDrag object that should be used 0064 * for the drag. Remember to give it "decoratedWidget()" as parent. 0065 * 0066 * Default implementation returns a null pointer, so that no drag is initiated. 0067 */ 0068 virtual QDrag *dragObject(); 0069 0070 /** 0071 * Reimplemented to add drag-support 0072 */ 0073 bool eventFilter(QObject *watched, QEvent *event) override; 0074 0075 /** 0076 * Starts a drag (Copy by default) using dragObject() 0077 */ 0078 virtual void startDrag(); 0079 0080 private: 0081 std::unique_ptr<class KDragWidgetDecoratorBasePrivate> const d; 0082 }; 0083 0084 /** 0085 * @class KDragWidgetDecorator kdragwidgetdecorator.h KDragWidgetDecorator 0086 * 0087 * @brief A decorator which adds drag-support to widgets 0088 * 0089 * This is a decorator using an event filter to implement drag-support 0090 * in widgets. 0091 * You must set the dragObjectFactory to specify the QDrag to be used. 0092 * 0093 * @author Kevin Ottens <ervin@kde.org> 0094 */ 0095 template<class Widget> 0096 class KDragWidgetDecorator : public KDragWidgetDecoratorBase 0097 { 0098 public: 0099 typedef QDrag *(*DragObjectFactory)(Widget *); 0100 0101 KDragWidgetDecorator(Widget *parent = nullptr) 0102 : KDragWidgetDecoratorBase(parent) 0103 , m_factory(nullptr) 0104 { 0105 } 0106 0107 /** 0108 * @return the QDrag factory used by this decorator 0109 */ 0110 DragObjectFactory dragObjectFactory() const 0111 { 0112 return m_factory; 0113 } 0114 0115 /** 0116 * Set a factory to be used by this decorator 0117 * 0118 * @param factory the new QDrag factory to use 0119 */ 0120 void setDragObjectFactory(DragObjectFactory factory) 0121 { 0122 m_factory = factory; 0123 } 0124 0125 private: 0126 /** 0127 * Reimplemented to use the QDrag factory 0128 */ 0129 QDrag *dragObject() override 0130 { 0131 if (m_factory) { 0132 Widget *w = static_cast<Widget *>(decoratedWidget()); 0133 return m_factory(w); 0134 } else { 0135 return KDragWidgetDecoratorBase::dragObject(); 0136 } 0137 } 0138 0139 DragObjectFactory m_factory; 0140 }; 0141 0142 #endif // KDRAGWIDGETDECORATOR_H