SubdRepresentation::SubdRepresentation(Rectangle rect, Crop& crop, QDate date) :
    rect(rect), crop(crop)
{
    this->setRect(get_rect().get_x(), - get_rect().get_y() - get_rect().get_height(),
                  get_rect().get_width(), get_rect().get_height());
    if (crop)
    {
        Plant& plant = crop.get_plant();
        QString text = toQString(plant.get_name());
        //text = re.sub("\s+", "\n", text)
        QGraphicsSimpleTextItem *textw =  new QGraphicsSimpleTextItem(text);
        QFont font = textw->font();
        font.setPointSize(6);
        font.setWeight(25);
        textw->setFont(font);
        center_text(textw, this->boundingRect());
        textw->setParentItem(this);
        QString color_str = toQString(plant.get_color_str());
        if (color_str == "") { color_str = "#FF00FF"; }
        QColor color = QColor(color_str);
        if (crop.is_planned_at_date(fromQDate(date)) && !crop.is_active_at_date(fromQDate(date)))
        {
            this->setBrush(QBrush(color, Qt::BDiagPattern));
        }
        else
        {
            this->setBrush(QBrush(color));
        }
    }
}
ModelItemGraphics::ModelItemGraphics(const QPointF& Coords, const QString& ID,
                                     QGraphicsItem* Parent):
  QGraphicsRectItem(Coords.x(),Coords.y(),m_DefaultSize.width(),m_DefaultSize.height(),Parent),
  m_ID(ID), m_Ghost(false), m_Initialized(false)
{
  setPen(QPen(QBrush(QColor(BUILDER_MODELVIEW_BORDERCOLOR)),2));

  setFlag(QGraphicsItem::ItemIsMovable);
  setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);

  setCursor(QCursor(Qt::OpenHandCursor));


  // Model item ID
  QGraphicsSimpleTextItem* IDGraphics = new QGraphicsSimpleTextItem(ID,Parent);
  IDGraphics->setParentItem(this);

  QFont TmpFont = IDGraphics->font();
  TmpFont.setPointSize(10);
  IDGraphics->setFont(TmpFont);

  QRectF IDRect = IDGraphics->boundingRect();

  if (rect().width() < IDRect.width()+20)
  setRect(Coords.x(),Coords.y(),IDRect.width()+20,m_DefaultSize.height());

  IDGraphics->setPos((rect().width()/2)-(IDRect.width()/2),(m_DefaultSize.height()/2)-(IDRect.height()/2));
}
Example #3
0
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;
}
Example #4
0
void HomeScreen::initSprite()
{
	_sprite = new QGraphicsWidget();

	QGraphicsItem* item;
	QGraphicsSimpleTextItem* text;
	QGraphicsProxyWidget* proxy;
	QLabel* label;
	QFont font;

	item = new QGraphicsPixmapItem();
	item->setParentItem(_sprite);
	item->setPos((BACKGROUND_W - (400.0f / 0.75f)) * 0.5f, BACKGROUND_H - 400.0f);
	_avatar = static_cast<QGraphicsPixmapItem*>(item);

	item = new QGraphicsPixmapItem(LoaderThread::instance()->getCachedPixmap(IMAGE_TITLE));
	item->setParentItem(_sprite);
	item->setPos(232.0f, 128.0f);

	font = QFont("Arial");
	font.setPixelSize(12);
	font.setWeight(QFont::Normal);
	font.setStretch(80);

	text = new QGraphicsSimpleTextItem("http://conanchen.com/Kinetris");
	text->setBrush(QColor::fromRgb(0xFF, 0xFF, 0xFF));
	text->setFont(font);
	text->setParentItem(_sprite);
	text->setPos(248.0f, 264.0f - 3.0f);

	label = new QLabel();
	label->resize(784, 24);
	label->setStyleSheet("background-color: transparent; color: #FFFFFF;");
	label->setAlignment(Qt::AlignTop | Qt::AlignHCenter);
	proxy = new QGraphicsProxyWidget();
	proxy->setParentItem(_sprite);
	proxy->setWidget(label);
	proxy->setPos(248.0f, 592.0f - 6.0f);
	_sprite_status = proxy;
	_status = label;
}
Example #5
0
void VisualMatrix::initSprite()
{
	_sprite = new QGraphicsWidget();

	QGraphicsItem* item;
	QGraphicsWidget* widget;
	QGraphicsRectItem* rect;
	QGraphicsColorizeEffect* effect;
	QGraphicsSimpleTextItem* text;
	QGraphicsProxyWidget* proxy;
	QLabel* label;
	QFont font;

	// Frame
	{
		item = new QGraphicsPixmapItem(LoaderThread::instance()->getCachedPixmap(IMAGE_FRAME_BG));
		item->setParentItem(_sprite);
		item->setPos(0.0f, 0.0f);
	}

	// Frame
	{
		item = new QGraphicsPixmapItem(LoaderThread::instance()->getCachedPixmap(IMAGE_FRAME_MG));
		item->setParentItem(_sprite);
		item->setPos(0.0f, 0.0f);
	}

	// Avatar
	{
		widget = new QGraphicsWidget();
		widget->resize(BLOCK_LARGE * 10, BLOCK_LARGE * 20);
		widget->setFlags(QGraphicsItem::ItemClipsChildrenToShape);
		widget->setParentItem(_sprite);
		widget->setPos(170.0f, 50.0f);
		
		item = new QGraphicsPixmapItem();
		item->setParentItem(widget);
		item->setPos(((BLOCK_LARGE * 10) - (320.0f / 0.75f)) * 0.5f, (BLOCK_LARGE * 20) - 320.0f);
		
		_avatar = widget;
	}

	// Help
	{
		widget = new QGraphicsWidget();
		widget->resize(BLOCK_LARGE * 10, BLOCK_LARGE * 20);
		widget->setFlags(QGraphicsItem::ItemClipsChildrenToShape);
		widget->setParentItem(_sprite);
		widget->setPos(170.0f, 50.0f);
		
		rect = new QGraphicsRectItem(0.0f, 0.0f, BLOCK_LARGE * 10, BLOCK_LARGE * 20);
		rect->setBrush(QBrush(QColor::fromRgb(0x00, 0x00, 0x00, 0xC0)));
		rect->setParentItem(widget);
		
		font = QApplication::font();
		font.setFamily("Arial");
		font.setPixelSize(16);
		font.setWeight(QFont::Bold);
		font.setStretch(80);
		
		text = new QGraphicsSimpleTextItem(tr("Slide left and right: Move."));
		text->setBrush(QColor::fromRgb(0xC0, 0xC0, 0xC0));
		text->setFont(font);
		text->setParentItem(widget);
		text->setPos(((BLOCK_LARGE * 10) - text->boundingRect().width()) * 0.5f, 217.0f - 6.0f);
		
		text = new QGraphicsSimpleTextItem(tr("Swipe up: Hold.  Swipe down: Drop."));
		text->setBrush(QColor::fromRgb(0xC0, 0xC0, 0xC0));
		text->setFont(font);
		text->setParentItem(widget);
		text->setPos(((BLOCK_LARGE * 10) - text->boundingRect().width()) * 0.5f, 237.0f - 6.0f);
		
		text = new QGraphicsSimpleTextItem(tr("Pull back, and make circles: Rotate."));
		text->setBrush(QColor::fromRgb(0xC0, 0xC0, 0xC0));
		text->setFont(font);
		text->setParentItem(widget);
		text->setPos(((BLOCK_LARGE * 10) - text->boundingRect().width()) * 0.5f, 257.0f - 6.0f);

//		widget->setVisible(false);
		
		_sprite_help = widget;
	}

	// Lines
	{
		rect = new QGraphicsRectItem(120.0f - 1.0f, 220.0f - 1.0f, 8.0f + 2.0f, 144.0f + 2.0f);
		rect->setBrush(QBrush(LoaderThread::instance()->getCachedPixmap(IMAGE_LINES_PROGRESS)));
		rect->setParentItem(_sprite);

		_sprite_lines = rect;
	}

	// Frame
	{
		item = new QGraphicsPixmapItem(LoaderThread::instance()->getCachedPixmap(IMAGE_FRAME_FG));
		item->setParentItem(_sprite);
		item->setPos(0.0f, 0.0f);
	}

	// Field
	{
		widget = new QGraphicsWidget();
		widget->resize(BLOCK_LARGE * 10, BLOCK_LARGE * 20);
		widget->setFlags(QGraphicsItem::ItemClipsChildrenToShape);
		widget->setParentItem(_sprite);
		widget->setPos(170.0f, 50.0f);
		
		_sprite_field = widget;

		_sprite_space.fill(NULL, _rows * _cols);
	}

	// Ghost
	{
		widget = new QGraphicsWidget();
		widget->resize(BLOCK_LARGE * 4, BLOCK_LARGE * 4);
		widget->setParentItem(_sprite_field);
		widget->setPos(BLOCK_LARGE * getShapePositionInField(18, 3));
		
		for (int i = 0; i < 4; ++i)
		{
			item = new QGraphicsPixmapItem(LoaderThread::instance()->getCachedPixmap(IMAGE_BLOCK_GHOST));
			item->setParentItem(widget);
		}
	
		_sprite_ghost = widget;
	}

	// Tetromino
	{
		widget = new QGraphicsWidget();
		widget->resize(BLOCK_LARGE * 4, BLOCK_LARGE * 4);
		widget->setParentItem(_sprite_field);
		widget->setPos(BLOCK_LARGE * getShapePositionInField(18, 3));
		widget->setZValue(1.0f);
		
		effect = new QGraphicsColorizeEffect(widget);
		effect->setColor(QColor::fromRgb(0xFF, 0xFF, 0xFF));
		effect->setStrength(0.0f);
		widget->setGraphicsEffect(effect);
		
		for (int i = 0; i < 4; ++i)
		{
			item = new QGraphicsPixmapItem();
			item->setParentItem(widget);
		}
		
		_sprite_tetromino = widget;
	}

	// Hold
	{
		widget = new QGraphicsWidget();
		widget->resize(BLOCK_LARGE * 4, BLOCK_LARGE * 2);
		widget->setScale(BLOCK_SMALL / BLOCK_LARGE);
		widget->setParentItem(_sprite);
		widget->setPos(56.0f, 108.0f + BLOCK_SMALL);
		
		effect = new QGraphicsColorizeEffect(widget);
		effect->setColor(QColor::fromRgb(0xFF, 0xFF, 0xFF));
		effect->setStrength(0.0f);
		widget->setGraphicsEffect(effect);
		
		for (int i = 0; i < 4; ++i)
		{
			item = new QGraphicsPixmapItem();
			item->setParentItem(widget);
		}
		
		_sprite_hold = widget;
	}

	// Hold fail
	{
		item = new QGraphicsPixmapItem(LoaderThread::instance()->getCachedPixmap(IMAGE_FAIL));
		item->setScale(BLOCK_SMALL / BLOCK_LARGE);
		item->setParentItem(_sprite);
		item->setPos(56.0f + (BLOCK_SMALL * 3), 108.0f + (BLOCK_SMALL * 3));
		item->setVisible(false);
	
		_sprite_holdFail = item;
	}

	// Next
	{
		widget = new QGraphicsWidget();
		widget->resize(BLOCK_LARGE * 4, BLOCK_LARGE * 2);
		widget->setScale(BLOCK_SMALL / BLOCK_LARGE);
		widget->setParentItem(_sprite);
		widget->setPos(460.0f, 108.0f + BLOCK_SMALL);
		
		effect = new QGraphicsColorizeEffect(widget);
		effect->setColor(QColor::fromRgb(0xFF, 0xFF, 0xFF));
		effect->setStrength(0.0f);
		widget->setGraphicsEffect(effect);
		
		for (int i = 0; i < 4; ++i)
		{
			item = new QGraphicsPixmapItem();
			item->setParentItem(widget);
		}
	
		_sprite_next << widget;
	}

	// Next
	{
		widget = new QGraphicsWidget();
		widget->resize(BLOCK_LARGE * 4, BLOCK_LARGE * 2);
		widget->setScale(BLOCK_SMALL / BLOCK_LARGE);
		widget->setParentItem(_sprite);
		widget->setPos(460.0f, 228.0f + BLOCK_SMALL);
		
		effect = new QGraphicsColorizeEffect(widget);
		effect->setColor(QColor::fromRgb(0xFF, 0xFF, 0xFF));
		effect->setStrength(0.0f);
		widget->setGraphicsEffect(effect);
		
		for (int i = 0; i < 4; ++i)
		{
			item = new QGraphicsPixmapItem();
			item->setParentItem(widget);
		}
	
		_sprite_next << widget;
	}

	// Next
	{
		widget = new QGraphicsWidget();
		widget->resize(BLOCK_LARGE * 4, BLOCK_LARGE * 2);
		widget->setScale(BLOCK_SMALL / BLOCK_LARGE);
		widget->setParentItem(_sprite);
		widget->setPos(460.0f, 292.0f + BLOCK_SMALL);
		
		effect = new QGraphicsColorizeEffect(widget);
		effect->setColor(QColor::fromRgb(0xFF, 0xFF, 0xFF));
		effect->setStrength(0.0f);
		widget->setGraphicsEffect(effect);
		
		for (int i = 0; i < 4; ++i)
		{
			item = new QGraphicsPixmapItem();
			item->setParentItem(widget);
		}
		
		_sprite_next << widget;
	}

	// Font
	{
		font = QApplication::font();
		font.setCapitalization(QFont::AllUppercase);
	}

	// Hold
	{
		text = new QGraphicsSimpleTextItem(tr("Hold"));
		text->setBrush(QColor::fromRgb(0x00, 0x00, 0x00));
		text->setFont(font);
		text->setParentItem(_sprite);
		text->setPos(56.0f, 80.0f - 6.0f);
	}

	// Next
	{
		text = new QGraphicsSimpleTextItem(tr("Next"));
		text->setBrush(QColor::fromRgb(0x00, 0x00, 0x00));
		text->setFont(font);
		text->setParentItem(_sprite);
		text->setPos(526.0f - text->boundingRect().width(), 80.0f - 6.0f);
	}

	// Level
	{
		text = new QGraphicsSimpleTextItem(tr("Level"));
		text->setBrush(QColor::fromRgb(0x00, 0x00, 0x00));
		text->setFont(font);
		text->setParentItem(_sprite);
		text->setPos(56.0f, 316.0f - 6.0f);
	}

	// Level
	{
		label = new QLabel();
		label->resize(48, 24);
		label->setStyleSheet("background-color: transparent; color: #FFFFFF;");
		label->setAlignment(Qt::AlignTop | Qt::AlignLeft);
		
		proxy = new QGraphicsProxyWidget();
		proxy->setParentItem(_sprite);
		proxy->setWidget(label);
		proxy->setPos(56.0f, 344.0f - 6.0f);
	
		_sprite_level = label;
	}

	// Score
	{
		text = new QGraphicsSimpleTextItem(tr("Score"));
		text->setBrush(QColor::fromRgb(0x00, 0x00, 0x00));
		text->setFont(font);
		text->setParentItem(_sprite);
		text->setPos(176.0f, 564.0f - 6.0f);
	}

	// Score
	{
		label = new QLabel();
		label->resize(148, 24);
		label->setStyleSheet("background-color: transparent; color: #FFFFFF;");
		label->setAlignment(Qt::AlignTop | Qt::AlignRight);
		
		proxy = new QGraphicsProxyWidget();
		proxy->setParentItem(_sprite);
		proxy->setWidget(label);
		proxy->setPos(256.0f, 564.0f - 6.0f);
		
		_sprite_score = label;
	}

	// Countdown
	{
		widget = new QGraphicsWidget();
		widget->resize(BLOCK_LARGE * 10, BLOCK_LARGE * 20);
		widget->setFlags(QGraphicsItem::ItemClipsChildrenToShape);
		widget->setParentItem(_sprite);
		widget->setPos(170.0f, 50.0f);

		rect = new QGraphicsRectItem(0.0f, 0.0f, BLOCK_LARGE * 10, BLOCK_LARGE * 20);
		rect->setBrush(QBrush(QColor::fromRgb(0x00, 0x00, 0x00, 0xC0)));
		rect->setParentItem(widget);

		item = new QGraphicsPixmapItem(LoaderThread::instance()->getCachedPixmap(IMAGE_COUNT[0]));
		item->setParentItem(widget);
		item->setPos(77.0f, 197.0f);
		item->setVisible(false);
		_sprite_count << item;

		item = new QGraphicsPixmapItem(LoaderThread::instance()->getCachedPixmap(IMAGE_COUNT[1]));
		item->setParentItem(widget);
		item->setPos(77.0f, 197.0f);
		item->setVisible(false);
		_sprite_count << item;

		item = new QGraphicsPixmapItem(LoaderThread::instance()->getCachedPixmap(IMAGE_COUNT[2]));
		item->setParentItem(widget);
		item->setPos(77.0f, 197.0f);
		item->setVisible(false);
		_sprite_count << item;

		widget->setVisible(false);
		
		_sprite_countdown = widget;
	}

	// Game over
	{
		widget = new QGraphicsWidget();
		widget->resize(BLOCK_LARGE * 10, BLOCK_LARGE * 20);
		widget->setFlags(QGraphicsItem::ItemClipsChildrenToShape);
		widget->setParentItem(_sprite);
		widget->setPos(170.0f, 50.0f);
		widget->setZValue(8.0f);
		
		rect = new QGraphicsRectItem(0.0f, 0.0f, BLOCK_LARGE * 10, BLOCK_LARGE * 20);
		rect->setBrush(QBrush(QColor::fromRgb(0x00, 0x00, 0x00, 0xC0)));
		rect->setParentItem(widget);
		
		item = new QGraphicsPixmapItem(LoaderThread::instance()->getCachedPixmap(IMAGE_OVER));
		item->setParentItem(widget);
		item->setPos(4.0f, 172.0f);
		
		rect = new QGraphicsRectItem(0.0f, 0.0f, BLOCK_LARGE * 10, BLOCK_LARGE * 20);
		rect->setBrush(QBrush(QColor::fromRgb(0xFF, 0xFF, 0xFF)));
		rect->setParentItem(widget);
		_sprite_overFlash = rect;
		
		widget->setVisible(false);
		
		_sprite_over = widget;
	}
}
Example #6
0
void TerrainProfileGraph::drawHoverCursor(const QPointF& position)
{
  if (_hoverLine)
  {
    _scene->removeItem(_hoverLine);
    delete _hoverLine;
    _hoverLine = 0L;
  }

  if (_graphField.width() < 2 || _graphField.height() < 2)
    return;

  double xPos = position.x() < _graphField.x() ? _graphField.x() : (position.x() > _graphField.x() + _graphField.width() ? _graphField.x() + _graphField.width() : position.x());

  QLineF vLine(xPos, _graphField.y(), xPos, _graphField.y() + _graphField.height());

  QPointF* intersect = new QPointF;
  bool foundIntersect = false;
  for (int i=0; i < _graphLines.count(); i++)
  {
    if (vLine.intersect(_graphLines[i], intersect) == QLineF::BoundedIntersection)
    {
      foundIntersect = true;
      break;
    }
  }

  if (foundIntersect)
  {
    // Draw the upper line segment.  Also serves as the parent item.
    _hoverLine = new QGraphicsLineItem(xPos, _graphField.y(), xPos, intersect->y() - 3);
    _hoverLine->setPen(_hoverPen);
    _hoverLine->setZValue(OVERLAY_Z);
    _scene->addItem(_hoverLine);

    // Draw the box around the intersect point
    QGraphicsRectItem* hoverBox = new QGraphicsRectItem(xPos - 3, intersect->y() - 3, 6, 6);
    hoverBox->setPen(_hoverPen);
    hoverBox->setBrush(Qt::NoBrush);
    hoverBox->setZValue(OVERLAY_Z);
    hoverBox->setParentItem(_hoverLine);

    // Draw the lower line segment
    QGraphicsLineItem* lowerLine = new QGraphicsLineItem(xPos, intersect->y() + 3, xPos, _graphField.y() + _graphField.height() + 5);
    lowerLine->setPen(_hoverPen);
    lowerLine->setZValue(OVERLAY_Z);
    lowerLine->setParentItem(_hoverLine);

    // Draw the text and background
    double y = (1.0 - ((intersect->y() - _graphField.y()) / _graphField.height())) * (_graphMaxY - _graphMinY) + _graphMinY;
    int textOffset = 10;

    QGraphicsSimpleTextItem* hoverText = new QGraphicsSimpleTextItem(QString::number(y) + tr("m"));
    hoverText->setBrush(QBrush(_axesColor));
    hoverText->setFont(_graphFont);
    hoverText->setZValue(OVERLAY_Z);

    if (intersect->x() + textOffset + hoverText->boundingRect().width() < _graphField.x() + _graphField.width())
      hoverText->setPos(intersect->x() + textOffset, intersect->y() - hoverText->boundingRect().height());
    else
      hoverText->setPos(intersect->x() - textOffset - hoverText->boundingRect().width(), intersect->y() - hoverText->boundingRect().height());

    QGraphicsRectItem* hoverTextBackground = new QGraphicsRectItem(hoverText->x() - 3, hoverText->y() - 1, 
                                                                   hoverText->boundingRect().width() + 6,
                                                                   hoverText->boundingRect().height() + 1);
    hoverTextBackground->setPen(_axesPen);
    hoverTextBackground->setBrush(QBrush(_graphColor));
    hoverTextBackground->setZValue(OVERLAY_Z);
    hoverTextBackground->setParentItem(_hoverLine);

    hoverText->setParentItem(_hoverLine);

    // Update callback
    if (_positionCallback.valid())
    {
      double distanceFactor = ((xPos - _graphField.x()) / (double)_graphField.width());

      osg::Vec3d worldStart, worldEnd;
      _calculator->getStart(ALTMODE_ABSOLUTE).toWorld(worldStart);
      _calculator->getEnd(ALTMODE_ABSOLUTE).toWorld(worldEnd);

      double worldX = (worldEnd.x() - worldStart.x()) * distanceFactor + worldStart.x();
      double worldY = (worldEnd.y() - worldStart.y()) * distanceFactor + worldStart.y();
      double worldZ = (worldEnd.z() - worldStart.z()) * distanceFactor + worldStart.z();

      GeoPoint mapPos;
      mapPos.fromWorld(_calculator->getStart().getSRS(), osg::Vec3d(worldX, worldY, worldZ));

      _positionCallback->updatePosition(mapPos.y(), mapPos.x(), hoverText->text().toStdString());
    }
  }
  else
  {
    // No intersect found so just draw the full line at xPos
    _hoverLine = new QGraphicsLineItem(xPos, _graphField.y(), xPos, _graphField.y() + _graphField.height() + 5);
    _hoverLine->setPen(_hoverPen);
    _hoverLine->setZValue(OVERLAY_Z);
    _scene->addItem(_hoverLine);
  }

  // Draw distance text
  double x = ((xPos - _graphField.x()) / _graphField.width()) * _totalDistance;

  BoxedSimpleTextItem* distanceText = new BoxedSimpleTextItem(QString::number(x / 1000.0, 'f', 2) + tr("km"), _backgroundColor);
  distanceText->setBrush(QBrush(_axesColor));
  distanceText->setFont(_graphFont);
  distanceText->setZValue(OVERLAY_Z);
  if(xPos - 2 - distanceText->boundingRect().width() > _graphField.x())
  {
      distanceText->setPos(xPos - 2 - distanceText->boundingRect().width(), _graphField.y() + _graphField.height() + 2);
  }
  else
  {
      distanceText->setPos(xPos + 2, _graphField.y() + _graphField.height() + 2);
  }
  distanceText->setParentItem(_hoverLine);

  // Draw selection box
  drawSelectionBox(xPos);

  delete intersect;
}
Example #7
0
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));
            }

        }
    }
}