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
|
2007-09-14 22:17:11 +02:00
|
|
|
* it under the terms of the GNU Library General Public License as
|
2007-09-14 21:06:18 +02:00
|
|
|
* published by the Free Software Foundation; either version 2, or
|
|
|
|
* (at your option) any later version.
|
2007-03-01 20:58:19 +01:00
|
|
|
*
|
|
|
|
* 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-09-13 21:33:44 +02:00
|
|
|
#include <QApplication>
|
2007-12-08 04:35:11 +01:00
|
|
|
#include <QGraphicsScene>
|
2007-12-21 06:33:17 +01:00
|
|
|
#include <QGraphicsSceneHoverEvent>
|
2007-12-08 04:35:11 +01:00
|
|
|
#include <QGraphicsView>
|
2007-12-21 06:33:17 +01:00
|
|
|
#include <QHelpEvent>
|
2007-09-13 21:33:44 +02:00
|
|
|
#include <QList>
|
2007-08-01 22:51:27 +02:00
|
|
|
#include <QPainter>
|
2007-09-10 21:06:58 +02:00
|
|
|
#include <QPixmapCache>
|
|
|
|
#include <QStyleOptionGraphicsItem>
|
2007-05-23 19:18:43 +02:00
|
|
|
|
2007-08-05 14:58:40 +02:00
|
|
|
#include <KDebug>
|
|
|
|
|
2007-11-19 23:42:56 +01:00
|
|
|
#include "layouts/freelayout.h"
|
2007-08-01 22:51:27 +02:00
|
|
|
#include "plasma/plasma.h"
|
2007-12-21 06:33:17 +01:00
|
|
|
#include "tooltip_p.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-10-24 08:54:26 +02:00
|
|
|
: minimumSize(0,0),
|
|
|
|
maximumSize(std::numeric_limits<qreal>::infinity(),
|
|
|
|
std::numeric_limits<qreal>::infinity()),
|
|
|
|
opacity(1.0),
|
|
|
|
cachePaintMode(Widget::NoCacheMode),
|
|
|
|
wasMovable(false)
|
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-08-13 11:01:00 +02:00
|
|
|
qreal opacity;
|
2007-09-13 07:34:11 +02:00
|
|
|
|
2007-09-10 21:06:58 +02:00
|
|
|
// Replace with CacheMode in 4.4
|
|
|
|
#if QT_VERSION >= 0x040400
|
|
|
|
#warning Replace Plasma::Widget::CachePaintMode with QGraphicsItem::CacheMode
|
|
|
|
#endif
|
2007-09-28 07:45:10 +02:00
|
|
|
Widget::CachePaintMode cachePaintMode;
|
2007-09-10 21:06:58 +02:00
|
|
|
QSize cacheSize;
|
|
|
|
QString cacheKey;
|
|
|
|
QRectF cacheInvalidated;
|
2007-08-13 11:01:00 +02:00
|
|
|
|
2007-10-24 08:54:26 +02:00
|
|
|
bool wasMovable;
|
|
|
|
|
2007-08-01 22:51:27 +02:00
|
|
|
bool shouldPaint(QPainter *painter, const QTransform &transform);
|
2007-12-21 06:33:17 +01:00
|
|
|
ToolTipData toolTip;
|
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-12-08 04:35:11 +01:00
|
|
|
QGraphicsView *Widget::view() const
|
|
|
|
{
|
|
|
|
// It's assumed that we won't be visible on more than one view here.
|
|
|
|
// Anything that actually needs view() should only really care about
|
|
|
|
// one of them anyway though.
|
|
|
|
if (!scene()) {
|
|
|
|
return 0;
|
|
|
|
}
|
2007-12-30 12:44:25 +01:00
|
|
|
|
2007-12-08 04:35:11 +01:00
|
|
|
foreach (QGraphicsView *view, scene()->views()) {
|
2007-12-30 12:44:25 +01:00
|
|
|
if (view->sceneRect().intersects(sceneBoundingRect()) ||
|
|
|
|
view->sceneRect().contains(scenePos())) {
|
2007-12-08 04:35:11 +01:00
|
|
|
return view;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
QRectF Widget::mapFromView(const QGraphicsView *view, const QRect &rect) const
|
|
|
|
{
|
|
|
|
// TODO: Confirm that adjusted() is needed and is not covering for some
|
|
|
|
// issue elsewhere
|
|
|
|
return mapFromScene(view->mapToScene(rect)).boundingRect().adjusted(0, 0, 1, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
QRect Widget::mapToView(const QGraphicsView *view, const QRectF &rect) const
|
|
|
|
{
|
|
|
|
// TODO: Confirm that adjusted() is needed and is not covering for some
|
|
|
|
// issue elsewhere
|
|
|
|
return view->mapFromScene(mapToScene(rect)).boundingRect().adjusted(0, 0, -1, -1);
|
|
|
|
}
|
|
|
|
|
2007-08-01 22:51:27 +02:00
|
|
|
bool Widget::Private::shouldPaint(QPainter *painter, const QTransform &transform)
|
|
|
|
{
|
2007-10-01 07:06:51 +02:00
|
|
|
Q_UNUSED(painter)
|
|
|
|
Q_UNUSED(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-10-30 22:00:01 +01:00
|
|
|
Widget::Widget(QGraphicsItem *parent, QObject* parentObject)
|
2007-08-17 17:05:01 +02:00
|
|
|
: 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-09-10 22:44:24 +02:00
|
|
|
setCachePaintMode(DeviceCoordinateCacheMode);
|
2007-05-22 18:48:34 +02:00
|
|
|
|
2007-10-30 22:00:01 +01:00
|
|
|
Widget *w = dynamic_cast<Widget *>(parent);
|
|
|
|
if (w) {
|
|
|
|
w->addChild(this);
|
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
|
|
|
{
|
2008-01-07 05:39:36 +01:00
|
|
|
if (ToolTip::self()->currentWidget() == this) {
|
|
|
|
ToolTip::self()->hide();
|
2007-12-31 02:45:48 +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-11-27 07:03:19 +01:00
|
|
|
|
2007-08-16 17:56:00 +02:00
|
|
|
qreal Widget::opacity() const
|
|
|
|
{
|
|
|
|
return d->opacity;
|
|
|
|
}
|
2007-08-13 11:01:00 +02:00
|
|
|
|
2007-09-10 21:06:58 +02:00
|
|
|
void Widget::setCachePaintMode(CachePaintMode mode, const QSize &size)
|
|
|
|
{
|
|
|
|
d->cachePaintMode = mode;
|
|
|
|
if (mode == NoCacheMode) {
|
2007-09-13 07:34:11 +02:00
|
|
|
QPixmapCache::remove(d->cacheKey);
|
|
|
|
d->cacheKey.clear();
|
2007-09-10 21:06:58 +02:00
|
|
|
} else {
|
2007-09-13 07:34:11 +02:00
|
|
|
d->cacheKey = QString("%1").arg(long(this));
|
|
|
|
if (mode == ItemCoordinateCacheMode) {
|
|
|
|
d->cacheSize = size.isNull() ? boundingRect().size().toSize() : size;
|
|
|
|
}
|
2007-09-10 21:06:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget::CachePaintMode Widget::cachePaintMode() const
|
|
|
|
{
|
|
|
|
return d->cachePaintMode;
|
|
|
|
}
|
|
|
|
|
2007-09-10 22:44:24 +02:00
|
|
|
void Widget::update(const QRectF &rect)
|
2007-09-10 21:06:58 +02:00
|
|
|
{
|
2007-09-13 07:34:11 +02:00
|
|
|
if (d->cachePaintMode != NoCacheMode) {
|
|
|
|
d->cacheInvalidated |= rect.isNull() ? boundingRect() : rect;
|
|
|
|
}
|
2007-09-10 22:44:24 +02:00
|
|
|
QGraphicsItem::update(rect);
|
2007-09-10 21:06:58 +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-11-28 19:41:21 +01:00
|
|
|
if (d->size != d->size.expandedTo(size)) {
|
|
|
|
d->size = d->size.expandedTo(size);
|
|
|
|
updateGeometry();
|
|
|
|
}
|
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;
|
2007-11-28 19:41:21 +01:00
|
|
|
if (d->size != d->size.boundedTo(size)) {
|
|
|
|
d->size = d->size.boundedTo(size);
|
|
|
|
updateGeometry();
|
|
|
|
}
|
2007-07-27 08:35:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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-10-01 07:06:51 +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-10-11 21:16:01 +02:00
|
|
|
if (geometry.size().width() > 0 && geometry.size().height() > 0 && 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-09-13 07:34:11 +02:00
|
|
|
if (layout()) {
|
2007-11-17 20:01:02 +01:00
|
|
|
layout()->setGeometry(QRectF(QPointF(0, 0), d->size));
|
2007-09-13 07:34:11 +02:00
|
|
|
}
|
2007-11-04 06:36:39 +01:00
|
|
|
|
|
|
|
if (managingLayout()) {
|
|
|
|
managingLayout()->invalidate();
|
|
|
|
}
|
2007-08-13 11:01:00 +02:00
|
|
|
}
|
2007-08-31 17:32:10 +02:00
|
|
|
|
2007-09-15 06:16:11 +02:00
|
|
|
setPos(geometry.topLeft());
|
2007-07-12 19:54:58 +02:00
|
|
|
update();
|
2007-05-22 18:48:34 +02:00
|
|
|
}
|
|
|
|
|
2007-11-19 08:53:48 +01:00
|
|
|
void Widget::setSize(qreal width, qreal height)
|
|
|
|
{
|
|
|
|
setSize(QSizeF(width, height));
|
|
|
|
}
|
|
|
|
|
2007-09-28 07:45:10 +02:00
|
|
|
void Widget::setSize(const QSizeF& size)
|
|
|
|
{
|
|
|
|
d->size = size;
|
|
|
|
}
|
|
|
|
|
2007-05-22 18:48:34 +02:00
|
|
|
void Widget::updateGeometry()
|
|
|
|
{
|
2007-10-01 07:06:51 +02:00
|
|
|
if (managingLayout()) {
|
2007-09-08 16:01:03 +02:00
|
|
|
managingLayout()->invalidate();
|
2007-10-11 05:30:50 +02:00
|
|
|
} else {
|
|
|
|
setGeometry(QRectF(pos(), sizeHint()));
|
2007-09-08 16:01:03 +02:00
|
|
|
}
|
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 {
|
2007-11-19 08:53:48 +01:00
|
|
|
return d->size;
|
2007-08-17 17:05:01 +02:00
|
|
|
}
|
2007-08-03 20:56:14 +02:00
|
|
|
}
|
|
|
|
|
2007-05-22 18:48:34 +02:00
|
|
|
QSizeF Widget::size() const
|
|
|
|
{
|
2007-11-19 08:53:48 +01:00
|
|
|
return d->size;
|
2007-05-22 18:48:34 +02:00
|
|
|
}
|
2007-09-13 21:33:44 +02:00
|
|
|
|
|
|
|
QFont Widget::font() const
|
|
|
|
{
|
|
|
|
return QApplication::font();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
void Widget::setFont(const QFront& font)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2007-05-22 18:48:34 +02:00
|
|
|
QRectF Widget::boundingRect() const
|
|
|
|
{
|
2007-10-01 07:06:51 +02:00
|
|
|
return QRectF(QPointF(0,0), d->size);
|
2007-05-22 18:48:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::resize(const QSizeF& size)
|
|
|
|
{
|
2007-11-20 02:19:59 +01:00
|
|
|
//FIXME: we need to respect minimum size here i think
|
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-11-27 07:03:19 +01:00
|
|
|
QGraphicsItem *parent = parentItem();
|
|
|
|
|
|
|
|
while (parent) {
|
2007-12-02 12:19:26 +01:00
|
|
|
Widget *parentWidget = dynamic_cast<Widget *>(parent);
|
2007-11-27 07:03:19 +01:00
|
|
|
|
|
|
|
if (parentWidget) {
|
|
|
|
return parentWidget;
|
|
|
|
}
|
|
|
|
|
|
|
|
parent = parent->parentItem();
|
|
|
|
}
|
|
|
|
return 0;
|
2007-05-22 18:48:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::addChild(Widget *w)
|
|
|
|
{
|
2007-11-27 07:03:19 +01:00
|
|
|
if (!w || QGraphicsItem::children().contains(w)) {
|
2007-07-12 19:54:58 +02:00
|
|
|
return;
|
|
|
|
}
|
2007-05-22 18:48:34 +02:00
|
|
|
|
2007-10-30 00:10:45 +01:00
|
|
|
w->setParentItem(this);
|
2007-05-22 18:48:34 +02:00
|
|
|
|
2008-01-08 02:25:09 +01:00
|
|
|
//kDebug() << "Added Child Widget" << (QObject*)w << "our geom is" << geometry();
|
2007-05-22 18:48:34 +02:00
|
|
|
|
2007-07-12 19:54:58 +02:00
|
|
|
if (layout()) {
|
|
|
|
layout()->addItem(w);
|
|
|
|
}
|
2007-10-30 22:00:01 +01:00
|
|
|
|
|
|
|
updateGeometry();
|
2008-01-08 02:25:09 +01:00
|
|
|
//kDebug() << "after the item is added our geom is now" << geometry();
|
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-11-27 07:03:19 +01:00
|
|
|
if (d->opacity < 1.0) {
|
|
|
|
painter->setOpacity(painter->opacity() * d->opacity);
|
|
|
|
}
|
2007-08-13 11:01:00 +02:00
|
|
|
|
2007-10-26 18:35:58 +02:00
|
|
|
/*
|
|
|
|
NOTE: put this back if we end up needing to control when things paint due to, e.g. zooming.
|
2007-09-18 23:30:30 +02:00
|
|
|
if (!d->shouldPaint(painter, transform())) {
|
|
|
|
return;
|
|
|
|
}
|
2007-10-26 18:35:58 +02:00
|
|
|
*/
|
2007-09-18 23:30:30 +02:00
|
|
|
|
|
|
|
if (d->cachePaintMode == NoCacheMode) {
|
|
|
|
paintWidget(painter, option, widget);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cached painting
|
|
|
|
QRectF brect = boundingRect();
|
|
|
|
|
|
|
|
// Fetch the off-screen transparent buffer and exposed area info.
|
|
|
|
QPixmap pix;
|
|
|
|
QPixmapCache::find(d->cacheKey, pix);
|
|
|
|
QRectF exposed = d->cacheInvalidated;
|
|
|
|
|
|
|
|
// Render using item coodinate cache mode.
|
|
|
|
if (d->cachePaintMode == ItemCoordinateCacheMode) {
|
|
|
|
// Recreate the pixmap if it's gone.
|
|
|
|
if (pix.isNull()) {
|
|
|
|
pix = QPixmap(d->cacheSize);
|
2007-10-11 21:16:01 +02:00
|
|
|
pix.fill(Qt::transparent);
|
2007-09-18 23:30:30 +02:00
|
|
|
exposed = brect;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for newly invalidated areas.
|
|
|
|
if (!exposed.isNull()) {
|
|
|
|
d->cacheInvalidated = QRectF();
|
|
|
|
|
|
|
|
QStyleOptionGraphicsItem cacheOption = *option;
|
|
|
|
cacheOption.exposedRect = exposed.toRect(); // <- truncation
|
|
|
|
|
|
|
|
QPainter pixmapPainter(&pix);
|
2007-10-01 07:06:51 +02:00
|
|
|
|
2007-09-18 23:30:30 +02:00
|
|
|
// Fit the item's bounding rect into the pixmap's coordinates.
|
|
|
|
pixmapPainter.scale(pix.width() / brect.width(),
|
2007-10-01 07:06:51 +02:00
|
|
|
pix.height() / brect.height());
|
2007-09-18 23:30:30 +02:00
|
|
|
pixmapPainter.translate(-brect.topLeft());
|
|
|
|
|
2007-10-01 07:06:51 +02:00
|
|
|
// erase the exposed area so we don't paint over and over
|
|
|
|
pixmapPainter.setCompositionMode(QPainter::CompositionMode_Source);
|
|
|
|
pixmapPainter.fillRect(exposed, Qt::transparent);
|
|
|
|
pixmapPainter.setCompositionMode(QPainter::CompositionMode_SourceOver);
|
|
|
|
|
2007-09-18 23:30:30 +02:00
|
|
|
// Re-render the invalidated areas of the pixmap. Important: don't
|
|
|
|
// fool the item into using the widget - pass 0 instead of \a
|
|
|
|
// widget.
|
|
|
|
paintWidget(&pixmapPainter, &cacheOption, 0);
|
|
|
|
pixmapPainter.end();
|
|
|
|
|
|
|
|
// Reinsert this pixmap into the cache
|
|
|
|
QPixmapCache::insert(d->cacheKey, pix);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Redraw the exposed area using the transformed painter. Depending on
|
|
|
|
// the hardware, this may be a server-side operation, or an expensive
|
|
|
|
// qpixmap-image-transform-pixmap roundtrip.
|
|
|
|
painter->drawPixmap(brect, pix, QRectF(QPointF(), pix.size()));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Render using device coordinate cache mode.
|
|
|
|
if (d->cachePaintMode == DeviceCoordinateCacheMode) {
|
|
|
|
QTransform transform = painter->worldTransform();
|
|
|
|
QRect deviceRect = transform.mapRect(brect).toRect();
|
|
|
|
|
2007-10-26 18:35:58 +02:00
|
|
|
if (deviceRect.width() < 1 ||
|
|
|
|
deviceRect.height() < 1) {
|
|
|
|
// we have nothing to paint!
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-09-18 23:30:30 +02:00
|
|
|
// Auto-adjust the pixmap size.
|
|
|
|
if (deviceRect.size() != pix.size()) {
|
|
|
|
pix = QPixmap(deviceRect.size());
|
2007-10-26 01:09:02 +02:00
|
|
|
pix.fill(Qt::transparent);
|
2007-09-18 23:30:30 +02:00
|
|
|
exposed = brect;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for newly invalidated areas.
|
|
|
|
if (!exposed.isNull()) {
|
|
|
|
d->cacheInvalidated = QRectF();
|
|
|
|
|
|
|
|
// Construct the new styleoption, reset the exposed rect.
|
|
|
|
QStyleOptionGraphicsItem cacheOption = *option;
|
|
|
|
cacheOption.exposedRect = exposed.toRect(); // <- truncation
|
|
|
|
|
|
|
|
QPointF viewOrigo = transform.map(QPointF(0, 0));
|
|
|
|
QPointF offset = viewOrigo - deviceRect.topLeft();
|
|
|
|
|
|
|
|
// Transform the painter, and render the item in device coordinates.
|
|
|
|
QPainter pixmapPainter(&pix);
|
2007-10-01 07:06:51 +02:00
|
|
|
|
2007-09-18 23:30:30 +02:00
|
|
|
pixmapPainter.translate(offset);
|
|
|
|
pixmapPainter.setWorldTransform(transform, true);
|
|
|
|
pixmapPainter.translate(transform.inverted().map(QPointF(0, 0)));
|
2007-10-01 07:06:51 +02:00
|
|
|
|
|
|
|
// erase the exposed area so we don't paint over and over
|
|
|
|
pixmapPainter.setCompositionMode(QPainter::CompositionMode_Source);
|
|
|
|
pixmapPainter.fillRect(exposed, Qt::transparent);
|
|
|
|
pixmapPainter.setCompositionMode(QPainter::CompositionMode_SourceOver);
|
|
|
|
|
2007-09-18 23:30:30 +02:00
|
|
|
paintWidget(&pixmapPainter, &cacheOption, 0);
|
|
|
|
pixmapPainter.end();
|
|
|
|
|
|
|
|
// Reinsert this pixmap into the cache
|
|
|
|
QPixmapCache::insert(d->cacheKey, pix);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Redraw the exposed area using an untransformed painter. This
|
|
|
|
// effectively becomes a bitblit that does not transform the cache.
|
|
|
|
painter->setWorldTransform(QTransform());
|
|
|
|
painter->drawPixmap(deviceRect.topLeft(), pix);
|
|
|
|
return;
|
2007-08-01 22:51:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-21 06:33:17 +01:00
|
|
|
ToolTipData Widget::toolTip() const
|
|
|
|
{
|
|
|
|
return d->toolTip;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::setToolTip(const ToolTipData &tip)
|
|
|
|
{
|
|
|
|
d->toolTip = tip;
|
2008-01-07 05:39:36 +01:00
|
|
|
if (ToolTip::self()->currentWidget() == this) {
|
2007-12-21 06:33:17 +01:00
|
|
|
QPoint viewPos = view()->mapFromScene(scenePos());
|
|
|
|
QPoint globalPos = view()->mapToGlobal(viewPos);
|
2008-01-07 05:39:36 +01:00
|
|
|
ToolTip::self()->show(globalPos, this);
|
2007-12-21 06:33:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-25 06:36:31 +01:00
|
|
|
void Widget::paintWidget(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
|
|
|
|
{
|
|
|
|
Q_UNUSED(painter);
|
|
|
|
Q_UNUSED(option);
|
|
|
|
Q_UNUSED(widget);
|
|
|
|
|
|
|
|
// Replaced by widget's own function
|
|
|
|
}
|
|
|
|
|
2007-10-30 00:10:45 +01:00
|
|
|
QVariant Widget::itemChange(GraphicsItemChange change, const QVariant &value)
|
2007-05-22 18:48:34 +02:00
|
|
|
{
|
2007-10-30 00:10:45 +01:00
|
|
|
if (change == QGraphicsItem::ItemChildRemovedChange) {
|
2007-11-27 07:03:19 +01:00
|
|
|
if (layout()) {
|
|
|
|
layout()->removeItem(dynamic_cast<Plasma::LayoutItem*>(value.value<QGraphicsItem*>()));
|
2007-10-30 00:10:45 +01:00
|
|
|
updateGeometry();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return QGraphicsItem::itemChange(change, value);
|
2007-05-22 18:48:34 +02:00
|
|
|
}
|
|
|
|
|
2007-10-24 08:54:26 +02:00
|
|
|
void Widget::managingLayoutChanged()
|
|
|
|
{
|
|
|
|
if (managingLayout()) {
|
|
|
|
d->wasMovable = flags() & ItemIsMovable;
|
|
|
|
if (!dynamic_cast<FreeLayout*>(managingLayout())) {
|
2007-10-26 01:09:02 +02:00
|
|
|
setFlag(ItemIsMovable, false);
|
2007-10-24 08:54:26 +02:00
|
|
|
}
|
|
|
|
} else {
|
2007-10-26 01:09:02 +02:00
|
|
|
setFlag(ItemIsMovable, d->wasMovable);
|
2007-10-24 08:54:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-16 14:21:16 +01:00
|
|
|
void Widget::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
|
|
|
|
{
|
2007-12-16 14:51:27 +01:00
|
|
|
// HACK: QGraphicsItem's documentation says that the event will be passed
|
|
|
|
// to the parent if it's not handled, but it isn't passed. This can be
|
|
|
|
// removed when Qt4.4 becomes a requirement. See Qt bug #176902.
|
2007-12-16 14:21:16 +01:00
|
|
|
Widget *parentWidget = parent();
|
|
|
|
if (parentWidget) {
|
|
|
|
parentWidget->contextMenuEvent(event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-30 11:45:21 +01:00
|
|
|
bool Widget::sceneEvent(QEvent *event)
|
|
|
|
{
|
|
|
|
switch (event->type()) {
|
|
|
|
case QEvent::GraphicsSceneHoverMove:
|
|
|
|
// If the tooltip isn't visible, run through showing the tooltip again
|
|
|
|
// so that it only becomes visible after a stationary hover
|
2008-01-07 05:39:36 +01:00
|
|
|
if (ToolTip::self()->isVisible()) {
|
2007-12-30 11:45:21 +01:00
|
|
|
break;
|
|
|
|
}
|
2007-12-21 06:33:17 +01:00
|
|
|
|
2007-12-30 11:45:21 +01:00
|
|
|
case QEvent::GraphicsSceneHoverEnter:
|
|
|
|
{
|
|
|
|
// Check that there is a tooltip to show
|
|
|
|
if (d->toolTip.image.isNull() &&
|
|
|
|
d->toolTip.subText.isEmpty() &&
|
|
|
|
d->toolTip.mainText.isEmpty()) {
|
|
|
|
break;
|
|
|
|
}
|
2007-12-30 11:17:33 +01:00
|
|
|
|
2007-12-30 11:45:21 +01:00
|
|
|
// If the mouse is in the widget's area at the time that it is being
|
|
|
|
// created the widget can receive a hover event before it is fully
|
|
|
|
// initialized, in which case view() will return 0.
|
|
|
|
QGraphicsView *parentView = view();
|
|
|
|
if (parentView) {
|
|
|
|
QPoint viewPos = parentView->mapFromScene(scenePos());
|
|
|
|
QPoint globalPos = parentView->mapToGlobal(viewPos);
|
2008-01-07 05:39:36 +01:00
|
|
|
ToolTip::self()->show(globalPos, this);
|
2007-12-30 11:45:21 +01:00
|
|
|
}
|
2007-12-22 07:43:05 +01:00
|
|
|
|
2007-12-30 11:45:21 +01:00
|
|
|
break;
|
2007-12-30 11:17:33 +01:00
|
|
|
}
|
|
|
|
|
2007-12-30 11:45:21 +01:00
|
|
|
case QEvent::GraphicsSceneHoverLeave:
|
|
|
|
case QEvent::GraphicsSceneMousePress:
|
|
|
|
case QEvent::GraphicsSceneWheel:
|
2008-01-07 05:39:36 +01:00
|
|
|
ToolTip::self()->hide();
|
2007-12-21 06:33:17 +01:00
|
|
|
|
2007-12-30 11:45:21 +01:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return QGraphicsItem::sceneEvent(event);
|
2007-12-21 06:33:17 +01:00
|
|
|
}
|
|
|
|
|
2007-03-02 06:27:33 +01:00
|
|
|
} // Plasma namespace
|
|
|
|
|