Implement connection value drawing
MODIFY
src/graphics/connection.cpp
src/graphics/connection.cpp
#include "connection.h"
#include "shape.h"
+#include <QFontMetrics>
#include <QPainter>
namespace Graphics {
-static const float Pi = 3.1415926535;
+static constexpr float Pi = 3.1415926535;
Connection::Connection(Shape* source, QPointF* sourcePoint, Shape* dest, QPointF* destPoint)
- : m_source(source), m_dest(dest), m_sourcePointPtr(sourcePoint), m_destPointPtr(destPoint) {}
+ : QObject(), m_source(source), m_dest(dest), m_sourcePointPtr(sourcePoint), m_destPointPtr(destPoint) {
+ m_label.m_source = source;
+ m_label.m_drawPos = sourcePoint;
+}
QRectF Connection::boundingRect() const {
qreal penWidth = 1;
qreal extra = (penWidth + m_arrowSize) / 2.0;
-
QPointF sourcePoint = mapFromItem(m_source, *m_sourcePointPtr);
QPointF destPoint = mapFromItem(m_dest, *m_destPointPtr);
@@ -44,7 +47,6 @@ void Connection::paint(QPainter* painter, const QStyleOptionGraphicsItem* option
// Generate polygon from calculated points
QVector<QPointF> polyLine;
polyLine << sourcePoint;
-
if (dir) {
// Feed forward connection
int bias = m_kinkBiases.size() > 0 ? m_kinkBiases[0] : 0;
@@ -84,4 +86,51 @@ void Connection::paint(QPainter* painter, const QStyleOptionGraphicsItem* option
}
}
+void Connection::setValue(uint32_t value) {
+ QString text;
+ if (value < 0b1111) {
+ // Draw in binary notation
+ text = QString("0b%1").arg(QString().setNum(value, 2));
+ } else {
+ // Draw in hex notation
+ text = QString("0x%1").arg(QString().setNum(value, 16));
+ }
+ m_label.setText(text);
+ m_label.setToolTip(text);
+}
+
+void Connection::addLabelToScene() {
+ // Must be called after connection has been added to a scene
+ scene()->addItem(&m_label);
+}
+
+// --------- label ------------
+void Label::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) {
+ if (m_showValue) {
+ // Draw label (connection value)
+ QPointF pos = mapFromItem(m_source, *m_drawPos);
+ QFontMetrics metric = QFontMetrics(QFont());
+ QRectF textRect = metric.boundingRect(m_text);
+ textRect.moveTo(pos);
+ textRect.translate(0, -textRect.height());
+ textRect.adjust(-5, 0, 5, 5);
+ painter->fillRect(textRect, Qt::white);
+ painter->setBrush(Qt::NoBrush);
+ painter->setPen(QPen(Qt::black, 1));
+ painter->drawRect(textRect);
+ painter->setFont(QFont());
+ painter->drawText(pos, m_text);
+ }
+}
+
+QRectF Label::boundingRect() const {
+ QPointF pos = mapFromItem(m_source, *m_drawPos);
+ QFontMetrics metric = QFontMetrics(QFont());
+ QRectF textRect = metric.boundingRect(m_text);
+ textRect.moveTo(pos);
+ textRect.translate(0, -textRect.height());
+ textRect.adjust(-5, 0, 5, 5);
+ return textRect;
+}
+
} // namespace Graphics

MODIFY
src/graphics/connection.h
src/graphics/connection.h
#define CONNECTION_H
#include <QGraphicsItem>
+#include <QObject>
#include "pipelinewidget.h"
@@ -10,20 +11,37 @@ class Shape;
enum class ValueDrawPos { Source, Middle, Destination };
+// Class for drawing labels for connections. Disattached from connection, such that tooltips for connections are only
+// shown when hovering over the label position
+class Label : public QGraphicsItem {
+ friend class Connection;
+
+public:
+ Label() {}
+ void setText(QString text) { m_text = text; }
+
+protected:
+ QRectF boundingRect() const override;
+ void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) override;
+
+private:
+ QPointF* m_drawPos;
+ Shape* m_source;
+ bool m_showValue = false;
+ QString m_text;
+};
+
/*
Class for drawing connections and signal values between shapes in the
pipeline view.
*/
-class Connection : public QGraphicsItem {
+class Connection : public QObject, public QGraphicsItem {
+ Q_OBJECT
public:
Connection(Shape* source, QPointF* sourcePoint, Shape* dest, QPointF* destPoint);
void setValueDrawPos(ValueDrawPos pos) { m_valuePos = pos, update(); }
- void setValue(int value) { m_value = value; }
- void showValue(bool state) {
- m_showValue = state;
- update();
- }
+ void setValue(uint32_t value);
QPair<QPointF, QPointF> getPoints() const;
QPair<Shape*, Shape*> getShapes() const { return QPair<Shape*, Shape*>(m_source, m_dest); }
Shape* getSource() { return m_source; }
@@ -38,6 +56,10 @@ public:
m_sourceStubLen = sourceStubLen;
m_destStubLen = destStubLen;
}
+ void addLabelToScene();
+
+public slots:
+ void showValue(bool state) { m_label.m_showValue = state; }
protected:
QRectF boundingRect() const override;
@@ -57,8 +79,6 @@ private:
int m_destStubLen;
// Variables related to the current value of the connection (signal value)
- bool m_showValue = false;
- int m_value;
ValueDrawPos m_valuePos = ValueDrawPos::Source;
int m_arrowSize = 10;
@@ -67,6 +87,7 @@ private:
Shape* m_dest;
QPointF* m_sourcePointPtr;
QPointF* m_destPointPtr;
+ Label m_label;
};
} // namespace Graphics

