CUSTOM_METAOBJECT(DeclarativeAction, QAction)
+// DeclarativeActionItem
+DeclarativeActionItem::DeclarativeActionItem(QObject *parent) : DeclarativeObjectProxy<ActionItem>(parent)
+{
+ connectAllSignals(m_proxiedObject, this);
+}
+
+CUSTOM_METAOBJECT(DeclarativeActionItem, ActionItem)
+
// DeclarativeSeparator
DeclarativeSeparator::DeclarativeSeparator(QObject *parent) : DeclarativeObjectProxy<QAction>(parent)
{
#include <QtDeclarative/qdeclarativeinfo.h>
#include <qdeclarative.h>
+#include "objectadaptors_p.h"
+
+Q_DECLARE_METATYPE(QAction*)
+
#define DECLARATIVE_OBJECT \
public: \
Q_OBJECT_CHECK \
QVector<QObject*> m_children;
};
+class DeclarativeActionItem : public DeclarativeObjectProxy<ActionItem>
+{
+ DECLARATIVE_OBJECT
+
+ public:
+ DeclarativeActionItem(QObject *parent = 0);
+};
+
template <class T>
class DeclarativeWidgetProxy : public DeclarativeObjectProxy<T>
{
return;
}
+ DeclarativeActionItem *declarativeActionItem = dynamic_cast<DeclarativeActionItem*>(object);
+ if (declarativeActionItem) {
+ addAction(qobject_cast<ActionItem*>(declarativeActionItem->object())->action(), declarativeObject);
+ return;
+ }
+
QAction *action = qobject_cast<QAction*>(declarativeObject->object());
if (action) {
addAction(action, declarativeObject);
public:
DeclarativeAction(QObject *parent = 0);
};
+Q_DECLARE_METATYPE(DeclarativeAction*)
class DeclarativeSeparator : public DeclarativeObjectProxy<QAction>
{
// objects
qmlRegisterType<QAction>();
qmlRegisterType<DeclarativeAction>("QtGui", 1, 0, "Action");
+ qmlRegisterType<DeclarativeActionItem>("QtGui", 1, 0, "ActionItem");
qmlRegisterType<DeclarativeSeparator>("QtGui", 1, 0, "Separator");
// layouts
Separator {}
Action {
- text: "Close"
+ text: qsTr("Close")
onTriggered: Qt.quit()
}
}
Separator {}
Action {
+ id: cutAction
text: qsTr("Cut")
onTriggered: textEdit.cut()
}
Action {
+ id: copyAction
text: qsTr("Copy")
onTriggered: textEdit.copy()
}
Action {
+ id: pasteAction
text: qsTr("Paste")
onTriggered: textEdit.paste()
}
contextMenuPolicy: Qt.ActionsContextMenu
- Action {
- text: qsTr("Cut")
- onTriggered: textEdit.cut()
+ ActionItem {
+ action: cutAction
}
- Action {
- text: qsTr("Copy")
- onTriggered: textEdit.copy()
+ ActionItem {
+ action: copyAction
}
- Action {
- text: qsTr("Paste")
- onTriggered: textEdit.paste()
+ ActionItem {
+ action: pasteAction
}
}
#include "objectadaptors_p.h"
+#include "declarativeobjects_p.h"
+
FileDialog::FileDialog(QWidget *parent)
: QFileDialog(parent)
{
{
return document()->isModified();
}
+
+ActionItem::ActionItem(QObject *parent)
+ : QObject(parent)
+ , m_placeholderAction(new QAction(this))
+ , m_qAction(m_placeholderAction)
+{
+}
+
+QAction* ActionItem::action()
+{
+ return m_qAction;
+}
+
+void ActionItem::setAction(const QVariant &action)
+{
+ if (m_action == action)
+ return;
+
+ m_action = action;
+
+ QObject *object = m_action.value<QObject*>();
+
+ // Is the passed action a QAction ...
+ QAction *newAction = qobject_cast<QAction*>(object);
+
+ // ... or a DeclarativeAction
+ DeclarativeAction *declarativeAction = dynamic_cast<DeclarativeAction*>(object);
+ if (declarativeAction) {
+ newAction = qobject_cast<QAction*>(declarativeAction->object());
+ }
+
+ // Check if the placeholder must be replaced with the actual action
+ if (m_qAction == m_placeholderAction) {
+ const QList<QWidget*> widgets = m_placeholderAction->associatedWidgets();
+ if (!widgets.isEmpty()) {
+ Q_ASSERT(widgets.count() == 1); // The placeholder action can only be plugged into one widget
+
+ // Replace the placeholder action with the new one
+ widgets.first()->insertAction(m_placeholderAction, newAction);
+ widgets.first()->removeAction(m_placeholderAction);
+
+ m_qAction = newAction;
+ }
+ }
+
+ emit actionChanged();
+}
+
+QVariant ActionItem::qmlAction() const
+{
+ return m_action;
+}
#ifndef OBJECTADAPTORS_P_H
#define OBJECTADAPTORS_P_H
+#include <QtGui/QAction>
#include <QtGui/QFileDialog>
#include <QtGui/QInputDialog>
#include <QtGui/QTextEdit>
bool modified() const;
};
+class ActionItem : public QObject
+{
+ Q_OBJECT
+
+ Q_PROPERTY(QVariant action READ qmlAction WRITE setAction NOTIFY actionChanged)
+
+ public:
+ ActionItem(QObject *parent = 0);
+
+ QAction* action();
+
+ Q_SIGNALS:
+ void actionChanged();
+
+ private:
+ void setAction(const QVariant &action);
+ QVariant qmlAction() const;
+
+ QVariant m_action;
+ QAction* m_placeholderAction;
+ QAction* m_qAction;
+};
+
#endif