metRo_
23rd August 2010, 17:59
Hi, i want to scale an image and i already did it with help of it: http://wiki.forum.nokia.com/index.php/CS001560_-_Animating_graphics_item_scaling but the scaling is made from point(0,0) and i want that the scaling is made from the center of the image.
I try it for the last hours without success (i'm despairing :s)
I attached the project and the code are below:
//main.cpp
#include <QtGui/QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.setFixedSize(240,320);
//w.setMinimumSize(240,320);
w.show();
return a.exec();
}
//mainwindow.cpp
#include "mainwindow.h"
#include <QDebug>
#include <QAction>
#include <QFile>
bool zoomIn=false;
PictureItem::PictureItem(QSizeF size, const QPixmap& pixmap, QObject* parent) :
QObject(parent), QGraphicsPixmapItem(pixmap)
{
m_size = size;
setCacheMode(DeviceCoordinateCache);
}
PictureItem::~PictureItem()
{
}
QRectF PictureItem::boundingRect() const
{
return QRectF(0, 0, m_size.width(), m_size.height());
}
void PictureItem::animatePosition(const QPointF& start, const QPointF& end)
{
// Start animate this class
QPropertyAnimation* anim = new QPropertyAnimation(this, "pos");
// 2 second duration animation
anim->setDuration(500);
// position to start animation
anim->setStartValue(start);
// end position of animation
anim->setEndValue(end);
// easing curve
anim->setEasingCurve(QEasingCurve::OutQuad);
// Listen animation finished signal
QObject::connect(anim, SIGNAL(finished()), this, SLOT(animationFinished()));
// Start animation and delete QPropertyAnimation class on the end
anim->start(QAbstractAnimation::DeleteWhenStopped);
}
void PictureItem::animateScale(qreal start, qreal end)
{
// Start animate this class
QPropertyAnimation* anim = new QPropertyAnimation(this, "scale");
anim->setDuration(1000);
anim->setStartValue(start);
anim->setEndValue(end);
anim->setEasingCurve(QEasingCurve::InOutBack);
QObject::connect(anim, SIGNAL(finished()), this, SLOT(animationFinished()));
anim->start(QAbstractAnimation::DeleteWhenStopped);
}
void PictureItem::animationFinished()
{
qDebug()<<"Animation Finished!";
}
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
// Create QGraphicsScene and QGraphicsView
m_scene = new QGraphicsScene(this);
m_view = new QGraphicsView(m_scene,this);
m_view->setCacheMode(QGraphicsView::CacheBackground);
m_view->setViewportUpdateMode(QGraphicsView::BoundingRectV iewportUpdate);
m_view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOf f);
m_view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff) ;
// Create PictureItem for the animation
QPixmap pixmap(":/qt1.jpg");
m_item = new PictureItem(pixmap.size(),pixmap,this);
// Add PictureItem to center of the scene
m_scene->addItem(m_item);
m_item->setScale(0.5);
createMenu();
this->setCentralWidget(m_view);
}
MainWindow::~MainWindow()
{
}
void MainWindow::resizeEvent(QResizeEvent *)
{
// Update scene rect
m_scene->setSceneRect(m_view->rect());
// Calculate center point for the picture
m_center = QPointF((rect().width()-m_item->boundingRect().width()*m_item->scale())/2,
(rect().height()-m_item->boundingRect().height()*m_item->scale())/2 );
// Set picture to center of the sceen
m_item->setPos(m_center);
}
void MainWindow::mousePressEvent(QMouseEvent *)
{
// Set picture to center of the sceen
m_item->setPos(m_center);
m_item->setScale(0.5);
}
void MainWindow::createMenu()
{
QMenu* menu = this->menuBar()->addMenu("Animate");
menu->addAction("Position", this, SLOT(animatePos()));
menu->addAction("Scale", this, SLOT(animateScale()));
}
void MainWindow::animatePos()
{
// Animate picture position
m_item->animatePosition(m_item->pos(),QPoint(0-this->rect().size().width(),0));
}
void MainWindow::animateScale()
{
m_item->animateScale(1.0,2.0);
}
//mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QtGui>
class PictureItem: public QObject, public QGraphicsPixmapItem
{
Q_OBJECT
// For Property animation:
// Change picture position
Q_PROPERTY(QPointF pos READ pos WRITE setPos)
// Scale picture
Q_PROPERTY(qreal scale READ scale WRITE setScale)
public:
enum
{
PictureItemType = UserType + 1
};
public:
PictureItem(QSizeF size, const QPixmap& pixmap = 0, QObject* parent = 0);
~PictureItem();
QRectF boundingRect() const;
int type() const
{
return PictureItemType;
}
// Animate position of this class
void animatePosition(const QPointF& start, const QPointF& end);
// Animate scale for this class
void animateScale(qreal start, qreal end);
public slots:
void animationFinished();
private:
QSizeF m_size;
};
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
void resizeEvent(QResizeEvent *);
void mousePressEvent(QMouseEvent *);
void createMenu();
public slots:
void animatePos();
void animateScale();
private:
QGraphicsScene* m_scene;
QGraphicsView* m_view;
PictureItem* m_item;
QPointF m_center;
};
#endif // MAINWINDOW_H
5095
I try it for the last hours without success (i'm despairing :s)
I attached the project and the code are below:
//main.cpp
#include <QtGui/QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.setFixedSize(240,320);
//w.setMinimumSize(240,320);
w.show();
return a.exec();
}
//mainwindow.cpp
#include "mainwindow.h"
#include <QDebug>
#include <QAction>
#include <QFile>
bool zoomIn=false;
PictureItem::PictureItem(QSizeF size, const QPixmap& pixmap, QObject* parent) :
QObject(parent), QGraphicsPixmapItem(pixmap)
{
m_size = size;
setCacheMode(DeviceCoordinateCache);
}
PictureItem::~PictureItem()
{
}
QRectF PictureItem::boundingRect() const
{
return QRectF(0, 0, m_size.width(), m_size.height());
}
void PictureItem::animatePosition(const QPointF& start, const QPointF& end)
{
// Start animate this class
QPropertyAnimation* anim = new QPropertyAnimation(this, "pos");
// 2 second duration animation
anim->setDuration(500);
// position to start animation
anim->setStartValue(start);
// end position of animation
anim->setEndValue(end);
// easing curve
anim->setEasingCurve(QEasingCurve::OutQuad);
// Listen animation finished signal
QObject::connect(anim, SIGNAL(finished()), this, SLOT(animationFinished()));
// Start animation and delete QPropertyAnimation class on the end
anim->start(QAbstractAnimation::DeleteWhenStopped);
}
void PictureItem::animateScale(qreal start, qreal end)
{
// Start animate this class
QPropertyAnimation* anim = new QPropertyAnimation(this, "scale");
anim->setDuration(1000);
anim->setStartValue(start);
anim->setEndValue(end);
anim->setEasingCurve(QEasingCurve::InOutBack);
QObject::connect(anim, SIGNAL(finished()), this, SLOT(animationFinished()));
anim->start(QAbstractAnimation::DeleteWhenStopped);
}
void PictureItem::animationFinished()
{
qDebug()<<"Animation Finished!";
}
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
// Create QGraphicsScene and QGraphicsView
m_scene = new QGraphicsScene(this);
m_view = new QGraphicsView(m_scene,this);
m_view->setCacheMode(QGraphicsView::CacheBackground);
m_view->setViewportUpdateMode(QGraphicsView::BoundingRectV iewportUpdate);
m_view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOf f);
m_view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff) ;
// Create PictureItem for the animation
QPixmap pixmap(":/qt1.jpg");
m_item = new PictureItem(pixmap.size(),pixmap,this);
// Add PictureItem to center of the scene
m_scene->addItem(m_item);
m_item->setScale(0.5);
createMenu();
this->setCentralWidget(m_view);
}
MainWindow::~MainWindow()
{
}
void MainWindow::resizeEvent(QResizeEvent *)
{
// Update scene rect
m_scene->setSceneRect(m_view->rect());
// Calculate center point for the picture
m_center = QPointF((rect().width()-m_item->boundingRect().width()*m_item->scale())/2,
(rect().height()-m_item->boundingRect().height()*m_item->scale())/2 );
// Set picture to center of the sceen
m_item->setPos(m_center);
}
void MainWindow::mousePressEvent(QMouseEvent *)
{
// Set picture to center of the sceen
m_item->setPos(m_center);
m_item->setScale(0.5);
}
void MainWindow::createMenu()
{
QMenu* menu = this->menuBar()->addMenu("Animate");
menu->addAction("Position", this, SLOT(animatePos()));
menu->addAction("Scale", this, SLOT(animateScale()));
}
void MainWindow::animatePos()
{
// Animate picture position
m_item->animatePosition(m_item->pos(),QPoint(0-this->rect().size().width(),0));
}
void MainWindow::animateScale()
{
m_item->animateScale(1.0,2.0);
}
//mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QtGui>
class PictureItem: public QObject, public QGraphicsPixmapItem
{
Q_OBJECT
// For Property animation:
// Change picture position
Q_PROPERTY(QPointF pos READ pos WRITE setPos)
// Scale picture
Q_PROPERTY(qreal scale READ scale WRITE setScale)
public:
enum
{
PictureItemType = UserType + 1
};
public:
PictureItem(QSizeF size, const QPixmap& pixmap = 0, QObject* parent = 0);
~PictureItem();
QRectF boundingRect() const;
int type() const
{
return PictureItemType;
}
// Animate position of this class
void animatePosition(const QPointF& start, const QPointF& end);
// Animate scale for this class
void animateScale(qreal start, qreal end);
public slots:
void animationFinished();
private:
QSizeF m_size;
};
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
void resizeEvent(QResizeEvent *);
void mousePressEvent(QMouseEvent *);
void createMenu();
public slots:
void animatePos();
void animateScale();
private:
QGraphicsScene* m_scene;
QGraphicsView* m_view;
PictureItem* m_item;
QPointF m_center;
};
#endif // MAINWINDOW_H
5095