1 /*
2  * Cantata
3  *
4  * Copyright (c) 2011-2020 Craig Drummond <craig.p.drummond@gmail.com>
5  *
6  * ----
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; see the file COPYING.  If not, write to
20  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21  * Boston, MA 02110-1301, USA.
22  */
23 
24 #include "mirrormenu.h"
25 #include <QAction>
26 
MirrorMenu(QWidget * p)27 MirrorMenu::MirrorMenu(QWidget *p)
28     : QMenu(p)
29 {
30 }
31 
addAction(QAction * act)32 void MirrorMenu::addAction(QAction *act)
33 {
34     QMenu::addAction(act);
35     updateMenus();
36 }
37 
addAction(const QString & text)38 QAction * MirrorMenu::addAction(const QString &text)
39 {
40     QAction *act=QMenu::addAction(text);
41     updateMenus();
42     return act;
43 }
44 
addAction(const QIcon & icon,const QString & text)45 QAction * MirrorMenu::addAction(const QIcon &icon, const QString &text)
46 {
47     QAction *act=QMenu::addAction(icon, text);
48     updateMenus();
49     return act;
50 }
51 
addAction(const QString & text,const QObject * receiver,const char * member,const QKeySequence & shortcut)52 QAction * MirrorMenu::addAction(const QString &text, const QObject *receiver, const char *member, const QKeySequence &shortcut)
53 {
54     QAction *act=QMenu::addAction(text, receiver, member, shortcut);
55     updateMenus();
56     return act;
57 }
58 
addAction(const QIcon & icon,const QString & text,const QObject * receiver,const char * member,const QKeySequence & shortcut)59 QAction * MirrorMenu::addAction(const QIcon &icon, const QString &text, const QObject *receiver, const char *member, const QKeySequence &shortcut)
60 {
61     QAction *act=QMenu::addAction(icon, text, receiver, member, shortcut);
62     updateMenus();
63     return act;
64 }
65 
removeAction(QAction * act)66 void MirrorMenu::removeAction(QAction *act)
67 {
68     QMenu::removeAction(act);
69     updateMenus();
70 }
71 
clear()72 void MirrorMenu::clear()
73 {
74     QMenu::clear();
75     updateMenus();
76 }
77 
duplicate(QWidget * p)78 QMenu * MirrorMenu::duplicate(QWidget *p)
79 {
80     QMenu *menu=new QMenu(p);
81     menus.append(menu);
82     updateMenu(menu);
83     connect(menu, SIGNAL(destroyed(QObject*)), this, SLOT(menuDestroyed(QObject*)));
84     return menu;
85 }
86 
updateMenus()87 void MirrorMenu::updateMenus()
88 {
89     for (QMenu *m: menus) {
90         updateMenu(m);
91     }
92 }
93 
updateMenu(QMenu * menu)94 void MirrorMenu::updateMenu(QMenu *menu)
95 {
96     menu->clear();
97     menu->addActions(actions());
98 }
99 
menuDestroyed(QObject * obj)100 void MirrorMenu::menuDestroyed(QObject *obj)
101 {
102     if (qobject_cast<QMenu *>(obj)) {
103         menus.removeAll(static_cast<QMenu *>(obj));
104     }
105 }
106 
107 #include "moc_mirrormenu.cpp"
108