# -*- coding: utf-8 -*-
"""
Simple example showing the creation of a Node that has buttons.
"""

# append the path to allow usage of nodeworks outside of python's sitepackages
import sys
sys.path.append('../')

# import OrderDict to preserve terminal order
from collections import OrderedDict

# import qt, qtpy is a PySide/PyQt4/PyQt5 interface that uses Qt5 syntax
# > pip install qtpy
from qtpy import QtWidgets

# Import nodeworks
from nodeworks import NodeWidget, Node, tools


# create a custom node with buttons.
class ButtonNode(Node):
    name = 'button node'

    def __init__(self):
        self.terminalOpts = OrderedDict([
            ('push', {'widget': 'pushbutton',
                      'in': False,
                      'out': False,
                      'showlabel': False,
                      'dtype': bool,
                      }),
            ('push checkable', {'widget': 'pushbutton',
                                'in': True,
                                'out': False,
                                'showlabel': False,
                                'dtype': bool,
                                'checkable': True
                                }),
            ('tool', {'widget': 'toolbutton',
                      'in': False,
                      'out': False,
                      'showlabel': False,
                      'dtype': bool,
                      }),
            ('tool checkable', {'widget': 'toolbutton',
                                'in': True,
                                'out': False,
                                'showlabel': False,
                                'dtype': bool,
                                'checkable': True,
                                }),
            ])

        Node.__init__(self)

        # Connect the "push" terminal value change event to "pushed"
        self.terminals['push'].valueChanged.connect(self.pushed)

        # Connect the "tool" terminal value change event to "tooled"
        self.terminals['tool'].valueChanged.connect(self.tooled)

        # Add an icon to the tool buttons
        # Note:
        #    1) tools.get_icon is a helper function that will turn .png, .jpg
        #       files into icons
        #    2) there are some icons in nodeworks/images that can be used
        self.terminals['tool'].widget.setIcon(tools.get_icon('addbox'))
        self.terminals['tool checkable'].widget.setIcon(tools.get_icon('play'))

    def pushed(self):
        print('The push button was pushed')

    def tooled(self):
        print('The tool button was tooled')

    def process(self):
        pass


def main():
    # Create a Qt App
    app = QtWidgets.QApplication(sys.argv)

    # initialize the node widget
    nodechart = NodeWidget(showtoolbar=True)

    # Build default node library
    nodechart.nodeLibrary.buildDefaultLibrary()

    # Add the dynamic node
    nodechart.nodeLibrary.addNode(ButtonNode, [])

    # create the main window
    mainwindow = QtWidgets.QMainWindow()
    mainwindow.setCentralWidget(nodechart)
    mainwindow.setGeometry(200, 200, 800, 500)
    mainwindow.setWindowTitle('Nodeworks')
    mainwindow.show()

    # Start Qt Event Loop
    app.exec_()
    app.deleteLater()
    sys.exit()

if __name__ == '__main__':
    main()
