[Companion] Unix format used everywhere
This commit is contained in:
+208
-208
@@ -18,213 +18,213 @@
|
||||
* GNU General Public License for more details.
|
||||
*/
|
||||
|
||||
#include <QGraphicsScene>
|
||||
#include <QGraphicsSceneMouseEvent>
|
||||
#include <QPainter>
|
||||
#include <QStyleOption>
|
||||
|
||||
#include "edge.h"
|
||||
#include "node.h"
|
||||
|
||||
Node::Node():
|
||||
minX(-100),
|
||||
maxX(+100)
|
||||
{
|
||||
setFlag(ItemIsMovable);
|
||||
setFlag(ItemSendsGeometryChanges);
|
||||
setCacheMode(DeviceCoordinateCache);
|
||||
setZValue(-1);
|
||||
nodecolor = QColor(Qt::yellow);
|
||||
bPressed = false;
|
||||
centerX = true;
|
||||
centerY = true;
|
||||
fixedX = false;
|
||||
fixedY = false;
|
||||
ballSize = DEFAULT_BALL_SIZE;
|
||||
}
|
||||
|
||||
void Node::addEdge(Edge *edge)
|
||||
{
|
||||
edgeList << edge;
|
||||
edge->adjust();
|
||||
}
|
||||
|
||||
void Node::stepToCenter(qreal step)
|
||||
{
|
||||
if(scene() && !bPressed)
|
||||
{
|
||||
QRectF qr = scene()->sceneRect();
|
||||
qreal xStep = qr.width()/step;
|
||||
qreal yStep = qr.height()/step;
|
||||
|
||||
QPointF p = pos();
|
||||
if(centerX)
|
||||
{
|
||||
qreal x = qr.right() - qr.width()/2;
|
||||
#include <QGraphicsScene>
|
||||
#include <QGraphicsSceneMouseEvent>
|
||||
#include <QPainter>
|
||||
#include <QStyleOption>
|
||||
|
||||
#include "edge.h"
|
||||
#include "node.h"
|
||||
|
||||
Node::Node():
|
||||
minX(-100),
|
||||
maxX(+100)
|
||||
{
|
||||
setFlag(ItemIsMovable);
|
||||
setFlag(ItemSendsGeometryChanges);
|
||||
setCacheMode(DeviceCoordinateCache);
|
||||
setZValue(-1);
|
||||
nodecolor = QColor(Qt::yellow);
|
||||
bPressed = false;
|
||||
centerX = true;
|
||||
centerY = true;
|
||||
fixedX = false;
|
||||
fixedY = false;
|
||||
ballSize = DEFAULT_BALL_SIZE;
|
||||
}
|
||||
|
||||
void Node::addEdge(Edge *edge)
|
||||
{
|
||||
edgeList << edge;
|
||||
edge->adjust();
|
||||
}
|
||||
|
||||
void Node::stepToCenter(qreal step)
|
||||
{
|
||||
if(scene() && !bPressed)
|
||||
{
|
||||
QRectF qr = scene()->sceneRect();
|
||||
qreal xStep = qr.width()/step;
|
||||
qreal yStep = qr.height()/step;
|
||||
|
||||
QPointF p = pos();
|
||||
if(centerX)
|
||||
{
|
||||
qreal x = qr.right() - qr.width()/2;
|
||||
if(fabs(x-p.x())>xStep)
|
||||
p.rx() += p.x()>x ? -xStep : xStep;
|
||||
else
|
||||
p.rx() = x;
|
||||
}
|
||||
|
||||
if(centerY)
|
||||
{
|
||||
qreal y = qr.bottom() - qr.height()/2;
|
||||
p.rx() += p.x()>x ? -xStep : xStep;
|
||||
else
|
||||
p.rx() = x;
|
||||
}
|
||||
|
||||
if(centerY)
|
||||
{
|
||||
qreal y = qr.bottom() - qr.height()/2;
|
||||
if(fabs(y-p.y())>yStep)
|
||||
p.ry() += p.y()>y ? -yStep : yStep;
|
||||
else
|
||||
p.ry() = y;
|
||||
}
|
||||
setPos(p);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void Node::setColor(const QColor & color)
|
||||
{
|
||||
nodecolor=color;
|
||||
}
|
||||
|
||||
|
||||
void Node::setBallSize(int size)
|
||||
{
|
||||
if(size>2) ballSize = size;
|
||||
}
|
||||
|
||||
qreal Node::getX()
|
||||
{
|
||||
if(scene())
|
||||
{
|
||||
QRectF rect = scene()->sceneRect();
|
||||
return 1+((x()-rect.right())*2/rect.width());
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void Node::setX(qreal newX)
|
||||
{
|
||||
if(scene())
|
||||
{
|
||||
QRectF rect = scene()->sceneRect();
|
||||
QGraphicsObject::setX(rect.left() + (newX+1) * rect.width()/2);
|
||||
}
|
||||
}
|
||||
|
||||
qreal Node::getY()
|
||||
{
|
||||
if(scene())
|
||||
{
|
||||
QRectF rect = scene()->sceneRect();
|
||||
return 1+((y()-rect.bottom())*2/rect.height());
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void Node::setY(qreal newY)
|
||||
{
|
||||
if(scene())
|
||||
{
|
||||
QRectF rect = scene()->sceneRect();
|
||||
QGraphicsObject::setY(rect.top() + (newY+1) * rect.height()/2);
|
||||
}
|
||||
}
|
||||
|
||||
QList<Edge *> Node::edges() const
|
||||
{
|
||||
return edgeList;
|
||||
}
|
||||
|
||||
QRectF Node::boundingRect() const
|
||||
{
|
||||
qreal adjust = 2;
|
||||
return QRectF(-ballSize/2 - adjust, -ballSize/2 - adjust,
|
||||
ballSize+BALL_HEIGHT + adjust, ballSize+BALL_HEIGHT + adjust);
|
||||
}
|
||||
|
||||
|
||||
QPainterPath Node::shape() const
|
||||
{
|
||||
QPainterPath path;
|
||||
path.addEllipse(-ballSize/2, -ballSize/2, ballSize, ballSize);
|
||||
return path;
|
||||
}
|
||||
|
||||
void Node::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
|
||||
{
|
||||
painter->setPen(Qt::NoPen);
|
||||
painter->setBrush(Qt::darkGray);
|
||||
painter->drawEllipse(-ballSize/2 + BALL_HEIGHT, -ballSize/2 + BALL_HEIGHT, ballSize, ballSize);
|
||||
|
||||
QRadialGradient gradient(-BALL_HEIGHT, -BALL_HEIGHT, ballSize/2);
|
||||
if (option->state & QStyle::State_Sunken) {
|
||||
gradient.setCenter(BALL_HEIGHT, BALL_HEIGHT);
|
||||
gradient.setFocalPoint(BALL_HEIGHT, BALL_HEIGHT);
|
||||
gradient.setColorAt(1, nodecolor.light(180));
|
||||
gradient.setColorAt(0, nodecolor.light(120));
|
||||
} else {
|
||||
gradient.setColorAt(0, nodecolor);
|
||||
gradient.setColorAt(1, nodecolor.dark(120));
|
||||
}
|
||||
painter->setBrush(gradient);
|
||||
painter->setPen(QPen(Qt::black, 0));
|
||||
painter->drawEllipse(-ballSize/2, -ballSize/2, ballSize, ballSize);
|
||||
}
|
||||
|
||||
QVariant Node::itemChange(GraphicsItemChange change, const QVariant &value)
|
||||
{
|
||||
switch (change) {
|
||||
case ItemPositionChange:
|
||||
if (scene())
|
||||
{
|
||||
// value is the new position.
|
||||
QPointF newPos = value.toPointF();
|
||||
QRectF rect = scene()->sceneRect();
|
||||
if(fixedX) newPos.setX(x());//make sure x doesn't change
|
||||
if(fixedY) newPos.setY(y());//make sure x doesn't change
|
||||
newPos.setX(qMin(rect.right(), qMax(newPos.x(), rect.left())));// bound X
|
||||
newPos.setY(qMin(rect.bottom(), qMax(newPos.y(), rect.top())));// bound Y
|
||||
|
||||
int newX = -100 + ( (newPos.x()-rect.left()) * 200) / rect.width();
|
||||
int newY = 100+(rect.top()-y())*200/rect.height();
|
||||
if (newX < minX) newX = minX;
|
||||
if (newX > maxX) newX = maxX;
|
||||
|
||||
if (!getFixedX()) {
|
||||
newPos.setX(((newX+100)*rect.width()/200+rect.left()));
|
||||
}
|
||||
|
||||
emit moved(newX, newY);
|
||||
return newPos;
|
||||
}
|
||||
break;
|
||||
case ItemPositionHasChanged:
|
||||
foreach (Edge *edge, edgeList)
|
||||
edge->adjust();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
};
|
||||
|
||||
return QGraphicsItem::itemChange(change, value);
|
||||
}
|
||||
|
||||
void Node::mousePressEvent(QGraphicsSceneMouseEvent *event)
|
||||
{
|
||||
update();
|
||||
bPressed = true;
|
||||
QGraphicsItem::mousePressEvent(event);
|
||||
emit focus();
|
||||
}
|
||||
|
||||
void Node::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
|
||||
{
|
||||
update();
|
||||
bPressed = false;
|
||||
if (scene()) {
|
||||
QGraphicsItem::mouseReleaseEvent(event);
|
||||
emit unfocus();
|
||||
}
|
||||
else {
|
||||
QGraphicsItem::mouseReleaseEvent(event);
|
||||
}
|
||||
}
|
||||
p.ry() += p.y()>y ? -yStep : yStep;
|
||||
else
|
||||
p.ry() = y;
|
||||
}
|
||||
setPos(p);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void Node::setColor(const QColor & color)
|
||||
{
|
||||
nodecolor=color;
|
||||
}
|
||||
|
||||
|
||||
void Node::setBallSize(int size)
|
||||
{
|
||||
if(size>2) ballSize = size;
|
||||
}
|
||||
|
||||
qreal Node::getX()
|
||||
{
|
||||
if(scene())
|
||||
{
|
||||
QRectF rect = scene()->sceneRect();
|
||||
return 1+((x()-rect.right())*2/rect.width());
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void Node::setX(qreal newX)
|
||||
{
|
||||
if(scene())
|
||||
{
|
||||
QRectF rect = scene()->sceneRect();
|
||||
QGraphicsObject::setX(rect.left() + (newX+1) * rect.width()/2);
|
||||
}
|
||||
}
|
||||
|
||||
qreal Node::getY()
|
||||
{
|
||||
if(scene())
|
||||
{
|
||||
QRectF rect = scene()->sceneRect();
|
||||
return 1+((y()-rect.bottom())*2/rect.height());
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void Node::setY(qreal newY)
|
||||
{
|
||||
if(scene())
|
||||
{
|
||||
QRectF rect = scene()->sceneRect();
|
||||
QGraphicsObject::setY(rect.top() + (newY+1) * rect.height()/2);
|
||||
}
|
||||
}
|
||||
|
||||
QList<Edge *> Node::edges() const
|
||||
{
|
||||
return edgeList;
|
||||
}
|
||||
|
||||
QRectF Node::boundingRect() const
|
||||
{
|
||||
qreal adjust = 2;
|
||||
return QRectF(-ballSize/2 - adjust, -ballSize/2 - adjust,
|
||||
ballSize+BALL_HEIGHT + adjust, ballSize+BALL_HEIGHT + adjust);
|
||||
}
|
||||
|
||||
|
||||
QPainterPath Node::shape() const
|
||||
{
|
||||
QPainterPath path;
|
||||
path.addEllipse(-ballSize/2, -ballSize/2, ballSize, ballSize);
|
||||
return path;
|
||||
}
|
||||
|
||||
void Node::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
|
||||
{
|
||||
painter->setPen(Qt::NoPen);
|
||||
painter->setBrush(Qt::darkGray);
|
||||
painter->drawEllipse(-ballSize/2 + BALL_HEIGHT, -ballSize/2 + BALL_HEIGHT, ballSize, ballSize);
|
||||
|
||||
QRadialGradient gradient(-BALL_HEIGHT, -BALL_HEIGHT, ballSize/2);
|
||||
if (option->state & QStyle::State_Sunken) {
|
||||
gradient.setCenter(BALL_HEIGHT, BALL_HEIGHT);
|
||||
gradient.setFocalPoint(BALL_HEIGHT, BALL_HEIGHT);
|
||||
gradient.setColorAt(1, nodecolor.light(180));
|
||||
gradient.setColorAt(0, nodecolor.light(120));
|
||||
} else {
|
||||
gradient.setColorAt(0, nodecolor);
|
||||
gradient.setColorAt(1, nodecolor.dark(120));
|
||||
}
|
||||
painter->setBrush(gradient);
|
||||
painter->setPen(QPen(Qt::black, 0));
|
||||
painter->drawEllipse(-ballSize/2, -ballSize/2, ballSize, ballSize);
|
||||
}
|
||||
|
||||
QVariant Node::itemChange(GraphicsItemChange change, const QVariant &value)
|
||||
{
|
||||
switch (change) {
|
||||
case ItemPositionChange:
|
||||
if (scene())
|
||||
{
|
||||
// value is the new position.
|
||||
QPointF newPos = value.toPointF();
|
||||
QRectF rect = scene()->sceneRect();
|
||||
if(fixedX) newPos.setX(x());//make sure x doesn't change
|
||||
if(fixedY) newPos.setY(y());//make sure x doesn't change
|
||||
newPos.setX(qMin(rect.right(), qMax(newPos.x(), rect.left())));// bound X
|
||||
newPos.setY(qMin(rect.bottom(), qMax(newPos.y(), rect.top())));// bound Y
|
||||
|
||||
int newX = -100 + ( (newPos.x()-rect.left()) * 200) / rect.width();
|
||||
int newY = 100+(rect.top()-y())*200/rect.height();
|
||||
if (newX < minX) newX = minX;
|
||||
if (newX > maxX) newX = maxX;
|
||||
|
||||
if (!getFixedX()) {
|
||||
newPos.setX(((newX+100)*rect.width()/200+rect.left()));
|
||||
}
|
||||
|
||||
emit moved(newX, newY);
|
||||
return newPos;
|
||||
}
|
||||
break;
|
||||
case ItemPositionHasChanged:
|
||||
foreach (Edge *edge, edgeList)
|
||||
edge->adjust();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
};
|
||||
|
||||
return QGraphicsItem::itemChange(change, value);
|
||||
}
|
||||
|
||||
void Node::mousePressEvent(QGraphicsSceneMouseEvent *event)
|
||||
{
|
||||
update();
|
||||
bPressed = true;
|
||||
QGraphicsItem::mousePressEvent(event);
|
||||
emit focus();
|
||||
}
|
||||
|
||||
void Node::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
|
||||
{
|
||||
update();
|
||||
bPressed = false;
|
||||
if (scene()) {
|
||||
QGraphicsItem::mouseReleaseEvent(event);
|
||||
emit unfocus();
|
||||
}
|
||||
else {
|
||||
QGraphicsItem::mouseReleaseEvent(event);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user