
import PySide2, RLPy, os
import PySide2
from PySide2 import *
from PySide2.QtCore import *
from PySide2.QtCore import QResource
from PySide2.QtCore import QFile
from PySide2.QtCore import QIODevice
from PySide2.QtGui import *
from PySide2.QtUiTools import QUiLoader
from PySide2.QtWidgets import *
from PySide2.shiboken2 import wrapInstance
from PySide2.QtCore import Qt

#-- Make a global variable contain Dialog --#
sample_dialog = None
qtui_widget = None

# This class get a value from a slider then
# then updates an LCD display
class LCDWidget(QtWidgets.QWidget):
    def __init__(self, parent=None):
        QtWidgets.QWidget.__init__(self, parent)

        label = QtWidgets.QLabel()
        label.setText("Move Slider to increase LCD.") 
  
        lcd = QtWidgets.QLCDNumber(3)

        slider = QtWidgets.QSlider(QtCore.Qt.Horizontal)
        slider.setRange(0, 199)
        slider.setValue(0)

        self.connect(slider, QtCore.SIGNAL("valueChanged(int)"), lcd, QtCore.SLOT("display(int)"))

        layout = QtWidgets.QHBoxLayout()
        layout.addWidget(lcd)
        layout.addWidget(slider)
        layout.addWidget(label)     
        self.setLayout(layout)

# This class get a colour from the dialog
# then displays it on screen
class GetColorWidget(QtWidgets.QWidget):
    def __init__(self, parent=None):
        QtWidgets.QWidget.__init__(self, parent)
        
        col = QColor(0, 0, 0) 

        self.btn = QPushButton('Select Colour', self)

        self.btn.clicked.connect(self.showDialog)

        self.frm = QtWidgets.QFrame(self)
        self.frm.setStyleSheet("QWidget { background-color: %s }" % col.name()) 
        
        layout = QtWidgets.QHBoxLayout()
        layout.addWidget(self.btn)
        layout.addWidget(self.frm)
        self.setLayout(layout)

    def showDialog(self):
      
        col = QColorDialog.getColor()

        if col.isValid():
            self.frm.setStyleSheet("QWidget { background-color: %s }" % col.name())

class LoadImageWidget(QtWidgets.QWidget):
    def __init__(self, parent=None):
        QtWidgets.QWidget.__init__(self, parent)
       
        #Get path for this running py script
        file_path=os.path.dirname(os.path.realpath(__file__) )        
        image_path = file_path + "/images/" + "image1.jpg" #path to your image file
        image_profile = QtGui.QImage(image_path) #QImage object
        image_profile = image_profile.scaled(220,220, aspectRatioMode=QtCore.Qt.KeepAspectRatio, transformMode=QtCore.Qt.SmoothTransformation) # To scale image for example and keep its Aspect Ration    

        
        self.label = QtWidgets.QLabel()
        self.label.setText(" ")
        self.label.setFrameStyle(QFrame.Panel | QFrame.Sunken)
        self.label.resize(220, 220)
        self.label.setPixmap(QtGui.QPixmap.fromImage(image_profile))    

        self.slider = QtWidgets.QSlider(QtCore.Qt.Horizontal)
        self.slider.setRange(0, 11)
        self.slider.setValue(0)

        self.slider.valueChanged.connect(self.run_slider)

        layout = QtWidgets.QHBoxLayout()         
        layout.addWidget(self.slider)
        layout.addWidget(self.label)  
        self.setLayout(layout)
        
    def run_slider(self, value):
        image_file=["image1.jpg", "image2.jpg", "image3.jpg", "image4.jpg", "image5.jpg", "image6.jpg", "image7.jpg", "image8.jpg", "image10.jpg", "image9.jpg", "image11.jpg", "image12.jpg"]
        
        # Get path for this running py script
        file_path=os.path.dirname(os.path.realpath(__file__) )        
        image_path = file_path + "/images/" + image_file[value] #path to your image file
        image_profile = QtGui.QImage(image_path) #QImage object
        image_profile = image_profile.scaled(220,220, aspectRatioMode=QtCore.Qt.KeepAspectRatio, transformMode=QtCore.Qt.SmoothTransformation) # To scale image for example and keep its Aspect Ration    
        self.label.setPixmap(QtGui.QPixmap.fromImage(image_profile)) 
        
