Skip to content

Commit

Permalink
Use the line thickness attribute properly (#12115)
Browse files Browse the repository at this point in the history
Fixes #12090
Do not make the pen cosmetic by default.
Use flat cap and miter join. Set the miter join limit to 1.
  • Loading branch information
adeas31 committed Mar 18, 2024
1 parent 063c0d3 commit 513c661
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 66 deletions.
60 changes: 25 additions & 35 deletions OMEdit/OMEditLIB/Annotations/ShapeAnnotation.cpp
Expand Up @@ -504,45 +504,35 @@ QRectF ShapeAnnotation::getBoundingRect() const
*/
void ShapeAnnotation::applyLinePattern(QPainter *painter)
{
qreal thickness = Utilities::convertMMToPixel(mLineThickness);
/* Ticket #4490
* The specification doesn't say anything about it.
* But just to keep this consist with Dymola set a default line thickness for border patterns raised & sunken.
* We need better handling of border patterns.
/* Fixes issue #12090.
* Some old issues with nice use cases #3222, #2272, #2268.
*/
if (mBorderPattern == StringHandler::BorderRaised || mBorderPattern == StringHandler::BorderSunken) {
thickness = Utilities::convertMMToPixel(0.25);
}
// Make the display of Library Browser icons sharper. Very low line thickness is hardly visible on high resolution.
if (mLineThickness < 1.0 && ((mpGraphicsView && mpGraphicsView->useSharpLibraryPixmap())
|| (mpParentComponent && mpParentComponent->getGraphicsView()->useSharpLibraryPixmap()))) {
thickness = Utilities::convertMMToPixel(1.0);
qreal thickness = mLineThickness;

qreal curScale = 0.0;
if ((mpGraphicsView && mpGraphicsView->isRenderingLibraryPixmap()) || (mpParentComponent && mpParentComponent->getGraphicsView()->isRenderingLibraryPixmap())) {
thickness = mLineThickness + 3.0;
} else if (mpParentComponent) {
const QTransform painterTransform = painter->transform();
const qreal m11 = painterTransform.m11();
const qreal m22 = painterTransform.m22();
const qreal m12 = painterTransform.m12();
const qreal m21 = painterTransform.m21();
qreal xScale = qSqrt(m11*m11 + m12*m12);
qreal yScale = qSqrt(m22*m22 + m21*m21);
curScale = qMin(xScale, yScale);

if (mLineThickness > 0.0 && mLineThickness < 1.0 && curScale < 1.0) {
thickness = 1.0;
}
}

QPen pen(QBrush(mLineColor), thickness, StringHandler::getLinePatternType(mLinePattern), Qt::SquareCap, Qt::MiterJoin);
/* The specification doesn't say anything about it.
* But just to keep this consist with Dymola we use Qt::BevelJoin for Line shapes.
* All other shapes use Qt::MiterJoin
*/
if (dynamic_cast<LineAnnotation*>(this)) {
pen.setJoinStyle(Qt::BevelJoin);
}
/* Ticket #3222
* Make all the shapes use cosmetic pens so that they perserve their pen width when scaled i.e zoomed in/out.
*/
pen.setCosmetic(true);
/* Ticket #2272, Ticket #2268.
* If thickness is greater than 4 then don't make the pen cosmetic since cosmetic pens don't change the width with respect to zoom.
* Use non cosmetic pens for Library Browser and shapes inside component when thickness is greater than 4.
*/
if (thickness > 4
&& ((mpGraphicsView && mpGraphicsView->isRenderingLibraryPixmap()) || mpParentComponent)) {
pen.setCosmetic(false);
}
// if thickness is greater than 1 pixel then use antialiasing.
if (thickness > 1) {
painter->setRenderHint(QPainter::Antialiasing);
QPen pen(QBrush(mLineColor), thickness, StringHandler::getLinePatternType(mLinePattern), Qt::FlatCap, Qt::MiterJoin);
if (qFuzzyCompare(mLineThickness, 0.0) || (mpParentComponent && mLineThickness < 1.0 && curScale < 1.0)) {
pen.setCosmetic(true);
}
pen.setMiterLimit(1);
painter->setRenderHint(QPainter::Antialiasing);
painter->setPen(pen);
}

Expand Down
16 changes: 7 additions & 9 deletions OMEdit/OMEditLIB/Modeling/LibraryTreeWidget.cpp
Expand Up @@ -1805,26 +1805,24 @@ void LibraryTreeModel::loadLibraryTreeItemPixmap(LibraryTreeItem *pLibraryTreeIt
libraryPainter.scale(1.0, -1.0);
libraryPainter.translate(0, ((-source.top()) - source.bottom()));
libraryPainter.setWindow(source.toRect());
// render library pixmap
pGraphicsView->setRenderingLibraryPixmap(true);
pGraphicsView->scene()->render(&libraryPainter, source, source);
libraryPainter.end();
pLibraryTreeItem->setPixmap(libraryPixmap);
// drag pixmap
QPixmap dragPixmap(QSize(50, 50));
QPixmap dragPixmap(source.size().toSize());
dragPixmap.fill(QColor(Qt::transparent));
QPainter dragPainter(&dragPixmap);
dragPainter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
dragPainter.scale(1.0, -1.0);
dragPainter.translate(0, ((-source.top()) - source.bottom()));
dragPainter.setWindow(source.toRect());
pGraphicsView->setRenderingLibraryPixmap(true);
pGraphicsView->setSharpLibraryPixmap(true);
// render library pixmap
pGraphicsView->scene()->render(&libraryPainter, source, source);
libraryPainter.end();
pGraphicsView->setSharpLibraryPixmap(false);
// render drag pixmap
pGraphicsView->scene()->render(&dragPainter, source, source);
dragPainter.end();
pLibraryTreeItem->setDragPixmap(dragPixmap.scaled(QSize(50, 50), Qt::KeepAspectRatio, Qt::SmoothTransformation));
pGraphicsView->setRenderingLibraryPixmap(false);
pLibraryTreeItem->setPixmap(libraryPixmap);
pLibraryTreeItem->setDragPixmap(dragPixmap);
} else {
pLibraryTreeItem->setPixmap(QPixmap());
pLibraryTreeItem->setDragPixmap(pLibraryTreeItem->getLibraryTreeItemIcon().pixmap(QSize(50, 50)));
Expand Down
1 change: 0 additions & 1 deletion OMEdit/OMEditLIB/Modeling/ModelWidgetContainer.cpp
Expand Up @@ -213,7 +213,6 @@ GraphicsView::GraphicsView(StringHandler::ViewType viewType, ModelWidget *pModel
mpClickedState = 0;
setIsMovingComponentsAndShapes(false);
setRenderingLibraryPixmap(false);
setSharpLibraryPixmap(false);
mElementsList.clear();
mOutOfSceneElementsList.clear();
mConnectionsList.clear();
Expand Down
3 changes: 0 additions & 3 deletions OMEdit/OMEditLIB/Modeling/ModelWidgetContainer.h
Expand Up @@ -129,7 +129,6 @@ class GraphicsView : public QGraphicsView
Element *mpClickedState;
bool mIsMovingComponentsAndShapes;
bool mRenderingLibraryPixmap;
bool mSharpLibraryPixmap;
QList<Element*> mElementsList;
// A list of components that are not deleted but are removed from scene.
QList<Element*> mOutOfSceneElementsList;
Expand Down Expand Up @@ -228,8 +227,6 @@ class GraphicsView : public QGraphicsView
bool isMovingComponentsAndShapes() {return mIsMovingComponentsAndShapes;}
void setRenderingLibraryPixmap(bool renderingLibraryPixmap) {mRenderingLibraryPixmap = renderingLibraryPixmap;}
bool isRenderingLibraryPixmap() {return mRenderingLibraryPixmap;}
void setSharpLibraryPixmap(bool sharpLibraryPixmap) {mSharpLibraryPixmap = sharpLibraryPixmap;}
bool useSharpLibraryPixmap() {return mSharpLibraryPixmap;}
QList<ShapeAnnotation*> getShapesList() {return mShapesList;}
QList<ShapeAnnotation*> getInheritedShapesList() {return mInheritedShapesList;}
QAction* getManhattanizeAction() {return mpManhattanizeAction;}
Expand Down
17 changes: 0 additions & 17 deletions OMEdit/OMEditLIB/Util/Utilities.cpp
Expand Up @@ -1074,23 +1074,6 @@ bool Utilities::containsWord(QString text, int index, QString keyword, bool chec
return false;
}

/*!
* \brief Utilities::convertMMToPixel
* Converts the value from mm to pixels
* pixel = (dpi * mm / 1 inch)
* 1 inch is 25.4
* \param value
* \return
*/
qreal Utilities::convertMMToPixel(qreal value)
{
#if (QT_VERSION >= QT_VERSION_CHECK(5, 11, 0))
return (QApplication::primaryScreen()->logicalDotsPerInchX() * value) / 25.4;
#else // QT_VERSION_CHECK
return (QApplication::desktop()->screen()->logicalDpiX() * value) / 25.4;
#endif // QT_VERSION_CHECK
}

/*!
* \brief Utilities::maxi
* This function gives the maximum
Expand Down
1 change: 0 additions & 1 deletion OMEdit/OMEditLIB/Util/Utilities.h
Expand Up @@ -511,7 +511,6 @@ namespace Utilities {
} // namespace FileIconProvider

bool containsWord(QString text, int index, QString keyword, bool checkParenthesis = false);
qreal convertMMToPixel(qreal value);
float maxi(float arr[],int n);
float mini(float arr[], int n);
QList<QPointF> liangBarskyClipper(float xmin, float ymin, float xmax, float ymax, float x1, float y1, float x2, float y2);
Expand Down

0 comments on commit 513c661

Please sign in to comment.