Blame view

GraphWidget.py 10.4 KB
9f9f1788   Pavel Govyadinov   clead up version ...
1
2
3
4
5
6
7
8
9
  #!/usr/bin/env python3
  # -*- coding: utf-8 -*-
  """
  Created on Mon Aug  5 15:42:19 2019
  
  @author: pavel
  """
  
  from GraphCanvas import GraphCanvas
4407a915   Pavel Govyadinov   working with new ...
10
  from PyQt5 import QtCore, QtGui, QtWidgets
9f9f1788   Pavel Govyadinov   clead up version ...
11
12
  import network_dep as nwt
  
25fa0bfe   Pavel Govyadinov   Stable, pre-vispy...
13
14
  import numpy as np
  
6eb102f5   Pavel Govyadinov   Fixed issue cause...
15
  DEBUG = False
9f9f1788   Pavel Govyadinov   clead up version ...
16
17
18
19
20
21
  
  """    
  Initializes the entire QTlayout and sets the mouse press events.
  These are connected to the slots such that each is processes by this class
  and the class it wraps.
  """    
4407a915   Pavel Govyadinov   working with new ...
22
23
  #class GraphWidget(QtGui.QWidget):
  class GraphWidget(QtWidgets.QWidget):
6aca1767   Pavel Govyadinov   Added a signal to...
24
      select = QtCore.pyqtSignal(list)
9f9f1788   Pavel Govyadinov   clead up version ...
25
26
      def __init__(self):
          super(GraphWidget, self).__init__()
4407a915   Pavel Govyadinov   working with new ...
27
          box = QtWidgets.QVBoxLayout(self)
9f9f1788   Pavel Govyadinov   clead up version ...
28
29
30
          self.resize(500,500)
          self.setLayout(box)
          self.canvas = GraphCanvas()
4407a915   Pavel Govyadinov   working with new ...
31
          self.setUpdatesEnabled(True)
9f9f1788   Pavel Govyadinov   clead up version ...
32
33
34
35
36
37
38
39
40
41
          #self.canvas.create_native()
          box.addWidget(self.canvas.native)
          self.color = True
          self.use_3D = False
          self.camera = [0,0,0]
  
          self.canvas.events.mouse_press.connect(self.on_mouse_press)
          self.canvas.events.mouse_release.connect(self.on_mouse_release)
          self.canvas.events.mouse_move.connect(self.on_mouse_move)
          self.canvas.events.mouse_double_click.connect(self.on_mouse_double_click)
4407a915   Pavel Govyadinov   working with new ...
42
          #self.canvas.events.
9f9f1788   Pavel Govyadinov   clead up version ...
43
          #self.show()
4407a915   Pavel Govyadinov   working with new ...
44
          #self.repaint()
9f9f1788   Pavel Govyadinov   clead up version ...
45
46
47
48
49
50
51
52
53
54
  
      """
      Handles the right click mouse event at the QWidget level.
      Depending on where the mouse button in clicked and the current zoom level,
      the function gets the unique id of the node, cluster or background under the
      cursor and opens a context menu based on the ID.
      
      The context menu level actions are also coded in this section of the code.
      """
      def on_mouse_press(self, event):
4407a915   Pavel Govyadinov   working with new ...
55
          self.canvas.set_current(event)
9f9f1788   Pavel Govyadinov   clead up version ...
56
57
          if event.button == 2:
              if self.canvas.view[0][0] >= 0.0010:
4407a915   Pavel Govyadinov   working with new ...
58
                  self.canvas.update()
9f9f1788   Pavel Govyadinov   clead up version ...
59
                  c_id = self.canvas.get_clicked_id(event)
4407a915   Pavel Govyadinov   working with new ...
60
                  self.canvas.update()
9f9f1788   Pavel Govyadinov   clead up version ...
61
              else:
4407a915   Pavel Govyadinov   working with new ...
62
                  self.canvas.update()
9f9f1788   Pavel Govyadinov   clead up version ...
63
                  c_id = self.canvas.get_clicked_id(event, clusters=True)
4407a915   Pavel Govyadinov   working with new ...
64
                  self.canvas.update()
9f9f1788   Pavel Govyadinov   clead up version ...
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
              #right click
              if(c_id == None):
                  menu = QtWidgets.QMenu(self)
                  NS = menu.addAction('Node Size')
  
                  #tmp = menu.addAction('temp')
                  tmp = menu.addMenu('Node Color')
                  vertex_props = self.canvas.G.vertex_properties.keys()
                  vertex_actions = []
                  for i in range(len(vertex_props)):
                      if(vertex_props[i] != "map" or vertex_props[i] != "RGBA"):
                          vertex_actions.append(tmp.addAction(vertex_props[i]))
                  #tmp.addAction("Cluster")
                  #NC = menu.addAction('Node Color')
                  #EXP = menu.addAction('Export VTK')
                  #EXP_adv = menu.addAction('Export VTK Advanced')
                  NL = menu.addAction('New Layout')
