support qdeclarativeitems as well

support qdeclarativeitems as well trough a parent qgraphicswidget that syncs the geometry
This commit is contained in:
Marco Martin 2011-02-24 23:46:23 +01:00
parent 93790aec7f
commit 9c6f2132ad
2 changed files with 64 additions and 5 deletions

View File

@ -19,6 +19,7 @@
#include "dialog.h" #include "dialog.h"
#include <QDeclarativeItem>
#include <QGraphicsObject> #include <QGraphicsObject>
#include <QGraphicsWidget> #include <QGraphicsWidget>
#include <QTimer> #include <QTimer>
@ -26,6 +27,42 @@
#include <Plasma/Corona> #include <Plasma/Corona>
#include <Plasma/Dialog> #include <Plasma/Dialog>
class DeclarativeItemContainer : public QGraphicsWidget
{
public:
DeclarativeItemContainer(QGraphicsItem *parent = 0)
: QGraphicsWidget(parent)
{}
~DeclarativeItemContainer()
{}
void setDeclarativeItem(QDeclarativeItem *item)
{
m_declarativeItem = item;
static_cast<QGraphicsItem *>(item)->setParentItem(this);
setMinimumWidth(item->implicitWidth());
setMinimumHeight(item->implicitHeight());
}
QDeclarativeItem *declarativeItem() const
{
return m_declarativeItem.data();
}
protected:
void resizeEvent(QGraphicsSceneResizeEvent *event)
{
if (m_declarativeItem) {
m_declarativeItem.data()->setProperty("width", event->newSize().width());
m_declarativeItem.data()->setProperty("height", event->newSize().height());
}
}
private:
QWeakPointer<QDeclarativeItem> m_declarativeItem;
};
DialogProxy::DialogProxy(QObject *parent) DialogProxy::DialogProxy(QObject *parent)
: QObject(parent) : QObject(parent)
{ {
@ -35,6 +72,7 @@ DialogProxy::DialogProxy(QObject *parent)
DialogProxy::~DialogProxy() DialogProxy::~DialogProxy()
{ {
delete m_dialog; delete m_dialog;
delete m_declarativeItemContainer;
} }
QGraphicsObject *DialogProxy::mainItem() const QGraphicsObject *DialogProxy::mainItem() const
@ -60,20 +98,38 @@ void DialogProxy::syncMainItem()
} }
//not have a scene? go up in the hyerarchy until we find something with a scene //not have a scene? go up in the hyerarchy until we find something with a scene
if (!m_mainItem.data()->scene()) { QGraphicsScene *scene = m_mainItem.data()->scene();
if (!scene) {
QObject *parent = m_mainItem.data(); QObject *parent = m_mainItem.data();
while (parent = parent->parent()) { while ((parent = parent->parent())) {
QGraphicsObject *qo = qobject_cast<QGraphicsObject *>(parent); QGraphicsObject *qo = qobject_cast<QGraphicsObject *>(parent);
if (qo) { if (qo) {
QGraphicsScene *scene = qo->scene(); scene = qo->scene();
scene->addItem(m_mainItem.data()); scene->addItem(m_mainItem.data());
break; break;
} }
} }
} }
//FIXME: make Dialog accept qgraphicsobjects //the parent of the qobject never changed, only the parentitem, so put it back what it was
m_dialog->setGraphicsWidget(qobject_cast<QGraphicsWidget *>(m_mainItem.data())); m_mainItem.data()->setParentItem(qobject_cast<QGraphicsObject *>(m_mainItem.data()->parent()));
QGraphicsWidget *widget = qobject_cast<QGraphicsWidget *>(m_mainItem.data());
if (widget) {
m_declarativeItemContainer->deleteLater();
m_declarativeItemContainer = 0;
} else {
QDeclarativeItem *di = qobject_cast<QDeclarativeItem *>(m_mainItem.data());
if (di) {
if (!m_declarativeItemContainer) {
m_declarativeItemContainer = new DeclarativeItemContainer();
scene->addItem(m_declarativeItemContainer);
}
m_declarativeItemContainer->setDeclarativeItem(di);
widget = m_declarativeItemContainer;
}
}
m_dialog->setGraphicsWidget(widget);
} }
bool DialogProxy::isVisible() const bool DialogProxy::isVisible() const

View File

@ -29,6 +29,8 @@ namespace Plasma
class Dialog; class Dialog;
} }
class DeclarativeItemContainer;
class DialogProxy : public QObject class DialogProxy : public QObject
{ {
Q_OBJECT Q_OBJECT
@ -57,6 +59,7 @@ protected Q_SLOTS:
private: private:
Plasma::Dialog *m_dialog; Plasma::Dialog *m_dialog;
DeclarativeItemContainer *m_declarativeItemContainer;
QWeakPointer<QGraphicsObject> m_mainItem; QWeakPointer<QGraphicsObject> m_mainItem;
}; };