Exemple #1
0
void tst_QGraphicsEffect::boundingRect2()
{
    CustomEffect *effect = new CustomEffect;
    QGraphicsRectItem *root = new QGraphicsRectItem;
    root->setGraphicsEffect(effect);

    QGraphicsRectItem *child = new QGraphicsRectItem;
    QRectF childRect(0, 0, 100, 100);
    child->setFlag(QGraphicsItem::ItemClipsChildrenToShape);
    child->setRect(childRect);
    child->setParentItem(root);

    QGraphicsRectItem *grandChild = new QGraphicsRectItem;
    QRectF grandChildRect(0, 0, 200, 200);
    grandChild->setRect(grandChildRect);
    grandChild->setParentItem(child);

    // Make sure the effect's bounding rect is clipped to the child's bounding rect.
    QCOMPARE(effect->boundingRect(), effect->boundingRectFor(childRect));

    // Disable ItemClipsChildrenToShape; effect's bounding rect is no longer clipped.
    child->setFlag(QGraphicsItem::ItemClipsChildrenToShape, false);
    QCOMPARE(effect->boundingRect(), effect->boundingRectFor(childRect | grandChildRect));

    // Add root item to a scene, do the same tests as above. Results should be the same.
    QGraphicsScene scene;
    scene.addItem(root);

    child->setFlag(QGraphicsItem::ItemClipsChildrenToShape);
    QCOMPARE(effect->boundingRect(), effect->boundingRectFor(childRect));

    child->setFlag(QGraphicsItem::ItemClipsChildrenToShape, false);
    QCOMPARE(effect->boundingRect(), effect->boundingRectFor(childRect | grandChildRect));

    // Now add the scene to a view, results should be the same.
    QGraphicsView view(&scene);

    child->setFlag(QGraphicsItem::ItemClipsChildrenToShape);
    QCOMPARE(effect->boundingRect(), effect->boundingRectFor(childRect));

    child->setFlag(QGraphicsItem::ItemClipsChildrenToShape, false);
    QCOMPARE(effect->boundingRect(), effect->boundingRectFor(childRect | grandChildRect));

    CustomEffect *childEffect = new CustomEffect;
    child->setGraphicsEffect(childEffect);
    QCOMPARE(effect->boundingRect(), effect->boundingRectFor(childEffect->boundingRectFor(childRect | grandChildRect)));

    child->setGraphicsEffect(0);
    QCOMPARE(effect->boundingRect(), effect->boundingRectFor(childRect | grandChildRect));
}
Exemple #2
0
void FadeMessage::setupScene()
{
    QGraphicsRectItem *parent = m_scene.addRect(0, 0, 800, 600);
    parent->setPen(Qt::NoPen);
    parent->setZValue(0);

    QGraphicsPixmapItem *bg = m_scene.addPixmap(QPixmap(":/background.jpg"));
    bg->setParentItem(parent);
    bg->setZValue(-1);

    for (int i = 1; i < 5; ++i)
        for (int j = 2; j < 5; ++j) {
            QGraphicsRectItem *item = m_scene.addRect(i * 50, (j - 1) * 50, 38, 38);
            item->setParentItem(parent);
            item->setZValue(1);
            int hue = 12 * (i * 5 + j);
            item->setBrush(QColor::fromHsv(hue, 128, 128));
        }

    QFont font;
    font.setPointSize(font.pointSize() * 2);
    font.setBold(true);
    QFontMetrics fontMetrics(font);
    int fh = fontMetrics.height();

    QString sceneText = "Qt Everywhere!";
    int sceneTextWidth = fontMetrics.width(sceneText);

    QGraphicsRectItem *block = m_scene.addRect(50, 300, sceneTextWidth, fh + 3);
    block->setPen(Qt::NoPen);
    block->setBrush(QColor(102, 153, 51));

    QGraphicsTextItem *text = m_scene.addText(sceneText, font);
    text->setDefaultTextColor(Qt::white);
    text->setPos(50, 300);
    block->setZValue(2);
    block->hide();

    text->setParentItem(block);
    m_message = block;

    m_effect = new QGraphicsColorizeEffect;
    m_effect->setColor(QColor(122, 193, 66));
    m_effect->setStrength(0);
    m_effect->setEnabled(true);
    parent->setGraphicsEffect(m_effect);

    QPushButton *press = new QPushButton;
    press->setText(tr("Press me"));
    connect(press, SIGNAL(clicked()), SLOT(togglePopup()));
    m_scene.addWidget(press);

#if defined(Q_WS_S60) || defined(Q_WS_MAEMO_5)
    press->move(200, 210);
#else
    press->move(300, 500);
#endif
}
Exemple #3
0
void Background::initSprite()
{
	_sprite = new QGraphicsWidget();
	_sprite->resize(BACKGROUND_W, BACKGROUND_H);
	_sprite->setFlags(QGraphicsItem::ItemClipsChildrenToShape);

	QGraphicsRectItem* rect;
	QGraphicsWidget* widget;

	// Background Layer 1
	widget = new QGraphicsWidget();
	widget->setParentItem(_sprite);
	widget->setPos(0.0f, -ROW_H);
	_sprite_bg << widget;

	// Background Solid 1
	rect = new QGraphicsRectItem(0.0f, 0.0f, BACKGROUND_W, BACKGROUND_H * 2.0f);
	rect->setBrush(QBrush(QColor::fromRgb(0x00, 0x80, 0xC0)));
	rect->setParentItem(widget);

	// Background Noise 1
	rect = new QGraphicsRectItem(0.0f, 0.0f, BACKGROUND_W + ROW_H, BACKGROUND_H * 2.0f);
	rect->setBrush(QBrush(LoaderThread::instance()->getCachedPixmap(IMAGE_NOISE).scaled(TEXTURE_W, TEXTURE_H)));
	rect->setParentItem(widget);
	rect->setPos(-(ROW_H * 0.5f), 0.0f);

	// Background Layer 2
	widget = new QGraphicsWidget();
	widget->setParentItem(_sprite);
	widget->setPos(0.0f, 0.0f);
	_sprite_bg << widget;

	// Background Solid 2
	rect = new QGraphicsRectItem(0.0f, 0.0f, BACKGROUND_W, BACKGROUND_H * 2.0f);
	rect->setBrush(QBrush(QColor::fromRgb(0x00, 0x80, 0xC0)));
	rect->setParentItem(widget);

	// Background Noise 2
	rect = new QGraphicsRectItem(0.0f, 0.0f, BACKGROUND_W + ROW_H, BACKGROUND_H * 2.0f);
	rect->setBrush(QBrush(LoaderThread::instance()->getCachedPixmap(IMAGE_NOISE).scaled(TEXTURE_W, TEXTURE_H)));
	rect->setParentItem(widget);
	rect->setPos(-(ROW_H * 0.5f), 0.0f);

	// Fence
	rect = new QGraphicsRectItem(0.0f, 0.0f, BACKGROUND_W, BACKGROUND_H + ROW_H);
	rect->setBrush(QBrush(LoaderThread::instance()->getCachedPixmap(IMAGE_FENCE)));
	rect->setParentItem(_sprite);
	rect->setPos(0.0f, -(ROW_H * 0.5f));

	// Background Gradient
	rect = new QGraphicsRectItem(0.0f, 0.0f, BACKGROUND_W, BACKGROUND_H);
	rect->setBrush(QBrush(LoaderThread::instance()->getCachedPixmap(IMAGE_COVER_BG)));
	rect->setParentItem(_sprite);
	
	// Foreground Gradient
	rect = new QGraphicsRectItem(0.0f, 0.0f, BACKGROUND_W, BACKGROUND_H);
	rect->setBrush(QBrush(LoaderThread::instance()->getCachedPixmap(IMAGE_COVER_FG)));
	rect->setParentItem(_sprite);
}
void TerrainProfileGraph::drawSelectionBox(double position)
{
  if (_selecting && _selectStart != position)
  {
    double selectionMin = osg::minimum(_selectStart, position);
    double selectionMax = osg::maximum(_selectStart, position);
    QGraphicsRectItem* selectionBox = new QGraphicsRectItem(selectionMin, _graphField.y(), selectionMax - selectionMin, _graphField.height());
    selectionBox->setPen(QPen(Qt::NoPen));
    selectionBox->setBrush(QBrush(QColor(0, 0, 0, 64)));
    selectionBox->setZValue(OVERLAY_Z);
    selectionBox->setParentItem(_hoverLine);
  }
}
void
ConfigurationSpaceScene::addCollision(const qreal& x, const qreal& y, const qreal& w, const qreal& h, const int& rgb)
{
	QGraphicsRectItem* rect = this->addRect(
		x - w * static_cast<qreal>(0.5),
		-y - h * static_cast<qreal>(0.5),
		w,
		h,
		QPen(Qt::NoPen),
		QBrush(QColor(rgb, rgb, rgb))
	);
	
	rect->setParentItem(this->scene);
	rect->setZValue(1);
}
Exemple #6
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;
	}
}
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;
}