-
Notifications
You must be signed in to change notification settings - Fork 0
/
widgetsmenu.cpp
executable file
·46 lines (41 loc) · 1.58 KB
/
widgetsmenu.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
#include "widgetsmenu.h"
#include <QWidgetAction>
#include <QActionEvent>
WidgetsMenu::WidgetsMenu(QWidget *parent) : QMenu(parent) {
connect(this,SIGNAL(aboutToShow()),this,SLOT(aboutToShow()));
}
void WidgetsMenu::addWidgetItem(QWidget * widget) {
QWidgetAction * widget_action = new QWidgetAction(this);
widget_action->setDefaultWidget(widget);
widget->setProperty("parent",QVariant::fromValue((void *)widget_action));
widget_action->setText(widget->windowTitle());
if (actions().count() > 0) {
QAction * sep = ((QMenu *)this)->addSeparator();
widget->setProperty("separator",QVariant::fromValue((void *)sep));
}
((QMenu *)this)->addAction(widget_action);
}
void WidgetsMenu::removeWidgetItem(QWidget * widget) {
QList<QAction *> actions = this->actions();
for (int i=0;i<actions.count();i++) {
if (!actions[i]->inherits("QWidgetAction")) continue;
QWidgetAction * action = (QWidgetAction *)actions[i];
if (action->defaultWidget() != widget) continue;
delete action;
return;
}
}
void WidgetsMenu::aboutToShow() {
QList<QAction *> actions = this->actions();
for (int i=0;i<actions.count();i++) {
if (!actions[i]->inherits("QWidgetAction")) continue;
QWidget * widget = ((QWidgetAction *)actions[i])->defaultWidget();
if (widget != NULL) widget->setMinimumHeight(widget->sizeHint().height());
}
}
void WidgetsMenu::actionEvent(QActionEvent * e) {
QMenu::actionEvent(e);
if (e->type() == QEvent::ActionRemoved) {
if (actions().count() <= 0) hide();
}
}