BibleTime
btopenworkaction.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-2021 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 "btopenworkaction.h"
14 
15 #include <QAbstractItemModel>
16 #include <QVariant>
17 #include <QtGlobal>
18 #include <utility>
19 #include "../backend/bookshelfmodel/btbookshelffiltermodel.h"
20 #include "../backend/bookshelfmodel/btbookshelfmodel.h"
21 #include "../backend/bookshelfmodel/btbookshelftreemodel.h"
22 #include "../backend/config/btconfigcore.h"
23 #include "../backend/managers/cswordbackend.h"
24 #include "../util/btconnect.h"
25 #include "../util/cresmgr.h"
27 
28 
30  QString groupingConfigKey,
31  QWidget * parent)
32  : BtMenuView(parent)
33  , m_treeModel(nullptr)
34  , m_postFilterModel(nullptr)
35  , m_groupingMenu(nullptr)
36  , m_groupingConfigGroup(std::move(groupingConfigGroup))
37  , m_groupingConfigKey(std::move(groupingConfigKey))
38 {
39  // Setup models:
42  this);
44  m_postFilterModel->setSourceModel(m_treeModel);
46 
48  [this](QModelIndex const & index) {
49  static constexpr auto const MPR =
51  if (auto * const i =
52  static_cast<CSwordModuleInfo *>(
53  model()->data(index, MPR).value<void *>()))
54  Q_EMIT triggered(i);
55  });
56 }
57 
59  std::shared_ptr<QAbstractItemModel> model)
60 { m_treeModel->setSourceModel(std::move(model)); }
61 
63  if (!m_groupingMenu)
64  return;
65  m_groupingMenu->setTitle(tr("&Grouping order"));
66  m_groupingMenu->setStatusTip(tr("Sets the grouping order for the items in "
67  "this menu."));
68 }
69 
70 void BtOpenWorkActionMenu::postBuildMenu(QActionGroup * actions) {
71  Q_UNUSED(actions)
72  addSeparator();
73  m_groupingMenu = new BtBookshelfGroupingMenu(false, this);
74 
77  [this](BtBookshelfTreeModel::Grouping const & grouping) {
78  m_treeModel->setGroupingOrder(grouping);
80  });
81 
82  retranslateUi();
83  addMenu(m_groupingMenu);
84 }
85 
87  QString groupingConfigKey,
88  QObject * parent)
89  : QAction(parent)
90 {
91  m_menu = new BtOpenWorkActionMenu(std::move(groupingConfigGroup),
92  std::move(groupingConfigKey));
94 
95  setMenu(m_menu);
96  setIcon(CResMgr::mainWindow::icon_openAction());
97  retranslateUi();
98 
99  auto slotModelChanged =
100  [this]{ setEnabled(m_menu->postFilterModel()->rowCount() > 0); };
101 
102  slotModelChanged();
103 
107  BT_CONNECT(filterModel, &BtBookshelfFilterModel::layoutChanged,
108  slotModelChanged);
109  BT_CONNECT(filterModel, &BtBookshelfFilterModel::modelReset,
110  slotModelChanged);
111  BT_CONNECT(filterModel, &BtBookshelfFilterModel::rowsInserted,
112  slotModelChanged);
113  BT_CONNECT(filterModel, &BtBookshelfFilterModel::rowsRemoved,
114  std::move(slotModelChanged));
115 }
116 
118  delete m_menu;
119 }
120 
122  setText(tr("&Open work"));
123 }
#define BT_CONNECT(...)
Definition: btconnect.h:20
void signalGroupingOrderChanged(const BtBookshelfTreeModel::Grouping &)
void saveTo(BtConfigCore &config, QString const &key) const
void setGroupingOrder(const BtBookshelfTreeModel::Grouping &groupingOrder, bool emitSignal=true)
void setSourceModel(std::shared_ptr< QAbstractItemModel > sourceModel)
QAbstractItemModel * model() const
Definition: btmenuview.h:66
void triggered(QModelIndex index)
void setModel(QAbstractItemModel *model)
Definition: btmenuview.cpp:50
QString const m_groupingConfigKey
void setSourceModel(std::shared_ptr< QAbstractItemModel > model)
void triggered(CSwordModuleInfo *module)
BtBookshelfGroupingMenu * m_groupingMenu
BtConfigCore m_groupingConfigGroup
BtBookshelfFilterModel * m_postFilterModel
BtOpenWorkActionMenu(BtConfigCore groupingConfigGroup, QString groupingConfigKey, QWidget *parent=nullptr)
void postBuildMenu(QActionGroup *actions) override
BtBookshelfTreeModel * m_treeModel
BtBookshelfFilterModel * postFilterModel() const
void triggered(CSwordModuleInfo *module)
BtOpenWorkActionMenu * m_menu
BtOpenWorkAction(BtConfigCore groupingConfigGroup, QString groupingConfigKey, QObject *parent=nullptr)
~BtOpenWorkAction() override
static CSwordBackend & instance() noexcept
Definition: cswordbackend.h:98