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

import Qt 4.7

Image {
    id: bubbleLevel

    //! [0]
    // Signaled when task switcher button is pressed
    signal minimizeApplication()

    // Signaled when correction angle is saved
    signal saveCorrectionAngle(variant angle)

    // These functions are used as Qt slots
    function handleRotation(deg) {
        horTube.rawangle = deg
    }

    function setCorrectionAngle(deg) {
        calibrationDialog.angleconstant = deg
    }

    // Used to show the task switcher button in Maemo targets
    property alias taskSwitcherVisible: taskSwitcher.visible
    //! [0]

    anchors.fill: parent
    source: "images/board.png"
    smooth: true

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

    Button {
        id: taskSwitcher

        width: parent.width * 0.113; height: parent.height * 0.17
        source: "images/taskswitcher.png"
        onClicked: bubbleLevel.minimizeApplication()
        visible: false
    }

    Button {
        id: exitButton

        anchors.right: parent.right
        width: parent.width * 0.113; height: parent.height * 0.17
        source: "images/exit.png"
        onClicked: Qt.quit()
    }

    //! [1]
    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
    }
    //! [1]

    Item {
        id: calibrationDialog

        property real angleconstant: 0

        x: parent.width * 0.2419; y: parent.height * 0.695
        width:  parent.width * 0.516; height: parent.height * 0.20

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

        Rectangle {
            id: hiddenDialog

            anchors.fill: parent
            opacity: 0
            radius:  parent.width * 0.03
            color: "#909090"

            Behavior on opacity { NumberAnimation { } }

            Text {
                id: oldValue

                anchors {
                    left: parent.left; leftMargin: 20
                    verticalCenter: parent.verticalCenter
                    verticalCenterOffset: -14
                }

                text: "Old correction angle: "
                      + calibrationDialog.angleconstant.toPrecision(2)
                color: "#302020"
                font { bold: true; pixelSize: parent.width * 0.044 }
            }

            Text {
                id: newValue

                anchors {
                    left: parent.left; leftMargin: 20
                    verticalCenter: parent.verticalCenter
                    verticalCenterOffset: 14
                }

                text: "New correction angle: "
                      + horTube.rawangle.toPrecision(2)

                color: "#302020"
                font { bold: true; pixelSize: oldValue.font.pixelSize }
            }

            Button {
                id: saveButton

                anchors {
                    right: parent.right; rightMargin: 20
                    verticalCenter: parent.verticalCenter
                }

                width: parent.width * 0.25; height: parent.height * 0.65
                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: parent.width * 0.2 }
                    smooth: true
                }
            }
        }
    }
}
