Fix incorrect bounding rect calculation of signal value labels
MODIFY
src/graphics/connection.cpp
src/graphics/connection.cpp
@@ -12,6 +12,8 @@ namespace {
inline double pointDistance(QPointF a, QPointF b) {
return std::sqrt(std::pow(a.x() - b.x(), 2) + std::pow(a.y() - b.y(), 2));
}
+
+#define PENWIDTH 1
} // namespace
using namespace std;
@@ -55,6 +57,9 @@ void Connection::calculatePaths(){
QPointF sourcePoint = mapFromItem(m_source, *m_sourcePointPtr);
+ // move label to source point
+ m_label.setPos(sourcePoint);
+
// sort destinations
if (m_dests.size() > 1) {
std::sort(m_dests.begin(), m_dests.end(), [=](const PointPair& a, const PointPair& b) {
@@ -243,40 +248,50 @@ void Connection::addLabelToScene() {
scene()->addItem(&m_label);
}
+void Connection::toggleLabel(bool show){
+ m_label.setVisible(show);
+}
+
// --------- label ------------
-void Label::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) {
- Q_UNUSED(option);
- Q_UNUSED(widget);
- if (m_showValue && m_signal != nullptr && !Pipeline::getPipeline()->isRunning()) {
+void Label::paint(QPainter* painter, const QStyleOptionGraphicsItem*, QWidget*) {
+ if (m_signal != nullptr && !Pipeline::getPipeline()->isRunning()) {
// Draw label (connection value)
- auto text = QString("0x%1").arg(QString().setNum(m_signal->getValue(), 16));
- QPointF pos = mapFromItem(m_source, *m_drawPos);
- QFontMetrics metric = QFontMetrics(QFont(text));
- QRectF textRect = metric.boundingRect(text);
- textRect.moveTo(pos);
- textRect.translate(0, -textRect.height());
- textRect.adjust(-5, 0, 5, 5);
+ const auto text = getText();
+ QRectF textRect = getTextRect(text);
+ textRect.adjust(-5, 0, 5, 5);// adjust for pen width
painter->fillRect(textRect, Qt::white);
painter->setBrush(Qt::NoBrush);
- painter->setPen(QPen(Qt::black, 1));
+ painter->setPen(QPen(Qt::black, PENWIDTH));
painter->drawRect(textRect);
painter->setFont(QFont());
- painter->drawText(pos, text);
+ // Calculate drawing position
+ QPointF textPos = textRect.topLeft();
+ textPos.rx() += 5;
+ textPos.ry() += 14;
+ painter->drawText(textPos, text);
}
}
-QRectF Label::boundingRect() const {
- if (m_signal != nullptr && !Pipeline::getPipeline()->isRunning()) {
- auto text = QString("0x%1").arg(QString().setNum(m_signal->getValue(), 16));
- QPointF pos = mapFromItem(m_source, *m_drawPos);
+QString Label::getText() const {
+ return QString("0x%1").arg(QString().setNum(m_signal->getValue(), 16));
+}
+
+QRectF Label::getTextRect(const QString& text) const{
QFontMetrics metric = QFontMetrics(QFont());
- QRectF textRect = metric.boundingRect(text);
- textRect.moveTo(pos);
- textRect.translate(0, -textRect.height());
- textRect.adjust(-5, 0, 5, 5);
- return textRect;
+ QRectF generalRect = metric.boundingRect(text);
+ return generalRect;
}
+
+QRectF Label::boundingRect() const {
+ if (m_signal != nullptr && !Pipeline::getPipeline()->isRunning()) {
+ auto boundingRect = getTextRect(getText());
+
+ // Adjust for pen width etc.
+ boundingRect.adjust(-5 - PENWIDTH, - PENWIDTH, 5 + PENWIDTH, 5 + PENWIDTH);
+ return boundingRect;
+ } else {
return QRectF();
}
+}
} // namespace Graphics

MODIFY
src/graphics/connection.h
src/graphics/connection.h
@@ -25,6 +25,8 @@ public:
Label() {}
protected:
+ QRectF getTextRect(const QString& text) const;
+ QString getText() const;
QRectF boundingRect() const override;
void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) override;
@@ -32,7 +34,6 @@ private:
void setSignal(SignalBase* signal) { m_signal = signal; }
QPointF* m_drawPos;
Shape* m_source;
- bool m_showValue = false;
SignalBase* m_signal = nullptr;
};
@@ -78,7 +79,7 @@ public:
void setSignal(SignalBase* sig) { m_label.setSignal(sig); }
public slots:
- void showValue(bool state) { m_label.m_showValue = state; }
+ void toggleLabel(bool show);
protected:
QRectF boundingRect() const override;

MODIFY
src/graphics/pipelinewidget.cpp
src/graphics/pipelinewidget.cpp
@@ -533,7 +533,8 @@ Graphics::Connection* PipelineWidget::createConnection(Graphics::Shape* source,
// Connects a source and destination shape using IO index numbers
auto* connection =
new Graphics::Connection(source, source->getOutputPoint(index1), dest, dest->getInputPoint(index2));
- connect(this, &PipelineWidget::displayAllValuesSig, connection, &Graphics::Connection::showValue);
+ connect(this, &PipelineWidget::displayAllValuesSig, connection, &Graphics::Connection::toggleLabel);
+
m_connections.append(connection);
source->addConnection(connection);
dest->addConnection(connection);
@@ -558,7 +559,7 @@ Graphics::Connection* PipelineWidget::createConnection(Graphics::Shape* source,
// Create connection
Graphics::Connection* connection = new Graphics::Connection(source, source->getOutputPoint(index1), shapePointList);
- connect(this, &PipelineWidget::displayAllValuesSig, connection, &Graphics::Connection::showValue);
+ connect(this, &PipelineWidget::displayAllValuesSig, connection, &Graphics::Connection::toggleLabel);
source->addConnection(connection);
m_connections.append(connection);
scene()->addItem(connection);

Fix incorrect bounding rect calculation of signal value labels