GraphWidget.py 7.43 KB
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Aug  5 15:42:19 2019

@author: pavel
"""

from GraphCanvas import GraphCanvas
from pyqtgraph.Qt import QtCore, QtGui, QtWidgets
import network_dep as nwt

DEBUG = False

"""    
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.
"""    
class GraphWidget(QtGui.QWidget):
    def __init__(self):
        super(GraphWidget, self).__init__()
        box = QtGui.QVBoxLayout(self)
        self.resize(500,500)
        self.setLayout(box)
        self.canvas = GraphCanvas()
        #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)

        #self.show()

    """
    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):
        if event.button == 2:
            if self.canvas.view[0][0] >= 0.0010:
                c_id = self.canvas.get_clicked_id(event)
            else:
                c_id = self.canvas.get_clicked_id(event, clusters=True)
            #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')
                action = menu.exec_(event.native.globalPos())
                if DEBUG:
                    print(action)
                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])
                            
                        if DEBUG:
                            print(vertex_props[i])
                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' )
                        self.canvas.color_vertices(self.canvas.G, 'degree')
                #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:
                    if DEBUG:
                        print("Stuff")
                    #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:
                    self.canvas.G = nwt.Network.gen_new_fd_layout(self.canvas.G)
                    self.canvas.gen_vertex_vbo(self.canvas.G)
                    #self.canvas.set_data(self.canvas.G, self.canvas.bbl, self.canvas.bbu)
                    self.canvas.expand_clusters(self.canvas.G, self.canvas.n_c)

                    #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.
    """
    def on_mouse_double_click(self, event):
        #print("in applet")
        n = 0

    """
        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])
            if DEBUG:
                print("got signal", x, y, z)

    """
        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)