Skip to content

Commit

Permalink
Merge pull request #777 from wawuwo/fix-765-wrong-text-angle-in-subci…
Browse files Browse the repository at this point in the history
…rcuit-symbol

Generalise Text::angle() logic
  • Loading branch information
ra3xdh committed Jun 21, 2024
2 parents 865c2e9 + e0363c2 commit 4929b41
Showing 1 changed file with 23 additions and 20 deletions.
43 changes: 23 additions & 20 deletions qucs/element.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "element.h"
#include "misc.h"

#include <cmath>
#include <QPainter>

namespace qucs {
Expand Down Expand Up @@ -70,28 +71,30 @@ void Text::draw(QPainter *painter, QRectF* br) const {
}

double Text::angle() const {
// Historically Text uses mSin and mCos values to store
// its rotation factor.
// Historically Text uses mSin and mCos values to describe
// at what angle the text is rotated.
//
// The actual rotation was implemented as a clever
// tranformation for a painter, like:
// QTransform(mCos, -mSin, mSin, mCos, …
// It employed some clever technique to draw the text rotated
// by applying "skew" transformations to QPainter and using mCos
// and mSin as skew factors, but later the whole drawing routine
// was refactored and it switched to using QPainter::rotate() and
// degrees to describe the rotation angle.
//
// There were only four combinations of these values
// and here we convert them to their human-readable
// equivalents
if (mCos == 0.0) {
if (mSin == 1.0) {
return 270;
}
return 90;
}

if (mCos == -1.0 && mSin == 0.0) {
return 180;
}

return 0;
// Many other parts of the codebase still depend on mCos and mSin,
// for example rotating and mirroring of a component and more than
// 200 component constructors. It is a piece of work to get rid
// of mCos and mSin completely, so they're kept as is and their
// "degrees" equivalent is calculated when the text needs to be drawn.

const double radians = std::atan2(mSin, mCos);
const double degrees = (radians * 180.0) / 3.14159265 /* Pi */;

// QPainter::rotate() rotates *clockwise*, so if you want a text
// to have rotation angle of 45° (towards up-right direction),
// then the -45° has to be passed to QPainter::rotate().
//
// QPainter::rotate() is called in Text::draw
return -degrees;
}


Expand Down

0 comments on commit 4929b41

Please sign in to comment.