2007-11-12 20:27:21 +01:00
|
|
|
/*
|
|
|
|
* Copyright 2007 by Kevin Ottens <ervin@kde.org>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Library General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2, 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 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.
|
|
|
|
*/
|
|
|
|
|
2008-07-30 22:35:42 +02:00
|
|
|
#include "private/applethandle_p.h"
|
2007-11-12 20:27:21 +01:00
|
|
|
|
2008-02-03 14:09:21 +01:00
|
|
|
#include <QApplication>
|
2008-05-12 01:56:32 +02:00
|
|
|
#include <QBitmap>
|
2007-11-12 20:27:21 +01:00
|
|
|
#include <QtGui/QGraphicsSceneMouseEvent>
|
2007-11-18 22:34:27 +01:00
|
|
|
#include <QtGui/QLinearGradient>
|
2007-11-12 20:27:21 +01:00
|
|
|
#include <QtGui/QPainter>
|
2008-01-05 12:58:48 +01:00
|
|
|
#include <QtGui/QApplication>
|
2007-11-12 20:27:21 +01:00
|
|
|
|
2007-11-18 22:34:27 +01:00
|
|
|
#include <KColorScheme>
|
2008-01-07 04:40:12 +01:00
|
|
|
#include <KGlobalSettings>
|
2007-11-12 20:27:21 +01:00
|
|
|
#include <KIcon>
|
2008-07-16 05:53:07 +02:00
|
|
|
#include <KIconLoader>
|
2008-05-11 15:56:35 +02:00
|
|
|
#include <KWindowSystem>
|
2007-11-12 20:27:21 +01:00
|
|
|
|
|
|
|
#include <cmath>
|
2007-12-10 20:52:29 +01:00
|
|
|
#include <math.h>
|
2007-11-12 20:27:21 +01:00
|
|
|
|
|
|
|
#include "applet.h"
|
2008-05-24 20:33:40 +02:00
|
|
|
#include "applet_p.h"
|
2007-11-18 22:34:27 +01:00
|
|
|
#include "containment.h"
|
2007-11-26 22:57:50 +01:00
|
|
|
#include "corona.h"
|
2008-07-04 01:50:13 +02:00
|
|
|
#include "paintutils.h"
|
2007-11-18 22:34:27 +01:00
|
|
|
#include "theme.h"
|
2008-02-19 10:12:34 +01:00
|
|
|
#include "view.h"
|
2008-10-29 18:46:33 +01:00
|
|
|
#include "framesvg.h"
|
2007-11-12 20:27:21 +01:00
|
|
|
|
|
|
|
namespace Plasma
|
|
|
|
{
|
|
|
|
|
2007-11-26 20:37:20 +01:00
|
|
|
qreal _k_angleForPoints(const QPointF ¢er, const QPointF &pt1, const QPointF &pt2);
|
|
|
|
|
2008-07-10 03:35:51 +02:00
|
|
|
AppletHandle::AppletHandle(Containment *parent, Applet *applet, const QPointF &hoverPos)
|
2007-11-18 03:00:20 +01:00
|
|
|
: QObject(),
|
|
|
|
QGraphicsItem(parent),
|
|
|
|
m_pressedButton(NoButton),
|
|
|
|
m_containment(parent),
|
|
|
|
m_applet(applet),
|
2008-07-16 05:53:07 +02:00
|
|
|
m_iconSize(16),
|
2007-11-18 03:00:20 +01:00
|
|
|
m_opacity(0.0),
|
|
|
|
m_anim(FadeIn),
|
|
|
|
m_animId(0),
|
|
|
|
m_angle(0.0),
|
2008-01-08 02:22:44 +01:00
|
|
|
m_tempAngle(0.0),
|
2008-01-05 12:58:48 +01:00
|
|
|
m_scaleWidth(1.0),
|
2008-01-10 02:21:45 +01:00
|
|
|
m_scaleHeight(1.0),
|
2008-07-10 03:35:51 +02:00
|
|
|
m_topview(0),
|
2008-09-18 21:40:46 +02:00
|
|
|
m_backgroundBuffer(0),
|
2008-09-09 02:23:02 +02:00
|
|
|
m_currentView(applet->view()),
|
|
|
|
m_entryPos(hoverPos),
|
|
|
|
m_buttonsOnRight(false),
|
|
|
|
m_pendingFade(false)
|
2007-11-12 20:27:21 +01:00
|
|
|
{
|
2008-10-12 01:28:20 +02:00
|
|
|
KColorScheme colorScheme(QPalette::Active, KColorScheme::View,
|
|
|
|
Theme::defaultTheme()->colorScheme());
|
2008-04-16 17:53:16 +02:00
|
|
|
m_gradientColor = colorScheme.background(KColorScheme::NormalBackground).color();
|
2007-11-26 20:37:20 +01:00
|
|
|
|
|
|
|
QTransform originalMatrix = m_applet->transform();
|
2008-07-10 03:35:51 +02:00
|
|
|
m_applet->resetTransform();
|
|
|
|
|
2008-04-13 15:23:39 +02:00
|
|
|
QRectF rect(m_applet->contentsRect());
|
2007-11-26 20:37:20 +01:00
|
|
|
QPointF center = rect.center();
|
|
|
|
originalMatrix.translate(center.x(), center.y());
|
|
|
|
|
|
|
|
qreal cosine = originalMatrix.m11();
|
|
|
|
qreal sine = originalMatrix.m12();
|
|
|
|
|
2008-01-08 02:22:44 +01:00
|
|
|
m_angle = _k_angleForPoints(QPointF(0, 0),
|
|
|
|
QPointF(1, 0),
|
|
|
|
QPointF(cosine, sine));
|
|
|
|
|
2007-11-12 20:27:21 +01:00
|
|
|
m_applet->setParentItem(this);
|
2008-01-08 02:22:44 +01:00
|
|
|
|
|
|
|
rect = QRectF(m_applet->pos(), m_applet->size());
|
|
|
|
center = rect.center();
|
|
|
|
QTransform matrix;
|
|
|
|
matrix.translate(center.x(), center.y());
|
|
|
|
matrix.rotateRadians(m_angle);
|
|
|
|
matrix.translate(-center.x(), -center.y());
|
|
|
|
setTransform(matrix);
|
2008-07-10 03:35:51 +02:00
|
|
|
|
2008-01-10 00:43:49 +01:00
|
|
|
m_hoverTimer = new QTimer(this);
|
|
|
|
m_hoverTimer->setSingleShot(true);
|
2008-02-13 01:49:07 +01:00
|
|
|
m_hoverTimer->setInterval(333);
|
2008-01-08 02:22:44 +01:00
|
|
|
|
2008-06-22 15:47:04 +02:00
|
|
|
m_leaveTimer = new QTimer(this);
|
|
|
|
m_leaveTimer->setSingleShot(true);
|
2008-07-08 00:59:03 +02:00
|
|
|
m_leaveTimer->setInterval(500);
|
2008-06-22 15:47:04 +02:00
|
|
|
|
2008-01-10 00:43:49 +01:00
|
|
|
connect(m_hoverTimer, SIGNAL(timeout()), this, SLOT(fadeIn()));
|
2008-06-22 15:47:04 +02:00
|
|
|
connect(m_leaveTimer, SIGNAL(timeout()), this, SLOT(leaveTimeout()));
|
2007-11-22 06:11:06 +01:00
|
|
|
connect(m_applet, SIGNAL(destroyed(QObject*)), this, SLOT(appletDestroyed()));
|
2008-01-10 00:43:49 +01:00
|
|
|
|
2007-11-12 20:27:21 +01:00
|
|
|
setAcceptsHoverEvents(true);
|
2008-01-10 00:43:49 +01:00
|
|
|
m_hoverTimer->start();
|
2008-02-13 01:49:07 +01:00
|
|
|
|
2008-09-15 22:08:21 +02:00
|
|
|
//icons
|
2008-09-18 21:40:46 +02:00
|
|
|
m_configureIcons = new Svg(this);
|
2008-09-15 22:08:21 +02:00
|
|
|
m_configureIcons->setImagePath("widgets/configuration-icons");
|
|
|
|
//FIXME: this should be of course true, but works only if false
|
|
|
|
m_configureIcons->setContainsMultipleImages(true);
|
|
|
|
|
2008-10-29 18:46:33 +01:00
|
|
|
m_background = new FrameSvg(this);
|
2008-09-18 21:40:46 +02:00
|
|
|
m_background->setImagePath("widgets/background");
|
|
|
|
|
2008-05-19 18:20:27 +02:00
|
|
|
//We got to be able to see the applet while dragging to to another containment,
|
|
|
|
//so we want a high zValue.
|
|
|
|
//FIXME: apparently this doesn't work: sometimes an applet still get's drawn behind
|
|
|
|
//the containment it's being dragged to, sometimes it doesn't.
|
2008-09-18 21:40:46 +02:00
|
|
|
m_zValue = m_applet->zValue()-1;
|
2008-05-19 18:20:27 +02:00
|
|
|
m_applet->raise();
|
2008-06-22 15:47:04 +02:00
|
|
|
m_applet->installSceneEventFilter(this);
|
2008-02-13 01:49:07 +01:00
|
|
|
setZValue(m_applet->zValue());
|
2007-11-12 20:27:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
AppletHandle::~AppletHandle()
|
|
|
|
{
|
2008-08-20 11:14:27 +02:00
|
|
|
detachApplet();
|
2008-09-24 13:44:39 +02:00
|
|
|
delete m_backgroundBuffer;
|
2008-05-18 14:54:20 +02:00
|
|
|
if (m_topview) {
|
|
|
|
delete m_topview;
|
|
|
|
}
|
2007-11-12 20:27:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Applet *AppletHandle::applet() const
|
|
|
|
{
|
|
|
|
return m_applet;
|
|
|
|
}
|
|
|
|
|
2008-08-20 11:14:27 +02:00
|
|
|
void AppletHandle::detachApplet ()
|
|
|
|
{
|
|
|
|
if (!m_applet) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
disconnect(m_hoverTimer, SIGNAL(timeout()), this, SLOT(fadeIn()));
|
|
|
|
disconnect(m_leaveTimer, SIGNAL(timeout()), this, SLOT(leaveTimeout()));
|
|
|
|
m_applet->disconnect(this);
|
|
|
|
|
|
|
|
m_applet->removeSceneEventFilter(this);
|
|
|
|
|
|
|
|
QRectF rect = QRectF(m_applet->pos(), m_applet->size());
|
|
|
|
QPointF center = m_applet->mapFromParent(rect.center());
|
|
|
|
|
|
|
|
QPointF newPos = transform().inverted().map(m_applet->pos());
|
|
|
|
m_applet->setPos(mapToParent(newPos));
|
|
|
|
|
|
|
|
QTransform matrix;
|
|
|
|
matrix.translate(center.x(), center.y());
|
|
|
|
matrix.rotateRadians(m_angle);
|
|
|
|
matrix.translate(-center.x(), -center.y());
|
|
|
|
m_applet->setTransform(matrix);
|
|
|
|
|
|
|
|
m_applet->setParentItem(m_containment);
|
|
|
|
|
|
|
|
m_applet->setZValue(m_zValue);
|
|
|
|
|
|
|
|
m_applet->update(); // re-render the background, now we've transformed the applet
|
|
|
|
|
|
|
|
m_applet = 0;
|
|
|
|
}
|
|
|
|
|
2007-11-12 20:27:21 +01:00
|
|
|
QRectF Plasma::AppletHandle::boundingRect() const
|
|
|
|
{
|
2008-07-06 02:18:43 +02:00
|
|
|
return m_totalRect;
|
|
|
|
}
|
|
|
|
|
|
|
|
QPainterPath AppletHandle::shape() const
|
|
|
|
{
|
2008-10-12 13:12:24 +02:00
|
|
|
//when the containment changes the applet is reset to 0
|
2008-07-06 15:01:02 +02:00
|
|
|
if (m_applet) {
|
2008-09-18 21:40:46 +02:00
|
|
|
QPainterPath path = PaintUtils::roundedRectangle(m_decorationRect, 10);
|
2008-07-06 15:01:02 +02:00
|
|
|
return path.united(m_applet->mapToParent(m_applet->shape()));
|
|
|
|
} else {
|
|
|
|
return QGraphicsItem::shape();
|
|
|
|
}
|
2007-11-12 20:27:21 +01:00
|
|
|
}
|
|
|
|
|
2008-07-10 03:35:51 +02:00
|
|
|
QPainterPath handleRect(const QRectF &rect, int radius, bool onRight)
|
|
|
|
{
|
|
|
|
QPainterPath path;
|
|
|
|
if (onRight) {
|
|
|
|
// make the left side straight
|
|
|
|
path.moveTo(rect.left(), rect.top()); // Top left
|
|
|
|
path.lineTo(rect.right() - radius, rect.top()); // Top side
|
2008-10-12 01:28:20 +02:00
|
|
|
path.quadTo(rect.right(), rect.top(),
|
|
|
|
rect.right(), rect.top() + radius); // Top right corner
|
|
|
|
path.lineTo(rect.right(), rect.bottom() - radius); // Right side
|
|
|
|
path.quadTo(rect.right(), rect.bottom(),
|
|
|
|
rect.right() - radius, rect.bottom()); // Bottom right corner
|
|
|
|
path.lineTo(rect.left(), rect.bottom()); // Bottom side
|
2008-07-10 03:35:51 +02:00
|
|
|
} else {
|
|
|
|
// make the right side straight
|
|
|
|
path.moveTo(QPointF(rect.left(), rect.top() + radius));
|
2008-10-12 01:28:20 +02:00
|
|
|
path.quadTo(rect.left(), rect.top(),
|
|
|
|
rect.left() + radius, rect.top()); // Top left corner
|
|
|
|
path.lineTo(rect.right(), rect.top()); // Top side
|
|
|
|
path.lineTo(rect.right(), rect.bottom()); // Right side
|
|
|
|
path.lineTo(rect.left() + radius, rect.bottom()); // Bottom side
|
|
|
|
path.quadTo(rect.left(), rect.bottom(),
|
|
|
|
rect.left(), rect.bottom() - radius); // Bottom left corner
|
2008-07-10 03:35:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
path.closeSubpath();
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2007-11-12 20:27:21 +01:00
|
|
|
void AppletHandle::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
|
|
|
|
{
|
|
|
|
Q_UNUSED(option);
|
|
|
|
Q_UNUSED(widget);
|
|
|
|
|
2008-09-22 18:37:36 +02:00
|
|
|
if (qFuzzyCompare(m_opacity + 1.0, 1.0)) {
|
2008-09-22 21:28:11 +02:00
|
|
|
if (m_anim == FadeOut) {
|
|
|
|
QTimer::singleShot(0, this, SLOT(emitDisappear()));
|
|
|
|
}
|
2008-09-18 21:40:46 +02:00
|
|
|
return;
|
|
|
|
}
|
2007-11-12 20:27:21 +01:00
|
|
|
|
2007-11-18 22:34:27 +01:00
|
|
|
painter->save();
|
2008-09-18 21:40:46 +02:00
|
|
|
|
2008-09-21 22:55:18 +02:00
|
|
|
qreal translation;
|
|
|
|
|
2008-09-18 21:40:46 +02:00
|
|
|
if (m_buttonsOnRight) {
|
2008-10-12 01:28:20 +02:00
|
|
|
//kDebug() << "translating by" << m_opacity
|
|
|
|
// << (-(1 - m_opacity) * m_rect.width()) << m_rect.width();
|
2008-09-21 22:55:18 +02:00
|
|
|
translation = -(1 - m_opacity) * m_rect.width();
|
2008-09-18 21:40:46 +02:00
|
|
|
} else {
|
2008-09-21 22:55:18 +02:00
|
|
|
translation = (1 - m_opacity) * m_rect.width();
|
2008-09-18 21:40:46 +02:00
|
|
|
}
|
|
|
|
|
2008-09-21 22:55:18 +02:00
|
|
|
painter->translate(translation, 0);
|
|
|
|
|
2007-11-18 22:34:27 +01:00
|
|
|
painter->setPen(Qt::NoPen);
|
|
|
|
painter->setRenderHints(QPainter::Antialiasing);
|
2008-01-12 07:03:54 +01:00
|
|
|
|
2008-07-16 05:53:07 +02:00
|
|
|
int iconMargin = m_iconSize / 2;
|
2008-09-18 21:40:46 +02:00
|
|
|
|
2008-10-12 01:28:20 +02:00
|
|
|
const QSize pixmapSize(int(m_decorationRect.width()),
|
|
|
|
int(m_decorationRect.height()) + m_iconSize * 4 + 1);
|
2008-09-24 17:59:29 +02:00
|
|
|
const QSize iconSize(KIconLoader::SizeSmall, KIconLoader::SizeSmall);
|
2008-09-18 21:40:46 +02:00
|
|
|
|
|
|
|
//regenerate our buffer?
|
|
|
|
if (m_animId > 0 || !m_backgroundBuffer || m_backgroundBuffer->size() != pixmapSize) {
|
|
|
|
QColor transparencyColor = Qt::black;
|
|
|
|
transparencyColor.setAlphaF(qMin(m_opacity, qreal(0.99)));
|
|
|
|
|
2008-10-12 01:28:20 +02:00
|
|
|
QLinearGradient g(QPoint(0, 0), QPoint(m_decorationRect.width(), 0));
|
2008-09-18 21:40:46 +02:00
|
|
|
//fading out panel
|
|
|
|
if (m_rect.height() > qreal(minimumHeight()) * 1.25) {
|
|
|
|
if (m_buttonsOnRight) {
|
2008-10-12 01:28:20 +02:00
|
|
|
qreal opaquePoint =
|
|
|
|
(m_background->marginSize(LeftMargin) - translation) / m_decorationRect.width();
|
2008-09-20 07:47:18 +02:00
|
|
|
//kDebug() << "opaquePoint" << opaquePoint
|
|
|
|
// << m_background->marginSize(LeftMargin) << m_decorationRect.width();
|
2008-09-18 21:40:46 +02:00
|
|
|
g.setColorAt(0.0, Qt::transparent);
|
2008-09-20 08:26:26 +02:00
|
|
|
g.setColorAt(qMax(0.0, opaquePoint - 0.05), Qt::transparent);
|
2008-09-18 21:40:46 +02:00
|
|
|
g.setColorAt(opaquePoint, transparencyColor);
|
|
|
|
g.setColorAt(1.0, transparencyColor);
|
|
|
|
} else {
|
2008-10-12 01:28:20 +02:00
|
|
|
qreal opaquePoint =
|
|
|
|
1 - ((m_background->marginSize(RightMargin) + translation) / m_decorationRect.width());
|
2008-09-18 21:40:46 +02:00
|
|
|
g.setColorAt(1.0, Qt::transparent);
|
|
|
|
g.setColorAt(opaquePoint, Qt::transparent);
|
2008-09-20 08:26:26 +02:00
|
|
|
g.setColorAt(qMax(0.0, opaquePoint - 0.05), transparencyColor);
|
2008-09-18 21:40:46 +02:00
|
|
|
g.setColorAt(0.0, transparencyColor);
|
|
|
|
}
|
|
|
|
//complete panel
|
|
|
|
} else {
|
|
|
|
g.setColorAt(0.0, transparencyColor);
|
|
|
|
}
|
|
|
|
|
2008-10-29 18:46:33 +01:00
|
|
|
m_background->resizeFrame(m_decorationRect.size());
|
2008-09-18 21:40:46 +02:00
|
|
|
|
|
|
|
if (!m_backgroundBuffer || m_backgroundBuffer->size() != pixmapSize) {
|
|
|
|
delete m_backgroundBuffer;
|
|
|
|
m_backgroundBuffer = new QPixmap(pixmapSize);
|
|
|
|
}
|
|
|
|
m_backgroundBuffer->fill(Qt::transparent);
|
|
|
|
QPainter buffPainter(m_backgroundBuffer);
|
|
|
|
|
2008-10-29 18:46:33 +01:00
|
|
|
m_background->paintFrame(&buffPainter);
|
2008-09-18 21:40:46 +02:00
|
|
|
|
|
|
|
//+1 because otherwise due to rounding errors when rotated could appear one pixel
|
|
|
|
//of the icon at the border of the applet
|
2008-09-21 22:55:18 +02:00
|
|
|
//QRectF iconRect(QPointF(pixmapSize.width() - m_iconSize + 1, m_iconSize), iconSize);
|
|
|
|
QRectF iconRect(QPointF(0, m_decorationRect.height() + 1), iconSize);
|
2008-09-18 21:40:46 +02:00
|
|
|
if (m_buttonsOnRight) {
|
2008-10-12 01:28:20 +02:00
|
|
|
iconRect.moveLeft(
|
|
|
|
pixmapSize.width() - m_iconSize - m_background->marginSize(LeftMargin));
|
2008-09-18 21:40:46 +02:00
|
|
|
m_configureIcons->paint(&buffPainter, iconRect, "size-diagonal-tr2bl");
|
|
|
|
} else {
|
2008-09-21 22:55:18 +02:00
|
|
|
iconRect.moveLeft(m_background->marginSize(RightMargin));
|
2008-09-18 21:40:46 +02:00
|
|
|
m_configureIcons->paint(&buffPainter, iconRect, "size-diagonal-tl2br");
|
|
|
|
}
|
|
|
|
|
|
|
|
iconRect.translate(0, m_iconSize);
|
|
|
|
m_configureIcons->paint(&buffPainter, iconRect, "rotate");
|
2008-09-21 10:02:54 +02:00
|
|
|
|
|
|
|
if (m_applet && m_applet->hasConfigurationInterface()) {
|
|
|
|
iconRect.translate(0, m_iconSize);
|
|
|
|
m_configureIcons->paint(&buffPainter, iconRect, "configure");
|
|
|
|
}
|
2008-09-18 21:40:46 +02:00
|
|
|
iconRect.translate(0, m_iconSize);
|
|
|
|
m_configureIcons->paint(&buffPainter, iconRect, "close");
|
|
|
|
|
|
|
|
buffPainter.setCompositionMode(QPainter::CompositionMode_DestinationIn);
|
|
|
|
//blend the background
|
2008-09-21 22:55:18 +02:00
|
|
|
buffPainter.fillRect(m_backgroundBuffer->rect(), g);
|
2008-09-18 21:40:46 +02:00
|
|
|
//blend the icons
|
2008-10-12 01:28:20 +02:00
|
|
|
//buffPainter.fillRect(QRect(QPoint((int)m_decorationRect.width(), 0), QSize(m_iconSize + 1,
|
|
|
|
// (int)m_decorationRect.height())), transparencyColor);
|
2008-07-10 21:48:55 +02:00
|
|
|
}
|
2008-09-18 21:40:46 +02:00
|
|
|
|
2008-10-12 01:28:20 +02:00
|
|
|
painter->drawPixmap(m_decorationRect.toRect(), *m_backgroundBuffer,
|
|
|
|
QRect(QPoint(0, 0), m_decorationRect.size().toSize()));
|
2007-11-12 20:27:21 +01:00
|
|
|
|
2008-01-23 13:12:57 +01:00
|
|
|
//XXX this code is duplicated in the next function
|
2008-07-16 05:53:07 +02:00
|
|
|
QPointF basePoint = m_rect.topLeft() + QPointF(HANDLE_MARGIN, iconMargin);
|
|
|
|
QPointF step = QPointF(0, m_iconSize + iconMargin);
|
|
|
|
QPointF separator = step + QPointF(0, iconMargin);
|
2008-01-23 13:12:57 +01:00
|
|
|
//end duplicate code
|
2007-11-12 20:27:21 +01:00
|
|
|
|
|
|
|
QPointF shiftC;
|
|
|
|
QPointF shiftD;
|
|
|
|
QPointF shiftR;
|
2008-01-06 04:13:07 +01:00
|
|
|
QPointF shiftM;
|
2007-11-12 20:27:21 +01:00
|
|
|
|
|
|
|
switch(m_pressedButton)
|
|
|
|
{
|
|
|
|
case ConfigureButton:
|
|
|
|
shiftC = QPointF(2, 2);
|
|
|
|
break;
|
|
|
|
case RemoveButton:
|
|
|
|
shiftD = QPointF(2, 2);
|
|
|
|
break;
|
|
|
|
case RotateButton:
|
|
|
|
shiftR = QPointF(2, 2);
|
|
|
|
break;
|
2008-01-06 04:13:07 +01:00
|
|
|
case ResizeButton:
|
|
|
|
shiftM = QPointF(2, 2);
|
|
|
|
break;
|
2007-11-12 20:27:21 +01:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-09-21 22:55:18 +02:00
|
|
|
QRectF sourceIconRect(QPointF(0, m_decorationRect.height() + 1), iconSize);
|
|
|
|
if (m_buttonsOnRight) {
|
2008-10-12 01:28:20 +02:00
|
|
|
sourceIconRect.moveLeft(
|
|
|
|
pixmapSize.width() - m_iconSize - m_background->marginSize(LeftMargin));
|
2008-09-21 22:55:18 +02:00
|
|
|
} else {
|
|
|
|
sourceIconRect.moveLeft(m_background->marginSize(RightMargin));
|
|
|
|
}
|
2008-09-15 22:08:21 +02:00
|
|
|
|
2008-09-13 19:09:34 +02:00
|
|
|
if (m_applet && m_applet->aspectRatioMode() != FixedSize) {
|
2008-09-18 21:40:46 +02:00
|
|
|
//resize
|
2008-10-12 01:28:20 +02:00
|
|
|
painter->drawPixmap(
|
|
|
|
QRectF(basePoint + shiftM, iconSize), *m_backgroundBuffer, sourceIconRect);
|
2008-09-13 19:09:34 +02:00
|
|
|
basePoint += step;
|
|
|
|
}
|
2008-09-18 21:40:46 +02:00
|
|
|
|
|
|
|
//rotate
|
|
|
|
sourceIconRect.translate(0, m_iconSize);
|
|
|
|
painter->drawPixmap(QRectF(basePoint + shiftR, iconSize), *m_backgroundBuffer, sourceIconRect);
|
2008-01-06 16:17:45 +01:00
|
|
|
|
2007-11-22 06:11:06 +01:00
|
|
|
if (m_applet && m_applet->hasConfigurationInterface()) {
|
2008-01-23 13:12:57 +01:00
|
|
|
basePoint += step;
|
2008-09-18 21:40:46 +02:00
|
|
|
sourceIconRect.translate(0, m_iconSize);
|
2008-10-12 01:28:20 +02:00
|
|
|
painter->drawPixmap(
|
|
|
|
QRectF(basePoint + shiftC, iconSize), *m_backgroundBuffer, sourceIconRect);
|
2007-11-12 20:27:21 +01:00
|
|
|
}
|
2008-01-06 04:13:07 +01:00
|
|
|
|
2008-09-18 21:40:46 +02:00
|
|
|
//close
|
2008-07-16 05:53:07 +02:00
|
|
|
basePoint = m_rect.bottomLeft() + QPointF(HANDLE_MARGIN, 0) - step;
|
2008-09-18 21:40:46 +02:00
|
|
|
sourceIconRect.translate(0, m_iconSize);
|
|
|
|
painter->drawPixmap(QRectF(basePoint + shiftD, iconSize), *m_backgroundBuffer, sourceIconRect);
|
2007-11-12 20:27:21 +01:00
|
|
|
|
|
|
|
painter->restore();
|
|
|
|
}
|
|
|
|
|
2008-09-22 21:28:11 +02:00
|
|
|
void AppletHandle::emitDisappear()
|
|
|
|
{
|
|
|
|
emit disappearDone(this);
|
|
|
|
}
|
|
|
|
|
2007-11-12 20:27:21 +01:00
|
|
|
AppletHandle::ButtonType AppletHandle::mapToButton(const QPointF &point) const
|
|
|
|
{
|
2008-07-16 05:53:07 +02:00
|
|
|
int iconMargin = m_iconSize / 2;
|
2008-01-23 13:12:57 +01:00
|
|
|
//XXX this code is duplicated in the prev. function
|
2008-07-16 05:53:07 +02:00
|
|
|
QPointF basePoint = m_rect.topLeft() + QPointF(HANDLE_MARGIN, iconMargin);
|
|
|
|
QPointF step = QPointF(0, m_iconSize + iconMargin);
|
|
|
|
QPointF separator = step + QPointF(0, iconMargin);
|
2008-07-06 02:18:43 +02:00
|
|
|
//end duplicate code
|
2007-11-12 20:27:21 +01:00
|
|
|
|
2008-07-16 05:53:07 +02:00
|
|
|
QRectF activeArea = QRectF(basePoint, QSizeF(m_iconSize, m_iconSize));
|
2007-11-12 20:27:21 +01:00
|
|
|
|
2008-09-13 19:09:34 +02:00
|
|
|
if (m_applet && m_applet->aspectRatioMode() != FixedSize) {
|
|
|
|
if (activeArea.contains(point)) {
|
|
|
|
return ResizeButton;
|
|
|
|
}
|
|
|
|
activeArea.translate(step);
|
2007-11-12 20:27:21 +01:00
|
|
|
}
|
|
|
|
|
2008-07-10 03:35:51 +02:00
|
|
|
if (activeArea.contains(point)) {
|
2007-11-18 03:00:20 +01:00
|
|
|
return RotateButton;
|
2007-11-12 20:27:21 +01:00
|
|
|
}
|
|
|
|
|
2008-01-06 16:17:45 +01:00
|
|
|
if (m_applet && m_applet->hasConfigurationInterface()) {
|
2008-01-23 13:12:57 +01:00
|
|
|
activeArea.translate(step);
|
2008-07-10 03:35:51 +02:00
|
|
|
if (activeArea.contains(point)) {
|
2008-01-06 16:17:45 +01:00
|
|
|
return ConfigureButton;
|
|
|
|
}
|
2008-01-06 04:13:07 +01:00
|
|
|
}
|
|
|
|
|
2008-07-16 05:53:07 +02:00
|
|
|
activeArea.moveTop(m_rect.bottom() - activeArea.height() - iconMargin);
|
2008-07-10 03:35:51 +02:00
|
|
|
if (activeArea.contains(point)) {
|
2007-11-18 03:00:20 +01:00
|
|
|
return RemoveButton;
|
2007-11-12 20:27:21 +01:00
|
|
|
}
|
|
|
|
|
2007-11-19 00:03:57 +01:00
|
|
|
return MoveButton;
|
|
|
|
//return m_applet->mapToParent(m_applet->shape()).contains(point) ? NoButton : MoveButton;
|
2007-11-12 20:27:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void AppletHandle::mousePressEvent(QGraphicsSceneMouseEvent *event)
|
|
|
|
{
|
2008-07-06 15:01:02 +02:00
|
|
|
//containment recently switched?
|
|
|
|
if (!m_applet) {
|
|
|
|
QGraphicsItem::mousePressEvent(event);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-01-10 02:21:45 +01:00
|
|
|
if (m_pendingFade) {
|
|
|
|
//m_pendingFade = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-11-18 22:34:27 +01:00
|
|
|
if (event->button() == Qt::LeftButton) {
|
2007-11-12 20:27:21 +01:00
|
|
|
m_pressedButton = mapToButton(event->pos());
|
2008-01-10 02:21:45 +01:00
|
|
|
//kDebug() << "button pressed:" << m_pressedButton;
|
|
|
|
if (m_pressedButton != NoButton) {
|
2008-02-13 02:53:27 +01:00
|
|
|
m_applet->raise();
|
2008-07-10 03:39:18 +02:00
|
|
|
m_zValue = m_applet->zValue();
|
|
|
|
setZValue(m_zValue);
|
2008-01-10 02:21:45 +01:00
|
|
|
}
|
2008-07-10 03:35:51 +02:00
|
|
|
|
|
|
|
if (m_pressedButton == MoveButton) {
|
|
|
|
m_pos = pos();
|
|
|
|
}
|
2007-11-18 22:34:27 +01:00
|
|
|
event->accept();
|
2008-05-16 15:28:33 +02:00
|
|
|
|
|
|
|
update();
|
|
|
|
|
2008-05-11 15:56:35 +02:00
|
|
|
//set mousePos to the position in the applet, in screencoords, so it becomes easy
|
|
|
|
//to reposition the toplevel view to the correct position.
|
2008-09-20 07:48:38 +02:00
|
|
|
QPoint localpos = m_currentView->mapFromScene(m_applet->scenePos());
|
|
|
|
m_mousePos = event->screenPos() - m_currentView->mapToGlobal(localpos);
|
2008-05-16 15:28:33 +02:00
|
|
|
|
2007-11-18 22:34:27 +01:00
|
|
|
return;
|
2007-11-12 20:27:21 +01:00
|
|
|
}
|
2007-11-18 22:34:27 +01:00
|
|
|
|
|
|
|
QGraphicsItem::mousePressEvent(event);
|
2007-11-12 20:27:21 +01:00
|
|
|
}
|
|
|
|
|
2008-05-26 17:09:06 +02:00
|
|
|
bool AppletHandle::leaveCurrentView(const QPoint &pos) const
|
2008-05-19 18:20:27 +02:00
|
|
|
{
|
|
|
|
foreach (QWidget *widget, QApplication::topLevelWidgets()) {
|
2008-05-26 17:09:06 +02:00
|
|
|
if (widget->geometry().contains(pos)) {
|
2008-05-19 18:20:27 +02:00
|
|
|
//is this widget a plasma view, a different view then our current one,
|
|
|
|
//AND not a dashboardview?
|
|
|
|
Plasma::View *v = qobject_cast<Plasma::View *>(widget);
|
2008-10-12 01:28:20 +02:00
|
|
|
if (v &&
|
|
|
|
v != m_currentView &&
|
|
|
|
v != m_topview &&
|
|
|
|
v->containment() != m_containment) {
|
2008-05-19 18:20:27 +02:00
|
|
|
return true;
|
|
|
|
}
|
2008-05-16 18:40:12 +02:00
|
|
|
}
|
|
|
|
}
|
2008-05-19 18:20:27 +02:00
|
|
|
return false;
|
2008-05-16 18:40:12 +02:00
|
|
|
}
|
|
|
|
|
2007-11-12 20:27:21 +01:00
|
|
|
void AppletHandle::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
|
|
|
|
{
|
2008-01-12 07:03:54 +01:00
|
|
|
//kDebug() << "button pressed:" << m_pressedButton << ", fade pending?" << m_pendingFade;
|
2008-01-10 02:21:45 +01:00
|
|
|
|
|
|
|
if (m_pendingFade) {
|
2008-07-10 03:35:51 +02:00
|
|
|
startFading(FadeOut, m_entryPos);
|
2008-01-10 02:21:45 +01:00
|
|
|
m_pendingFade = false;
|
|
|
|
}
|
|
|
|
|
2007-11-12 20:27:21 +01:00
|
|
|
ButtonType releasedAtButton = mapToButton(event->pos());
|
|
|
|
|
2007-12-21 04:10:35 +01:00
|
|
|
if (m_applet && event->button() == Qt::LeftButton) {
|
|
|
|
switch (m_pressedButton) {
|
2008-10-12 01:28:20 +02:00
|
|
|
case ResizeButton:
|
|
|
|
case RotateButton:
|
|
|
|
{
|
|
|
|
if (m_scaleWidth > 0 && m_scaleHeight > 0) {
|
|
|
|
QRectF rect(m_applet->boundingRect());
|
|
|
|
const qreal newWidth = rect.width() * m_scaleWidth;
|
|
|
|
const qreal newHeight = rect.height() * m_scaleHeight;
|
|
|
|
m_applet->resetTransform();
|
|
|
|
m_applet->resize(newWidth, newHeight);
|
|
|
|
scale(1.0 / m_scaleWidth, 1.0 / m_scaleHeight);
|
|
|
|
moveBy((rect.width() - newWidth) / 2, (rect.height() - newHeight) / 2);
|
|
|
|
m_scaleWidth = m_scaleHeight = 0;
|
|
|
|
}
|
|
|
|
QRectF rect = QRectF(m_applet->pos(), m_applet->size());
|
|
|
|
QPointF center = rect.center();
|
2007-12-21 04:10:35 +01:00
|
|
|
|
2008-10-12 01:28:20 +02:00
|
|
|
m_angle += m_tempAngle;
|
|
|
|
m_tempAngle = 0;
|
2008-01-08 02:22:44 +01:00
|
|
|
|
2008-10-12 01:28:20 +02:00
|
|
|
QTransform matrix;
|
|
|
|
matrix.translate(center.x(), center.y());
|
|
|
|
matrix.rotateRadians(m_angle);
|
|
|
|
matrix.translate(-center.x(), -center.y());
|
2007-12-21 04:10:35 +01:00
|
|
|
|
2008-10-12 01:28:20 +02:00
|
|
|
setTransform(matrix);
|
|
|
|
m_applet->update();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ConfigureButton:
|
|
|
|
//FIXME: Remove this call once the configuration management change was done
|
|
|
|
if (m_pressedButton == releasedAtButton) {
|
|
|
|
m_applet->showConfigurationInterface();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case RemoveButton:
|
|
|
|
if (m_pressedButton == releasedAtButton) {
|
|
|
|
forceDisappear();
|
|
|
|
m_applet->destroy();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case MoveButton:
|
|
|
|
{
|
|
|
|
if (m_topview) {
|
|
|
|
m_topview->hide();
|
|
|
|
delete m_topview;
|
|
|
|
m_topview = 0;
|
|
|
|
m_applet->d->ghost = false;
|
2007-12-21 04:10:35 +01:00
|
|
|
m_applet->update();
|
|
|
|
}
|
2008-05-12 00:02:35 +02:00
|
|
|
|
2008-10-12 01:28:20 +02:00
|
|
|
//find out if we were dropped on a panel or something
|
|
|
|
if (leaveCurrentView(event->screenPos())) {
|
|
|
|
startFading(FadeOut, m_entryPos);
|
|
|
|
Plasma::View *v = Plasma::View::topLevelViewAt(event->screenPos());
|
|
|
|
if (v && v != m_currentView) {
|
|
|
|
Containment *c = v->containment();
|
|
|
|
QPoint pos = v->mapFromGlobal(event->screenPos());
|
|
|
|
//we actually have been dropped on another containment, so
|
|
|
|
//move there: we have a screenpos, we need a scenepos
|
|
|
|
//FIXME how reliable is this transform?
|
|
|
|
switchContainment(c, v->mapToScene(pos));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// test for containment change
|
|
|
|
//kDebug() << "testing for containment change, sceneBoundingRect = "
|
|
|
|
// << m_containment->sceneBoundingRect();
|
|
|
|
if (!m_containment->sceneBoundingRect().contains(m_applet->scenePos())) {
|
|
|
|
// see which containment it belongs to
|
|
|
|
Corona * corona = qobject_cast<Corona*>(scene());
|
|
|
|
if (corona) {
|
|
|
|
QList<Containment*> containments = corona->containments();
|
|
|
|
for (int i = 0; i < containments.size(); ++i) {
|
|
|
|
QPointF pos;
|
|
|
|
QGraphicsView *v;
|
|
|
|
v = containments[i]->view();
|
|
|
|
if (v) {
|
|
|
|
pos = v->mapToScene(
|
|
|
|
v->mapFromGlobal(event->screenPos() - m_mousePos));
|
|
|
|
|
|
|
|
if (containments[i]->sceneBoundingRect().contains(pos)) {
|
|
|
|
//kDebug() << "new containment = " << containments[i];
|
|
|
|
//kDebug() << "rect = " << containments[i]->sceneBoundingRect();
|
|
|
|
// add the applet to the new containment and take it from the old one
|
|
|
|
//kDebug() << "moving to other containment with position" << pos;;
|
|
|
|
switchContainment(containments[i], pos);
|
|
|
|
break;
|
2008-05-19 18:20:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-02-03 14:09:21 +01:00
|
|
|
}
|
|
|
|
}
|
2008-10-12 01:28:20 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
2007-11-12 20:27:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m_pressedButton = NoButton;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
qreal _k_distanceForPoint(QPointF point)
|
|
|
|
{
|
2008-10-12 01:28:20 +02:00
|
|
|
return std::sqrt(point.x() * point.x() + point.y() * point.y());
|
2007-11-12 20:27:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
qreal _k_angleForPoints(const QPointF ¢er, const QPointF &pt1, const QPointF &pt2)
|
|
|
|
{
|
|
|
|
QPointF vec1 = pt1 - center;
|
|
|
|
QPointF vec2 = pt2 - center;
|
|
|
|
|
2007-12-10 20:52:29 +01:00
|
|
|
qreal alpha = std::atan2(vec1.y(), vec1.x());
|
|
|
|
qreal beta = std::atan2(vec2.y(), vec2.x());
|
2007-11-12 20:27:21 +01:00
|
|
|
|
|
|
|
return beta - alpha;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AppletHandle::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
|
|
|
|
{
|
2007-12-10 20:52:29 +01:00
|
|
|
static const qreal snapAngle = M_PI_2 /* $i 3.14159 / 2.0 */;
|
2007-11-18 03:37:30 +01:00
|
|
|
|
2007-11-22 06:11:06 +01:00
|
|
|
if (!m_applet) {
|
|
|
|
QGraphicsItem::mouseMoveEvent(event);
|
2007-12-15 06:48:13 +01:00
|
|
|
return;
|
2007-11-22 06:11:06 +01:00
|
|
|
}
|
2007-12-15 06:48:13 +01:00
|
|
|
|
2008-05-16 15:28:33 +02:00
|
|
|
//Track how much the mouse has moved.
|
|
|
|
QPointF deltaScene = event->scenePos() - event->lastScenePos();
|
2007-12-15 06:48:13 +01:00
|
|
|
|
|
|
|
if (m_pressedButton == MoveButton) {
|
2008-05-16 15:28:33 +02:00
|
|
|
m_pos += deltaScene;
|
|
|
|
|
2008-05-12 00:02:35 +02:00
|
|
|
//Are we moving out of the current view?
|
2008-05-26 17:09:06 +02:00
|
|
|
bool toTopLevel = leaveCurrentView(event->screenPos());
|
2008-05-12 00:02:35 +02:00
|
|
|
|
|
|
|
if (!toTopLevel) {
|
|
|
|
setPos(m_pos);
|
|
|
|
if (m_topview) {
|
2008-05-16 15:28:33 +02:00
|
|
|
//We were on a toplevel view, but are moving back on the scene
|
|
|
|
//again. destroy the toplevel view:
|
2008-05-12 00:02:35 +02:00
|
|
|
m_topview->hide();
|
|
|
|
delete m_topview;
|
|
|
|
m_topview = 0;
|
2008-09-09 02:23:02 +02:00
|
|
|
m_applet->d->ghost = false;
|
2008-05-12 00:02:35 +02:00
|
|
|
}
|
|
|
|
} else {
|
2008-05-26 17:09:06 +02:00
|
|
|
//set the screenRect correctly. the screenRect contains the bounding
|
|
|
|
//rect of the applet in screen coordinates. m_mousePos contains the
|
|
|
|
//position of the mouse relative to the applet, in screen coords.
|
2008-09-20 08:08:28 +02:00
|
|
|
QRect screenRect = QRect(event->screenPos() - m_mousePos, m_applet->size().toSize());
|
2008-05-26 17:09:06 +02:00
|
|
|
|
2008-07-10 03:35:51 +02:00
|
|
|
//kDebug() << "screenRect = " << screenRect;
|
2008-05-26 17:09:06 +02:00
|
|
|
|
2008-05-12 00:02:35 +02:00
|
|
|
if (!m_topview) { //create a new toplevel view
|
2008-05-19 18:20:27 +02:00
|
|
|
m_topview = new View(m_containment, -1, 0);
|
2008-05-11 15:56:35 +02:00
|
|
|
|
2008-05-16 15:28:33 +02:00
|
|
|
m_topview->setTrackContainmentChanges(false);
|
2008-10-12 01:28:20 +02:00
|
|
|
m_topview->setWindowFlags(
|
|
|
|
Qt::ToolTip | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
|
2008-05-12 00:02:35 +02:00
|
|
|
m_topview->setWallpaperEnabled(false);
|
2008-05-26 17:09:06 +02:00
|
|
|
m_topview->resize(screenRect.size());
|
2008-05-12 00:02:35 +02:00
|
|
|
m_topview->setSceneRect(m_applet->sceneBoundingRect());
|
|
|
|
m_topview->centerOn(m_applet);
|
2008-05-16 15:28:33 +02:00
|
|
|
|
|
|
|
//We might have to scale the view, because we might be zoomed out.
|
2008-05-26 17:09:06 +02:00
|
|
|
qreal scale = screenRect.width() / m_applet->boundingRect().width();
|
2008-05-12 00:02:35 +02:00
|
|
|
m_topview->scale(scale, scale);
|
2008-05-11 15:56:35 +02:00
|
|
|
|
2008-05-12 01:56:32 +02:00
|
|
|
//Paint a mask based on the applets shape.
|
2008-05-16 15:28:33 +02:00
|
|
|
//TODO: I think it's nicer to have this functionality in Applet.
|
|
|
|
//TODO: When the corona tiled background is disabled, disable the
|
|
|
|
//mask when compositing is enabled.
|
|
|
|
//FIXME: the mask doesn't function correctly when zoomed out.
|
2008-05-26 17:09:06 +02:00
|
|
|
QBitmap bitmap(screenRect.size());
|
2008-08-23 18:21:49 +02:00
|
|
|
{
|
|
|
|
QPainter shapePainter;
|
|
|
|
shapePainter.begin(&bitmap);
|
|
|
|
shapePainter.fillRect(0, 0, screenRect.width(),
|
|
|
|
screenRect.height(),
|
|
|
|
Qt::white);
|
|
|
|
shapePainter.setBrush(Qt::black);
|
|
|
|
shapePainter.drawPath(m_applet->shape());
|
|
|
|
shapePainter.end();
|
|
|
|
}
|
2008-05-12 01:56:32 +02:00
|
|
|
m_topview->setMask(bitmap);
|
|
|
|
|
2008-05-12 00:02:35 +02:00
|
|
|
m_topview->show();
|
2008-05-11 15:56:35 +02:00
|
|
|
|
2008-09-09 02:23:02 +02:00
|
|
|
m_applet->d->ghost = true;
|
2008-05-11 15:56:35 +02:00
|
|
|
|
2008-05-16 15:28:33 +02:00
|
|
|
//TODO: non compositing users are screwed: masking looks terrible.
|
|
|
|
//Consider always enabling the applet background. Stuff like the analog clock
|
|
|
|
//looks absolutely terrible when masked, while the minor rounded corners of most
|
|
|
|
//themes should look quite ok. I said should, since shape() doesn't really
|
|
|
|
//function correctly right now for applets drawing standard backgrounds.
|
|
|
|
}
|
2008-05-12 00:02:35 +02:00
|
|
|
|
2008-05-26 17:09:06 +02:00
|
|
|
m_topview->setGeometry(screenRect);
|
2008-05-12 00:02:35 +02:00
|
|
|
}
|
2008-05-11 15:56:35 +02:00
|
|
|
|
2008-01-06 04:13:07 +01:00
|
|
|
} else if (m_pressedButton == RotateButton ||
|
|
|
|
m_pressedButton == ResizeButton) {
|
2008-05-16 15:28:33 +02:00
|
|
|
if (_k_distanceForPoint(deltaScene) <= 1.0) {
|
2007-11-12 20:27:21 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QPointF pressPos = mapFromScene(event->buttonDownScenePos(Qt::LeftButton));
|
|
|
|
|
|
|
|
QRectF rect = QRectF(m_applet->pos(), m_applet->size());
|
|
|
|
QPointF center = rect.center();
|
|
|
|
|
2008-01-06 04:13:07 +01:00
|
|
|
if (m_pressedButton == RotateButton) {
|
2008-01-08 02:22:44 +01:00
|
|
|
m_tempAngle = _k_angleForPoints(center, pressPos, event->pos());
|
2008-01-05 12:58:48 +01:00
|
|
|
|
2008-10-12 01:28:20 +02:00
|
|
|
if (fabs(remainder(m_angle + m_tempAngle, snapAngle)) < 0.15) {
|
|
|
|
m_tempAngle = m_tempAngle - remainder(m_angle + m_tempAngle, snapAngle);
|
2008-01-05 12:58:48 +01:00
|
|
|
}
|
|
|
|
|
2008-01-06 04:13:07 +01:00
|
|
|
m_scaleWidth = m_scaleHeight = 1.0;
|
|
|
|
} else {
|
|
|
|
qreal w = m_applet->size().width();
|
|
|
|
qreal h = m_applet->size().height();
|
|
|
|
QSizeF min = m_applet->minimumSize();
|
|
|
|
QSizeF max = m_applet->maximumSize();
|
|
|
|
|
|
|
|
// If the applet doesn't have a minimum size, calculate based on a
|
|
|
|
// minimum content area size of 16x16
|
|
|
|
if (min.isEmpty()) {
|
2008-04-13 15:23:39 +02:00
|
|
|
min = m_applet->boundingRect().size() - m_applet->boundingRect().size();
|
2008-01-06 04:13:07 +01:00
|
|
|
min += QSizeF(16, 16);
|
2008-01-05 12:58:48 +01:00
|
|
|
}
|
|
|
|
|
2008-04-29 22:31:18 +02:00
|
|
|
bool ignoreAspectRatio = m_applet->aspectRatioMode() == Plasma::IgnoreAspectRatio;
|
2008-01-10 20:03:31 +01:00
|
|
|
|
2008-01-06 04:13:07 +01:00
|
|
|
if (QApplication::keyboardModifiers() & Qt::ControlModifier) {
|
2008-01-10 20:03:31 +01:00
|
|
|
ignoreAspectRatio = !ignoreAspectRatio;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ignoreAspectRatio) {
|
2008-01-06 04:13:07 +01:00
|
|
|
// free resizing
|
|
|
|
qreal newScaleWidth = 0;
|
|
|
|
qreal newScaleHeight = 0;
|
|
|
|
|
|
|
|
QPointF startDistance(pressPos - center);
|
|
|
|
QPointF currentDistance(event->pos() - center);
|
|
|
|
newScaleWidth = currentDistance.x() / startDistance.x();
|
|
|
|
newScaleHeight = currentDistance.y() / startDistance.y();
|
|
|
|
|
2008-01-07 04:40:12 +01:00
|
|
|
if (qAbs(w - (newScaleWidth * w)) <= KGlobalSettings::dndEventDelay()) {
|
2008-01-06 04:13:07 +01:00
|
|
|
newScaleWidth = 1.0;
|
|
|
|
}
|
2008-01-07 04:40:12 +01:00
|
|
|
if (qAbs(h - (newScaleHeight * h)) <= KGlobalSettings::dndEventDelay()) {
|
2008-01-06 04:13:07 +01:00
|
|
|
newScaleHeight = 1.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (newScaleHeight * h < min.height()) {
|
|
|
|
m_scaleHeight = min.height() / h;
|
|
|
|
} else if (newScaleHeight * h > max.height()) {
|
|
|
|
m_scaleHeight = max.height() / h;
|
|
|
|
} else {
|
|
|
|
m_scaleHeight = newScaleHeight;
|
|
|
|
}
|
|
|
|
if (newScaleWidth * w < min.width()) {
|
|
|
|
m_scaleWidth = min.width() / w;
|
|
|
|
} else if (newScaleWidth * w > max.width()) {
|
|
|
|
m_scaleWidth = max.width() / w;
|
|
|
|
} else {
|
|
|
|
m_scaleWidth = newScaleWidth;
|
|
|
|
}
|
2008-01-05 12:58:48 +01:00
|
|
|
} else {
|
2008-01-06 04:13:07 +01:00
|
|
|
// maintain aspect ratio
|
|
|
|
qreal newScale = 0;
|
|
|
|
|
2008-10-12 01:28:20 +02:00
|
|
|
newScale =
|
|
|
|
_k_distanceForPoint(event->pos()-center) /
|
|
|
|
_k_distanceForPoint(pressPos - center);
|
2008-01-07 04:40:12 +01:00
|
|
|
if (qAbs(h - (newScale * h)) <= KGlobalSettings::dndEventDelay()) {
|
2008-01-06 04:13:07 +01:00
|
|
|
newScale = 1.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (newScale * w < min.width() || newScale * h < min.height()) {
|
|
|
|
m_scaleWidth = m_scaleHeight = qMax(min.width() / w, min.height() / h);
|
|
|
|
} else if (newScale * w > max.width() && newScale * h > max.height()) {
|
|
|
|
m_scaleWidth = m_scaleHeight = qMin(max.width() / w, max.height() / h);
|
|
|
|
} else {
|
|
|
|
m_scaleHeight = m_scaleWidth = newScale;
|
|
|
|
}
|
2008-01-05 12:58:48 +01:00
|
|
|
}
|
2007-11-26 20:44:40 +01:00
|
|
|
}
|
|
|
|
|
2007-11-12 20:27:21 +01:00
|
|
|
QTransform matrix;
|
|
|
|
matrix.translate(center.x(), center.y());
|
2008-10-12 01:28:20 +02:00
|
|
|
matrix.rotateRadians(m_angle + m_tempAngle);
|
2008-01-05 12:58:48 +01:00
|
|
|
matrix.scale(m_scaleWidth, m_scaleHeight);
|
2007-11-12 20:27:21 +01:00
|
|
|
matrix.translate(-center.x(), -center.y());
|
|
|
|
setTransform(matrix);
|
|
|
|
} else {
|
|
|
|
QGraphicsItem::mouseMoveEvent(event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-03 14:09:21 +01:00
|
|
|
//pos relative to scene
|
|
|
|
void AppletHandle::switchContainment(Containment *containment, const QPointF &pos)
|
|
|
|
{
|
2008-06-29 02:27:04 +02:00
|
|
|
if (containment->containmentType() != Containment::PanelContainment) {
|
2008-06-26 21:23:07 +02:00
|
|
|
//FIXME assuming everything else behaves like desktop?
|
|
|
|
kDebug() << "desktop";
|
|
|
|
m_containment = containment;
|
2008-02-03 14:09:21 +01:00
|
|
|
}
|
2008-06-26 21:23:07 +02:00
|
|
|
|
|
|
|
Applet *applet = m_applet;
|
|
|
|
m_applet = 0; //make sure we don't try to act on the applet again
|
|
|
|
applet->removeSceneEventFilter(this);
|
|
|
|
forceDisappear(); //takes care of event filter and killing handle
|
|
|
|
applet->disconnect(this); //make sure the applet doesn't tell us to do anything
|
|
|
|
applet->setZValue(m_zValue);
|
|
|
|
containment->addApplet(applet, containment->mapFromScene(pos));
|
2008-02-03 14:09:21 +01:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2007-12-08 04:38:11 +01:00
|
|
|
QVariant AppletHandle::itemChange(GraphicsItemChange change, const QVariant &value)
|
|
|
|
{
|
|
|
|
if (change == ItemPositionHasChanged && m_applet) {
|
2008-04-27 13:04:43 +02:00
|
|
|
m_applet->updateConstraints(Plasma::LocationConstraint);
|
2007-12-08 04:38:11 +01:00
|
|
|
}
|
|
|
|
return QGraphicsItem::itemChange(change, value);
|
|
|
|
}
|
|
|
|
|
2007-11-12 20:27:21 +01:00
|
|
|
void AppletHandle::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
|
|
|
|
{
|
|
|
|
Q_UNUSED(event);
|
2008-01-08 02:25:09 +01:00
|
|
|
//kDebug() << "hover enter";
|
2008-06-22 15:47:04 +02:00
|
|
|
|
2008-08-20 11:14:27 +02:00
|
|
|
//if a disappear was scheduled stop the timer
|
|
|
|
m_leaveTimer->stop();
|
|
|
|
|
2008-08-12 21:08:55 +02:00
|
|
|
// if we're already fading out, fade back in
|
2008-08-20 11:14:27 +02:00
|
|
|
if (m_animId != 0 && m_anim == FadeOut) {
|
2008-08-12 21:08:55 +02:00
|
|
|
startFading(FadeIn, m_entryPos);
|
|
|
|
} else {
|
|
|
|
//schedule appear
|
|
|
|
m_hoverTimer->start();
|
|
|
|
}
|
2007-11-12 20:27:21 +01:00
|
|
|
}
|
|
|
|
|
2008-07-10 03:35:51 +02:00
|
|
|
void AppletHandle::hoverMoveEvent(QGraphicsSceneHoverEvent *event)
|
|
|
|
{
|
|
|
|
Q_UNUSED(event);
|
|
|
|
m_leaveTimer->stop();
|
|
|
|
}
|
|
|
|
|
2007-11-12 20:27:21 +01:00
|
|
|
void AppletHandle::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
|
|
|
|
{
|
|
|
|
Q_UNUSED(event);
|
2008-01-10 00:43:49 +01:00
|
|
|
m_hoverTimer->stop();
|
2008-01-10 02:21:45 +01:00
|
|
|
|
|
|
|
if (m_pressedButton != NoButton) {
|
|
|
|
m_pendingFade = true;
|
|
|
|
} else {
|
2008-06-22 15:47:04 +02:00
|
|
|
//wait a moment to hide the handle in order to recheck the mouse position
|
|
|
|
m_leaveTimer->start();
|
2008-01-10 02:21:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AppletHandle::sceneEventFilter(QGraphicsItem *watched, QEvent *event)
|
|
|
|
{
|
|
|
|
if (watched == m_applet && event->type() == QEvent::GraphicsSceneHoverLeave) {
|
|
|
|
hoverLeaveEvent(static_cast<QGraphicsSceneHoverEvent*>(event));
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2007-11-12 20:27:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void AppletHandle::fadeAnimation(qreal progress)
|
|
|
|
{
|
2008-09-20 07:47:18 +02:00
|
|
|
//qreal endOpacity = (m_anim == FadeIn) ? 1.0 : -1.0;
|
|
|
|
if (m_anim == FadeIn) {
|
|
|
|
m_opacity = progress;
|
|
|
|
} else {
|
|
|
|
m_opacity = 1 - progress;
|
|
|
|
}
|
|
|
|
//kDebug() << "progress" << progress << "m_opacity" << m_opacity;// << endOpacity;
|
2008-09-24 17:59:29 +02:00
|
|
|
if (qFuzzyCompare(progress, qreal(1.0))) {
|
2008-08-20 11:14:27 +02:00
|
|
|
m_animId = 0;
|
2008-09-18 21:40:46 +02:00
|
|
|
delete m_backgroundBuffer;
|
|
|
|
m_backgroundBuffer = 0;
|
2007-11-12 20:27:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2008-01-10 00:43:49 +01:00
|
|
|
void AppletHandle::fadeIn()
|
|
|
|
{
|
2008-07-10 03:35:51 +02:00
|
|
|
startFading(FadeIn, m_entryPos);
|
2008-01-10 00:43:49 +01:00
|
|
|
}
|
|
|
|
|
2008-06-22 15:47:04 +02:00
|
|
|
void AppletHandle::leaveTimeout()
|
|
|
|
{
|
2008-07-10 03:35:51 +02:00
|
|
|
startFading(FadeOut, m_entryPos);
|
2008-06-22 15:47:04 +02:00
|
|
|
}
|
|
|
|
|
2007-11-22 06:11:06 +01:00
|
|
|
void AppletHandle::appletDestroyed()
|
|
|
|
{
|
|
|
|
m_applet = 0;
|
|
|
|
}
|
|
|
|
|
2007-11-23 09:01:29 +01:00
|
|
|
void AppletHandle::appletResized()
|
|
|
|
{
|
|
|
|
prepareGeometryChange();
|
|
|
|
calculateSize();
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2008-07-10 03:35:51 +02:00
|
|
|
void AppletHandle::startFading(FadeType anim, const QPointF &hoverPos)
|
2007-11-12 20:27:21 +01:00
|
|
|
{
|
2008-01-10 00:43:49 +01:00
|
|
|
if (m_animId != 0) {
|
2008-04-25 05:11:59 +02:00
|
|
|
Animator::self()->stopCustomAnimation(m_animId);
|
2007-11-12 20:27:21 +01:00
|
|
|
}
|
|
|
|
|
2008-07-10 03:35:51 +02:00
|
|
|
m_hoverTimer->stop();
|
|
|
|
m_leaveTimer->stop();
|
|
|
|
|
|
|
|
m_entryPos = hoverPos;
|
2008-09-20 07:47:18 +02:00
|
|
|
qreal time = 100;
|
2008-02-13 01:49:07 +01:00
|
|
|
|
2008-07-10 03:35:51 +02:00
|
|
|
if (!m_applet || (anim == FadeOut && m_hoverTimer->isActive())) {
|
2008-02-13 01:49:07 +01:00
|
|
|
// fading out before we've started fading in
|
|
|
|
fadeAnimation(1.0);
|
|
|
|
return;
|
|
|
|
}
|
2007-11-12 20:27:21 +01:00
|
|
|
|
2008-01-10 00:43:49 +01:00
|
|
|
if (anim == FadeIn) {
|
2008-07-10 03:35:51 +02:00
|
|
|
//kDebug() << m_entryPos.x() << m_applet->pos().x();
|
|
|
|
prepareGeometryChange();
|
|
|
|
bool wasOnRight = m_buttonsOnRight;
|
|
|
|
m_buttonsOnRight = m_entryPos.x() > (m_applet->size().width() / 2);
|
|
|
|
calculateSize();
|
|
|
|
QPolygonF region = mapToParent(m_rect).intersected(parentWidget()->boundingRect());
|
|
|
|
//kDebug() << region << m_rect << mapToParent(m_rect) << parentWidget()->boundingRect();
|
|
|
|
if (region != mapToParent(m_rect)) {
|
|
|
|
// switch sides
|
|
|
|
//kDebug() << "switch sides";
|
|
|
|
m_buttonsOnRight = !m_buttonsOnRight;
|
|
|
|
calculateSize();
|
|
|
|
QPolygonF region2 = mapToParent(m_rect).intersected(parentWidget()->boundingRect());
|
|
|
|
if (region2 != mapToParent(m_rect)) {
|
|
|
|
// ok, both sides failed to be perfect... which one is more perfect?
|
|
|
|
QRectF f1 = region.boundingRect();
|
|
|
|
QRectF f2 = region2.boundingRect();
|
2008-10-12 01:28:20 +02:00
|
|
|
//kDebug() << "still not a perfect world"
|
|
|
|
// << f2.width() << f2.height() << f1.width() << f1.height();
|
2008-07-10 03:35:51 +02:00
|
|
|
if ((f2.width() * f2.height()) < (f1.width() * f1.height())) {
|
|
|
|
//kDebug() << "we did better the first time";
|
|
|
|
m_buttonsOnRight = !m_buttonsOnRight;
|
|
|
|
calculateSize();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-12 01:28:20 +02:00
|
|
|
if (wasOnRight != m_buttonsOnRight &&
|
|
|
|
m_anim == FadeIn &&
|
|
|
|
anim == FadeIn &&
|
|
|
|
m_opacity <= 1) {
|
2008-07-10 03:35:51 +02:00
|
|
|
m_opacity = 0.0;
|
|
|
|
}
|
|
|
|
|
2008-02-13 01:49:07 +01:00
|
|
|
time *= 1.0 - m_opacity;
|
2007-11-12 20:27:21 +01:00
|
|
|
} else {
|
|
|
|
time *= m_opacity;
|
|
|
|
}
|
|
|
|
|
2008-07-10 03:35:51 +02:00
|
|
|
m_anim = anim;
|
2008-09-20 07:47:18 +02:00
|
|
|
//kDebug() << "animating for " << time << "ms";
|
2008-10-12 01:28:20 +02:00
|
|
|
m_animId = Animator::self()->customAnimation(
|
|
|
|
80 * (time / 1000.0), (int)time, Animator::EaseInCurve, this, "fadeAnimation");
|
2007-11-12 20:27:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void AppletHandle::forceDisappear()
|
|
|
|
{
|
|
|
|
setAcceptsHoverEvents(false);
|
2008-07-10 03:35:51 +02:00
|
|
|
startFading(FadeOut, m_entryPos);
|
2007-11-12 20:27:21 +01:00
|
|
|
}
|
|
|
|
|
2008-07-10 21:48:55 +02:00
|
|
|
int AppletHandle::minimumHeight()
|
2007-11-23 09:01:29 +01:00
|
|
|
{
|
2008-07-16 05:53:07 +02:00
|
|
|
int iconMargin = m_iconSize / 2;
|
|
|
|
int requiredHeight = iconMargin + //first margin
|
2008-10-12 01:28:20 +02:00
|
|
|
(m_iconSize + iconMargin) * 4 + //XXX remember to update this if the number of buttons changes
|
2008-07-16 05:53:07 +02:00
|
|
|
iconMargin ; //blank space before the close button
|
2008-07-06 02:18:43 +02:00
|
|
|
|
2008-07-10 21:48:55 +02:00
|
|
|
if (m_applet && m_applet->hasConfigurationInterface()) {
|
2008-10-12 01:28:20 +02:00
|
|
|
requiredHeight += (m_iconSize + iconMargin);
|
2007-11-23 09:01:29 +01:00
|
|
|
}
|
|
|
|
|
2008-07-10 21:48:55 +02:00
|
|
|
return requiredHeight;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AppletHandle::calculateSize()
|
|
|
|
{
|
2008-07-16 19:54:46 +02:00
|
|
|
KIconLoader *iconLoader = KIconLoader::global();
|
2008-07-16 19:23:35 +02:00
|
|
|
//m_iconSize = iconLoader->currentSize(KIconLoader::Small); //does not work with double sized icon
|
|
|
|
m_iconSize = iconLoader->loadIcon("transform-scale", KIconLoader::Small).width(); //workaround
|
2008-07-16 05:53:07 +02:00
|
|
|
|
2008-09-18 21:40:46 +02:00
|
|
|
int handleHeight = qMax(minimumHeight(), int(m_applet->contentsRect().height() * 0.8));
|
2008-07-16 05:53:07 +02:00
|
|
|
int handleWidth = m_iconSize + 2 * HANDLE_MARGIN;
|
2008-10-12 01:28:20 +02:00
|
|
|
int top =
|
|
|
|
m_applet->contentsRect().top() + (m_applet->contentsRect().height() - handleHeight) / 2.0;
|
2008-07-10 03:35:51 +02:00
|
|
|
|
2008-09-18 21:40:46 +02:00
|
|
|
qreal marginLeft, marginTop, marginRight, marginBottom;
|
|
|
|
m_background->getMargins(marginLeft, marginTop, marginRight, marginBottom);
|
2008-07-10 03:35:51 +02:00
|
|
|
|
|
|
|
if (m_buttonsOnRight) {
|
2008-07-06 02:18:43 +02:00
|
|
|
//put the rect on the right of the applet
|
2008-07-16 05:53:07 +02:00
|
|
|
m_rect = QRectF(m_applet->size().width(), top, handleWidth, handleHeight);
|
2007-11-23 09:01:29 +01:00
|
|
|
} else {
|
2008-07-06 02:18:43 +02:00
|
|
|
//put the rect on the left of the applet
|
2008-07-16 05:53:07 +02:00
|
|
|
m_rect = QRectF(-handleWidth, top, handleWidth, handleHeight);
|
2007-11-23 09:01:29 +01:00
|
|
|
}
|
2008-07-06 02:18:43 +02:00
|
|
|
|
2008-09-18 21:40:46 +02:00
|
|
|
if (m_applet->contentsRect().height() > qreal(minimumHeight()) * 1.25) {
|
2008-10-12 01:28:20 +02:00
|
|
|
int addedMargin = marginLeft / 2;
|
2008-10-02 00:26:43 +02:00
|
|
|
|
|
|
|
// now we check to see if the shape is smaller than the contents,
|
|
|
|
// and that the shape is not just the bounding rect; in those cases
|
|
|
|
// we have a shaped guy and we draw a full panel;
|
|
|
|
// TODO: allow applets to mark when they have translucent areas and
|
|
|
|
// should therefore skip this test?
|
2008-09-18 21:40:46 +02:00
|
|
|
if (!m_applet->shape().contains(m_applet->contentsRect())) {
|
2008-10-02 00:26:43 +02:00
|
|
|
QPainterPath p;
|
|
|
|
p.addRect(m_applet->boundingRect());
|
|
|
|
if (m_applet->shape() != p) {
|
2008-10-12 01:28:20 +02:00
|
|
|
addedMargin = m_applet->contentsRect().width() / 2;
|
2008-10-02 00:26:43 +02:00
|
|
|
}
|
2008-09-18 21:40:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (m_buttonsOnRight) {
|
|
|
|
marginLeft += addedMargin;
|
|
|
|
} else {
|
|
|
|
marginRight += addedMargin;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-08 00:59:03 +02:00
|
|
|
m_rect = m_applet->mapToParent(m_rect).boundingRect();
|
2008-09-18 21:40:46 +02:00
|
|
|
m_decorationRect = m_rect.adjusted(-marginLeft, -marginTop, marginRight, marginBottom);
|
|
|
|
m_totalRect = m_decorationRect.united(m_applet->geometry());
|
2007-11-23 09:01:29 +01:00
|
|
|
}
|
|
|
|
|
2008-07-10 03:35:51 +02:00
|
|
|
} // Plasma Namespace
|
2007-11-12 20:27:21 +01:00
|
|
|
|
|
|
|
#include "applethandle_p.moc"
|
2008-07-30 22:35:42 +02:00
|
|
|
|