#!/usr/bin/python
#dv3d_data_AddSurfaceToWindow.py
#Adds a generated surface actor to the main window with the required structure and attributes
#DV3D refactor 2
#AG 31/07/2013

import vtk

from dv3d_dialogs_surface_properties_panel import *
from dv3d_data_updateAllViews import *
from dv3d_data_PolydataClipper import *
from dv3d_gui_updateCurrentInteractions import *

def dv3d_data_AddSurfaceToWindow(the_parent_window, theVTKSurface):
    #once vtk surface data has been loaded
    tpw = the_parent_window #for brevity

    # .. assign surface to the Surfaces list of the main app
    tpw.Surfaces.append(theVTKSurface)

    # also extract the source so we can use label the tree item
    myDataLabel = theVTKSurface.dataLabel
 
    ### ------------------------------------------------ 
    # check if this is the first surface - if so generate a surface property dialog
    # create or update the property panel so the user can adjust the data
    if tpw.SurfaceLoaded == 0: #i.e. this is the first surface oaded    
        tpw.SurfaceLoaded = 1
        tpw.SurfacePropertyPanel = SurfacePropertyPanel('Surface', tpw) #create a panel passing the parent window into its scope

        #big breakthrough here - use window flag as Qt.Window rather than Qt.Dialog to allow minimise button
        # TODO - check cross-platform stability of the this call
        tpw.SurfacePropertyPanel.setWindowFlags(Qt.Window|Qt.WindowMinimizeButtonHint)

        tpw.SurfacePropertyPanel.show()
        tpw.SurfacePropertyPanel.raise_()

        #first load so enable the surfaces item in the list tree
        tpw.ItemList_QTreeWidget.Surfaces.setDisabled(0)

    else: 
        pass #it's already created .. just update the info


    ### ------------------------------------------------ 

    # add the object to the item list
    tpw.currentlyLoading = 1 #flag to stop unwanted auto-behaviout

    child_surface = QTreeWidgetItem([myDataLabel])
    child_surface.setCheckState(0,Qt.Checked)
    child_surface.setFlags(child_surface.flags()|Qt.ItemIsEditable) #allow renaming
    
    #also append a pointer to the data this item represents so we can 
    # manipluate the data if we click the list item
    setattr(child_surface, 'dv3dDataType', 'surface')
    setattr(child_surface, 'surfaceDataIndex', len(tpw.Surfaces)-1)

    #add it to the main list
    tpw.ItemList_QTreeWidget.Surfaces.addChild(child_surface) 

    #tell the object list that data has been loaded and is visible
    tpw.ItemList_QTreeWidget.Surfaces.setCheckState(0,Qt.Checked)

    #highlight the correct item in the tree (set focus on current item)
    tpw.ItemList_QTreeWidget.setItemExpanded(tpw.ItemList_QTreeWidget.Surfaces, 1) #make sure its visible
    tpw.ItemList_QTreeWidget.clearSelection() #deselect all currently selected
    tpw.ItemList_QTreeWidget.setCurrentItem(child_surface) #set current and then ...
    child_surface.setSelected(1) #and highlight it
    tpw.ItemList_QTreeWidget.update() #update
    tpw.ItemList_QTreeWidget.repaint() #redraw

    #surfaces have an additional complexity - they have an associated Clipper
    # let's set that up here ..
    clipActor, clipPlanes, clipper = AddPolyDataClipper(tpw, theVTKSurface)

    # replace the recently added surface with this new one
    tpw.Surfaces[-1] = clipActor   

    #let's keep track of some values for update / reference later
    # NB some already set in dv3d_data_ColorLookupTable
    setattr(tpw.Surfaces[-1], 'dataLabel', myDataLabel)
    setattr(tpw.Surfaces[-1], 'bounds', tpw.Surfaces[-1].GetBounds())
    setattr(tpw.Surfaces[-1], 'currSurfaceMode', 'Solid')
    setattr(tpw.Surfaces[-1], 'currOpacity', 1.0)
    setattr(tpw.Surfaces[-1], 'currRGB', (1,0,0))
    setattr(tpw.Surfaces[-1], 'currQColor', QColor(255,0,0))
    setattr(tpw.Surfaces[-1], 'orig_Surface', theVTKSurface)
    setattr(tpw.Surfaces[-1], 'clipper', clipper)
    setattr(tpw.Surfaces[-1], 'clipPlanes', clipPlanes)
    setattr(tpw.Surfaces[-1], 'clipBoxWidget', clipActor.ClipperBoxWidget)
    setattr(tpw.Surfaces[-1], 'clipperInsideOut', 'ClipOutside')


    #we may want to reset the mapper in the future, so keep track of 
    #  the original calculated clipPlane parameters
    clipPlanes.Modified() #make sure they're updated first
    setattr(tpw.Surfaces[-1], 'orig_clipPlanePoints', clipPlanes.GetPoints())
    setattr(tpw.Surfaces[-1], 'orig_clipPlaneNormals', clipPlanes.GetNormals())

    tpw.currentlyLoading = 0 #flag to stop unwanted auto-behaviout


    #Add the actor to the Window
    tpw.ren3d.AddActor(clipActor)

    #enable the clipping widget so we can calculate the default layout
    # .. allows us to revert back at the click of a button later
    clipActor.ClipperBoxWidget.On()
    clipActor.ClipperBoxWidget.Modified()
    clipActor.ClipperBoxWidget.PlaceWidget()
    clipActor.ClipperBoxWidget.Modified()

    clipActor.VisibilityOn()
    clipActor.ClipperBoxWidget.Off()
    clipActor.SetOrigin(clipActor.GetCenter())

    dv3d_data_updateAllViews(tpw)

    #some additional environment code
    dv3d_gui_updateCurrentInteractions(tpw, 'surfaces')    
    # --


