aboutsummaryrefslogtreecommitdiffhomepage
path: root/qml/Bit.qml
blob: be6d0f67adfc7d11c937ac7cc7e0593f63ae12c6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import QtQuick 2.0
import Sailfish.Silica 1.0

Loader {
    property int index: 0
    property int bits: 0

    Component {
        id: bit
        Switch {
            width: Theme.paddingLarge * 2
            height: width
            onClicked: root.check(index)
            Component.onCompleted: root.matrix[index] = 0
        }
    }

    function pad(n, width) {
        return n.length >= width ? n : new Array(width - n.length + 1).join('0') + n;
    }

    Component {
        id: bit_index
        Label {
            id: bit_label
            color: Theme.highlightColor
            text: parent.index.toString()
            width: Theme.paddingLarge * 2
            height: width
            horizontalAlignment: TextInput.AlignHCenter
            verticalAlignment: TextInput.AlignVCenter
            Component.onCompleted: {
                // This code could definitely be improved ;)
                if (index > bits) {
                    grid.row++;

                    var indices = root.matrix.slice(0, bits);
                    var transformed = [];
                    indices.forEach(function(elem) {
                        transformed.unshift((pad((parseInt(elem) >>> 0).toString(2), bits))[grid.row - 1]);
                    });

                    var transformed_num = parseInt(transformed.join(""), 2);
                    this.text = transformed_num;
                    root.matrix[index] = transformed_num;
                } else if (index !== bits){
                    var num = Math.floor(Math.random() * (Math.pow(2, bits) - 1)) + 1;
                    this.text = num;
                    root.matrix[index] = num;
                } else {
                    this.text = "0/1";
                }
            }
        }
    }

    sourceComponent: (index % (bits + 1) == bits)
                     || (index <= bits) ? bit_index : bit
}