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-04-28 00:27:52 +02:00
|
|
|
#include "declarativeitemcontainer_p.h"
|
2011-02-24 18:24:45 +01:00
|
|
|
|
2011-11-02 18:15:17 +01:00
|
|
|
#include <QApplication>
|
2011-02-24 23:46:23 +01:00
|
|
|
#include <QDeclarativeItem>
|
2011-11-02 18:15:17 +01:00
|
|
|
#include <QDesktopWidget>
|
2011-02-24 18:24:45 +01:00
|
|
|
#include <QGraphicsObject>
|
|
|
|
#include <QGraphicsWidget>
|
|
|
|
#include <QTimer>
|
2011-06-07 19:12:04 +02:00
|
|
|
#include <QLayout>
|
2011-02-24 18:24:45 +01:00
|
|
|
|
|
|
|
#include <Plasma/Corona>
|
|
|
|
#include <Plasma/Dialog>
|
2011-08-15 19:32:01 +02:00
|
|
|
#include <Plasma/WindowEffects>
|
2011-02-24 18:24:45 +01:00
|
|
|
|
2011-02-24 23:46:23 +01:00
|
|
|
|
2011-11-02 18:15:17 +01:00
|
|
|
int DialogProxy::offscreenX = 0;
|
|
|
|
int DialogProxy::offscreenY = 0;
|
|
|
|
|
2011-06-08 17:53:19 +02:00
|
|
|
DialogMargins::DialogMargins(Plasma::Dialog *dialog, QObject *parent)
|
|
|
|
: QObject(parent),
|
|
|
|
m_dialog(dialog)
|
|
|
|
{
|
|
|
|
checkMargins();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DialogMargins::checkMargins()
|
|
|
|
{
|
|
|
|
int left, top, right, bottom;
|
|
|
|
m_dialog->getContentsMargins(&left, &top, &right, &bottom);
|
|
|
|
|
|
|
|
if (left != m_left) {
|
|
|
|
m_left = left;
|
|
|
|
emit leftChanged();
|
|
|
|
}
|
|
|
|
if (top != m_top) {
|
|
|
|
m_top = top;
|
|
|
|
emit topChanged();
|
|
|
|
}
|
|
|
|
if (right != m_right) {
|
|
|
|
m_right = right;
|
|
|
|
emit rightChanged();
|
|
|
|
}
|
|
|
|
if (bottom != m_bottom) {
|
|
|
|
m_bottom = bottom;
|
|
|
|
emit bottomChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int DialogMargins::left() const
|
|
|
|
{
|
|
|
|
return m_left;
|
|
|
|
}
|
|
|
|
|
|
|
|
int DialogMargins::top() const
|
|
|
|
{
|
|
|
|
return m_top;
|
|
|
|
}
|
|
|
|
|
|
|
|
int DialogMargins::right() const
|
|
|
|
{
|
|
|
|
return m_right;
|
|
|
|
}
|
|
|
|
|
|
|
|
int DialogMargins::bottom() const
|
|
|
|
{
|
|
|
|
return m_bottom;
|
|
|
|
}
|
|
|
|
|
2011-02-24 18:24:45 +01:00
|
|
|
DialogProxy::DialogProxy(QObject *parent)
|
2011-06-07 21:07:27 +02:00
|
|
|
: QObject(parent),
|
2011-06-19 23:08:15 +02:00
|
|
|
m_declarativeItemContainer(0),
|
2011-08-15 19:32:01 +02:00
|
|
|
m_activeWindow(false),
|
|
|
|
m_location(Plasma::Floating)
|
2011-02-24 18:24:45 +01:00
|
|
|
{
|
|
|
|
m_dialog = new Plasma::Dialog();
|
2011-06-08 17:53:19 +02:00
|
|
|
m_margins = new DialogMargins(m_dialog, this);
|
2011-06-07 16:40:33 +02:00
|
|
|
m_dialog->installEventFilter(this);
|
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
|
|
|
|
{
|
2011-06-06 17:33:02 +02:00
|
|
|
return m_mainItem.data();
|
2011-02-24 18:24:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void DialogProxy::setMainItem(QGraphicsObject *mainItem)
|
|
|
|
{
|
|
|
|
if (m_mainItem.data() != mainItem) {
|
2011-02-25 22:38:03 +01:00
|
|
|
if (m_mainItem) {
|
2011-07-13 13:14:40 +02:00
|
|
|
m_mainItem.data()->setParent(mainItem ? mainItem->parent() : 0);
|
2011-02-25 22:38:03 +01:00
|
|
|
}
|
2011-07-13 13:14:40 +02:00
|
|
|
|
2011-02-24 18:24:45 +01:00
|
|
|
m_mainItem = mainItem;
|
2011-07-13 13:14:40 +02:00
|
|
|
|
2011-06-13 13:56:55 +02:00
|
|
|
if (mainItem) {
|
|
|
|
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-06-19 19:07:26 +02:00
|
|
|
if (scene) {
|
|
|
|
scene->addItem(m_mainItem.data());
|
|
|
|
break;
|
|
|
|
}
|
2011-02-24 18:24:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-19 19:07:26 +02:00
|
|
|
if (!scene) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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) {
|
2011-07-13 13:16:14 +02:00
|
|
|
if (m_declarativeItemContainer) {
|
|
|
|
m_declarativeItemContainer->deleteLater();
|
|
|
|
m_declarativeItemContainer = 0;
|
|
|
|
}
|
2011-02-24 23:46:23 +01:00
|
|
|
} 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-11-02 18:15:17 +01:00
|
|
|
|
|
|
|
if (!qobject_cast<Plasma::Corona *>(scene)) {
|
|
|
|
offscreenX -= 1024;
|
|
|
|
offscreenY -= 1024;
|
|
|
|
widget->setPos(offscreenX, offscreenY);
|
|
|
|
}
|
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);
|
2011-06-07 19:12:04 +02:00
|
|
|
m_dialog->setVisible(visible);
|
2011-03-21 21:38:48 +01:00
|
|
|
m_dialog->raise();
|
|
|
|
}
|
2011-02-24 18:24:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-03 14:46:05 +01:00
|
|
|
QPoint DialogProxy::popupPosition(QGraphicsObject *item, int alignment)
|
2011-02-24 18:24:45 +01:00
|
|
|
{
|
2011-11-03 14:46:05 +01:00
|
|
|
QGraphicsObject *actualItem = item;
|
|
|
|
|
|
|
|
//if no item is passed search the root item in order to figure out the view
|
|
|
|
if (!actualItem) {
|
|
|
|
actualItem = qobject_cast<QGraphicsObject *>(parent());
|
|
|
|
|
|
|
|
//search the root object
|
|
|
|
while (true) {
|
|
|
|
QGraphicsObject *ancestor = qobject_cast<QGraphicsObject *>(actualItem->parent());
|
|
|
|
|
|
|
|
if (ancestor) {
|
|
|
|
actualItem = ancestor;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!actualItem) {
|
|
|
|
return QPoint();
|
|
|
|
}
|
2011-06-24 17:56:42 +02:00
|
|
|
}
|
2011-11-03 14:46:05 +01:00
|
|
|
|
|
|
|
//ensure the dialog has the proper size
|
|
|
|
syncMainItem();
|
|
|
|
m_dialog->syncToGraphicsWidget();
|
|
|
|
|
|
|
|
Plasma::Corona *corona = qobject_cast<Plasma::Corona *>(actualItem->scene());
|
|
|
|
if (corona && item) {
|
|
|
|
return corona->popupPosition(actualItem, m_dialog->size(), (Qt::AlignmentFlag)alignment);
|
2011-02-24 18:24:45 +01:00
|
|
|
} else {
|
2011-11-02 18:15:17 +01:00
|
|
|
|
2011-11-03 14:46:05 +01:00
|
|
|
QList<QGraphicsView*> views = actualItem->scene()->views();
|
|
|
|
|
2011-11-02 18:15:17 +01:00
|
|
|
|
|
|
|
if (views.size() < 1) {
|
|
|
|
return QPoint();
|
|
|
|
}
|
|
|
|
|
|
|
|
QGraphicsView *view = 0;
|
|
|
|
if (views.size() == 1) {
|
|
|
|
view = views[0];
|
|
|
|
} else {
|
|
|
|
QGraphicsView *found = 0;
|
|
|
|
QGraphicsView *possibleFind = 0;
|
2011-11-03 14:46:05 +01:00
|
|
|
|
2011-11-02 18:15:17 +01:00
|
|
|
foreach (QGraphicsView *v, views) {
|
2011-11-03 14:46:05 +01:00
|
|
|
if (v->sceneRect().intersects(actualItem->sceneBoundingRect()) ||
|
|
|
|
v->sceneRect().contains(actualItem->scenePos())) {
|
2011-11-02 18:15:17 +01:00
|
|
|
if (v->isActiveWindow()) {
|
|
|
|
found = v;
|
|
|
|
} else {
|
|
|
|
possibleFind = v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
view = found ? found : possibleFind;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!view) {
|
|
|
|
return QPoint();
|
|
|
|
}
|
|
|
|
|
2011-11-03 14:46:05 +01:00
|
|
|
//if no item was explicitly specified, align the dialog in the center of the parent view
|
|
|
|
if (!item) {
|
|
|
|
return view->geometry().center() - QPoint(m_dialog->width()/2, m_dialog->height()/2);
|
|
|
|
}
|
|
|
|
|
2011-11-02 18:15:17 +01:00
|
|
|
//swap direction if necessary
|
|
|
|
if (QApplication::isRightToLeft() && alignment != Qt::AlignCenter) {
|
|
|
|
if (alignment == Qt::AlignRight) {
|
|
|
|
alignment = Qt::AlignLeft;
|
|
|
|
} else {
|
|
|
|
alignment = Qt::AlignRight;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int xOffset = 0;
|
|
|
|
|
|
|
|
if (alignment == Qt::AlignCenter) {
|
2011-11-03 14:46:05 +01:00
|
|
|
xOffset = actualItem->boundingRect().width()/2 - m_dialog->width()/2;
|
2011-11-02 18:15:17 +01:00
|
|
|
} else if (alignment == Qt::AlignRight) {
|
2011-11-03 14:46:05 +01:00
|
|
|
xOffset = actualItem->boundingRect().width() - m_dialog->width();
|
2011-11-02 18:15:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const QRect avail = QApplication::desktop()->availableGeometry(view);
|
2011-11-03 14:46:05 +01:00
|
|
|
QPoint menuPos = view->mapToGlobal(view->mapFromScene(actualItem->scenePos()+QPoint(xOffset, actualItem->boundingRect().height())));
|
2011-11-02 18:15:17 +01:00
|
|
|
|
|
|
|
if (menuPos.y() + m_dialog->height() > avail.bottom()) {
|
2011-11-03 14:46:05 +01:00
|
|
|
menuPos = view->mapToGlobal(view->mapFromScene(actualItem->scenePos() - QPoint(-xOffset, m_dialog->height())));
|
2011-11-02 18:15:17 +01:00
|
|
|
}
|
|
|
|
return menuPos;
|
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-06-07 16:40:33 +02:00
|
|
|
int DialogProxy::width() const
|
|
|
|
{
|
|
|
|
return m_dialog->size().width();
|
|
|
|
}
|
|
|
|
|
|
|
|
int DialogProxy::height() const
|
|
|
|
{
|
|
|
|
return m_dialog->size().height();
|
|
|
|
}
|
|
|
|
|
2011-06-19 23:08:15 +02:00
|
|
|
bool DialogProxy::isActiveWindow() const
|
|
|
|
{
|
|
|
|
return m_activeWindow;
|
|
|
|
}
|
|
|
|
|
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-08-15 19:32:01 +02:00
|
|
|
int DialogProxy::location() const
|
|
|
|
{
|
|
|
|
return (int)m_location;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DialogProxy::setLocation(int location)
|
|
|
|
{
|
|
|
|
if (m_location == location) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_location = (Plasma::Location)location;
|
|
|
|
emit locationChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-08 17:53:19 +02:00
|
|
|
QObject *DialogProxy::margins() const
|
|
|
|
{
|
|
|
|
return m_margins;
|
|
|
|
}
|
|
|
|
|
2011-02-25 20:37:56 +01:00
|
|
|
bool DialogProxy::eventFilter(QObject *watched, QEvent *event)
|
|
|
|
{
|
|
|
|
if (watched == m_dialog && event->type() == QEvent::Move) {
|
2011-06-07 16:40:33 +02:00
|
|
|
QMoveEvent *me = static_cast<QMoveEvent *>(event);
|
|
|
|
if (me->oldPos().x() != me->pos().x()) {
|
|
|
|
emit xChanged();
|
|
|
|
}
|
|
|
|
if (me->oldPos().y() != me->pos().y()) {
|
|
|
|
emit yChanged();
|
|
|
|
}
|
2011-06-08 17:53:19 +02:00
|
|
|
if ((me->oldPos().x() != me->pos().x()) || (me->oldPos().y() != me->pos().y())) {
|
|
|
|
m_margins->checkMargins();
|
|
|
|
}
|
2011-06-07 16:40:33 +02:00
|
|
|
} else if (watched == m_dialog && event->type() == QEvent::Resize) {
|
|
|
|
QResizeEvent *re = static_cast<QResizeEvent *>(event);
|
|
|
|
if (re->oldSize().width() != re->size().width()) {
|
|
|
|
emit widthChanged();
|
|
|
|
}
|
|
|
|
if (re->oldSize().height() != re->size().height()) {
|
|
|
|
emit heightChanged();
|
|
|
|
}
|
2011-07-12 20:30:22 +02:00
|
|
|
} else if (watched == m_dialog && event->type() == QEvent::Show) {
|
2011-08-15 19:32:01 +02:00
|
|
|
Plasma::WindowEffects::slideWindow(m_dialog, m_location);
|
2011-07-12 20:30:22 +02:00
|
|
|
emit visibleChanged();
|
|
|
|
} else if (watched == m_dialog && event->type() == QEvent::Hide) {
|
2011-08-15 19:32:01 +02:00
|
|
|
Plasma::WindowEffects::slideWindow(m_dialog, m_location);
|
2011-07-12 20:30:22 +02:00
|
|
|
emit visibleChanged();
|
2011-06-19 23:08:15 +02:00
|
|
|
} else if (watched == m_dialog && event->type() == QEvent::WindowActivate) {
|
|
|
|
m_activeWindow = true;
|
|
|
|
emit activeWindowChanged();
|
|
|
|
} else if (watched == m_dialog && event->type() == QEvent::WindowDeactivate) {
|
|
|
|
m_activeWindow = false;
|
|
|
|
emit activeWindowChanged();
|
2011-06-07 18:11:30 +02:00
|
|
|
}
|
2011-02-25 20:37:56 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-02-25 19:00:01 +01:00
|
|
|
void DialogProxy::setAttribute(int attribute, bool on)
|
|
|
|
{
|
2011-06-07 19:12:04 +02:00
|
|
|
m_dialog->setAttribute((Qt::WidgetAttribute)attribute, on);
|
2011-02-25 19:00:01 +01:00
|
|
|
}
|
|
|
|
|
2011-02-24 18:24:45 +01:00
|
|
|
#include "dialog.moc"
|
|
|
|
|