1/*
2 This file is part of the KDE project.
3
4 Copyright (c) 2011 Lionel Chauvin <megabigbug@yahoo.fr>
5 Copyright (c) 2011,2012 Cédric Bellegarde <gnumdk@gmail.com>
6
7 Permission is hereby granted, free of charge, to any person obtaining a
8 copy of this software and associated documentation files (the "Software"),
9 to deal in the Software without restriction, including without limitation
10 the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 and/or sell copies of the Software, and to permit persons to whom the
12 Software is furnished to do so, subject to the following conditions:
13
14 The above copyright notice and this permission notice shall be included in
15 all copies or substantial portions of the Software.
16
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 DEALINGS IN THE SOFTWARE.
24*/
25
26#ifndef MENUWIDGET__H
27#define MENUWIDGET__H
28
29#include "menubutton.h"
30
31#include <QGraphicsWidget>
32#include <QTimer>
33
34class QGraphicsLinearLayout;
35class QGraphicsView;
36
37class MenuWidget : public QGraphicsWidget
38{
39Q_OBJECT
40public:
41 MenuWidget(QGraphicsView *view = 0);
42 ~MenuWidget();
43
44 /**
45 * Set root menu
46 */
47 void setMenu(QMenu *menu);
48 /**
49 * Init layout with root menu
50 */
51 void initLayout();
52 /**
53 * True if a menu is visible in menuwidget
54 */
55 bool aMenuIsVisible() { return m_visibleMenu; }
56 /**
57 * Activate action, or first action if null
58 */
59 void setActiveAction(QAction *action);
60
61 /**
62 * Auto open menu items on mouse over
63 */
64 void autoOpen() { m_mouseTimer->start(100); }
65
66 /**
67 * Return content bottom margin
68 */
69 qreal contentBottomMargin() { return m_contentBottomMargin; }
70
71 void hide();
72
73protected:
74 /**
75 * Use to get keyboard events
76 */
77 virtual bool eventFilter(QObject*, QEvent*);
78 /**
79 * Filter events on main menu
80 */
81 bool menuEventFilter(QEvent* event);
82 /**
83 * Filter events on submenus
84 */
85 bool subMenuEventFilter(QObject* object, QEvent* event);
86private Q_SLOTS:
87 /**
88 * Clean menu if destroyed
89 */
90 void slotMenuDestroyed();
91 /**
92 * Check hovered item and active it
93 */
94 void slotCheckActiveItem();
95 /**
96 * A menu is hidding
97 */
98 void slotMenuAboutToHide();
99 /**
100 * Menubar button clicked
101 */
102 void slotButtonClicked();
103 /**
104 * Update pending actions
105 */
106 void slotUpdateActions();
107Q_SIGNALS:
108 void needResize();
109 void aboutToHide();
110private:
111 /**
112 * Return a button based on action
113 */
114 MenuButton* createButton(QAction *action);
115 /**
116 * Show current button menu
117 * return showed menu
118 */
119 QMenu* showMenu();
120 /**
121 * Show next menu if next, otherwise previous
122 */
123 void showLeftRightMenu(bool next);
124 /**
125 * Install event filter for menu and it submenus
126 */
127 void installEventFilterForAll(QMenu *menu, QObject *object);
128
129 //Follow mouse position
130 QTimer *m_mouseTimer;
131 //Update actions
132 QTimer *m_actionTimer;
133 QGraphicsView *m_view;
134 QGraphicsLinearLayout *m_layout;
135 QList<MenuButton*> m_buttons;
136 MenuButton *m_currentButton;
137 qreal m_contentBottomMargin;
138 QPoint m_mousePosition;
139 QMenu *m_visibleMenu;
140 QMenu *m_menu;
141};
142
143#endif //MENUWIDGET__H
144