Add support for menu sections

Also adds a manual unit test

CHANGELOG: PlasmaComponents.Menu now supports sections
REVIEW: 123537
This commit is contained in:
Kai Uwe Broulik 2015-04-27 22:19:41 +02:00
parent 4207218d8b
commit 93cf07deac
5 changed files with 123 additions and 6 deletions

View File

@ -84,7 +84,15 @@ void QMenuProxy::setVisualParent(QObject *parent)
action->setMenu(m_menu);
m_menu->clear();
foreach (QMenuItem *item, m_items) {
m_menu->addAction(item->action());
if (item->section()) {
if (!item->isVisible()) {
continue;
}
m_menu->addSection(item->text());
} else {
m_menu->addAction(item->action());
}
}
m_menu->updateGeometry();
}
@ -116,7 +124,11 @@ bool QMenuProxy::event(QEvent *event)
QMenuItem *mi = qobject_cast<QMenuItem *>(ce->child());
//FIXME: linear complexity here
if (mi && !m_items.contains(mi)) {
m_menu->addAction(mi->action());
if (mi->separator()) {
m_menu->addSection(mi->text());
} else {
m_menu->addAction(mi->action());
}
m_items << mi;
}
break;
@ -161,6 +173,11 @@ void QMenuProxy::addMenuItem(QMenuItem *item)
m_items << item;
}
void QMenuProxy::addSection(const QString &text)
{
m_menu->addSection(text);
}
void QMenuProxy::itemTriggered(QAction *action)
{
QMenuItem *item = qobject_cast<QMenuItem *>(action);
@ -178,8 +195,17 @@ void QMenuProxy::open(int x, int y)
qDebug() << "opening menu at " << x << y;
m_menu->clear();
foreach (QMenuItem *item, m_items) {
qDebug() << "Adding action: " << item->text();
m_menu->addAction(item->action());
if (item->section()) {
if (!item->isVisible()) {
continue;
}
qDebug() << "Adding section: " << item->text();
m_menu->addSection(item->text());
} else {
qDebug() << "Adding action: " << item->text();
m_menu->addAction(item->action());
}
}
QQuickItem *parentItem;

View File

@ -67,6 +67,7 @@ public:
Q_INVOKABLE void clearMenuItems();
Q_INVOKABLE void addMenuItem(const QString &text);
Q_INVOKABLE void addMenuItem(QMenuItem *item);
Q_INVOKABLE void addSection(const QString &text);
protected:
bool event(QEvent *event) Q_DECL_OVERRIDE;

View File

@ -22,8 +22,8 @@
QMenuItem::QMenuItem(QQuickItem *parent)
: QQuickItem(parent),
m_action(0)
m_action(0),
m_section(false)
{
setAction(new QAction(this));
connect(m_action, &QAction::triggered, this, &QMenuItem::clicked);
@ -78,6 +78,16 @@ void QMenuItem::setSeparator(bool s)
m_action->setSeparator(s);
}
bool QMenuItem::section() const
{
return m_section;
}
void QMenuItem::setSection(bool s)
{
m_section = s;
}
QString QMenuItem::text() const
{
return m_action->text();

View File

@ -37,6 +37,10 @@ class QMenuItem : public QQuickItem
* If true, the menu item will behave like a separator
*/
Q_PROPERTY(bool separator READ separator WRITE setSeparator NOTIFY separatorChanged)
/**
* If true, the menu item will behave like a section
*/
Q_PROPERTY(bool section READ section WRITE setSection NOTIFY sectionChanged)
Q_PROPERTY(QString text READ text WRITE setText NOTIFY textChanged)
Q_PROPERTY(QVariant icon READ icon WRITE setIcon NOTIFY iconChanged)
Q_PROPERTY(QAction *action READ action WRITE setAction NOTIFY actionChanged)
@ -52,6 +56,8 @@ public:
void setIcon(const QVariant &i);
bool separator() const;
void setSeparator(bool s);
bool section() const;
void setSection(bool s);
QString text() const;
void setText(const QString &t);
@ -67,6 +73,7 @@ Q_SIGNALS:
void actionChanged();
void iconChanged();
void separatorChanged();
void sectionChanged();
void textChanged();
void toggled(bool checked);
void checkableChanged();
@ -75,6 +82,7 @@ private:
void updateAction();
QAction *m_action;
QVariant m_icon;
bool m_section;
};
#endif // QMENUITEM_H

72
tests/components/menu.qml Normal file
View File

@ -0,0 +1,72 @@
import QtQuick 2.0
import org.kde.plasma.components 2.0 as PlasmaComponents
Rectangle {
width: 600
height: 200
color: "white"
Flow {
anchors.fill: parent
anchors.margins: 20
spacing: 20
PlasmaComponents.Button {
text: "Simple menu"
onClicked: simpleMenu.open(0, height)
PlasmaComponents.Menu {
id: simpleMenu
PlasmaComponents.MenuItem { text: "Hello" }
PlasmaComponents.MenuItem { text: "This is just a simple" }
PlasmaComponents.MenuItem { text: "Menu" }
PlasmaComponents.MenuItem { text: "without separators" }
PlasmaComponents.MenuItem { text: "and other stuff" }
}
}
PlasmaComponents.Button {
text: "Checkable menu items"
onClicked: checkableMenu.open(0, height)
PlasmaComponents.Menu {
id: checkableMenu
PlasmaComponents.MenuItem { text: "Apple"; checkable: true }
PlasmaComponents.MenuItem { text: "Banana"; checkable: true }
PlasmaComponents.MenuItem { text: "Orange"; checkable: true }
}
}
PlasmaComponents.Button {
text: "Icons"
onClicked: iconsMenu.open(0, height)
PlasmaComponents.Menu {
id: iconsMenu
PlasmaComponents.MenuItem { text: "Error"; icon: "dialog-error" }
PlasmaComponents.MenuItem { text: "Warning"; icon: "dialog-warning" }
PlasmaComponents.MenuItem { text: "Information"; icon: "dialog-information" }
}
}
PlasmaComponents.Button {
text: "Separators and sections"
onClicked: sectionsMenu.open(0, height)
PlasmaComponents.Menu {
id: sectionsMenu
PlasmaComponents.MenuItem { text: "A menu"; section: true }
PlasmaComponents.MenuItem { text: "One entry" }
PlasmaComponents.MenuItem { text: "Another entry" }
PlasmaComponents.MenuItem { separator: true }
PlasmaComponents.MenuItem { text: "One item" }
PlasmaComponents.MenuItem { text: "Another item" }
}
}
}
}