Fixed Issue #3.

--HG--
branch : develop
This commit is contained in:
dismine 2013-10-14 11:40:01 +03:00
parent 1e70aea0db
commit 244756cb06
2 changed files with 40 additions and 28 deletions

View File

@ -23,30 +23,50 @@
#include <QApplication> #include <QApplication>
#include <QWheelEvent> #include <QWheelEvent>
#include <QScrollBar> #include <QScrollBar>
#include <QTimeLine>
VMainGraphicsView::VMainGraphicsView(QWidget *parent) : VMainGraphicsView::VMainGraphicsView(QWidget *parent) :
QGraphicsView(parent){ QGraphicsView(parent), _numScheduledScalings(0){
QGraphicsView::setResizeAnchor(QGraphicsView::AnchorUnderMouse); QGraphicsView::setResizeAnchor(QGraphicsView::AnchorUnderMouse);
setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform); setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
} }
void VMainGraphicsView::wheelEvent(QWheelEvent *event){ void VMainGraphicsView::wheelEvent(QWheelEvent *event){
// If you press CTRL this code will execute
int numDegrees = event->delta() / 8;
int numSteps = numDegrees / 15; // see QWheelEvent documentation
_numScheduledScalings += numSteps;
if (_numScheduledScalings * numSteps < 0){ // if user moved the wheel in another direction, we reset
_numScheduledScalings = numSteps; // previously scheduled scalings
}
QTimeLine *anim = new QTimeLine(350, this);
anim->setUpdateInterval(20);
connect(anim, &QTimeLine::valueChanged, this, &VMainGraphicsView::scalingTime);
connect(anim, &QTimeLine::finished, this, &VMainGraphicsView::animFinished);
anim->start();
}
void VMainGraphicsView::scalingTime(qreal x){
Q_UNUSED(x);
qreal factor = 1.0 + qreal(_numScheduledScalings) / 300.0;
if (QApplication::keyboardModifiers() == Qt::ControlModifier){ if (QApplication::keyboardModifiers() == Qt::ControlModifier){
// Если нажата клавиша CTRL этот код выполнится scale(factor, factor);
if ((event->delta())>0){
ZoomIn();
} else if ((event->delta())<0){
ZoomOut();
}
} else { } else {
verticalScrollBar()->setValue(verticalScrollBar()->value() - event->delta()); if(_numScheduledScalings < 0){
verticalScrollBar()->setValue(qRound(verticalScrollBar()->value() + factor*3.5));
} else {
verticalScrollBar()->setValue(qRound(verticalScrollBar()->value() - factor*3.5));
}
} }
} }
void VMainGraphicsView::ZoomIn(){ void VMainGraphicsView::animFinished(){
scale(1.1,1.1); if (_numScheduledScalings > 0){
} _numScheduledScalings--;
} else {
void VMainGraphicsView::ZoomOut(){ _numScheduledScalings++;
scale(1/1.1,1/1.1); }
sender()->~QObject();
} }

View File

@ -24,12 +24,10 @@
#include <QGraphicsView> #include <QGraphicsView>
class VMainGraphicsView : public QGraphicsView class VMainGraphicsView : public QGraphicsView{
{
Q_OBJECT Q_OBJECT
public: public:
explicit VMainGraphicsView(QWidget *parent = 0); explicit VMainGraphicsView(QWidget *parent = 0);
signals: signals:
protected: protected:
/** /**
@ -37,17 +35,11 @@ protected:
* @param event передається подія. * @param event передається подія.
*/ */
void wheelEvent ( QWheelEvent * event ); void wheelEvent ( QWheelEvent * event );
public slots: public slots:
/** void scalingTime(qreal x);
* @brief ZoomIn збільшує масштаб листа. void animFinished();
*/ private:
void ZoomIn(); qint32 _numScheduledScalings;
/**
* @brief ZoomOut зменшує масштаб листа.
*/
void ZoomOut();
}; };
#endif // VMAINGRAPHICSVIEW_H #endif // VMAINGRAPHICSVIEW_H