diff options
author | Karel Kočí <cynerd@email.cz> | 2017-12-15 22:45:28 +0100 |
---|---|---|
committer | Karel Kočí <cynerd@email.cz> | 2017-12-15 22:45:28 +0100 |
commit | e6ca4b4568e311b47239bfe83de15ed9e91c57b9 (patch) | |
tree | 3da2f72faf360058bae02c35b0c724233bd64d61 /qtmips_gui/coreview/connection.cpp | |
parent | bbea996112eb7ac81ec50d2af08f4bd681d0e50d (diff) | |
download | qtmips-e6ca4b4568e311b47239bfe83de15ed9e91c57b9.tar.gz qtmips-e6ca4b4568e311b47239bfe83de15ed9e91c57b9.tar.bz2 qtmips-e6ca4b4568e311b47239bfe83de15ed9e91c57b9.zip |
Implement few initial graphic elements
Diffstat (limited to 'qtmips_gui/coreview/connection.cpp')
-rw-r--r-- | qtmips_gui/coreview/connection.cpp | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/qtmips_gui/coreview/connection.cpp b/qtmips_gui/coreview/connection.cpp new file mode 100644 index 0000000..ebca519 --- /dev/null +++ b/qtmips_gui/coreview/connection.cpp @@ -0,0 +1,79 @@ +#include "connection.h" + +using namespace coreview; + +void Connector::setPos(qreal x, qreal y) { + qx = x; + qy = y; + emit updated(); +} + +qreal Connector::x() const { + return qx; +} + +qreal Connector::y() const { + return qy; +} + +QPointF Connector::point() const { + return QPointF(qx, qy); +} + +Connection::Connection(const Connector *a, const Connector *b) : QGraphicsObject(nullptr) { + connect(a, SIGNAL(updated()), this, SLOT(moved())); + connect(b, SIGNAL(updated()), this, SLOT(moved())); + this->a = a; + this->b = b; + update_pos(); +} + +void Connection::setHasText(bool has) { + if (has && value == nullptr) { + value = new QGraphicsSimpleTextItem(this); + value->setText(text); + } else if (!has && value != nullptr) { + delete value; + } + update_pos(); +} + +void Connection::setText(QString val) { + text = val; + if (value != nullptr) + value->setText(val); +} + +void Connection::moved() { + update_pos(); +} + +QRectF Connection::boundingRect() const { + QRectF rect; + for (int i = 0; i < (points.size() - 1); i++) { + qreal x = points[i].x(); + if (x > points[i+1].x()) + x = points[i+1].x(); + qreal y = points[i].y(); + if (y > points[i+1].y()) + y = points[i+1].y(); + // TODO pen width + rect.united(QRectF(x - 0.5, y - 0.5, fabs(points[i].x() - points[i+1].x()) + 1, fabs(points[i].y() - points[i+1].y()) + 1)); + } + //return rect; + return QRectF(0, 0, 300, 300); +} + +void Connection::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { + for (int i = 0; i < (points.size() - 1); i++) + painter->drawLine(points[i], points[i+1]); + // TODO meaby use QPath instead? +} + +void Connection::update_pos() { + points.clear(); + points.append(a->point()); + points.append(b->point()); + // TODO more than one line + // TODO update position of value +} |