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 <QStyleOptionGraphicsItem>
|
2008-01-24 16:44:11 +01:00
|
|
|
#include <QDesktopWidget>
|
2007-05-23 19:18:43 +02:00
|
|
|
|
2007-08-05 14:58:40 +02:00
|
|
|
#include <KDebug>
|
|
|
|
|
2008-02-18 00:02:46 +01:00
|
|
|
#include "plasma/applet.h"
|
|
|
|
|
2007-11-19 23:42:56 +01:00
|
|
|
#include "layouts/freelayout.h"
|
2007-08-01 22:51:27 +02:00
|
|
|
#include "plasma/plasma.h"
|
2008-01-21 12:42:08 +01:00
|
|
|
#include "plasma/view.h"
|
|
|
|
#include "plasma/containment.h"
|
2008-02-18 00:02:46 +01:00
|
|
|
#include "plasma/widgets/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()
|
2008-02-29 18:43:31 +01:00
|
|
|
: opacity(1.0),
|
2008-02-05 19:26:17 +01:00
|
|
|
wasMovable(false),
|
|
|
|
toolTip(0)
|
2007-07-12 20:11:50 +02:00
|
|
|
{ }
|
2008-02-05 19:26:17 +01:00
|
|
|
|
|
|
|
~Private()
|
|
|
|
{
|
|
|
|
delete toolTip;
|
|
|
|
}
|
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-10-24 08:54:26 +02:00
|
|
|
bool wasMovable;
|
|
|
|
|
2007-08-01 22:51:27 +02:00
|
|
|
bool shouldPaint(QPainter *painter, const QTransform &transform);
|
2008-02-05 19:26: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()) ||
|
2008-02-25 21:50:26 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2008-02-29 18:43:31 +01:00
|
|
|
Widget::Widget(QGraphicsItem *parent)
|
|
|
|
: QGraphicsWidget(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);
|
2008-02-15 07:51:37 +01:00
|
|
|
setCacheMode(DeviceCoordinateCache);
|
2008-02-29 18:43:31 +01:00
|
|
|
// setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding,QSizePolicy::DefaultType);
|
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)
|
|
|
|
{
|
2008-02-15 07:51:37 +01:00
|
|
|
setCacheMode(CacheMode(mode), size);
|
2007-09-10 21:06:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Widget::CachePaintMode Widget::cachePaintMode() const
|
|
|
|
{
|
2008-02-15 07:51:37 +01:00
|
|
|
return CachePaintMode(cacheMode());
|
2007-09-10 21:06:58 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2008-02-15 07:51:37 +01:00
|
|
|
paintWidget(painter, option, widget);
|
2007-08-01 22:51:27 +02:00
|
|
|
}
|
|
|
|
|
2008-02-05 19:26:17 +01:00
|
|
|
const ToolTipData* Widget::toolTip() const
|
2007-12-21 06:33:17 +01:00
|
|
|
{
|
|
|
|
return d->toolTip;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::setToolTip(const ToolTipData &tip)
|
|
|
|
{
|
2008-02-05 19:26:17 +01:00
|
|
|
if (tip.image.isNull() &&
|
|
|
|
tip.subText.isEmpty() &&
|
|
|
|
tip.mainText.isEmpty()) {
|
|
|
|
delete d->toolTip;
|
|
|
|
d->toolTip = 0;
|
2008-02-05 21:10:53 +01:00
|
|
|
return;
|
2008-02-05 19:26:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!d->toolTip) {
|
|
|
|
d->toolTip = new ToolTipData;
|
|
|
|
}
|
|
|
|
|
|
|
|
*d->toolTip = tip;
|
|
|
|
|
2008-01-07 05:39:36 +01:00
|
|
|
if (ToolTip::self()->currentWidget() == this) {
|
2008-02-05 19:26:17 +01:00
|
|
|
ToolTip::self()->show(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()) {
|
2008-02-29 18:43:31 +01:00
|
|
|
//FIXME: Port
|
|
|
|
// layout()->removeItem(dynamic_cast<QGraphicsLayoutItem*>(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
|
|
|
}
|
|
|
|
|
2008-02-19 10:19:02 +01:00
|
|
|
QPoint Widget::popupPosition(const QSize &s) const
|
2008-01-21 12:42:08 +01:00
|
|
|
{
|
2008-02-25 21:50:26 +01:00
|
|
|
QGraphicsView *v = view();
|
|
|
|
Q_ASSERT(v);
|
|
|
|
|
|
|
|
QPoint pos = v->mapFromScene(scenePos());
|
|
|
|
pos = v->mapToGlobal(pos);
|
2008-02-26 02:55:50 +01:00
|
|
|
kDebug() << "==> position is" << scenePos() << v->mapFromScene(scenePos()) << pos;
|
2008-02-25 21:50:26 +01:00
|
|
|
Plasma::View *pv = dynamic_cast<Plasma::View *>(v);
|
2008-01-21 12:42:08 +01:00
|
|
|
|
|
|
|
Plasma::Location loc = Floating;
|
|
|
|
if (pv) {
|
|
|
|
loc = pv->containment()->location();
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (loc) {
|
|
|
|
case BottomEdge:
|
2008-01-22 15:06:37 +01:00
|
|
|
pos = QPoint(pos.x(), pos.y() - s.height());
|
|
|
|
break;
|
2008-01-21 12:42:08 +01:00
|
|
|
case TopEdge:
|
2008-01-22 15:06:37 +01:00
|
|
|
pos = QPoint(pos.x(), pos.y() + (int)size().height());
|
|
|
|
break;
|
2008-01-21 12:42:08 +01:00
|
|
|
case LeftEdge:
|
2008-01-22 15:06:37 +01:00
|
|
|
pos = QPoint(pos.x() + (int)size().width(), pos.y());
|
|
|
|
break;
|
2008-01-21 12:42:08 +01:00
|
|
|
case RightEdge:
|
2008-01-22 15:06:37 +01:00
|
|
|
pos = QPoint(pos.x() - s.width(), pos.y());
|
|
|
|
break;
|
2008-01-21 12:42:08 +01:00
|
|
|
default:
|
2008-01-26 13:37:17 +01:00
|
|
|
if (pos.y() - s.height() > 0) {
|
2008-01-22 15:06:37 +01:00
|
|
|
pos = QPoint(pos.x(), pos.y() - s.height());
|
2008-01-21 12:42:08 +01:00
|
|
|
} else {
|
2008-01-22 15:06:37 +01:00
|
|
|
pos = QPoint(pos.x(), pos.y() + (int)size().height());
|
2008-01-21 12:42:08 +01:00
|
|
|
}
|
|
|
|
}
|
2008-01-22 15:06:37 +01:00
|
|
|
|
2008-01-24 16:44:11 +01:00
|
|
|
//are we out of screen?
|
|
|
|
|
2008-02-12 11:07:45 +01:00
|
|
|
QRect screenRect = QApplication::desktop()->screenGeometry(pv ? pv->containment()->screen() : -1);
|
2008-02-26 02:55:50 +01:00
|
|
|
kDebug() << "==> rect for" << (pv ? pv->containment()->screen() : -1) << "is" << screenRect;
|
2008-02-12 11:07:45 +01:00
|
|
|
|
2008-02-12 10:41:49 +01:00
|
|
|
if (pos.rx() + s.width() > screenRect.right()) {
|
|
|
|
pos.rx() -= ((pos.rx() + s.width()) - screenRect.right());
|
2008-01-24 16:44:11 +01:00
|
|
|
}
|
2008-02-12 11:07:45 +01:00
|
|
|
|
2008-02-12 10:41:49 +01:00
|
|
|
if (pos.ry() + s.height() > screenRect.bottom()) {
|
|
|
|
pos.ry() -= ((pos.ry() + s.height()) - screenRect.bottom());
|
2008-01-22 15:06:37 +01:00
|
|
|
}
|
2008-01-26 13:37:17 +01:00
|
|
|
pos.rx() = qMax(0, pos.rx());
|
2008-01-22 15:06:37 +01:00
|
|
|
|
|
|
|
return pos;
|
2008-01-21 12:42:08 +01:00
|
|
|
}
|
|
|
|
|
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
|
2008-02-05 19:26:17 +01:00
|
|
|
if (!d->toolTip) {
|
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
|
|
|
// 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) {
|
2008-02-05 19:26:17 +01:00
|
|
|
ToolTip::self()->show(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:
|
2008-02-05 19:26:17 +01:00
|
|
|
ToolTip::self()->delayedHide();
|
|
|
|
break;
|
|
|
|
|
2007-12-30 11:45:21 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2008-02-29 18:43:31 +01:00
|
|
|
QSizeF Widget::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
|
|
|
|
{
|
|
|
|
if (layout()) {
|
|
|
|
return layout()->effectiveSizeHint(which, constraint);
|
|
|
|
}
|
|
|
|
return size();
|
|
|
|
}
|
|
|
|
|
2007-03-02 06:27:33 +01:00
|
|
|
} // Plasma namespace
|
|
|
|
|