-
Notifications
You must be signed in to change notification settings - Fork 1
/
nodz_utils.py
170 lines (123 loc) · 3.96 KB
/
nodz_utils.py
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import os
import json
import re
from Qt import QtCore, QtGui
def _convertDataToColor(data=None, alternate=False, av=20):
"""
Convert a list of 3 (rgb) or 4(rgba) values from the configuration
file into a QColor.
:param data: Input color.
:type data: List.
:param alternate: Whether or not this is an alternate color.
:type alternate: Bool.
:param av: Alternate value.
:type av: Int.
"""
# rgb
if len(data) == 3:
color = QtGui.QColor(data[0], data[1], data[2])
if alternate:
mult = _generateAlternateColorMultiplier(color, av)
color = QtGui.QColor(data[0]-(av*mult), data[1]-(av*mult), data[2]-(av*mult))
return color
# rgba
elif len(data) == 4:
color = QtGui.QColor(data[0], data[1], data[2], data[3])
if alternate:
mult = _generateAlternateColorMultiplier(color, av)
color = QtGui.QColor(data[0]-(av*mult), data[1]-(av*mult), data[2]-(av*mult), data[3])
return color
# wrong
else:
print 'Color from configuration is not recognized : ', data
print 'Can only be [R, G, B] or [R, G, B, A]'
print 'Using default color !'
color = QtGui.QColor(120, 120, 120)
if alternate:
color = QtGui.QColor(120-av, 120-av, 120-av)
return color
def _generateAlternateColorMultiplier(color, av):
"""
Generate a multiplier based on the input color lighness to increase
the alternate value for dark color or reduce it for bright colors.
:param color: Input color.
:type color: QColor.
:param av: Alternate value.
:type av: Int.
"""
lightness = color.lightness()
mult = float(lightness)/255
return mult
def _createPointerBoundingBox(pointerPos, bbSize):
"""
generate a bounding box around the pointer.
:param pointerPos: Pointer position.
:type pointerPos: QPoint.
:param bbSize: Width and Height of the bounding box.
:type bbSize: Int.
"""
# Create pointer's bounding box.
point = pointerPos
mbbPos = point
point.setX(point.x() - bbSize / 2)
point.setY(point.y() - bbSize / 2)
size = QtCore.QSize(bbSize, bbSize)
bb = QtCore.QRect(mbbPos, size)
bb = QtCore.QRectF(bb)
return bb
def _swapListIndices(inputList, oldIndex, newIndex):
"""
Simply swap 2 indices in a the specified list.
:param inputList: List that contains the elements to swap.
:type inputList: List.
:param oldIndex: Index of the element to move.
:type oldIndex: Int.
:param newIndex: Destination index of the element.
:type newIndex: Int.
"""
if oldIndex == -1:
oldIndex = len(inputList)-1
if newIndex == -1:
newIndex = len(inputList)
value = inputList[oldIndex]
inputList.pop(oldIndex)
inputList.insert(newIndex, value)
# IO
def _loadConfig(filePath):
"""
Read the configuration file and strips out comments.
:param filePath: File path.
:type filePath: Str.
"""
with open(filePath, 'r') as myfile:
fileString = myfile.read()
# remove comments
cleanString = re.sub('//.*?\n|/\*.*?\*/', '', fileString, re.S)
data = json.loads(cleanString)
return data
def _saveData(filePath, data):
"""
save data as a .json file
:param filePath: Path of the .json file.
:type filePath: Str.
:param data: Data you want to save.
:type data: Dict or List.
"""
f = open(filePath, "w")
f.write(json.dumps(data,
sort_keys = True,
indent = 4,
ensure_ascii=False))
f.close()
print "Data successfully saved !"
def _loadData(filePath):
"""
load data from a .json file.
:param filePath: Path of the .json file.
:type filePath: Str.
"""
with open(filePath) as json_file:
j_data = json.load(json_file)
json_file.close()
print "Data successfully loaded !"
return j_data