edge color with k

master
Denes Matetelki 14 years ago
parent b26e01972c
commit 79bf16a352

@ -43,6 +43,12 @@ double Edge::getAngle() const
return m_angle; return m_angle;
} }
void Edge::setColor(const QColor &color)
{
m_color = color;
update();
}
void Edge::adjust() void Edge::adjust()
{ {
if (!m_sourceNode || !m_destNode) if (!m_sourceNode || !m_destNode)
@ -95,7 +101,7 @@ void Edge::paint(QPainter *painter,
return; return;
// Draw the line itself // Draw the line itself
painter->setPen(QPen(Qt::black, painter->setPen(QPen(m_color,
1, 1,
Qt::SolidLine, Qt::SolidLine,
Qt::RoundCap, Qt::RoundCap,
@ -113,7 +119,7 @@ void Edge::paint(QPainter *painter,
QPointF(sin(m_angle - Edge::m_pi + Edge::m_pi / 3) * m_arrowSize, QPointF(sin(m_angle - Edge::m_pi + Edge::m_pi / 3) * m_arrowSize,
cos(m_angle - Edge::m_pi + Edge::m_pi / 3) * m_arrowSize); cos(m_angle - Edge::m_pi + Edge::m_pi / 3) * m_arrowSize);
painter->setBrush(Qt::black); painter->setBrush(m_color);
painter->drawPolygon(QPolygonF() << line.p2() painter->drawPolygon(QPolygonF() << line.p2()
<< destArrowP1 << destArrowP1
<< destArrowP2); << destArrowP2);

@ -15,6 +15,8 @@ public:
Node *sourceNode() const; Node *sourceNode() const;
Node *destNode() const; Node *destNode() const;
double getAngle() const; double getAngle() const;
void setColor(const QColor &color);
QColor color() const { return m_color; }
void adjust(); void adjust();
@ -33,6 +35,7 @@ private:
QPointF m_sourcePoint; QPointF m_sourcePoint;
QPointF m_destPoint; QPointF m_destPoint;
double m_angle; double m_angle;
QColor m_color;
static const qreal m_arrowSize; static const qreal m_arrowSize;
static const double m_pi; static const double m_pi;

@ -25,6 +25,7 @@ GraphWidget::GraphWidget(MainWindow *parent) :
m_editingNode(false), m_editingNode(false),
m_edgeAdding(false), m_edgeAdding(false),
m_edgeDeleting(false), m_edgeDeleting(false),
m_edgeColoring(false),
m_contentChanged(false) m_contentChanged(false)
{ {
m_scene = new QGraphicsScene(this); m_scene = new QGraphicsScene(this);
@ -104,9 +105,15 @@ void GraphWidget::readContentFromXmlFile(const QString &fileName)
QDomElement e = edges.item(i).toElement(); QDomElement e = edges.item(i).toElement();
if(!e.isNull()) if(!e.isNull())
{ {
m_scene->addItem(new Edge( Edge *edge = new Edge(
m_nodeList[e.attribute("source").toInt()], m_nodeList[e.attribute("source").toInt()],
m_nodeList[e.attribute("destination").toInt()])); m_nodeList[e.attribute("destination").toInt()]);
edge->setColor(QColor(e.attribute("red").toFloat(),
e.attribute("green").toFloat(),
e.attribute("blue").toFloat(),
e.attribute("alpha").toFloat()));
m_scene->addItem(edge);
} }
} }
@ -158,6 +165,11 @@ void GraphWidget::writeContentToXmlFile(const QString &fileName)
QString::number(m_nodeList.indexOf(edge->sourceNode()))); QString::number(m_nodeList.indexOf(edge->sourceNode())));
cn.setAttribute( "destination", cn.setAttribute( "destination",
QString::number(m_nodeList.indexOf(edge->destNode()))); QString::number(m_nodeList.indexOf(edge->destNode())));
cn.setAttribute( "red", QString::number(edge->color().red()));
cn.setAttribute( "green", QString::number(edge->color().green()));
cn.setAttribute( "blue", QString::number(edge->color().blue()));
cn.setAttribute( "alpha", QString::number(edge->color().alpha()));
edges_root.appendChild(cn); edges_root.appendChild(cn);
} }
@ -220,6 +232,7 @@ void GraphWidget::keyPressEvent(QKeyEvent *event)
event->key() == Qt::Key_D || // remove edge event->key() == Qt::Key_D || // remove edge
event->key() == Qt::Key_C || // node color event->key() == Qt::Key_C || // node color
event->key() == Qt::Key_T || // node text color event->key() == Qt::Key_T || // node text color
event->key() == Qt::Key_K || // edge text color
( event->modifiers() & Qt::ControlModifier && // moving node ( event->modifiers() & Qt::ControlModifier && // moving node
( event->key() == Qt::Key_Up || ( event->key() == Qt::Key_Up ||
event->key() == Qt::Key_Down || event->key() == Qt::Key_Down ||
@ -247,6 +260,11 @@ void GraphWidget::keyPressEvent(QKeyEvent *event)
m_edgeDeleting = false; m_edgeDeleting = false;
m_parent->statusBarMsg(tr("Edge deleting cancelled.")); m_parent->statusBarMsg(tr("Edge deleting cancelled."));
} }
else if (m_edgeColoring)
{
m_edgeColoring = false;
m_parent->statusBarMsg(tr("Edge coloring cancelled."));
}
else if(m_showingNodeNumbers) else if(m_showingNodeNumbers)
{ {
m_hintNumber.clear(); m_hintNumber.clear();
@ -428,6 +446,14 @@ void GraphWidget::keyPressEvent(QKeyEvent *event)
break; break;
} }
case Qt::Key_K:
{
m_parent->statusBarMsg(tr("Edge color: select other end-node."));
m_edgeColoring = true;
break;
}
default: default:
QGraphicsView::keyPressEvent(event); QGraphicsView::keyPressEvent(event);
} }
@ -559,6 +585,21 @@ void GraphWidget::nodeSelected(Node *node)
removeEdge(m_activeNode, node); removeEdge(m_activeNode, node);
m_edgeDeleting = false; m_edgeDeleting = false;
} }
else if (m_edgeColoring)
{
QColorDialog dialog(this);
dialog.setWindowTitle(tr("Select edge color"));
dialog.setOption(QColorDialog::ShowAlphaChannel);
if (dialog.exec())
{
QColor color = dialog.selectedColor();
Edge *edge = m_activeNode->edgeTo(node);
if (edge)
edge->setColor(color);
}
m_edgeColoring = false;
}
else else
{ {
setActiveNode(node); setActiveNode(node);

@ -61,6 +61,7 @@ private:
bool m_editingNode; bool m_editingNode;
bool m_edgeAdding; bool m_edgeAdding;
bool m_edgeDeleting; bool m_edgeDeleting;
bool m_edgeColoring;
bool m_contentChanged; bool m_contentChanged;
QString m_fileName; QString m_fileName;

@ -277,6 +277,16 @@ QList<Edge *> Node::edgesFrom() const
return list; return list;
} }
Edge * Node::edgeTo(const Node *node) const
{
foreach(EdgeElement element, m_edgeList)
if (element.edge->sourceNode() == node ||
element.edge->destNode() == node)
return element.edge;
return 0;
}
void Node::paint(QPainter *painter, void Node::paint(QPainter *painter,
const QStyleOptionGraphicsItem *option, const QStyleOptionGraphicsItem *option,
QWidget *w) QWidget *w)

@ -40,6 +40,7 @@ public:
QPointF intersect(const QLineF &line, const bool &reverse = false) const; QPointF intersect(const QLineF &line, const bool &reverse = false) const;
QList<Edge *> edgesFrom() const; QList<Edge *> edgesFrom() const;
Edge * edgeTo(const Node* node) const;
protected: protected:

Loading…
Cancel
Save