#include "mainwindow.h"

#include <QVBoxLayout>
#include <QLabel>
#include <QQmlEngine>
#include <QQmlContext>
#include <QDir>
#include <QDebug>
#include <QDateTime>
#include <QDrag>
#include <QMimeData>

MainWindow::MainWindow(QWidget *parent) :
    QWidget(parent),
    m_targetWidget(0),
    m_quickWidget(0),
    m_internalCounter(0)
{
    QVBoxLayout * layout = new QVBoxLayout(this);
    m_quickWidget = new QQuickWidget(this);
    QQmlContext *rootContext = m_quickWidget->rootContext();
    rootContext->setContextProperty(QStringLiteral("mainWindow"), this);
    m_quickWidget->setResizeMode(QQuickWidget::SizeRootObjectToView);
    layout->addWidget(m_quickWidget);
    m_quickWidget->installEventFilter(this);

    m_targetWidget = new QLabel("target");
    layout->addWidget(m_targetWidget);


    m_qmlSourceUpdateShortcut = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_F5), this);
    connect(m_qmlSourceUpdateShortcut, SIGNAL(activated()), this, SLOT(reloadQmlSource()));

    // init the first load of the QML UI elements
    reloadQmlSource();
}

MainWindow::~MainWindow()
{
}

bool MainWindow::eventFilter(QObject *target, QEvent *event)
{
    if (target == m_quickWidget && event->type() == QEvent::MouseButtonPress) {
        qDebug() << Q_FUNC_INFO << ++m_internalCounter;
        qDebug() << m_currentDrag;
    }
   return QWidget::eventFilter(target, event);
}

void MainWindow::startDragAndDropDelayed()
{
    qDebug() << Q_FUNC_INFO << ++m_internalCounter;
    m_currentDrag = new QDrag(this);

    m_currentDrag->setMimeData(new QMimeData);

    m_currentDrag->exec(Qt::MoveAction);
}


void MainWindow::reloadQmlSource()
{
    m_quickWidget->setSource(QUrl::fromUserInput(QString(SRCDIR) + "/Drag_and_Drop_Test.qml"));
    m_quickWidget->engine()->clearComponentCache();
}
