-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtils.qml
90 lines (75 loc) · 2.77 KB
/
Utils.qml
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
import QtQuick 2.15
import "Logger.js" as Logger
Item {
TextMetrics {
id: calculateFontSizeMetrics
}
function calculateFontSizeModel(font, width, height, model, minSize = 8, maxSize = 72) {
//property int fontSize = calculateFontSizeMetrics.font.pixelSize;
// Minimum size
var size = maxSize;
calculateFontSizeMetrics.font = font;
// console.log("The width of e:" + width);
// console.log("The height of e: " + height)
model.forEach((item) => {
var fSize;
calculateFontSizeMetrics.font.pixelSize = height;
calculateFontSizeMetrics.text = item;
var brect = calculateFontSizeMetrics.boundingRect;
// console.log("brect width: " + brect.width);
// console.log("brect height: " + brect.height);
if (brect.width > width) {
var k = width / brect.width;
fSize = Math.floor(calculateFontSizeMetrics.font.pixelSize * k);
}
else {
fSize = calculateFontSizeMetrics.font.pixelSize;
}
if (fSize < size) {
size = fSize;
}
});
return size;
}
function getTextWidth(text, font, size) {
calculateFontSizeMetrics.font = font;
calculateFontSizeMetrics.font.pixelSize = size;
calculateFontSizeMetrics.text = text;
return calculateFontSizeMetrics.width;
}
function generateRangeModel(min, max, step=1) {
var model = [];
// var count = (max - min) / step
for (let i = min; i <= max; i+= step) {
model.push(i);
}
return model;
}
function findIndexByValue(array, value) {
//var f = Array(array);
Logger.debug("Utils:findIndexByValue:array:" + array);
Logger.debug("Utils:findIndexbyValue:value:" + value);
var index = array.indexOf(value);
Logger.debug("Utils:findIndexByValue:index:" + index);
return index;
}
function getModelByValue(model, criteria) {
for(var i=0; i < model.count; ++i) {
if (criteria(model.get(i))) { return model.get(i) }
}
return null
}
function findModelIndex(model, criteria) {
Logger.info("Utils:findModelIndex:model:" + model);
for(var i=0; i < model.count; ++i) {
if (criteria(model.get(i))) { return i }
}
Logger.info("Utils:findModelIndex:NoMatch");
return 0
}
Component.onCompleted: {
// var logger = Logger;
Logger.LOG_PRIORITY = Logger.DEBUG;
Logger.info("Utils:onCompleted:Logger:" + Logger.LOG_PRIORITY);
}
}