//-----------------------------------------------------------------------------
// File: memorymapview.cpp
//-----------------------------------------------------------------------------
// Project: Kactus2
// Author: Antti Kamppi
// Date: 17.04.2012
//
// Description:
// The graphics view that displays the memory map graphics items.
//-----------------------------------------------------------------------------

#include "memorymapview.h"
#include "memorymapscene.h"
#include <common/graphicsItems/visualizeritem.h>

#include <QMouseEvent>
#include <QRect>
#include <QResizeEvent>
#include <QScrollBar>

//-----------------------------------------------------------------------------
// Function: MemoryMapView()
//-----------------------------------------------------------------------------
MemoryMapView::MemoryMapView(QWidget *parent):
QGraphicsView(parent)
{

}

//-----------------------------------------------------------------------------
// Function: ~MemoryMapView()
//-----------------------------------------------------------------------------
MemoryMapView::~MemoryMapView()
{

}

//-----------------------------------------------------------------------------
// Function: resizeEvent()
//-----------------------------------------------------------------------------
void MemoryMapView::resizeEvent(QResizeEvent *event)
{
    QGraphicsView::resizeEvent(event);
   
    if (event->size().width() != event->oldSize().width())
    {
        MemoryMapScene* memoryscene = dynamic_cast<MemoryMapScene*>(scene());
        Q_ASSERT(memoryscene);
        memoryscene->setWidth(event->size().width());
        event->accept();
    }
}

//-----------------------------------------------------------------------------
// Function: mousePressEvent()
//-----------------------------------------------------------------------------
void MemoryMapView::mousePressEvent(QMouseEvent* mouseEvent)
{
    // Scrolling to left with side button.
    if (mouseEvent->button() == Qt::XButton1)        
    {
        if (horizontalScrollBar() != 0)
        {
            int sliderPosition = horizontalScrollBar()->value();
            int step = -horizontalScrollBar()->singleStep();
            horizontalScrollBar()->setSliderPosition(sliderPosition + step);
        }
    }

    // Scrolling to right with side button.
    else if (mouseEvent->button() == Qt::XButton2)
    {
        if (horizontalScrollBar() != 0)
        {
            int sliderPosition = horizontalScrollBar()->value();
            int step = horizontalScrollBar()->singleStep();
            horizontalScrollBar()->setSliderPosition(sliderPosition + step);
        }
    }

    // Normal operation.
    else
    {
        QGraphicsView::mousePressEvent(mouseEvent);
    }
}