2011-02-24 18:24:45 +01:00
|
|
|
/***************************************************************************
|
|
|
|
* Copyright 2011 Marco Martin <mart@kde.org> *
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or *
|
|
|
|
* (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the *
|
|
|
|
* Free Software Foundation, Inc., *
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . *
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#include "dialog.h"
|
|
|
|
|
2011-02-24 23:46:23 +01:00
|
|
|
#include <QDeclarativeItem>
|
2011-02-24 18:24:45 +01:00
|
|
|
#include <QGraphicsObject>
|
|
|
|
#include <QGraphicsWidget>
|
|
|
|
#include <QTimer>
|
|
|
|
|
|
|
|
#include <Plasma/Corona>
|
|
|
|
#include <Plasma/Dialog>
|
|
|
|
|
2011-02-24 23:46:23 +01:00
|
|
|
class DeclarativeItemContainer : public QGraphicsWidget
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
DeclarativeItemContainer(QGraphicsItem *parent = 0)
|
|
|
|
: QGraphicsWidget(parent)
|
|
|
|
{}
|
|
|
|
|
|
|
|
~DeclarativeItemContainer()
|
|
|
|
{}
|
|
|
|
|
|
|
|
void setDeclarativeItem(QDeclarativeItem *item)
|
|
|
|
{
|
2011-03-19 18:36:29 +01:00
|
|
|
if (m_declarativeItem) {
|
|
|
|
m_declarativeItem.data()->removeSceneEventFilter(this);
|
|
|
|
}
|
2011-02-24 23:46:23 +01:00
|
|
|
m_declarativeItem = item;
|
|
|
|
static_cast<QGraphicsItem *>(item)->setParentItem(this);
|
|
|
|
setMinimumWidth(item->implicitWidth());
|
|
|
|
setMinimumHeight(item->implicitHeight());
|
2011-03-19 18:30:49 +01:00
|
|
|
resize(item->width(), item->height());
|
2011-03-19 18:36:29 +01:00
|
|
|
item->installSceneEventFilter(this);
|
2011-02-24 23:46:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-19 18:36:29 +01:00
|
|
|
bool sceneEventFilter(QGraphicsItem *watched, QEvent *event)
|
|
|
|
{
|
|
|
|
if (event->type() == QEvent::GraphicsSceneResize) {
|
|
|
|
resize(watched->boundingRect().size());
|
|
|
|
}
|
|
|
|
|
|
|
|
return QGraphicsWidget::sceneEventFilter(watched, event);
|
|
|
|
}
|
|
|
|
|
2011-02-24 23:46:23 +01:00
|
|
|
private:
|
|
|
|
QWeakPointer<QDeclarativeItem> m_declarativeItem;
|
|
|
|
};
|
|
|
|
|
2011-02-24 18:24:45 +01:00
|
|
|
DialogProxy::DialogProxy(QObject *parent)
|
|
|
|
: QObject(parent)
|
|
|
|
{
|
|
|
|
m_dialog = new Plasma::Dialog();
|
2011-03-21 21:38:48 +01:00
|
|
|
m_flags = m_dialog->windowFlags();
|
2011-02-24 18:24:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
DialogProxy::~DialogProxy()
|
|
|
|
{
|
|
|
|
delete m_dialog;
|
2011-02-24 23:46:23 +01:00
|
|
|
delete m_declarativeItemContainer;
|
2011-02-24 18:24:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QGraphicsObject *DialogProxy::mainItem() const
|
|
|
|
{
|
|
|
|
return m_dialog->graphicsWidget();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DialogProxy::setMainItem(QGraphicsObject *mainItem)
|
|
|
|
{
|
|
|
|
if (m_mainItem.data() != mainItem) {
|
2011-02-25 22:38:03 +01:00
|
|
|
if (m_mainItem) {
|
|
|
|
m_mainItem.data()->setParent(mainItem->parent());
|
|
|
|
}
|
2011-02-24 18:24:45 +01:00
|
|
|
m_mainItem = mainItem;
|
2011-02-25 22:38:03 +01:00
|
|
|
mainItem->setParentItem(0);
|
|
|
|
mainItem->setParent(this);
|
2011-02-24 18:24:45 +01:00
|
|
|
|
|
|
|
//if this is called in Compenent.onCompleted we have to wait a loop the item is added to a scene
|
|
|
|
QTimer::singleShot(0, this, SLOT(syncMainItem()));
|
|
|
|
emit mainItemChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DialogProxy::syncMainItem()
|
|
|
|
{
|
|
|
|
if (!m_mainItem) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//not have a scene? go up in the hyerarchy until we find something with a scene
|
2011-02-24 23:46:23 +01:00
|
|
|
QGraphicsScene *scene = m_mainItem.data()->scene();
|
|
|
|
if (!scene) {
|
2011-02-24 18:24:45 +01:00
|
|
|
QObject *parent = m_mainItem.data();
|
2011-02-24 23:46:23 +01:00
|
|
|
while ((parent = parent->parent())) {
|
2011-02-24 18:24:45 +01:00
|
|
|
QGraphicsObject *qo = qobject_cast<QGraphicsObject *>(parent);
|
|
|
|
if (qo) {
|
2011-02-24 23:46:23 +01:00
|
|
|
scene = qo->scene();
|
2011-02-24 18:24:45 +01:00
|
|
|
scene->addItem(m_mainItem.data());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-24 23:46:23 +01:00
|
|
|
//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);
|
2011-02-24 18:24:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool DialogProxy::isVisible() const
|
|
|
|
{
|
|
|
|
return m_dialog->isVisible();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DialogProxy::setVisible(const bool visible)
|
|
|
|
{
|
2011-02-25 15:01:04 +01:00
|
|
|
if (m_dialog->isVisible() != visible) {
|
2011-02-24 18:24:45 +01:00
|
|
|
m_dialog->setVisible(visible);
|
2011-03-21 21:40:00 +01:00
|
|
|
if (visible) {
|
2011-03-21 21:38:48 +01:00
|
|
|
m_dialog->setWindowFlags(m_flags);
|
|
|
|
m_dialog->raise();
|
|
|
|
}
|
2011-02-24 18:24:45 +01:00
|
|
|
emit visibleChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-21 19:18:28 +01:00
|
|
|
QPoint DialogProxy::popupPosition(QGraphicsObject *item) const
|
2011-02-24 18:24:45 +01:00
|
|
|
{
|
2011-03-21 19:18:28 +01:00
|
|
|
Plasma::Corona *corona = qobject_cast<Plasma::Corona *>(item->scene());
|
|
|
|
if (corona) {
|
|
|
|
return corona->popupPosition(item, m_dialog->size());
|
2011-02-24 18:24:45 +01:00
|
|
|
} else {
|
2011-03-21 19:18:28 +01:00
|
|
|
return QPoint();
|
2011-02-24 18:24:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-25 20:37:56 +01:00
|
|
|
|
|
|
|
int DialogProxy::x() const
|
|
|
|
{
|
|
|
|
return m_dialog->pos().x();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DialogProxy::setX(int x)
|
|
|
|
{
|
|
|
|
m_dialog->move(x, m_dialog->pos().y());
|
|
|
|
}
|
|
|
|
|
|
|
|
int DialogProxy::y() const
|
|
|
|
{
|
|
|
|
return m_dialog->pos().y();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DialogProxy::setY(int y)
|
|
|
|
{
|
|
|
|
m_dialog->move(m_dialog->pos().x(), y);
|
|
|
|
}
|
|
|
|
|
2011-03-21 21:38:48 +01:00
|
|
|
int DialogProxy::windowFlags() const
|
|
|
|
{
|
|
|
|
return (int)m_dialog->windowFlags();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DialogProxy::setWindowFlags(const int flags)
|
|
|
|
{
|
|
|
|
m_flags = (Qt::WindowFlags)flags;
|
|
|
|
m_dialog->setWindowFlags((Qt::WindowFlags)flags);
|
|
|
|
}
|
|
|
|
|
2011-02-25 20:37:56 +01:00
|
|
|
bool DialogProxy::eventFilter(QObject *watched, QEvent *event)
|
|
|
|
{
|
|
|
|
if (watched == m_dialog && event->type() == QEvent::Move) {
|
|
|
|
emit positionChanged();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-02-25 19:00:01 +01:00
|
|
|
void DialogProxy::setAttribute(int attribute, bool on)
|
|
|
|
{
|
|
|
|
m_dialog->setAttribute((Qt::WidgetAttribute)attribute, on);
|
|
|
|
}
|
|
|
|
|
2011-02-24 18:24:45 +01:00
|
|
|
#include "dialog.moc"
|
|
|
|
|