2008-09-07 23:33:02 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2008 by Alessandro Diaferia <alediaferia@gmail.com>
|
|
|
|
* Copyright 2007 by Alexis Ménard <darktears31@gmail.com>
|
|
|
|
* Copyright 2007 Sebastian Kuegler <sebas@kde.org>
|
|
|
|
* Copyright 2006 Aaron Seigo <aseigo@kde.org>
|
|
|
|
*
|
2008-09-08 19:32:25 +02:00
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2008-09-07 23:33:02 +02:00
|
|
|
*
|
2008-09-08 19:32:25 +02:00
|
|
|
* This library is distributed in the hope that it will be useful,
|
2008-09-07 23:33:02 +02:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2008-09-08 19:32:25 +02:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
2008-09-07 23:33:02 +02:00
|
|
|
*
|
2008-09-08 19:32:25 +02:00
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor,
|
2008-09-07 23:33:02 +02:00
|
|
|
* Boston, MA 02110-1301 USA
|
|
|
|
*/
|
2007-12-05 23:32:45 +01:00
|
|
|
|
|
|
|
#include "dialog.h"
|
|
|
|
|
|
|
|
#include <QPainter>
|
|
|
|
#include <QSvgRenderer>
|
|
|
|
#include <QResizeEvent>
|
|
|
|
#include <QMouseEvent>
|
2008-01-25 17:24:40 +01:00
|
|
|
#ifdef Q_WS_X11
|
2007-12-05 23:32:45 +01:00
|
|
|
#include <QX11Info>
|
2008-01-25 17:24:40 +01:00
|
|
|
#endif
|
2008-03-05 22:03:27 +01:00
|
|
|
#include <QBitmap>
|
2008-08-06 22:41:10 +02:00
|
|
|
#include <QtGui/QVBoxLayout>
|
2007-12-06 00:09:28 +01:00
|
|
|
#include <QtGui/QGraphicsSceneEvent>
|
2008-08-06 22:41:10 +02:00
|
|
|
#include <QtGui/QGraphicsView>
|
|
|
|
#include <QtGui/QGraphicsWidget>
|
2007-12-05 23:32:45 +01:00
|
|
|
|
|
|
|
#include <KDebug>
|
|
|
|
#include <NETRootInfo>
|
|
|
|
|
2008-04-16 19:23:58 +02:00
|
|
|
#include <plasma/panelsvg.h>
|
2008-02-08 21:50:55 +01:00
|
|
|
#include <plasma/theme.h>
|
2007-12-05 23:32:45 +01:00
|
|
|
|
2008-01-25 17:24:40 +01:00
|
|
|
#ifdef Q_WS_X11
|
2007-12-05 23:32:45 +01:00
|
|
|
#include <X11/Xlib.h>
|
2008-01-25 17:24:40 +01:00
|
|
|
#endif
|
2007-12-05 23:32:45 +01:00
|
|
|
|
2008-09-07 18:55:37 +02:00
|
|
|
const int resizeAreaMargin = 20;
|
2007-12-05 23:32:45 +01:00
|
|
|
|
|
|
|
namespace Plasma
|
|
|
|
{
|
|
|
|
|
2008-07-01 20:56:43 +02:00
|
|
|
class DialogPrivate
|
2008-02-06 22:07:16 +01:00
|
|
|
{
|
|
|
|
public:
|
2008-08-06 22:41:10 +02:00
|
|
|
DialogPrivate(Dialog *dialog)
|
|
|
|
: q(dialog),
|
|
|
|
background(0),
|
|
|
|
view(0),
|
2008-09-07 18:55:37 +02:00
|
|
|
widget(0),
|
2008-10-04 17:56:56 +02:00
|
|
|
resizeCorners(Dialog::NoCorner),
|
|
|
|
resizeStartCorner(Dialog::NoCorner)
|
2008-08-06 22:41:10 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~DialogPrivate()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void themeUpdated();
|
|
|
|
void adjustView();
|
|
|
|
|
|
|
|
Plasma::Dialog *q;
|
2008-02-06 22:07:16 +01:00
|
|
|
/**
|
|
|
|
* Holds the background SVG, to be re-rendered when the cache is invalidated,
|
|
|
|
* for example by resizing the dialogue.
|
|
|
|
*/
|
2008-04-16 19:23:58 +02:00
|
|
|
Plasma::PanelSvg *background;
|
2008-08-06 22:41:10 +02:00
|
|
|
QGraphicsView *view;
|
|
|
|
QGraphicsWidget *widget;
|
2008-09-07 18:55:37 +02:00
|
|
|
Dialog::ResizeCorners resizeCorners;
|
|
|
|
QMap<Dialog::ResizeCorner, QRect> resizeAreas;
|
|
|
|
Dialog::ResizeCorner resizeStartCorner;
|
2008-02-06 22:07:16 +01:00
|
|
|
};
|
2007-12-05 23:32:45 +01:00
|
|
|
|
2008-08-06 22:41:10 +02:00
|
|
|
void DialogPrivate::themeUpdated()
|
|
|
|
{
|
|
|
|
const int topHeight = background->marginSize(Plasma::TopMargin);
|
|
|
|
const int leftWidth = background->marginSize(Plasma::LeftMargin);
|
|
|
|
const int rightWidth = background->marginSize(Plasma::RightMargin);
|
|
|
|
const int bottomHeight = background->marginSize(Plasma::BottomMargin);
|
|
|
|
q->setContentsMargins(leftWidth, topHeight, rightWidth, bottomHeight);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DialogPrivate::adjustView()
|
|
|
|
{
|
|
|
|
if (view && widget) {
|
2008-08-28 16:32:48 +02:00
|
|
|
QSize prevSize = q->size();
|
|
|
|
|
2008-09-10 23:28:16 +02:00
|
|
|
kDebug() << "Widget size:" << widget->size()
|
|
|
|
<< "| Widget size hint:" << widget->effectiveSizeHint(Qt::PreferredSize)
|
|
|
|
<< "| Widget bounding rect:" << widget->boundingRect();
|
|
|
|
|
|
|
|
QRectF boundingRect = widget->boundingRect();
|
|
|
|
boundingRect.setSize(widget->effectiveSizeHint(Qt::PreferredSize));
|
|
|
|
|
2008-08-28 16:32:48 +02:00
|
|
|
//reposition and resize the view.
|
2008-09-10 23:28:16 +02:00
|
|
|
view->setSceneRect(widget->mapToScene(boundingRect).boundingRect());
|
|
|
|
view->resize(view->mapFromScene(view->sceneRect()).boundingRect().size());
|
2008-08-26 01:16:18 +02:00
|
|
|
view->centerOn(widget);
|
2008-08-28 16:32:48 +02:00
|
|
|
|
|
|
|
//set the sizehints correctly:
|
|
|
|
int left, top, right, bottom;
|
|
|
|
q->getContentsMargins(&left, &top, &right, &bottom);
|
2008-10-05 19:58:14 +02:00
|
|
|
|
|
|
|
q->setMinimumSize(qMin(int(widget->minimumSize().width()) + left + right, QWIDGETSIZE_MAX),
|
|
|
|
qMin(int(widget->minimumSize().height()) + top + bottom, QWIDGETSIZE_MAX));
|
|
|
|
q->setMaximumSize(qMin(int(widget->maximumSize().width()) + left + right, QWIDGETSIZE_MAX),
|
|
|
|
qMin(int(widget->maximumSize().height()) + top + bottom, QWIDGETSIZE_MAX));
|
2008-08-28 16:32:48 +02:00
|
|
|
q->updateGeometry();
|
|
|
|
|
|
|
|
if (q->size() != prevSize) {
|
|
|
|
//the size of the dialog has changed, emit the signal:
|
|
|
|
emit q->dialogResized();
|
|
|
|
}
|
2008-08-06 22:41:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-05 23:32:45 +01:00
|
|
|
Dialog::Dialog( QWidget * parent, Qt::WindowFlags f )
|
|
|
|
: QWidget(parent, f),
|
2008-08-06 22:41:10 +02:00
|
|
|
d(new DialogPrivate(this))
|
2007-12-05 23:32:45 +01:00
|
|
|
{
|
2008-04-23 15:07:41 +02:00
|
|
|
d->background = new PanelSvg(this);
|
|
|
|
d->background->setImagePath("dialogs/background");
|
2008-04-16 22:17:36 +02:00
|
|
|
d->background->setEnabledBorders(PanelSvg::AllBorders);
|
2008-04-23 22:42:10 +02:00
|
|
|
d->background->resizePanel(size());
|
2008-02-06 22:07:16 +01:00
|
|
|
|
|
|
|
connect(d->background, SIGNAL(repaintNeeded()), this, SLOT(update()));
|
2008-02-08 21:50:55 +01:00
|
|
|
|
2008-04-23 20:35:03 +02:00
|
|
|
connect(Plasma::Theme::defaultTheme(), SIGNAL(themeChanged()), this, SLOT(themeUpdated()));
|
2008-04-17 15:12:25 +02:00
|
|
|
d->themeUpdated();
|
2008-09-07 18:55:37 +02:00
|
|
|
|
|
|
|
setMouseTracking(true);
|
2007-12-05 23:32:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Dialog::~Dialog()
|
|
|
|
{
|
2008-03-20 08:44:02 +01:00
|
|
|
delete d;
|
2007-12-05 23:32:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Dialog::paintEvent(QPaintEvent *e)
|
|
|
|
{
|
|
|
|
QPainter p(this);
|
|
|
|
p.setRenderHint(QPainter::Antialiasing);
|
|
|
|
p.setClipRect(e->rect());
|
|
|
|
p.setCompositionMode(QPainter::CompositionMode_Source );
|
|
|
|
p.fillRect(rect(), Qt::transparent);
|
2008-07-27 19:20:27 +02:00
|
|
|
d->background->paintPanel(&p);
|
2008-09-07 18:55:37 +02:00
|
|
|
|
|
|
|
//we set the resize handlers
|
|
|
|
d->resizeAreas.clear();
|
2008-10-01 23:53:38 +02:00
|
|
|
if (d->resizeCorners & Dialog::NorthEast) {
|
|
|
|
d->resizeAreas[Dialog::NorthEast] = QRect(rect().right() - resizeAreaMargin, 0,
|
|
|
|
resizeAreaMargin, resizeAreaMargin);
|
|
|
|
}
|
2008-09-07 18:55:37 +02:00
|
|
|
|
2008-10-01 23:53:38 +02:00
|
|
|
if (d->resizeCorners & Dialog::NorthWest) {
|
|
|
|
d->resizeAreas[Dialog::NorthWest] = QRect(0,0, resizeAreaMargin, resizeAreaMargin);
|
|
|
|
}
|
2008-09-07 18:55:37 +02:00
|
|
|
|
2008-10-01 23:53:38 +02:00
|
|
|
if (d->resizeCorners & Dialog::SouthEast) {
|
|
|
|
d->resizeAreas[Dialog::SouthEast] = QRect(rect().right() - resizeAreaMargin,
|
|
|
|
rect().bottom() - resizeAreaMargin,
|
|
|
|
resizeAreaMargin, resizeAreaMargin);
|
|
|
|
}
|
2008-09-07 18:55:37 +02:00
|
|
|
|
2008-10-01 23:53:38 +02:00
|
|
|
if (d->resizeCorners & Dialog::SouthWest) {
|
|
|
|
d->resizeAreas[Dialog::SouthWest] = QRect(0, rect().bottom() - resizeAreaMargin,
|
|
|
|
resizeAreaMargin, resizeAreaMargin);
|
|
|
|
}
|
2008-09-07 18:55:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Dialog::mouseMoveEvent(QMouseEvent *event)
|
|
|
|
{
|
|
|
|
if (d->resizeAreas[Dialog::NorthEast].contains(event->pos()) && d->resizeCorners & Dialog::NorthEast) {
|
|
|
|
setCursor(Qt::SizeBDiagCursor);
|
|
|
|
} else if (d->resizeAreas[Dialog::NorthWest].contains(event->pos()) && d->resizeCorners & Dialog::NorthWest) {
|
|
|
|
setCursor(Qt::SizeFDiagCursor);
|
|
|
|
} else if (d->resizeAreas[Dialog::SouthEast].contains(event->pos()) && d->resizeCorners & Dialog::SouthEast) {
|
|
|
|
setCursor(Qt::SizeFDiagCursor);
|
|
|
|
} else if (d->resizeAreas[Dialog::SouthWest].contains(event->pos()) && d->resizeCorners & Dialog::SouthWest) {
|
|
|
|
setCursor(Qt::SizeBDiagCursor);
|
|
|
|
} else {
|
|
|
|
unsetCursor();
|
|
|
|
}
|
|
|
|
|
|
|
|
// here we take care of resize..
|
|
|
|
if (d->resizeStartCorner != Dialog::NoCorner ) {
|
|
|
|
int newWidth;
|
|
|
|
int newHeight;
|
|
|
|
QPoint position;
|
|
|
|
|
|
|
|
switch(d->resizeStartCorner) {
|
|
|
|
case Dialog::NorthEast:
|
|
|
|
newWidth = event->x();
|
|
|
|
newHeight = height() - event->y();
|
|
|
|
position = QPoint(x(), y() + height() - newHeight);
|
|
|
|
break;
|
|
|
|
case Dialog::NorthWest:
|
|
|
|
newWidth = width() - event->x();
|
|
|
|
newHeight = height() - event->y();
|
|
|
|
position = QPoint(x() + width() - newWidth, y() + height() - newHeight);
|
|
|
|
break;
|
|
|
|
case Dialog::SouthWest:
|
|
|
|
newWidth = width() - event->x();
|
|
|
|
newHeight = event->y();
|
|
|
|
position = QPoint(x() + width() - newWidth, y());
|
|
|
|
break;
|
|
|
|
case Dialog::SouthEast:
|
|
|
|
newWidth = event->x();
|
|
|
|
newHeight = event->y();
|
|
|
|
position = QPoint(x(), y());
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
newWidth = width();
|
|
|
|
newHeight = height();
|
|
|
|
position = QPoint(x(), y());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// let's check for limitations
|
|
|
|
if (newWidth < minimumWidth() || newWidth > maximumWidth()) {
|
|
|
|
newWidth = width();
|
|
|
|
position.setX(x());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (newHeight < minimumHeight() || newHeight > maximumHeight()) {
|
|
|
|
newHeight = height();
|
|
|
|
position.setY(y());
|
|
|
|
}
|
|
|
|
|
|
|
|
setGeometry(QRect(position, QSize(newWidth, newHeight)));
|
|
|
|
}
|
|
|
|
|
|
|
|
QWidget::mouseMoveEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Dialog::mousePressEvent(QMouseEvent *event)
|
|
|
|
{
|
|
|
|
if (d->resizeAreas[Dialog::NorthEast].contains(event->pos()) && d->resizeCorners & Dialog::NorthEast) {
|
|
|
|
d->resizeStartCorner = Dialog::NorthEast;
|
|
|
|
|
|
|
|
} else if (d->resizeAreas[Dialog::NorthWest].contains(event->pos()) && d->resizeCorners & Dialog::NorthWest) {
|
|
|
|
d->resizeStartCorner = Dialog::NorthWest;
|
|
|
|
|
|
|
|
} else if (d->resizeAreas[Dialog::SouthEast].contains(event->pos()) && d->resizeCorners & Dialog::SouthEast) {
|
|
|
|
d->resizeStartCorner = Dialog::SouthEast;
|
|
|
|
|
|
|
|
} else if (d->resizeAreas[Dialog::SouthWest].contains(event->pos()) && d->resizeCorners & Dialog::SouthWest) {
|
|
|
|
d->resizeStartCorner = Dialog::SouthWest;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
d->resizeStartCorner = Dialog::NoCorner;
|
|
|
|
}
|
|
|
|
|
|
|
|
QWidget::mousePressEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Dialog::mouseReleaseEvent(QMouseEvent *event)
|
|
|
|
{
|
2008-09-08 14:33:46 +02:00
|
|
|
if (d->resizeStartCorner != Dialog::NoCorner) {
|
|
|
|
d->resizeStartCorner = Dialog::NoCorner;
|
|
|
|
emit dialogResized();
|
|
|
|
}
|
2008-09-07 18:55:37 +02:00
|
|
|
|
|
|
|
QWidget::mouseReleaseEvent(event);
|
2007-12-05 23:32:45 +01:00
|
|
|
}
|
|
|
|
|
2008-02-06 22:07:16 +01:00
|
|
|
void Dialog::resizeEvent(QResizeEvent *e)
|
2007-12-05 23:32:45 +01:00
|
|
|
{
|
2008-04-23 22:42:10 +02:00
|
|
|
d->background->resizePanel(e->size());
|
2008-03-05 22:03:27 +01:00
|
|
|
|
|
|
|
setMask(d->background->mask());
|
2008-10-05 19:58:14 +02:00
|
|
|
|
|
|
|
if (d->resizeStartCorner != Dialog::NoCorner && d->view && d->widget) {
|
|
|
|
d->widget->resize(d->view->size());
|
|
|
|
d->view->setSceneRect(d->widget->mapToScene(d->widget->boundingRect()).boundingRect());
|
|
|
|
d->view->centerOn(d->widget);
|
|
|
|
}
|
2007-12-05 23:32:45 +01:00
|
|
|
}
|
|
|
|
|
2008-08-06 22:41:10 +02:00
|
|
|
void Dialog::setGraphicsWidget(QGraphicsWidget *widget)
|
|
|
|
{
|
2008-08-07 12:34:14 +02:00
|
|
|
if (d->widget) {
|
|
|
|
d->widget->removeEventFilter(this);
|
|
|
|
}
|
|
|
|
|
2008-08-06 22:41:10 +02:00
|
|
|
d->widget = widget;
|
|
|
|
|
|
|
|
if (widget) {
|
|
|
|
if (!layout()) {
|
|
|
|
QVBoxLayout *lay = new QVBoxLayout(this);
|
|
|
|
lay->setMargin(0);
|
|
|
|
lay->setSpacing(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!d->view) {
|
|
|
|
d->view = new QGraphicsView(this);
|
|
|
|
d->view->setFrameShape(QFrame::NoFrame);
|
|
|
|
d->view->viewport()->setAutoFillBackground(false);
|
|
|
|
layout()->addWidget(d->view);
|
|
|
|
}
|
|
|
|
|
|
|
|
d->view->setScene(widget->scene());
|
|
|
|
d->adjustView();
|
|
|
|
|
|
|
|
adjustSize();
|
|
|
|
|
2008-08-07 12:34:14 +02:00
|
|
|
widget->installEventFilter(this);
|
2008-08-06 22:41:10 +02:00
|
|
|
} else {
|
|
|
|
delete d->view;
|
|
|
|
d->view = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QGraphicsWidget *Dialog::graphicsWidget()
|
|
|
|
{
|
|
|
|
return d->widget;
|
|
|
|
}
|
|
|
|
|
2008-08-07 12:34:14 +02:00
|
|
|
bool Dialog::eventFilter(QObject *watched, QEvent *event)
|
|
|
|
{
|
2008-10-05 19:58:14 +02:00
|
|
|
if (d->resizeStartCorner == Dialog::NoCorner && watched == d->widget &&
|
|
|
|
(event->type() == QEvent::GraphicsSceneResize || event->type() == QEvent::GraphicsSceneMove)) {
|
2008-08-07 12:34:14 +02:00
|
|
|
d->adjustView();
|
|
|
|
}
|
|
|
|
|
|
|
|
return QWidget::eventFilter(watched, event);
|
|
|
|
}
|
|
|
|
|
2008-09-01 12:45:33 +02:00
|
|
|
void Dialog::hideEvent(QHideEvent * event)
|
|
|
|
{
|
|
|
|
Q_UNUSED(event);
|
|
|
|
emit dialogVisible(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Dialog::showEvent(QShowEvent * event)
|
|
|
|
{
|
|
|
|
Q_UNUSED(event);
|
|
|
|
emit dialogVisible(true);
|
|
|
|
}
|
|
|
|
|
2008-09-07 18:55:37 +02:00
|
|
|
void Dialog::setResizeHandleCorners(ResizeCorners corners)
|
|
|
|
{
|
|
|
|
d->resizeCorners = corners;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
Dialog::ResizeCorners Dialog::resizeCorners() const
|
|
|
|
{
|
|
|
|
return d->resizeCorners;
|
|
|
|
}
|
|
|
|
|
2008-10-01 23:53:38 +02:00
|
|
|
bool Dialog::inControlArea(const QPoint &point)
|
|
|
|
{
|
|
|
|
foreach(const QRect &r, d->resizeAreas) {
|
|
|
|
if (r.contains(point)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-12-05 23:32:45 +01:00
|
|
|
}
|
|
|
|
#include "dialog.moc"
|