Changeset View
Changeset View
Standalone View
Standalone View
ui/toggleactionmenu.cpp
- This file was added.
1 | /*************************************************************************** | ||||
---|---|---|---|---|---|
2 | * Copyright (C) 2019 by David Hurka <david.hurka@mailbox.org> * | ||||
3 | * * | ||||
4 | * Inspired by and replacing toolaction.h by: * | ||||
5 | * Copyright (C) 2004-2006 by Albert Astals Cid <aacid@kde.org> * | ||||
6 | * * | ||||
7 | * This program is free software; you can redistribute it and/or modify * | ||||
8 | * it under the terms of the GNU General Public License as published by * | ||||
9 | * the Free Software Foundation; either version 2 of the License, or * | ||||
10 | * (at your option) any later version. * | ||||
11 | ***************************************************************************/ | ||||
12 | | ||||
13 | #include "toggleactionmenu.h" | ||||
14 | | ||||
15 | #include <QPointer> | ||||
16 | | ||||
17 | ToggleActionMenu::ToggleActionMenu( QWidget * parent ) | ||||
18 | : KActionMenu( parent ), | ||||
19 | m_defaultAction( nullptr ) | ||||
20 | { | ||||
21 | } | ||||
22 | | ||||
23 | ToggleActionMenu::ToggleActionMenu( const QString &text, QWidget * parent ) | ||||
24 | : KActionMenu( text, parent ), | ||||
25 | m_defaultAction( nullptr ) | ||||
26 | { | ||||
27 | } | ||||
28 | | ||||
29 | ToggleActionMenu::ToggleActionMenu( const QIcon &icon, const QString &text, QWidget * parent ) | ||||
30 | : KActionMenu( icon, text, parent ), | ||||
31 | m_defaultAction( nullptr ) | ||||
32 | { | ||||
33 | } | ||||
34 | | ||||
35 | QWidget * ToggleActionMenu::createWidget( QWidget * parent ) | ||||
36 | { | ||||
37 | QToolButton * button = qobject_cast< QToolButton * >( KActionMenu::createWidget( parent ) ); | ||||
38 | if ( !button ) { | ||||
39 | // This function is used to add a button into the toolbar. | ||||
40 | // KActionMenu will plug itself as QToolButton. | ||||
41 | // So, if no QToolButton was returned, this was not called the intended way. | ||||
42 | return button; | ||||
43 | } | ||||
44 | | ||||
45 | button->setPopupMode( QToolButton::MenuButtonPopup ); | ||||
46 | // Remove this menu action from the button, | ||||
47 | // so it doesn't compose a menu of this menu action and its own menu. | ||||
48 | button->removeAction( this ); | ||||
49 | // The button has lost the menu now, let it use the correct menu. | ||||
50 | button->setMenu( menu() ); | ||||
51 | | ||||
52 | if ( m_defaultAction ) { | ||||
53 | button->setDefaultAction( m_defaultAction ); | ||||
54 | } | ||||
55 | | ||||
56 | m_buttons.append( QPointer< QToolButton >( button ) ); | ||||
57 | | ||||
58 | return button; | ||||
59 | } | ||||
60 | | ||||
61 | void ToggleActionMenu::setDefaultAction( QAction *action ) | ||||
62 | { | ||||
63 | for ( QPointer< QToolButton > button : qAsConst( m_buttons ) ) | ||||
64 | { | ||||
65 | if ( button ) | ||||
66 | { | ||||
67 | button->setDefaultAction( action ); | ||||
68 | } | ||||
69 | } | ||||
70 | | ||||
71 | m_defaultAction = action; | ||||
72 | } | ||||
73 | | ||||
74 | QAction * ToggleActionMenu::defaultAction() | ||||
75 | { | ||||
76 | return m_defaultAction; | ||||
77 | } |