<commit_before>/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
 * Qwt Widget Library
 * Copyright (C) 1997   <NAME>
 * Copyright (C) 2002   <NAME>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the Qwt License, Version 1.0
 *****************************************************************************/

#include "qwt_plot_barchart.h"
#include "qwt_scale_map.h"
#include "qwt_column_symbol.h"
#include "qwt_painter.h"
#include <qpainter.h>
#include <qpalette.h>

class QwtPlotBarChart::PrivateData
{
public:
    PrivateData():
        symbol( NULL )
    {
    }

    QwtColumnSymbol *symbol;
};

QwtPlotBarChart::QwtPlotBarChart( const QwtText &title ):
    QwtPlotBarItem( title )
{
    init();
}

QwtPlotBarChart::QwtPlotBarChart( const QString &title ):
    QwtPlotBarItem( QwtText( title ) )
{
    init();
}

QwtPlotBarChart::~QwtPlotBarChart()
{
    delete d_data;
}

void QwtPlotBarChart::init()
{
    d_data = new PrivateData;
    setData( new QwtPointSeriesData() );
}

//! \return QwtPlotItem::Rtti_PlotBarChart
int QwtPlotBarChart::rtti() const
{
    return QwtPlotItem::Rtti_PlotBarChart;
}

void QwtPlotBarChart::setSamples(
    const QVector<QPointF> &samples )
{
    setData( new QwtPointSeriesData( samples ) );
}

void QwtPlotBarChart::setSamples(
    const QVector<double> &samples )
{
    QVector<QPointF> points;
    for ( int i = 0; i < samples.size(); i++ )
        points += QPointF( i, samples[ i ] );

    setData( new QwtPointSeriesData( points ) );
}

void QwtPlotBarChart::setSymbol( QwtColumnSymbol *symbol )
{
    if ( symbol != d_data->symbol )
    {
        delete d_data->symbol;
        d_data->symbol = symbol;

        legendChanged();
        itemChanged();
    }
}

/*!
  \return Current symbol or NULL, when no symbol has been assigned
  \sa setSymbol()
*/
const QwtColumnSymbol *QwtPlotBarChart::symbol() const
{
    return d_data->symbol;
}

QRectF QwtPlotBarChart::boundingRect() const
{
    const size_t numSamples = dataSize();
    if ( numSamples == 0 )
        return QwtPlotSeriesItem::boundingRect();

    const double baseLine = baseline();

    QRectF rect = QwtPlotSeriesItem::boundingRect();
    if ( rect.bottom() < baseLine )
        rect.setBottom( baseLine );
    if ( rect.top() > baseLine )
        rect.setTop( baseLine );

    if ( rect.isValid() && ( orientation() == Qt::Horizontal ) )
        rect.setRect( rect.y(), rect.x(), rect.height(), rect.width() );

    return rect;
}

/*!
  Draw an interval of the bar chart

  \param painter Painter
  \param xMap Maps x-values into pixel coordinates.
  \param yMap Maps y-values into pixel coordinates.
  \param canvasRect Contents rect of the canvas
  \param from Index of the first point to be painted
  \param to Index of the last point to be painted. If to < 0 the
         curve will be painted to its last point.

  \sa drawSymbols()
*/
void QwtPlotBarChart::drawSeries( QPainter *painter,
    const QwtScaleMap &xMap, const QwtScaleMap &yMap,
    const QRectF &canvasRect, int from, int to ) const
{
    if ( to < 0 )
        to = dataSize() - 1;

    if ( from < 0 )
        from = 0;

    if ( from > to )
        return;


    const QRectF br = data()->boundingRect();
    const QwtInterval interval( br.left(), br.right() );

    painter->save();

    for ( int i = from; i <= to; i++ )
    {
        drawSample( painter, xMap, yMap,
                    canvasRect, interval, i, sample( i ) );
    }

    painter->restore();
}

