/*!
  \brief Assign a scale division

  The scale division determines where to set the tick marks.

  \param transformation Transformation, needed to translate between
                        scale and pixal values
  \param scaleDiv Scale Division
  \sa For more information about scale divisions, see QwtScaleDiv.
*/
void QwtScaleWidget::setScaleDiv(
    QwtScaleTransformation *transformation,
    const QwtScaleDiv &scaleDiv )
{
    QwtScaleDraw *sd = d_data->scaleDraw;
    if ( sd->scaleDiv() != scaleDiv ||
        sd->scaleMap().transformation()->type() != transformation->type() )
    {
        sd->setTransformation( transformation );
        sd->setScaleDiv( scaleDiv );
        layoutScale();

        Q_EMIT scaleDivChanged();
    }
    else
    {
        /*
          The transformation doesn't anything different as the 
          previous one. So we better throw it silently away instead of 
          initiating heavy updates
         */

        delete transformation;
    }
}
/*!
  \brief Assign a scale division

  The scale division determines where to set the tick marks.

  \param scaleDiv Scale Division
  \sa For more information about scale divisions, see QwtScaleDiv.
*/
void QwtScaleWidget::setScaleDiv( const QwtScaleDiv &scaleDiv )
{
    QwtScaleDraw *sd = d_data->scaleDraw;
    if ( sd->scaleDiv() != scaleDiv )
    {
        sd->setScaleDiv( scaleDiv );
        layoutScale();

        Q_EMIT scaleDivChanged();
    }
}
/*!
  \brief Assign a scale division

  The scale division determines where to set the tick marks.

  \param transformation Transformation, needed to translate between
                        scale and pixal values
  \param scaleDiv Scale Division
  \sa For more information about scale divisions, see QwtScaleDiv.
*/
void QwtScaleWidget::setScaleDiv(
    const QwtScaleTransformation& transformation,
    const QwtScaleDiv &scaleDiv)
{
    QwtScaleDraw *sd = d_data->scaleDraw;
    if (sd->scaleDiv() != scaleDiv ||
        sd->map().transformation().xForm != transformation.xForm )
    {
        sd->setTransformation(transformation);
        sd->setScaleDiv(scaleDiv);
        layoutScale();

        emit scaleDivChanged();
    }
}
/*!
  \brief Assign a scale division

  The scale division determines where to set the tick marks.

  \param transformation Transformation, needed to translate between
                        scale and pixal values
  \param scaleDiv Scale Division
  \sa For more information about scale divisions, see QwtScaleDiv.
*/
void QwtScaleWidget::setScaleDiv(
    QwtScaleTransformation *transformation,
    const QwtScaleDiv &scaleDiv)
{
    QwtScaleDraw *sd = d_data->scaleDraw;
    if (sd->scaleDiv() != scaleDiv ||
        sd->map().transformation()->type() != transformation->type() )
    {
        sd->setTransformation(transformation);
        sd->setScaleDiv(scaleDiv);
        layoutScale();

        emit scaleDivChanged();
    }
    else
        delete transformation;
}
void QwtPlotScaleItem::updateScaleDiv( const QwtScaleDiv& xScaleDiv,
    const QwtScaleDiv& yScaleDiv )
{
    QwtScaleDraw *scaleDraw = d_data->scaleDraw;

    if ( d_data->scaleDivFromAxis && scaleDraw )
    {
        const QwtScaleDiv &scaleDiv = 
            scaleDraw->orientation() == Qt::Horizontal ? xScaleDiv : yScaleDiv;

        const QwtPlot *plt = plot();
        if ( plt != NULL )
        {
            const QRectF canvasRect = plt->canvas()->contentsRect();

            const QwtInterval interval = d_data->scaleInterval( 
                canvasRect, plt->canvasMap( xAxis() ), plt->canvasMap( yAxis() ) );

            QwtScaleDiv sd = scaleDiv;
            sd.setInterval( interval );

            if ( sd != scaleDraw->scaleDiv() )
            {
                // the internal label cache of QwtScaleDraw
                // is cleared here, so better avoid pointless
                // assignments.

                scaleDraw->setScaleDiv( sd );
            }
        }
        else
        {
            scaleDraw->setScaleDiv( scaleDiv );
        }
    }
}
/*!
  \brief Draw the scale
*/
void QwtPlotScaleItem::draw( QPainter *painter,
    const QwtScaleMap &xMap, const QwtScaleMap &yMap,
    const QRectF &canvasRect ) const
{
    QwtScaleDraw *sd = d_data->scaleDraw;

    if ( d_data->scaleDivFromAxis )
    {
        const QwtInterval interval = 
            d_data->scaleInterval( canvasRect, xMap, yMap );

        if ( interval != sd->scaleDiv().interval() )
        {
            QwtScaleDiv scaleDiv = sd->scaleDiv();
            scaleDiv.setInterval( interval );
            sd->setScaleDiv( scaleDiv );
        }
    }

    QPen pen = painter->pen();
    pen.setStyle( Qt::SolidLine );
    painter->setPen( pen );

    if ( sd->orientation() == Qt::Horizontal )
    {
        double y;
        if ( d_data->borderDistance >= 0 )
        {
            if ( sd->alignment() == QwtScaleDraw::BottomScale )
                y = canvasRect.top() + d_data->borderDistance;
            else
            {
                y = canvasRect.bottom() - d_data->borderDistance;
            }

        }
        else
        {
            y = yMap.transform( d_data->position );
        }

        if ( y < canvasRect.top() || y > canvasRect.bottom() )
            return;

        sd->move( canvasRect.left(), y );
        sd->setLength( canvasRect.width() - 1 );

        QwtTransform *transform = NULL;
        if ( xMap.transformation() )
            transform = xMap.transformation()->copy();

        sd->setTransformation( transform );
    }
    else // == Qt::Vertical
    {
        double x;
        if ( d_data->borderDistance >= 0 )
        {
            if ( sd->alignment() == QwtScaleDraw::RightScale )
                x = canvasRect.left() + d_data->borderDistance;
            else
            {
                x = canvasRect.right() - d_data->borderDistance;
            }
        }
        else
        {
            x = xMap.transform( d_data->position );
        }
        if ( x < canvasRect.left() || x > canvasRect.right() )
            return;

        sd->move( x, canvasRect.top() );
        sd->setLength( canvasRect.height() - 1 );

        QwtTransform *transform = NULL;
        if ( yMap.transformation() )
            transform = yMap.transformation()->copy();

        sd->setTransformation( transform );
    }

    painter->setFont( d_data->font );

    sd->draw( painter, d_data->palette );
}
/*!
  Draw the grid and axes

  \param painter Painter
  \param azimuthMap Maps azimuth values to values related to 0.0, M_2PI
  \param radialMap Maps radius values into painter coordinates.
  \param pole Position of the pole in painter coordinates
  \param radius Radius of the complete plot area in painter coordinates
  \param canvasRect Contents rect of the canvas in painter coordinates
*/
void QwtPolarGrid::draw( QPainter *painter,
                         const QwtScaleMap &azimuthMap, const QwtScaleMap &radialMap,
                         const QwtDoublePoint &pole, double radius,
                         const QwtDoubleRect &canvasRect ) const
{
  updateScaleDraws( azimuthMap, radialMap, pole, radius );

  painter->save();

  if ( testDisplayFlag( ClipAxisBackground ) )
  {
    QRegion clipRegion( canvasRect.toRect() );
    for ( int axisId = 0; axisId < QwtPolar::AxesCount; axisId++ )
    {
      const AxisData &axis = d_data->axisData[axisId];
      if ( axisId != QwtPolar::AxisAzimuth && axis.isVisible )
      {
        QwtScaleDraw *scaleDraw = ( QwtScaleDraw * )axis.scaleDraw;
        if ( scaleDraw->hasComponent( QwtScaleDraw::Labels ) )
        {
          const QwtValueList &ticks =
            scaleDraw->scaleDiv().ticks( QwtScaleDiv::MajorTick );
          for ( int i = 0; i < int( ticks.size() ); i++ )
          {
            QRect labelRect =
              scaleDraw->boundingLabelRect( axis.font, ticks[i] );

            const int margin = 2;
            labelRect.setRect(
              labelRect.x() - margin,
              labelRect.y() - margin,
              labelRect.width() + 2 * margin,
              labelRect.height() + 2 * margin
            );

            if ( labelRect.isValid() )
              clipRegion -= QRegion( labelRect );
          }
        }
      }
    }
    painter->setClipRegion( clipRegion );
  }

  //  draw radial grid

  const GridData &radialGrid = d_data->gridData[QwtPolar::Radius];
  if ( radialGrid.isVisible && radialGrid.isMinorVisible )
  {
    painter->setPen( radialGrid.minorPen );

    drawCircles( painter, canvasRect, pole, radialMap,
                 radialGrid.scaleDiv.ticks( QwtScaleDiv::MinorTick ) );
    drawCircles( painter, canvasRect, pole, radialMap,
                 radialGrid.scaleDiv.ticks( QwtScaleDiv::MediumTick ) );
  }
  if ( radialGrid.isVisible )
  {
    painter->setPen( radialGrid.majorPen );

    drawCircles( painter, canvasRect, pole, radialMap,
                 radialGrid.scaleDiv.ticks( QwtScaleDiv::MajorTick ) );
  }

  // draw azimuth grid

  const GridData &azimuthGrid =
    d_data->gridData[QwtPolar::Azimuth];

  if ( azimuthGrid.isVisible && azimuthGrid.isMinorVisible )
  {
    painter->setPen( azimuthGrid.minorPen );

    drawRays( painter, canvasRect, pole, radius, azimuthMap,
              azimuthGrid.scaleDiv.ticks( QwtScaleDiv::MinorTick ) );
    drawRays( painter, canvasRect, pole, radius, azimuthMap,
              azimuthGrid.scaleDiv.ticks( QwtScaleDiv::MediumTick ) );
  }
  if ( azimuthGrid.isVisible )
  {
    painter->setPen( azimuthGrid.majorPen );

    drawRays( painter, canvasRect, pole, radius, azimuthMap,
              azimuthGrid.scaleDiv.ticks( QwtScaleDiv::MajorTick ) );
  }
  painter->restore();

  for ( int axisId = 0; axisId < QwtPolar::AxesCount; axisId++ )
  {
    const AxisData &axis = d_data->axisData[axisId];
    if ( axis.isVisible )
    {
      painter->save();
      drawAxis( painter, axisId );
      painter->restore();
    }
  }
}