FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
OSPlugin/GraphBase.py at main · StreamController/OSPlugin · GitHub
StreamController
/
OSPlugin
Public
Notifications
You must be signed in to change notification settings
Fork
14
Star
8
Code
Issues
10
Pull requests
2
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
OSPlugin
/
GraphBase.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
272 lines (208 loc) · 9.9 KB
Breadcrumbs
OSPlugin
/
GraphBase.py
Copy path
File metadata and controls
272 lines (208 loc) · 9.9 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
from
threading
import
Thread
from
src
.
backend
.
PluginManager
.
ActionBase
import
ActionBase
from
src
.
backend
.
DeckManagement
.
DeckController
import
DeckController
from
src
.
backend
.
PageManagement
.
Page
import
Page
from
src
.
backend
.
PluginManager
.
PluginBase
import
PluginBase
import
matplotlib
.
pyplot
as
plt
import
matplotlib
from
multiprocessing
import
Process
,
Queue
# Use different backend to prevent errors with running plt in different threads
matplotlib
.
use
(
'agg'
)
from
matplotlib
.
backends
.
backend_agg
import
FigureCanvasAgg
as
FigureCanvas
from
PIL
import
Image
import
io
# Import gtk
import
gi
gi
.
require_version
(
"Gtk"
,
"4.0"
)
gi
.
require_version
(
"Adw"
,
"1"
)
from
gi
.
repository
import
Gtk
,
Adw
,
Gdk
# Import globals
import
globals
as
gl
from
src
.
Signals
import
Signals
class
GraphBase
(
ActionBase
):
def
__init__
(
self
,
*
args
,
**
kwargs
):
super
().
__init__
(
*
args
,
**
kwargs
)
self
.
percentages
:
list
[
float
]
=
[]
self
.
task_queue
=
Queue
()
self
.
result_queue
=
Queue
()
# self.process = Process(target=self.worker, args=(self.task_queue, self.result_queue), name="GraphBaseCreator")
self
.
process
=
GraphCreator
(
task_queue
=
self
.
task_queue
,
result_queue
=
self
.
result_queue
)
self
.
process
.
start
()
gl
.
signal_manager
.
connect_signal
(
Signals
.
AppQuit
,
self
.
stop_process
)
def
stop_process
(
self
,
*
args
):
print
(
"stop graph base"
)
self
.
task_queue
.
put
((
None
,
None
))
def
set_percentages_lenght
(
self
,
length
:
int
):
if
len
(
self
.
percentages
)
>
length
:
self
.
percentages
=
self
.
percentages
[
-
length
:]
elif
len
(
self
.
percentages
)
<
length
:
for
_
in
range
(
length
-
len
(
self
.
percentages
)):
self
.
percentages
.
insert
(
0
,
0
)
return
self
.
percentages
def
get_graph
(
self
)
->
Image
:
## Get vars
settings
=
self
.
get_settings
()
time_period
=
settings
.
get
(
"time-period"
,
15
)
self
.
set_percentages_lenght
(
time_period
)
self
.
task_queue
.
put
((
settings
,
self
.
percentages
))
img
=
self
.
result_queue
.
get
()
return
img
def
show_graph
(
self
):
image
=
self
.
get_graph
()
if
image
is
None
:
return
self
.
set_media
(
image
=
image
)
def
get_custom_config_area
(
self
):
return
Gtk
.
Label
(
label
=
self
.
plugin_base
.
lm
.
get
(
"actions.graph-base.memory-warning"
),
css_classes
=
[
"destructive-action"
])
def
get_config_rows
(
self
)
->
list
:
self
.
line_color_row
=
ColorRow
()
self
.
line_color_row
.
color_label
.
set_label
(
"Line Color:"
)
self
.
fill_color
=
ColorRow
()
self
.
fill_color
.
color_label
.
set_label
(
"Fill Color:"
)
self
.
line_width_row
=
Adw
.
SpinRow
.
new_with_range
(
1
,
30
,
1
)
self
.
line_width_row
.
set_title
(
"Line Width:"
)
self
.
time_period_row
=
Adw
.
SpinRow
.
new_with_range
(
1
,
60
,
1
)
self
.
time_period_row
.
set_title
(
"Time Period (s):"
)
self
.
dynamic_scaling_row
=
Adw
.
SwitchRow
(
title
=
"Dynamic Y-axis Scaling:"
)
self
.
toggle_dynamic_scaling_on_press_row
=
Adw
.
SwitchRow
(
title
=
"Toggle Dynamic Y-axis Scaling on Press:"
)
## Load defaults:
settings
=
self
.
get_settings
()
self
.
fill_color
.
color_button
.
set_rgba
(
self
.
prepare_color
(
settings
.
get
(
"fill-color"
, [
255
,
255
,
255
,
255
])))
self
.
line_color_row
.
color_button
.
set_rgba
(
self
.
prepare_color
(
settings
.
get
(
"line-color"
, [
255
,
255
,
255
,
150
])))
self
.
line_width_row
.
set_value
(
settings
.
get
(
"line-width"
,
5
))
self
.
time_period_row
.
set_value
(
settings
.
get
(
"time-period"
,
15
))
self
.
dynamic_scaling_row
.
set_active
(
settings
.
get
(
"dynamic-scaling"
,
False
))
self
.
toggle_dynamic_scaling_on_press_row
.
set_active
(
settings
.
get
(
"toggle-dynamic-scaling-on-press"
,
False
))
## Connect signals
self
.
fill_color
.
color_button
.
connect
(
"color-set"
,
self
.
on_fill_color_change
)
self
.
line_color_row
.
color_button
.
connect
(
"color-set"
,
self
.
on_line_color_change
)
self
.
line_width_row
.
connect
(
"changed"
,
self
.
on_line_width_change
)
self
.
time_period_row
.
connect
(
"changed"
,
self
.
on_time_period_change
)
self
.
dynamic_scaling_row
.
connect
(
"notify::active"
,
self
.
on_dynamic_scaling_change
)
self
.
toggle_dynamic_scaling_on_press_row
.
connect
(
"notify::active"
,
self
.
on_dynamic_scaling_toggle_on_press_change
)
return
[
self
.
line_color_row
,
self
.
fill_color
,
self
.
line_width_row
,
self
.
time_period_row
,
self
.
dynamic_scaling_row
,
self
.
toggle_dynamic_scaling_on_press_row
]
def
prepare_color
(
self
,
color_values
:
list
[
int
])
->
Gdk
.
RGBA
:
if
len
(
color_values
)
==
3
:
color_values
.
append
(
255
)
color
=
Gdk
.
RGBA
()
color
.
parse
(
f"rgba(
{
color_values
[
0
]
}
,
{
color_values
[
1
]
}
,
{
color_values
[
2
]
}
,
{
color_values
[
3
]
}
)"
)
return
color
def
on_fill_color_change
(
self
,
button
):
color
=
self
.
fill_color
.
color_button
.
get_rgba
()
green
=
round
(
color
.
green
*
255
)
blue
=
round
(
color
.
blue
*
255
)
red
=
round
(
color
.
red
*
255
)
alpha
=
round
(
color
.
alpha
*
255
)
settings
=
self
.
get_settings
()
settings
[
"fill-color"
]
=
[
red
,
green
,
blue
,
alpha
]
self
.
set_settings
(
settings
)
self
.
show_graph
()
def
on_line_color_change
(
self
,
button
):
color
=
self
.
line_color_row
.
color_button
.
get_rgba
()
green
=
round
(
color
.
green
*
255
)
blue
=
round
(
color
.
blue
*
255
)
red
=
round
(
color
.
red
*
255
)
alpha
=
round
(
color
.
alpha
*
255
)
settings
=
self
.
get_settings
()
settings
[
"line-color"
]
=
[
red
,
green
,
blue
,
alpha
]
self
.
set_settings
(
settings
)
self
.
show_graph
()
def
on_line_width_change
(
self
,
spin
):
settings
=
self
.
get_settings
()
settings
[
"line-width"
]
=
spin
.
get_value
()
self
.
set_settings
(
settings
)
self
.
show_graph
()
def
on_time_period_change
(
self
,
spin
):
settings
=
self
.
get_settings
()
settings
[
"time-period"
]
=
int
(
spin
.
get_value
())
self
.
set_settings
(
settings
)
self
.
set_percentages_lenght
(
int
(
spin
.
get_value
()))
self
.
show_graph
()
def
on_dynamic_scaling_change
(
self
,
switch
,
*
args
):
settings
=
self
.
get_settings
()
settings
[
"dynamic-scaling"
]
=
switch
.
get_active
()
self
.
set_settings
(
settings
)
self
.
show_graph
()
def
on_dynamic_scaling_toggle_on_press_change
(
self
,
switch
,
*
args
):
settings
=
self
.
get_settings
()
settings
[
"toggle-dynamic-scaling-on-press"
]
=
switch
.
get_active
()
self
.
set_settings
(
settings
)
self
.
show_graph
()
def
on_key_down
(
self
):
settings
=
self
.
get_settings
()
if
not
settings
.
get
(
"toggle-dynamic-scaling-on-press"
,
False
):
return
settings
[
"dynamic-scaling"
]
=
not
settings
.
get
(
"dynamic-scaling"
,
False
)
self
.
set_settings
(
settings
)
if
hasattr
(
self
,
"dynamic_scaling_row"
):
self
.
dynamic_scaling_row
.
set_active
(
settings
[
"dynamic-scaling"
])
self
.
show_graph
()
def
on_removed_from_cache
(
self
)
->
None
:
self
.
task_queue
.
put
((
None
,
None
))
class
ColorRow
(
Adw
.
PreferencesRow
):
def
__init__
(
self
):
super
().
__init__
()
self
.
main_box
=
Gtk
.
Box
(
orientation
=
Gtk
.
Orientation
.
HORIZONTAL
,
hexpand
=
True
,
margin_top
=
10
,
margin_bottom
=
10
,
margin_start
=
15
,
margin_end
=
15
)
self
.
set_child
(
self
.
main_box
)
self
.
color_label
=
Gtk
.
Label
(
label
=
"Color:"
,
hexpand
=
True
,
xalign
=
0
)
self
.
main_box
.
append
(
self
.
color_label
)
self
.
color_button
=
Gtk
.
ColorButton
(
use_alpha
=
True
)
self
.
main_box
.
append
(
self
.
color_button
)
class
GraphCreator
(
Process
):
def
__init__
(
self
,
task_queue
:
Queue
,
result_queue
:
Queue
):
super
().
__init__
(
daemon
=
True
,
name
=
"GraphCreator"
)
self
.
task_queue
=
task_queue
self
.
result_queue
=
result_queue
def
run
(
self
):
while
True
:
settings
,
percentages
=
self
.
task_queue
.
get
()
if
None
in
[
settings
,
percentages
]:
break
result
=
self
.
generate_graph
(
settings
,
percentages
)
self
.
result_queue
.
put
(
result
)
print
(
"GraphCreator stopped"
)
def
generate_graph
(
self
,
settings
:
dict
,
percentages
:
list
[
float
]):
line_color
=
self
.
conv_color_to_plt
(
settings
.
get
(
"line-color"
, [
255
,
255
,
255
,
255
]))
fill_color
=
self
.
conv_color_to_plt
(
settings
.
get
(
"fill-color"
, [
255
,
255
,
255
,
150
]))
line_width
=
settings
.
get
(
"line-width"
,
5
)
dynamic_scaling
=
settings
.
get
(
"dynamic-scaling"
,
False
)
# Create a new figure with a transparent background
fig
=
plt
.
figure
(
figsize
=
(
6
,
6
))
fig
.
patch
.
set_alpha
(
0
)
fig
.
patch
.
set_facecolor
(
'none'
)
# Set the FigureCanvas to the backend
canvas
=
FigureCanvas
(
fig
)
# Plot the data with a transparent background
ax
=
plt
.
Axes
(
fig
, [
0.
,
0.
,
1.
,
1.
])
ax
.
set_axis_off
()
fig
.
add_axes
(
ax
)
ax
.
plot
(
percentages
,
color
=
line_color
,
linewidth
=
line_width
)
ax
.
fill_between
(
range
(
len
(
percentages
)),
percentages
,
color
=
fill_color
[:
3
],
alpha
=
fill_color
[
3
])
# Hide the spines
for
spine
in
ax
.
spines
.
values
():
spine
.
set_visible
(
False
)
# Turn off the axis and set margins to zero
ax
.
margins
(
0
)
ax
.
axis
(
'off'
)
# Set the y-axis to range from 0 to 100
if
not
dynamic_scaling
:
ax
.
set_ylim
(
0
,
100
)
# Draw the canvas and retrieve the buffer
canvas
.
draw
()
buf
=
io
.
BytesIO
()
canvas
.
print_png
(
buf
)
# Convert buffer to a Pillow Image
buf
.
seek
(
0
)
img
=
Image
.
open
(
buf
)
# The Pillow Image now has a transparent background
# img.show() # If you want to display the image
# img.save('plot.png') # If you want to save the image
plt
.
close
()
# Close the plot to free resources
# result_queue.put(img)
return
img
def
conv_color_to_plt
(
self
,
color
:
list
[
int
])
->
list
[
float
]:
float_color
:
list
[
float
]
=
[]
for
c
in
color
:
float_color
.
append
(
c
/
255
)
return
float_color
Back
|
FazBrowse Home
|
New Git URL