#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QCompleter>
#include <QLineEdit>
#include <QStringList>
#include <QDebug>
#include <QTimer>
#include <QMessageBox>

#define NATIVE_WIDGETS

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

    QStringList wordList;
    wordList << "alpha" << "angle" << "omega" << "omicron" << "zeta";

    QCompleter *completer = new QCompleter(wordList, this);
    completer->setCaseSensitivity(Qt::CaseInsensitive);
    ui->lineEdit->setCompleter(completer);
    ui->lineEdit->setFocus();

    QMenu *menu = new QMenu(this);
    menu->addAction(tr("&First Item"));
    menu->addAction(tr("&Second Item"));
    menu->addAction(tr("&Third Item"));
    menu->addAction(tr("F&ourth Item"));
    ui->popupButton->setMenu(menu);

#ifdef NATIVE_WIDGETS
    setAttribute(Qt::WA_NativeWindow);
    ui->lineEdit->setAttribute(Qt::WA_NativeWindow);
#endif

#if QT_VERSION >= 0x50000
    setWindowTitle("Qt 5.0");
#else
    setWindowTitle("Qt 4.8");
#endif
    qApp->installEventFilter(this);

    connect(ui->messageBoxButton, SIGNAL(clicked()), this, SLOT(messageBoxButtonPressed()));
    connect(ui->delayedPopupButton, SIGNAL(clicked()), this, SLOT(delayedPopupButtonPressed()));
    connect(ui->grabButton, SIGNAL(clicked()), this, SLOT(grabMouseButtonPressed()));

    timer = new QTimer(this);
}

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

void MainWindow::timeout()
{
    qDebug() << "timeout";
    popupWidget = new QWidget(0, Qt::Popup);
    popupWidget->setGeometry(x() - 50, y(), 50, 50);
    popupWidget->show();
}

bool MainWindow::eventFilter(QObject *o, QEvent *e)
{
    static int counter = 0;
    counter++;
    if (o->isWidgetType()) {
        switch (e->type()) {
        case QEvent::MouseButtonPress:
            qDebug() << counter << "## Got MouseButtonPress for:" << o;
            break;
        case QEvent::MouseButtonRelease:
            qDebug() << counter << "## Got MouseButtonRelease for:" << o;
            break;
        case QEvent::MouseMove:
            //qDebug() << counter << "## Got Move for:" << o;
            break;
        case QEvent::Enter:
            qDebug() << counter << "## Got Enter for:" << o;
            break;
        case QEvent::Leave:
            qDebug() << counter << "## Got Leave for:" << o;
            break;
        case QEvent::HoverEnter:
            //qDebug() << counter << "## Got HoverEnter for:" << o;
            break;
        case QEvent::HoverLeave:
            //qDebug() << counter << "## Got HoverLeave for:" << o;
            break;
        case QEvent::HoverMove:
            //qDebug() << counter << "## Got HoverMove for:" << o;
            break;
        default:
            break;
        }
    }
    return QMainWindow::eventFilter(o ,e);
}

void MainWindow::messageBoxButtonPressed()
{
    qDebug() << "messageBoxButtonPressed()";
    QMessageBox msgBox;
    msgBox.setText("Message in a box!");
    msgBox.exec();
}

void MainWindow::delayedPopupButtonPressed()
{
    timer->singleShot(1000, this, SLOT(timeout()));
}

void MainWindow::grabMouseButtonPressed()
{
    static bool grabbed = false;
    if (!grabbed) {
        ui->grabButton->grabMouse();
        ui->grabButton->setText("Release Mouse");
    } else {
        ui->grabButton->releaseMouse();
        ui->grabButton->setText("Grab Mouse");
    }
    grabbed = !grabbed;
}

void MainWindow::closeEvent(QCloseEvent *event)
{
    QApplication::quit();
}
