/*
 * Copyright (c) 2010 Nokia Corporation.
 */

#include <QApplication>
#include <QGraphicsObject>
#include <QDeclarativeView>
#include <QAccelerometer>
#include "accelerometerfilter.h"
#include "taskswitcher.h"
#include "settings.h"

// Lock orientation in Symbian
#ifdef Q_OS_SYMBIAN
    #include <eikenv.h>
    #include <eikappui.h>
    #include <aknenv.h>
    #include <aknappui.h>
#endif

QTM_USE_NAMESPACE

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

#ifdef Q_OS_SYMBIAN
    // Lock orientation in Symbian
    CAknAppUi* appUi = dynamic_cast<CAknAppUi*> (CEikonEnv::Static()->AppUi());
    TRAP_IGNORE(
        if(appUi) {
            appUi->SetOrientationL(CAknAppUi::EAppUiOrientationLandscape);
        }
    );
#endif

    QAccelerometer sensor;
    AccelerometerFilter *filter = new AccelerometerFilter;
    sensor.addFilter(filter);

    QDeclarativeView view;
    view.setSource(QUrl("qrc:/bubblelevel.qml"));
    view.setResizeMode(QDeclarativeView::SizeRootObjectToView);

    QObject *rootObject = dynamic_cast<QObject*>(view.rootObject());

    TaskSwitcher taskSwitcher;
    Settings settings;

    QObject::connect(rootObject, SIGNAL(minimizeApplication()), &taskSwitcher, SLOT(minimizeApplication()));
    QObject::connect((QObject*)view.engine(), SIGNAL(quit()), &app, SLOT(quit()));
    QObject::connect(filter->getSignaler(), SIGNAL(rotationChanged(QVariant, QVariant, QVariant)),
                     rootObject, SLOT(handleRotation(QVariant, QVariant, QVariant)));
    QObject::connect(&settings, SIGNAL(correctionAngle(QVariant)), rootObject, SLOT(setCorrectionAngle(QVariant)));
    QObject::connect(rootObject, SIGNAL(saveCorrectionAngle(QVariant)), &settings, SLOT(saveCorrectionAngle(QVariant)));

    settings.loadAndEmitCorrectionAngle();

    sensor.start();

#if defined(Q_WS_MAEMO_5)
    view.setGeometry(QRect(0,0, 800, 480));
    view.showFullScreen();
#elif defined(Q_OS_SYMBIAN)
    view.setGeometry(QRect(0, 0, 640, 360));
    view.showFullScreen();
#elif defined(Q_WS_SIMULATOR)
    view.showFullScreen();
#else
    view.setGeometry((QRect(100, 100, 800, 480)));
    view.show();
#endif

    return app.exec();
}
