Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

📚 The CoCalc Library - books, templates and other resources

132928 views
License: OTHER
1
#!/usr/bin/env python
2
#
3
# Copyright 2019 the original author or authors.
4
#
5
# Licensed under the Apache License, Version 2.0 (the "License");
6
# you may not use this file except in compliance with the License.
7
# You may obtain a copy of the License at
8
#
9
# http://www.apache.org/licenses/LICENSE-2.0
10
#
11
# Unless required by applicable law or agreed to in writing, software
12
# distributed under the License is distributed on an "AS IS" BASIS,
13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
# See the License for the specific language governing permissions and
15
# limitations under the License.
16
#
17
import pygame
18
import numpy as np
19
from cmath import isclose
20
from .number_picker import NumberPicker
21
from .matrix_label import MatrixLabel
22
from vqe_playground.utils.labels import comp_graph_node_labels
23
from vqe_playground.utils.fonts import ARIAL_36
24
25
26
class AdjacencyMatrix(pygame.sprite.RenderPlain):
27
ELEMENT_WIDTH_HEIGHT = 48
28
MAX_EDGE_VALUE = 4
29
30
"""UI control for maintaining adjacency matrix"""
31
def __init__(self, xpos, ypos, adj_matrix_numeric):
32
self.adj_matrix_numeric = adj_matrix_numeric
33
self.xpos = xpos
34
self.ypos = ypos
35
36
self.adj_matrix_graph_dirty = False
37
self.num_nodes = adj_matrix_numeric.shape[0]
38
self.row_col_labels_dict = comp_graph_node_labels(self.num_nodes)
39
self.row_labels_list = self.create_row_labels_list()
40
self.col_labels_list = self.create_col_labels_list()
41
self.number_pickers_list = self.create_number_pickers_list()
42
pygame.sprite.RenderPlain.__init__(self,
43
self.row_labels_list,
44
self.col_labels_list,
45
self.number_pickers_list)
46
self.arrange()
47
48
def create_number_pickers_list(self):
49
pickers = []
50
for row in range(self.num_nodes):
51
for col in range(self.num_nodes):
52
pickers.append(NumberPicker(self.adj_matrix_numeric[row, col],
53
self.ELEMENT_WIDTH_HEIGHT,
54
self.ELEMENT_WIDTH_HEIGHT,
55
row != col))
56
return pickers
57
58
def create_row_labels_list(self):
59
row_labels = []
60
for row in range(self.num_nodes):
61
row_labels.append(MatrixLabel(self.row_col_labels_dict[row],
62
self.ELEMENT_WIDTH_HEIGHT,
63
self.ELEMENT_WIDTH_HEIGHT))
64
return row_labels
65
66
def create_col_labels_list(self):
67
col_labels = []
68
for col in range(self.num_nodes):
69
col_labels.append(MatrixLabel(self.row_col_labels_dict[col],
70
self.ELEMENT_WIDTH_HEIGHT,
71
self.ELEMENT_WIDTH_HEIGHT))
72
return col_labels
73
74
def arrange(self):
75
for col in range(self.num_nodes):
76
col_label = self.col_labels_list[col]
77
col_label.rect.left = self.xpos + (col + 1) * col_label.rect.width
78
col_label.rect.top = self.ypos
79
80
for row in range(self.num_nodes):
81
row_label = self.row_labels_list[row]
82
row_label.rect.left = self.xpos
83
row_label.rect.top = self.ypos + (row + 1) * row_label.rect.height
84
85
next_ypos = self.ypos + self.ELEMENT_WIDTH_HEIGHT
86
for row in range(self.num_nodes):
87
next_xpos = self.xpos + self.ELEMENT_WIDTH_HEIGHT
88
for col in range(self.num_nodes):
89
picker = self.number_pickers_list[row * self.num_nodes + col]
90
picker.rect.left = next_xpos
91
picker.rect.top = next_ypos
92
next_xpos += picker.rect.width
93
next_ypos += picker.rect.height
94
95
def handle_element_clicked(self, picker):
96
for idx, picker_in_list in enumerate(self.number_pickers_list):
97
if picker == picker_in_list:
98
row = idx // self.num_nodes
99
col = idx % self.num_nodes
100
if row != col:
101
if isclose(picker_in_list.number, 0):
102
picker_in_list.number = 1
103
self.adj_matrix_graph_dirty = True
104
elif picker_in_list.number < self.MAX_EDGE_VALUE:
105
picker_in_list.number += 1
106
else:
107
picker_in_list.number = 0
108
self.adj_matrix_graph_dirty = True
109
110
picker_in_list.draw_number_picker()
111
self.adj_matrix_numeric[row, col] = picker_in_list.number
112
113
# Also update the other side
114
other_idx = col * self.num_nodes + row
115
other_picker = self.number_pickers_list[other_idx]
116
other_picker.number = picker_in_list.number
117
other_picker.draw_number_picker()
118
self.adj_matrix_numeric[col, row] = picker_in_list.number
119
120