25fa0bfe   Pavel Govyadinov   Stable, pre-vispy...
82
83
84
                  NSL = menu.addAction('New Spring-Onion Layout (sum)')
                  NSLMI = menu.addAction('New Spring-Onion Layout(max)')
                  EXPAND = menu.addAction('Expand')
9f9f1788   Pavel Govyadinov   clead up version ...
85
                  action = menu.exec_(event.native.globalPos())
6eb102f5   Pavel Govyadinov   Fixed issue cause...
86
87
                  if DEBUG:
                      print(action)
9f9f1788   Pavel Govyadinov   clead up version ...
88
89
90
91
92
93
                  for i in range(len(vertex_actions)):
                      if action == vertex_actions[i]:
                          if vertex_props[i] == "clusters":
                              self.canvas.color_vertices(self.canvas.G, vertex_props[i], dtype=True)
                          else:
                              self.canvas.color_vertices(self.canvas.G, vertex_props[i])
6eb102f5   Pavel Govyadinov   Fixed issue cause...
94
95
96
                              
                          if DEBUG:
                              print(vertex_props[i])
9f9f1788   Pavel Govyadinov   clead up version ...
97
98
99
100
101
102
                  if action == NS:
                      if self.use_3D == False:
                          self.use_3D = True
                      else:
                          self.use_3D = False
                          self.canvas.size_vertices(self.canvas.G, 'degree' )
6eb102f5   Pavel Govyadinov   Fixed issue cause...
103
                          self.canvas.color_vertices(self.canvas.G, 'degree')
9f9f1788   Pavel Govyadinov   clead up version ...
104
105
106
107
108
109
110
111
112
113
114
                  #if action == NC:
                  #    self.canvas.color_vertices(self.canvas.G, not self.color)
                  #    self.color = not self.color
  #                if action == EXP:
  #                    nwt.Network.write_vtk(self.canvas.G, "./nwt.vtk")
  #                if action == EXP_adv:
  #                    nwt.Network.write_vtk(self.canvas.G, "./nwt_median_binned.vtk")
  #                    nwt.Network.write_vtk(self.canvas.G, "./nwt_median_non_binned.vtk", binning=False)
  #                    nwt.Network.write_vtk(self.canvas.G, "./nwt_points_wise_binned.vtk", camera=self.camera, binning = True)
  #                    nwt.Network.write_vtk(self.canvas.G, "./nwt_points_wise_non_binned.vtk", camera=self.camera, binning = False)
                  if action == tmp:
6eb102f5   Pavel Govyadinov   Fixed issue cause...
115
116
                      if DEBUG:
                          print("Stuff")
9f9f1788   Pavel Govyadinov   clead up version ...
117
118
119
120
121
122
123
                      #self.cb = QtWidgets.QComboBox()
                      #vertex_props = self.canvas.G.vertex_properties.keys()
                      #for i in range(len(vertex_props)):
                      #    self.cb.addItem(vertex_props[i])
                      #self.cb.currentIndexChanged.connect(self.selection_change)
                      #self.cb.show()
                  if action == NL:
25fa0bfe   Pavel Govyadinov   Stable, pre-vispy...
124
125
126
127
128
129
                      old_pos = self.canvas.G.vertex_properties["pos"].get_2d_array(range(3)).T
                      nG = nwt.Network.gen_new_fd_layout(self.canvas.G)
                      new_pos = nG.vertex_properties["pos"].get_2d_array(range(3)).T
                      self.canvas.animate(old_pos, new_pos)
                      
                      #self.canvas.gen_vertex_vbo(self.canvas.G)
9f9f1788   Pavel Govyadinov   clead up version ...
130
                      #self.canvas.set_data(self.canvas.G, self.canvas.bbl, self.canvas.bbu)
25fa0bfe   Pavel Govyadinov   Stable, pre-vispy...
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
                      #self.canvas.expand_clusters(self.canvas.G, self.canvas.n_c)
                  if action == NSL:
                      old_pos = self.canvas.G.vertex_properties["pos"].get_2d_array(range(3)).T
                      nG = nwt.Network.gen_spring_onion_layout(self.canvas.G, 'degree', 1.0, 1000, 0.01, 1.0)
                      new_pos = nG.vertex_properties["pos"].get_2d_array(range(3)).T
                      
                      self.canvas.animate(old_pos, new_pos)
                      #self.canvas.set_data(nG, self.canvas.bbl, self.canvas.bbu)
                      
                      #self.canvas.gen_vertex_vbo(self.canvas.G)
                      #self.canvas.expand_clusters(self.canvas.G, self.canvas.n_c)
                  if action == NSLMI:
                      old_pos = self.canvas.G.vertex_properties["pos"].get_2d_array(range(3)).T
                      nG = nwt.Network.gen_spring_onion_layout(self.canvas.G, 'degree', 1.0, 1000, 0.01, 1.0, False)
                      new_pos = nG.vertex_properties["pos"].get_2d_array(range(3)).T
                      
                      self.canvas.animate(old_pos, new_pos)
                      #self.canvas.set_data(nG, self.canvas.bbl, self.canvas.bbu)
                      #self.canvas.expand_clusters(self.canvas.G, self.canvas.n_c)
                      
                      #self.canvas.gen_vertex_vbo(self.canvas.G)
                      
                  if action == EXPAND:
                      #self.canvas.gen_vertex_vbo(self.canvas.G)
                      old_pos = self.canvas.G.vertex_properties["pos"].get_2d_array(range(3)).T
