Ejemplo n.º 1
0
class Form(QDialog):

    cfg = configparser.ConfigParser()

    def __init__(self, parent=None):
        super(Form, self).__init__(parent)

        client_ID, client_secret, user_agent = IniConfig.get_ini(self.cfg)
        self.clientID, self.clientSecret, self.userAgent = client_ID, client_secret, user_agent

        self.setWindowTitle("Ini Configurator")

        self.t_clientID = QLabel("Client ID")
        self.t_clientSecret = QLabel("Client Secret")
        self.t_userAgent = QLabel("User Agent")

        self.f_clientID = QLineEdit(client_ID)
        self.f_clientSecret = QLineEdit(client_secret)
        self.f_userAgent = QLineEdit(user_agent)

        self.buttonBox = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)

        layout = QFormLayout()
        layout.addRow(self.t_clientID, self.f_clientID)
        layout.addRow(self.t_clientSecret, self.f_clientSecret)
        layout.addRow(self.t_userAgent, self.f_userAgent)
        layout.addWidget(self.buttonBox)

        self.setLayout(layout)

        self.buttonBox.accepted.connect(self.save)
        self.buttonBox.rejected.connect(self.cancel)

    def save(self):
        if self.f_clientID.isModified() or self.f_clientSecret.isModified() or self.f_userAgent.isModified():
            IniConfig.save_ini(self.cfg,
                               self.f_clientID.text(),
                               self.f_clientSecret.text(),
                               self.f_userAgent.text())
        self.accept()

    def cancel(self):
        self.reject()
Ejemplo n.º 2
0
class Socket(QFrame):
    """One socket with label and text entry"""
    def __init__(self, master, letter, connect_plug, charset):
        """
        :param master: Qt parent object
        :param letter: Letter to serve as the label
        :param connect_plug: calls parent to connect with the letter typed in
                             the entry box
        :param charset: {str} Allowed letters
        """
        super().__init__(master)

        # QT WINDOW SETTINGS ===================================================

        layout = QVBoxLayout(self)
        self.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)

        # ATTRIBUTES ===========================================================

        self.connect_plug = connect_plug
        self.letter = letter
        self.connected_to = None
        self.marking = None
        self.charset = charset

        # ENTRY ================================================================

        label = QLabel(letter)
        label.setStyleSheet("font-size: 30px; text-align: center;")
        self.entry = QLineEdit()
        self.entry.setMaxLength(1)
        self.entry.textChanged.connect(self.entry_event)
        self.entry.setFixedSize(40, 40)
        self.entry.setAlignment(Qt.AlignCenter)

        # SHOW WIDGETS

        layout.addWidget(label, alignment=Qt.AlignCenter)
        layout.addWidget(self.entry, alignment=Qt.AlignCenter)

    def pair(self):
        """Returns currently wired pair."""
        if self.connected_to:
            return self.letter + self.connected_to
        return None

    def entry_event(self):
        """Responds to a event when something changes in the plug entry"""
        letter = self.entry.text().upper()
        if letter not in self.charset:
            self.set_text("", True)
        elif self.entry.isModified():  # Prevents recursive event calls
            if letter:
                self.connect_plug(self.letter, letter)
            else:
                self.connect_plug(self.letter, None)

    def set_text(self, letter, block_event=False, marking=None, uhr=False):
        """
        Sets text to the plug entrybox and sets white (vacant) or black
        (occupied) background color
        :param letter: Sets text to the newly selected letter
        :param block_event: {bool} Starts blocking Qt signals if True
        :param marking: {str} Uhr marking (like 1a, 3b, ...)
        :param uhr: {bool} Colors sockets differently when True (when Uhr connected)
        """
        stylesheet = (
            "background-color: %s; color: %s; text-align: center; font-size: 30px;"
        )

        if block_event:
            self.entry.blockSignals(True)

        self.setToolTip(None)

        if letter:
            color = ("black", "white")
            self.marking = marking
        else:
            color = ("white", "black")
            self.marking = None

        if uhr:
            if "a" in marking:
                color = ("red", "white")
            else:
                color = ("gray", "white")

            self.setToolTip(str(marking[0]) + marking[1])

        self.entry.setStyleSheet(stylesheet % color)
        self.entry.setText(letter)
        self.connected_to = letter

        if block_event:
            self.entry.blockSignals(False)