MODIFY
src/graphics/pipelinewidget.cpp
src/graphics/pipelinewidget.cpp
@@ -14,10 +14,10 @@ PipelineWidget::PipelineWidget(QWidget* parent) : QGraphicsView(parent) {
QGraphicsScene* scene = new QGraphicsScene(this);
scene->setItemIndexMethod(QGraphicsScene::NoIndex);
setScene(scene);
- // setCacheMode(CacheBackground);
setRenderHint(QPainter::Antialiasing);
setRenderHint(QPainter::TextAntialiasing);
setDragMode(QGraphicsView::ScrollHandDrag);
+ setCursor(Qt::CrossCursor);
// ------------ Create pipeline objects ---------------
// Registers memory
@@ -290,12 +290,19 @@ void PipelineWidget::moveToIO(Graphics::Shape* source, Graphics::Shape* dest, QP
Graphics::Connection* PipelineWidget::createConnection(Graphics::Shape* source, int index1, Graphics::Shape* dest,
int index2) {
+ // for testing
+ static uint32_t value = 1;
+ value <<= 1;
+ //
Graphics::Connection* connection =
new Graphics::Connection(source, source->getOutputPoint(index1), dest, dest->getInputPoint(index2));
+ connect(this, &PipelineWidget::displayAllValuesSig, connection, &Graphics::Connection::showValue);
+ connection->setValue(value);
m_connections.append(connection);
source->addConnection(connection);
dest->addConnection(connection);
scene()->addItem(connection);
+ connection->addLabelToScene();
return connection;
}
@@ -340,3 +347,8 @@ void PipelineWidget::scaleView(qreal scaleFactor) {
scale(scaleFactor, scaleFactor);
}
+
+void PipelineWidget::displayAllValues(bool state) {
+ emit displayAllValuesSig(state);
+ scene()->update();
+}

MODIFY
src/graphics/pipelinewidget.h
src/graphics/pipelinewidget.h
@@ -16,6 +16,10 @@ public:
void wheelEvent(QWheelEvent* event);
void expandToView() { fitInView(scene()->sceneRect().adjusted(-10, 0, 10, 0), Qt::KeepAspectRatio); }
+ void displayAllValues(bool state);
+
+signals:
+ void displayAllValuesSig(bool state);
private:
void scaleView(qreal scaleFactor);

MODIFY
src/instructionmodel.cpp
src/instructionmodel.cpp
#include <QHeaderView>
-InstructionModel::InstructionModel(QObject *parent) : QStandardItemModel(parent)
-{
-
+InstructionModel::InstructionModel(QObject *parent) : QStandardItemModel(parent) {
setItem(0, 2, new QStandardItem());
setHeaderData(0, Qt::Horizontal, "PC");
setHeaderData(1, Qt::Horizontal, "Stage");

MODIFY
src/processortab.cpp
src/processortab.cpp
@@ -38,6 +38,10 @@ ProcessorTab::~ProcessorTab() {
delete m_ui;
}
-void ProcessorTab::on_toolButton_clicked() {
+void ProcessorTab::on_expandView_clicked() {
m_ui->pipelineWidget->expandToView();
}
+
+void ProcessorTab::on_displayValues_toggled(bool checked) {
+ m_ui->pipelineWidget->displayAllValues(checked);
+}

MODIFY
src/processortab.h
src/processortab.h
@@ -19,7 +19,9 @@ public:
void initRegWidget(std::vector<uint32_t>* regPtr);
private slots:
- void on_toolButton_clicked();
+ void on_expandView_clicked();
+
+ void on_displayValues_toggled(bool checked);
private:
Ui::ProcessorTab* m_ui;

MODIFY
src/processortab.ui
src/processortab.ui
</item>
<item>
<widget class="QCheckBox" name="displayValues">
+ <property name="toolTip">
+ <string>Hover over an output node to view signal value</string>
+ </property>
<property name="text">
- <string>Display signal values</string>
+ <string>Display all signal values</string>
</property>
</widget>
</item>
</widget>
</item>
<item>
- <widget class="QToolButton" name="toolButton">
+ <widget class="QToolButton" name="expandView">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>

Implement connection value drawing