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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "applethandle_p.h"
|
|
|
|
|
|
|
|
#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>
|
|
|
|
|
|
|
|
#include <cmath>
|
2007-12-10 20:52:29 +01:00
|
|
|
#include <math.h>
|
2007-11-12 20:27:21 +01:00
|
|
|
|
|
|
|
#include "applet.h"
|
2007-11-18 22:34:27 +01:00
|
|
|
#include "containment.h"
|
2007-11-26 22:57:50 +01:00
|
|
|
#include "corona.h"
|
|
|
|
#include "applet.h"
|
2007-11-18 22:34:27 +01:00
|
|
|
#include "theme.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);
|
|
|
|
|
2007-11-12 20:27:21 +01:00
|
|
|
AppletHandle::AppletHandle(Containment *parent, Applet *applet)
|
2007-11-18 03:00:20 +01:00
|
|
|
: QObject(),
|
|
|
|
QGraphicsItem(parent),
|
|
|
|
m_pressedButton(NoButton),
|
|
|
|
m_containment(parent),
|
|
|
|
m_applet(applet),
|
|
|
|
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),
|
|
|
|
m_buttonsOnRight(false),
|
|
|
|
m_pendingFade(false)
|
2007-11-12 20:27:21 +01:00
|
|
|
{
|
2007-11-18 22:34:27 +01:00
|
|
|
KColorScheme colors(QPalette::Active, KColorScheme::View, Theme::self()->colors());
|
|
|
|
m_gradientColor = colors.background(KColorScheme::NormalBackground).color();
|
2007-11-26 20:37:20 +01:00
|
|
|
|
|
|
|
QTransform originalMatrix = m_applet->transform();
|
|
|
|
QRectF rect(m_applet->boundingRect());
|
|
|
|
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));
|
|
|
|
|
|
|
|
m_applet->resetTransform();
|
2007-11-12 20:27:21 +01:00
|
|
|
|
2007-11-23 09:01:29 +01:00
|
|
|
calculateSize();
|
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-01-10 00:43:49 +01:00
|
|
|
m_hoverTimer = new QTimer(this);
|
|
|
|
m_hoverTimer->setSingleShot(true);
|
|
|
|
m_hoverTimer->setInterval(300);
|
2008-01-08 02:22:44 +01:00
|
|
|
|
2008-01-10 00:43:49 +01:00
|
|
|
connect(m_hoverTimer, SIGNAL(timeout()), this, SLOT(fadeIn()));
|
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();
|
2007-11-12 20:27:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
AppletHandle::~AppletHandle()
|
|
|
|
{
|
2007-12-21 04:10:35 +01:00
|
|
|
if (m_applet) {
|
2008-01-10 08:28:33 +01:00
|
|
|
m_applet->removeSceneEventFilter(this);
|
|
|
|
|
2008-01-08 01:52:34 +01:00
|
|
|
QRectF rect = QRectF(m_applet->pos(), m_applet->size());
|
|
|
|
QPointF center = m_applet->mapFromParent(rect.center());
|
2007-11-19 08:58:31 +01:00
|
|
|
|
2007-12-21 04:10:35 +01:00
|
|
|
QPointF newPos = transform().inverted().map(m_applet->pos());
|
|
|
|
m_applet->setPos(mapToParent(newPos));
|
2008-01-08 01:52:34 +01:00
|
|
|
|
2007-12-21 04:10:35 +01:00
|
|
|
QTransform matrix;
|
|
|
|
matrix.translate(center.x(), center.y());
|
2008-01-08 02:22:44 +01:00
|
|
|
matrix.rotateRadians(m_angle);
|
2007-12-21 04:10:35 +01:00
|
|
|
matrix.translate(-center.x(), -center.y());
|
|
|
|
m_applet->setTransform(matrix);
|
2008-01-08 01:52:34 +01:00
|
|
|
|
2007-12-21 04:10:35 +01:00
|
|
|
m_applet->setParentItem(m_containment);
|
2008-01-11 13:11:16 +01:00
|
|
|
|
|
|
|
m_applet->update(); // re-render the background, now we've transformed the applet
|
2007-11-19 08:58:31 +01:00
|
|
|
}
|
2007-11-12 20:27:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Applet *AppletHandle::applet() const
|
|
|
|
{
|
|
|
|
return m_applet;
|
|
|
|
}
|
|
|
|
|
|
|
|
QRectF Plasma::AppletHandle::boundingRect() const
|
|
|
|
{
|
|
|
|
return m_rect;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AppletHandle::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
|
|
|
|
{
|
|
|
|
Q_UNUSED(option);
|
|
|
|
Q_UNUSED(widget);
|
|
|
|
|
|
|
|
painter->save();
|
|
|
|
painter->setOpacity(m_opacity);
|
|
|
|
|
2007-11-18 22:34:27 +01:00
|
|
|
painter->save();
|
|
|
|
painter->setOpacity(m_opacity * 0.4);
|
|
|
|
painter->setPen(Qt::NoPen);
|
|
|
|
painter->setRenderHints(QPainter::Antialiasing);
|
|
|
|
QLinearGradient gr(boundingRect().topLeft(), boundingRect().bottomRight());
|
|
|
|
gr.setColorAt(0, m_gradientColor);
|
|
|
|
gr.setColorAt(0.1, KColorScheme::shade(m_gradientColor, KColorScheme::LightShade));
|
|
|
|
gr.setColorAt(1, KColorScheme::shade(m_gradientColor, KColorScheme::DarkShade));
|
|
|
|
painter->setBrush(gr);
|
2008-01-12 07:03:54 +01:00
|
|
|
QPainterPath path = Plasma::roundedRectangle(boundingRect(), 10);
|
|
|
|
|
|
|
|
if (m_applet) {
|
|
|
|
QPainterPath shape = m_applet->shape();
|
|
|
|
|
|
|
|
if (!shape.isEmpty()) {
|
|
|
|
path = path.subtracted(m_applet->mapToParent(m_applet->shape()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
painter->drawPath(path);
|
2007-11-18 22:34:27 +01:00
|
|
|
painter->restore();
|
2007-11-12 20:27:21 +01:00
|
|
|
|
|
|
|
QPointF point = m_rect.topLeft();
|
|
|
|
|
|
|
|
if (m_buttonsOnRight) {
|
2007-11-19 08:23:07 +01:00
|
|
|
point += QPointF(m_rect.width() - ICON_SIZE - HANDLE_WIDTH, HANDLE_WIDTH);
|
2007-11-12 20:27:21 +01:00
|
|
|
} else {
|
2007-11-19 08:23:07 +01:00
|
|
|
point+= QPointF(HANDLE_WIDTH / 2, HANDLE_WIDTH);
|
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-01-06 16:17:45 +01:00
|
|
|
painter->drawPixmap(point + shiftM, KIcon("transform-move").pixmap(ICON_SIZE, ICON_SIZE)); // no transform-resize icon
|
|
|
|
|
|
|
|
point += QPointF(0.0, ICON_SIZE + ICON_MARGIN);
|
|
|
|
painter->drawPixmap(point + shiftR, KIcon("transform-rotate").pixmap(ICON_SIZE, ICON_SIZE));
|
|
|
|
|
2007-11-22 06:11:06 +01:00
|
|
|
if (m_applet && m_applet->hasConfigurationInterface()) {
|
2007-11-19 16:52:17 +01:00
|
|
|
point += QPointF(0.0, ICON_SIZE + ICON_MARGIN);
|
2008-01-06 16:17:45 +01:00
|
|
|
painter->drawPixmap(point + shiftC, KIcon("configure").pixmap(ICON_SIZE, ICON_SIZE));
|
2007-11-12 20:27:21 +01:00
|
|
|
}
|
2008-01-06 04:13:07 +01:00
|
|
|
|
2007-11-19 08:58:31 +01:00
|
|
|
point += QPointF(0.0, ICON_SIZE + ICON_MARGIN * 2);
|
2007-11-18 03:00:20 +01:00
|
|
|
painter->drawPixmap(point + shiftD, KIcon("edit-delete").pixmap(ICON_SIZE, ICON_SIZE));
|
2007-11-12 20:27:21 +01:00
|
|
|
|
|
|
|
painter->restore();
|
|
|
|
}
|
|
|
|
|
|
|
|
AppletHandle::ButtonType AppletHandle::mapToButton(const QPointF &point) const
|
|
|
|
{
|
|
|
|
QPointF basePoint = m_rect.topLeft();
|
|
|
|
|
|
|
|
if (m_buttonsOnRight) {
|
2007-11-19 08:23:07 +01:00
|
|
|
basePoint+= QPointF(m_rect.width() - ICON_SIZE, HANDLE_WIDTH);
|
2007-11-12 20:27:21 +01:00
|
|
|
} else {
|
2007-11-18 02:16:47 +01:00
|
|
|
basePoint+= QPointF(HANDLE_WIDTH, HANDLE_WIDTH);
|
2007-11-12 20:27:21 +01:00
|
|
|
}
|
|
|
|
|
2007-11-18 02:16:47 +01:00
|
|
|
QPolygonF activeArea = QPolygonF(QRectF(basePoint, QSizeF(ICON_SIZE, ICON_SIZE)));
|
2007-11-12 20:27:21 +01:00
|
|
|
|
2008-01-06 16:17:45 +01:00
|
|
|
if (activeArea.containsPoint(point, Qt::OddEvenFill)) {
|
|
|
|
return ResizeButton;
|
2007-11-12 20:27:21 +01:00
|
|
|
}
|
|
|
|
|
2008-01-06 16:17:45 +01:00
|
|
|
activeArea.translate(QPointF(0.0, ICON_SIZE + ICON_MARGIN));
|
2007-11-12 20:27:21 +01:00
|
|
|
if (activeArea.containsPoint(point, Qt::OddEvenFill)) {
|
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()) {
|
|
|
|
activeArea.translate(QPointF(0.0, ICON_SIZE + ICON_MARGIN));
|
|
|
|
if (activeArea.containsPoint(point, Qt::OddEvenFill)) {
|
|
|
|
return ConfigureButton;
|
|
|
|
}
|
2008-01-06 04:13:07 +01:00
|
|
|
}
|
|
|
|
|
2007-11-19 08:58:31 +01:00
|
|
|
activeArea.translate(QPointF(0.0, ICON_SIZE + ICON_MARGIN * 2));
|
2007-11-12 20:27:21 +01:00
|
|
|
if (activeArea.containsPoint(point, Qt::OddEvenFill)) {
|
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-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) {
|
|
|
|
// when the mouse goes over a window, the applet is likely to
|
|
|
|
// get a hover out event that we really don't want to respond to
|
|
|
|
// so while we have a button pressed we intercept these events
|
|
|
|
// and handle them ourselves here in AppletHandle
|
|
|
|
m_applet->installSceneEventFilter(this);
|
|
|
|
}
|
2007-11-18 22:34:27 +01:00
|
|
|
event->accept();
|
2007-11-12 20:27:21 +01:00
|
|
|
update();
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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_applet) {
|
|
|
|
m_applet->removeSceneEventFilter(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_pendingFade) {
|
|
|
|
startFading(FadeOut);
|
|
|
|
m_pendingFade = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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-01-06 04:13:07 +01:00
|
|
|
case ResizeButton:
|
2007-12-21 04:10:35 +01:00
|
|
|
case RotateButton: {
|
2008-01-05 12:58:48 +01:00
|
|
|
if (m_scaleWidth > 0 && m_scaleHeight > 0) {
|
2007-12-21 04:10:35 +01:00
|
|
|
QRectF rect(m_applet->boundingRect());
|
2008-01-05 12:58:48 +01:00
|
|
|
const qreal newWidth = rect.width() * m_scaleWidth;
|
|
|
|
const qreal newHeight = rect.height() * m_scaleHeight;
|
2007-12-21 04:10:35 +01:00
|
|
|
m_applet->resetTransform();
|
|
|
|
m_applet->resize(newWidth, newHeight);
|
2008-01-05 12:58:48 +01:00
|
|
|
scale(1.0/m_scaleWidth, 1.0/m_scaleHeight);
|
2007-12-21 04:10:35 +01:00
|
|
|
moveBy((rect.width() - newWidth) / 2, (rect.height() - newHeight) / 2);
|
2008-01-05 12:58:48 +01:00
|
|
|
m_scaleWidth = m_scaleHeight = 0;
|
2007-12-21 04:10:35 +01:00
|
|
|
}
|
2008-01-08 01:39:32 +01:00
|
|
|
QRectF rect = QRectF(m_applet->pos(), m_applet->size());
|
2007-12-21 04:10:35 +01:00
|
|
|
QPointF center = rect.center();
|
|
|
|
|
2008-01-08 02:22:44 +01:00
|
|
|
m_angle += m_tempAngle;
|
|
|
|
m_tempAngle = 0;
|
|
|
|
|
2007-12-21 04:10:35 +01:00
|
|
|
QTransform matrix;
|
|
|
|
matrix.translate(center.x(), center.y());
|
2008-01-08 02:22:44 +01:00
|
|
|
matrix.rotateRadians(m_angle);
|
2007-12-21 04:10:35 +01:00
|
|
|
matrix.translate(-center.x(), -center.y());
|
|
|
|
|
|
|
|
setTransform(matrix);
|
|
|
|
m_applet->update();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ConfigureButton:
|
|
|
|
//FIXME: Remove this call once the configuration management change was done
|
|
|
|
if (m_pressedButton == releasedAtButton) {
|
|
|
|
m_containment->emitLaunchActivated();
|
|
|
|
m_applet->showConfigurationInterface();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case RemoveButton:
|
|
|
|
if (m_pressedButton == releasedAtButton) {
|
|
|
|
forceDisappear();
|
|
|
|
Phase::self()->animateItem(m_applet, Phase::Disappear);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2007-11-12 20:27:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m_pressedButton = NoButton;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
qreal _k_distanceForPoint(QPointF point)
|
|
|
|
{
|
2007-12-10 20:52:29 +01: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
|
|
|
|
|
|
|
QPointF curPos = transform().map(event->pos());
|
|
|
|
QPointF lastPos = transform().map(event->lastPos());
|
|
|
|
QPointF delta = curPos-lastPos;
|
|
|
|
|
|
|
|
if (m_pressedButton == MoveButton) {
|
2007-11-12 20:27:21 +01:00
|
|
|
setPos(pos()+delta);
|
2007-11-26 22:57:50 +01:00
|
|
|
// test for containment change
|
|
|
|
if (!m_containment->sceneBoundingRect().contains(event->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) {
|
|
|
|
if (containments[i]->sceneBoundingRect().contains(event->scenePos())) {
|
|
|
|
// add the applet to the new containment
|
|
|
|
// and take it from the old one
|
|
|
|
QPointF scenePosition = scenePos();
|
2008-01-10 02:21:45 +01:00
|
|
|
//kDebug() << "moving to other containment with position" << pos() << event->scenePos();
|
|
|
|
//kDebug() << "position before reparenting" << pos() << scenePos();
|
2007-11-26 22:57:50 +01:00
|
|
|
m_containment = containments[i];
|
|
|
|
//m_containment->addChild(m_applet);
|
|
|
|
//setParentItem(containments[i]);
|
2007-11-27 00:38:39 +01:00
|
|
|
m_containment->addApplet(m_applet);
|
|
|
|
setParentItem(m_containment);
|
2007-11-27 07:04:45 +01:00
|
|
|
m_applet->setParentItem(this);
|
|
|
|
setPos(m_containment->mapFromScene(scenePosition));
|
2007-11-26 22:57:50 +01:00
|
|
|
update();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-01-06 04:13:07 +01:00
|
|
|
} else if (m_pressedButton == RotateButton ||
|
|
|
|
m_pressedButton == ResizeButton) {
|
2007-12-15 06:48:13 +01:00
|
|
|
if (_k_distanceForPoint(delta) <= 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-01-08 02:22:44 +01: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()) {
|
|
|
|
min = m_applet->boundingRect().size() - m_applet->contentRect().size();
|
|
|
|
min += QSizeF(16, 16);
|
2008-01-05 12:58:48 +01:00
|
|
|
}
|
|
|
|
|
2008-01-10 20:03:31 +01:00
|
|
|
bool ignoreAspectRatio = m_applet->aspectRatioMode() == Qt::IgnoreAspectRatio;
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
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-01-08 02:22:44 +01: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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-08 04:38:11 +01:00
|
|
|
QVariant AppletHandle::itemChange(GraphicsItemChange change, const QVariant &value)
|
|
|
|
{
|
|
|
|
if (change == ItemPositionHasChanged && m_applet) {
|
|
|
|
m_applet->constraintsUpdated(Plasma::LocationConstraint);
|
|
|
|
}
|
|
|
|
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-01-10 00:43:49 +01:00
|
|
|
m_hoverTimer->start();
|
2007-11-12 20:27:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void AppletHandle::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
|
|
|
|
{
|
|
|
|
Q_UNUSED(event);
|
2008-01-10 02:21:45 +01:00
|
|
|
//kDebug() << "hover leave" << m_pressedButton;
|
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 {
|
|
|
|
startFading(FadeOut);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AppletHandle::sceneEventFilter(QGraphicsItem *watched, QEvent *event)
|
|
|
|
{
|
|
|
|
if (watched == m_applet && event->type() == QEvent::GraphicsSceneHoverLeave) {
|
|
|
|
hoverLeaveEvent(static_cast<QGraphicsSceneHoverEvent*>(event));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2007-11-12 20:27:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void AppletHandle::fadeAnimation(qreal progress)
|
|
|
|
{
|
|
|
|
qreal endOpacity = 1.0;
|
|
|
|
if (m_anim==FadeOut) {
|
|
|
|
endOpacity = 0.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_opacity += (endOpacity-m_opacity)*progress;
|
|
|
|
|
|
|
|
if (progress>=1.0 && m_anim==FadeOut) {
|
2008-01-12 07:03:54 +01:00
|
|
|
if (m_applet) {
|
|
|
|
m_applet->removeSceneEventFilter(this);
|
|
|
|
}
|
|
|
|
|
2007-11-12 20:27:21 +01:00
|
|
|
emit disappearDone(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2008-01-10 00:43:49 +01:00
|
|
|
void AppletHandle::fadeIn()
|
|
|
|
{
|
|
|
|
startFading(FadeIn);
|
|
|
|
}
|
|
|
|
|
2007-11-22 06:11:06 +01:00
|
|
|
void AppletHandle::appletDestroyed()
|
|
|
|
{
|
|
|
|
m_applet = 0;
|
|
|
|
deleteLater();
|
|
|
|
}
|
|
|
|
|
2007-11-23 09:01:29 +01:00
|
|
|
void AppletHandle::appletResized()
|
|
|
|
{
|
|
|
|
prepareGeometryChange();
|
|
|
|
calculateSize();
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2007-11-12 20:27:21 +01:00
|
|
|
void AppletHandle::startFading(FadeType anim)
|
|
|
|
{
|
2008-01-10 00:43:49 +01:00
|
|
|
if (m_animId != 0) {
|
2007-11-12 20:27:21 +01:00
|
|
|
Phase::self()->stopCustomAnimation(m_animId);
|
|
|
|
}
|
|
|
|
|
2007-11-18 02:16:47 +01:00
|
|
|
qreal time = 250;
|
2008-01-11 06:11:08 +01:00
|
|
|
m_hoverTimer->stop();
|
2007-11-12 20:27:21 +01:00
|
|
|
|
2008-01-10 08:28:33 +01:00
|
|
|
if (m_applet) {
|
|
|
|
m_applet->removeSceneEventFilter(this);
|
|
|
|
}
|
|
|
|
|
2008-01-10 00:43:49 +01:00
|
|
|
if (anim == FadeIn) {
|
2007-11-12 20:27:21 +01:00
|
|
|
time *= 1.0-m_opacity;
|
|
|
|
} else {
|
2008-01-10 00:43:49 +01:00
|
|
|
m_hoverTimer->stop();
|
2007-11-12 20:27:21 +01:00
|
|
|
time *= m_opacity;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_anim = anim;
|
2007-12-21 04:10:35 +01:00
|
|
|
m_animId = Phase::self()->customAnimation(40, (int)time, Phase::EaseInOutCurve, this, "fadeAnimation");
|
2007-11-12 20:27:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void AppletHandle::forceDisappear()
|
|
|
|
{
|
|
|
|
setAcceptsHoverEvents(false);
|
|
|
|
startFading(FadeOut);
|
|
|
|
}
|
|
|
|
|
2007-11-23 09:01:29 +01:00
|
|
|
void AppletHandle::calculateSize()
|
|
|
|
{
|
|
|
|
m_rect = m_applet->boundingRect();
|
|
|
|
m_rect = m_applet->mapToParent(m_rect).boundingRect();
|
|
|
|
|
2007-12-21 04:10:35 +01:00
|
|
|
const int requiredHeight = (HANDLE_WIDTH * 2) + m_applet->hasConfigurationInterface() ?
|
|
|
|
((ICON_SIZE + ICON_MARGIN) * 4) :
|
|
|
|
((ICON_SIZE + ICON_MARGIN) * 3) +
|
|
|
|
ICON_MARGIN; // that last margin is blank space before the close button
|
2007-11-23 09:01:29 +01:00
|
|
|
if (m_rect.height() < requiredHeight) {
|
|
|
|
float delta = requiredHeight - m_rect.height();
|
|
|
|
delta = delta/2.0;
|
|
|
|
if (delta > 0.0) {
|
|
|
|
m_rect.adjust(0.0, -delta, 0.0, delta);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m_rect.adjust(-HANDLE_WIDTH, -HANDLE_WIDTH, HANDLE_WIDTH, HANDLE_WIDTH);
|
|
|
|
|
|
|
|
if (m_applet->pos().x() <= ((HANDLE_WIDTH * 2) + ICON_SIZE)) {
|
|
|
|
m_rect.adjust(0.0, 0.0, ICON_SIZE, 0.0);
|
|
|
|
m_buttonsOnRight = true;
|
|
|
|
} else {
|
|
|
|
m_rect.adjust(- ICON_SIZE, 0.0, 0.0, 0.0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-12 20:27:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#include "applethandle_p.moc"
|