<commit_before>//
// This file is part of the Marble Desktop 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 2006-2007 <NAME> <<EMAIL>>"
// Copyright 2007      <NAME>  <<EMAIL>>"
//

#include "MarbleWidgetInputHandler.h"

#include <cmath>

#include <QtCore/QDebug>
#include <QtGui/QMouseEvent>
#include <QRubberBand>

#include "global.h"
#include "GeoDataCoordinates.h"
#include "MarbleDirs.h"
#include "MarbleWidget.h"
#include "MarbleMap.h"
#include "MarbleModel.h"
#include "ViewParams.h"
#include "ViewportParams.h"
#include "AbstractFloatItem.h"
#include "MeasureTool.h"
#include "MarbleWidgetPopupMenu.h"

using namespace Marble;

MarbleWidgetInputHandler::MarbleWidgetInputHandler()
    : m_widget( 0 ),
      m_model( 0 ),
      m_positionSignalConnected( false ),
      m_mouseWheelTimer( new QTimer(this) )
{
    connect( m_mouseWheelTimer, SIGNAL( timeout() ),
	     this,              SLOT( restoreViewContext() ) );
}

MarbleWidgetInputHandler::~MarbleWidgetInputHandler()
{
    delete m_mouseWheelTimer;
}

void MarbleWidgetInputHandler::init(MarbleWidget *w)
{
    m_widget = w;
    m_model = w->model();

    foreach(RenderPlugin *renderPlugin, m_widget->renderPlugins())
    {
        m_widget->installEventFilter(renderPlugin);
    }
}


MarbleWidgetDefaultInputHandler::MarbleWidgetDefaultInputHandler()
    : MarbleWidgetInputHandler(), m_popupmenu( 0 ), m_leftpopup(true)
{
    curpmtl.load( MarbleDirs::path("bitmaps/cursor_tl.xpm") );
    curpmtc.load( MarbleDirs::path("bitmaps/cursor_tc.xpm") );
    curpmtr.load( MarbleDirs::path("bitmaps/cursor_tr.xpm") );
    curpmcr.load( MarbleDirs::path("bitmaps/cursor_cr.xpm") );
    curpmcl.load( MarbleDirs::path("bitmaps/cursor_cl.xpm") );
    curpmbl.load( MarbleDirs::path("bitmaps/cursor_bl.xpm") );
    curpmbc.load( MarbleDirs::path("bitmaps/cursor_bc.xpm") );
    curpmbr.load( MarbleDirs::path("bitmaps/cursor_br.xpm") );

    arrowcur [0][0] = QCursor(curpmtl,2,2);
    arrowcur [1][0] = QCursor(curpmtc,10,3);
    arrowcur [2][0] = QCursor(curpmtr,19,2);
    arrowcur [0][1] = QCursor(curpmcl,3,10);
    arrowcur [1][1] = QCursor(Qt::OpenHandCursor);
    arrowcur [2][1] = QCursor(curpmcr,18,10);
    arrowcur [0][2] = QCursor(curpmbl,2,19);
    arrowcur [1][2] = QCursor(curpmbc,11,18);
    arrowcur [2][2] = QCursor(curpmbr,19,19);

    m_leftpressed     = false;
    m_midpressed      = false;

    m_selectionRubber = new QRubberBand(QRubberBand::Rectangle, m_widget);
    m_selectionRubber->hide();

    m_dragThreshold   = 3;    
}

MarbleWidgetDefaultInputHandler::~MarbleWidgetDefaultInputHandler()
{
    delete m_selectionRubber;
}

void MarbleWidgetInputHandler::restoreViewContext()
{
    // Needs to stop the timer since it repeats otherwise.
    m_mouseWheelTimer->stop();

    // Redraw the map with the quality set for Marble::Still (if necessary).
    m_widget->setViewContext( Marble::Still );
    if ( m_widget->mapQuality( Marble::Still )
        != m_widget->mapQuality( Marble::Animation ) )
    {
        m_widget->updateChangedMap();
    }
}