def run_script():
    global sample_dialog

    layer_manager_lcd_widget = LCDWidget()
    layer_manager_color_widget = GetColorWidget()
    layer_manager_image_widget = LoadImageWidget()
    
    #-- Make the RL Dialog --#
    sample_dialog = RLPy.RUi.CreateRDialog()
    sample_dialog.SetWindowTitle("Demo Dialog")

    #-- Wrap to PySide Dialog --#
    pyside_dialog = wrapInstance(int(sample_dialog.GetWindow()), QtWidgets.QDialog)
    sample_layout = pyside_dialog.layout()

    #-- Load Ui file --#
    ui_file = QtCore.QFile(os.path.dirname(__file__) + "/demo.ui")
    ui_file.open(QtCore.QFile.ReadOnly)
    qtui_widget = QtUiTools.QUiLoader().load(ui_file)
    ui_file.close()

    # Get Controls from demo.ui 
    ui_default_button = qtui_widget.findChild(PySide2.QtWidgets.QPushButton, "pushButton")
    ui_slider1_slider = qtui_widget.findChild(PySide2.QtWidgets.QSlider, "slider1")
    ui_slider2_slider = qtui_widget.findChild(PySide2.QtWidgets.QSlider, "slider2")
    ui_bar1_scrollbar = qtui_widget.findChild(PySide2.QtWidgets.QScrollBar, "scrollbar1")
    ui_bar2_scrollbar = qtui_widget.findChild(PySide2.QtWidgets.QScrollBar, "scrollbar2")
    ui_dial = qtui_widget.findChild(PySide2.QtWidgets.QDial, "dial")
    ui_checkbox = qtui_widget.findChild(PySide2.QtWidgets.QCheckBox, "checkBox")
    ui_editline_edit = qtui_widget.findChild(PySide2.QtWidgets.QLineEdit, "lineEdit")
    ui_spin_box = qtui_widget.findChild(PySide2.QtWidgets.QSpinBox, "spinBox")
    ui_double_spin_box = qtui_widget.findChild(PySide2.QtWidgets.QDoubleSpinBox, "doubleSpinBox")
    ui_radio_button = qtui_widget.findChild(PySide2.QtWidgets.QRadioButton, "radioButton")
    ui_tree_view = qtui_widget.findChild(PySide2.QtWidgets.QTreeWidget, "treeWidget")
    ui_combo_box = qtui_widget.findChild(PySide2.QtWidgets.QComboBox, "comboBox")
    ui_graphics_view = qtui_widget.findChild(PySide2.QtWidgets.QGraphicsView, "graphicsView")

    # Lets go and get scene objects and put into TreeView
    get_scene_objects(ui_tree_view)

    # Check Widgets then go and do something
    ui_default_button.clicked.connect(run_default_button)
    ui_slider1_slider.valueChanged[int].connect(run_slider1)
    ui_slider2_slider.valueChanged.connect(run_slider2)
    ui_bar1_scrollbar.valueChanged.connect(run_bar1)
    ui_bar2_scrollbar.valueChanged.connect(run_bar2)
    ui_dial.valueChanged.connect(run_dial)
    ui_checkbox.toggled.connect(run_checkbox)
    ui_radio_button.toggled.connect(run_radio_button)
    ui_spin_box.valueChanged.connect(run_spin_box)
    ui_double_spin_box.valueChanged.connect(run_double_spin_box)
    ui_editline_edit.textChanged.connect(run_line_edit)
    ui_tree_view.itemClicked.connect(run_tree_view)
    ui_combo_box.currentTextChanged.connect(run_combo_box)
    
    #-- Assign the Ui file to PySide Dialog --#
    sample_layout.addWidget(qtui_widget)
    
    #-- Assign the classes to PySide Dialog --#
    sample_layout.addWidget(layer_manager_image_widget)    
    sample_layout.addWidget(layer_manager_color_widget) 
    sample_layout.addWidget(layer_manager_lcd_widget) 

    
    sample_dialog.Show()

def get_scene_objects(ui_treeWidget):
    #-----Get all avatar / prop / camera / light-----
    avatar_objects = RLPy.RScene.FindObjects( RLPy.EObjectType_Avatar )
    prop_objects = RLPy.RScene.FindObjects( RLPy.EObjectType_Prop )
    light_objects = RLPy.RScene.FindObjects( RLPy.EObjectType_Light )
    camera_objects = RLPy.RScene.FindObjects( RLPy.EObjectType_Camera )

    ui_treeWidget.setColumnCount(1)
    ui_treeWidget.setHeaderLabels(['Scene Objects'])

    #Avatar level item and its kids
    item0 = QTreeWidgetItem(ui_treeWidget, ['Avatar'])
    #-----Create item for all scene objects-----
    for item in avatar_objects:
        _name = item.GetName()
        QTreeWidgetItem(item0, [_name])
    #Prop level item and its kids
    item0 = QTreeWidgetItem(ui_treeWidget, ['Prop'])
    #-----Create item for all scene objects-----
    for item in prop_objects:
        _name = item.GetName()
        QTreeWidgetItem(item0, [_name])
    #Light level item and its kids
    item0 = QTreeWidgetItem(ui_treeWidget, ['Light'])
    #-----Create item for all scene objects-----
    for item in light_objects:
        _name = item.GetName()
        QTreeWidgetItem(item0, [_name])
    #Camera level item and its kids
    item0 = QTreeWidgetItem(ui_treeWidget, ['camera'])
    #-----Create item for all scene objects-----
    for item in camera_objects:
        _name = item.GetName()
        QTreeWidgetItem(item0, [_name])

# Lets run some routines
def run_tree_view(it, col):
    # print(it, col, it.text(col))
    print(it.text(col))
def run_combo_box(value):
    print(value)
def run_default_button():
	print("Default Button")
def run_slider1(value):
    print("Slider 1 Value {}".format(value))
def run_slider2(value):
	print("Slider 2 Value {}".format(value))
def run_bar1(value):
	print("Bar 1 Value {}".format(value))
def run_bar2(value):
	print("Bar 2 Value {}".format(value))
def run_dial(value):
	print("Dial Value {}".format(value))
def run_radio_button(value):
    if value:
        print("Im a True Radio Button")
    else:
        print("Im a False Radio Button")
def run_checkbox(value):
    if value:
        print("Im a True Check Box")
    else:
        print("Im a False Check Box")
def run_spin_box(value):
    print("Spin Box Value {}".format(value))
def run_double_spin_box(value):
    print("Double Spin Box Value {}".format(value))
def run_line_edit(value):
    if value:
        print("Yes Line Edit {}".format(value))
