Blame view

TubeWidget.py 4.78 KB
9f9f1788   Pavel Govyadinov   clead up version ...
1
2
3
4
5
6
7
8
9
10
11
12
13
  #!/usr/bin/env python3
  # -*- coding: utf-8 -*-
  """
  Created on Mon Aug  5 15:53:16 2019
  
  @author: pavel
  """
  
  """
      Qt wrapper/container class that handles all the top level events and maintains the
      methods necessary to use the QT signals and slots API.
  """
  
4407a915   Pavel Govyadinov   working with new ...
14
15
  #from pyqtgraph.Qt import QtCore, QtGui, QtWidgets
  from PyQt5 import QtCore, QtGui, QtWidgets
9f9f1788   Pavel Govyadinov   clead up version ...
16
  from TubeCanvas import TubeDraw
193cb4c6   Pavel Govyadinov   need this to test
17
  import numpy as np
25fa0bfe   Pavel Govyadinov   Stable, pre-vispy...
18
  #import trimesh as tm
9f9f1788   Pavel Govyadinov   clead up version ...
19
  
6eb102f5   Pavel Govyadinov   Fixed issue cause...
20
21
  DEBUG = False
  
4407a915   Pavel Govyadinov   working with new ...
22
  class TubeWidget(QtWidgets.QWidget):
9f9f1788   Pavel Govyadinov   clead up version ...
23
24
25
26
      sigUpdate = QtCore.pyqtSignal(float, float, float)
      #Initializes the QT wrapper class.
      def __init__(self):
          super(TubeWidget, self).__init__()
4407a915   Pavel Govyadinov   working with new ...
27
          box = QtWidgets.QVBoxLayout(self)
9f9f1788   Pavel Govyadinov   clead up version ...
28
29
30
31
32
33
34
          self.resize(500,500)
          self.setLayout(box)
          self.canvas = TubeDraw()
          #self.canvas.create_native()
          box.addWidget(self.canvas.native)
          self.camera = [0,0,0]
          self.down = False
17e4b25a   Pavel Govyadinov   Bug fixes, Dual v...
35
          self.other = None
9f9f1788   Pavel Govyadinov   clead up version ...
36
  
17e4b25a   Pavel Govyadinov   Bug fixes, Dual v...
37
          self.canvas.location = self.geometry().center()
9f9f1788   Pavel Govyadinov   clead up version ...
38
39
40
41
42
43
44
          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_wheel.connect(self.on_mouse_wheel)
  
          #self.show()
  
17e4b25a   Pavel Govyadinov   Bug fixes, Dual v...
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
  
      def link_cameras(self, o_canvas):
          self.canvas.events.mouse_press.connect(o_canvas.on_mouse_press)
          self.canvas.events.mouse_release.connect(o_canvas.on_mouse_release)
          self.canvas.events.mouse_move.connect(o_canvas.on_mouse_move)
          self.canvas.events.mouse_wheel.connect(o_canvas.on_mouse_wheel)
          
      def set_other(self, other):
          self.other = other
      
      def pass_event(self, event, case):
          if self.other != None:
              if case == 0:
                  self.other.canvas.on_mouse_wheel(event)
              if case == 1:
                  self.other.canvas.on_mouse_press(event)
              if case == 2:
                  self.other.canvas.on_mouse_move(event)
              if case == 3:
                  self.other.canvas.on_mouse_release(event)
              #if case == 4:
              #    self.other.sendCameraInfo()
          
          
9f9f1788   Pavel Govyadinov   clead up version ...
69
70
71
72
      #Handles the mouse release event
      def on_mouse_release(self, event):
          self.down=False
          self.sendCameraInfo()
17e4b25a   Pavel Govyadinov   Bug fixes, Dual v...
73
          self.pass_event(event, 3)