/*!
  Draw a sample

  \param painter Painter
  \param xMap x map
  \param yMap y map
  \param canvasRect Contents rect of the canvas
  \param boundingInterval Bounding interval of sample values
  \param from Index of the first point to be painted
  \param to Index of the last point to be painted

  \sa drawSeries()
*/
void QwtPlotBarChart::drawSample( QPainter *painter,
    const QwtScaleMap &xMap, const QwtScaleMap &yMap,
    const QRectF &canvasRect, const QwtInterval &boundingInterval,
    int index, const QPointF &point ) const
{
    double sampleW;

    if ( orientation() == Qt::Horizontal )
    {
        sampleW = sampleWidth( yMap, canvasRect.height(),
            boundingInterval.width(), point.y() );
    }
    else
    {
        sampleW = sampleWidth( xMap, canvasRect.width(),
            boundingInterval.width(), point.y() );
    }

    // ....
    Q_UNUSED( painter );
    Q_UNUSED( sampleW );
    Q_UNUSED( index );
}

void QwtPlotBarChart::drawBar( QPainter *painter,
    int sampleIndex, const QwtColumnRect &rect ) const
{
    static Qt::GlobalColor colors[] =
        { Qt::blue, Qt::red, Qt::green, Qt::magenta, Qt::yellow };

    const int colorIndex = sampleIndex % ( sizeof( colors ) / sizeof( colors[0] ) );

    if ( d_data->symbol &&
        ( d_data->symbol->style() != QwtColumnSymbol::NoStyle ) )
    {
        d_data->symbol->setPalette( QPalette( colors[ colorIndex ] ) );
        d_data->symbol->draw( painter, rect );
    }
    else
    {
        QRectF r = rect.toRect();
        if ( QwtPainter::roundingAlignment( painter ) )
        {
            r.setLeft( qRound( r.left() ) );
            r.setRight( qRound( r.right() ) );
            r.setTop( qRound( r.top() ) );<commit_msg>starting to work on QwtPlotBarChart<commit_after>/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
 * Qwt Widget Library
 * Copyright (C) 1997   <NAME>
 * Copyright (C) 2002   <NAME>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the Qwt License, Version 1.0
 *****************************************************************************/

#include "qwt_plot_barchart.h"
#include "qwt_scale_map.h"
#include "qwt_column_symbol.h"
#include "qwt_painter.h"
#include <qpainter.h>
#include <qpalette.h>

class QwtPlotBarChart::PrivateData
{
public:
    PrivateData():
        symbol( NULL )
    {
    }

    QwtColumnSymbol *symbol;
};

QwtPlotBarChart::QwtPlotBarChart( const QwtText &title ):
    QwtPlotBarItem( title )
{
    init();
}

QwtPlotBarChart::QwtPlotBarChart( const QString &title ):
    QwtPlotBarItem( QwtText( title ) )
{
    init();
}

QwtPlotBarChart::~QwtPlotBarChart()
{
    delete d_data;
}

void QwtPlotBarChart::init()
{
    d_data = new PrivateData;
    setData( new QwtPointSeriesData() );
}

//! \return QwtPlotItem::Rtti_PlotBarChart
int QwtPlotBarChart::rtti() const
{
    return QwtPlotItem::Rtti_PlotBarChart;
}

void QwtPlotBarChart::setSamples(
    const QVector<QPointF> &samples )
{
    setData( new QwtPointSeriesData( samples ) );
}

void QwtPlotBarChart::setSamples(
    const QVector<double> &samples )
{
    QVector<QPointF> points;
    for ( int i = 0; i < samples.size(); i++ )
        points += QPointF( i, samples[ i ] );

    setData( new QwtPointSeriesData( points ) );
}

void QwtPlotBarChart::setSymbol( QwtColumnSymbol *symbol )
{
    if ( symbol != d_data->symbol )
    {
        delete d_data->symbol;
        d_data->symbol = symbol;

        legendChanged();
        itemChanged();
    }
}

/*!
  \return Current symbol or NULL, when no symbol has been assigned
  \sa setSymbol()
*/
const QwtColumnSymbol *QwtPlotBarChart::symbol() const
{
    return d_data->symbol;
}

QRectF QwtPlotBarChart::boundingRect() const
{
    const size_t numSamples = dataSize();
    if ( numSamples == 0 )
        return QwtPlotSeriesItem::boundingRect();

    const double baseLine = baseline();

    QRectF rect = QwtPlotSeriesItem::boundingRect();
    if ( rect.bottom() < baseLine )
        rect.setBottom( baseLine );
    if ( rect.top() > baseLine )
        rect.setTop( baseLine );

    if ( rect.isValid() && ( orientation() == Qt::Horizontal ) )
        rect.setRect( rect.y(), rect.x(), rect.height(), rect.width() );

    return rect;
}

/*!
  Draw an interval of the bar chart

  \param painter Painter
  \param xMap Maps x-values into pixel coordinates.
  \param yMap Maps y-values into pixel coordinates.
  \param canvasRect Contents rect of the canvas
  \param from Index of the first point to be painted
  \param to Index of the last point to be painted. If to < 0 the
         curve will be painted to its last point.

  \sa drawSymbols()
*/
void QwtPlotBarChart::drawSeries( QPainter *painter,
    const QwtScaleMap &xMap, const QwtScaleMap &yMap,
    const QRectF &canvasRect, int from, int to ) const
{
    if ( to < 0 )
        to = dataSize() - 1;

    if ( from < 0 )
        from = 0;

    if ( from > to )
        return;


    const QRectF br = data()->boundingRect();
    const QwtInterval interval( br.left(), br.right() );

    painter->save();

    for ( int i = from; i <= to; i++ )
    {
        drawSample( painter, xMap, yMap,
                    canvasRect, interval, i, sample( i ) );
    }

    painter->restore();
}

/*!
  Draw a sample

  \param painter Painter
  \param xMap x map
  \param yMap y map
  \param canvasRect Contents rect of the canvas
  \param boundingInterval Bounding interval of sample values
  \param from Index of the first point to be painted
  \param to Index of the last point to be painted

  \sa drawSeries()
*/
void QwtPlotBarChart::drawSample( QPainter *painter,
    const QwtScaleMap &xMap, const QwtScaleMap &yMap,
    const QRectF &canvasRect, const QwtInterval &boundingInterval,
    int index, const QPointF &point ) const
{
    QwtColumnRect bar;

    if ( orientation() == Qt::Horizontal )
    {
        const double barHeight = sampleWidth( yMap, canvasRect.height(),
            boundingInterval.width(), point.y() );

        const double x1 = xMap.transform( baseline() );
        const double x2 = xMap.transform( point.y() );

        const double y = yMap.transform( point.x() );
        const double y1 = y - 0.5 * barHeight;
        const double y2 = y + 0.5 * barHeight;

        bar.direction = ( x1 < x2 ) ?
            QwtColumnRect::LeftToRight : QwtColumnRect::RightToLeft;

        bar.hInterval = QwtInterval( x1, x2 ).normalized();
        bar.vInterval = QwtInterval( y1, y2 );
    }
    else
    {
        const double barWidth = sampleWidth( xMap, canvasRect.width(),
            boundingInterval.width(), point.y() );

        const double x = xMap.transform( point.x() );
        const double x1 = x - 0.5 * barWidth;
        const double x2 = x + 0.5 * barWidth;

        const double y1 = yMap.transform( baseline() );
        const double y2 = yMap.transform( point.y() );

        bar.direction = ( y1 < y2 ) ?
            QwtColumnRect::TopToBottom : QwtColumnRect::BottomToTop;

        bar.hInterval = QwtInterval( x1, x2 );
        bar.vInterval = QwtInterval( y1, y2 ).normalized();
    }

    drawBar( painter, index, bar );
}

void QwtPlotBarChart::drawBar( QPainter *painter,
    int sampleIndex, const QwtColumnRect &rect ) const
{
    static Qt::GlobalColor colors[] =
        { Qt::blue, Qt::red, Qt::green, Qt::magenta, Qt::yellow };

    const int colorIndex = sampleIndex % ( sizeof( colors ) / sizeof( colors[0] ) );

    if ( d_data->symbol &&
        ( d_data->symbol->style() != QwtColumnSymbol::NoStyle ) )
    {
        d_data->symbol->setPalette( QPalette( colors[ colorIndex ] ) );
        d_data->symbol->draw( painter, rect );
    }
    else
    {
        QRectF r = rect.toRect();
        if ( QwtPainter::roundingAlignment( painter ) )
        {
            r.setLeft( qRound( r.left() ) );
            r.setRight( qRound( r.right() ) );
            r.setTop( qRound( r.top() ) );
{'fraction_non_alphanumeric': 0.1399320379890215, 'fraction_numerical': 0.005576370131567482, 'mean_word_length': 2.8349482124958234, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5970664', 'n_tokens_mistral': 3840, 'n_tokens_neox': 3661, 'n_words': 1024}