2017-12-13 22:38:46 +00:00
|
|
|
#include "GraphView.h"
|
|
|
|
|
2019-04-04 05:54:42 +00:00
|
|
|
#include "GraphGridLayout.h"
|
|
|
|
|
2017-12-13 22:38:46 +00:00
|
|
|
#include <vector>
|
|
|
|
#include <QPainter>
|
|
|
|
#include <QMouseEvent>
|
|
|
|
#include <QPropertyAnimation>
|
|
|
|
|
2019-04-07 10:53:42 +00:00
|
|
|
#ifndef QT_NO_OPENGL
|
|
|
|
#include <QOpenGLContext>
|
|
|
|
#include <QOpenGLWidget>
|
|
|
|
#include <QOpenGLPaintDevice>
|
|
|
|
#include <QOpenGLExtraFunctions>
|
|
|
|
#endif
|
|
|
|
|
2017-12-13 22:38:46 +00:00
|
|
|
GraphView::GraphView(QWidget *parent)
|
|
|
|
: QAbstractScrollArea(parent)
|
2019-04-04 05:54:42 +00:00
|
|
|
, graphLayoutSystem(new GraphGridLayout())
|
2019-04-07 10:53:42 +00:00
|
|
|
, useGL(false)
|
|
|
|
#ifndef QT_NO_OPENGL
|
|
|
|
, cacheTexture(0)
|
|
|
|
, cacheFBO(0)
|
|
|
|
#endif
|
2017-12-13 22:38:46 +00:00
|
|
|
{
|
2019-04-07 10:53:42 +00:00
|
|
|
#ifndef QT_NO_OPENGL
|
|
|
|
if (useGL) {
|
|
|
|
glWidget = new QOpenGLWidget(this);
|
|
|
|
setViewport(glWidget);
|
|
|
|
} else {
|
|
|
|
glWidget = nullptr;
|
|
|
|
}
|
|
|
|
#endif
|
2017-12-13 22:38:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GraphView::~GraphView()
|
|
|
|
{
|
|
|
|
// TODO: Cleanups
|
|
|
|
}
|
|
|
|
|
|
|
|
// Callbacks
|
2018-03-21 20:32:32 +00:00
|
|
|
void GraphView::drawBlock(QPainter &p, GraphView::GraphBlock &block)
|
2017-12-13 22:38:46 +00:00
|
|
|
{
|
|
|
|
Q_UNUSED(p);
|
|
|
|
Q_UNUSED(block);
|
|
|
|
qWarning() << "Draw block not overriden!";
|
|
|
|
}
|
|
|
|
|
|
|
|
void GraphView::blockClicked(GraphView::GraphBlock &block, QMouseEvent *event, QPoint pos)
|
|
|
|
{
|
|
|
|
Q_UNUSED(block);
|
|
|
|
Q_UNUSED(event);
|
|
|
|
Q_UNUSED(pos);
|
|
|
|
qWarning() << "Block clicked not overridden!";
|
|
|
|
}
|
|
|
|
|
2017-12-14 21:07:48 +00:00
|
|
|
void GraphView::blockDoubleClicked(GraphView::GraphBlock &block, QMouseEvent *event, QPoint pos)
|
|
|
|
{
|
|
|
|
Q_UNUSED(block);
|
|
|
|
Q_UNUSED(event);
|
|
|
|
Q_UNUSED(pos);
|
|
|
|
qWarning() << "Block double clicked not overridden!";
|
|
|
|
}
|
|
|
|
|
2017-12-19 16:59:39 +00:00
|
|
|
void GraphView::blockHelpEvent(GraphView::GraphBlock &block, QHelpEvent *event, QPoint pos)
|
|
|
|
{
|
|
|
|
Q_UNUSED(block);
|
|
|
|
Q_UNUSED(event);
|
|
|
|
Q_UNUSED(pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GraphView::helpEvent(QHelpEvent *event)
|
|
|
|
{
|
2019-03-23 08:21:06 +00:00
|
|
|
int x = event->pos().x() + offset.x();
|
|
|
|
int y = event->pos().y() - offset.y();
|
2017-12-19 16:59:39 +00:00
|
|
|
|
2018-03-21 20:32:32 +00:00
|
|
|
for (auto &blockIt : blocks) {
|
2017-12-19 16:59:39 +00:00
|
|
|
GraphBlock &block = blockIt.second;
|
|
|
|
|
2018-03-21 20:32:32 +00:00
|
|
|
if ((block.x <= x) && (block.y <= y) &&
|
|
|
|
(x <= block.x + block.width) & (y <= block.y + block.height)) {
|
2017-12-19 16:59:39 +00:00
|
|
|
QPoint pos = QPoint(x - block.x, y - block.y);
|
|
|
|
blockHelpEvent(block, event, pos);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-12-13 22:38:46 +00:00
|
|
|
void GraphView::blockTransitionedTo(GraphView::GraphBlock *to)
|
|
|
|
{
|
|
|
|
Q_UNUSED(to);
|
|
|
|
qWarning() << "blockTransitionedTo not overridden!";
|
|
|
|
}
|
|
|
|
|
2018-03-21 20:32:32 +00:00
|
|
|
GraphView::EdgeConfiguration GraphView::edgeConfiguration(GraphView::GraphBlock &from,
|
|
|
|
GraphView::GraphBlock *to)
|
2017-12-13 22:38:46 +00:00
|
|
|
{
|
|
|
|
Q_UNUSED(from);
|
|
|
|
Q_UNUSED(to);
|
|
|
|
qWarning() << "Edge configuration not overridden!";
|
|
|
|
EdgeConfiguration ec;
|
|
|
|
return ec;
|
|
|
|
}
|
|
|
|
|
2017-12-19 16:59:39 +00:00
|
|
|
bool GraphView::event(QEvent *event)
|
|
|
|
{
|
2018-03-21 20:32:32 +00:00
|
|
|
if (event->type() == QEvent::ToolTip) {
|
|
|
|
if (helpEvent(static_cast<QHelpEvent *>(event))) {
|
2017-12-19 16:59:39 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return QAbstractScrollArea::event(event);
|
|
|
|
}
|
|
|
|
|
2017-12-13 22:38:46 +00:00
|
|
|
// This calculates the full graph starting at block entry.
|
|
|
|
void GraphView::computeGraph(ut64 entry)
|
|
|
|
{
|
2019-04-04 05:54:42 +00:00
|
|
|
graphLayoutSystem->CalculateLayout(blocks, entry, width, height);
|
2017-12-14 21:07:48 +00:00
|
|
|
ready = true;
|
2017-12-13 22:38:46 +00:00
|
|
|
|
2017-12-14 21:07:48 +00:00
|
|
|
viewport()->update();
|
2019-02-16 17:17:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QPolygonF GraphView::recalculatePolygon(QPolygonF polygon)
|
|
|
|
{
|
|
|
|
QPolygonF ret;
|
|
|
|
for (int i = 0; i < polygon.size(); i++) {
|
2019-03-23 08:21:06 +00:00
|
|
|
ret << QPointF(polygon[i].x() - offset.x(), polygon[i].y() - offset.y());
|
2019-02-16 17:17:11 +00:00
|
|
|
}
|
|
|
|
return ret;
|
2017-12-13 22:38:46 +00:00
|
|
|
}
|
|
|
|
|
2019-04-07 11:02:35 +00:00
|
|
|
void GraphView::beginMouseDrag(QMouseEvent *event)
|
|
|
|
{
|
|
|
|
scroll_base_x = event->x();
|
|
|
|
scroll_base_y = event->y();
|
|
|
|
scroll_mode = true;
|
|
|
|
setCursor(Qt::ClosedHandCursor);
|
|
|
|
viewport()->grabMouse();
|
|
|
|
}
|
|
|
|
|
2019-04-07 10:53:42 +00:00
|
|
|
void GraphView::paintEvent(QPaintEvent *)
|
2017-12-13 22:38:46 +00:00
|
|
|
{
|
2019-04-07 10:53:42 +00:00
|
|
|
#ifndef QT_NO_OPENGL
|
|
|
|
if (useGL) {
|
|
|
|
glWidget->makeCurrent();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if(!useCache || !qFuzzyCompare(devicePixelRatioF(), pixmap.devicePixelRatioF())) {
|
|
|
|
paintGraphCache();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (useGL) {
|
|
|
|
#ifndef QT_NO_OPENGL
|
|
|
|
auto gl = glWidget->context()->extraFunctions();
|
|
|
|
gl->glBindFramebuffer(GL_READ_FRAMEBUFFER, cacheFBO);
|
|
|
|
gl->glBindFramebuffer(GL_DRAW_FRAMEBUFFER, glWidget->defaultFramebufferObject());
|
|
|
|
gl->glBlitFramebuffer(0, 0, viewport()->width(), viewport()->height(),
|
|
|
|
0, 0, viewport()->width(), viewport()->height(),
|
|
|
|
GL_COLOR_BUFFER_BIT, GL_NEAREST);
|
|
|
|
glWidget->doneCurrent();
|
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
QRectF target(0.0, 0.0, viewport()->width(), viewport()->height());
|
|
|
|
QRectF source(0.0, 0.0, viewport()->width() * pixmap.devicePixelRatioF(),
|
|
|
|
viewport()->height() * pixmap.devicePixelRatioF());
|
|
|
|
QPainter p(viewport());
|
|
|
|
p.drawPixmap(target, pixmap, source);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!useCache) { // TODO: does this condition make sense?
|
|
|
|
emit refreshBlock();
|
2019-03-12 07:37:10 +00:00
|
|
|
}
|
2019-04-07 10:53:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GraphView::paintGraphCache()
|
|
|
|
{
|
|
|
|
#ifndef QT_NO_OPENGL
|
|
|
|
QOpenGLPaintDevice *paintDevice = nullptr;
|
|
|
|
#endif
|
|
|
|
QPainter p;
|
|
|
|
if (useGL) {
|
|
|
|
#ifndef QT_NO_OPENGL
|
|
|
|
auto gl = QOpenGLContext::currentContext()->functions();
|
|
|
|
|
|
|
|
bool resizeTex = false;
|
|
|
|
if (!cacheTexture) {
|
|
|
|
gl->glGenTextures(1, &cacheTexture);
|
|
|
|
gl->glBindTexture(GL_TEXTURE_2D, cacheTexture);
|
|
|
|
gl->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
|
|
|
gl->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
|
|
|
gl->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
|
|
|
gl->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
|
|
|
gl->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
|
|
|
resizeTex = true;
|
|
|
|
} else if(cacheSize != viewport()->size()) {
|
|
|
|
gl->glBindTexture(GL_TEXTURE_2D, cacheTexture);
|
|
|
|
resizeTex = true;
|
|
|
|
}
|
|
|
|
if (resizeTex) {
|
|
|
|
cacheSize = viewport()->size();
|
|
|
|
gl->glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, viewport()->width(), viewport()->height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
|
|
|
|
gl->glGenFramebuffers(1, &cacheFBO);
|
|
|
|
gl->glBindFramebuffer(GL_FRAMEBUFFER, cacheFBO);
|
|
|
|
gl->glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, cacheTexture, 0);
|
|
|
|
} else {
|
|
|
|
gl->glBindFramebuffer(GL_FRAMEBUFFER, cacheFBO);
|
|
|
|
}
|
|
|
|
gl->glViewport(0, 0, viewport()->width(), viewport()->height());
|
|
|
|
gl->glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
|
|
|
|
gl->glClear(GL_COLOR_BUFFER_BIT);
|
2018-07-18 10:15:10 +00:00
|
|
|
|
2019-04-07 10:53:42 +00:00
|
|
|
paintDevice = new QOpenGLPaintDevice(viewport()->size());
|
|
|
|
p.begin(paintDevice);
|
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
auto dpr = devicePixelRatioF();
|
|
|
|
pixmap = QPixmap(int(viewport()->width() * dpr), int(viewport()->height() * dpr));
|
|
|
|
pixmap.setDevicePixelRatio(dpr);
|
|
|
|
p.begin(&pixmap);
|
|
|
|
p.setRenderHint(QPainter::Antialiasing);
|
|
|
|
}
|
2018-07-18 10:15:10 +00:00
|
|
|
|
2019-02-16 17:17:11 +00:00
|
|
|
int render_width = viewport()->width();
|
|
|
|
int render_height = viewport()->height();
|
2017-12-14 21:07:48 +00:00
|
|
|
|
2017-12-13 22:38:46 +00:00
|
|
|
p.setBrush(backgroundColor);
|
2019-04-03 08:55:39 +00:00
|
|
|
p.drawRect(viewport()->rect());
|
2017-12-13 22:38:46 +00:00
|
|
|
p.setBrush(Qt::black);
|
|
|
|
|
|
|
|
p.scale(current_scale, current_scale);
|
|
|
|
|
2018-03-21 20:32:32 +00:00
|
|
|
for (auto &blockIt : blocks) {
|
2017-12-13 22:38:46 +00:00
|
|
|
GraphBlock &block = blockIt.second;
|
|
|
|
|
2019-02-16 17:17:11 +00:00
|
|
|
qreal blockX = block.x * current_scale;
|
|
|
|
qreal blockY = block.y * current_scale;
|
|
|
|
qreal blockWidth = block.width * current_scale;
|
|
|
|
qreal blockHeight = block.height * current_scale;
|
2019-02-05 15:21:02 +00:00
|
|
|
|
|
|
|
// Check if block is visible by checking if block intersects with view area
|
2019-03-23 08:21:06 +00:00
|
|
|
if (offset.x() * current_scale < blockX + blockWidth
|
|
|
|
&& blockX < offset.x() * current_scale + render_width
|
|
|
|
&& offset.y() * current_scale < blockY + blockHeight
|
|
|
|
&& blockY < offset.y() * current_scale + render_height) {
|
2017-12-13 22:38:46 +00:00
|
|
|
drawBlock(p, block);
|
|
|
|
}
|
|
|
|
|
|
|
|
p.setBrush(Qt::gray);
|
|
|
|
|
|
|
|
// Always draw edges
|
|
|
|
// TODO: Only draw edges if they are actually visible ...
|
|
|
|
// Draw edges
|
2018-03-21 20:32:32 +00:00
|
|
|
for (GraphEdge &edge : block.edges) {
|
2019-04-05 06:28:11 +00:00
|
|
|
if (edge.polyline.empty()) {
|
|
|
|
continue;
|
|
|
|
}
|
2019-02-16 17:17:11 +00:00
|
|
|
QPolygonF polyline = recalculatePolygon(edge.polyline);
|
2019-04-04 05:54:42 +00:00
|
|
|
EdgeConfiguration ec = edgeConfiguration(block, &blocks[edge.target]);
|
|
|
|
QPen pen(ec.color);
|
2019-02-05 15:21:02 +00:00
|
|
|
pen.setWidth(pen.width() / ec.width_scale);
|
2017-12-13 22:38:46 +00:00
|
|
|
p.setPen(pen);
|
2019-04-04 05:54:42 +00:00
|
|
|
p.setBrush(ec.color);
|
2019-02-16 17:17:11 +00:00
|
|
|
p.drawPolyline(polyline);
|
2017-12-13 22:38:46 +00:00
|
|
|
pen.setStyle(Qt::SolidLine);
|
|
|
|
p.setPen(pen);
|
2019-04-04 05:54:42 +00:00
|
|
|
if (!polyline.empty()) {
|
|
|
|
if (ec.start_arrow) {
|
|
|
|
auto firstPt = edge.polyline.first();
|
|
|
|
QPolygonF arrowStart;
|
|
|
|
arrowStart << QPointF(firstPt.x() - 3, firstPt.y() + 6);
|
|
|
|
arrowStart << QPointF(firstPt.x() + 3, firstPt.y() + 6);
|
|
|
|
arrowStart << QPointF(firstPt);
|
|
|
|
p.drawConvexPolygon(recalculatePolygon(arrowStart));
|
|
|
|
}
|
|
|
|
if (ec.end_arrow) {
|
|
|
|
auto lastPt = edge.polyline.last();
|
|
|
|
QPolygonF arrowEnd;
|
|
|
|
arrowEnd << QPointF(lastPt.x() - 3, lastPt.y() - 6);
|
|
|
|
arrowEnd << QPointF(lastPt.x() + 3, lastPt.y() - 6);
|
|
|
|
arrowEnd << QPointF(lastPt);
|
|
|
|
p.drawConvexPolygon(recalculatePolygon(arrowEnd));
|
|
|
|
}
|
2017-12-13 22:38:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-07 10:53:42 +00:00
|
|
|
p.end();
|
|
|
|
#ifndef QT_NO_OPENGL
|
|
|
|
delete paintDevice;
|
|
|
|
#endif
|
2019-03-12 07:37:10 +00:00
|
|
|
}
|
|
|
|
|
2017-12-13 22:38:46 +00:00
|
|
|
|
2019-02-16 17:17:11 +00:00
|
|
|
void GraphView::center()
|
2017-12-13 22:38:46 +00:00
|
|
|
{
|
2019-02-16 17:17:11 +00:00
|
|
|
centerX();
|
|
|
|
centerY();
|
2017-12-13 22:38:46 +00:00
|
|
|
}
|
|
|
|
|
2019-02-16 17:17:11 +00:00
|
|
|
void GraphView::centerX()
|
2017-12-13 22:38:46 +00:00
|
|
|
{
|
2019-03-23 08:21:06 +00:00
|
|
|
offset.rx() = -((viewport()->width() - width * current_scale) / 2);
|
|
|
|
offset.rx() /= current_scale;
|
2019-02-16 17:17:11 +00:00
|
|
|
}
|
2017-12-13 22:38:46 +00:00
|
|
|
|
2019-02-16 17:17:11 +00:00
|
|
|
void GraphView::centerY()
|
|
|
|
{
|
2019-03-23 08:21:06 +00:00
|
|
|
offset.ry() = -((viewport()->height() - height * current_scale) / 2);
|
|
|
|
offset.ry() /= current_scale;
|
2019-02-16 17:17:11 +00:00
|
|
|
}
|
2017-12-13 22:38:46 +00:00
|
|
|
|
2019-02-16 17:17:11 +00:00
|
|
|
void GraphView::showBlock(GraphBlock &block)
|
|
|
|
{
|
|
|
|
showBlock(&block);
|
|
|
|
}
|
2017-12-13 22:38:46 +00:00
|
|
|
|
2019-02-16 17:17:11 +00:00
|
|
|
void GraphView::showBlock(GraphBlock *block)
|
|
|
|
{
|
|
|
|
if (width * current_scale <= viewport()->width()) {
|
|
|
|
centerX();
|
2017-12-13 22:38:46 +00:00
|
|
|
} else {
|
2019-02-16 17:17:11 +00:00
|
|
|
int render_width = viewport()->width() / current_scale;
|
2019-03-23 08:21:06 +00:00
|
|
|
offset.rx() = block->x - ((render_width - block->width) / 2);
|
2019-02-16 17:17:11 +00:00
|
|
|
}
|
|
|
|
if (height * current_scale <= viewport()->height()) {
|
|
|
|
centerY();
|
|
|
|
} else {
|
2019-03-23 08:21:06 +00:00
|
|
|
offset.ry() = block->y - 30;
|
2017-12-13 22:38:46 +00:00
|
|
|
}
|
|
|
|
blockTransitionedTo(block);
|
2017-12-14 21:07:48 +00:00
|
|
|
viewport()->update();
|
2017-12-13 22:38:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GraphView::addBlock(GraphView::GraphBlock block)
|
|
|
|
{
|
2017-12-14 21:07:48 +00:00
|
|
|
blocks[block.entry] = block;
|
2017-12-13 22:38:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GraphView::setEntry(ut64 e)
|
|
|
|
{
|
2017-12-14 21:07:48 +00:00
|
|
|
entry = e;
|
2017-12-13 22:38:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool GraphView::checkPointClicked(QPointF &point, int x, int y, bool above_y)
|
|
|
|
{
|
|
|
|
int half_target_size = 5;
|
2018-03-21 20:32:32 +00:00
|
|
|
if ((point.x() - half_target_size < x) &&
|
2017-12-13 22:38:46 +00:00
|
|
|
(point.y() - (above_y ? (2 * half_target_size) : 0) < y) &&
|
|
|
|
(x < point.x() + half_target_size) &&
|
2018-03-21 20:32:32 +00:00
|
|
|
(y < point.y() + (above_y ? 0 : (3 * half_target_size)))) {
|
2017-12-13 22:38:46 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mouse events
|
|
|
|
void GraphView::mousePressEvent(QMouseEvent *event)
|
|
|
|
{
|
2019-04-07 11:02:35 +00:00
|
|
|
if (event->button() == Qt::MiddleButton) {
|
|
|
|
beginMouseDrag(event);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-03-23 08:21:06 +00:00
|
|
|
int x = event->pos().x() / current_scale + offset.x();
|
|
|
|
int y = event->pos().y() / current_scale + offset.y();
|
2017-12-13 22:38:46 +00:00
|
|
|
|
|
|
|
// Check if a block was clicked
|
2018-03-21 20:32:32 +00:00
|
|
|
for (auto &blockIt : blocks) {
|
2017-12-13 22:38:46 +00:00
|
|
|
GraphBlock &block = blockIt.second;
|
|
|
|
|
2018-03-21 20:32:32 +00:00
|
|
|
if ((block.x <= x) && (block.y <= y) &&
|
|
|
|
(x <= block.x + block.width) & (y <= block.y + block.height)) {
|
2017-12-13 22:38:46 +00:00
|
|
|
QPoint pos = QPoint(x - block.x, y - block.y);
|
|
|
|
blockClicked(block, event, pos);
|
|
|
|
// Don't do anything else here! blockClicked might seek and
|
|
|
|
// all our data is invalid then.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if a line beginning/end was clicked
|
2018-03-21 20:32:32 +00:00
|
|
|
for (auto &blockIt : blocks) {
|
2017-12-13 22:38:46 +00:00
|
|
|
GraphBlock &block = blockIt.second;
|
2018-03-21 20:32:32 +00:00
|
|
|
for (GraphEdge &edge : block.edges) {
|
|
|
|
if (edge.polyline.length() < 2) {
|
2017-12-13 22:38:46 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
QPointF start = edge.polyline.first();
|
|
|
|
QPointF end = edge.polyline.last();
|
2018-03-21 20:32:32 +00:00
|
|
|
if (checkPointClicked(start, x, y)) {
|
2019-04-04 05:54:42 +00:00
|
|
|
showBlock(blocks[edge.target]);
|
2017-12-13 22:38:46 +00:00
|
|
|
// TODO: Callback to child
|
|
|
|
return;
|
|
|
|
break;
|
|
|
|
}
|
2018-03-21 20:32:32 +00:00
|
|
|
if (checkPointClicked(end, x, y, true)) {
|
2019-02-16 17:17:11 +00:00
|
|
|
showBlock(block);
|
2017-12-13 22:38:46 +00:00
|
|
|
// TODO: Callback to child
|
|
|
|
return;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// No block was clicked
|
2018-03-21 20:32:32 +00:00
|
|
|
if (event->button() == Qt::LeftButton) {
|
2017-12-13 22:38:46 +00:00
|
|
|
//Left click outside any block, enter scrolling mode
|
2019-04-07 11:02:35 +00:00
|
|
|
beginMouseDrag(event);
|
2017-12-13 22:38:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-03-21 20:32:32 +00:00
|
|
|
void GraphView::mouseMoveEvent(QMouseEvent *event)
|
2017-12-13 22:38:46 +00:00
|
|
|
{
|
2018-03-21 20:32:32 +00:00
|
|
|
if (scroll_mode) {
|
2019-03-23 08:21:06 +00:00
|
|
|
offset.rx() += (scroll_base_x - event->x()) / current_scale;
|
|
|
|
offset.ry() += (scroll_base_y - event->y()) / current_scale;
|
2017-12-14 21:07:48 +00:00
|
|
|
scroll_base_x = event->x();
|
|
|
|
scroll_base_y = event->y();
|
2019-02-16 17:17:11 +00:00
|
|
|
viewport()->update();
|
2017-12-14 21:07:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GraphView::mouseDoubleClickEvent(QMouseEvent *event)
|
|
|
|
{
|
2019-03-23 08:21:06 +00:00
|
|
|
int x = event->pos().x() / current_scale + offset.x();
|
|
|
|
int y = event->pos().y() / current_scale + offset.y();
|
2017-12-14 21:07:48 +00:00
|
|
|
|
|
|
|
// Check if a block was clicked
|
2018-03-21 20:32:32 +00:00
|
|
|
for (auto &blockIt : blocks) {
|
2017-12-14 21:07:48 +00:00
|
|
|
GraphBlock &block = blockIt.second;
|
|
|
|
|
2018-03-21 20:32:32 +00:00
|
|
|
if ((block.x <= x) && (block.y <= y) &&
|
|
|
|
(x <= block.x + block.width) & (y <= block.y + block.height)) {
|
2017-12-14 21:07:48 +00:00
|
|
|
QPoint pos = QPoint(x - block.x, y - block.y);
|
|
|
|
blockDoubleClicked(block, event, pos);
|
|
|
|
return;
|
|
|
|
}
|
2017-12-13 22:38:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-21 20:32:32 +00:00
|
|
|
void GraphView::mouseReleaseEvent(QMouseEvent *event)
|
2017-12-13 22:38:46 +00:00
|
|
|
{
|
|
|
|
// TODO
|
|
|
|
// if(event->button() == Qt::ForwardButton)
|
|
|
|
// gotoNextSlot();
|
|
|
|
// else if(event->button() == Qt::BackButton)
|
|
|
|
// gotoPreviousSlot();
|
|
|
|
|
2019-04-07 11:02:35 +00:00
|
|
|
if (scroll_mode && (event->buttons() & (Qt::LeftButton | Qt::MiddleButton)) == 0) {
|
2017-12-14 21:07:48 +00:00
|
|
|
scroll_mode = false;
|
|
|
|
setCursor(Qt::ArrowCursor);
|
|
|
|
viewport()->releaseMouse();
|
2017-12-13 22:38:46 +00:00
|
|
|
}
|
|
|
|
}
|
2018-05-21 17:33:46 +00:00
|
|
|
|
|
|
|
void GraphView::wheelEvent(QWheelEvent *event)
|
|
|
|
{
|
2019-04-07 11:02:35 +00:00
|
|
|
if (scroll_mode) {
|
|
|
|
// With some mice it's easy to hit sideway scroll button while holding middle mouse.
|
|
|
|
// That would result in unwanted scrolling while panning.
|
|
|
|
return;
|
|
|
|
}
|
2019-03-23 08:21:06 +00:00
|
|
|
QPoint delta = -event->angleDelta();
|
|
|
|
|
|
|
|
delta /= current_scale;
|
|
|
|
offset += delta;
|
|
|
|
|
2019-02-16 17:17:11 +00:00
|
|
|
viewport()->update();
|
2018-05-21 17:33:46 +00:00
|
|
|
event->accept();
|
|
|
|
}
|