BibleTime
btmodulechoosermenu.cpp
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-2026 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#include "btmodulechoosermenu.h"
14
15#include <QAction>
16#include <QActionGroup>
17#include "../../backend/bookshelfmodel/btbookshelffiltermodel.h"
18#include "../../backend/drivers/cswordmoduleinfo.h"
19#include "../../util/btassert.h"
20#include "../../util/btconnect.h"
21#include "../btbookshelfdockwidget.h"
22#include "../btmenuview.h"
23
24
26 QString const & title,
28 Flags flags,
29 QWidget * parent)
30 : BtMenuView(title, parent)
31 , m_filterModel(new BtBookshelfFilterModel(this))
32 , m_flags(flags)
33{
35 m_filterModel->setSourceModel(
38
40 [this](QModelIndex const itemIndex) {
41 Q_EMIT sigModuleChosen(m_filterModel->module(itemIndex));
42 });
43}
44
45void BtModuleChooserMenu::preBuildMenu(QActionGroup * actionGroup) {
46 BT_ASSERT(actionGroup);
47 actionGroup->setExclusive(true);
48
49 if (m_flags & AddNoneButton) {
50 QAction * noneAction = new QAction(this);
51 noneAction->setCheckable(true);
52 noneAction->setText(tr("NONE"));
53 noneAction->setChecked(!m_selectedModule);
54 noneAction->setDisabled(
55 m_newModulesToUse.size() <= 1
56 || (m_buttonIndex <= 0 && m_leftLikeModules <= 1));
57 noneAction->setActionGroup(actionGroup);
58 BT_CONNECT(noneAction, &QAction::triggered,
59 [this]{ Q_EMIT triggered(QModelIndex()); });
60 addAction(noneAction);
61
62 addSeparator();
63 }
64}
65
67 QModelIndex const & itemIndex)
68{
69 auto * const action = BtMenuView::newAction(parentMenu, itemIndex);
70 auto * const module = m_filterModel->module(itemIndex);
71 BT_ASSERT(module);
72 action->setCheckable(true);
73 action->setChecked(module == m_selectedModule);
74
76 action->setDisabled(module == m_selectedModule
77 || m_newModulesToUse.contains(module)
78 || module->isLocked());
79 } else {
80 action->setDisabled((module != m_selectedModule
81 && m_newModulesToUse.contains(module))
82 || module->isLocked());
83 }
84
85 // Disable non-Bible modules on first button:
87 && m_buttonIndex <= 0
89 && module->category() != CSwordModuleInfo::Category::Bibles)
90 action->setDisabled(true);
91
92 return action;
93}
94
96 CSwordModuleInfo * newSelectedModule,
97 int newButtonIndexIndex,
98 int newLeftLikeModules)
99{
100 m_newModulesToUse = newModulesToUse;
101 m_selectedModule = newSelectedModule;
102 m_buttonIndex = newButtonIndexIndex;
103 m_leftLikeModules = newLeftLikeModules;
104 rebuildMenu();
105}
106
113
115 CSwordModuleInfo * const newSelectedModule) noexcept
116{
117 m_selectedModule = newSelectedModule;
118 rebuildMenu();
119}
#define BT_ASSERT(...)
Definition btassert.h:17
#define BT_CONNECT(...)
Definition btconnect.h:20
QList< CSwordModuleInfo * > BtModuleList
static BtBookshelfDockWidget * getInstance()
CSwordModuleInfo * module(QModelIndex const &index) const noexcept
std::optional< CSwordModuleInfo::ModuleType > const & moduleChooserType() const noexcept
void setModuleChooserType(std::optional< CSwordModuleInfo::ModuleType > type)
void triggered(QModelIndex index)
void rebuildMenu()
Rebuilds the menu.
virtual QAction * newAction(QMenu *parentMenu, const QModelIndex &itemIndex)
void setModel(QAbstractItemModel *model)
QAction * newAction(QMenu *parentMenu, QModelIndex const &itemIndex) final override
void update(BtModuleList newModulesToUse, CSwordModuleInfo *newSelectedModule, int newButtonIndexIndex, int newLeftLikeModules)
BtBookshelfFilterModel *const m_filterModel
void preBuildMenu(QActionGroup *actionGroup) final override
void sigModuleChosen(CSwordModuleInfo *module)
CSwordModuleInfo * m_selectedModule
BtModuleChooserMenu(QString const &title, CSwordModuleInfo::ModuleType moduleType, Flags flags, QWidget *parent=nullptr)
void setSelectedModule(CSwordModuleInfo *const newSelectedModule) noexcept
static QIcon const & categoryIconAdd(CSwordModuleInfo::Category category)
static QIcon const & categoryIcon(CSwordModuleInfo::Category category)
CSwordModuleInfo::Category category() const