Skip to content

Commit

Permalink
fixed image fit in qgraphicsview issues with initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
ponchio committed Feb 15, 2024
1 parent 2fe588a commit 459b231
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 23 deletions.
29 changes: 23 additions & 6 deletions relightlab/canvas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ void Canvas::gentle_zoom(double factor) {
if(currentScale * factor < min_scale)
factor = min_scale/currentScale;

if(currentScale *factor > max_scale)
factor = max_scale/currentScale;

scale(factor, factor);
centerOn(target_scene_pos);
QPointF delta_viewport_pos = target_viewport_pos - QPointF(viewport()->width() / 2.0,
Expand Down Expand Up @@ -85,19 +88,33 @@ bool Canvas::eventFilter(QObject *object, QEvent *event) {
return false;
}

void Canvas::fitInView(const QRectF &rect, Qt::AspectRatioMode aspectRadioMode) {
if(!resized) {
rect_fit = rect;
aspect_fit = aspectRadioMode;
needs_fit = true;
return;
}
QGraphicsView::fitInView(rect, aspectRadioMode);
}

void Canvas::resizeEvent(QResizeEvent *event) {
/*if(first_resize) {
first_resize = false;
return;
}*/

QSize old = event->oldSize();
QGraphicsView::resizeEvent(event);

if(!old.isValid()) {
if(needs_fit) {
QGraphicsView::fitInView(rect_fit, aspect_fit);
}
resized = true;
return;
}
//preservinca scale
double sx = event->size().width()/(double)event->oldSize().width();
double sy = event->size().height()/(double)event->oldSize().height();
double s = std::min(sx, sy);
qDebug() << event->oldSize() << " -> " << event->size();
scale(s, s);

}


Expand Down
12 changes: 10 additions & 2 deletions relightlab/canvas.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ class Canvas : public QGraphicsView {
void set_zoom_factor_base(double value);

double min_scale = 0.0f;
double max_scale = 0.0f;
double max_scale = 4.0f;
void fitInView(const QRectF &rect, Qt::AspectRatioMode aspectRadioMode = Qt::KeepAspectRatio);

protected:
void resizeEvent(QResizeEvent *event);

Expand All @@ -22,7 +24,13 @@ class Canvas : public QGraphicsView {
QPointF target_scene_pos, target_viewport_pos;
QPoint pressPosition;
double click_threshold = 2;
bool first_resize = true;

//this stuff is needed to call fitinview before the image is properly resized.
bool needs_fit = false;
bool resized = false;
QRectF rect_fit;
Qt::AspectRatioMode aspect_fit;

bool eventFilter(QObject* object, QEvent* event);

public slots:
Expand Down
12 changes: 2 additions & 10 deletions relightlab/imageframe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,16 +134,8 @@ void ImageFrame::showImage(int id) {
}

void ImageFrame::fit() {
Project &project = qRelightApp->project();
qDebug() << "Canvas: " << canvas->size() << " imgsize: " << project.imgsize;
//find smallest problems
double sx = double(canvas->width()) / project.imgsize.width();
double sy = double(canvas->height()) / project.imgsize.height();
double s = std::min(sx, sy);
double current_scale = canvas->transform().m11();
s = s/current_scale;
canvas->scale(s, s);

if(imagePixmap)
canvas->fitInView(imagePixmap->boundingRect());
}

void ImageFrame::one() {
Expand Down
1 change: 0 additions & 1 deletion relightlab/imageframe.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ public slots:

private:
QGraphicsPixmapItem *imagePixmap = nullptr;
bool first_resize = true;
};

#endif // IMAGEFRAME_H
1 change: 1 addition & 0 deletions relightlab/spheredialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
SphereDialog::SphereDialog(QWidget *parent): QDialog(parent) {
setModal(true);
sphere_picking = new SpherePicking;
sphere_picking->init();
QVBoxLayout *content = new QVBoxLayout(this);

QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
Expand Down
6 changes: 3 additions & 3 deletions relightlab/spherepicking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ void SpherePicking::updateBorderPoints() {
for(size_t i = 0; i < border.size(); i++) {
sphere->border[i] = border[i]->pos();
}
fit();
fitSphere();
}


Expand Down Expand Up @@ -246,7 +246,7 @@ void SpherePicking::deleteSelected(int currentImage) {
sphere->resetHighlight(currentImage);
showHighlight(currentImage);
}
fit();
fitSphere();
}

void SpherePicking::keyReleaseEvent(QKeyEvent *e) {
Expand All @@ -261,7 +261,7 @@ void SpherePicking::keyReleaseEvent(QKeyEvent *e) {
delete border.back();
border.pop_back();
sphere->border.pop_back();
fit();
fitSphere();
return;
}
break;
Expand Down
1 change: 0 additions & 1 deletion relightlab/verifyview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ VerifyView:: VerifyView(int _id, Sphere *_sphere, int _height, QWidget *parent):

void VerifyView::updateReflection() {
QPointF p = reflection->pos();
qDebug() << img_item->boundingRect()<< " P: " << p;
if(!img_item->boundingRect().contains(p)) {
reflection->setPos(sphere->inner.center() - sphere->inner.topLeft());
reflection->setPen(QPen(Qt::red, 2));
Expand Down

0 comments on commit 459b231

Please sign in to comment.