Exemplo n.º 1
0
class DialogLoginKey(QDialog):
    def __init__(self, parent, windowTitle, icon=None):
        def initGui():
            def connect():
                buttonLogin.clicked.connect(self.onLogin)
                self.textKey.textEdited.connect(self.onTextEdited)

            #
            self.setWindowTitle(windowTitle)
            if not icon is None:
                self.setWindowIcon(icon)
            labelKey = QLabel("Key: ", self)
            self.labelError = QLabel(self)
            self.labelError.hide()
            self.textKey = QLineEdit(self)
            self.textKey.setEchoMode(QLineEdit.Password)
            buttonLogin = QPushButton("Login", self)
            connect()
            layout = QVBoxLayout(self)
            layout.addWidget(labelKey)
            layout.addWidget(self.textKey)
            layout.addWidget(buttonLogin)
            layout.addWidget(self.labelError)
            #
            self.resize(4 * len(windowTitle) + 200, 30)

        #
        super(DialogLoginKey, self).__init__(parent)
        self.apiPL = API_PlanetLabs()
        self.responsePL = None
        initGui()

    @pyqtSlot(bool)
    def onLogin(self, checked):
        def setFinishedPL(response):
            self.responsePL = response
            loop.quit()

        def setKeyResponse():
            if self.responsePL['isOk']:
                self.accept()
            else:
                self.labelError.setTextFormat(Qt.RichText)
                msg = "<font color=\"red\"><b><i>Invalid key! %s</i></b></font>" % self.responsePL[
                    'message']
                self.labelError.setText(msg)
                self.labelError.show()

        key = self.textKey.text().encode('ascii', 'ignore')
        self.responsePL = None
        loop = QEventLoop()
        self.apiPL.setKey(key, setFinishedPL)
        loop.exec_()
        setKeyResponse()

    @pyqtSlot(str)
    def onTextEdited(self, text):
        if self.labelError.isHidden():
            return
        self.labelError.hide()