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 <QDeclarativeItem>
#include <QGraphicsObject>
#include <QGraphicsWidget>
#include <QTimer>
@ -26,6 +27,42 @@
#include <Plasma/Corona>
#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)
: QObject(parent)
{
@ -35,6 +72,7 @@ DialogProxy::DialogProxy(QObject *parent)
DialogProxy::~DialogProxy()
{
delete m_dialog;
delete m_declarativeItemContainer;
}
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
if (!m_mainItem.data()->scene()) {
QGraphicsScene *scene = m_mainItem.data()->scene();
if (!scene) {
QObject *parent = m_mainItem.data();
while (parent = parent->parent()) {
while ((parent = parent->parent())) {
QGraphicsObject *qo = qobject_cast<QGraphicsObject *>(parent);
if (qo) {
QGraphicsScene *scene = qo->scene();
scene = qo->scene();
scene->addItem(m_mainItem.data());
break;
}
}
}
//FIXME: make Dialog accept qgraphicsobjects
m_dialog->setGraphicsWidget(qobject_cast<QGraphicsWidget *>(m_mainItem.data()));
//the parent of the qobject never changed, only the parentitem, so put it back what it was
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

View File

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