porymap/src/ui/connectionpixmapitem.cpp

89 lines
2.6 KiB
C++
Raw Normal View History

2018-09-27 00:30:05 +01:00
#include "connectionpixmapitem.h"
#include <math.h>
2018-09-27 00:30:05 +01:00
2024-07-12 19:05:37 +01:00
void ConnectionPixmapItem::render() {
QPixmap pixmap = this->basePixmap.copy(0, 0, this->basePixmap.width(), this->basePixmap.height());
this->setZValue(-1);
// When editing is inactive the current selection is ignored, all connections should appear normal.
if (this->getEditable()) {
if (this->selected) {
// Draw highlight
QPainter painter(&pixmap);
painter.setPen(QColor(255, 0, 255));
painter.drawRect(0, 0, pixmap.width() - 1, pixmap.height() - 1);
painter.end();
} else {
// Darken the image
this->setZValue(-2);
QPainter painter(&pixmap);
int alpha = static_cast<int>(255 * 0.25);
painter.fillRect(0, 0, pixmap.width(), pixmap.height(), QColor(0, 0, 0, alpha));
painter.end();
}
2018-09-27 00:30:05 +01:00
}
2024-07-12 19:05:37 +01:00
this->setPixmap(pixmap);
2018-09-27 00:30:05 +01:00
}
QVariant ConnectionPixmapItem::itemChange(GraphicsItemChange change, const QVariant &value)
{
if (change == ItemPositionChange) {
QPointF newPos = value.toPointF();
qreal x, y;
int newOffset = this->initialOffset;
2024-08-04 21:08:16 +01:00
if (MapConnection::isVertical(this->connection->direction())) {
2018-09-27 00:30:05 +01:00
x = round(newPos.x() / 16) * 16;
newOffset += (x - initialX) / 16;
x = newOffset * 16;
}
else {
x = this->initialX;
}
2024-08-04 21:08:16 +01:00
if (MapConnection::isHorizontal(this->connection->direction())) {
2018-09-27 00:30:05 +01:00
y = round(newPos.y() / 16) * 16;
newOffset += (y - this->initialY) / 16;
y = newOffset * 16;
}
else {
y = this->initialY;
}
2024-08-04 21:08:16 +01:00
if (this->connection->offset() != newOffset)
2024-07-08 21:01:30 +01:00
emit connectionMoved(this->connection, newOffset);
2018-09-27 00:30:05 +01:00
return QPointF(x, y);
}
else {
return QGraphicsItem::itemChange(change, value);
}
}
void ConnectionPixmapItem::setEditable(bool editable) {
setFlag(ItemIsMovable, editable);
setFlag(ItemSendsGeometryChanges, editable);
}
bool ConnectionPixmapItem::getEditable() {
return (this->flags() & ItemIsMovable) != 0;
}
2024-07-12 19:05:37 +01:00
void ConnectionPixmapItem::setSelected(bool selected) {
if (this->selected == selected)
return;
2024-07-12 19:05:37 +01:00
this->selected = selected;
this->render();
emit selectionChanged(selected);
}
2018-09-27 00:30:05 +01:00
void ConnectionPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *) {
if (!this->getEditable())
return;
2024-07-12 19:05:37 +01:00
this->setSelected(true);
2018-09-27 00:30:05 +01:00
}
void ConnectionPixmapItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *) {
emit connectionItemDoubleClicked(this);
}