Commit 2666275e authored by Aleks Kissinger's avatar Aleks Kissinger

keyboard edge bend

parent 6db2bfd2
......@@ -149,7 +149,7 @@ void EdgeItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidge
painter->setPen(QPen(draw1));
float r = GLOBAL_SCALEF * _edge->cpDist();
qreal r = GLOBAL_SCALEF * _edge->cpDist();
painter->drawEllipse(toScreen(_edge->source()->point()), r, r);
painter->drawEllipse(toScreen(_edge->target()->point()), r, r);
......@@ -159,6 +159,24 @@ void EdgeItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidge
painter->drawLine(toScreen(_edge->tail()), toScreen(_edge->cp1()));
painter->drawLine(toScreen(_edge->head()), toScreen(_edge->cp2()));
if (scene()) {
TikzScene *sc = static_cast<TikzScene*>(scene());
painter->setFont(Tikzit::LABEL_FONT);
QFontMetrics fm(Tikzit::LABEL_FONT);
QRectF rect = fm.boundingRect("<>");
if (sc->highlightHeads()) {
QPointF headMark(_edge->head().x(), _edge->head().y() + _edge->cpDist() - 0.25);
rect.moveCenter(toScreen(headMark));
painter->drawText(rect, Qt::AlignCenter, "<>");
} else if (sc->highlightTails()) {
QPointF tailMark(_edge->tail().x(), _edge->tail().y() + _edge->cpDist() - 0.25);
rect.moveCenter(toScreen(tailMark));
painter->drawText(rect, Qt::AlignCenter, "<>");
}
}
//painter->drawEllipse(toScreen(_edge->cp1()), r, r);
//painter->drawEllipse(toScreen(_edge->cp2()), r, r);
......
......@@ -583,6 +583,18 @@ void TikzScene::keyReleaseEvent(QKeyEvent *event)
{
if (!_enabled) return;
// clear highlighting for edge bends (if there was any)
if (event->modifiers() & Qt::ControlModifier) {
// it could be the case the user has released shift and is still holding control
bool head = !(event->modifiers() & Qt::ShiftModifier);
_highlightHeads = head;
_highlightTails = !head;
} else {
_highlightHeads = false;
_highlightTails = false;
}
if (event->key() == Qt::Key_Backspace || event->key() == Qt::Key_Delete) {
deleteSelectedItems();
} else if (event->modifiers() == Qt::NoModifier) {
......@@ -602,6 +614,8 @@ void TikzScene::keyReleaseEvent(QKeyEvent *event)
break;
}
}
foreach (QGraphicsItem *it, selectedItems()) it->update();
}
void TikzScene::keyPressEvent(QKeyEvent *event)
......@@ -614,42 +628,113 @@ void TikzScene::keyPressEvent(QKeyEvent *event)
}
if (event->modifiers() & Qt::ControlModifier) {
QPointF delta(0,0);
qreal shift = (event->modifiers() & Qt::ShiftModifier) ? 1.0 : 10.0;
switch(event->key()) {
case Qt::Key_Left:
delta.setX(-0.025 * shift);
break;
case Qt::Key_Right:
delta.setX(0.025 * shift);
break;
case Qt::Key_Up:
delta.setY(0.025 * shift);
break;
case Qt::Key_Down:
delta.setY(-0.025 * shift);
break;
}
QSet<Node*> selNodes;
QSet<Edge*> selEdges;
getSelection(selNodes, selEdges);
if (!selNodes.isEmpty()) {
QPointF delta(0,0);
qreal shift = (event->modifiers() & Qt::ShiftModifier) ? 1.0 : 10.0;
switch(event->key()) {
case Qt::Key_Left:
delta.setX(-0.025 * shift);
break;
case Qt::Key_Right:
delta.setX(0.025 * shift);
break;
case Qt::Key_Up:
delta.setY(0.025 * shift);
break;
case Qt::Key_Down:
delta.setY(-0.025 * shift);
break;
}
if (!delta.isNull()) {
capture = true;
QMap<Node*,QPointF> oldNodePositions;
QMap<Node*,QPointF> newNodePositions;
QPointF pos;
if (!delta.isNull()) {
capture = true;
QMap<Node*,QPointF> oldNodePositions;
QMap<Node*,QPointF> newNodePositions;
QPointF pos;
foreach (QGraphicsItem *gi, selectedItems()) {
if (NodeItem *ni = dynamic_cast<NodeItem*>(gi)) {
pos = ni->node()->point();
oldNodePositions.insert(ni->node(), pos);
newNodePositions.insert(ni->node(), pos + delta);
foreach (QGraphicsItem *gi, selectedItems()) {
if (NodeItem *ni = dynamic_cast<NodeItem*>(gi)) {
pos = ni->node()->point();
oldNodePositions.insert(ni->node(), pos);
newNodePositions.insert(ni->node(), pos + delta);
}
}
MoveCommand *cmd = new MoveCommand(this, oldNodePositions, newNodePositions);
_tikzDocument->undoStack()->push(cmd);
}
} else if (!selEdges.isEmpty()) {
int deltaAngle = 0;
MoveCommand *cmd = new MoveCommand(this, oldNodePositions, newNodePositions);
_tikzDocument->undoStack()->push(cmd);
bool head = !(event->modifiers() & Qt::ShiftModifier);
_highlightHeads = head;
_highlightTails = !head;
switch(event->key()) {
case Qt::Key_Left:
deltaAngle = 15;
//head = true;
break;
case Qt::Key_Right:
deltaAngle = -15;
//head = true;
break;
// case Qt::Key_Down:
// deltaAngle = -15;
// head = false;
// break;
// case Qt::Key_Up:
// deltaAngle = 15;
// head = false;
// break;
}
if (deltaAngle != 0) {
capture = true;
_tikzDocument->undoStack()->beginMacro("Bend edges");
// shift bend by deltaAngle or -deltaAngle (see below)
int sign = 1;
foreach (Edge *e, selEdges) {
if (e->basicBendMode()) {
_tikzDocument->undoStack()->push(new ChangeEdgeModeCommand(this, e));
}
if (head) {
int oldInAngle = e->inAngle();
e->setInAngle(oldInAngle + sign * deltaAngle);
EdgeBendCommand *cmd = new EdgeBendCommand(this, e,
e->weight(),
e->bend(),
oldInAngle,
e->outAngle());
_tikzDocument->undoStack()->push(cmd);
} else {
int oldOutAngle = e->outAngle();
e->setOutAngle(oldOutAngle + sign * deltaAngle);
EdgeBendCommand *cmd = new EdgeBendCommand(this, e,
e->weight(),
e->bend(),
e->inAngle(),
oldOutAngle);
_tikzDocument->undoStack()->push(cmd);
}
// in the special case where 2 edges are selected, bend in opposite directions
if (selEdges.size() == 2) sign *= -1;
}
_tikzDocument->undoStack()->endMacro();
}
}
}
foreach (QGraphicsItem *it, selectedItems()) it->update();
if (!capture) QGraphicsScene::keyPressEvent(event);
}
......@@ -689,6 +774,16 @@ void TikzScene::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
}
}
bool TikzScene::highlightTails() const
{
return _highlightTails;
}
bool TikzScene::highlightHeads() const
{
return _highlightHeads;
}
bool TikzScene::enabled() const
{
return _enabled;
......
......@@ -82,6 +82,10 @@ public:
QSet<Node*> getSelectedNodes();
void refreshSceneBounds();
bool highlightHeads() const;
bool highlightTails() const;
public slots:
void graphReplaced();
void refreshZIndices();
......@@ -114,6 +118,9 @@ private:
int _oldInAngle;
int _oldOutAngle;
bool _enabled;
bool _highlightHeads;
bool _highlightTails;
};
#endif // TIKZSCENE_H
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment