BibleTime
btmenuview.h
Go to the documentation of this file.
1/*********
2*
3* In the name of the Father, and of the Son, and of the Holy Spirit.
4*
5* This file is part of BibleTime's source code, https://bibletime.info/
6*
7* Copyright 1999-2025 by the BibleTime developers.
8* The BibleTime source code is licensed under the GNU General Public License
9* version 2.0.
10*
11**********/
12
13#pragma once
14
15#include <QMenu>
16
17#include <QObject>
18#include <QString>
19
20
22class QAction;
23class QActionGroup;
24class QEvent;
25class QModelIndex;
26class QWidget;
27
28/**
29 This is a special menu, which shows the contents of an item model. The menu is repopulated
30 with data from the item model each time before its shown, and it does not in any other way
31 react to changes in the model.
32
33 The menu is built from items in the model which are below the given parent index. By
34 default this parent index is invalid. When the menu is about to show, all items directly
35 below the parent index are inserted. If a child item has children of its own it is inserted
36 as a QMenu which will be built recursively. Otherwise the child item is inserted as a
37 QAction which may be triggered by the user.
38
39 When subclassing this menu, reimplement preBuildMenu() and postBuildMenu() to add new menu
40 items before or after the menu is populated with data from the associated item model. You
41 can also reimplement newAction() and newMenu() if you further want to tune the appearance
42 or behaviour of this menu.
43
44 \warning This menu might not properly handle changes in the source model while being shown,
45 so beware to check whether the index emitted by triggered() is valid.
46*/
47class BtMenuView: public QMenu {
48 Q_OBJECT
49 public:
50 BtMenuView(QWidget * parent = nullptr);
51 BtMenuView(QString const & title, QWidget * parent = nullptr);
52 ~BtMenuView() override;
53
54 /**
55 Sets or resets the data model for this menu and resets the parent index to an
56 invalid index.
57 \param[in] model Pointer to the data model to represent.
58 \warning Do not (re)set the model when the menu is being shown!
59 */
61
62 /**
63 Returns a pointer to the data model associated with this menu.
64 \retval 0 If this menu is not associated to any model.
65 */
66 QAbstractItemModel * model() const { return m_model; }
67
68 bool event(QEvent * e) override;
69
70 Q_SIGNALS:
71 /**
72 This signal is emitted when the user activates a menu item corresponding to an
73 index in the associated model.
74 \param index The index of the model which was activated.
75 */
76 void triggered(QModelIndex index);
77
78 protected:
79 /**
80 This method is called by BtMenuView before populating itself with data from the
81 model. Reimplement this method to add any menus/actions to this menu before the
82 items of the menu. The default implementation does nothing.
83
84 The model might be unset before this method is called. When the menu is about to be
85 shown, this allows for this method to initialize the model on request. If the model
86 is unset after this method returns, the menu is not populated with data from the
87 item model.
88 */
89 virtual void preBuildMenu(QActionGroup * actions);
90
91 /**
92 This method is called by BtMenuView after populating itself with data from the
93 model. If there was no model set, this method is still called after preBuildMenu().
94 Reimplement this method to add any menus/actions to this menu after the items of
95 the menu. The default implementation does nothing.
96 */
97 virtual void postBuildMenu(QActionGroup * actions);
98
99 /**
100 This method is called by BtMenuView to initialize an action to add to this menu. If
101 the action corresponding to the given index is not to be added to this menu, please
102 return 0.
103 \param[in] parentMenu the parent menu under which the new action is to be added.
104 \param[in] itemIndex the index of the item corresponding to the action.
105 */
106 virtual QAction *newAction(QMenu *parentMenu, const QModelIndex &itemIndex);
107
108 /**
109 This method is called by BtMenuView to initialize a menu to add to this menu. If
110 the menu corresponding to the given index is not to be added to this menu, please
111 return 0. If the menu should not be populated by BtMenuView itself, please use
112 setProperty("BtMenuView_NoPopulate", true) on the menu to be returned by this
113 method.
114 \param[in] parentMenu the parent menu under which the new menu is to be added.
115 \param[in] itemIndex the index of the item corresponding to the menu.
116 */
117 virtual QMenu *newMenu(QMenu *parentMenu, const QModelIndex &itemIndex);
118
119 /** \brief Rebuilds the menu. */
120 void rebuildMenu();
121
122 private: // methods:
123
124 void buildMenu(QMenu *parentMenu, const QModelIndex &parentIndex);
125 void removeMenus();
126
127 private: // fields:
128
130 QActionGroup *m_actions;
131
132};
virtual QMenu * newMenu(QMenu *parentMenu, const QModelIndex &itemIndex)
~BtMenuView() override
void triggered(QModelIndex index)
QAbstractItemModel * model() const
Definition btmenuview.h:66
QAbstractItemModel * m_model
Definition btmenuview.h:129
void buildMenu(QMenu *parentMenu, const QModelIndex &parentIndex)
void rebuildMenu()
Rebuilds the menu.
QActionGroup * m_actions
Definition btmenuview.h:130
virtual void preBuildMenu(QActionGroup *actions)
virtual QAction * newAction(QMenu *parentMenu, const QModelIndex &itemIndex)
void removeMenus()
void setModel(QAbstractItemModel *model)
bool event(QEvent *e) override
virtual void postBuildMenu(QActionGroup *actions)