-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscripttools.cpp
89 lines (80 loc) · 2.83 KB
/
scripttools.cpp
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
/****************************************************************************}
{ scripttool.cpp - misc tools for scripts }
{ }
{ Copyright (c) 2017 Alexey Parfenov <zxed@alkatrazstudio.net> }
{ }
{ This file is part of GPU Fan Meister. }
{ }
{ GPU Fan Meister is free software: you can redistribute it and/or modify it }
{ under the terms of the GNU General Public License as published by }
{ the Free Software Foundation, either version 3 of the License, }
{ or (at your option) any later version. }
{ }
{ GPU Fan Meister 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 }
{ General Public License for more details: https://gnu.org/licenses/gpl.html }
{****************************************************************************/
#include "scripttools.h"
#include <QQmlEngine>
#include <QColor>
#include <QJSValueIterator>
ScriptTools::ScriptTools(QJSEngine* engine) : QObject(nullptr)
,engine(engine)
{
QQmlEngine::setObjectOwnership(this, QQmlEngine::CppOwnership);
}
QVariantMap ScriptTools::fromColor(const QColor &color) const
{
return {
{"r", color.red()},
{"g", color.green()},
{"b", color.blue()},
{"a", color.alpha()}
};
}
QVariantMap ScriptTools::fromColorF(const QColor &color) const
{
return {
{"r", color.redF()},
{"g", color.greenF()},
{"b", color.blueF()},
{"a", color.alphaF()}
};
}
QColor ScriptTools::toColor(const QVariantMap &obj) const
{
return QColor(
obj["r"].toInt(),
obj["g"].toInt(),
obj["b"].toInt(),
obj["a"].toInt());
}
QColor ScriptTools::toColorF(const QVariantMap &obj) const
{
return QColor::fromRgbF(
obj["r"].toReal(),
obj["g"].toReal(),
obj["b"].toReal(),
obj["a"].toReal());
}
QJSValue ScriptTools::clone(const QJSValue &obj, bool deep) const
{
if(obj.isQMetaObject() || obj.isQObject())
return obj;
QJSValue newVal;
if(obj.isArray())
newVal = engine->newArray();
else if(obj.isObject())
newVal = engine->newObject();
else
return obj;
QJSValueIterator it(obj);
while(it.next())
{
newVal.setProperty(
it.name(),
deep ? clone(it.value(), deep) : it.value());
}
return newVal;
}