def loadData(self, data):
        self.commands = {}
        self.clear()
        self.hintLbl.show()
        commandClasses = getModuleClasses(CommandsGUI)

        # Fill the list with new data
        for commandSave in data:
            # Convert from string to an actual event obj
            self.addCommand(commandClasses[commandSave['type']], parameters=commandSave['parameters'])

        self.refreshIndents()
    def __init__(self, env, interpreter, parameters=None):
        super(LoopCommand, self).__init__(parameters)

        Commands = sys.modules[__name__]

        commandClasses = getModuleClasses(Commands)
        testType = commandClasses[self.parameters["testType"]]
        testParams = self.parameters["testParameters"]

        self.test = testType(env, interpreter, parameters=testParams)


        self.errors = self.test.errors
    def loadData(self, data):
        # Perform cleanup
        self.events = {}
        self.clear()  # clear eventList
        self.hintLbl.show()

        eventClasses = getModuleClasses(EventsGUI)

        # Fill event list with new data
        for eventSave in data:
            # Convert the string 'EventClass' to an actual class, load its command save, and add the event
            self.addEvent(eventClasses[eventSave['type']],
                          commandListSave =  eventSave['commandList'],
                          parameters      =  eventSave['parameters'])

        # Select the first event for viewing
        if self.count() > 0: self.setCurrentRow(0)
    def __loadAllObjects(self):
        # Load all objects into the ObjectManager

        foldersAndItems = os.listdir(self.__directory)

        resourceClasses = getModuleClasses(Resources)


        for folder in foldersAndItems:
            path = self.__directory + "\\" + folder


            if not os.path.isdir(path):
                printf("ObjectManager| ERROR: Could not find directory ", path)
                continue


            # Get the type and name of the object by breaking up the filename into words
            words = folder.split(' ', 1)
            if len(words) < 2:
                printf("ObjectManager| ERROR: File ", folder, " did not have the correct format!")
                continue   # If there isn't a 'TYPE NAME' format
            newType = str(words[0])
            name    = words[1]


            # Check that that type of resource exists

            if newType not in resourceClasses:
                printf("ObjectManager| ERROR: Tried to create a resource that is not in Resources.py!")
                continue

            # Get the type, then instantiate it
            newType = resourceClasses[newType]
            newObj = newType(name, path)


            # # Check that loading is complete and add the object if it was created successfully
            # if newObj is None:
            #     printf("ERROR: Could not find relevant object for folder: ", folder)
            #     continue

            if newObj.loadSuccess: self.__addObject(newObj)

        self.refreshGroups()
    def __loadAllObjects(self):
        # Load all objects into the ObjectManager

        foldersAndItems = os.listdir(self.__directory)

        resourceClasses = getModuleClasses(Resources)


        for folder in foldersAndItems:
            path = self.__directory + "\\" + folder


            if not os.path.isdir(path):
                printf("ObjectManager| ERROR: Could not find directory ", path)
                continue


            # Get the type and name of the object by breaking up the filename into words
            words = folder.split(' ', 1)
            if len(words) < 2:
                printf("ObjectManager| ERROR: File ", folder, " did not have the correct format!")
                continue   # If there isn't a 'TYPE NAME' format
            newType = str(words[0])
            name    = words[1]


            # Check that that type of resource exists

            if newType not in resourceClasses:
                printf("ObjectManager| ERROR: Tried to create a resource that is not in Resources.py!")
                continue

            # Get the type, then instantiate it
            newType = resourceClasses[newType]
            newObj = newType(name, path)

            if newObj.loadSuccess: self.__addObject(newObj)

        # Search through vision objects tags and generate TrackableGroup objects for each of them
        self.refreshGroups()
    def dropEvent(self, event):
        if event.mimeData().hasText():
            # Get the mouse position, offset it by the width of the listWidgets, and get the index of that listWidget
            newPoint = event.pos()
            newPoint.setY(newPoint.y() + self.rectForIndex(self.indexFromItem(self.item(0))).height() / 2)
            dropIndex = self.indexAt(newPoint).row()
            if dropIndex == -1: dropIndex = self.count()  # If dropped at a index past the end of list, drop at end

            # Add the new dragged in widget to the index that was just found
            commandClasses = getModuleClasses(CommandsGUI)
            cType = commandClasses[event.mimeData().text()]
            self.addCommand(cType, index=dropIndex)

            # For easy usability, when you drop a Test command, a StartBlock and EndBlock will drop right after it.
            if cType in CommandsGUI.testTypes or cType == CommandsGUI.LoopCommand or cType == CommandsGUI.ElseCommand:
                self.addCommand(CommandsGUI.StartBlockCommand, index=dropIndex + 1)
                self.addCommand(CommandsGUI.EndBlockCommand, index=dropIndex + 2)

            event.accept()
        else:
            event.setDropAction(QtCore.Qt.MoveAction)
            super(CommandList, self).dropEvent(event)
        self.refreshIndents()
    def keyPressEvent(self, event):
        super(CommandList, self).keyPressEvent(event)  # Let arrow keys move around the list

        # If the script is running, then the widget is locked. Don't allow the user to do anything during this time.
        if self.locked: return


        # Delete selected items
        if event.key() == QtCore.Qt.Key_Delete:
            self.deleteSelected()


        # Copy commands
        if event == QtGui.QKeySequence.Copy:
            copyData = []
            for item in self.selectedItems():
                command = self.commands[self.itemWidget(item)]
                data    = command.getSaveData()
                copyData.append(data)



            # Create a JSON, convert it to a string, then a byteArray
            byteData = bytearray(str(json.dumps(copyData)), 'utf-8')
            byteData = QtCore.QByteArray(byteData)

            # Load the byteArray into QMime Data, under the name "CommandData"
            md = QtCore.QMimeData()
            md.setData("CommandData", byteData)

            # Load the mimeData into the clipboard, for later pasting
            clipboard = QtWidgets.QApplication.clipboard()
            clipboard.setMimeData(md) # json.dumps(copyData))
            # md.setText(self.dragData)


        # Paste commands
        if event == QtGui.QKeySequence.Paste:
            # Pull the data from the clipboard, it will be a QByteArray
            clipboard = QtWidgets.QApplication.clipboard()
            pasteData = clipboard.mimeData().data("CommandData")

            # Make sure there was something in the clipboard
            if len(pasteData) == 0: return

            # Convert the QByteArray to a normal utf-8 string, representing a JSON
            pasteData = bytearray(pasteData)
            pasteData = pasteData.decode('utf-8')

            # Convert the string into a normal python dictionary
            commandData = json.loads(pasteData)


            # Figure out the index to paste the commands
            selectedItems = self.selectedItems()
            if len(selectedItems):
                pasteIndex = self.row(selectedItems[-1]) + 1
            else:
                pasteIndex = self.count()

            commandClasses = getModuleClasses(CommandsGUI)

            for commandSave in reversed(commandData):
                self.addCommand(commandClasses[commandSave['type']],
                                parameters=commandSave['parameters'],
                                index=pasteIndex )

            # Since indents don't get refreshed in self.addCommand when parameters are specified, do it here
            self.refreshIndents()

        # Select All
        if event == QtGui.QKeySequence.SelectAll:
            self.selectAll()