nodes are selectable with vimperator-like follow keys

master
Denes Matetelki 14 years ago
parent a6c1ec643f
commit 6e0524f820

@ -32,7 +32,10 @@ Node *Edge::destNode() const
return m_destNode; return m_destNode;
} }
/// @note This is brute force. Isn't there a simple fv for this? /** @note This is brute force. Isn't there a simple fv for this?
* The precision is not the best either
*/
QPointF firstNotContainedPoint(const QLineF &line, QPointF firstNotContainedPoint(const QLineF &line,
const QRectF &rect, const QRectF &rect,
bool reverse = false) bool reverse = false)

@ -8,7 +8,6 @@
GraphWidget::GraphWidget(QWidget *parent) : GraphWidget::GraphWidget(QWidget *parent) :
QGraphicsView(parent), QGraphicsView(parent),
m_activeNode(0),
m_showingNodeNumbers(false) m_showingNodeNumbers(false)
{ {
qDebug() << __PRETTY_FUNCTION__; qDebug() << __PRETTY_FUNCTION__;
@ -59,14 +58,58 @@ GraphWidget::GraphWidget(QWidget *parent) :
node6->setPos(-10, 100); node6->setPos(-10, 100);
m_nodeList.append(node6); m_nodeList.append(node6);
Node *node7 = new Node(this);
node7->setHtml(QString("node <h1>important</h1> shit"));
m_scene->addItem(node7);
node7->setPos(-200, 50);
m_nodeList.append(node7);
Node *node8 = new Node(this);
node8->setHtml(QString("more than\none lins"));
m_scene->addItem(node8);
node8->setPos(50, -80);
m_nodeList.append(node8);
Node *node9 = new Node(this);
node9->setHtml(QString("iam a <b>bald</b> and <i>italian</i> guy"));
m_scene->addItem(node9);
node9->setPos(50, 40);
m_nodeList.append(node9);
Node *node10 = new Node(this);
node10->setHtml(QString("no joke"));
m_scene->addItem(node10);
node10->setPos(-160, -10);
m_nodeList.append(node10);
Node *node11 = new Node(this);
node11->setHtml(QString("salalal"));
m_scene->addItem(node11);
node11->setPos(-120, -120);
m_nodeList.append(node11);
Node *node12 = new Node(this);
node12->setHtml(QString("lalalala"));
m_scene->addItem(node12);
node12->setPos(130, -10);
m_nodeList.append(node12);;
m_scene->addItem(new Edge(node1, node2)); m_scene->addItem(new Edge(node1, node2));
m_scene->addItem(new Edge(node1, node3)); m_scene->addItem(new Edge(node1, node3));
m_scene->addItem(new Edge(node3, node4)); m_scene->addItem(new Edge(node3, node4));
m_scene->addItem(new Edge(node1, node5)); m_scene->addItem(new Edge(node1, node5));
m_scene->addItem(new Edge(node5, node6)); m_scene->addItem(new Edge(node5, node6));
m_scene->addItem(new Edge(node4, node11));
m_scene->addItem(new Edge(node2, node12));
m_scene->addItem(new Edge(node3, node10));
m_scene->addItem(new Edge(node10, node7));
m_scene->addItem(new Edge(node5, node7));
m_scene->addItem(new Edge(node2, node8));
m_scene->addItem(new Edge(node2, node9));
m_activeNode = m_nodeList.first(); m_activeNode = m_nodeList.first();
m_activeNode->setActive(true); m_activeNode->setActive();
} }
QGraphicsScene *GraphWidget::getScene() QGraphicsScene *GraphWidget::getScene()
@ -77,7 +120,7 @@ QGraphicsScene *GraphWidget::getScene()
void GraphWidget::keyPressEvent(QKeyEvent *event) void GraphWidget::keyPressEvent(QKeyEvent *event)
{ {
qDebug() << __PRETTY_FUNCTION__; qDebug() << __PRETTY_FUNCTION__;
// qDebug() << event->key(); qDebug() << event->key();
switch (event->key()) { switch (event->key()) {
case Qt::Key_Up: case Qt::Key_Up:
@ -99,12 +142,15 @@ void GraphWidget::keyPressEvent(QKeyEvent *event)
scaleView(1 / qreal(1.2)); scaleView(1 / qreal(1.2));
break; break;
case Qt::Key_F: case Qt::Key_F:
{ // need brackets because of local variable
m_showingNodeNumbers = !m_showingNodeNumbers; m_showingNodeNumbers = !m_showingNodeNumbers;
int i =0; if (m_showingNodeNumbers)
for (QList<Node *>::const_iterator it = m_nodeList.begin(); it != m_nodeList.end(); it++, i++) m_followNumber.clear();
dynamic_cast<Node*>(*it)->showNumber(i,m_showingNodeNumbers);
} showingAllNodeNumbers(m_showingNodeNumbers);
break;
case Qt::Key_Insert:
insertNode();
break; break;
case Qt::Key_0: case Qt::Key_0:
@ -117,11 +163,44 @@ void GraphWidget::keyPressEvent(QKeyEvent *event)
case Qt::Key_7: case Qt::Key_7:
case Qt::Key_8: case Qt::Key_8:
case Qt::Key_9: case Qt::Key_9:
if (!m_showingNodeNumbers)
break;
m_followNumber.append(QString::number(event->key()-48));
showingAllNodeNumbers(false);
showingNodeNumbersBeginWithNumber(m_followNumber.toInt(), true);
break;
m_activeNode->setActive(false); case Qt::Key_Backspace:
m_activeNode = m_nodeList[3]; if (!m_showingNodeNumbers)
m_activeNode->setActive(true); break;
if (!m_followNumber.isEmpty())
{
m_followNumber.remove(m_followNumber.length()-1,1);
if (m_followNumber.isEmpty())
{
showingAllNodeNumbers(true);
}
else
{
showingAllNodeNumbers(false);
showingNodeNumbersBeginWithNumber(m_followNumber.toInt(), true);
}
}
break;
case Qt::Key_Return:
case Qt::Key_Enter:
if (m_followNode)
{
showingAllNodeNumbers(false);
m_activeNode->setActive(false);
m_activeNode = m_followNode;
m_activeNode->setActive();
m_showingNodeNumbers = false;
}
break; break;
default: default:
@ -158,7 +237,6 @@ void GraphWidget::scaleView(qreal scaleFactor)
scale(scaleFactor, scaleFactor); scale(scaleFactor, scaleFactor);
} }
/// @note this == 0? how is it possible?
void GraphWidget::setActiveNode(Node *node) void GraphWidget::setActiveNode(Node *node)
{ {
qDebug() << __PRETTY_FUNCTION__; qDebug() << __PRETTY_FUNCTION__;
@ -167,5 +245,59 @@ void GraphWidget::setActiveNode(Node *node)
m_activeNode->setActive(false); m_activeNode->setActive(false);
m_activeNode = node; m_activeNode = node;
m_activeNode->setActive(true); m_activeNode->setActive();
}
void GraphWidget::insertNode()
{
qDebug() << __PRETTY_FUNCTION__;
}
void GraphWidget::showingAllNodeNumbers(const bool &show)
{
int i =0;
for (QList<Node *>::const_iterator it = m_nodeList.begin(); it != m_nodeList.end(); it++, i++)
dynamic_cast<Node*>(*it)->showNumber(i,show);
}
void GraphWidget::showingNodeNumbersBeginWithNumber(const int &number, const bool &show)
{
int i(0);
int hit(0);
for (QList<Node *>::const_iterator it = m_nodeList.begin(); it != m_nodeList.end(); it++, i++)
{
if (i == number)
{
hit++;
dynamic_cast<Node*>(*it)->showNumber(i,show,true);
m_followNode = dynamic_cast<Node*>(*it);
continue;
}
if (numberStartsWithNumber(i, number))
{
hit++;
dynamic_cast<Node*>(*it)->showNumber(i,show);
}
}
if (hit==1)
{
qDebug() << "set active";
showingAllNodeNumbers(false);
m_activeNode->setActive(false);
m_activeNode = m_followNode;
m_activeNode->setActive();
m_showingNodeNumbers = false;
}
/// @bug handle this case
// else if (hit == 0)
// {
// }
}
bool GraphWidget::numberStartsWithNumber(const int &number, const int &prefix)
{
return (QString::number(number)).startsWith(QString::number(prefix));
} }

@ -17,6 +17,7 @@ public:
GraphWidget(QWidget *parent = 0); GraphWidget(QWidget *parent = 0);
QGraphicsScene *getScene(); QGraphicsScene *getScene();
void setActiveNode(Node *node); void setActiveNode(Node *node);
void insertNode();
protected: protected:
@ -28,11 +29,16 @@ protected:
private: private:
void showingAllNodeNumbers(const bool &show = true);
void showingNodeNumbersBeginWithNumber(const int &number, const bool &show = true);
bool numberStartsWithNumber(const int &number, const int &prefix);
QList<Node *> m_nodeList; QList<Node *> m_nodeList;
Node *m_activeNode; Node *m_activeNode;
QGraphicsScene *m_scene; QGraphicsScene *m_scene;
bool m_showingNodeNumbers; bool m_showingNodeNumbers;
QString m_followNumber;
Node *m_followNode;
}; };

