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

@ -129,7 +129,7 @@ QGraphicsScene *GraphWidget::getScene()
void GraphWidget::keyPressEvent(QKeyEvent *event)
{
{
// esc leaves node editing mode
if (event->key() == Qt::Key_Escape && m_editingNode)
{
@ -144,6 +144,24 @@ void GraphWidget::keyPressEvent(QKeyEvent *event)
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())
{
@ -155,14 +173,14 @@ void GraphWidget::keyPressEvent(QKeyEvent *event)
m_edgeAdding = false;
dynamic_cast<MainWindow *>(m_parent)->getStatusBar()->showMessage(
tr("Edge adding cancelled"),
5000); // millisec
3000); // millisec
}
else if (m_edgeDeleting)
{
m_edgeDeleting = false;
dynamic_cast<MainWindow *>(m_parent)->getStatusBar()->showMessage(
tr("Edge deleting cancelled"),
5000); // millisec
3000); // millisec
}
else if(m_showingNodeNumbers)
{
@ -179,22 +197,13 @@ void GraphWidget::keyPressEvent(QKeyEvent *event)
case Qt::Key_Left:
case Qt::Key_Right:
if (event->modifiers() == Qt::ControlModifier)
{
if (m_activeNode)
{
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_Left) m_activeNode->moveBy(-20, 0);
else if (event->key() == Qt::Key_Right) m_activeNode->moveBy(20, 0);
}
else
{
dynamic_cast<MainWindow *>(m_parent)->getStatusBar()->showMessage(
tr("No active node."),
5000); // millisec
}
}
else
else // move scene
{
QGraphicsView::keyPressEvent(event);
}
@ -211,29 +220,22 @@ void GraphWidget::keyPressEvent(QKeyEvent *event)
// Hint mode: select a node vimperator style
case Qt::Key_F:
m_showingNodeNumbers = !m_showingNodeNumbers;
if (m_showingNodeNumbers)
m_hintNumber.clear();
showingAllNodeNumbers(m_showingNodeNumbers);
if (m_showingNodeNumbers)
if (!m_showingNodeNumbers)
{
m_nodeList.first()->showNumber(0,true,true);
m_hintNode = m_nodeList.first();
showingAllNodeNumbers(false);
break;
}
m_hintNumber.clear();
showNodeNumbers();
break;
// insert new node
case Qt::Key_Insert:
if (!m_activeNode)
{
dynamic_cast<MainWindow *>(m_parent)->getStatusBar()->showMessage(
tr("No active node."),
5000); // millisec
}
else
{
insertNode();
}
if (m_showingNodeNumbers)
showNodeNumbers();
break;
// 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
case Qt::Key_Backspace:
if (!m_showingNodeNumbers)
if (!m_showingNodeNumbers && m_hintNumber.isEmpty())
break;
if (!m_hintNumber.isEmpty())
{
m_hintNumber.remove(m_hintNumber.length()-1,1);
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);
}
}
showNodeNumbers();
break;
// in node selection select node if nudenum = enterednum
@ -283,38 +272,23 @@ void GraphWidget::keyPressEvent(QKeyEvent *event)
case Qt::Key_Enter:
if (m_hintNode && m_showingNodeNumbers)
{
nodeSelected(m_hintNode);
}
break;
// edit node
case Qt::Key_F2:
if (m_activeNode)
{
setActiveNodeEditable();
}
else
{
dynamic_cast<MainWindow *>(m_parent)->getStatusBar()->showMessage(
tr("No active node."),
5000); // millisec
}
break;
// delete node
case Qt::Key_Delete:
if (m_activeNode)
{
if (m_nodeList.size()==1)
{
dynamic_cast<MainWindow *>(m_parent)->getStatusBar()->showMessage(
tr("Last node cannot be deleted."),
5000); // millisec
3000); // millisec
break;
}
@ -326,74 +300,37 @@ void GraphWidget::keyPressEvent(QKeyEvent *event)
m_activeNode = 0;
if (m_showingNodeNumbers)
{
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
}
showNodeNumbers();
break;
// add edge to active node
case Qt::Key_A:
if (m_activeNode)
{
dynamic_cast<MainWindow *>(m_parent)->getStatusBar()->showMessage(
tr("Add edge: select destination node"),
5000); // millisec
4000); // millisec
m_edgeAdding = true;
}
else
{
dynamic_cast<MainWindow *>(m_parent)->getStatusBar()->showMessage(
tr("No active node."),
5000); // millisec
}
break;
// add edge to active node
case Qt::Key_D:
if (m_activeNode)
{
dynamic_cast<MainWindow *>(m_parent)->getStatusBar()->showMessage(
tr("Delete edge: select other end-node"),
5000); // millisec
4000); // millisec
m_edgeDeleting = true;
}
else
{
dynamic_cast<MainWindow *>(m_parent)->getStatusBar()->showMessage(
tr("No active node."),
5000); // millisec
}
break;
default:
QGraphicsView::keyPressEvent(event);
}
}
}
@ -403,7 +340,7 @@ void GraphWidget::wheelEvent(QWheelEvent *event)
}
void GraphWidget::drawBackground(QPainter *painter, const QRectF &rect)
{
{
Q_UNUSED(rect);
QRectF sceneRect = this->sceneRect();
@ -415,7 +352,7 @@ void GraphWidget::drawBackground(QPainter *painter, const QRectF &rect)
painter->fillRect(rect.intersect(sceneRect), gradient);
painter->setBrush(Qt::NoBrush);
painter->drawRect(sceneRect);
}
}
void GraphWidget::scaleView(qreal scaleFactor)
@ -520,7 +457,7 @@ void GraphWidget::nodeSelected(Node *node)
addEdge(m_activeNode, node);
m_edgeAdding = false;
}
if (m_edgeDeleting)
else if (m_edgeDeleting)
{
removeEdge(m_activeNode, node);
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))
{
@ -541,7 +478,7 @@ void GraphWidget::addEdge(const Node *source, const Node *destination)
}
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(
tr("There no edge between these two nodes."),
5000); // millisec
3000); // millisec
}
else
{
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:
void showNodeNumbers();
void showingAllNodeNumbers(const bool &show = true);
void showingNodeNumbersBeginWithNumber(const int &number,
const bool &show = true);
bool numberStartsWithNumber(const int &number, const int &prefix);
qreal calculateBiggestAngle(Node *node);
void addEdge(const Node *source, const Node *destination);
void addEdge(Node *source, Node *destination);
void removeEdge(Node* source, Node *destination);
QList<Node *> m_nodeList;

Loading…
Cancel
Save