BibleTime
Signals | Public Member Functions | Protected Member Functions | Private Member Functions | Private Attributes | List of all members
BtMenuView Class Reference

#include <btmenuview.h>

Inheritance diagram for BtMenuView:

Signals

void triggered (QModelIndex index)
 

Public Member Functions

 BtMenuView (QString const &title, QWidget *parent=nullptr)
 
 BtMenuView (QWidget *parent=nullptr)
 
bool event (QEvent *e) override
 
QAbstractItemModelmodel () const
 
void setModel (QAbstractItemModel *model)
 
 ~BtMenuView () override
 

Protected Member Functions

virtual QActionnewAction (QMenu *parentMenu, const QModelIndex &itemIndex)
 
virtual QMenunewMenu (QMenu *parentMenu, const QModelIndex &itemIndex)
 
virtual void postBuildMenu (QActionGroup *actions)
 
virtual void preBuildMenu (QActionGroup *actions)
 
void rebuildMenu ()
 Rebuilds the menu. More...
 

Private Member Functions

void buildMenu (QMenu *parentMenu, const QModelIndex &parentIndex)
 
void removeMenus ()
 

Private Attributes

QActionGroup * m_actions
 
QAbstractItemModelm_model
 

Detailed Description

This is a special menu, which shows the contents of an item model. The menu is repopulated with data from the item model each time before its shown, and it does not in any other way react to changes in the model.

The menu is built from items in the model which are below the given parent index. By default this parent index is invalid. When the menu is about to show, all items directly below the parent index are inserted. If a child item has children of its own it is inserted as a QMenu which will be built recursively. Otherwise the child item is inserted as a QAction which may be triggered by the user.

When subclassing this menu, reimplement preBuildMenu() and postBuildMenu() to add new menu items before or after the menu is populated with data from the associated item model. You can also reimplement newAction() and newMenu() if you further want to tune the appearance or behaviour of this menu.

Warning
This menu might not properly handle changes in the source model while being shown, so beware to check whether the index emitted by triggered() is valid.

Definition at line 47 of file btmenuview.h.

Constructor & Destructor Documentation

◆ BtMenuView() [1/2]

BtMenuView::BtMenuView ( QWidget parent = nullptr)

Definition at line 36 of file btmenuview.cpp.

◆ BtMenuView() [2/2]

BtMenuView::BtMenuView ( QString const &  title,
QWidget parent = nullptr 
)

Definition at line 40 of file btmenuview.cpp.

◆ ~BtMenuView()

BtMenuView::~BtMenuView ( )
override

Definition at line 46 of file btmenuview.cpp.

References m_actions.

Member Function Documentation

◆ buildMenu()

void BtMenuView::buildMenu ( QMenu parentMenu,
const QModelIndex &  parentIndex 
)
private

◆ event()

bool BtMenuView::event ( QEvent *  e)
override

◆ model()

QAbstractItemModel* BtMenuView::model ( ) const
inline

Returns a pointer to the data model associated with this menu.

Return values
0If this menu is not associated to any model.

Definition at line 66 of file btmenuview.h.

References m_model.

Referenced by BtOpenWorkActionMenu::BtOpenWorkActionMenu(), setModel(), and BtOpenWorkActionMenu::setSourceModel().

◆ newAction()

QAction * BtMenuView::newAction ( QMenu parentMenu,
const QModelIndex &  itemIndex 
)
protectedvirtual

This method is called by BtMenuView to initialize an action to add to this menu. If the action corresponding to the given index is not to be added to this menu, please return 0.

Parameters
[in]parentMenuthe parent menu under which the new action is to be added.
[in]itemIndexthe index of the item corresponding to the action.

Reimplemented in BtModuleChooserMenu.

Definition at line 93 of file btmenuview.cpp.

References m_model.

Referenced by buildMenu(), and BtModuleChooserMenu::newAction().

◆ newMenu()

QMenu * BtMenuView::newMenu ( QMenu parentMenu,
const QModelIndex &  itemIndex 
)
protectedvirtual

This method is called by BtMenuView to initialize a menu to add to this menu. If the menu corresponding to the given index is not to be added to this menu, please return 0. If the menu should not be populated by BtMenuView itself, please use setProperty("BtMenuView_NoPopulate", true) on the menu to be returned by this method.

Parameters
[in]parentMenuthe parent menu under which the new menu is to be added.
[in]itemIndexthe index of the item corresponding to the menu.

Definition at line 143 of file btmenuview.cpp.

References m_model.

Referenced by buildMenu().

◆ postBuildMenu()

void BtMenuView::postBuildMenu ( QActionGroup *  actions)
protectedvirtual

This method is called by BtMenuView after populating itself with data from the model. If there was no model set, this method is still called after preBuildMenu(). Reimplement this method to add any menus/actions to this menu after the items of the menu. The default implementation does nothing.

Reimplemented in BtOpenWorkActionMenu.

Definition at line 89 of file btmenuview.cpp.

Referenced by rebuildMenu().

◆ preBuildMenu()

void BtMenuView::preBuildMenu ( QActionGroup *  actions)
protectedvirtual

This method is called by BtMenuView before populating itself with data from the model. Reimplement this method to add any menus/actions to this menu before the items of the menu. The default implementation does nothing.

The model might be unset before this method is called. When the menu is about to be shown, this allows for this method to initialize the model on request. If the model is unset after this method returns, the menu is not populated with data from the item model.

Reimplemented in BtModuleChooserMenu.

Definition at line 85 of file btmenuview.cpp.

Referenced by rebuildMenu().

◆ rebuildMenu()

void BtMenuView::rebuildMenu ( )
protected

◆ removeMenus()

void BtMenuView::removeMenus ( )
private

Definition at line 237 of file btmenuview.cpp.

Referenced by rebuildMenu().

◆ setModel()

void BtMenuView::setModel ( QAbstractItemModel model)

Sets or resets the data model for this menu and resets the parent index to an invalid index.

Parameters
[in]modelPointer to the data model to represent.
Warning
Do not (re)set the model when the menu is being shown!

Definition at line 50 of file btmenuview.cpp.

References BT_CONNECT, m_model, model(), anonymous_namespace{btmenuview.cpp}::rebuildEventType, and rebuildMenu().

Referenced by BtModuleChooserMenu::BtModuleChooserMenu(), and BtOpenWorkActionMenu::BtOpenWorkActionMenu().

◆ triggered

void BtMenuView::triggered ( QModelIndex  index)
signal

This signal is emitted when the user activates a menu item corresponding to an index in the associated model.

Parameters
indexThe index of the model which was activated.

Referenced by BtModuleChooserMenu::BtModuleChooserMenu(), BtOpenWorkActionMenu::BtOpenWorkActionMenu(), BtModuleChooserMenu::preBuildMenu(), and rebuildMenu().

Member Data Documentation

◆ m_actions

QActionGroup* BtMenuView::m_actions
private

Definition at line 130 of file btmenuview.h.

Referenced by buildMenu(), rebuildMenu(), and ~BtMenuView().

◆ m_model

QAbstractItemModel* BtMenuView::m_model
private

Definition at line 129 of file btmenuview.h.

Referenced by buildMenu(), model(), newAction(), newMenu(), rebuildMenu(), and setModel().


The documentation for this class was generated from the following files: