void GraphViewer::onSceneMouseRelease(QGraphicsSceneMouseEvent *mouseEvent)
{

    // need to put the connecting line on the background otherwise it is detected under the mouse
    _drawingEdge->setZValue(EDGE_LINE_Z);

    QGraphicsItem * item = _scene.itemAt(mouseEvent->scenePos(), QTransform());
    if (qgraphicsitem_cast<QGraphicsEllipseItem*>(item)
            || qgraphicsitem_cast<QGraphicsSimpleTextItem*>(item))
    {
        QGraphicsEllipseItem* vertex = qgraphicsitem_cast<QGraphicsEllipseItem*>(item->parentItem());
        if (!vertex)
            vertex = qgraphicsitem_cast<QGraphicsEllipseItem*>(item);

        if (vertex)
        {
            double x = _drawingEdge->transform().dx();
            double y = _drawingEdge->transform().dy();
            _drawingEdge->setLine(0.0,0.0, vertex->scenePos().x()-x, vertex->scenePos().y()-y);
            _drawingEdge->setZValue(EDGE_LINE_Z);

            if (_drawingEdge->data(KEY_EDGE_VERTEX1) == vertex->data(KEY_EDGE_VERTEX1))
                _scene.removeItem(_drawingEdge);
            else
            {
                int defaultWeight = 1;
                _drawingEdge->setData(KEY_EDGE_VERTEX2, vertex->data(KEY_VERTEX_ID));
                _drawingEdge->setData(KEY_EDGE_WEIGHT, defaultWeight);
                // draw edge weight
                QGraphicsSimpleTextItem * edgeWeight = _scene.addSimpleText(QString("%1").arg(defaultWeight));
                edgeWeight->setParentItem(_drawingEdge);
                edgeWeight->setBrush(Qt::blue);
                QLineF line = _drawingEdge->line();
                edgeWeight->setTransform(
                            QTransform::fromScale(0.005, 0.005)
                            * QTransform::fromTranslate(line.x2()*0.5, line.y2()*0.5)
                            );
                edgeWeight->setZValue(EDGE_TEXT_Z);
                // add to graph edges
                _edges << _drawingEdge;
            }
        }
    }
    else
    {
        _scene.removeItem(_drawingEdge);
    }
    _isDrawingEdge=false;
    _drawingEdge=0;
}
Exemple #2
0
void
PacketsScene::addPacket (qreal tx, qreal rx, uint32_t fromNodeId, uint32_t toNodeId, QString metaInfo, bool drawPacket)
{
  QString shortMeta = "";
  if (m_filter != AnimPacket::ALL)
    {
      bool result;
      shortMeta = AnimPacket::getMeta (metaInfo, m_filter, result, false);
      if (!result)
        return;
    }
  else
    {
      shortMeta = AnimPacket::getMeta (metaInfo, false);
    }

  QRegExp rex (m_filterRegex);
  if (rex.indexIn (metaInfo) == -1)
  {
    return;
  }

  qreal txY = 0;
  qreal rxY = 0;
  if (drawPacket && m_showGraph)
    {
      qreal fromNodeX = m_interNodeSpacing * m_lineIndex[fromNodeId];
      qreal toNodeX = m_interNodeSpacing * m_lineIndex[toNodeId];
      txY = timeToY (tx);
      rxY = timeToY (rx);

      GraphPacket * graphPacket = new GraphPacket (QPointF (fromNodeX, txY), QPointF (toNodeX, rxY));
      //addItem (graphPacket);
      m_packetPath.moveTo (graphPacket->line ().p1 ());
      m_packetPath.lineTo (graphPacket->line ().p2 ());
      qreal angle = 45;
      qreal mag = 9;
      QPointF endPoint (graphPacket->line ().p2 ());
      if (1)
        {
        if (graphPacket->line ().angle () > 270)
          {
            m_packetPath.moveTo (endPoint);
            angle += graphPacket->line ().angle ();
            //NS_LOG_DEBUG ("Angle:" << graphPacket->line ().angle () << " Final Angle:" << angle);
            m_packetPath.lineTo (endPoint.x () - mag * cos (angle * PI/180), endPoint.y () - mag * sin (angle * PI/180));
            m_packetPath.moveTo (endPoint);
            m_packetPath.lineTo (endPoint.x () - mag * cos (angle * PI/180), endPoint.y () + mag * sin (angle * PI/180));

          }
        else if (graphPacket->line ().angle () > 180)
          {
            m_packetPath.moveTo (endPoint);
            angle += 180 - graphPacket->line ().angle ();
            //NS_LOG_DEBUG ("Angle:" << graphPacket->line ().angle () << " Final Angle:" << angle);
            m_packetPath.lineTo (endPoint.x () + mag * cos (angle * PI/180), endPoint.y () - mag * sin (angle * PI/180));
            m_packetPath.moveTo (endPoint);
            m_packetPath.lineTo (endPoint.x () + mag * cos (angle * PI/180), endPoint.y () + mag * sin (angle * PI/180));
          }
        }

      m_packetPathItem->setPath (m_packetPath);

      m_packetLines.push_back (graphPacket);
      QGraphicsSimpleTextItem * info = new QGraphicsSimpleTextItem (shortMeta);
      addItem (info);
      m_packetInfoTexts.push_back (info);
      info->setFlag (QGraphicsItem::ItemIgnoresTransformations);
      info->setPos (QPointF (fromNodeX, txY));
      qreal textAngle = graphPacket->line().angle ();
      if(textAngle < 90)
        {
          textAngle = 360-textAngle;
        }
      else if (textAngle > 270)
        {
          textAngle = 360-textAngle;
        }
      else
        {
          textAngle = 180-textAngle;
          info->setPos (QPointF (toNodeX, rxY));

        }
      info->setTransform (QTransform ().rotate (textAngle));
    }

  Table * table = PacketsMode::getInstance ()->getTable ();
  QStringList sl;
  sl << QString::number (fromNodeId)
     << QString::number (toNodeId)
     << QString::number (tx)
     << shortMeta;
  table->addRow (sl);

  if (m_showGrid && drawPacket && m_showGraph)
    {
      QGraphicsSimpleTextItem * txText = new QGraphicsSimpleTextItem (QString::number (tx));
      txText->setFlag (QGraphicsItem::ItemIgnoresTransformations);
      addItem (txText);
      txText->setPos (RULER_X, txY);
      QPen pen (QColor (200, 100, 155, 100));
      QGraphicsLineItem * horizontalTxLine = new QGraphicsLineItem (RULER_X, txY, m_interNodeSpacing * m_lineIndex.size (), txY);
      QGraphicsLineItem * horizontalRxLine = new QGraphicsLineItem (RULER_X, rxY, m_interNodeSpacing * m_lineIndex.size (), rxY);
      horizontalTxLine->setPen (pen);
      horizontalRxLine->setPen (pen);
      addItem (horizontalTxLine);
      addItem (horizontalRxLine);

      QGraphicsSimpleTextItem * rxText = new QGraphicsSimpleTextItem (QString::number (rx));
      addItem (rxText);
      rxText->setFlag (QGraphicsItem::ItemIgnoresTransformations);
      rxText->setPos (RULER_X, rxY);
      //graphPacket->setPos (QPointF (fromNodeX, txY));
      m_rulerTexts.push_back (txText);
      m_rulerTexts.push_back (rxText);
      m_horizontalRulerLines.push_back (horizontalTxLine);
      m_horizontalRulerLines.push_back (horizontalRxLine);
    }

}
void GraphViewer::onSceneMousePress(QGraphicsSceneMouseEvent *mouseEvent)
{
    if (_vertices.isEmpty())
        _initialText->setVisible(false);

    if (_scene.items(QRectF(
                mouseEvent->scenePos().x() - VERTEX_SIZE*0.5,
                mouseEvent->scenePos().y() - VERTEX_SIZE*0.5,
                VERTEX_SIZE,
                VERTEX_SIZE)
                ).isEmpty())
    {
        // Create new vertex
        QGraphicsEllipseItem * vertex = _scene.addEllipse(
                    QRectF(-VERTEX_SIZE*0.5, -VERTEX_SIZE*0.5, VERTEX_SIZE, VERTEX_SIZE),
                    QPen(Qt::black, 0),
                    QBrush(Qt::white)
                    );
        vertex->setTransform(
                    QTransform::fromTranslate(mouseEvent->scenePos().x(), mouseEvent->scenePos().y())
                    );
        vertex->setZValue(VERTEX_CIRCLE_Z);
        _vertices << vertex;
        int id = _vertices.size()-1;
        vertex->setData(KEY_VERTEX_ID, id);
        QGraphicsSimpleTextItem * vertexId = _scene.addSimpleText(QString("%1").arg(id+1));
        vertexId->setParentItem(vertex);
        vertexId->setTransform(
                    QTransform::fromScale(0.005, 0.005)
                    * QTransform::fromTranslate(-VERTEX_SIZE*( (id < 9) ? 0.18 : 0.28 ), -VERTEX_SIZE*0.35)
                    );
        vertexId->setZValue(VERTEX_TEXT_Z);

    }
    else
    {
        // Propose to draw new edge
        QGraphicsItem * item = _scene.itemAt(mouseEvent->scenePos(), QTransform());
        if (qgraphicsitem_cast<QGraphicsEllipseItem*>(item)
                || qgraphicsitem_cast<QGraphicsSimpleTextItem*>(item))
        {
            QGraphicsEllipseItem* vertex = qgraphicsitem_cast<QGraphicsEllipseItem*>(item->parentItem());
            if (!vertex)
                vertex = qgraphicsitem_cast<QGraphicsEllipseItem*>(item);

            if (vertex)
            {
                _isDrawingEdge=true;
                _drawingEdge = _scene.addLine(0.0,
                                              0.0,
                                              mouseEvent->scenePos().x()-vertex->scenePos().x(),
                                              mouseEvent->scenePos().y()-vertex->scenePos().y(),
                                              QPen(Qt::black, 0));
                _drawingEdge->setTransform(QTransform::fromTranslate(vertex->scenePos().x(), vertex->scenePos().y()));
                _drawingEdge->setZValue(VERTEX_CIRCLE_Z);
                _drawingEdge->setData(KEY_EDGE_VERTEX1, vertex->data(KEY_VERTEX_ID));
            }

        }
    }
}