Commit 15d97051 authored by Aleks Kissinger's avatar Aleks Kissinger

fixed CTRL key bug and added option to auto-select new edges

parent 8a46efa8
......@@ -26,10 +26,11 @@ PreferenceDialog::PreferenceDialog(QWidget *parent) :
setColor(ui->minorColor, settings.value("grid-color-minor",
QColor(250,250,255)).value<QColor>());
connect(ui->axesColor, SIGNAL(clicked()), this, SLOT(colorClick()));
connect(ui->majorColor, SIGNAL(clicked()), this, SLOT(colorClick()));
connect(ui->minorColor, SIGNAL(clicked()), this, SLOT(colorClick()));
ui->selectNewEdges->setChecked(settings.value("select-new-edges", false).toBool());
}
PreferenceDialog::~PreferenceDialog()
......@@ -45,6 +46,7 @@ void PreferenceDialog::accept()
settings.setValue("grid-color-axes", color(ui->axesColor));
settings.setValue("grid-color-major", color(ui->majorColor));
settings.setValue("grid-color-minor", color(ui->minorColor));
settings.setValue("select-new-edges", ui->selectNewEdges->isChecked());
QDialog::accept();
}
......
......@@ -6,7 +6,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>345</width>
<width>400</width>
<height>176</height>
</rect>
</property>
......@@ -220,6 +220,20 @@
</property>
</widget>
</item>
<item row="4" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string>Auto-select new edges</string>
</property>
</widget>
</item>
<item row="4" column="1">
<widget class="QCheckBox" name="selectNewEdges">
<property name="text">
<string/>
</property>
</widget>
</item>
</layout>
</item>
<item>
......
......@@ -67,6 +67,9 @@ TikzScene::TikzScene(TikzDocument *tikzDocument, ToolPalette *tools,
_rubberBandItem->setVisible(false);
addItem(_rubberBandItem);
_highlightHeads = false;
_highlightTails = false;
}
TikzScene::~TikzScene() {
......@@ -509,6 +512,7 @@ void TikzScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
void TikzScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
if (!_enabled) return;
QSettings settings("tikzit", "tikzit");
// current mouse position, in scene coordinates
QPointF mousePos = event->scenePos();
......@@ -593,7 +597,13 @@ void TikzScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
if (_edgeStartNodeItem != nullptr && _edgeEndNodeItem != nullptr) {
Edge *e = new Edge(_edgeStartNodeItem->node(), _edgeEndNodeItem->node(), _tikzDocument);
e->setStyleName(_styles->activeEdgeStyleName());
AddEdgeCommand *cmd = new AddEdgeCommand(this, e);
bool selectEdge = settings.value("select-new-edges", false).toBool();
QSet<Node*> selNodes;
QSet<Edge*> selEdges;
if (selectEdge) getSelection(selNodes, selEdges);
AddEdgeCommand *cmd = new AddEdgeCommand(this, e, selectEdge,
selNodes, selEdges);
_tikzDocument->undoStack()->push(cmd);
}
_edgeStartNodeItem = nullptr;
......@@ -618,12 +628,17 @@ void TikzScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
void TikzScene::keyReleaseEvent(QKeyEvent *event)
{
//qDebug() << "keyrelease:" << QString::number(event->key(), 16);
//qDebug() << "modifiers:" << QString::number(QApplication::queryKeyboardModifiers(), 16);
if (!_enabled) return;
// slower, but seems to be more reliable than event->modifiers()
Qt::KeyboardModifiers mod = QApplication::queryKeyboardModifiers();
// clear highlighting for edge bends (if there was any)
if (event->modifiers() & Qt::ControlModifier) {
if (mod & Qt::ControlModifier) {
// it could be the case the user has released shift and is still holding control
bool head = !(event->modifiers() & Qt::ShiftModifier);
bool head = !(mod & Qt::ShiftModifier);
_highlightHeads = head;
_highlightTails = !head;
} else {
......@@ -634,7 +649,7 @@ void TikzScene::keyReleaseEvent(QKeyEvent *event)
if (event->key() == Qt::Key_Backspace || event->key() == Qt::Key_Delete) {
deleteSelectedItems();
} else if (event->modifiers() == Qt::NoModifier) {
} else if (mod == Qt::NoModifier) {
switch(event->key()) {
case Qt::Key_S:
tikzit->activeWindow()->toolPalette()->setCurrentTool(ToolPalette::SELECT);
......@@ -657,21 +672,26 @@ void TikzScene::keyReleaseEvent(QKeyEvent *event)
void TikzScene::keyPressEvent(QKeyEvent *event)
{
//qDebug() << "keypress:" << QString::number(event->key(), 16);
//qDebug() << "modifiers:" << QString::number(QApplication::queryKeyboardModifiers(), 16);
bool capture = false;
// slower, but seems to be more reliable than event->modifiers()
Qt::KeyboardModifiers mod = QApplication::queryKeyboardModifiers();
if (event->key() == Qt::Key_QuoteLeft) {
capture = true;
_styles->nextNodeStyle();
}
if (event->modifiers() & Qt::ControlModifier) {
if (mod & Qt::ControlModifier) {
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;
qreal shift = (mod & Qt::ShiftModifier) ? 1.0 : 10.0;
switch(event->key()) {
case Qt::Key_Left:
delta.setX(-0.025 * shift);
......@@ -708,7 +728,7 @@ void TikzScene::keyPressEvent(QKeyEvent *event)
int deltaAngle = 0;
qreal deltaWeight = 0.0;
bool head = !(event->modifiers() & Qt::ShiftModifier);
bool head = !(mod & Qt::ShiftModifier);
_highlightHeads = head;
_highlightTails = !head;
......@@ -829,12 +849,12 @@ void TikzScene::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
bool TikzScene::highlightTails() const
{
return _highlightTails;
return _highlightTails && getSelectedNodes().isEmpty();
}
bool TikzScene::highlightHeads() const
{
return _highlightHeads;
return _highlightHeads && getSelectedNodes().isEmpty();
}
bool TikzScene::enabled() const
......@@ -983,7 +1003,7 @@ void TikzScene::rotateNodes(bool clockwise)
}
void TikzScene::getSelection(QSet<Node *> &selNodes, QSet<Edge *> &selEdges)
void TikzScene::getSelection(QSet<Node *> &selNodes, QSet<Edge *> &selEdges) const
{
foreach (QGraphicsItem *gi, selectedItems()) {
if (NodeItem *ni = dynamic_cast<NodeItem*>(gi)) selNodes << ni->node();
......@@ -991,7 +1011,7 @@ void TikzScene::getSelection(QSet<Node *> &selNodes, QSet<Edge *> &selEdges)
}
}
QSet<Node *> TikzScene::getSelectedNodes()
QSet<Node *> TikzScene::getSelectedNodes() const
{
QSet<Node*> selNodes;
foreach (QGraphicsItem *gi, selectedItems()) {
......
......@@ -80,8 +80,9 @@ public:
void reverseSelectedEdges();
void getSelection(QSet<Node*> &selNodes, QSet<Edge*> &selEdges);
QSet<Node*> getSelectedNodes();
void getSelection(QSet<Node*> &selNodes, QSet<Edge*> &selEdges) const;
QSet<Node*> getSelectedNodes() const;
void refreshSceneBounds();
bool highlightHeads() const;
......
......@@ -20,6 +20,7 @@
#include "nodeitem.h"
#include "edgeitem.h"
#include <QApplication>
#include <QGraphicsView>
GraphUpdateCommand::GraphUpdateCommand(TikzScene *scene, QUndoCommand *parent) : QUndoCommand(parent), _scene(scene)
......@@ -217,8 +218,14 @@ void AddNodeCommand::redo()
GraphUpdateCommand::redo();
}
AddEdgeCommand::AddEdgeCommand(TikzScene *scene, Edge *edge, QUndoCommand *parent) :
GraphUpdateCommand(scene, parent), _edge(edge)
AddEdgeCommand::AddEdgeCommand(TikzScene *scene,
Edge *edge,
bool selectEdge,
QSet<Node*> selNodes,
QSet<Edge*> selEdges,
QUndoCommand *parent) :
GraphUpdateCommand(scene, parent), _edge(edge),
_selectEdge(selectEdge), _selNodes(selNodes), _selEdges(selEdges)
{
}
......@@ -231,24 +238,38 @@ void AddEdgeCommand::undo()
_scene->graph()->removeEdge(_edge);
_scene->refreshZIndices();
if (_selectEdge) {
foreach (NodeItem *ni, _scene->nodeItems()) {
ni->setSelected(_selNodes.contains(ni->node()));
}
foreach (EdgeItem *ei, _scene->edgeItems()) {
ei->setSelected(_selEdges.contains(ei->edge()));
}
}
GraphUpdateCommand::undo();
}
void AddEdgeCommand::redo()
{
_edge->attachStyle(); // do for every redo, in case styles have changed
_edge->attachStyle(); // do for every redo, in case styles have changed
_scene->graph()->addEdge(_edge);
EdgeItem *ei = new EdgeItem(_edge);
_scene->edgeItems().insert(_edge, ei);
_scene->addItem(ei);
// TODO: deal consistently with stacking order
// edges should always be stacked below nodes
if (!_scene->graph()->nodes().isEmpty()) {
ei->stackBefore(_scene->nodeItems()[_scene->graph()->nodes().first()]);
}
_scene->refreshZIndices();
if (_selectEdge) {
_scene->clearSelection();
ei->setSelected(true);
}
GraphUpdateCommand::redo();
}
......
......@@ -121,11 +121,14 @@ private:
class AddEdgeCommand : public GraphUpdateCommand
{
public:
explicit AddEdgeCommand(TikzScene *scene, Edge *edge, QUndoCommand *parent = nullptr);
explicit AddEdgeCommand(TikzScene *scene, Edge *edge, bool selectEdge, QSet<Node *> selNodes, QSet<Edge *> selEdges, QUndoCommand *parent = nullptr);
void undo() override;
void redo() override;
private:
bool _selectEdge;
Edge *_edge;
QSet<Node*> _selNodes;
QSet<Edge*> _selEdges;
};
class ChangeEdgeModeCommand : public GraphUpdateCommand
......
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