maxLibQt
ActionPushButton.cpp
Go to the documentation of this file.
1 /*
2  ActionPushButton
3  https://github.com/mpaperno/maxLibQt
4 
5  COPYRIGHT: (c)2019 Maxim Paperno; All Right Reserved.
6  Contact: http://www.WorldDesign.com/contact
7 
8  LICENSE:
9 
10  Commercial License Usage
11  Licensees holding valid commercial licenses may use this file in
12  accordance with the terms contained in a written agreement between
13  you and the copyright holder.
14 
15  GNU General Public License Usage
16  Alternatively, this file may be used under the terms of the GNU
17  General Public License as published by the Free Software Foundation,
18  either version 3 of the License, or (at your option) any later version.
19 
20  This program is distributed in the hope that it will be useful,
21  but WITHOUT ANY WARRANTY; without even the implied warranty of
22  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23  GNU General Public License for more details.
24 
25  A copy of the GNU General Public License is available at <http://www.gnu.org/licenses/>.
26 */
27 
28 #include "ActionPushButton.h"
29 
30 #include <QAction>
31 #include <QActionEvent>
32 
35 {
37 }
38 
40 {
41  switch (e->type()) {
43  if (QActionEvent *ae = static_cast<QActionEvent *>(e))
44  connect(ae->action(), &QAction::triggered, this, &ActionPushButton::onActionTriggered);
45  break;
46 
48  if (QActionEvent *ae = static_cast<QActionEvent *>(e)) {
49  ae->action()->disconnect(this);
50  if (ae->action() == m_defaultAction)
51  setDefaultAction(nullptr);
52  }
53  break;
54 
56  if (QActionEvent *ae = static_cast<QActionEvent *>(e))
57  if (ae->action() == m_defaultAction)
58  updateFromAction(m_defaultAction);
59  break;
60 
61  default:
62  break;
63  }
64  return QPushButton::event(e);
65 }
66 
68 {
69  if (!!m_defaultAction)
70  m_defaultAction->trigger();
71  else
73 }
74 
75 void ActionPushButton::updateFromAction(QAction *action)
76 {
77  if (!action)
78  return;
79  QString buttonText = action->iconText();
80  // If iconText() is generated from text(), we need to remove any '&'s so they don't turn into shortcuts
81  if (buttonText == action->text())
82  buttonText.replace(QLatin1String("&"), QLatin1String(""));
83  setText(buttonText);
84  setIcon(action->icon());
85  setToolTip(action->toolTip());
86  setStatusTip(action->statusTip());
87  setWhatsThis(action->whatsThis());
88  setCheckable(action->isCheckable());
89  setChecked(action->isChecked());
90  setEnabled(action->isEnabled());
91  setVisible(action->isVisible());
92  setAutoRepeat(action->autoRepeat());
94  setFont(action->font());
96  }
97 }
98 
100 {
101  if (m_defaultAction == action)
102  return;
103 
104  if (!!m_defaultAction && !!m_defaultAction->menu() && m_defaultAction->menu() == menu())
105  setMenu(nullptr);
106 
107  m_defaultAction = action;
108  if (!action)
109  return;
110 
111  if (!actions().contains(action))
112  addAction(action);
113  updateFromAction(action);
114  if (!!action->menu() && !menu())
115  setMenu(action->menu());
116 }
117 
118 void ActionPushButton::onActionTriggered()
119 {
120  if (QAction *act = qobject_cast<QAction *>(sender()))
121  emit triggered(act);
122 }
void setAutoRepeat(bool)
void triggered(QAction *)
Signal emitted whenever any QAction added to this button (with QWidget::addAction() or setDefaultActi...
void setMenu(QMenu *menu)
void setStatusTip(const QString &)
void triggered(bool checked)
QEvent::Type type() const const
void addAction(QAction *action)
QObject * sender() const const
bool isChecked() const const
bool isVisible() const const
virtual void setVisible(bool visible)
void setAttribute(Qt::WidgetAttribute attribute, bool on)
void setDefaultAction(QAction *action)
Sets the default action to action.
void setIcon(const QIcon &icon)
void nextCheckState() override
ActionPushButton(QAction *defaultAction, QWidget *parent=nullptr)
Construct using defaultAction as the default action.
void trigger()
bool testAttribute(Qt::WidgetAttribute attribute) const const
void setEnabled(bool)
void setCheckable(bool)
void setFont(const QFont &)
bool isCheckable() const const
void setChecked(bool)
WA_SetFont
QString & replace(int position, int n, QChar after)
bool event(QEvent *e) override
void setWhatsThis(const QString &)
virtual bool event(QEvent *e) override
void setText(const QString &text)
QMenu * menu() const const
void setToolTip(const QString &)
virtual void nextCheckState()
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QList< QAction * > actions() const const
QMenu * menu() const const
QObject * parent() const const
QAction * defaultAction() const
Current default action, if any. Returns nullptr if no default action has been set.
bool isEnabled() const const