forked from open-eid/qt-common
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMacMenuBar.cpp
148 lines (136 loc) · 3.79 KB
/
MacMenuBar.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
* QEstEidCommon
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include "MacMenuBar.h"
#include <QtCore/QEvent>
#include <QtWidgets/QApplication>
MacMenuBar::MacMenuBar( bool enableDock )
: QMenuBar()
, dock(0)
, dockSeparator(0)
, windowGroup(0)
{
file = addMenu( tr("&File") );
if( enableDock )
{
dock = new QMenu;
windowGroup = new QActionGroup( dock );
qApp->installEventFilter( this );
connect( dock, SIGNAL(triggered(QAction*)), SLOT(activateWindow(QAction*)) );
dock->setAsDockMenu();
}
}
MacMenuBar::~MacMenuBar()
{
//delete dock;
}
void MacMenuBar::activateWindow( QAction *a )
{
if( QWidget *w = a->data().value<QWidget*>() )
{
w->activateWindow();
w->showNormal();
w->raise();
}
}
QAction* MacMenuBar::addAction( ActionType type, const QObject *receiver, const char *member )
{
QAction *a = file->addAction( typeName( type ), receiver, member );
switch( type )
{
case AboutAction: a->setMenuRole( QAction::AboutRole ); break;
case CloseAction: a->setShortcut( Qt::CTRL + Qt::Key_W ); break;
case PreferencesAction: a->setMenuRole( QAction::PreferencesRole ); break;
default: break;
}
actions[type] = a;
return a;
}
QMenu* MacMenuBar::dockMenu() const { return dock; }
bool MacMenuBar::eventFilter( QObject *o, QEvent *e )
{
QWidget *w = qobject_cast<QWidget*>(o);
if( w && dock )
{
switch( e->type() )
{
case QEvent::Close:
case QEvent::Destroy:
for( QAction *a : windowGroup->actions() )
if( w == a->data().value<QWidget*>() )
delete a;
if( windowGroup->actions().isEmpty() && dockSeparator )
{
delete dockSeparator;
dockSeparator = nullptr;
}
break;
case QEvent::WindowActivate:
{
QWidget *main = w->topLevelWidget();
for( QAction *a : windowGroup->actions() )
a->setChecked( main == a->data().value<QWidget*>() );
if( !windowGroup->checkedAction() )
{
QAction *a = new QAction( title( w ), dock );
a->setCheckable( true );
a->setData( QVariant::fromValue( main ) );
a->setActionGroup( windowGroup );
if( !dockSeparator )
dockSeparator = dock->insertSeparator( dock->actions().value(0) );
dock->insertAction( dockSeparator, a );
}
break;
}
case QEvent::WindowTitleChange:
for( QAction *a : windowGroup->actions() )
if( w == a->data().value<QWidget*>() )
a->setText( title( w ) );
break;
default: break;
}
}
switch( e->type() )
{
case QEvent::LanguageChange:
file->setTitle( tr("&File") );
for( auto i = actions.constBegin(); i != actions.constEnd(); ++i )
i.value()->setText( typeName( i.key() ) );
break;
default: break;
}
return QMenuBar::eventFilter( o, e );
}
QMenu* MacMenuBar::fileMenu() const { return file; }
QString MacMenuBar::title( QObject *o ) const
{
QString title = o->property( "windowTitle" ).toString();
title = title.isEmpty() ? o->property( "windowFilePath" ).toString() : title;
title.remove( "[*]" );
return title;
}
QString MacMenuBar::typeName( ActionType type ) const
{
switch( type )
{
case AboutAction: return tr("Info");
case CloseAction: return tr("Close");
case PreferencesAction: return tr("Settings");
default: return QString();
}
}