Skip to content

Commit

Permalink
restructing and proper zooming!
Browse files Browse the repository at this point in the history
  • Loading branch information
jurplel committed Mar 5, 2018
1 parent cf4b311 commit de7e56d
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 10 deletions.
9 changes: 4 additions & 5 deletions mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ void MainWindow::resizeEvent(QResizeEvent *event)
QMainWindow::resizeEvent(event);
if(isPixmapLoaded)
{
ui->graphicsView->fitInView(loadedPixmapItem->boundingRect(), Qt::KeepAspectRatio);
ui->graphicsView->resetScale(loadedPixmapItem);
}
}

void MainWindow::PickFile()
void MainWindow::pickFile()
{
QString fileName = QFileDialog::getOpenFileName(this,
tr("Open"), "",
Expand All @@ -57,14 +57,13 @@ void MainWindow::PickFile()
scene->clear();
loadedPixmapItem = scene->addPixmap(loadedPixmap);
loadedPixmapItem->setOffset((50000.0 - loadedPixmap.width()/2), (50000.0 - loadedPixmap.height()/2));
ui->graphicsView->fitInView(loadedPixmapItem->boundingRect(), Qt::KeepAspectRatio);
ui->graphicsView->resetScale(loadedPixmapItem);
loadedPixmapItem->setTransformationMode(Qt::SmoothTransformation);
isPixmapLoaded = true;
}

void MainWindow::on_actionOpen_triggered()
{
PickFile();
pickFile();
}

void MainWindow::on_actionAbout_Qt_triggered()
Expand Down
2 changes: 1 addition & 1 deletion mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ private slots:
private:
Ui::MainWindow *ui;

void PickFile();
void pickFile();

QGraphicsScene *scene;
QPixmap loadedPixmap;
Expand Down
33 changes: 30 additions & 3 deletions qvgraphicsview.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "qvgraphicsview.h"
#include <QDebug>
#include <QWheelEvent>
#include <QGraphicsPixmapItem>

QVGraphicsView::QVGraphicsView(QWidget *parent) : QGraphicsView(parent)
{
Expand All @@ -9,18 +10,44 @@ QVGraphicsView::QVGraphicsView(QWidget *parent) : QGraphicsView(parent)

void QVGraphicsView::wheelEvent(QWheelEvent *event)
{
qDebug() << event->angleDelta();
qDebug() << getCurrentScale();

int DeltaY = event->angleDelta().y();
QPoint pos = event->pos();

if (getCurrentScale() >= 1.0)
{
setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
}
else
{
setTransformationAnchor(QGraphicsView::AnchorViewCenter);
centerOn(scene()->height()/2, scene()->width()/2);
}

if (DeltaY > 0)
{
scale(1.1, 1.1);
setCurrentScale(getCurrentScale()*1.1);
}
else
{
scale(0.9, 0.9);
setCurrentScale(getCurrentScale()*0.9);
}
centerOn(scene()->height()/2, scene()->width()/2);
}

void QVGraphicsView::resetScale(QGraphicsPixmapItem* affectedItem)
{
fitInView(affectedItem->boundingRect(), Qt::KeepAspectRatio);
setCurrentScale(1.0);
}

void QVGraphicsView::setCurrentScale(qreal newCurrentScale)
{
currentScale = newCurrentScale;
}

qreal QVGraphicsView::getCurrentScale()
{
return currentScale;
}
9 changes: 8 additions & 1 deletion qvgraphicsview.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,15 @@ class QVGraphicsView : public QGraphicsView
public:
QVGraphicsView(QWidget *parent = nullptr);

void resetScale(QGraphicsPixmapItem* affectedItem);

void setCurrentScale(qreal newScale);
qreal getCurrentScale();

protected:
virtual void wheelEvent(QWheelEvent *event);
};

private:
qreal currentScale;
};
#endif // QVGRAPHICSVIEW_H

0 comments on commit de7e56d

Please sign in to comment.