2007-03-01 20:58:19 +01:00
|
|
|
/*
|
2007-08-06 13:20:02 +02:00
|
|
|
* Copyright 2007 by Alexander Wiedenbruch <mail@wiedenbruch.de>
|
2007-05-22 18:48:34 +02:00
|
|
|
* and Matias Valdenegro <mvaldenegro@informatica.utem.cl>
|
2007-03-01 20:58:19 +01:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Library General Public License version 2 as
|
|
|
|
* published by the Free Software Foundation
|
|
|
|
*
|
|
|
|
* 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 Library 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.
|
|
|
|
*/
|
|
|
|
|
2007-05-22 18:48:34 +02:00
|
|
|
|
2007-05-30 18:47:36 +02:00
|
|
|
#include "widget.h"
|
|
|
|
|
2007-08-05 16:24:24 +02:00
|
|
|
#include <cmath>
|
2007-08-05 14:58:40 +02:00
|
|
|
#include <limits>
|
2007-07-23 00:16:40 +02:00
|
|
|
|
2007-05-23 19:18:43 +02:00
|
|
|
#include <QtCore/QList>
|
2007-08-01 22:51:27 +02:00
|
|
|
#include <QPainter>
|
2007-05-23 19:18:43 +02:00
|
|
|
|
2007-08-05 14:58:40 +02:00
|
|
|
#include <KDebug>
|
|
|
|
|
2007-05-22 18:48:34 +02:00
|
|
|
#include "layout.h"
|
2007-08-01 22:51:27 +02:00
|
|
|
#include "plasma/plasma.h"
|
2007-03-01 20:58:19 +01:00
|
|
|
|
2007-03-02 06:27:33 +01:00
|
|
|
namespace Plasma
|
|
|
|
{
|
|
|
|
|
2007-03-03 14:13:39 +01:00
|
|
|
class Widget::Private
|
|
|
|
{
|
|
|
|
public:
|
2007-07-12 20:11:50 +02:00
|
|
|
Private()
|
2007-08-22 22:13:24 +02:00
|
|
|
: minimumSize(0,0)
|
|
|
|
, maximumSize(INFINITY,INFINITY)
|
|
|
|
, parent(0)
|
2007-08-13 11:01:00 +02:00
|
|
|
, opacity(1.0)
|
2007-07-12 20:11:50 +02:00
|
|
|
{ }
|
2007-03-20 15:21:56 +01:00
|
|
|
~Private() { }
|
2007-05-22 18:48:34 +02:00
|
|
|
|
2007-07-23 00:16:40 +02:00
|
|
|
QSizeF size;
|
2007-07-27 08:35:39 +02:00
|
|
|
QSizeF minimumSize;
|
|
|
|
QSizeF maximumSize;
|
2007-05-22 18:48:34 +02:00
|
|
|
|
2007-07-12 19:54:58 +02:00
|
|
|
Widget *parent;
|
|
|
|
QList<Widget *> childList;
|
2007-08-01 22:51:27 +02:00
|
|
|
|
2007-08-13 11:01:00 +02:00
|
|
|
qreal opacity;
|
|
|
|
|
2007-08-01 22:51:27 +02:00
|
|
|
bool shouldPaint(QPainter *painter, const QTransform &transform);
|
2007-03-03 14:13:39 +01:00
|
|
|
};
|
2007-03-01 20:58:19 +01:00
|
|
|
|
2007-08-13 11:01:00 +02:00
|
|
|
QGraphicsItem* Widget::graphicsItem()
|
|
|
|
{
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2007-08-01 22:51:27 +02:00
|
|
|
bool Widget::Private::shouldPaint(QPainter *painter, const QTransform &transform)
|
|
|
|
{
|
|
|
|
qreal zoomLevel = painter->transform().m11() / transform.m11();
|
2007-08-28 21:12:13 +02:00
|
|
|
//return (fabs(zoomLevel - scalingFactor(Plasma::DesktopZoom))) < std::numeric_limits<double>::epsilon();
|
|
|
|
return true;
|
2007-08-01 22:51:27 +02:00
|
|
|
}
|
|
|
|
|
2007-08-17 17:05:01 +02:00
|
|
|
Widget::Widget(QGraphicsItem *parent,QObject* parentObject)
|
|
|
|
: QObject(parentObject),
|
|
|
|
QGraphicsItem(parent),
|
2007-03-03 14:13:39 +01:00
|
|
|
d(new Private)
|
2007-03-01 20:58:19 +01:00
|
|
|
{
|
2007-07-25 01:31:52 +02:00
|
|
|
setFlag(QGraphicsItem::ItemClipsToShape, true);
|
2007-07-28 21:27:09 +02:00
|
|
|
setFlag(QGraphicsItem::ItemClipsChildrenToShape, true);
|
2007-07-23 00:16:40 +02:00
|
|
|
d->parent = dynamic_cast<Widget *>(parent);
|
2007-05-22 18:48:34 +02:00
|
|
|
|
2007-07-12 20:11:50 +02:00
|
|
|
if (d->parent) {
|
|
|
|
d->parent->addChild(this);
|
2007-08-31 17:32:10 +02:00
|
|
|
d->parent->updateGeometry();
|
2007-07-12 19:54:58 +02:00
|
|
|
}
|
2007-03-01 20:58:19 +01:00
|
|
|
}
|
|
|
|
|
2007-03-03 14:13:39 +01:00
|
|
|
Widget::~Widget()
|
2007-03-01 20:58:19 +01:00
|
|
|
{
|
2007-03-03 14:13:39 +01:00
|
|
|
delete d;
|
2007-03-01 20:58:19 +01:00
|
|
|
}
|
2007-03-02 06:27:33 +01:00
|
|
|
|
2007-08-13 11:01:00 +02:00
|
|
|
void Widget::setOpacity(qreal opacity)
|
|
|
|
{
|
|
|
|
d->opacity = opacity;
|
|
|
|
}
|
2007-08-16 17:56:00 +02:00
|
|
|
qreal Widget::opacity() const
|
|
|
|
{
|
|
|
|
return d->opacity;
|
|
|
|
}
|
2007-08-13 11:01:00 +02:00
|
|
|
|
2007-05-22 18:48:34 +02:00
|
|
|
Qt::Orientations Widget::expandingDirections() const
|
|
|
|
{
|
2007-08-31 17:32:10 +02:00
|
|
|
return Qt::Horizontal | Qt::Vertical;
|
2007-05-22 18:48:34 +02:00
|
|
|
}
|
|
|
|
|
2007-07-27 08:35:39 +02:00
|
|
|
void Widget::setMinimumSize(const QSizeF& size)
|
2007-05-22 18:48:34 +02:00
|
|
|
{
|
2007-07-27 08:35:39 +02:00
|
|
|
d->minimumSize = size;
|
2007-05-22 18:48:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QSizeF Widget::minimumSize() const
|
|
|
|
{
|
2007-07-27 08:35:39 +02:00
|
|
|
return d->minimumSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::setMaximumSize(const QSizeF& size)
|
|
|
|
{
|
|
|
|
d->maximumSize = size;
|
|
|
|
}
|
|
|
|
|
|
|
|
QSizeF Widget::maximumSize() const
|
|
|
|
{
|
|
|
|
return d->maximumSize;
|
2007-05-22 18:48:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Widget::hasHeightForWidth() const
|
|
|
|
{
|
2007-07-12 19:54:58 +02:00
|
|
|
return false;
|
2007-05-22 18:48:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
qreal Widget::heightForWidth(qreal w) const
|
|
|
|
{
|
2007-07-12 19:54:58 +02:00
|
|
|
Q_UNUSED(w);
|
2007-05-22 18:48:34 +02:00
|
|
|
|
2007-07-12 19:54:58 +02:00
|
|
|
return -1.0;
|
2007-05-22 18:48:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Widget::hasWidthForHeight() const
|
|
|
|
{
|
2007-07-12 19:54:58 +02:00
|
|
|
return false;
|
2007-05-22 18:48:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
qreal Widget::widthForHeight(qreal h) const
|
|
|
|
{
|
2007-07-12 19:54:58 +02:00
|
|
|
Q_UNUSED(h);
|
2007-05-22 18:48:34 +02:00
|
|
|
|
2007-07-12 19:54:58 +02:00
|
|
|
return -1.0;
|
2007-05-22 18:48:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QRectF Widget::geometry() const
|
|
|
|
{
|
2007-08-17 17:05:01 +02:00
|
|
|
return QRectF(pos(),d->size);
|
2007-07-23 00:16:40 +02:00
|
|
|
}
|
|
|
|
|
2007-08-17 17:05:01 +02:00
|
|
|
#if 0
|
2007-07-23 00:16:40 +02:00
|
|
|
QRectF Widget::localGeometry() const
|
|
|
|
{
|
2007-08-17 17:05:01 +02:00
|
|
|
return QRectF(QPointF(0.0f, 0.0f), boundingRect().size);
|
2007-05-22 18:48:34 +02:00
|
|
|
}
|
2007-08-17 17:05:01 +02:00
|
|
|
#endif
|
2007-05-22 18:48:34 +02:00
|
|
|
|
|
|
|
void Widget::setGeometry(const QRectF& geometry)
|
|
|
|
{
|
2007-08-17 18:04:49 +02:00
|
|
|
if ( d->size != geometry.size() ) {
|
2007-08-13 11:01:00 +02:00
|
|
|
prepareGeometryChange();
|
2007-08-17 18:04:49 +02:00
|
|
|
qreal width = qBound(d->minimumSize.width(), geometry.size().width(), d->maximumSize.width());
|
|
|
|
qreal height = qBound(d->minimumSize.height(), geometry.size().height(), d->maximumSize.height());
|
|
|
|
|
|
|
|
d->size = QSizeF(width, height);
|
|
|
|
|
2007-08-31 17:32:10 +02:00
|
|
|
if ( layout() )
|
|
|
|
layout()->setGeometry(boundingRect());
|
2007-08-13 11:01:00 +02:00
|
|
|
}
|
2007-08-31 17:32:10 +02:00
|
|
|
|
|
|
|
setPos(geometry.topLeft() - boundingRect().topLeft());
|
2007-05-22 18:48:34 +02:00
|
|
|
|
2007-07-12 19:54:58 +02:00
|
|
|
update();
|
2007-05-22 18:48:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::updateGeometry()
|
|
|
|
{
|
2007-08-03 20:56:14 +02:00
|
|
|
prepareGeometryChange();
|
2007-08-31 17:32:10 +02:00
|
|
|
|
|
|
|
Widget *widget = this;
|
|
|
|
while ( widget ) {
|
|
|
|
if ( widget->parent() == 0 || widget->parent()->layout() != 0 ) {
|
|
|
|
if ( widget->layout() ) {
|
|
|
|
widget->layout()->update();
|
|
|
|
widget->update();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
widget = widget->parent();
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
prepareGeometryChange();
|
2007-08-03 20:56:14 +02:00
|
|
|
|
2007-07-12 19:54:58 +02:00
|
|
|
if (layout()) {
|
2007-08-13 11:01:00 +02:00
|
|
|
// kDebug() << (void *) this << " updating geometry to " << size();
|
2007-08-31 17:32:10 +02:00
|
|
|
//
|
|
|
|
//qDebug() << "Widget" << metaObject()->className() << "updating geometry to " << boundingRect();
|
2007-08-17 18:28:54 +02:00
|
|
|
layout()->setGeometry(boundingRect());
|
2007-07-12 19:54:58 +02:00
|
|
|
}
|
2007-08-31 17:32:10 +02:00
|
|
|
#endif
|
2007-05-22 18:48:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QSizeF Widget::sizeHint() const
|
|
|
|
{
|
2007-08-17 17:05:01 +02:00
|
|
|
if (layout()) {
|
|
|
|
return layout()->sizeHint();
|
|
|
|
} else {
|
|
|
|
return QSizeF();
|
|
|
|
}
|
2007-08-03 20:56:14 +02:00
|
|
|
}
|
|
|
|
|
2007-05-22 18:48:34 +02:00
|
|
|
QSizeF Widget::size() const
|
|
|
|
{
|
2007-08-17 17:05:01 +02:00
|
|
|
return geometry().size();
|
2007-05-22 18:48:34 +02:00
|
|
|
}
|
|
|
|
QRectF Widget::boundingRect() const
|
|
|
|
{
|
2007-08-17 17:05:01 +02:00
|
|
|
return QRectF(QPointF(0,0),geometry().size());
|
2007-05-22 18:48:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::resize(const QSizeF& size)
|
|
|
|
{
|
2007-07-23 00:16:40 +02:00
|
|
|
setGeometry(QRectF(pos(), size));
|
2007-05-22 18:48:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::resize(qreal w, qreal h)
|
|
|
|
{
|
2007-07-12 19:54:58 +02:00
|
|
|
resize(QSizeF(w, h));
|
2007-05-22 18:48:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Widget *Widget::parent() const
|
|
|
|
{
|
2007-07-12 19:54:58 +02:00
|
|
|
return d->parent;
|
2007-05-22 18:48:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::addChild(Widget *w)
|
|
|
|
{
|
2007-07-12 19:54:58 +02:00
|
|
|
if (!w) {
|
|
|
|
return;
|
|
|
|
}
|
2007-05-22 18:48:34 +02:00
|
|
|
|
2007-07-12 19:54:58 +02:00
|
|
|
w->reparent(this);
|
|
|
|
d->childList.append(w);
|
2007-05-22 18:48:34 +02:00
|
|
|
|
2007-07-12 19:54:58 +02:00
|
|
|
qDebug("Added Child Widget : %p", (void*)w);
|
2007-05-22 18:48:34 +02:00
|
|
|
|
2007-07-12 19:54:58 +02:00
|
|
|
if (layout()) {
|
|
|
|
layout()->addItem(w);
|
|
|
|
updateGeometry();
|
|
|
|
}
|
2007-05-22 18:48:34 +02:00
|
|
|
}
|
|
|
|
|
2007-07-12 20:11:50 +02:00
|
|
|
void Widget::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
|
2007-08-01 22:51:27 +02:00
|
|
|
{
|
2007-08-13 11:01:00 +02:00
|
|
|
painter->setOpacity(d->opacity);
|
|
|
|
|
2007-08-01 22:51:27 +02:00
|
|
|
if (d->shouldPaint(painter, transform())) {
|
|
|
|
paintWidget(painter, option, widget);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::paintWidget(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
|
2007-07-12 20:11:50 +02:00
|
|
|
{
|
2007-07-23 00:16:40 +02:00
|
|
|
Q_UNUSED(painter);
|
|
|
|
Q_UNUSED(option);
|
|
|
|
Q_UNUSED(widget);
|
|
|
|
|
2007-08-01 22:51:27 +02:00
|
|
|
// Replaced by widget's own function
|
2007-07-12 20:11:50 +02:00
|
|
|
}
|
|
|
|
|
2007-05-22 18:48:34 +02:00
|
|
|
void Widget::reparent(Widget *w)
|
|
|
|
{
|
2007-07-12 19:54:58 +02:00
|
|
|
d->parent = w;
|
|
|
|
setParentItem(w);
|
|
|
|
update();
|
2007-05-22 18:48:34 +02:00
|
|
|
}
|
|
|
|
|
2007-03-02 06:27:33 +01:00
|
|
|
} // Plasma namespace
|
|
|
|
|