BibleTime
btmodulechooserbar.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 "btmodulechooserbar.h"
14 
15 #include <QAction>
16 #include <Qt>
17 #include <utility>
18 #include "../../util/btconnect.h"
19 #include "../../util/btmodules.h"
20 #include "../displaywindow/cdisplaywindow.h"
21 #include "btmodulechooserbutton.h"
22 
23 
25  : QToolBar(parent)
26 {
27  setAllowedAreas(Qt::TopToolBarArea);
28  setFloatable(false);
29 }
30 
33  adjustButtonCount(modules.size() + 1,
34  static_cast<CDisplayWindow *>(sender()));
35  updateButtonMenus(modules);
36 }
37 
38 void BtModuleChooserBar::adjustButtonCount(int const numButtons,
39  CDisplayWindow * const window)
40 {
41  if (m_buttonList.size() > numButtons) {
42  auto toRemove = m_buttonList.size() - numButtons;
43  do {
44  delete m_buttonList.takeFirst();
45  } while (--toRemove);
46  } else if (numButtons > m_buttonList.size()) {
47  auto toAdd = numButtons - m_buttonList.size();
48  do {
49  addButton(window);
50  } while (--toAdd);
51  }
52 }
53 
57  QAction* a = addWidget(b);
58  m_buttonList.append(b);
59 
60  /* The button sends signals directly to the window which then signals back
61  when the module list has changed. Changes to the module list may mean
62  deletion of the buttons, hence we must queue these signals. Otherwise,
63  when triggered via QAction::triggered(), a mouse release event on the
64  widget with that QAction may follow, but the widget might already have
65  been deleted. */
68  Qt::QueuedConnection);
71  Qt::QueuedConnection);
74  Qt::QueuedConnection);
75 
76  a->setVisible(true);
77  return b;
78 }
79 
80 
81 /** Sets the modules which are chosen in this module chooser bar. */
83  auto const & modules = window->modules();
84  m_moduleType = modules.first()->type();
85 
86  clear();
87  qDeleteAll(m_buttonList);
88  m_buttonList.clear();
89 
90  //if (!useModules.count()) return;
91  for (int i = 0; i < modules.size(); i++) {
92  addButton(window);
93  }
95  addButton(window); // for ADD button
96  } else {
97  BT_ASSERT(modules.size() == 1);
98  }
99  updateButtonMenus(modules);
100 
102  this, qOverload<BtModuleList>(&BtModuleChooserBar::setModules));
103 }
104 
106  int const leftLikeModules = leftLikeParallelModules(modules);
107  for (int i = 0; i < m_buttonList.count(); i++)
108  m_buttonList.at(i)->updateMenu(modules,
109  (i >= modules.size())
110  ? nullptr
111  : modules.at(i),
112  i,
113  leftLikeModules);
114 }
#define BT_ASSERT(...)
Definition: btassert.h:17
#define BT_CONNECT(...)
Definition: btconnect.h:20
QList< CSwordModuleInfo * > BtModuleList
Definition: btmodulelist.h:20
int leftLikeParallelModules(BtModuleList const &modules)
Definition: btmodules.cpp:19
void updateButtonMenus(BtModuleList const &modules)
void associateWithWindow(CDisplayWindow *window)
BtModuleChooserBar(QWidget *parent)
QList< BtModuleChooserButton * > m_buttonList
CSwordModuleInfo::ModuleType m_moduleType
void adjustButtonCount(int const numButtons, CDisplayWindow *const window)
Ensures exactly numButtons buttons..
void setModules(BtModuleList newModules)
BtModuleChooserButton * addButton(CDisplayWindow *const window)
Adds an empty button to the toolbar.
void sigModuleAdd(int index, CSwordModuleInfo *module)
void sigModuleReplace(int index, CSwordModuleInfo *newModule)
void sigModuleRemove(int index)
The base class for all display windows of BibleTime.
BtModuleList const & modules() const noexcept
void slotAddModule(int index, CSwordModuleInfo *module)
void sigModuleListChanged(BtModuleList newList)
void slotRemoveModule(int index)
void slotReplaceModule(int index, CSwordModuleInfo *newModule)