/*!
 *
 * Copyright (c) 2020-2024 Diality Inc. - All Rights Reserved.
 * \copyright
 * THIS CODE MAY NOT BE COPIED OR REPRODUCED IN ANY FORM, IN PART OR IN
 * WHOLE, WITHOUT THE EXPLICIT PERMISSION OF THE COPYRIGHT OWNER.
 *
 * \file    TickMarks.qml
 * \author  (last)      Vy
 * \date    (last)      15-Mar-2023
 * \author  (original)  Behrouz NematiPour
 * \date    (original)  22-Mar-2020
 *
 */

// Qt
import QtQuick 2.12

// Project

//  Qml imports
import "qrc:/globals"

/*!
 * \brief   Denali project TickMarks
 */
Item { id : _root
    // if loader used then assign ranges when used.
    // since loader get the parenthood and it has no range definition.
    property int    decimal     : 0
    property real   minimum     : parent.minimum
    property real   maximum     : parent.maximum
    property real   step        : parent.step
    property bool   stepSnap    : false

    property color  color       : Colors.backgroundDialog

    property bool   isTickMarkRound         : false
    property real   roundTickMarkDiameter   : Variables.sliderDefaultRoundTickMarkDiameter
    property real   yDisplacement           : 0
    property bool   showEndMarks            : true

    // For the case that the tickmark is a line
    property real   lineTickMarkHeight      : 20
    property real   lineTickMarkThickness   : 1

    property bool   textVisible : false
    property color  textColor   : Colors.textMain

    anchors.fill: parent

    Repeater { id : _repeater
        readonly property real totalTickCount: ((maximum - minimum) / step + 1) // added property for clarity

        model: totalTickCount

        Rectangle {
            visible     : _repeater.isTickMarkVisible(index)
            x           : (((index * step) * (parent.width - parent.x)) / (maximum - minimum)) - width/2 // "-width/2 " for moving tick mark to center on handle
            y           : _root.yDisplacement
            color       : _root.color

            height  : isTickMarkRound ? _root.roundTickMarkDiameter : _root.lineTickMarkHeight
            width   : isTickMarkRound ? _root.roundTickMarkDiameter : _root.lineTickMarkThickness
            radius  : isTickMarkRound ? _root.roundTickMarkDiameter : 0

            Text { id : _text
                visible: _root.textVisible
                color: _root.textColor
                font.pixelSize: 10
                text: ((index * step) + minimum).toFixed(decimal)
                anchors {
                    top: parent.bottom
                    topMargin: 5
                    horizontalCenter: parent.horizontalCenter
                }
            }
        }

        function isTickMarkVisible(index) {
            if (showEndMarks) {
                return true // all marks shown
            } else {
                // Only the center marks are shown, ie: exclude the end marks
                if ((index > 0) && (index < (_repeater.totalTickCount-1))) {
                    return true
                }
            }
            return false
        }
    }
}