void MarbleWidgetDefaultInputHandler::init(MarbleWidget *w)
{
    MarbleWidgetInputHandler::init(w);
  
    // The interface to the measure tool consists of a RMB popup menu
    // and some signals.
    MeasureTool  *measureTool = m_widget->map()->measureTool();

    // Connect the inputHandler and the measure tool to the popup menu
    if ( !m_popupmenu ) {
        m_popupmenu    = new MarbleWidgetPopupMenu( m_widget, m_model );
    }
    connect( this, SIGNAL( rmbRequest( int, int ) ),
                       m_popupmenu,    SLOT( showRmbMenu( int, int ) ) );
    connect( m_popupmenu, SIGNAL( addMeasurePoint( qreal, qreal ) ),
                       measureTool, SLOT( addMeasurePoint( qreal, qreal ) ) );
    connect( m_popupmenu, SIGNAL( removeLastMeasurePoint() ),
		       measureTool, SLOT( removeLastMeasurePoint() ) );
    connect( m_popupmenu, SIGNAL( removeMeasurePoints() ),
                       measureTool, SLOT( removeMeasurePoints( ) ) );  
    connect( measureTool, SIGNAL( numberOfMeasurePointsChanged( int ) ),
		       m_popupmenu, SLOT( slotNumberOfMeasurePointsChanged( int ) ) );
    connect( this, SIGNAL( lmbRequest( int, int ) ),
                       this,    SLOT( showLmbMenu( int, int ) ) );		       
}

void MarbleWidgetDefaultInputHandler::showLmbMenu( int x, int y)
{
  if (m_leftpopup)
  {
    m_popupmenu->showLmbMenu(x,y);
  }
}

void MarbleWidgetDefaultInputHandler::setLeftMouseButtonPopup(bool enabled)
{
  m_leftpopup = enabled;
}
    
bool MarbleWidgetDefaultInputHandler::leftMouseButtonPopup()
{
  return m_leftpopup;
}

