refactor, pulling common code to function, when inserting a new node in hint mode the new node gets a number too

master
Denes Matetelki 14 years ago
parent b333f16b6d
commit baf944f8de

@ -144,6 +144,24 @@ void GraphWidget::keyPressEvent(QKeyEvent *event)
return; return;
} }
// certain actions need an active node
if (!m_activeNode &&
(event->key() == Qt::Key_Insert || // add new node
event->key() == Qt::Key_F2 || // edit node
event->key() == Qt::Key_Delete || // delete node
event->key() == Qt::Key_A || // add edge
event->key() == Qt::Key_D || // remove edge
( event->modifiers() == Qt::ControlModifier && // moving node
( event->key() == Qt::Key_Up ||
event->key() == Qt::Key_Down ||
event->key() == Qt::Key_Left ||
event->key() == Qt::Key_Right))))
{
dynamic_cast<MainWindow *>(m_parent)->getStatusBar()->showMessage(
tr("No active node."),
3000); // millisec
return;
}
switch (event->key()) switch (event->key())
{ {
@ -155,14 +173,14 @@ void GraphWidget::keyPressEvent(QKeyEvent *event)
m_edgeAdding = false; m_edgeAdding = false;
dynamic_cast<MainWindow *>(m_parent)->getStatusBar()->showMessage( dynamic_cast<MainWindow *>(m_parent)->getStatusBar()->showMessage(
tr("Edge adding cancelled"), tr("Edge adding cancelled"),
5000); // millisec 3000); // millisec
} }
else if (m_edgeDeleting) else if (m_edgeDeleting)
{ {
m_edgeDeleting = false; m_edgeDeleting = false;
dynamic_cast<MainWindow *>(m_parent)->getStatusBar()->showMessage( dynamic_cast<MainWindow *>(m_parent)->getStatusBar()->showMessage(
tr("Edge deleting cancelled"), tr("Edge deleting cancelled"),
5000); // millisec 3000); // millisec
} }
else if(m_showingNodeNumbers) else if(m_showingNodeNumbers)
{ {
@ -179,22 +197,13 @@ void GraphWidget::keyPressEvent(QKeyEvent *event)
case Qt::Key_Left: case Qt::Key_Left:
case Qt::Key_Right: case Qt::Key_Right:
if (event->modifiers() == Qt::ControlModifier) if (event->modifiers() == Qt::ControlModifier)
{
if (m_activeNode)
{ {
if (event->key() == Qt::Key_Up) m_activeNode->moveBy(0, -20); if (event->key() == Qt::Key_Up) m_activeNode->moveBy(0, -20);
else if (event->key() == Qt::Key_Down) m_activeNode->moveBy(0, 20); else if (event->key() == Qt::Key_Down) m_activeNode->moveBy(0, 20);
else if (event->key() == Qt::Key_Left) m_activeNode->moveBy(-20, 0); else if (event->key() == Qt::Key_Left) m_activeNode->moveBy(-20, 0);
else if (event->key() == Qt::Key_Right) m_activeNode->moveBy(20, 0); else if (event->key() == Qt::Key_Right) m_activeNode->moveBy(20, 0);
} }
else else // move scene
{
dynamic_cast<MainWindow *>(m_parent)->getStatusBar()->showMessage(
tr("No active node."),
5000); // millisec
}
}
else
{ {
QGraphicsView::keyPressEvent(event); QGraphicsView::keyPressEvent(event);
} }
@ -211,29 +220,22 @@ void GraphWidget::keyPressEvent(QKeyEvent *event)
// Hint mode: select a node vimperator style // Hint mode: select a node vimperator style
case Qt::Key_F: case Qt::Key_F:
m_showingNodeNumbers = !m_showingNodeNumbers; m_showingNodeNumbers = !m_showingNodeNumbers;
if (m_showingNodeNumbers) if (!m_showingNodeNumbers)
m_hintNumber.clear();
showingAllNodeNumbers(m_showingNodeNumbers);
if (m_showingNodeNumbers)
{ {
m_nodeList.first()->showNumber(0,true,true); showingAllNodeNumbers(false);
m_hintNode = m_nodeList.first(); break;
} }
m_hintNumber.clear();
showNodeNumbers();
break; break;
// insert new node // insert new node
case Qt::Key_Insert: case Qt::Key_Insert:
if (!m_activeNode)
{
dynamic_cast<MainWindow *>(m_parent)->getStatusBar()->showMessage(
tr("No active node."),
5000); // millisec
}
else
{
insertNode(); insertNode();
} if (m_showingNodeNumbers)
showNodeNumbers();
break; break;
// used in node selection mode, to select node with numbers/enter // used in node selection mode, to select node with numbers/enter
@ -258,24 +260,11 @@ void GraphWidget::keyPressEvent(QKeyEvent *event)
// delete one letter back in node selection // delete one letter back in node selection
case Qt::Key_Backspace: case Qt::Key_Backspace:
if (!m_showingNodeNumbers) if (!m_showingNodeNumbers && m_hintNumber.isEmpty())
break; break;
if (!m_hintNumber.isEmpty())
{
m_hintNumber.remove(m_hintNumber.length()-1,1); m_hintNumber.remove(m_hintNumber.length()-1,1);
if (m_hintNumber.isEmpty()) showNodeNumbers();
{
showingAllNodeNumbers(true);
m_nodeList.first()->showNumber(0,true,true);
m_hintNode = m_nodeList.first();
}
else
{
showingAllNodeNumbers(false);
showingNodeNumbersBeginWithNumber(m_hintNumber.toInt(), true);
}
}
break; break;
// in node selection select node if nudenum = enterednum // in node selection select node if nudenum = enterednum
@ -283,38 +272,23 @@ void GraphWidget::keyPressEvent(QKeyEvent *event)
case Qt::Key_Enter: case Qt::Key_Enter:
if (m_hintNode && m_showingNodeNumbers) if (m_hintNode && m_showingNodeNumbers)
{
nodeSelected(m_hintNode); nodeSelected(m_hintNode);
}
break; break;
// edit node // edit node
case Qt::Key_F2: case Qt::Key_F2:
if (m_activeNode)
{
setActiveNodeEditable(); setActiveNodeEditable();
}
else
{
dynamic_cast<MainWindow *>(m_parent)->getStatusBar()->showMessage(
tr("No active node."),
5000); // millisec
}
break; break;
// delete node // delete node
case Qt::Key_Delete: case Qt::Key_Delete:
if (m_activeNode)
{
if (m_nodeList.size()==1) if (m_nodeList.size()==1)
{ {
dynamic_cast<MainWindow *>(m_parent)->getStatusBar()->showMessage( dynamic_cast<MainWindow *>(m_parent)->getStatusBar()->showMessage(
tr("Last node cannot be deleted."), tr("Last node cannot be deleted."),
5000); // millisec 3000); // millisec
break; break;
} }
@ -326,67 +300,30 @@ void GraphWidget::keyPressEvent(QKeyEvent *event)
m_activeNode = 0; m_activeNode = 0;
if (m_showingNodeNumbers) if (m_showingNodeNumbers)
{ showNodeNumbers();
showingAllNodeNumbers(false);
if (m_hintNumber.isEmpty())
{
showingAllNodeNumbers(true);
m_nodeList.first()->showNumber(0,true,true);
m_hintNode = m_nodeList.first();
}
else
{
showingNodeNumbersBeginWithNumber(m_hintNumber.toInt(),
true);
}
}
}
else
{
dynamic_cast<MainWindow *>(m_parent)->getStatusBar()->showMessage(
tr("No active node."),
5000); // millisec
}
break; break;
// add edge to active node // add edge to active node
case Qt::Key_A: case Qt::Key_A:
if (m_activeNode)
{
dynamic_cast<MainWindow *>(m_parent)->getStatusBar()->showMessage( dynamic_cast<MainWindow *>(m_parent)->getStatusBar()->showMessage(
tr("Add edge: select destination node"), tr("Add edge: select destination node"),
5000); // millisec 4000); // millisec
m_edgeAdding = true; m_edgeAdding = true;
}
else
{
dynamic_cast<MainWindow *>(m_parent)->getStatusBar()->showMessage(
tr("No active node."),
5000); // millisec
}
break; break;
// add edge to active node // add edge to active node
case Qt::Key_D: case Qt::Key_D:
if (m_activeNode)
{
dynamic_cast<MainWindow *>(m_parent)->getStatusBar()->showMessage( dynamic_cast<MainWindow *>(m_parent)->getStatusBar()->showMessage(
tr("Delete edge: select other end-node"), tr("Delete edge: select other end-node"),
5000); // millisec 4000); // millisec
m_edgeDeleting = true; m_edgeDeleting = true;
}
else
{
dynamic_cast<MainWindow *>(m_parent)->getStatusBar()->showMessage(
tr("No active node."),
5000); // millisec
}
break; break;
@ -520,7 +457,7 @@ void GraphWidget::nodeSelected(Node *node)
addEdge(m_activeNode, node); addEdge(m_activeNode, node);
m_edgeAdding = false; m_edgeAdding = false;
} }
if (m_edgeDeleting) else if (m_edgeDeleting)
{ {
removeEdge(m_activeNode, node); removeEdge(m_activeNode, node);
m_edgeDeleting = false; m_edgeDeleting = false;
@ -531,7 +468,7 @@ void GraphWidget::nodeSelected(Node *node)
} }
} }
void GraphWidget::addEdge(const Node *source, const Node *destination) void GraphWidget::addEdge(Node *source, Node *destination)
{ {
if (source->isConnected(destination)) if (source->isConnected(destination))
{ {
@ -541,7 +478,7 @@ void GraphWidget::addEdge(const Node *source, const Node *destination)
} }
else else
{ {
m_scene->addItem(new Edge(m_activeNode, m_hintNode)); m_scene->addItem(new Edge(source, destination));
} }
} }
@ -551,10 +488,25 @@ void GraphWidget::removeEdge(Node *source, Node *destination)
{ {
dynamic_cast<MainWindow *>(m_parent)->getStatusBar()->showMessage( dynamic_cast<MainWindow *>(m_parent)->getStatusBar()->showMessage(
tr("There no edge between these two nodes."), tr("There no edge between these two nodes."),
5000); // millisec 3000); // millisec
} }
else else
{ {
source->removeEdge(destination); source->removeEdge(destination);
} }
} }
void GraphWidget::showNodeNumbers()
{
if (m_hintNumber.isEmpty())
{
showingAllNodeNumbers(true);
m_nodeList.first()->showNumber(0,true,true);
m_hintNode = m_nodeList.first();
}
else
{
showingAllNodeNumbers(false);
showingNodeNumbersBeginWithNumber(m_hintNumber.toInt(), true);
}
}

@ -32,12 +32,13 @@ protected:
private: private:
void showNodeNumbers();
void showingAllNodeNumbers(const bool &show = true); void showingAllNodeNumbers(const bool &show = true);
void showingNodeNumbersBeginWithNumber(const int &number, void showingNodeNumbersBeginWithNumber(const int &number,
const bool &show = true); const bool &show = true);
bool numberStartsWithNumber(const int &number, const int &prefix); bool numberStartsWithNumber(const int &number, const int &prefix);
qreal calculateBiggestAngle(Node *node); qreal calculateBiggestAngle(Node *node);
void addEdge(const Node *source, const Node *destination); void addEdge(Node *source, Node *destination);
void removeEdge(Node* source, Node *destination); void removeEdge(Node* source, Node *destination);
QList<Node *> m_nodeList; QList<Node *> m_nodeList;

Loading…
Cancel
Save