<commit_before>import QtQuick 2.0
import Sailfish.Silica 1.0
import "helpers.js" as Helpers

Page {
    id: page

    property string modelFilename
    property string quantityName

    function formatText(value, unit) {
        return value.toPrecision(5) + " " + pluralize(unit, value);
    }

    function updateResult() {
        var f = parseFloat(fromField.text);

        if (fromField.text.length === 0) {
            f = 0.0;
        }

        if (fromCombo.value.length === 0 || toCombo.value.length === 0)
            return;

        var model = modelLoader.item;

        var result = model.from(fromCombo.value, f);
        result = model.to(toCombo.value, result);

        resultLabel.text = formatText(result, toCombo.value);
    }

    function pluralize(s, n) {
        if (n !== 1) {
            if (s === "inch") {
                return "inches";
            } else if (s === "foot") {
                return "feet";
            } else if (s === "celcius") {
                return s;
            }
            return s + "s";
        }

        return s;
    }

    Component.onCompleted: {
        modelLoader.setSource(modelFilename);
    }

    Loader {
        id: modelLoader
        onLoaded: {
            fromRepeater.model = modelLoader.item;
            toRepeater.model = modelLoader.item;
        }
    }

    Column {
        id: column
        spacing: Theme.paddingLarge
        anchors.fill: parent

        PageHeader {
            title: Helpers.capitalize(page.quantityName)
        }

        ComboBox {
            id: fromCombo
            width: parent.width
            label: "From"
            menu: ContextMenu {
                Repeater {
                    id: fromRepeater
                    delegate: MenuItem {
                        text: title
                    }
                }
            }
            onValueChanged: updateResult()
        }

        TextField {
            id: fromField
            width: parent.width
            placeholderText: "enter " + pluralize(fromCombo.value, 0)
            inputMethodHints: Qt.ImhFormattedNumbersOnly
            EnterKey.onClicked: {
                parent.focus = true;
                updateResult();
            }
        }

        ComboBox {
            id: toCombo
            width: parent.width
            label: "To"
            menu: ContextMenu {
                Repeater {
                    id: toRepeater
                    delegate: MenuItem {
                        text: title
                    }
                }
            }
            onValueChanged: updateResult()
        }<commit_msg>show unit abbreviations<commit_after>import QtQuick 2.0
import Sailfish.Silica 1.0
import "helpers.js" as Helpers

Page {
    id: page

    property string modelFilename
    property string quantityName

    function formatText(value, unit) {
        return value.toPrecision(5) + " " + pluralize(unit, value);
    }

    function updateResult() {
        var f = parseFloat(fromField.text);

        if (fromField.text.length === 0) {
            f = 0.0;
        }

        if (fromCombo.value.length === 0 || toCombo.value.length === 0)
            return;

        var model = modelLoader.item;

        var fromUnit = model.get(fromCombo.currentIndex).title;
        var toUnit = model.get(toCombo.currentIndex).title;

        var result = model.from(fromUnit, f);
        result = model.to(toUnit, result);

        resultLabel.text = formatText(result, toUnit);
    }

    function pluralize(s, n) {
        if (n !== 1) {
            if (s === "inch") {
                return "inches";
            } else if (s === "foot") {
                return "feet";
            } else if (s === "celcius") {
                return s;
            }
            return s + "s";
        }

        return s;
    }

    function makeTitle(title, abbr) {
        if (abbr.length > 0) {
            return title + " (" + abbr + ")";
        }

        return title;
    }

    Component.onCompleted: {
        modelLoader.setSource(modelFilename);
    }

    Loader {
        id: modelLoader
        onLoaded: {
            fromRepeater.model = modelLoader.item;
            toRepeater.model = modelLoader.item;
        }
    }

    Column {
        id: column
        spacing: Theme.paddingLarge
        anchors.fill: parent

        PageHeader {
            title: Helpers.capitalize(page.quantityName)
        }

        ComboBox {
            id: fromCombo
            width: parent.width
            label: "From"
            menu: ContextMenu {
                Repeater {
                    id: fromRepeater
                    delegate: MenuItem {
                        text: makeTitle(title, abbr)
                    }
                }
            }
            onValueChanged: updateResult()
        }

        TextField {
            id: fromField
            width: parent.width
            placeholderText: "enter " + pluralize(fromRepeater.model.get(fromCombo.currentIndex).title, 0)
            inputMethodHints: Qt.ImhFormattedNumbersOnly
            EnterKey.onClicked: {
                parent.focus = true;
                updateResult();
            }
        }

        ComboBox {
            id: toCombo
            width: parent.width
            label: "To"
            menu: ContextMenu {
                Repeater {
                    id: toRepeater
                    delegate: MenuItem {
                        text: makeTitle(title, abbr)
                    }
                }
            }
            onValueChanged: updateResult()
        }
{'fraction_non_alphanumeric': 0.09343794579172611, 'fraction_numerical': 0.004457917261055635, 'mean_word_length': 1.242702918832467, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '633830', 'n_tokens_mistral': 1572, 'n_tokens_neox': 1479, 'n_words': 329}