BibleTime
bttoolbarpopupaction.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 "bttoolbarpopupaction.h"
14 
15 #include <QEvent>
16 #include <QMenu>
17 #include <QToolButton>
18 #include "../../util/btconnect.h"
19 
20 
21 namespace {
22 
23 class BtToolButton: public QToolButton {
24  public:
25  BtToolButton(QWidget *parent = nullptr)
26  : QToolButton(parent) {}
27  private:
28  void nextCheckState() override {}
29 };
30 
31 } // anonymous namespace
32 
33 
34 // This class provides a toolbar widget that has a icon plus a right side down arrow
35 // The icon is typically set to a back or forward arrow and the down arrow has a popup
36 // menu when clicked. The menu is typicallly populated with history actions.
38  QString const & text,
39  QObject * parent)
40  : QWidgetAction(parent)
41  , m_menu(std::make_unique<QMenu>())
42  , m_icon(icon)
43  , m_text(text)
44 { setText(text); }
45 
47 
48 // return the QMenu object so a popup menu can be constructed
49 QMenu * BtToolBarPopupAction::popupMenu() const { return m_menu.get(); }
50 
52  auto * const button = new BtToolButton(parent);
53  setIcon(m_icon);
54  setToolTip(m_text);
55  button->setDefaultAction(this);
56  button->setPopupMode(QToolButton::MenuButtonPopup);
57  button->setMenu(m_menu.get());
58  BT_CONNECT(button, &BtToolButton::pressed,
59  this /* Meeded */, [this] { Q_EMIT triggered(); });
60  return button;
61 }
62 
63 // Function to catch the Shortcut event and emit the triggered signal
64 bool BtToolBarPopupAction::event(QEvent *event) {
65  if (event->type() == QEvent::Shortcut) {
66  Q_EMIT triggered();
67  return true;
68  }
69  return QWidgetAction::event(event);
70 }
#define BT_CONNECT(...)
Definition: btconnect.h:20
QWidget * createWidget(QWidget *parent) override
~BtToolBarPopupAction() override
BtToolBarPopupAction(const QIcon &icon, const QString &text, QObject *parent)
bool event(QEvent *e) override
std::unique_ptr< QMenu > const m_menu