summaryrefslogtreecommitdiff
path: root/lib/qtcreator/qtcomponents/custom/ButtonGroup.js
blob: 19d05fee33d95bbfb302efab2dc1a3d05444c871 (plain)
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
var self;
var checkHandlers = [];
var visibleButtons = [];
var nonVisibleButtons = [];
var direction;

function create(that, options) {
    self = that;
    direction = options.direction || Qt.Horizontal;
    self.childrenChanged.connect(rebuild);
//    self.widthChanged.connect(resizeChildren);
    build();
}

function isButton(item) {
    if (item && item.hasOwnProperty("__position"))
        return true;
    return false;
}

function hasChecked(item) {
    return (item && item.hasOwnProperty("checked"));
}

function destroy() {
    self.childrenChanged.disconnect(rebuild);
//    self.widthChanged.disconnect(resizeChildren);
    cleanup();
}

function build() {
    visibleButtons = [];
    nonVisibleButtons = [];

    for (var i = 0, item; (item = self.children[i]); i++) {
        if (!hasChecked(item))
            continue;

        item.visibleChanged.connect(rebuild); // Not optimal, but hardly a bottleneck in your app
        if (!item.visible) {
            nonVisibleButtons.push(item);
            continue;
        }
        visibleButtons.push(item);

        if (self.exclusive && item.hasOwnProperty("checkable"))
            item.checkable = true;

        if (self.exclusive) {
            item.checked = false;
            checkHandlers.push(checkExclusive(item));
            item.checkedChanged.connect(checkHandlers[checkHandlers.length - 1]);
        }
    }

    var nrButtons = visibleButtons.length;
    if (nrButtons == 0)
        return;

    if (self.checkedButton)
        self.checkedButton.checked = true;
    else if (self.exclusive) {
        self.checkedButton = visibleButtons[0];
        self.checkedButton.checked = true;
    }

    if (nrButtons == 1) {
        finishButton(visibleButtons[0], "only");
    } else {
        finishButton(visibleButtons[0], direction == Qt.Horizontal ? "leftmost" : "top");
        for (var i = 1; i < nrButtons - 1; i++)
            finishButton(visibleButtons[i], direction == Qt.Horizontal ? "h_middle": "v_middle");
        finishButton(visibleButtons[nrButtons - 1], direction == Qt.Horizontal ? "rightmost" : "bottom");
    }
}

function finishButton(button, position) {
    if (isButton(button)) {
        button.__position = position;
        if (direction == Qt.Vertical) {
            button.anchors.left = self.left     //mm How to make this not cause binding loops? see QTBUG-17162
            button.anchors.right = self.right
        }
    }
}

function cleanup() {
    visibleButtons.forEach(function(item, i) {
        if (checkHandlers[i])
            item.checkedChanged.disconnect(checkHandlers[i]);
        item.visibleChanged.disconnect(rebuild);
    });
    checkHandlers = [];

    nonVisibleButtons.forEach(function(item, i) {
        item.visibleChanged.disconnect(rebuild);
    });
}

function rebuild() {
    if (self == undefined)
        return;

    cleanup();
    build();
}

function resizeChildren() {
    if (direction != Qt.Horizontal)
        return;

    var extraPixels = self.width % visibleButtons;
    var buttonSize = (self.width - extraPixels) / visibleButtons;
    visibleButtons.forEach(function(item, i) {
        if (!item || !item.visible)
            return;
        item.width = buttonSize + (extraPixels > 0 ? 1 : 0);
        if (extraPixels > 0)
            extraPixels--;
    });
}

function checkExclusive(item) {
    var button = item;
    return function() {
        for (var i = 0, ref; (ref = visibleButtons[i]); i++) {
            if (ref.checked == (button === ref))
                continue;

            // Disconnect the signal to avoid recursive calls
            ref.checkedChanged.disconnect(checkHandlers[i]);
            ref.checked = !ref.checked;
            ref.checkedChanged.connect(checkHandlers[i]);
        }
        self.checkedButton = button;
    }
}