Commit 8b77baaa authored by Stefan Majewsky's avatar Stefan Majewsky

Add ScrollViewportInteractor.

svn path=/trunk/KDE/kdegames/palapeli/; revision=1118501
parent 0f3846ef
......@@ -20,7 +20,6 @@
#include "piece.h"
#include "view.h"
#include <QScrollBar>
#include <QStyle>
#include <QStyleOptionRubberBand>
#include <KLocalizedString>
......@@ -155,18 +154,17 @@ bool Palapeli::MoveViewportInteractor::startInteraction(const Palapeli::MouseEve
void Palapeli::MoveViewportInteractor::continueInteraction(const Palapeli::MouseEvent& event)
{
QGraphicsView* v = view();
const QPointF delta = event.pos - m_lastPos;
Palapeli::View* view = qobject_cast<Palapeli::View*>(this->view());
if (view)
view->moveViewportBy(event.pos - m_lastPos);
m_lastPos = event.pos;
v->horizontalScrollBar()->setValue(v->horizontalScrollBar()->value() + (v->isRightToLeft() ? delta.x() : -delta.x()));
v->verticalScrollBar()->setValue(v->verticalScrollBar()->value() - delta.y());
}
//END Palapeli::MoveViewportInteractor
//BEGIN Palapeli::ZoomViewportInteractor
Palapeli::ZoomViewportInteractor::ZoomViewportInteractor(QGraphicsView* view)
: Palapeli::Interactor(0, Palapeli::WheelInteractor, view) //priority: undecided ATM
: Palapeli::Interactor(0, Palapeli::WheelInteractor, view) //priority: unused for wheel interactors ATM
{
setMetadata(ViewportInteraction, i18nc("Description (used like a name) for a mouse interaction method", "Zoom viewport"), QIcon());
}
......@@ -179,6 +177,29 @@ void Palapeli::ZoomViewportInteractor::doInteraction(const Palapeli::WheelEvent&
}
//END Palapeli::ZoomViewportInteractor
//BEGIN Palapeli::ScrollViewportInteractor
Palapeli::ScrollViewportInteractor::ScrollViewportInteractor(Qt::Orientation orientation, QGraphicsView* view)
: Palapeli::Interactor(0, Palapeli::WheelInteractor, view) //priority: unused for wheel interactors ATM
, m_orientation(orientation)
{
QString description;
if (orientation == Qt::Horizontal)
description = i18nc("Description (used like a name) for a mouse interaction method", "Scroll viewport horizontally");
else
description = i18nc("Description (used like a name) for a mouse interaction method", "Scroll viewport vertically");
setMetadata(ViewportInteraction, description, QIcon());
}
void Palapeli::ScrollViewportInteractor::doInteraction(const Palapeli::WheelEvent& event)
{
const QPoint widgetDelta = (m_orientation == Qt::Horizontal) ? QPoint(event.delta, 0) : QPoint(0, event.delta);
Palapeli::View* view = qobject_cast<Palapeli::View*>(this->view());
if (view)
view->moveViewportBy(view->mapToScene(widgetDelta)- view->mapToScene(QPoint()));
}
//END Palapeli::ScrollViewportInteractor
//BEGIN Palapeli::RubberBandItem
Palapeli::RubberBandItem::RubberBandItem(QGraphicsItem* parent)
......
......@@ -81,6 +81,18 @@ namespace Palapeli
virtual void doInteraction(const Palapeli::WheelEvent& event);
};
//This interactor is assigned to nothing by default.
//Turning the wheel will scroll the viewport either horizontally or vertically.
class ScrollViewportInteractor : public Palapeli::Interactor
{
public:
ScrollViewportInteractor(Qt::Orientation orientation, QGraphicsView* view);
protected:
virtual void doInteraction(const Palapeli::WheelEvent& event);
private:
Qt::Orientation m_orientation;
};
class RubberBandItem : public QGraphicsItem
{
public:
......
......@@ -40,6 +40,8 @@ QMap<QByteArray, Palapeli::Interactor*> Palapeli::TriggerMapper::createInteracto
result["SelectPiece"] = new Palapeli::SelectPieceInteractor(view);
result["MoveViewport"] = new Palapeli::MoveViewportInteractor(view);
result["ZoomViewport"] = new Palapeli::ZoomViewportInteractor(view);
result["ScrollViewportHoriz"] = new Palapeli::ScrollViewportInteractor(Qt::Horizontal, view);
result["ScrollViewportVert"] = new Palapeli::ScrollViewportInteractor(Qt::Vertical, view);
result["RubberBand"] = new Palapeli::RubberBandInteractor(view);
result["Constraints"] = new Palapeli::ConstraintInteractor(view);
return result;
......
......@@ -24,6 +24,7 @@
#include <cmath>
#include <QMouseEvent>
#include <QPropertyAnimation>
#include <QScrollBar>
#include <KLocalizedString>
#include <KMessageBox>
......@@ -118,6 +119,12 @@ void Palapeli::View::wheelEvent(QWheelEvent* event)
//We do intentionally *not* propagate to QGV::wheelEvent.
}
void Palapeli::View::moveViewportBy(const QPointF& sceneDelta)
{
horizontalScrollBar()->setValue(horizontalScrollBar()->value() + (isRightToLeft() ? sceneDelta.x() : -sceneDelta.x()));
verticalScrollBar()->setValue(verticalScrollBar()->value() - sceneDelta.y());
}
void Palapeli::View::zoomBy(int delta)
{
zoomTo(m_zoomLevel + delta / 10);
......
......@@ -45,6 +45,7 @@ namespace Palapeli
public Q_SLOTS:
void setScene(Palapeli::Scene* scene);
void moveViewportBy(const QPointF& sceneDelta);
void zoomIn();
void zoomOut();
void zoomBy(int delta); //delta = 0 -> no change, delta < 0 -> zoom out, delta > 0 -> zoom in
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment