ditch the model, support a repeater for items
add menu items when a child that is a menuitem is added, this lets us to not deviate too much from the standard api
This commit is contained in:
parent
ecc105c564
commit
98e7440620
@ -40,7 +40,7 @@ QMenuProxy::~QMenuProxy()
|
||||
delete m_menu;
|
||||
}
|
||||
|
||||
QDeclarativeListProperty<QMenuItem> QMenuProxy::items()
|
||||
QDeclarativeListProperty<QMenuItem> QMenuProxy::content()
|
||||
{
|
||||
return QDeclarativeListProperty<QMenuItem>(this, m_items);
|
||||
}
|
||||
@ -75,6 +75,37 @@ void QMenuProxy::setVisualParent(QDeclarativeItem *parent)
|
||||
emit visualParentChanged();
|
||||
}
|
||||
|
||||
bool QMenuProxy::event(QEvent *event)
|
||||
{
|
||||
switch (event->type()) {
|
||||
case QEvent::ChildAdded: {
|
||||
QChildEvent *ce = static_cast<QChildEvent *>(event);
|
||||
QMenuItem *mi = qobject_cast<QMenuItem *>(ce->child());
|
||||
//FIXME: linear complexity here
|
||||
if (mi && !m_items.contains(mi)) {
|
||||
m_items << mi;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case QEvent::ChildRemoved: {
|
||||
QChildEvent *ce = static_cast<QChildEvent *>(event);
|
||||
QMenuItem *mi = qobject_cast<QMenuItem *>(ce->child());
|
||||
|
||||
//FIXME: linear complexity here
|
||||
if (mi) {
|
||||
m_items.removeAll(mi);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return QObject::event(event);
|
||||
}
|
||||
|
||||
void QMenuProxy::clearMenuItems()
|
||||
{
|
||||
qDeleteAll(m_items);
|
||||
@ -88,6 +119,11 @@ void QMenuProxy::addMenuItem(const QString &text)
|
||||
m_items << item;
|
||||
}
|
||||
|
||||
void QMenuProxy::addMenuItem(QMenuItem *item)
|
||||
{
|
||||
m_items << item;
|
||||
}
|
||||
|
||||
void QMenuProxy::itemTriggered(QAction *action)
|
||||
{
|
||||
QMenuItem *item = qobject_cast<QMenuItem *>(action);
|
||||
|
@ -32,8 +32,8 @@ class QMenuProxy : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
Q_PROPERTY(QDeclarativeListProperty<QMenuItem> items READ items CONSTANT)
|
||||
Q_CLASSINFO("DefaultProperty", "items")
|
||||
Q_PROPERTY(QDeclarativeListProperty<QMenuItem> content READ content CONSTANT)
|
||||
Q_CLASSINFO("DefaultProperty", "content")
|
||||
Q_PROPERTY(QDeclarativeItem *visualParent READ visualParent WRITE setVisualParent NOTIFY visualParentChanged())
|
||||
Q_PROPERTY(DialogStatus::Status status READ status NOTIFY statusChanged)
|
||||
|
||||
@ -41,7 +41,7 @@ public:
|
||||
QMenuProxy(QObject *parent = 0);
|
||||
~QMenuProxy();
|
||||
|
||||
QDeclarativeListProperty<QMenuItem> items();
|
||||
QDeclarativeListProperty<QMenuItem> content();
|
||||
int actionCount() const;
|
||||
QMenuItem *action(int) const;
|
||||
DialogStatus::Status status() const;
|
||||
@ -54,6 +54,10 @@ public:
|
||||
Q_INVOKABLE void close();
|
||||
Q_INVOKABLE void clearMenuItems();
|
||||
Q_INVOKABLE void addMenuItem(const QString &text);
|
||||
Q_INVOKABLE void addMenuItem(QMenuItem *item);
|
||||
|
||||
protected:
|
||||
bool event(QEvent *event);
|
||||
|
||||
Q_SIGNALS:
|
||||
void statusChanged();
|
||||
|
@ -27,6 +27,7 @@ class QMenuItem : public QAction
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
Q_PROPERTY(QObject *parent READ parent WRITE setParent)
|
||||
Q_PROPERTY(bool separator READ isSeparator WRITE setSeparator)
|
||||
|
||||
public:
|
||||
|
@ -67,56 +67,4 @@ import org.kde.plasma.components 0.1
|
||||
|
||||
Menu {
|
||||
id: root
|
||||
property variant model
|
||||
onModelChanged: rebuildMenu()
|
||||
Component.onCompleted: if (model != undefined) rebuildMenu()
|
||||
|
||||
|
||||
function rebuildMenu()
|
||||
{
|
||||
clearMenuItems();
|
||||
for (var i = 0; i < items.length; ++i) {
|
||||
addMenuItem(items[i].text)
|
||||
}
|
||||
|
||||
//it's a model
|
||||
if (model != undefined && model.count) {
|
||||
for (var j = 0; j < model.count; ++j) {
|
||||
var data = model.get(j)
|
||||
var text = data.text
|
||||
|
||||
if (!text) {
|
||||
text = data.display
|
||||
}
|
||||
addMenuItem(text)
|
||||
|
||||
//enabled property must be present -and- be false
|
||||
if (data.enabled === false) {
|
||||
items[items.length-1].enabled = false
|
||||
}
|
||||
if (data.separator === true) {
|
||||
items[items.length-1].separator = true
|
||||
}
|
||||
}
|
||||
//it's a list
|
||||
} else if (model != undefined && model.length) {
|
||||
for (var j = 0; j < model.length; ++j) {
|
||||
var data = model[j]
|
||||
var text = data.text
|
||||
|
||||
if (!text) {
|
||||
text = data.display
|
||||
}
|
||||
addMenuItem(text)
|
||||
|
||||
//enabled property must be present -and- be false
|
||||
if (data.enabled === false) {
|
||||
items[items.length-1].enabled = false
|
||||
}
|
||||
if (data.separator === true) {
|
||||
items[items.length-1].separator = true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user