+#ifndef INCLUDE__BasicBlockWidget_hxx
+#define INCLUDE__BasicBlockWidget_hxx
+
#include "gui/qt.hxx"
+#include "disassembler/BasicBlock.hxx"
+#include <vector>
+#include <cassert>
class BasicBlockWidget : public QGraphicsItem
{
public:
- BasicBlockWidget(const QString& name)
- : x(-5), y(-20)
- , dx(270), dy(45)
- , name(name) {
- _widget.setMinimumHeight(_widget.rowHeight(0) + 10);
- _widget.setMaximumHeight(20);
- _widget.setColumnCount(3);
- _widget.verticalHeader()->hide();
- _widget.horizontalHeader()->hide();
- dx = _widget.rowHeight(0) + 20;
- if (dx < 270) dx = 270;
- }
-
- void addItem(uint8_t* bytes, size_t num_bytes,
- const QString& line) {
- size_t current_row = _widget.rowCount();
- int column_width;
+ BasicBlockWidget(const QString& name, BasicBlock * block);
- QString bytestring;
+ void addItem(uint8_t* bytes, size_t num_bytes, const QString& line);
- for (size_t i(0); i < num_bytes; ++i) {
- const char * hexdigits = "0123456789ABCDEF";
- bytestring += hexdigits[(bytes[i] >> 4) & 0xF];
- bytestring += hexdigits[bytes[i] & 0xF];
- bytestring += ' ';
- }
+ QRectF boundingRect() const {
+ qreal penWidth = 1;
- _widget.setRowCount(current_row + 1);
- _widget.setItem(current_row, 0, new QTableWidgetItem(bytestring));
- _widget.setItem(current_row, 1, new QTableWidgetItem(line));
- _widget.setItem(current_row, 2, new QTableWidgetItem(""));
- _widget.updateGeometry();
+ if (next[0]) {
+ if (next[1]) {
+ result |= QRectF(QPointF(x + dx/3, y+dy),
+ mapFromScene(next[0]->getEntry()));
+ } else {
+ result |= QRectF(QPointF(x + dx/2, y+dy),
+ mapFromScene(next[0]->getEntry()));
+ }
+ }
+ if (next[1])
+ result |= QRectF(QPointF(x + 2*dx/3, y+dy),
+ mapFromScene(next[1]->getEntry()));
- _widget.resizeColumnToContents(0);
- _widget.resizeColumnToContents(1);
- _widget.resizeColumnToContents(2);
+ QRectF result(- penWidth / 2, - penWidth / 2,
+ width + penWidth, height + penWidth);
+ return result;
+ }
+ void mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
+ QGraphicsItem::mouseMoveEvent(event);
+ std::for_each(previous.begin(), previous.end(),
+ [&] (BasicBlockWidget * item) {
+ item->update(boundingRect() | item->boundingRect());
+ });
+ }
- _widget.resizeRowToContents(current_row);
+ QPointF getEntry() const {
+ return mapToScene(QPointF(width/2, 0));
+ }
- column_width =
- _widget.columnWidth(0) +
- _widget.columnWidth(1) +
- _widget.columnWidth(2) +
- 2;
+ void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
+ QWidget *widget) {
+ painter->fillRect(0, 0, width, height, QColor(0xcc, 0xcc, 0xff, 0xff));
+ painter->setPen(QColor(0x00, 0x00, 0xff, 0xff));
+ painter->drawRect(0, 0, width, height);
+ painter->drawText(5, 15, name);
+ if (_widget.rowCount() != 0)
+ _widget.render(painter);
- _widget.setMinimumWidth(column_width);
- _widget.setMinimumHeight(_widget.rowHeight(1) * (_widget.rowCount()) + 2);
- _widget.setMaximumHeight(_widget.rowHeight(1) * (_widget.rowCount()) + 2);
+ if (next[0]) {
+ if (next[1]) {
+ painter->setPen(QColor(0x00, 0xff, 0x00, 0xff));
+ painter->drawLine(QPointF(x + dx/3, y+dy),
+ mapFromScene(next[0]->getEntry()));
+ } else {
+ painter->setPen(QColor(0x00, 0x00, 0x00, 0xff));
+ painter->drawLine(QPointF(x + dx/2, y+dy),
+ mapFromScene(next[0]->getEntry()));
+ }
+ }
+ painter->setPen(QColor(0xff, 0x00, 0x00, 0xff));
+ if (next[1])
+ painter->drawLine(QPointF(x + 2*dx/3, y+dy),
+ mapFromScene(next[1]->getEntry()));
+ }
- dy = _widget.rowHeight(0) * (_widget.rowCount()) + 25;
- dx = column_width + 10;
- if (dx < 270) dx = 270;
+ void addPrevious(BasicBlockWidget * widget) {
+ previous.push_back(widget);
}
- QRectF boundingRect() const
- {
- qreal penWidth = 1;
- return QRectF(x - penWidth / 2, y - penWidth / 2,
- dx + penWidth, dy + penWidth);
- }
+ void addNext(BasicBlockWidget * left, BasicBlockWidget * right) {
+ next[0] = left;
+ next[1] = right;
+ }
- void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
- QWidget *widget)
- {
- painter->fillRect(x, y, dx, dy, QColor(0xcc, 0xcc, 0xff, 0xff));
- painter->setPen(QColor(0x00, 0x00, 0xff, 0xff));
- painter->drawRect(x, y, dx, dy);
- painter->drawText(0, -5, name);
- _widget.render(painter);
- }
private:
- int x, y, dx, dy;
+ uint32_t width, height;
QTableWidget _widget;
QString name;
+ BasicBlock * block;
+ std::vector<BasicBlockWidget*> previous;
+ BasicBlockWidget* next[2];
};
+
+#endif