diff --git a/dialog.cpp b/dialog.cpp index b516f2480..cc5991a58 100644 --- a/dialog.cpp +++ b/dialog.cpp @@ -1,7 +1,8 @@ /*************************************************************************** + * Copyright (C) 2008 by Alessandro Diaferia * * Copyright (C) 2007 by Alexis Ménard * * Copyright (C) 2007 Sebastian Kuegler * - * Copyright (C) 2006 Aaron Seigo * + * Copyright (C) 2006 Aaron Seigo * * * * 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 * @@ -44,6 +45,7 @@ #include #endif +const int resizeAreaMargin = 20; namespace Plasma { @@ -55,7 +57,8 @@ public: : q(dialog), background(0), view(0), - widget(0) + widget(0), + resizeCorners(Dialog::NoCorner) { } @@ -74,6 +77,9 @@ public: Plasma::PanelSvg *background; QGraphicsView *view; QGraphicsWidget *widget; + Dialog::ResizeCorners resizeCorners; + QMap resizeAreas; + Dialog::ResizeCorner resizeStartCorner; }; void DialogPrivate::themeUpdated() @@ -125,6 +131,8 @@ Dialog::Dialog( QWidget * parent, Qt::WindowFlags f ) connect(Plasma::Theme::defaultTheme(), SIGNAL(themeChanged()), this, SLOT(themeUpdated())); d->themeUpdated(); + + setMouseTracking(true); } Dialog::~Dialog() @@ -140,6 +148,117 @@ void Dialog::paintEvent(QPaintEvent *e) p.setCompositionMode(QPainter::CompositionMode_Source ); p.fillRect(rect(), Qt::transparent); d->background->paintPanel(&p); + + //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); } void Dialog::resizeEvent(QResizeEvent *e) @@ -210,5 +329,16 @@ void Dialog::showEvent(QShowEvent * event) emit dialogVisible(true); } +void Dialog::setResizeHandleCorners(ResizeCorners corners) +{ + d->resizeCorners = corners; + update(); +} + +Dialog::ResizeCorners Dialog::resizeCorners() const +{ + return d->resizeCorners; +} + } #include "dialog.moc" diff --git a/dialog.h b/dialog.h index 6d7a43abc..4d49d3275 100644 --- a/dialog.h +++ b/dialog.h @@ -1,4 +1,5 @@ /*************************************************************************** + * Copyright (C) 2008 by Alessandro Diaferia * * Copyright (C) 2007 by Alexis Ménard * * Copyright (C) 2007 Sebastian Kuegler * * Copyright (C) 2006 Aaron Seigo * @@ -51,6 +52,18 @@ class PLASMA_EXPORT Dialog : public QWidget Q_OBJECT public: + /** + * Use these flags to choose the active resize corners. + */ + enum ResizeCorner { NoCorner = 0, + NorthEast = 1, + SouthEast = 2, + NorthWest = 4, + SouthWest = 8, + All = NorthEast | SouthEast | NorthWest | SouthWest + }; + Q_DECLARE_FLAGS(ResizeCorners, ResizeCorner) + /** * @arg parent the parent widget, for plasmoids, this is usually 0. * @arg f the Qt::WindowFlags, default is to not show a windowborder. @@ -61,6 +74,17 @@ class PLASMA_EXPORT Dialog : public QWidget void setGraphicsWidget(QGraphicsWidget *widget); QGraphicsWidget *graphicsWidget(); + /** + * @arg corners the corners the resize handlers should be placed in. + */ + void setResizeHandleCorners(ResizeCorners corners); + + /** + * Convenience method to get the enabled resize corners. + * @return which resize corners are active. + */ + ResizeCorners resizeCorners() const; + Q_SIGNALS: /** * Fires when the dialog automatically resizes. @@ -81,6 +105,9 @@ class PLASMA_EXPORT Dialog : public QWidget bool eventFilter(QObject *watched, QEvent *event); void hideEvent (QHideEvent * event); void showEvent (QShowEvent * event); + void mouseMoveEvent (QMouseEvent * event); + void mousePressEvent (QMouseEvent * event); + void mouseReleaseEvent (QMouseEvent * event); private: DialogPrivate * const d; @@ -94,4 +121,6 @@ class PLASMA_EXPORT Dialog : public QWidget } // Plasma namespace +Q_DECLARE_OPERATORS_FOR_FLAGS(Plasma::Dialog::ResizeCorners) + #endif