MuseScore  3.4
Music composition and notation
toolbuttonmenu.h
Go to the documentation of this file.
1 //=============================================================================
2 // toolbuttonmenu.h
3 //
4 // Copyright (C) 2016 Peter Jonas <shoogle@users.noreply.github.com>
5 //
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License version 2
8 // as published by the Free Software Foundation and appearing in
9 // the file LICENCE.GPL
10 //=============================================================================
11 
12 #ifndef TOOLBUTTONMENU_H
13 #define TOOLBUTTONMENU_H
14 
15 #include "accessibletoolbutton.h"
16 
17 namespace Ms {
18 
19 //---------------------------------------------------------
20 // ToolButtonMenu
21 // ==============
22 // This creates a button with an arrow next to it. Clicking the button triggers the default
23 // action, while pressing the arrow brings up a menu of alternative actions and/or other
24 // actions. Selecting an alternative action has some effect on the default action's icon
25 // and/or its behavior. Other actions have no effect on the default action.
26 //---------------------------------------------------------
27 
28 class ToolButtonMenu : public AccessibleToolButton { // : public QToolButton {
29  Q_OBJECT
30 
31  public:
32  enum class TYPES {
33  // What happens to the default action if an alternative (non-default) action is triggered?
34  FIXED, // default action is also triggered but is otherwise unchanged.
35  ICON_CHANGED, // default action is triggered and its icon is modified and/or set to that of the triggering action.
36  ACTION_SWAPPED // default action is not triggered. Triggering action (and its icon) become the new default.
37  };
38 
39  private:
41  QActionGroup* _alternativeActions;
42 
43  public:
44  ToolButtonMenu(QString str,
45  TYPES type,
46  QAction* defaultAction,
47  QActionGroup* alternativeActions,
48  QWidget* parent);
49  void addAction(QAction* a) { menu()->addAction(a); }
50  void addSeparator() { menu()->addSeparator(); }
51  void addActions(QList<QAction*> actions) { for (QAction* a : actions) addAction(a); }
52 
53  private:
54  void switchIcon(QAction* a) {
55  Q_ASSERT(_type == TYPES::ICON_CHANGED && _alternativeActions->actions().contains(a));
56  defaultAction()->setIcon(a->icon());
57  }
58 
59  protected:
60  virtual void changeIcon(QAction* a) { switchIcon(a); }
61 
62  private slots:
63  void handleAlternativeAction(QAction* a);
64 
65  };
66 
67 } // namespace Ms
68 
69 #endif // TOOLBUTTONMENU_H
QActionGroup * _alternativeActions
Definition: toolbuttonmenu.h:41
Definition: toolbuttonmenu.h:28
ToolButtonMenu(QString str, TYPES type, QAction *defaultAction, QActionGroup *alternativeActions, QWidget *parent)
Definition: toolbuttonmenu.cpp:16
TYPES
Definition: toolbuttonmenu.h:32
TYPES _type
Definition: toolbuttonmenu.h:40
void addSeparator()
Definition: toolbuttonmenu.h:50
Definition: aeolus.cpp:26
void addAction(QAction *a)
Definition: toolbuttonmenu.h:49
void addActions(QList< QAction *> actions)
Definition: toolbuttonmenu.h:51
Definition: accessibletoolbutton.h:10
void handleAlternativeAction(QAction *a)
Definition: toolbuttonmenu.cpp:56
void switchIcon(QAction *a)
Definition: toolbuttonmenu.h:54
virtual void changeIcon(QAction *a)
Definition: toolbuttonmenu.h:60