File indexing completed on 2025-01-19 03:56:06
0001 /* ============================================================ 0002 * 0003 * This file is a part of digiKam project 0004 * https://www.digikam.org 0005 * 0006 * Date : 2010-12-02 0007 * Description : Generic, standard item based model for DCategorizedView 0008 * 0009 * SPDX-FileCopyrightText: 2010-2011 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de> 0010 * 0011 * SPDX-License-Identifier: GPL-2.0-or-later 0012 * 0013 * ============================================================ */ 0014 0015 #ifndef DIGIKAM_CATEGORIZED_ITEM_MODEL_H 0016 #define DIGIKAM_CATEGORIZED_ITEM_MODEL_H 0017 0018 // Qt includes 0019 0020 #include <QFlags> 0021 #include <QStandardItemModel> 0022 0023 // Local includes 0024 0025 #include "digikam_export.h" 0026 #include "dcategorizedsortfilterproxymodel.h" 0027 0028 class QAction; 0029 0030 namespace Digikam 0031 { 0032 0033 class DIGIKAM_EXPORT CategorizedItemModel : public QStandardItemModel 0034 { 0035 Q_OBJECT 0036 0037 public: 0038 0039 enum ExtraRoles 0040 { 0041 /// This role, per default, reflects the order in which items are added 0042 ItemOrderRole = Qt::UserRole + 1 0043 }; 0044 0045 public: 0046 0047 explicit CategorizedItemModel(QObject* const parent = nullptr); 0048 0049 QStandardItem* addItem(const QString& text, const QVariant& category, const QVariant& categorySorting = QVariant()); 0050 QStandardItem* addItem(const QString& text, const QIcon& decoration, const QVariant& category, 0051 const QVariant& categorySorting = QVariant()); 0052 0053 virtual DCategorizedSortFilterProxyModel* createFilterModel(); 0054 }; 0055 0056 // ----------------------------------------------------------------------------------------------------------------------- 0057 0058 class DIGIKAM_EXPORT ActionItemModel : public CategorizedItemModel 0059 { 0060 Q_OBJECT 0061 0062 public: 0063 0064 enum ExtraRoles 0065 { 0066 ItemActionRole = Qt::UserRole + 10 0067 }; 0068 0069 enum MenuCategoryFlag 0070 { 0071 /// The toplevel menu's text is used as category 0072 ToplevelMenuCategory = 1 << 0, 0073 0074 /// If the action is in a submenu, this menu's text is taken as category 0075 ParentMenuCategory = 1 << 1, 0076 0077 /// Sort categories alphabetically by category name 0078 SortCategoriesAlphabetically = 1 << 10, 0079 0080 /// Sort categories by the order they are added (found in the scanned menu) 0081 SortCategoriesByInsertionOrder = 1 << 11 0082 }; 0083 Q_DECLARE_FLAGS(MenuCategoryMode, MenuCategoryFlag) 0084 0085 public: 0086 0087 /** 0088 * This class is a CategorizedItemModel based on QActions, taking an action's text and icon 0089 * for display and decoration. 0090 * It is possible to retrieve an action for an index, and to call the action's slots from 0091 * a given index. 0092 */ 0093 explicit ActionItemModel(QObject* const parent = nullptr); 0094 0095 QStandardItem* addAction(QAction* action, const QString& category, const QVariant& categorySorting = QVariant()); 0096 0097 void setMode(MenuCategoryMode mode); 0098 MenuCategoryMode mode() const; 0099 0100 void addActions(QWidget* widget); 0101 void addActions(QWidget* widget, const QList<QAction*>& actionWhiteList); 0102 0103 DCategorizedSortFilterProxyModel* createFilterModel() override; 0104 0105 /** 0106 * Returns the action for the given index. 0107 * Note: these methods perform O(n). 0108 */ 0109 QStandardItem* itemForAction(QAction* action) const; 0110 QModelIndex indexForAction(QAction* action) const; 0111 0112 /** 0113 * Returns the action for the given index. 0114 * The method can also be used for indices from proxy models. 0115 */ 0116 static QAction* actionForIndex(const QModelIndex& index); 0117 0118 public Q_SLOTS: 0119 0120 /** 0121 * These three slots will cause the slots of the referred action to be called. 0122 * Connect here for example a view's signals. 0123 * Note that you can also pass indices from proxy models. 0124 */ 0125 void hover(const QModelIndex& index); 0126 void toggle(const QModelIndex& index); 0127 void trigger(const QModelIndex& index); 0128 0129 protected Q_SLOTS: 0130 0131 void slotActionChanged(); 0132 0133 protected: 0134 0135 void setPropertiesFromAction(QStandardItem* item, QAction* action); 0136 0137 protected: 0138 0139 MenuCategoryMode m_mode; 0140 DCategorizedSortFilterProxyModel* m_filterModel; 0141 }; 0142 0143 } // namespace Digikam 0144 0145 Q_DECLARE_OPERATORS_FOR_FLAGS(Digikam::ActionItemModel::MenuCategoryMode) 0146 0147 #endif // DIGIKAM_CATEGORIZED_ITEM_MODEL_H