#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMouseEvent>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    // Mouse events are processed only if mouse tracking is enabled
    // The main window contains another widget, so the mouse events
    // go first to that widget and since that doesn't process the
    // events, they are next delegated to the parent.
    // Mouse tracking needs to be enabled from both the widget and parent
    setMouseTracking(true);
    ui->centralWidget->setMouseTracking(true);

    // Another option would be to install an event filter.
    // In this case the main windows mouse tracking is not
    // needed, since mouse events come via the filter function.
//    ui->centralWidget->installEventFilter(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::changeEvent(QEvent *e)
{
    QMainWindow::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void MainWindow::mouseMoveEvent(QMouseEvent *e)
{
    ui->label->setText("(" + QString::number(e->x()) + ", " + QString::number(e->y()) + ")");
}

bool MainWindow::eventFilter(QObject *, QEvent *e)
{
    if (e->type() == QEvent::MouseMove) {
        QMouseEvent *me = static_cast<QMouseEvent *>(e);
        ui->label->setText("(" + QString::number(me->x()) + ", " + QString::number(me->y()) + ")");
    }
    return false;
}
