#include "mainwindow.h"
#include <QGraphicsScene>
#include <QHBoxLayout>
#include <QGraphicsView>
#include <QKeyEvent>
MainWindow
::MainWindow(QWidget *parent
){
scene->setSceneRect(-150, -150, 300, 300);
scene->setBackgroundBrush(Qt::white);
item
->setPos
(QPointF(-item
->boundingRect
().
width()/2,
-item
->boundingRect
().
height()/2));
scene->addItem(item);
view->setScene(scene);
view->setSceneRect(-150, -150, 300, 300);
view->setFocus();
layout->addWidget(view);
setLayout(layout);
rotateTimeLine
->setCurveShape
(QTimeLine::LinearCurve);
connect(rotateTimeLine, SIGNAL(valueChanged(qreal)),
this, SLOT(updateRotate(qreal)));
}
MainWindow::~MainWindow()
{
}
void MainWindow
::keyPressEvent(QKeyEvent *event
) {
qDebug("MainWindow::keyPressEvent");
switch (event->key()) {
case Qt::Key_Up:
rotate(0);
break;
case Qt::Key_Right:
rotate(1);
break;
case Qt::Key_Left:
rotate(2);
break;
case Qt::Key_Down:
rotate(3);
break;
}
}
void MainWindow::rotate(int axis)
{
rotateAxis = axis;
rotateAngle = 0;
rotateTimeLine->start();
}
void MainWindow::updateRotate(qreal value)
{
QTransform txf = QTransform();
rotateAngle = 360*value;
switch (rotateAxis)
{
case 0:
// Up arrow - Rotate along top axis
txf.rotate(rotateAngle, Qt::XAxis);
item->setTransformOriginPoint(item->boundingRect().width()/2, item->boundingRect().height()/2);
break;
case 1:
// Right arrow - Rotate along rigth axis
txf.rotate(rotateAngle, Qt::YAxis);
item->setTransformOriginPoint(item->boundingRect().width(), item->boundingRect().height()/2);
break;
case 2:
// Left arrow - Rotate along left axis
txf.rotate(rotateAngle, Qt::YAxis);
item->setTransformOriginPoint(0, item->boundingRect().height()/2);
break;
case 3:
// Down arrow - Rotate along bottom axis
txf.rotate(rotateAngle, Qt::XAxis);
item->setTransformOriginPoint(item->boundingRect().width()/2,item->boundingRect().height());
break;
}
item->setTransform(txf, false);
update();
}
#include "mainwindow.h"
#include <QGraphicsScene>
#include <QHBoxLayout>
#include <QGraphicsView>
#include <QKeyEvent>
MainWindow::MainWindow(QWidget *parent)
: QWidget(parent)
{
QGraphicsScene *scene = new QGraphicsScene();
scene->setSceneRect(-150, -150, 300, 300);
scene->setBackgroundBrush(Qt::white);
item = new QGraphicsPixmapItem(QPixmap(":/images/0.png"));
item->setPos(QPointF(-item->boundingRect().width()/2,-item->boundingRect().height()/2));
scene->addItem(item);
QGraphicsView *view = new QGraphicsView();
view->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
view->setScene(scene);
view->setSceneRect(-150, -150, 300, 300);
view->setFocus();
QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(view);
setLayout(layout);
rotateTimeLine = new QTimeLine(1500, this);
rotateTimeLine->setCurveShape(QTimeLine::LinearCurve);
connect(rotateTimeLine, SIGNAL(valueChanged(qreal)),
this, SLOT(updateRotate(qreal)));
}
MainWindow::~MainWindow()
{
}
void MainWindow::keyPressEvent(QKeyEvent *event)
{
qDebug("MainWindow::keyPressEvent");
switch (event->key()) {
case Qt::Key_Up:
rotate(0);
break;
case Qt::Key_Right:
rotate(1);
break;
case Qt::Key_Left:
rotate(2);
break;
case Qt::Key_Down:
rotate(3);
break;
}
}
void MainWindow::rotate(int axis)
{
rotateAxis = axis;
rotateAngle = 0;
rotateTimeLine->start();
}
void MainWindow::updateRotate(qreal value)
{
QTransform txf = QTransform();
rotateAngle = 360*value;
switch (rotateAxis)
{
case 0:
// Up arrow - Rotate along top axis
txf.rotate(rotateAngle, Qt::XAxis);
item->setTransformOriginPoint(item->boundingRect().width()/2, item->boundingRect().height()/2);
break;
case 1:
// Right arrow - Rotate along rigth axis
txf.rotate(rotateAngle, Qt::YAxis);
item->setTransformOriginPoint(item->boundingRect().width(), item->boundingRect().height()/2);
break;
case 2:
// Left arrow - Rotate along left axis
txf.rotate(rotateAngle, Qt::YAxis);
item->setTransformOriginPoint(0, item->boundingRect().height()/2);
break;
case 3:
// Down arrow - Rotate along bottom axis
txf.rotate(rotateAngle, Qt::XAxis);
item->setTransformOriginPoint(item->boundingRect().width()/2,item->boundingRect().height());
break;
}
item->setTransform(txf, false);
update();
}
To copy to clipboard, switch view to plain text mode
Bookmarks