<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 2012   <NAME>      <<EMAIL>>
// Copyright 2012   <NAME>   <<EMAIL>>
// Copyright 2012   <NAME>  <<EMAIL>>
// Copyright 2012   <NAME> <<EMAIL>>
//

#include "PopupItem.h"
#include "MarbleWidget.h"

#include <QtWebKit/QWebView>
#include <QtGui/QMouseEvent>
#include <QtGui/QApplication>
#include <QtGui/QVBoxLayout>
#include <QtGui/QLabel>
#include <QtGui/QPushButton>
#include <QtGui/QPixmapCache>
#include <qdrawutil.h>

namespace Marble
{

PopupItem::PopupItem( QObject* parent ) :
    QObject( parent ),
    BillboardGraphicsItem(),
    m_widget( new QWidget ),
    m_webView( new QWebView ( m_widget ) ),
    m_textColor( QColor(Qt::black) ),
    m_backColor( QColor(Qt::white) ),
    m_needMouseRelease(false)
{
    setVisible( false );

    QGridLayout *childLayout = new QGridLayout;
    m_titleText = new QLabel( m_widget );
    childLayout->addWidget( m_titleText, 0, 0 );
    QPushButton *hideButton = new QPushButton( m_widget );
    hideButton->setIcon( QIcon( ":/marble/webpopup/icon-remove.png" ) );
    hideButton->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
    hideButton->setCursor( QCursor( Qt::PointingHandCursor ) );
    hideButton->setFlat( true );
    childLayout->addWidget( hideButton, 0, 1 );
    QVBoxLayout *layout = new QVBoxLayout( m_widget );
    layout->addLayout( childLayout );
    layout->addWidget( m_webView );
    m_widget->setLayout( layout );
    m_widget->setAttribute( Qt::WA_NoSystemBackground, true );
    setSize( QSizeF( 300, 350 ) );
    QPalette palette = m_webView->palette();
    palette.setBrush(QPalette::Base, Qt::transparent);
    m_webView->setPalette(palette);
    m_webView->page()->setPalette(palette);
    m_webView->setAttribute(Qt::WA_OpaquePaintEvent, false);

    connect( m_webView, SIGNAL(titleChanged(QString)), m_titleText, SLOT(setText(QString)) );
    connect( hideButton, SIGNAL(clicked()), this, SIGNAL(hide()) );
}

PopupItem::~PopupItem()
{
    delete m_widget;
}

void PopupItem::setUrl( const QUrl &url )
{
    if ( m_webView ) {
        m_webView->setUrl( url );
        setVisible( true );

	QPalette palette = m_webView->palette();
	palette.setBrush(QPalette::Base, Qt::transparent);
	m_webView->setPalette(palette);
	m_webView->page()->setPalette(palette);
	m_webView->setAttribute(Qt::WA_OpaquePaintEvent, false);

        emit dirty();
    }
}

void PopupItem::setContent( const QString &html )
{
    m_content = html;
    m_webView->setHtml(html);
}

void PopupItem::setTextColor(const QColor &color)
{
    if(color.isValid() && m_titleText != 0) {
        m_textColor = color;
        QPalette palette(m_titleText->palette());
        palette.setColor(QPalette::WindowText, m_textColor);
        m_titleText->setPalette(palette);
    }
}
void PopupItem::setBackgroundColor(const QColor &color)
{
    if(color.isValid()) {
        m_backColor = color;
        QPixmapCache::remove( "marble/webpopup/webpopup2" );
        QPixmapCache::remove( "marble/webpopup/arrow2_topleft" );
        QPixmapCache::remove( "marble/webpopup/arrow2_bottomleft" );
        QPixmapCache::remove( "marble/webpopup/arrow2_topright" );
        QPixmapCache::remove( "marble/webpopup/arrow2_bottomright" );
    }
}

void PopupItem::colorize( QImage &img, const QColor &col )
{
    if (img.depth() <= 8) return;
    int pixels = img.width()*img.height();
    unsigned int *data = (unsigned int *) img.bits();
    for (int i=0; i < pixels; ++i) {
        int val = qGray(data[i]);
        data[i] = qRgba(col.red()*val/255,col.green()*val/255, col.blue()*val/255, qAlpha(data[i]));
    }
}

void PopupItem::paint( QPainter *painter )
{
    QRect popupRect( -10, -10, size().width(), size().height() );
    qDrawBorderPixmap(painter, popupRect, QMargins( 20, 20, 20, 20 ),
                      pixmap("marble/webpopup/webpopup2"));

    if ( alignment() & Qt::AlignRight ) {
        if ( alignment() & Qt::AlignTop ) {
            QPixmap const image = pixmap("marble/webpopup/arrow2_topleft");
            painter->drawPixmap( - ( image.width() - 3 ), 0, image );
        } else if ( alignment() & Qt::AlignBottom ) {
            QPixmap const image = pixmap("marble/webpopup/arrow2_bottomleft");
            painter->drawPixmap( - ( image.width() - 3 ),
                                size().height() - image.height(),
                                image );
        } else { // for no horizontal align value and Qt::AlignVCenter
            QPixmap const image = pixmap("marble/webpopup/arrow2_topleft");
            painter->drawPixmap( - ( image.width() - 3 ),
                                size().height() / 2 - image.height() / 2,
                                image );
        }
    } else if ( alignment() & Qt::AlignLeft ) {
        if ( alignment() & Qt::AlignTop ) {
            QPixmap const image = pixmap("marble/webpopup/arrow2_topright");
            painter->drawPixmap( size().width() - 23, 0, image );
        } else if ( alignment() & Qt::AlignBottom ) {
            QPixmap const image = pixmap("marble/webpopup/arrow2_bottomright");
            painter->drawPixmap( size().width() - 23, size().height() - image.height(), image );
        } else { // for no horizontal align value and Qt::AlignVCenter
            QPixmap const image = pixmap("marble/webpopup/arrow2_topright");
            painter->drawPixmap( size().width() - 23, size().height() / 2 - image.height() / 2, image );
        }
    }

    m_widget->setFixedSize( size().toSize() - QSize( 20, 20 ) );
    m_widget->render( painter, QPoint( 0, 0 ), QRegion() );
}

bool PopupItem::eventFilter( QObject *object, QEvent *e )
{
    MarbleWidget *widget = dynamic_cast<MarbleWidget*> ( object );
    if ( !widget ) {
        return BillboardGraphicsItem::eventFilter( object, e );
    }

    if ( e->type() == QEvent::MouseButtonDblClick
            || e->type() == QEvent::MouseMove
            || e->type() == QEvent::MouseButtonPress
            || e->type() == QEvent::MouseButtonRelease )
    {
        // Mouse events are forwarded to the underlying widget
        QMouseEvent *event = static_cast<QMouseEvent*> ( e );
        QPoint shiftedPos = event->pos();
        QWidget* child = transform( shiftedPos );
        bool const forcedMouseRelease = m_needMouseRelease && e->type() == QEvent::MouseButtonRelease;
        if ( child || forcedMouseRelease ) {
            if ( !m_needMouseRelease && e->type() == QEvent::MouseButtonPress ) {
                m_needMouseRelease = true;
            } else if ( forcedMouseRelease ) {<commit_msg>Don't leave a margin of 20 pixel to the right/bottom<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 2012   <NAME>      <<EMAIL>>
// Copyright 2012   <NAME>   <<EMAIL>>
// Copyright 2012   <NAME>  <<EMAIL>>
// Copyright 2012   <NAME> <<EMAIL>>
//

#include "PopupItem.h"
#include "MarbleWidget.h"

#include <QtWebKit/QWebView>
#include <QtGui/QMouseEvent>
#include <QtGui/QApplication>
#include <QtGui/QVBoxLayout>
#include <QtGui/QLabel>
#include <QtGui/QPushButton>
#include <QtGui/QPixmapCache>
#include <qdrawutil.h>

namespace Marble
{

PopupItem::PopupItem( QObject* parent ) :
    QObject( parent ),
    BillboardGraphicsItem(),
    m_widget( new QWidget ),
    m_webView( new QWebView ( m_widget ) ),
    m_textColor( QColor(Qt::black) ),
    m_backColor( QColor(Qt::white) ),
    m_needMouseRelease(false)
{
    setVisible( false );

    QGridLayout *childLayout = new QGridLayout;
    m_titleText = new QLabel( m_widget );
    childLayout->addWidget( m_titleText, 0, 0 );
    QPushButton *hideButton = new QPushButton( m_widget );
    hideButton->setIcon( QIcon( ":/marble/webpopup/icon-remove.png" ) );
    hideButton->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
    hideButton->setCursor( QCursor( Qt::PointingHandCursor ) );
    hideButton->setFlat( true );
    childLayout->addWidget( hideButton, 0, 1 );
    QVBoxLayout *layout = new QVBoxLayout( m_widget );
    layout->addLayout( childLayout );
    layout->addWidget( m_webView );
    m_widget->setLayout( layout );
    m_widget->setAttribute( Qt::WA_NoSystemBackground, true );
    setSize( QSizeF( 300, 350 ) );
    QPalette palette = m_webView->palette();
    palette.setBrush(QPalette::Base, Qt::transparent);
    m_webView->setPalette(palette);
    m_webView->page()->setPalette(palette);
    m_webView->setAttribute(Qt::WA_OpaquePaintEvent, false);

    connect( m_webView, SIGNAL(titleChanged(QString)), m_titleText, SLOT(setText(QString)) );
    connect( hideButton, SIGNAL(clicked()), this, SIGNAL(hide()) );
}

PopupItem::~PopupItem()
{
    delete m_widget;
}

void PopupItem::setUrl( const QUrl &url )
{
    if ( m_webView ) {
        m_webView->setUrl( url );
        setVisible( true );

	QPalette palette = m_webView->palette();
	palette.setBrush(QPalette::Base, Qt::transparent);
	m_webView->setPalette(palette);
	m_webView->page()->setPalette(palette);
	m_webView->setAttribute(Qt::WA_OpaquePaintEvent, false);

        emit dirty();
    }
}

void PopupItem::setContent( const QString &html )
{
    m_content = html;
    m_webView->setHtml(html);
}

void PopupItem::setTextColor(const QColor &color)
{
    if(color.isValid() && m_titleText != 0) {
        m_textColor = color;
        QPalette palette(m_titleText->palette());
        palette.setColor(QPalette::WindowText, m_textColor);
        m_titleText->setPalette(palette);
    }
}
void PopupItem::setBackgroundColor(const QColor &color)
{
    if(color.isValid()) {
        m_backColor = color;
        QPixmapCache::remove( "marble/webpopup/webpopup2" );
        QPixmapCache::remove( "marble/webpopup/arrow2_topleft" );
        QPixmapCache::remove( "marble/webpopup/arrow2_bottomleft" );
        QPixmapCache::remove( "marble/webpopup/arrow2_topright" );
        QPixmapCache::remove( "marble/webpopup/arrow2_bottomright" );
    }
}

void PopupItem::colorize( QImage &img, const QColor &col )
{
    if (img.depth() <= 8) return;
    int pixels = img.width()*img.height();
    unsigned int *data = (unsigned int *) img.bits();
    for (int i=0; i < pixels; ++i) {
        int val = qGray(data[i]);
        data[i] = qRgba(col.red()*val/255,col.green()*val/255, col.blue()*val/255, qAlpha(data[i]));
    }
}

void PopupItem::paint( QPainter *painter )
{
    QRect popupRect( -10, -10, size().width()+20, size().height()+20 );
    qDrawBorderPixmap(painter, popupRect, QMargins( 20, 20, 20, 20 ),
                      pixmap("marble/webpopup/webpopup2"));

    if ( alignment() & Qt::AlignRight ) {
        if ( alignment() & Qt::AlignTop ) {
            QPixmap const image = pixmap("marble/webpopup/arrow2_topleft");
            painter->drawPixmap( - ( image.width() - 3 ), 0, image );
        } else if ( alignment() & Qt::AlignBottom ) {
            QPixmap const image = pixmap("marble/webpopup/arrow2_bottomleft");
            painter->drawPixmap( - ( image.width() - 3 ),
                                size().height() - image.height(),
                                image );
        } else { // for no horizontal align value and Qt::AlignVCenter
            QPixmap const image = pixmap("marble/webpopup/arrow2_topleft");
            painter->drawPixmap( - ( image.width() - 3 ),
                                size().height() / 2 - image.height() / 2,
                                image );
        }
    } else if ( alignment() & Qt::AlignLeft ) {
        if ( alignment() & Qt::AlignTop ) {
            QPixmap const image = pixmap("marble/webpopup/arrow2_topright");
            painter->drawPixmap( size().width() - 23, 0, image );
        } else if ( alignment() & Qt::AlignBottom ) {
            QPixmap const image = pixmap("marble/webpopup/arrow2_bottomright");
            painter->drawPixmap( size().width() - 23, size().height() - image.height(), image );
        } else { // for no horizontal align value and Qt::AlignVCenter
            QPixmap const image = pixmap("marble/webpopup/arrow2_topright");
            painter->drawPixmap( size().width() - 23, size().height() / 2 - image.height() / 2, image );
        }
    }

    m_widget->setFixedSize( size().toSize() );
    m_widget->render( painter, QPoint( 0, 0 ), QRegion() );
}

bool PopupItem::eventFilter( QObject *object, QEvent *e )
{
    MarbleWidget *widget = dynamic_cast<MarbleWidget*> ( object );
    if ( !widget ) {
        return BillboardGraphicsItem::eventFilter( object, e );
    }

    if ( e->type() == QEvent::MouseButtonDblClick
            || e->type() == QEvent::MouseMove
            || e->type() == QEvent::MouseButtonPress
            || e->type() == QEvent::MouseButtonRelease )
    {
        // Mouse events are forwarded to the underlying widget
        QMouseEvent *event = static_cast<QMouseEvent*> ( e );
        QPoint shiftedPos = event->pos();
        QWidget* child = transform( shiftedPos );
        bool const forcedMouseRelease = m_needMouseRelease && e->type() == QEvent::MouseButtonRelease;
        if ( child || forcedMouseRelease ) {
            if ( !m_needMouseRelease && e->type() == QEvent::MouseButtonPress ) {
                m_needMouseRelease = true;
            } else if ( forcedMouseRelease ) {
{'fraction_non_alphanumeric': 0.14630902238971402, 'fraction_numerical': 0.012414098869430281, 'mean_word_length': 2.9607843137254903, 'pattern_counts': {'":': 2, '<': 51, '<?xml version=': 0, '>': 125, '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': '4657485', 'n_tokens_mistral': 4547, 'n_tokens_neox': 4335, 'n_words': 910}