예제 #1
0
class SupportUI(QWidget):
    """
    Top-level help/support view.

    :param MainFrame mainframe: the mainframe of the application
    :param QWidget op: the parent widget
    """
    def __init__(self, mainframe, op):
        super().__init__()
        self.mainframe = mainframe
        self.op = op
        self.initUI()

    def initUI(self):
        """
        Initializes the GUI.
        Does a lot of stuff.
        """
        self.mainframe.setWindowTitle("Contact/Support")

        self.grid = QGridLayout()
        self.setLayout(self.grid)

        importB = QPushButton(self, text="Import")
        importB.clicked.connect(self.importF)
        self.grid.addWidget(importB, 0, 0)

        exportB = QPushButton(self, text="Export")
        exportB.clicked.connect(self.export)
        self.grid.addWidget(exportB, 0, 1)

        self.contact = QPushButton(self, text="Contact")
        self.contact.clicked.connect(self.contactF)
        self.grid.addWidget(self.contact, 0, 2)

        self.text = QLabel(
            self,
            text=
            "Hello and thank you for using the Persona X Story Creator.\n\nTo "
            "import data from other versions of the Story Creator, click "
            "\"Import\".\n\nTo export your data to a seperate directory, (to "
            "prepare for a version change), click \"Export\".\n\nTo send your data"
            " to the dev team or to report a bug with the program, click "
            "\"Contact\"")
        self.text.setAlignment(Qt.AlignHCenter)
        self.grid.addWidget(self.text, 1, 0, 1, 3)

        self.back = QPushButton(self, text="Back")
        self.back.clicked.connect(self.backF)
        self.grid.addWidget(self.back, 2, 1)

    def importF(self):
        """
        Import a file or set of files from disk into Story-Creator controlled directories.

        :raises AssertionError: with an object attempting to being loaded if the object cannot be loaded as a
                 Social Link, Character or Persona.
        """
        fileBrowser = QFileDialog()
        fileBrowser.setFileMode(QFileDialog.Directory)
        fileBrowser.setViewMode(QFileDialog.Detail)
        fileBrowser.setOption(QFileDialog.ShowDirsOnly, True)
        if fileBrowser.exec_():
            paths = fileBrowser.selectedFiles()
        else:
            print("Cancelled")
            return
        print("Copying data from " + str(paths[0]))
        files = os.listdir(str(paths[0]))
        print(files)
        for file in files:
            if file.endswith(".json"):
                print("Copying valid file " + file)
                if "_link" in file:
                    if self.checkOverwrite(file):
                        copy(os.path.join(str(paths[0]), file),
                             json_reader.buildPath("data"))
                else:
                    try:  # Ugly AF
                        # TODO omgf this is more than ugly AF
                        characterL = json_reader.readOne(
                            file[:len(file) - 5], 'chars')
                        assert "name" in characterL and "important" in characterL
                        if self.checkOverwrite(file, 'chars'):
                            copy(os.path.join(str(paths[0]), file),
                                 json_reader.buildPath("data/chars"))
                    except AssertionError:
                        print("Not a Character")
                        try:
                            personaL = json_reader.readOne(
                                file[:len(file) - 5], 'pers')
                            assert "name" in personaL and "arcana" in personaL
                            if self.checkOverwrite(file, 'pers'):
                                copy(os.path.join(str(paths[0]), file),
                                     json_reader.buildPath("data/pers"))
                        except AssertionError:
                            print("Not a Persona")
                            raise AssertionError(personaL)
        print("Successfully copied files")
        popup("Files imported successfully!", "Information")

    def checkOverwrite(self, filepath, ctype=''):
        """
        Confirm with user if file should be overwritten.

        :param str filepath: path to file that could be overwritten
        :param str ctype: chars or pers if the file represents either type
        """
        if ctype:
            ctype = ctype + "/"
        if os.path.exists(os.path.join(json_reader.buildPath("data"),
                                       filepath)):
            if popup(
                    "File " + filepath[:len(filepath) - 5] +
                    " already exists. Overwrite?", "Warning"):
                os.remove(
                    json_reader.buildPath("data/%s%s" % (ctype, filepath)))

    def export(self):
        """
        Export the story-creator data files to a user-selected locaion.
        """
        fileBrowser = QFileDialog()
        fileBrowser.setFileMode(QFileDialog.Directory)
        fileBrowser.setViewMode(QFileDialog.Detail)
        fileBrowser.setOption(QFileDialog.ShowDirsOnly, True)
        if fileBrowser.exec_():
            paths = fileBrowser.selectedFiles()
        else:
            print("Cancelled")
            return
        print("Copying data to " + str(paths[0]) + "/exportdata")
        try:
            copytree(json_reader.buildPath("data"),
                     str(paths[0]) + "/exportdata")
        except OSError as e:
            print(e)
            popup(
                "Error in copying files. There is a file in the selected directory that has the same name "
                "as a Story Creator file.\n\nFiles are copied to " +
                str(paths[0]) + "/exportdata" + ". Please "
                "ensure this directory does not already exist.", "Critical")
            return
        print("Successfully copied files")
        popup("Files exported successfully!", "Information")

    def contactF(self):
        """
        Bring up the email sending frame instead of the fluff text currently being displayed.
        """
        self.contact.clicked.disconnect()
        self.text.close()
        EmailFrame(self)

    def backF(self):
        """
        The user is done here. Return the view back to whatever brought us to the support page (the OP).
        """
        self.mainframe.changeState(self.op)