9f9f1788   Pavel Govyadinov   clead up version ...
74
75
76
77
78
  
      #Handles the mouse move event
      def on_mouse_move(self, event):
          if self.down:
              self.sendCameraInfo()
17e4b25a   Pavel Govyadinov   Bug fixes, Dual v...
79
              self.pass_event(event, 2)
9f9f1788   Pavel Govyadinov   clead up version ...
80
  
2282da38   Pavel Govyadinov   added path select...
81
      #Handles the mouse press events
9f9f1788   Pavel Govyadinov   clead up version ...
82
83
      def on_mouse_press(self, event):
          self.down = True
193cb4c6   Pavel Govyadinov   need this to test
84
85
86
87
88
89
          if event.button == 2:
              menu = QtWidgets.QMenu(self)
              NS = menu.addAction('Export Model')
              action = menu.exec_(event.native.globalPos())
              if action == NS:
                  self.save_stl_mesh()
17e4b25a   Pavel Govyadinov   Bug fixes, Dual v...
90
91
          else:
              self.pass_event(event, 1)
193cb4c6   Pavel Govyadinov   need this to test
92
                  
9f9f1788   Pavel Govyadinov   clead up version ...
93
94
95
96
  
      #Handles the mouse wheel event
      def on_mouse_wheel(self, event):
          self.sendCameraInfo()
17e4b25a   Pavel Govyadinov   Bug fixes, Dual v...
97
          self.pass_event(event, 0)
9f9f1788   Pavel Govyadinov   clead up version ...
98
99
100
101
102
103
104
  
      #controls the emit function of the QT class to send a signal to the slot
      #located in the other window.
      def sendCameraInfo(self):
          #self.view = self.canvas.view
          self.camera = self.canvas.camera
          #print("stuff", self.view[3, 0], self.view[3, 1], self.view[3, 2])
6eb102f5   Pavel Govyadinov   Fixed issue cause...
105
106
          if DEBUG:
              print("stuff", self.camera[0], self.camera[1], self.camera[2])
17e4b25a   Pavel Govyadinov   Bug fixes, Dual v...
107
          #self.pass_event(None, 4)
9f9f1788   Pavel Govyadinov   clead up version ...
108
          self.sigUpdate.emit(self.camera[0], self.camera[1], self.camera[2])
6aca1767   Pavel Govyadinov   Added a signal to...
109
110
111
112
113
114
115
          
      """
      Function to receive the signal with the information necessary to visualize
      specific fibers only. With the rest of them being minimized.
      """
      @QtCore.pyqtSlot(list)
      def select(self, x):
2282da38   Pavel Govyadinov   added path select...
116
117
118
119
120
121
          self.canvas.select_edges(x)
  #        for e in x:
  #            if e in self.canvas.edge_dict:    
  #                print(self.canvas.edge_dict[e])
  #            elif (e[1], e[0]) in self.canvas.edge_dict:
  #                print(self.canvas.edge_dict[(e[1], e[0])])
6aca1767   Pavel Govyadinov   Added a signal to...
122
          print("got signal", x)
9f9f1788   Pavel Govyadinov   clead up version ...
123
  
6aca1767   Pavel Govyadinov   Added a signal to...
124
125
126
127
128
129
      """
          Initialization method that connects the slot to the function that handles
          the information being sent.
      """
      def connect(self, signal_object):
          signal_object.select.connect(self.select)
193cb4c6   Pavel Govyadinov   need this to test
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
          
      def save_stl_mesh(self):
          triangles = self.canvas.triangle_data
          caps = self.canvas.cap_data
          points = self.canvas.cylinder_data['a_position']
          colors = self.canvas.cylinder_data['a_fg_color']
          normals = self.canvas.cylinder_data['a_normal']
          
          mesh = tm.Trimesh(vertices=points, faces=np.append(triangles, caps, axis=0),
                            vertex_colors=colors, vertex_normals=normals)
          mesh.export('with_caps.obj')
          mesh.export('with_caps.ply')