9f9f1788   Pavel Govyadinov   clead up version ...
156
                      self.canvas.expand_clusters(self.canvas.G, self.canvas.n_c)
25fa0bfe   Pavel Govyadinov   Stable, pre-vispy...
157
158
159
160
                      new_pos = self.canvas.G.vertex_properties["pos"].get_2d_array(range(3)).T
                      #self.canvas.expand_clusters(self.canvas.G, self.canvas.n_c)
                      
                      self.canvas.animate(old_pos, new_pos)
9f9f1788   Pavel Govyadinov   clead up version ...
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
                      #self.canvas.size_vertices(self.canvas.G, 'degree_volume')
              else:
                  if self.canvas.view[0][0] >= 0.0010:
                      menu = QtWidgets.QMenu(self)
                      NS = menu.addAction('Display Node Info')
                      action = menu.exec_(event.native.globalPos())
                      if action == NS:
                          print("Display Node Info")
                  else:
                      menu = QtWidgets.QMenu(self)
                      MnOC = menu.addAction('Minimize Other Clusters')
                      RA   = menu.addAction('Reset Alpha')
                      action = menu.exec_(event.native.globalPos())
                      if action == MnOC:
                          new_Graph = self.canvas.focus_on_cluster(self.canvas.G, c_id)
                          self.test.draw_new(new_Graph)
                      if action == RA:
                          self.canvas.reset_alpha(c_id[0])
  
  
  #    def selection_change(self, i):
  #        self.canvas.size_vertices(self.canvas.G, self.cb.currentText())
  
  
      """
          Handles the mouse double click event.
          Pass-through function.
      """
4407a915   Pavel Govyadinov   working with new ...
189
      
9f9f1788   Pavel Govyadinov   clead up version ...
190
      def on_mouse_double_click(self, event):
2282da38   Pavel Govyadinov   added path select...
191
192
          self.canvas.update_path(event)
          self.select.emit(self.canvas.get_path())
4407a915   Pavel Govyadinov   working with new ...
193
194
  #        old_pos = self.canvas.G.vertex_properties["pos"].get_2d_array(range(3)).T
  #        nG = nwt.Network.gen_new_fd_layout(self.canvas.G)
25fa0bfe   Pavel Govyadinov   Stable, pre-vispy...
195
196
          #    def animate(self, new_G):
  #            
4407a915   Pavel Govyadinov   working with new ...
197
198
  #        new_pos = nG.vertex_properties["pos"].get_2d_array(range(3)).T
  #        self.canvas.animate(old_pos, new_pos)
9f9f1788   Pavel Govyadinov   clead up version ...
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
  
      """
          Handles the mouse release event.
          Pass-through function.
      """
      def on_mouse_release(self, event):
          n = 1
  
      """
          Handles the mouse move event.
          Pass-through function.
      """
      def on_mouse_move(self, event):
          n = 2
  
      """
          Internal function that interacts with the tube visualization.
      """
      def set_g_view(self, test):
          self.test = test
  
      """
          Function to handle the pyqt Slot that receives the interacted-with signal
          from the Tube visualization. The signal sends the camera position in 3D coordinates
          every time the camera is moved.
      """
      @QtCore.pyqtSlot(float, float, float)
      def sigUpdate(self, x, y, z):
          self.camera = [x,y,z]
          if(self.use_3D == True):
              self.camera = [x,y,z]
              self.canvas.vertexSizeFromDistance(self.canvas.G, [x,y,z])
              self.canvas.vertexAlphaFromDistance(self.canvas.G, [x,y,z])
6eb102f5   Pavel Govyadinov   Fixed issue cause...
232
233
              if DEBUG:
                  print("got signal", x, y, z)
9f9f1788   Pavel Govyadinov   clead up version ...
234
235
236
237
238
239
240
  
      """
          Initialization method that connects the slot to the function that handles
          the information being sent.
      """
      def connect(self, signal_object):
          signal_object.sigUpdate.connect(self.sigUpdate)