forked from qt-creator/qt-creator
ModelEditor: Improve toolbar icon rendering
Change-Id: I11e523117dd192dc8450a0b5b2cca92f7b0ef166 Reviewed-by: Tobias Hunger <tobias.hunger@qt.io>
This commit is contained in:
@@ -133,49 +133,72 @@ QIcon StereotypeController::createIcon(StereotypeIcon::Element element, const QL
|
|||||||
{
|
{
|
||||||
// TODO implement cache with key build from element, stereotypes, defaultIconPath, style, size and margins
|
// TODO implement cache with key build from element, stereotypes, defaultIconPath, style, size and margins
|
||||||
// TODO implement unique id for style which can be used as key
|
// TODO implement unique id for style which can be used as key
|
||||||
// TODO fix rendering of icon which negativ extension of bounding box (e.g. stereotype "component")
|
|
||||||
QIcon icon;
|
QIcon icon;
|
||||||
QString stereotypeIconId = findStereotypeIconId(element, stereotypes);
|
QString stereotypeIconId = findStereotypeIconId(element, stereotypes);
|
||||||
if (!stereotypeIconId.isEmpty()) {
|
if (!stereotypeIconId.isEmpty()) {
|
||||||
StereotypeIcon stereotypeIcon = findStereotypeIcon(stereotypeIconId);
|
StereotypeIcon stereotypeIcon = findStereotypeIcon(stereotypeIconId);
|
||||||
|
|
||||||
qreal width = size.width() - margins.left() - margins.right();
|
// calculate bounding rectangle relativ to original icon size
|
||||||
qreal height = size.height() - margins.top() - margins.bottom();
|
|
||||||
qreal ratioWidth = height * stereotypeIcon.width() / stereotypeIcon.height();
|
|
||||||
qreal ratioHeight = width * stereotypeIcon.height() / stereotypeIcon.width();
|
|
||||||
if (ratioWidth > width)
|
|
||||||
height = ratioHeight;
|
|
||||||
else if (ratioHeight > height)
|
|
||||||
width = ratioWidth;
|
|
||||||
QSizeF shapeSize(width, height);
|
|
||||||
|
|
||||||
ShapeSizeVisitor sizeVisitor(QPointF(0.0, 0.0),
|
ShapeSizeVisitor sizeVisitor(QPointF(0.0, 0.0),
|
||||||
QSizeF(stereotypeIcon.width(), stereotypeIcon.height()),
|
QSizeF(stereotypeIcon.width(), stereotypeIcon.height()),
|
||||||
shapeSize, shapeSize);
|
QSizeF(stereotypeIcon.width(), stereotypeIcon.height()),
|
||||||
|
QSizeF(stereotypeIcon.width(), stereotypeIcon.height()));
|
||||||
stereotypeIcon.iconShape().visitShapes(&sizeVisitor);
|
stereotypeIcon.iconShape().visitShapes(&sizeVisitor);
|
||||||
QRectF iconBoundingRect = sizeVisitor.boundingRect();
|
QRectF iconBoundingRect = sizeVisitor.boundingRect();
|
||||||
QPixmap pixmap(iconBoundingRect.width() + margins.left() + margins.right(),
|
|
||||||
iconBoundingRect.height() + margins.top() + margins.bottom());
|
// calc painting space within margins
|
||||||
|
qreal innerWidth = size.width() - margins.left() - margins.right();
|
||||||
|
qreal innerHeight = size.height() - margins.top() - margins.bottom();
|
||||||
|
|
||||||
|
// calculate width/height ratio from icon size
|
||||||
|
qreal widthRatio = 1.0;
|
||||||
|
qreal heightRatio = 1.0;
|
||||||
|
qreal ratio = stereotypeIcon.width() / stereotypeIcon.height();
|
||||||
|
if (ratio > 1.0)
|
||||||
|
heightRatio /= ratio;
|
||||||
|
else
|
||||||
|
widthRatio *= ratio;
|
||||||
|
|
||||||
|
// calculate inner painting area
|
||||||
|
qreal paintWidth = stereotypeIcon.width() * innerWidth / iconBoundingRect.width() * widthRatio;
|
||||||
|
qreal paintHeight = stereotypeIcon.height() * innerHeight / iconBoundingRect.height() * heightRatio;
|
||||||
|
// icons which renders smaller than their size should not be zoomed
|
||||||
|
if (paintWidth > innerWidth) {
|
||||||
|
paintHeight *= innerWidth / paintHeight;
|
||||||
|
paintWidth = innerWidth;
|
||||||
|
}
|
||||||
|
if (paintHeight > innerHeight) {
|
||||||
|
paintWidth *= innerHeight / paintHeight;
|
||||||
|
paintHeight = innerHeight;
|
||||||
|
}
|
||||||
|
|
||||||
|
// calculate offset of top/left edge
|
||||||
|
qreal paintLeft = iconBoundingRect.left() * paintWidth / stereotypeIcon.width();
|
||||||
|
qreal paintTop = iconBoundingRect.top() * paintHeight / stereotypeIcon.height();
|
||||||
|
|
||||||
|
// calculate total painting size
|
||||||
|
qreal totalPaintWidth = iconBoundingRect.width() * paintWidth / stereotypeIcon.width();
|
||||||
|
qreal totalPaintHeight = iconBoundingRect.height() * paintHeight / stereotypeIcon.height();
|
||||||
|
|
||||||
|
QPixmap pixmap(size);
|
||||||
pixmap.fill(Qt::transparent);
|
pixmap.fill(Qt::transparent);
|
||||||
QPainter painter(&pixmap);
|
QPainter painter(&pixmap);
|
||||||
painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::SmoothPixmapTransform);
|
painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::SmoothPixmapTransform);
|
||||||
painter.setBrush(Qt::NoBrush);
|
painter.setBrush(Qt::NoBrush);
|
||||||
painter.translate(-iconBoundingRect.topLeft() + QPointF(margins.left(), margins.top()));
|
// set painting origin taking margin, offset and centering into account
|
||||||
|
painter.translate(QPointF(margins.left(), margins.top()) - QPointF(paintLeft, paintTop)
|
||||||
|
+ QPointF((innerWidth - totalPaintWidth) / 2, (innerHeight - totalPaintHeight) / 2));
|
||||||
QPen linePen = style->linePen();
|
QPen linePen = style->linePen();
|
||||||
linePen.setWidthF(lineWidth);
|
linePen.setWidthF(lineWidth);
|
||||||
painter.setPen(linePen);
|
painter.setPen(linePen);
|
||||||
painter.setBrush(style->fillBrush());
|
painter.setBrush(style->fillBrush());
|
||||||
|
|
||||||
ShapePaintVisitor visitor(&painter, QPointF(0.0, 0.0),
|
ShapePaintVisitor visitor(&painter, QPointF(0.0, 0.0),
|
||||||
QSizeF(stereotypeIcon.width(), stereotypeIcon.height()),
|
QSizeF(stereotypeIcon.width(), stereotypeIcon.height()),
|
||||||
shapeSize, shapeSize);
|
QSizeF(paintWidth, paintHeight), QSizeF(paintWidth, paintHeight));
|
||||||
stereotypeIcon.iconShape().visitShapes(&visitor);
|
stereotypeIcon.iconShape().visitShapes(&visitor);
|
||||||
|
|
||||||
QPixmap iconPixmap(size);
|
icon = QIcon(pixmap);
|
||||||
iconPixmap.fill(Qt::transparent);
|
|
||||||
QPainter iconPainter(&iconPixmap);
|
|
||||||
iconPainter.drawPixmap((iconPixmap.width() - pixmap.width()) / 2,
|
|
||||||
(iconPixmap.width() - pixmap.height()) / 2, pixmap);
|
|
||||||
icon = QIcon(iconPixmap);
|
|
||||||
}
|
}
|
||||||
if (icon.isNull() && !defaultIconPath.isEmpty())
|
if (icon.isNull() && !defaultIconPath.isEmpty())
|
||||||
icon = QIcon(defaultIconPath);
|
icon = QIcon(defaultIconPath);
|
||||||
|
|||||||
@@ -1063,7 +1063,7 @@ void ModelEditor::initToolbars()
|
|||||||
const qmt::Style *style = documentController->styleController()->adaptStyle(styleEngineElementType);
|
const qmt::Style *style = documentController->styleController()->adaptStyle(styleEngineElementType);
|
||||||
icon = stereotypeController->createIcon(
|
icon = stereotypeController->createIcon(
|
||||||
stereotypeIconElement, QStringList() << tool.m_stereotype,
|
stereotypeIconElement, QStringList() << tool.m_stereotype,
|
||||||
QString(), style, QSize(256, 256), QMarginsF(12.0, 8.0, 12.0, 16.0), 16.0);
|
QString(), style, QSize(128, 128), QMarginsF(6.0, 4.0, 6.0, 8.0), 8.0);
|
||||||
if (!icon.isNull()) {
|
if (!icon.isNull()) {
|
||||||
QString stereotypeIconId = stereotypeController->findStereotypeIconId(
|
QString stereotypeIconId = stereotypeController->findStereotypeIconId(
|
||||||
stereotypeIconElement, QStringList() << tool.m_stereotype);
|
stereotypeIconElement, QStringList() << tool.m_stereotype);
|
||||||
|
|||||||
Reference in New Issue
Block a user