File indexing completed on 2025-01-05 04:47:09

0001 /*
0002     SPDX-FileCopyrightText: 2006-2007 Volker Krause <vkrause@kde.org>
0003     SPDX-FileCopyrightText: 2008 Stephen Kelly <steveire@gmail.com>
0004     SPDX-FileCopyrightText: 2012-2024 Laurent Montel <montel@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-or-later
0007 */
0008 
0009 #pragma once
0010 
0011 #include "akonadiwidgets_export.h"
0012 
0013 #include <QTreeView>
0014 
0015 #include <memory>
0016 
0017 class KXMLGUIClient;
0018 class QDragMoveEvent;
0019 
0020 namespace Akonadi
0021 {
0022 class Collection;
0023 class Item;
0024 class EntityTreeViewPrivate;
0025 
0026 /**
0027  * @short A view to show an item/collection tree provided by an EntityTreeModel.
0028  *
0029  * When a KXmlGuiWindow is passed to the constructor, the XMLGUI
0030  * defined context menu @c akonadi_collectionview_contextmenu or
0031  * @c akonadi_itemview_contextmenu is used if available.
0032  *
0033  * Example:
0034  *
0035  * @code
0036  *
0037  * using namespace Akonadi;
0038  *
0039  * class MyWindow : public KXmlGuiWindow
0040  * {
0041  *   public:
0042  *    MyWindow()
0043  *      : KXmlGuiWindow()
0044  *    {
0045  *      EntityTreeView *view = new EntityTreeView( this, this );
0046  *      setCentralWidget( view );
0047  *
0048  *      EntityTreeModel *model = new EntityTreeModel( ... );
0049  *      view->setModel( model );
0050  *    }
0051  * }
0052  *
0053  * @endcode
0054  *
0055  * @author Volker Krause <vkrause@kde.org>
0056  * @author Stephen Kelly <steveire@gmail.com>
0057  * @since 4.4
0058  */
0059 class AKONADIWIDGETS_EXPORT EntityTreeView : public QTreeView
0060 {
0061     Q_OBJECT
0062 
0063 public:
0064     /**
0065      * Creates a new entity tree view.
0066      *
0067      * @param parent The parent widget.
0068      */
0069     explicit EntityTreeView(QWidget *parent = nullptr);
0070 
0071     /**
0072      * Creates a new entity tree view.
0073      *
0074      * @param xmlGuiClient The KXMLGUIClient the view is used in.
0075      *                     This is needed for the XMLGUI based context menu.
0076      *                     Passing 0 is ok and will disable the builtin context menu.
0077      * @param parent The parent widget.
0078      */
0079     explicit EntityTreeView(KXMLGUIClient *xmlGuiClient, QWidget *parent = nullptr);
0080 
0081     /**
0082      * Destroys the entity tree view.
0083      */
0084     ~EntityTreeView() override;
0085 
0086     /**
0087      * Sets the XML GUI client which the view is used in.
0088      *
0089      * This is needed if you want to use the built-in context menu.
0090      *
0091      * @param xmlGuiClient The KXMLGUIClient the view is used in.
0092      */
0093     void setXmlGuiClient(KXMLGUIClient *xmlGuiClient);
0094 
0095     /**
0096      * Return the XML GUI client which the view is used in.
0097      * @since 4.12
0098      */
0099     KXMLGUIClient *xmlGuiClient() const;
0100 
0101     /**
0102      * @reimp
0103      * @param model the model to set
0104      */
0105     void setModel(QAbstractItemModel *model) override;
0106 
0107     /**
0108      * Sets whether the drop action menu is @p enabled and will
0109      * be shown on drop operation.
0110      * @param enabled enables drop action menu if set as @c true
0111      * @since 4.5
0112      */
0113     void setDropActionMenuEnabled(bool enabled);
0114 
0115     /**
0116      * Returns whether the drop action menu is enabled and will
0117      * be shown on drop operation.
0118      *
0119      * @since 4.5
0120      */
0121     [[nodiscard]] bool isDropActionMenuEnabled() const;
0122 
0123     /**
0124      * Return true if we use an manual sorting
0125      * Necessary to fix dnd menu
0126      * We must show just move when we move item between two items
0127      * When automatic no show dnd menu between two items.
0128      * @since 4.8.1
0129      */
0130     [[nodiscard]] bool isManualSortingActive() const;
0131 
0132     /**
0133      * Set true if we automatic sorting
0134      * @param active enables automatic sorting if set as @c true
0135      * @since 4.8.1
0136      */
0137     void setManualSortingActive(bool active);
0138 
0139     /**
0140      * Set the name of the default popup menu (retrieved from the
0141      * application's XMLGUI file).
0142      *
0143      * This menu is used as a fallback if the context of the menu request
0144      * is neither an item nor a collection, e.g. the click is on an empty
0145      * area inside the view.  If the click is over an entry in the view,
0146      * the menu which is applicable to the clicked entry (either an Item
0147      * or a Collection) is used.
0148      *
0149      * @param name The name of the popup menu
0150      *
0151      * @since 4.9
0152      * @note For backwards compatibility, the default is the standard
0153      * collection popup menu, "akonadi_collectionview_contextmenu".
0154      * @see KXMLGUIClient, KXMLGUIFactory::container()
0155      */
0156     void setDefaultPopupMenu(const QString &name);
0157 
0158 Q_SIGNALS:
0159     /**
0160      * This signal is emitted whenever the user has clicked
0161      * a collection in the view.
0162      *
0163      * @param collection The clicked collection.
0164      */
0165     void clicked(const Akonadi::Collection &collection);
0166 
0167     /**
0168      * This signal is emitted whenever the user has clicked
0169      * an item in the view.
0170      *
0171      * @param item The clicked item.
0172      */
0173     void clicked(const Akonadi::Item &item);
0174 
0175     /**
0176      * This signal is emitted whenever the user has double clicked
0177      * a collection in the view.
0178      *
0179      * @param collection The double clicked collection.
0180      */
0181     void doubleClicked(const Akonadi::Collection &collection);
0182 
0183     /**
0184      * This signal is emitted whenever the user has double clicked
0185      * an item in the view.
0186      *
0187      * @param item The double clicked item.
0188      */
0189     void doubleClicked(const Akonadi::Item &item);
0190 
0191     /**
0192      * This signal is emitted whenever the current collection
0193      * in the view has changed.
0194      *
0195      * @param collection The new current collection.
0196      */
0197     void currentChanged(const Akonadi::Collection &collection);
0198 
0199     /**
0200      * This signal is emitted whenever the current item
0201      * in the view has changed.
0202      *
0203      * @param item The new current item.
0204      */
0205     void currentChanged(const Akonadi::Item &item);
0206 
0207 protected:
0208     using QTreeView::currentChanged;
0209 #ifndef QT_NO_DRAGANDDROP
0210     void startDrag(Qt::DropActions supportedActions) override;
0211     void dragMoveEvent(QDragMoveEvent *event) override;
0212     void dropEvent(QDropEvent *event) override;
0213 #endif
0214     void timerEvent(QTimerEvent *event) override;
0215 #ifndef QT_NO_CONTEXTMENU
0216     void contextMenuEvent(QContextMenuEvent *event) override;
0217 #endif
0218 
0219 private:
0220     /// @cond PRIVATE
0221     std::unique_ptr<EntityTreeViewPrivate> const d;
0222     /// @endcond
0223 };
0224 
0225 }