1
0

fix bugs within scale and rotate

This commit is contained in:
Gary Wang
2019-10-02 16:04:50 +08:00
parent ad879f8f8b
commit f47ee03ce3
3 changed files with 74 additions and 10 deletions

View File

@@ -84,6 +84,48 @@ void GraphicsView::setScene(GraphicsScene *scene)
return QGraphicsView::setScene(scene);
}
qreal GraphicsView::scaleFactor() const
{
int angle = static_cast<int>(m_rotateAngle);
if (angle == 0 || angle == 180) {
return qAbs(transform().m11());
} else {
return qAbs(transform().m12());
}
}
void GraphicsView::resetTransform()
{
m_scaleFactor = 1;
m_rotateAngle = 0;
QGraphicsView::resetTransform();
}
void GraphicsView::zoomView(qreal scaleFactor)
{
m_scaleFactor *= scaleFactor;
reapplyViewTransform();
}
void GraphicsView::resetScale()
{
m_scaleFactor = 1;
reapplyViewTransform();
}
void GraphicsView::rotateView(qreal rotateAngel)
{
m_rotateAngle += rotateAngel;
m_rotateAngle = static_cast<int>(m_rotateAngle) % 360;
reapplyViewTransform();
}
void GraphicsView::fitInView(const QRectF &rect, Qt::AspectRatioMode aspectRadioMode)
{
QGraphicsView::fitInView(rect, aspectRadioMode);
m_scaleFactor = scaleFactor();
}
void GraphicsView::checkAndDoFitInView()
{
if (!isThingSmallerThanWindowWith(transform())) {
@@ -132,16 +174,18 @@ void GraphicsView::wheelEvent(QWheelEvent *event)
{
m_enableFitInView = false;
if (event->delta() > 0) {
scale(1.25, 1.25);
zoomView(1.25);
} else {
scale(0.8, 0.8);
zoomView(0.8);
}
}
void GraphicsView::resizeEvent(QResizeEvent *event)
{
if (m_enableFitInView) {
if (isThingSmallerThanWindowWith(QTransform()) && transform().m11() >= 1) {
QTransform tf;
tf.rotate(m_rotateAngle);
if (isThingSmallerThanWindowWith(tf) && scaleFactor() >= 1) {
// no longer need to do fitInView()
// but we leave the m_enableFitInView value unchanged in case
// user resize down the window again.
@@ -197,7 +241,7 @@ void GraphicsView::dropEvent(QDropEvent *event)
}
bool GraphicsView::isThingSmallerThanWindowWith(const QTransform &transform) const
{qDebug() << sceneRect();
{
return rect().size().expandedTo(transform.mapRect(sceneRect()).size().toSize())
== rect().size();
}
@@ -238,3 +282,10 @@ void GraphicsView::setCheckerboardEnabled(bool enabled)
setBackgroundBrush(Qt::transparent);
}
}
void GraphicsView::reapplyViewTransform()
{
QGraphicsView::resetTransform();
scale(m_scaleFactor, m_scaleFactor);
rotate(m_rotateAngle);
}