bool MarbleWidgetDefaultInputHandler::eventFilter( QObject* o, QEvent* e )
{
    Q_UNUSED( o );

    if ( keyEvent( m_widget->map(), e ) ) {
        m_widget->repaint();
        return true;
    }

    int polarity = m_widget->map()->viewParams()->viewport()->polarity();

    if ( e->type() == QEvent::MouseMove
              || e->type() == QEvent::MouseButtonPress
              || e->type() == QEvent::MouseButtonRelease ) {

        QMouseEvent  *event        = static_cast<QMouseEvent*>(e);
        QRegion       activeRegion = m_widget->activeRegion();

        m_dirX = 0;
        m_dirY = 0;
    
        if ( event->type() == QEvent::MouseMove
             && !( event->buttons() & Qt::LeftButton ) )
        {
            m_leftpressed = false;
        }

        // Do not handle (and therefore eat) mouse press and release events 
        // that occur above visible float items. Mouse motion events are still 
        // handled, however.
        foreach( AbstractFloatItem *floatItem, m_widget->floatItems() ) {
            QRectF widgetRect(0,0,m_widget->width(),m_widget->height());
            QRectF floatItemRect = QRectF(floatItem->positivePosition(widgetRect), 
                    floatItem->size());
            if ( floatItem->enabled() && floatItem->visible() 
                    && floatItemRect.contains(event->pos())
                    && e->type() != QEvent::MouseMove )
            {
                return false;
            }
        }

        // emit the position string only if the signal got attached
        if ( m_positionSignalConnected ) {
            qreal  lat;
            qreal  lon;<commit_msg>Also preventing lost midMouseButtonRelease events<commit_after>//
// This file is part of the Marble Desktop 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 2006-2007 <NAME> <<EMAIL>>"
// Copyright 2007      <NAME>  <<EMAIL>>"
//

#include "MarbleWidgetInputHandler.h"

#include <cmath>

#include <QtCore/QDebug>
#include <QtGui/QMouseEvent>
#include <QRubberBand>

#include "global.h"
#include "GeoDataCoordinates.h"
#include "MarbleDirs.h"
#include "MarbleWidget.h"
#include "MarbleMap.h"
#include "MarbleModel.h"
#include "ViewParams.h"
#include "ViewportParams.h"
#include "AbstractFloatItem.h"
#include "MeasureTool.h"
#include "MarbleWidgetPopupMenu.h"

using namespace Marble;

MarbleWidgetInputHandler::MarbleWidgetInputHandler()
    : m_widget( 0 ),
      m_model( 0 ),
      m_positionSignalConnected( false ),
      m_mouseWheelTimer( new QTimer(this) )
{
    connect( m_mouseWheelTimer, SIGNAL( timeout() ),
	     this,              SLOT( restoreViewContext() ) );
}

MarbleWidgetInputHandler::~MarbleWidgetInputHandler()
{
    delete m_mouseWheelTimer;
}

void MarbleWidgetInputHandler::init(MarbleWidget *w)
{
    m_widget = w;
    m_model = w->model();

    foreach(RenderPlugin *renderPlugin, m_widget->renderPlugins())
    {
        m_widget->installEventFilter(renderPlugin);
    }
}


MarbleWidgetDefaultInputHandler::MarbleWidgetDefaultInputHandler()
    : MarbleWidgetInputHandler(), m_popupmenu( 0 ), m_leftpopup(true)
{
    curpmtl.load( MarbleDirs::path("bitmaps/cursor_tl.xpm") );
    curpmtc.load( MarbleDirs::path("bitmaps/cursor_tc.xpm") );
    curpmtr.load( MarbleDirs::path("bitmaps/cursor_tr.xpm") );
    curpmcr.load( MarbleDirs::path("bitmaps/cursor_cr.xpm") );
    curpmcl.load( MarbleDirs::path("bitmaps/cursor_cl.xpm") );
    curpmbl.load( MarbleDirs::path("bitmaps/cursor_bl.xpm") );
    curpmbc.load( MarbleDirs::path("bitmaps/cursor_bc.xpm") );
    curpmbr.load( MarbleDirs::path("bitmaps/cursor_br.xpm") );

    arrowcur [0][0] = QCursor(curpmtl,2,2);
    arrowcur [1][0] = QCursor(curpmtc,10,3);
    arrowcur [2][0] = QCursor(curpmtr,19,2);
    arrowcur [0][1] = QCursor(curpmcl,3,10);
    arrowcur [1][1] = QCursor(Qt::OpenHandCursor);
    arrowcur [2][1] = QCursor(curpmcr,18,10);
    arrowcur [0][2] = QCursor(curpmbl,2,19);
    arrowcur [1][2] = QCursor(curpmbc,11,18);
    arrowcur [2][2] = QCursor(curpmbr,19,19);

    m_leftpressed     = false;
    m_midpressed      = false;

    m_selectionRubber = new QRubberBand(QRubberBand::Rectangle, m_widget);
    m_selectionRubber->hide();

    m_dragThreshold   = 3;    
}

MarbleWidgetDefaultInputHandler::~MarbleWidgetDefaultInputHandler()
{
    delete m_selectionRubber;
}

void MarbleWidgetInputHandler::restoreViewContext()
{
    // Needs to stop the timer since it repeats otherwise.
    m_mouseWheelTimer->stop();

    // Redraw the map with the quality set for Marble::Still (if necessary).
    m_widget->setViewContext( Marble::Still );
    if ( m_widget->mapQuality( Marble::Still )
        != m_widget->mapQuality( Marble::Animation ) )
    {
        m_widget->updateChangedMap();
    }
}

void MarbleWidgetDefaultInputHandler::init(MarbleWidget *w)
{
    MarbleWidgetInputHandler::init(w);
  
    // The interface to the measure tool consists of a RMB popup menu
    // and some signals.
    MeasureTool  *measureTool = m_widget->map()->measureTool();

    // Connect the inputHandler and the measure tool to the popup menu
    if ( !m_popupmenu ) {
        m_popupmenu    = new MarbleWidgetPopupMenu( m_widget, m_model );
    }
    connect( this, SIGNAL( rmbRequest( int, int ) ),
                       m_popupmenu,    SLOT( showRmbMenu( int, int ) ) );
    connect( m_popupmenu, SIGNAL( addMeasurePoint( qreal, qreal ) ),
                       measureTool, SLOT( addMeasurePoint( qreal, qreal ) ) );
    connect( m_popupmenu, SIGNAL( removeLastMeasurePoint() ),
		       measureTool, SLOT( removeLastMeasurePoint() ) );
    connect( m_popupmenu, SIGNAL( removeMeasurePoints() ),
                       measureTool, SLOT( removeMeasurePoints( ) ) );  
    connect( measureTool, SIGNAL( numberOfMeasurePointsChanged( int ) ),
		       m_popupmenu, SLOT( slotNumberOfMeasurePointsChanged( int ) ) );
    connect( this, SIGNAL( lmbRequest( int, int ) ),
                       this,    SLOT( showLmbMenu( int, int ) ) );		       
}

void MarbleWidgetDefaultInputHandler::showLmbMenu( int x, int y)
{
  if (m_leftpopup)
  {
    m_popupmenu->showLmbMenu(x,y);
  }
}

void MarbleWidgetDefaultInputHandler::setLeftMouseButtonPopup(bool enabled)
{
  m_leftpopup = enabled;
}
    
bool MarbleWidgetDefaultInputHandler::leftMouseButtonPopup()
{
  return m_leftpopup;
}

bool MarbleWidgetDefaultInputHandler::eventFilter( QObject* o, QEvent* e )
{
    Q_UNUSED( o );

    if ( keyEvent( m_widget->map(), e ) ) {
        m_widget->repaint();
        return true;
    }

    int polarity = m_widget->map()->viewParams()->viewport()->polarity();

    if ( e->type() == QEvent::MouseMove
              || e->type() == QEvent::MouseButtonPress
              || e->type() == QEvent::MouseButtonRelease ) {

        QMouseEvent  *event        = static_cast<QMouseEvent*>(e);
        QRegion       activeRegion = m_widget->activeRegion();

        m_dirX = 0;
        m_dirY = 0;
    
        // To prevent error from lost MouseButtonRelease events
        if ( event->type() == QEvent::MouseMove
             && !( event->buttons() & Qt::LeftButton ) )
        {
            m_leftpressed = false;
        }
        if ( event->type() == QEvent::MouseMove
             && !( event->buttons() & Qt::MidButton ) )
        {
            m_midpressed = false;
        }

        // Do not handle (and therefore eat) mouse press and release events 
        // that occur above visible float items. Mouse motion events are still 
        // handled, however.
        foreach( AbstractFloatItem *floatItem, m_widget->floatItems() ) {
            QRectF widgetRect(0,0,m_widget->width(),m_widget->height());
            QRectF floatItemRect = QRectF(floatItem->positivePosition(widgetRect), 
                    floatItem->size());
            if ( floatItem->enabled() && floatItem->visible() 
                    && floatItemRect.contains(event->pos())
                    && e->type() != QEvent::MouseMove )
            {
                return false;
            }
        }

        // emit the position string only if the signal got attached
        if ( m_positionSignalConnected ) {
            qreal  lat;
            qreal  lon;
{'fraction_non_alphanumeric': 0.12882965686274508, 'fraction_numerical': 0.00980392156862745, 'mean_word_length': 3.043666769897801, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 93, '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': '2747825', 'n_tokens_mistral': 4312, 'n_tokens_neox': 4141, 'n_words': 855}