@ -55,6 +55,9 @@
</widget> </widget>
<widget class="QStatusBar" name="statusBar"/> <widget class="QStatusBar" name="statusBar"/>
<action name="actionNew"> <action name="actionNew">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text"> <property name="text">
<string>&amp;New</string> <string>&amp;New</string>
</property> </property>
@ -63,6 +66,9 @@
</property> </property>
</action> </action>
<action name="actionOpen"> <action name="actionOpen">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text"> <property name="text">
<string>&amp;Open</string> <string>&amp;Open</string>
</property> </property>
@ -71,6 +77,9 @@
</property> </property>
</action> </action>
<action name="actionSave"> <action name="actionSave">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text"> <property name="text">
<string>&amp;Save</string> <string>&amp;Save</string>
</property> </property>
@ -92,6 +101,9 @@
</property> </property>
</action> </action>
<action name="actionClose"> <action name="actionClose">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text"> <property name="text">
<string>&amp;Close</string> <string>&amp;Close</string>
</property> </property>

@ -8,8 +8,9 @@
Node::Node(GraphWidget *parent) : Node::Node(GraphWidget *parent) :
m_graph(parent), m_graph(parent),
m_isActive(false), m_isActive(false),
m_activeEdge(0), // m_activeEdge(0),
m_number(-1) m_number(-1),
m_numberIsSpecial(false)
{ {
qDebug() << __PRETTY_FUNCTION__; qDebug() << __PRETTY_FUNCTION__;
@ -72,7 +73,13 @@ QVariant Node::itemChange(GraphicsItemChange change, const QVariant &value)
void Node::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *w) void Node::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *w)
{ {
qDebug() << __PRETTY_FUNCTION__; // qDebug() << __PRETTY_FUNCTION__;
if (m_number != -1)
{
painter->setBackground(m_numberIsSpecial ? Qt::green : Qt::yellow);
painter->setBackgroundMode(Qt::OpaqueMode);
}
QGraphicsTextItem::paint(painter, option, w); QGraphicsTextItem::paint(painter, option, w);
@ -82,12 +89,10 @@ void Node::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWid
boundingRect().bottomRight().toPoint() - boundingRect().bottomRight().toPoint() -
QPoint(1,1))); QPoint(1,1)));
qDebug() << m_number;
if (m_number != -1) if (m_number != -1)
{ {
painter->setPen(Qt::yellow); painter->setPen(Qt::white);
painter->setBackground(Qt::black); painter->setBackground(Qt::red);
painter->setBackgroundMode(Qt::OpaqueMode); painter->setBackgroundMode(Qt::OpaqueMode);
painter->drawText(boundingRect().topLeft()+QPointF(0,11), QString("%1").arg(m_number)); painter->drawText(boundingRect().topLeft()+QPointF(0,11), QString("%1").arg(m_number));
} }
@ -102,18 +107,28 @@ void Node::setActive(const bool &active)
} }
/// @note who shall set active: press or release?
void Node::mousePressEvent(QGraphicsSceneMouseEvent *event) void Node::mousePressEvent(QGraphicsSceneMouseEvent *event)
{ {
qDebug() << __PRETTY_FUNCTION__; qDebug() << __PRETTY_FUNCTION__;
m_graph->setActiveNode(this);
QGraphicsItem::mousePressEvent(event); QGraphicsItem::mousePressEvent(event);
} }
void Node::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) void Node::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
{ {
qDebug() << __PRETTY_FUNCTION__; qDebug() << __PRETTY_FUNCTION__;
m_graph->setActiveNode(this); m_graph->insertNode();
QGraphicsItem::mouseDoubleClickEvent(event);
}
void Node::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
qDebug() << __PRETTY_FUNCTION__;
QGraphicsItem::mouseReleaseEvent(event); QGraphicsItem::mouseReleaseEvent(event);
} }
@ -125,10 +140,11 @@ void Node::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
QGraphicsItem::mouseMoveEvent(event); QGraphicsItem::mouseMoveEvent(event);
} }
void Node::showNumber(const int &number, const bool& show) void Node::showNumber(const int &number, const bool& show, const bool &numberIsSpecial)
{ {
qDebug() << __PRETTY_FUNCTION__; // qDebug() << __PRETTY_FUNCTION__;
m_number = show ? number : -1; m_number = show ? number : -1;
m_numberIsSpecial = numberIsSpecial;
update(); update();
} }

@ -16,8 +16,8 @@ public:
void addEdge(Edge *edge); void addEdge(Edge *edge);
// QList<Edge *> edges() const; // QList<Edge *> edges() const;
void setActive(const bool &active); void setActive(const bool &active = true);
void showNumber(const int &number, const bool& show); void showNumber(const int &number, const bool& show = true, const bool &numberIsSpecial = false);
protected: protected:
@ -25,6 +25,7 @@ protected:
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
QVariant itemChange(GraphicsItemChange change, const QVariant &value); QVariant itemChange(GraphicsItemChange change, const QVariant &value);
void mousePressEvent(QGraphicsSceneMouseEvent *event); void mousePressEvent(QGraphicsSceneMouseEvent *event);
void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event);
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event); void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
void mouseMoveEvent(QGraphicsSceneMouseEvent *event); void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
@ -33,8 +34,9 @@ private:
QList<Edge *> m_edgeList; QList<Edge *> m_edgeList;
GraphWidget *m_graph; GraphWidget *m_graph;
bool m_isActive; bool m_isActive;
Edge *m_activeEdge; // Edge *m_activeEdge;
int m_number; int m_number;
bool m_numberIsSpecial;
}; };
#endif // NODE_H #endif // NODE_H

Loading…
Cancel
Save