From c797c146e9da827ea97cdc89a357947e4a32d657 Mon Sep 17 00:00:00 2001 From: hlorus <64740362+hlorus@users.noreply.github.com> Date: Tue, 14 May 2024 17:53:22 +0200 Subject: [PATCH] Gui: Add icon support in SoFrameLabel --- src/Gui/SoTextLabel.cpp | 39 +++++++++++++++++++++++++++++++++++---- src/Gui/SoTextLabel.h | 4 ++++ 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/src/Gui/SoTextLabel.cpp b/src/Gui/SoTextLabel.cpp index 9aaf958a81..a081055819 100644 --- a/src/Gui/SoTextLabel.cpp +++ b/src/Gui/SoTextLabel.cpp @@ -61,7 +61,6 @@ #include #include "SoTextLabel.h" -#include "BitmapFactory.h" #include "SoFCInteractiveElement.h" #include "Tools.h" @@ -383,6 +382,12 @@ SoFrameLabel::SoFrameLabel() //SO_NODE_ADD_FIELD(image, (SbVec2s(0,0), 0, NULL)); } +void SoFrameLabel::setIcon(const QPixmap &pixMap) +{ + iconPixmap = pixMap; +} + + void SoFrameLabel::notify(SoNotList * list) { SoField *f = list->getLastField(); @@ -425,7 +430,27 @@ void SoFrameLabel::drawImage() lines << line; } - QImage image(w+10,h+10,QImage::Format_ARGB32_Premultiplied); + int padding = 5; + + bool drawIcon = false; + QImage iconImg; + int widthIcon = 0; + int heightIcon = 0; + if (!iconPixmap.isNull()) { + drawIcon = true; + iconImg = iconPixmap.toImage(); + widthIcon = iconImg.width() + 2*padding; + heightIcon = iconImg.height() + 2*padding; + } + + int widthText = w + 2*padding; + int heightText = h + 2*padding; + int widthTotal = widthText + widthIcon; + int heightTotal = heightText > heightIcon ? heightText : heightIcon; + int paddingTextV = (heightTotal - h) / 2; + int paddingIconV = (heightTotal - iconImg.height()) / 2; + + QImage image(widthTotal, heightTotal, QImage::Format_ARGB32_Premultiplied); image.fill(0x00000000); QPainter painter(&image); painter.setRenderHint(QPainter::Antialiasing); @@ -435,10 +460,16 @@ void SoFrameLabel::drawImage() painter.setPen(QPen(QColor(0,0,127), 2, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin)); painter.setBrush(QBrush(brush, Qt::SolidPattern)); - QRectF rectangle(0.0, 0.0, w+10, h+10); + QRectF rectangle(0.0, 0.0, widthTotal, heightTotal); painter.drawRoundedRect(rectangle, 5, 5); } + + if (drawIcon) { + painter.drawImage(QPoint(padding, paddingIconV), iconImg); + + } + painter.setPen(front); Qt::Alignment align = Qt::AlignVCenter; @@ -450,7 +481,7 @@ void SoFrameLabel::drawImage() align = Qt::AlignVCenter | Qt::AlignHCenter; QString text = lines.join(QLatin1String("\n")); painter.setFont(font); - painter.drawText(5,5,w,h,align,text); + painter.drawText(widthIcon + padding, paddingTextV, w, h, align, text); painter.end(); SoSFImage sfimage; diff --git a/src/Gui/SoTextLabel.h b/src/Gui/SoTextLabel.h index 6581455d6a..b91feb1980 100644 --- a/src/Gui/SoTextLabel.h +++ b/src/Gui/SoTextLabel.h @@ -34,6 +34,8 @@ #include #include +#include "BitmapFactory.h" + namespace Gui { @@ -107,6 +109,7 @@ public: static void initClass(); SoFrameLabel(); + void setIcon(const QPixmap &pixMap); SoMFString string; SoSFColor textColor; @@ -116,6 +119,7 @@ public: SoSFInt32 size; SoSFBool frame; //SoSFImage image; + QPixmap iconPixmap; protected: ~SoFrameLabel() override = default;