File: menuwidget.h

package info (click to toggle)
plasma-widget-menubar 0.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 224 kB
  • ctags: 161
  • sloc: cpp: 1,253; xml: 143; python: 42; makefile: 9; sh: 2
file content (101 lines) | stat: -rw-r--r-- 2,321 bytes parent folder | download | duplicates (2)
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
/*
 * Plasma applet to display application window menus
 *
 * Copyright 2010 Canonical Ltd.
 *
 * Authors:
 * - Aurélien Gâteau <aurelien.gateau@canonical.com>
 *
 * License: GPL v3
 */
#ifndef MENUWIDGET_H
#define MENUWIDGET_H

// Qt
#include <QFontMetrics>
#include <QGraphicsSceneMouseEvent>
#include <QToolButton>

// KDE
#include <Plasma/Applet>
#include <Plasma/ToolButton>

class QMenu;

class MenuButton : public Plasma::ToolButton
{
    Q_OBJECT
public:
    MenuButton(QGraphicsWidget* parent)
    : Plasma::ToolButton(parent)
    , mMenu(0)
    {}

    void setMenu(QMenu* menu) { mMenu = menu; }

    QMenu* menu() const { return mMenu; }

    QSizeF sizeHint(Qt::SizeHint which, const QSizeF& constraint) const
    {
        QSizeF sh = Plasma::ToolButton::sizeHint(which, constraint);
        if (which == Qt::MinimumSize || which == Qt::PreferredSize) {
            sh.setHeight(nativeWidget()->fontMetrics().height());
        }
        return sh;
    }

protected:
    // Let the event propagate so that the applet context menu is shown
    void contextMenuEvent(QGraphicsSceneContextMenuEvent* event)
    {
        event->ignore();
    }

private:
    QMenu* mMenu;
};


class MenuWidget : public QGraphicsWidget
{
    Q_OBJECT
public:
    MenuWidget(Plasma::Applet* applet);
    void setMenu(QMenu*);
    void activate();
    void activateAction(QAction*);
    void activateActionInMenu(QAction* action);

    virtual QSizeF sizeHint(Qt::SizeHint which, const QSizeF& constraint = QSize()) const;

protected:
    virtual bool eventFilter(QObject*, QEvent*);
    virtual void resizeEvent(QGraphicsSceneResizeEvent* event);

private Q_SLOTS:
    void slotButtonClicked();
    void showMenu(MenuButton*);
    void checkMousePosition();
    void slotAboutToHideMenu();
    void updateButtons();

private:
    Plasma::Applet* mApplet;
    QTimer* mMouseChecker;
    QTimer* mUpdateButtonsTimer;
    QMenu* mRootMenu;
    QList<MenuButton*> mMenuButtonList;
    MenuButton* mOverflowButton;
    MenuButton* mCurrentButton;
    QPoint mLastMousePosition;

    void showNextPrevMenu(bool next);
    MenuButton* createButton();

    bool rootMenuEventFilter(QEvent*);
    bool subMenuEventFilter(QMenu*, QEvent*);

    void updateButtonsGeometries();
    void startMouseChecker();
};
#endif /* MENUWIDGET_H */