2007-12-05 23:32:45 +01:00
|
|
|
/***************************************************************************
|
2008-09-07 18:55:37 +02:00
|
|
|
* Copyright (C) 2008 by Alessandro Diaferia <alediaferia@gmail.com> *
|
2007-12-05 23:32:45 +01:00
|
|
|
* Copyright (C) 2007 by Alexis Ménard <darktears31@gmail.com> *
|
|
|
|
* Copyright (C) 2007 Sebastian Kuegler <sebas@kde.org> *
|
2008-09-07 18:55:37 +02:00
|
|
|
* Copyright (C) 2006 Aaron Seigo <aseigo@kde.org> *
|
2007-12-05 23:32:45 +01:00
|
|
|
* *
|
|
|
|
* 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"
|
|
|
|
|
|
|
|
#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),
|
|
|
|
resizeCorners(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-09-04 19:09:34 +02:00
|
|
|
kDebug() << "resize the view.";
|
2008-08-28 16:32:48 +02:00
|
|
|
QSize prevSize = q->size();
|
|
|
|
|
|
|
|
//reposition and resize the view.
|
2008-08-06 22:41:10 +02:00
|
|
|
view->setSceneRect(widget->mapToScene(widget->boundingRect()).boundingRect());
|
2008-09-04 19:09:34 +02:00
|
|
|
view->resize(widget->size().toSize());
|
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);
|
|
|
|
q->setMinimumSize(view->size().width() + left + right,
|
|
|
|
view->size().height() + top + bottom);
|
|
|
|
q->setMaximumSize(view->size().width() + left + right,
|
|
|
|
view->size().height() + top + bottom);
|
|
|
|
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();
|
|
|
|
d->resizeAreas[Dialog::NorthEast] = QRect(rect().right() - resizeAreaMargin, 0,
|
|
|
|
resizeAreaMargin, resizeAreaMargin);
|
|
|
|
|
|
|
|
d->resizeAreas[Dialog::NorthWest] = QRect(0,0, resizeAreaMargin, resizeAreaMargin);
|
|
|
|
|
|
|
|
d->resizeAreas[Dialog::SouthEast] = QRect(rect().right() - resizeAreaMargin,
|
|
|
|
rect().bottom() - resizeAreaMargin,
|
|
|
|
resizeAreaMargin, resizeAreaMargin);
|
|
|
|
|
|
|
|
d->resizeAreas[Dialog::SouthWest] = QRect(0, rect().bottom() - resizeAreaMargin,
|
|
|
|
resizeAreaMargin, resizeAreaMargin);
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
d->resizeStartCorner = Dialog::NoCorner;
|
|
|
|
|
|
|
|
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());
|
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-08-07 16:17:55 +02:00
|
|
|
if (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;
|
|
|
|
}
|
|
|
|
|
2007-12-05 23:32:45 +01:00
|
|
|
}
|
|
|
|
#include "dialog.moc"
|