aboutsummaryrefslogtreecommitdiffstats
path: root/qml/Viper/utils.js
blob: ad00957c48c656b3604ee9b6c9d98e6d93ae9fb2 (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
/******************************************************************************
    QtAV:  Multimedia framework based on Qt and FFmpeg
    Copyright (C) 2012-2016 Wang Bin <wbsecg1@gmail.com>
*   This file is part of QtAV (from 2013)
    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.
    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.
    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
******************************************************************************/

var kItemWidth = scaled(60)
var kItemHeight = scaled(30)
var kMargin = scaled(8)
var kFontSize = scaled(16)
var kSpacing = scaled(4)

// "/xxx" will be resolved as qrc:///xxx. while "xxx" is "qrc:///QMLDIR/xxx
var resprefix = Qt.resolvedUrl(" ").substring(0, 4) == "qrc:" ? "/" : ""

function resurl(s) { //why called twice if in qrc?
    return resprefix + s
}

String.prototype.startsWith = function(s) {
    return this.indexOf(s) === 0;
};

String.prototype.endsWith = function(suffix) {
    return this.indexOf(suffix, this.length - suffix.length) !== -1;
};

function fileName(path) {
    return path.substring(path.lastIndexOf("/") + 1)
}

function msec2string(t) {
    t = Math.floor(t/1000)
    var ss = t%60
    t = (t-ss)/60
    var mm = t%60
    var hh = (t-mm)/60
    if (ss < 10)
        ss = "0" + ss
    if (mm < 10)
        mm = "0" + mm
    if (hh < 10)
        hh = "0" + hh
    return hh + ":" + mm +":" + ss
}

function scaled(x) {
    //console.log("scaleRatio: " + scaleRatio + "; " + x + ">>>" + x*scaleRatio);
    return x * scaleRatio;
}


function htmlEscaped(s) {
    if (!s) {
        return '';
    }
    var escaped = '';
    var namedHtml = {
        '38': '&amp;',
        '60': '&lt;',
        '62': '&gt;',
        '34': '&quot;',
        '160': '&nbsp;',
        '162': '&cent;',
        '163': '&pound;',
        '164': '&curren;',
        '169': '&copy;',
        '174': '&reg;',
    };
    var wasNewLine = 0;
    for (var i = 0, il = s.length; i < il; ++i) {
        var c = s.charCodeAt(i);
        var es = namedHtml[c];
        if (typeof es !== 'undefined') {
            wasNewLine = 0;
            escaped += es;
        } else {
            if (c === 13 || c === 10) {
                if (wasNewLine == 0)
                    escaped += '<br>';
                wasNewLine++;
            } else {
                wasNewLine = 0;
                escaped += String.fromCharCode(c);
            }
        }
    }
    return escaped;
}