Simplify some math

svn path=/trunk/KDE/kdelibs/; revision=1059914
This commit is contained in:
Ambroz Bizjak 2009-12-07 16:54:25 +00:00
parent 0e503cbc5f
commit 07fcab757c

View File

@ -27,6 +27,8 @@
#include <QtGui/QApplication> #include <QtGui/QApplication>
#include <QtGui/QMenu> #include <QtGui/QMenu>
#include <QTouchEvent> #include <QTouchEvent>
#include <QMatrix>
#include <QTransform>
#include <kcolorscheme.h> #include <kcolorscheme.h>
#include <kglobalsettings.h> #include <kglobalsettings.h>
@ -50,8 +52,9 @@ namespace Plasma
{ {
qreal _k_distanceForPoint(QPointF point); qreal _k_distanceForPoint(QPointF point);
qreal _k_pointAngle(QPointF in); qreal _k_pointAngle(QPointF point);
QPointF _k_rotatePoint(QPointF in, qreal rotateAngle); QPointF _k_rotatePoint(QPointF point, qreal angle);
QPointF _k_projectPoint(QPointF point, QPointF v);
AppletHandle::AppletHandle(Containment *parent, Applet *applet, const QPointF &hoverPos) AppletHandle::AppletHandle(Containment *parent, Applet *applet, const QPointF &hoverPos)
: QGraphicsObject(applet), : QGraphicsObject(applet),
@ -558,24 +561,30 @@ qreal _k_distanceForPoint(QPointF point)
return std::sqrt(point.x() * point.x() + point.y() * point.y()); return std::sqrt(point.x() * point.x() + point.y() * point.y());
} }
qreal _k_pointAngle(QPointF in) qreal _k_pointAngle(QPointF point)
{ {
qreal r = sqrt(in.x()*in.x() + in.y()*in.y()); qreal r = sqrt(point.x() * point.x() + point.y() * point.y());
qreal cosine = in.x()/r; qreal cosine = point.x() / r;
qreal sine = in.y()/r;
if (sine >= 0) { if (point.y() >= 0) {
return acos(cosine); return acos(cosine);
} else { } else {
return -acos(cosine); return -acos(cosine);
} }
} }
QPointF _k_rotatePoint(QPointF in, qreal rotateAngle) QPointF _k_rotatePoint(QPointF point, qreal angle)
{ {
QTransform trans; return QTransform().rotateRadians(angle).map(point);
trans.rotateRadians(rotateAngle); }
return trans.map(in);
QPointF _k_projectPoint(QPointF point, QPointF v)
{
v /= sqrt(v.x() * v.x() + v.y() * v.y());
qreal a = v.x() * v.x();
qreal b = v.x() * v.y();
qreal d = v.y() * v.y();
return QMatrix(a, b, b, d, 0., 0.).map(point);
} }
void AppletHandle::mouseMoveEvent(QGraphicsSceneMouseEvent *event) void AppletHandle::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
@ -662,26 +671,19 @@ void AppletHandle::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
newSize = m_origAppletSize + QPointF(rGrabPoint.x() - rCursorPoint.x(), rGrabPoint.y() - rCursorPoint.y()); newSize = m_origAppletSize + QPointF(rGrabPoint.x() - rCursorPoint.x(), rGrabPoint.y() - rCursorPoint.y());
} }
// if preserving aspect ratio, project the calculated size point to the line // preserving aspect ratio?
// theough the origin and the original size point
if ((m_applet->aspectRatioMode() != Plasma::IgnoreAspectRatio && if ((m_applet->aspectRatioMode() != Plasma::IgnoreAspectRatio &&
!(event->modifiers() & Qt::ControlModifier)) || !(event->modifiers() & Qt::ControlModifier)) ||
(m_applet->aspectRatioMode() == Plasma::IgnoreAspectRatio && (m_applet->aspectRatioMode() == Plasma::IgnoreAspectRatio &&
(event->modifiers() & Qt::ControlModifier))) { (event->modifiers() & Qt::ControlModifier))) {
qreal ox = m_origAppletSize.x(); // project size to keep ratio
qreal oy = m_origAppletSize.y(); newSize = _k_projectPoint(newSize, m_origAppletSize);
qreal sx = newSize.x(); // limit size, presering ratio
qreal sy = newSize.y(); qreal ratio = m_origAppletSize.y() / m_origAppletSize.x();
qreal x = ox*(sx*ox+sy*oy)/(ox*ox+oy*oy);
qreal y = (oy/ox)*x;
newSize = QPointF(x, y);
// limit size, preserve ratio
newSize.rx() = qMin(max.width(), qMax(min.width(), newSize.x())); newSize.rx() = qMin(max.width(), qMax(min.width(), newSize.x()));
newSize.ry() = newSize.x()*(oy/ox); newSize.ry() = newSize.x() * ratio;
newSize.ry() = qMin(max.height(), qMax(min.height(), newSize.y())); newSize.ry() = qMin(max.height(), qMax(min.height(), newSize.y()));
newSize.rx() = newSize.y()/(oy/ox); newSize.rx() = newSize.y() / ratio;
} else { } else {
// limit size // limit size
newSize.rx() = qMin(max.width(), qMax(min.width(), newSize.x())); newSize.rx() = qMin(max.width(), qMax(min.width(), newSize.x()));