1 //=============================================================================
2 //  toolbuttonmenu.cpp
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 #include "toolbuttonmenu.h"
13 
14 namespace Ms {
15 
ToolButtonMenu(QString name,QAction * defaultAction,QActionGroup * alternativeActions,QWidget * parent,bool swapAction)16 ToolButtonMenu::ToolButtonMenu(QString name,
17                      QAction* defaultAction,
18                      QActionGroup* alternativeActions,
19                      QWidget* parent,
20                      bool swapAction)
21       : AccessibleToolButton(parent, defaultAction)
22       {
23       // does the default action count as one of the alternative actions?
24       Q_ASSERT(swapAction == alternativeActions->actions().contains(defaultAction));
25 
26       _swapAction = swapAction;
27       _alternativeActions = alternativeActions;
28 
29       setMenu(new QMenu(name, this));
30       menu()->setToolTipsVisible(true);
31 
32       setPopupMode(QToolButton::MenuButtonPopup);
33 
34       if (!swapAction) {
35             addAction(defaultAction);
36             addSeparator();
37             }
38 
39       for (QAction* a : _alternativeActions->actions()) {
40             a->setCheckable(true);
41             a->setIconVisibleInMenu(true);
42             }
43 
44       _alternativeActions->setExclusive(true);
45       addActions(_alternativeActions->actions());
46 
47       connect(_alternativeActions, SIGNAL(triggered(QAction*)), this, SLOT(handleAlternativeAction(QAction*)));
48 
49       if (!swapAction) {
50             // select first alternative action and use its icon for the default action
51             QAction* a = _alternativeActions->actions().first();
52             a->setChecked(true);
53             switchIcon(a);
54             }
55       }
56 
handleAlternativeAction(QAction * a)57 void ToolButtonMenu::handleAlternativeAction(QAction* a)
58       {
59       Q_ASSERT(_alternativeActions->actions().contains(a));
60 
61       if (_swapAction)
62             setDefaultAction(a);
63       else
64             switchIcon(a);
65 
66       QAction* def = defaultAction();
67 
68       if (!def->isChecked())
69             def->trigger();
70       }
71 
keyPressEvent(QKeyEvent * event)72 void ToolButtonMenu::keyPressEvent(QKeyEvent* event)
73       {
74       switch(event->key()) {
75             case Qt::Key_Enter:
76             case Qt::Key_Return:
77             case Qt::Key_Space:
78                   menu()->exec(this->mapToGlobal(QPoint(0, size().height())));
79                   menu()->setFocus();
80                   menu()->setActiveAction(defaultAction());
81                   break;
82             default:
83                   AccessibleToolButton::keyPressEvent(event);
84             }
85       }
86 
87 } // namespace Ms
88