Esempio n. 1
0
void RingChart::drawRing(QPainter &p, const QPoint &center, int ring, int radius1, int radius2, bool checkHighlight, double mouseAngle)
{
	int row_count = m_model->rowCount();

	// calculate segments
    double totalValue = 0;

    for (int r = 0; r < row_count; r++)
    {
        const QModelIndex index(m_model->index(r, ring));
        double value = m_model->data(index).toDouble();

        if (value > 0.0)
            totalValue += value;
    }

	// draw segments
	double startAngle = 0.0;
    bool isHighlighted = false;
    double angleHl1, angleHl2, valueHl;
    QModelIndex indexHl;

	// outer rect
	QRect pieRect(center.x() - radius2, center.y() - radius2, radius2 * 2, radius2 * 2);

    for (int r = 0; r < row_count; r++)
    {
        const QModelIndex index(m_model->index(r, ring));
        double value = m_model->data(index).toDouble();

        if (value > 0.0) {
            double angle = 360 * value / totalValue;



            if (checkHighlight && startAngle <= mouseAngle && mouseAngle <= (startAngle + angle))
            {
                isHighlighted = true;
                angleHl1 = startAngle;
                angleHl2 = angle;
                valueHl = value;
                indexHl = index;
            }
            else
            {            
				drawSegment(p, pieRect, index, value, startAngle, angle, false);
                drawValue(p, pieRect, index, value, startAngle, angle, false);
            }

            startAngle += angle;
        }
    }

    // highlight to be drawn over the other segments
    if (isHighlighted)
    {
        setIndexUnderMouse(indexHl);

        drawSegment(p, pieRect, indexHl, valueHl, angleHl1, angleHl2, true);
        drawValue(p, pieRect, indexHl, valueHl, angleHl1, angleHl2, true);
    }
    else
        setIndexUnderMouse(QModelIndex());
}
Esempio n. 2
0
void QgsPaperItem::paint( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle, QWidget* pWidget )
{
  Q_UNUSED( itemStyle );
  Q_UNUSED( pWidget );
  if ( !painter )
  {
    return;
  }

  drawBackground( painter );

  //draw grid

  if ( mComposition )
  {
    if ( mComposition->snapToGridEnabled() && mComposition->plotStyle() ==  QgsComposition::Preview
         && mComposition->snapGridResolution() > 0 )
    {
      int gridMultiplyX = ( int )( mComposition->snapGridOffsetX() / mComposition->snapGridResolution() );
      int gridMultiplyY = ( int )( mComposition->snapGridOffsetY() / mComposition->snapGridResolution() );
      double currentXCoord = mComposition->snapGridOffsetX() - gridMultiplyX * mComposition->snapGridResolution();
      double currentYCoord;
      double minYCoord = mComposition->snapGridOffsetY() - gridMultiplyY * mComposition->snapGridResolution();

      if ( mComposition->gridStyle() == QgsComposition::Solid )
      {
        painter->setPen( mComposition->gridPen() );

        //draw vertical lines


        for ( ; currentXCoord <= rect().width(); currentXCoord += mComposition->snapGridResolution() )
        {
          painter->drawLine( QPointF( currentXCoord, 0 ), QPointF( currentXCoord, rect().height() ) );
        }

        //draw horizontal lines
        currentYCoord = minYCoord;
        for ( ; currentYCoord <= rect().height(); currentYCoord += mComposition->snapGridResolution() )
        {
          painter->drawLine( QPointF( 0, currentYCoord ), QPointF( rect().width(), currentYCoord ) );
        }
      }
      else //'Dots' or 'Crosses'
      {
        QPen gridPen = mComposition->gridPen();
        painter->setPen( gridPen );
        painter->setBrush( QBrush( gridPen.color() ) );
        double halfCrossLength = mComposition->snapGridResolution() / 6;

        for ( ; currentXCoord <= rect().width(); currentXCoord += mComposition->snapGridResolution() )
        {
          currentYCoord = minYCoord;
          for ( ; currentYCoord <= rect().height(); currentYCoord += mComposition->snapGridResolution() )
          {
            if ( mComposition->gridStyle() == QgsComposition::Dots )
            {
              QRectF pieRect( currentXCoord - gridPen.widthF() / 2, currentYCoord - gridPen.widthF() / 2, gridPen.widthF(), gridPen.widthF() );
              painter->drawChord( pieRect, 0, 5760 );
            }
            else if ( mComposition->gridStyle() == QgsComposition::Crosses )
            {
              painter->drawLine( QPointF( currentXCoord - halfCrossLength, currentYCoord ), QPointF( currentXCoord + halfCrossLength, currentYCoord ) );
              painter->drawLine( QPointF( currentXCoord, currentYCoord - halfCrossLength ), QPointF( currentXCoord, currentYCoord + halfCrossLength ) );
            }
          }
        }
      }
    }
  }
}