<commit_before>//
// This file is part of the Marble Virtual Globe.
//
// This program is free software licensed under the GNU LGPL. You can
// find a copy of this license in LICENSE.txt in the top directory of
// the source code.
//
// Copyright 2011 <NAME> <<EMAIL>>
//

#include "ElevationProfileFloatItem.h"

#include <QThread>
#include <QtCore/QRect>
#include <QtGui/QApplication>
#include <QtGui/QPushButton>
#include <QtGui/QMenu>

#include "ui_ElevationProfileConfigWidget.h"
#include "MarbleDebug.h"
#include "MarbleLocale.h"
#include "MarbleModel.h"
#include "GeoPainter.h"
#include "ViewportParams.h"
#include "routing/RoutingManager.h"
#include "MarbleDirs.h"
#include "AltitudeModel.h"
#include "MarbleGraphicsGridLayout.h"
#include "LabelGraphicsItem.h"

namespace Marble
{

ElevationProfileFloatItem::ElevationProfileFloatItem( const QPointF &point, const QSizeF &size )
        : AbstractFloatItem( point, size ),
        m_aboutDialog( 0 ),
        m_configDialog( 0 ),
        m_target( QString() ),
        m_leftGraphMargin( 0 ),
        m_eleGraphWidth( 0 ),
        m_viewportWidth( 0 ),
        m_eleGraphHeight( 50 ),
        m_bestDivisorX( 0 ),
        m_pixelIntervalX( 0 ),
        m_valueIntervalX( 0 ),
        m_bestDivisorY( 0 ),
        m_pixelIntervalY( 0 ),
        m_valueIntervalY( 0 ),
        m_unitX( tr( "km" ) ),
        m_unitY( tr( "m") ),
        m_isInitialized( false ),
        m_contextMenu( 0 ),
        m_marbleWidget( 0 ),
        m_routingModel( 0 ),
        m_routingLayer( 0 ),
        m_routeAvailable( false ),
        m_firstVisiblePoint( 0 ),
        m_lastVisiblePoint( 0 ),
        m_zoomToViewport( false ),
        m_lastMarkerRegion( QRegion() )

{
    bool const smallScreen = MarbleGlobal::getInstance()->profiles() & MarbleGlobal::SmallScreen;
    if ( smallScreen ) {
        setPosition( QPointF( 220.0, 10.5 ) );
    }
    setPadding(1);
    m_markerIcon = new LabelGraphicsItem( &m_markerIconContainer );
    m_markerIcon->setImage( QImage( ":/flag-red-mirrored.png" ) );
    m_markerText = new LabelGraphicsItem( &m_markerTextContainer );

    MarbleGraphicsGridLayout *topLayout1 = new MarbleGraphicsGridLayout( 1, 1 );
    m_markerIconContainer.setLayout( topLayout1 );
    topLayout1->addItem( m_markerIcon, 0, 0 );

    MarbleGraphicsGridLayout *topLayout2 = new MarbleGraphicsGridLayout( 1, 1 );
    m_markerTextContainer.setLayout( topLayout2 );
    m_markerText->setFrame( RoundedRectFrame );
    topLayout2->setAlignment( Qt::AlignCenter );
    topLayout2->addItem( m_markerText, 0, 1 );
}

ElevationProfileFloatItem::~ElevationProfileFloatItem()
{
}

QStringList ElevationProfileFloatItem::backendTypes() const
{
    return QStringList( "elevationprofile" );
}

QStringList ElevationProfileFloatItem::renderPosition() const
{
    return QStringList() << "FLOAT_ITEM" << "HOVERS_ABOVE_SURFACE";
}

qreal ElevationProfileFloatItem::zValue() const
{
    return 1.0;
}

QString ElevationProfileFloatItem::name() const
{
    return tr("Elevation Profile");
}

QString ElevationProfileFloatItem::guiString() const
{
    return tr("&Elevation Profile");
}

QString ElevationProfileFloatItem::nameId() const
{
    return QString( "elevationprofile" );
}

QString ElevationProfileFloatItem::description() const
{
    return tr("This is a float item that provides an route/track elevation profile.");
}

QIcon ElevationProfileFloatItem::icon () const
{
    return QIcon(":/icons/elevationprofile.png");
}

void ElevationProfileFloatItem::initialize ()
{
    connect( marbleModel()->altitudeModel(), SIGNAL( updateAvailable() ), SLOT( updateData() ) );
}

bool ElevationProfileFloatItem::isInitialized () const
{
    return m_isInitialized;
}

QDialog *ElevationProfileFloatItem::aboutDialog()
{
    if ( !m_aboutDialog ) {
        // Initializing about dialog
        m_aboutDialog = new PluginAboutDialog();
        m_aboutDialog->setName( "Elevation Profile Plugin" );
        m_aboutDialog->setVersion( "0.1" );
        // FIXME: Can we store this string for all of Marble
        m_aboutDialog->setAboutText( tr( "<br />(c) 2009, 2010, 2011 The Marble Project<br /><br /><a href=\"http://edu.kde.org/marble\">http://edu.kde.org/marble</a>" ) );
        QList<Author> authors;
        Author esser;
        esser.name = QString::fromUtf8 ( "<NAME>" );
        esser.task = tr( "Developer" );
        esser.email = "<EMAIL>";
        authors.append( esser );
        m_aboutDialog->setAuthors( authors );
    }
    return m_aboutDialog;
}



void ElevationProfileFloatItem::changeViewport( ViewportParams *viewport )
{
    if ( !( viewport->width() == m_viewportWidth && m_isInitialized ) ) {
        m_fontHeight     = QFontMetrics( font() ).ascent();
        setContentSize( QSizeF( viewport->width() / 3, m_eleGraphHeight + m_fontHeight * 2.5 + 3 ) );
        m_leftGraphMargin = QFontMetrics( font() ).width("0000");
        m_eleGraphWidth = contentSize().width() - m_leftGraphMargin;
        m_viewportWidth = viewport->width();
        if ( !m_isInitialized ) {
            setPosition( QPointF( (viewport->width() - contentSize().width()) / 2 , 10.5 ) );
            connect( this, SIGNAL( dataUpdated() ), SLOT( forceRepaint() ) );
        }
        m_isInitialized = true;
        updateData();
    }
    update();
}



void ElevationProfileFloatItem::paintContent( GeoPainter *painter,
        ViewportParams *viewport,
        const QString& renderPos,
        GeoSceneLayer * layer )
{
    // TODO: Cleanup, reduce redundant variables etc.
    Q_UNUSED( viewport )
    Q_UNUSED( renderPos )
    Q_UNUSED( layer )

    painter->save();
    painter->setRenderHint( QPainter::Antialiasing, true );
    painter->setFont( font() );

    m_fontHeight     = QFontMetrics( font() ).ascent();

    if ( ! ( m_routeAvailable && m_isInitialized && m_eleData.size() > 0 ) ) {
        painter->setPen( QColor( Qt::black ) );
        QString text = tr( "No route available. Please create a route in the Routing Widget first." );

        QPoint pos(
            ( contentSize().width() - QFontMetrics( font() ).boundingRect( text ).width() ) / 2,
            ( contentSize().height() + m_fontHeight ) / 2
        );
        painter->drawText( pos, text );
        painter->restore();
        return;
    }
    qreal graphDistance = m_eleData.last().x();
    qreal graphAltitude = m_maxElevation;
    int   valueOffsetX = 0;<commit_msg>Shorten text, mention the purpose.<commit_after>//
// This file is part of the Marble Virtual Globe.
//
// This program is free software licensed under the GNU LGPL. You can
// find a copy of this license in LICENSE.txt in the top directory of
// the source code.
//
// Copyright 2011 <NAME> <<EMAIL>>
//

#include "ElevationProfileFloatItem.h"

#include <QThread>
#include <QtCore/QRect>
#include <QtGui/QApplication>
#include <QtGui/QPushButton>
#include <QtGui/QMenu>

#include "ui_ElevationProfileConfigWidget.h"
#include "MarbleDebug.h"
#include "MarbleLocale.h"
#include "MarbleModel.h"
#include "GeoPainter.h"
#include "ViewportParams.h"
#include "routing/RoutingManager.h"
#include "MarbleDirs.h"
#include "AltitudeModel.h"
#include "MarbleGraphicsGridLayout.h"
#include "LabelGraphicsItem.h"

namespace Marble
{

ElevationProfileFloatItem::ElevationProfileFloatItem( const QPointF &point, const QSizeF &size )
        : AbstractFloatItem( point, size ),
        m_aboutDialog( 0 ),
        m_configDialog( 0 ),
        m_target( QString() ),
        m_leftGraphMargin( 0 ),
        m_eleGraphWidth( 0 ),
        m_viewportWidth( 0 ),
        m_eleGraphHeight( 50 ),
        m_bestDivisorX( 0 ),
        m_pixelIntervalX( 0 ),
        m_valueIntervalX( 0 ),
        m_bestDivisorY( 0 ),
        m_pixelIntervalY( 0 ),
        m_valueIntervalY( 0 ),
        m_unitX( tr( "km" ) ),
        m_unitY( tr( "m") ),
        m_isInitialized( false ),
        m_contextMenu( 0 ),
        m_marbleWidget( 0 ),
        m_routingModel( 0 ),
        m_routingLayer( 0 ),
        m_routeAvailable( false ),
        m_firstVisiblePoint( 0 ),
        m_lastVisiblePoint( 0 ),
        m_zoomToViewport( false ),
        m_lastMarkerRegion( QRegion() )

{
    bool const smallScreen = MarbleGlobal::getInstance()->profiles() & MarbleGlobal::SmallScreen;
    if ( smallScreen ) {
        setPosition( QPointF( 220.0, 10.5 ) );
    }
    setPadding(1);
    m_markerIcon = new LabelGraphicsItem( &m_markerIconContainer );
    m_markerIcon->setImage( QImage( ":/flag-red-mirrored.png" ) );
    m_markerText = new LabelGraphicsItem( &m_markerTextContainer );

    MarbleGraphicsGridLayout *topLayout1 = new MarbleGraphicsGridLayout( 1, 1 );
    m_markerIconContainer.setLayout( topLayout1 );
    topLayout1->addItem( m_markerIcon, 0, 0 );

    MarbleGraphicsGridLayout *topLayout2 = new MarbleGraphicsGridLayout( 1, 1 );
    m_markerTextContainer.setLayout( topLayout2 );
    m_markerText->setFrame( RoundedRectFrame );
    topLayout2->setAlignment( Qt::AlignCenter );
    topLayout2->addItem( m_markerText, 0, 1 );
}

ElevationProfileFloatItem::~ElevationProfileFloatItem()
{
}

QStringList ElevationProfileFloatItem::backendTypes() const
{
    return QStringList( "elevationprofile" );
}

QStringList ElevationProfileFloatItem::renderPosition() const
{
    return QStringList() << "FLOAT_ITEM" << "HOVERS_ABOVE_SURFACE";
}

qreal ElevationProfileFloatItem::zValue() const
{
    return 1.0;
}

QString ElevationProfileFloatItem::name() const
{
    return tr("Elevation Profile");
}

QString ElevationProfileFloatItem::guiString() const
{
    return tr("&Elevation Profile");
}

QString ElevationProfileFloatItem::nameId() const
{
    return QString( "elevationprofile" );
}

QString ElevationProfileFloatItem::description() const
{
    return tr("This is a float item that provides an route/track elevation profile.");
}

QIcon ElevationProfileFloatItem::icon () const
{
    return QIcon(":/icons/elevationprofile.png");
}

void ElevationProfileFloatItem::initialize ()
{
    connect( marbleModel()->altitudeModel(), SIGNAL( updateAvailable() ), SLOT( updateData() ) );
}

bool ElevationProfileFloatItem::isInitialized () const
{
    return m_isInitialized;
}

QDialog *ElevationProfileFloatItem::aboutDialog()
{
    if ( !m_aboutDialog ) {
        // Initializing about dialog
        m_aboutDialog = new PluginAboutDialog();
        m_aboutDialog->setName( "Elevation Profile Plugin" );
        m_aboutDialog->setVersion( "0.1" );
        // FIXME: Can we store this string for all of Marble
        m_aboutDialog->setAboutText( tr( "<br />(c) 2009, 2010, 2011 The Marble Project<br /><br /><a href=\"http://edu.kde.org/marble\">http://edu.kde.org/marble</a>" ) );
        QList<Author> authors;
        Author esser;
        esser.name = QString::fromUtf8 ( "<NAME>" );
        esser.task = tr( "Developer" );
        esser.email = "<EMAIL>";
        authors.append( esser );
        m_aboutDialog->setAuthors( authors );
    }
    return m_aboutDialog;
}



void ElevationProfileFloatItem::changeViewport( ViewportParams *viewport )
{
    if ( !( viewport->width() == m_viewportWidth && m_isInitialized ) ) {
        m_fontHeight     = QFontMetrics( font() ).ascent();
        setContentSize( QSizeF( viewport->width() / 3, m_eleGraphHeight + m_fontHeight * 2.5 + 3 ) );
        m_leftGraphMargin = QFontMetrics( font() ).width("0000");
        m_eleGraphWidth = contentSize().width() - m_leftGraphMargin;
        m_viewportWidth = viewport->width();
        if ( !m_isInitialized ) {
            setPosition( QPointF( (viewport->width() - contentSize().width()) / 2 , 10.5 ) );
            connect( this, SIGNAL( dataUpdated() ), SLOT( forceRepaint() ) );
        }
        m_isInitialized = true;
        updateData();
    }
    update();
}



void ElevationProfileFloatItem::paintContent( GeoPainter *painter,
        ViewportParams *viewport,
        const QString& renderPos,
        GeoSceneLayer * layer )
{
    // TODO: Cleanup, reduce redundant variables etc.
    Q_UNUSED( viewport )
    Q_UNUSED( renderPos )
    Q_UNUSED( layer )

    painter->save();
    painter->setRenderHint( QPainter::Antialiasing, true );
    painter->setFont( font() );

    m_fontHeight     = QFontMetrics( font() ).ascent();

    if ( ! ( m_routeAvailable && m_isInitialized && m_eleData.size() > 0 ) ) {
        painter->setPen( QColor( Qt::black ) );
        QString text = tr( "Create a route to view its elevation profile." );

        QPoint pos(
            ( contentSize().width() - QFontMetrics( font() ).boundingRect( text ).width() ) / 2,
            ( contentSize().height() + m_fontHeight ) / 2
        );
        painter->drawText( pos, text );
        painter->restore();
        return;
    }
    qreal graphDistance = m_eleData.last().x();
    qreal graphAltitude = m_maxElevation;
    int   valueOffsetX = 0;
{'fraction_non_alphanumeric': 0.12669789227166275, 'fraction_numerical': 0.012334113973458237, 'mean_word_length': 3.2803207484129637, 'pattern_counts': {'":': 4, '<': 43, '<?xml version=': 0, '>': 79, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7487523', 'n_tokens_mistral': 4200, 'n_tokens_neox': 4014, 'n_words': 893}