Skip to content

Commit

Permalink
fix zooming
Browse files Browse the repository at this point in the history
  • Loading branch information
zergud committed Oct 19, 2023
1 parent 51ace3a commit 77f50c2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 30 deletions.
55 changes: 25 additions & 30 deletions qucs/schematic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ Schematic::Schematic(QucsApp *App_, const QString& Name_)
UsedX1 = UsedY1 = INT_MAX;
UsedX2 = UsedY2 = INT_MIN;

zx1 = zy1 = zx2 = zy2 = dx = dy = 0;

tmpPosX = tmpPosY = -100;
tmpUsedX1 = tmpUsedY1 = tmpViewX1 = tmpViewY1 = -200;
tmpUsedX2 = tmpUsedY2 = tmpViewX2 = tmpViewY2 = 200;
Expand Down Expand Up @@ -795,45 +797,39 @@ void Schematic::showAll()
}

// ------------------------------------------------------
void Schematic::zoomToSelection()
{
void Schematic::zoomToSelection() {
int x1, x2, y1, y2 = 0;
sizeOfSelection(x1, y1, x2, y2);
if (x1 == 0 && x2 == 0 && y1 == 0 && y2 == 0) {
showAll();
return;
}
//
if (zx1 == contentsX() && zx2 == contentsWidth() &&
zy1 == contentsY() && zy2 == contentsHeight() &&
dx == x2 - x1 &&
dy == y2 - y1) {
return;
}

float initialScale = Scale;
float scale = 1;
float xShift = 0;
float yShift = 0;

float ax1 = (x2 - ViewX1) * initialScale;
float ay1 = (y2 - ViewY1) * initialScale;
float DX = float(x2 - x1);
float DY = float(y2 - y1);

if((Scale * DX) < 6.0) {
// a simple click zooms by constant factor
scale = zoom(1.5)/initialScale;
dx = x2 - x1;
dy = y2 - y1;

xShift = scale * x2;
yShift = scale * x2;
} else {
float xScale = float(visibleWidth()) / std::abs(DX);
float yScale = float(visibleHeight()) / std::abs(DY);
scale = qMin(xScale, yScale)/initialScale;
scale = zoom(scale)/initialScale;
float xScale = float(visibleWidth()) / std::abs(dx + 80);
float yScale = float(visibleHeight()) / std::abs(dy + 80);
float scale = qMin(xScale, yScale) / Scale;
zoom(scale);

xShift = scale * (ax1 - 0.5*DX);
yShift = scale * (ay1 - 0.5*DY);
}
xShift -= (0.5*visibleWidth() + contentsX());
yShift -= (0.5*visibleHeight() + contentsY());
scrollBy(xShift, yShift);
ViewX1 = x1 - 40;
ViewY1 = y1 - 40;
ViewX2 = x2 + 40;
ViewY2 = y2 + 40;
zx1 = contentsX();
zy1 = contentsY();
zx2 = contentsWidth();
zy2 = contentsHeight();

releaseKeyboard(); // allow keyboard inputs again
//releaseKeyboard(); // allow keyboard inputs again
}

// ---------------------------------------------------
Expand Down Expand Up @@ -2071,7 +2067,6 @@ void Schematic::contentsWheelEvent(QWheelEvent *Event)
viewport()->update(); // because QScrollView thinks nothing has changed
App->view->drawn = false;
}

Event->accept(); // QScrollView must not handle this event
}

Expand Down
1 change: 1 addition & 0 deletions qucs/schematic.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ class Schematic : public Q3ScrollView, public QucsDoc {
int GridX, GridY;
int ViewX1, ViewY1, ViewX2, ViewY2; // size of the document area
int UsedX1, UsedY1, UsedX2, UsedY2; // document area used by elements
int zx1, zy1, zx2, zy2, dx, dy = 0;

int showFrame;
QString Frame_Text0, Frame_Text1, Frame_Text2, Frame_Text3;
Expand Down

0 comments on commit 77f50c2

Please sign in to comment.