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

import Qt 4.7

Image {
    id: bubbleLevel

    signal minimizeApplication()                // signaled when task switcher button is pressed
    signal saveCorrectionAngle(variant angle)   // signaled when correction angle is saved

    // this functions are used as Qt slot
    function handleRotation(xdeg, ydeg, zdeg) { horTube.rawangle = xdeg }
    function setCorrectionAngle(deg)          { calibrationDialog.angleconstant = deg }

    anchors.fill: parent
    source: "board.png"

    MouseArea {
        anchors.fill: parent
        onClicked: hiddenDialog.opacity = 0
    }

    Button {
        id: taskSwitcher

        anchors { left: parent.left; top: parent.top }
        source: "taskswitcher.png"
        onClicked: bubbleLevel.minimizeApplication()
    }

    Button {
        id: exitButton

        anchors { right: parent.right; top: parent.top }
        source: "exit.png"
        onClicked: Qt.quit()
    }

    Tube {
        id: horTube

        property real rawangle: 0

        anchors.horizontalCenter: parent.horizontalCenter
        width: parent.width * 0.775; height: parent.height * 0.15625
        deg: rawangle - calibrationDialog.angleconstant
    }

    Item {
        id: calibrationDialog

        property real angleconstant: 0

        anchors.horizontalCenter: parent.horizontalCenter
        y: 332
        width: 416; height: 101

        MouseArea {
            anchors.fill: parent
            onClicked: { hiddenDialog.opacity = 0.9 }
        }

        Rectangle {
            id: hiddenDialog

            anchors.fill: parent
            opacity: 0
            radius: 14
            color: "#909090"

            Behavior on opacity { NumberAnimation { } }

            Text {
                id: oldValue

                anchors { left: parent.left; leftMargin: 20; top: parent.top; topMargin: 20 }
                text: "Old correction angle: " + calibrationDialog.angleconstant.toPrecision(2)
                color: "#302020"
                font { bold: true; pixelSize: 20 }
            }

            Text {
                id: newValue

                anchors { left: parent.left; leftMargin: 20; top: oldValue.bottom; topMargin: 10 }
                text: "New correction angle: " + horTube.rawangle.toPrecision(2)
                color: "#302020"
                font { bold: true; pixelSize: 20 }
            }

            Button {
                id: saveButton

                anchors { right: parent.right; rightMargin: 20; verticalCenter: parent.verticalCenter }
                width: 100; height: 60
                radius: 10
                border { width: 2; color: "#555555" }
                smooth: true

                onClicked: {
                    calibrationDialog.angleconstant = horTube.rawangle
                    saveCorrectionAngle(calibrationDialog.angleconstant)
                }

                gradient: Gradient {
                    GradientStop { position: 0.0; color: "#999999" }
                    GradientStop { position: 0.2; color: "#BBBBBB" }
                    GradientStop { position: 0.8; color: "#BBBBBB" }
                    GradientStop { position: 1.0; color: "#999999" }
                }

                Text {
                    anchors.centerIn: parent
                    color: "#302020"
                    text: "Save"
                    font { bold: true; pixelSize: 20 }
                }
            }
        }
    }
}
