Skip to content

Commit

Permalink
Hold viewrect center while flipping
Browse files Browse the repository at this point in the history
hombit authored and matwey committed Nov 13, 2017
1 parent 286e8ce commit ac4a8a7
Showing 2 changed files with 26 additions and 0 deletions.
2 changes: 2 additions & 0 deletions include/openglwidget.h
Original file line number Diff line number Diff line change
@@ -143,6 +143,8 @@ private slots:
private:
OpenGLTransform opengl_transform_;
WidgetToFitsOpenGLTransform widget_to_fits_;
protected:
void flipViewrect(Qt::Axis flip_axis);
public:
inline double rotation() const { return opengl_transform_.rotation(); }
inline const bool& horizontalFlip() const { return opengl_transform_.horizontalFlip(); }
24 changes: 24 additions & 0 deletions src/openglwidget.cpp
Original file line number Diff line number Diff line change
@@ -340,9 +340,31 @@ void OpenGLWidget::setRotation(double angle) {
emit rotationChanged(rotation());
}

void OpenGLWidget::flipViewrect(Qt::Axis flip_axis) {
QMatrix4x4 rotation_matrix;
rotation_matrix.rotate(-rotation(), 0, 0, 1);
auto unrotated_view_center = rotation_matrix.transposed().map(viewrect_.view().center());
switch (flip_axis) {
case Qt::XAxis:
unrotated_view_center.setX(-unrotated_view_center.x());
break;
case Qt::YAxis:
unrotated_view_center.setY(-unrotated_view_center.y());
break;
default:
Q_ASSERT(false);
}
const auto view_center = rotation_matrix.map(unrotated_view_center);
auto new_view = viewrect_.view();
new_view.moveCenter(view_center);
viewrect_.setView(new_view);
}

void OpenGLWidget::setHorizontalFlip(bool flip) {
if (horizontalFlip() == flip) return;

flipViewrect(Qt::XAxis);

opengl_transform_.setHorizontalFlip(flip);
widget_to_fits_.setHorizontalFlip(flip);

@@ -352,6 +374,8 @@ void OpenGLWidget::setHorizontalFlip(bool flip) {
void OpenGLWidget::setVerticalFlip(bool flip) {
if (verticalFlip() == flip) return;

flipViewrect(Qt::YAxis);

opengl_transform_.setVerticalFlip(flip);
widget_to_fits_.setVerticalFlip(flip);

0 comments on commit ac4a8a7

Please sign in to comment.