コード例 #1
0
class ConnectStatus(GridLayout):

    key = 'ConnectStatus'
    _allowLocalMode = True
    _updatting = False
    _mode = None
    _server = None
    labels = DAMGLIST()

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

        self.parent = parent
        self._server = self.getServer()
        self.serverStatus = Label({
            'wmax': 20,
            'sst': 'Server Connection Status',
        })
        self.internetStatus = Label({
            'wmax': 20,
            'sst': 'Internet Connection Status',
        })
        self.modeStatus = Label({
            'txt': self._mode,
            'sst': 'Operating Mode Status'
        })
        self.updateTimer = DAMGTIMER()
        self.updateTimer.setParent(self)
        self.updateTimer.timeout.connect(self.update_icon)

        self.server_status()
        self.internet_status()
        self.mode_status()
        self.addWidget(self.serverStatus, 0, 0, 1, 1)
        self.addWidget(self.internetStatus, 0, 1, 1, 1)
        self.addWidget(self.modeStatus, 0, 2, 1, 1)

        self.labels.appendList(
            [self.serverStatus, self.internetStatus, self.modeStatus])

        if not self._updatting:
            self.updateTimer.stop()
        else:
            self.updateTimer.start(1000)

    def server_status(self):

        try:
            r = requests.get(__localServer__)
        except Exception:
            if not self._allowLocalMode:
                MessageBox(None, 'Connection Failed', 'critical',
                           SERVER_CONNECT_FAIL, 'close')
                sys.exit()
            else:
                self.parent.signals.emit('sysNotify', 'Offline',
                                         'Can not connect to Server', 'crit',
                                         500)
                self.serverIcon = get_app_icon(16, 'Disconnected')
        else:
            if r.status_code == 200:
                self.serverIcon = get_app_icon(16, 'Connected')
            else:
                self.serverIcon = get_app_icon(16, 'Disconnected')

        self.serverStatus.setPixmap(QPixmap(self.serverIcon))
        self.serverStatus.update()

    def internet_status(self):

        try:
            r = requests.get(__google__)
        except requests.ConnectionError:
            self.parent.signals.emit('sysNotify', 'Offline',
                                     'Can not connect to Internet', 'crit',
                                     500)
            self.internetIcon = get_app_icon(16, 'Disconnected')
            self.internetStatus.setToolTip('Disconnected')
        else:
            self.parent.signals.emit('sysNotify', 'Online',
                                     'Internet connected', 'info', 500)
            self.internetIcon = get_app_icon(16, 'Connected')
            self.internetStatus.setStatusTip('Connected')

        self.internetStatus.setPixmap(QPixmap(self.internetIcon))
        self.internetStatus.update()

    def mode_status(self):
        self.getServer()
        self.modeStatus.setText(self._mode)
        self.modeStatus.setToolTip(self._mode)
        self.modeStatus.update()

    def update_icon(self):
        # print('update Icon')
        self.internet_status()
        self.server_status()
        self.mode_status()

    def setMode(self, mode):
        self._mode = mode

    def getServer(self):
        try:
            r = requests.get(__globalServer__)
        except Exception:
            try:
                r = requests.get(__localServer__)
            except Exception:
                if not self._allowLocalMode:
                    MessageBox(None, 'Connection Failed', 'critical',
                               SERVER_CONNECT_FAIL, 'close')
                    sys.exit()
                else:
                    self.setMode('Offline')
            else:
                if r.status_code == 200:
                    self._server = __localServer__
                    self.setMode('Local')
                else:
                    self.setMode('Offline')
        else:
            if r.status_code == 200:
                self._server = __globalServer__
                self.setMode('GLobal')
            else:
                self.setMode('Offline')

        return self._server

    @property
    def updatting(self):
        return self._updatting

    @property
    def allowOfflineMode(self):
        return self._allowLocalMode

    @property
    def mode(self):
        return self._mode

    @property
    def server(self):
        return self._server

    @server.setter
    def server(self, val):
        self._server = val

    @mode.setter
    def mode(self, val):
        self._mode = val

    @updatting.setter
    def updatting(self, val):
        self._updatting = val

    @allowOfflineMode.setter
    def allowOfflineMode(self, val):
        self._allowLocalMode = val


# -------------------------------------------------------------------------------------------------------------
# Created by panda on 25/05/2018
コード例 #2
0
class UserSetting(Widget):

    key = 'UserSetting'
    query = QuerryDB()

    def __init__(self, parent=None):

        super(UserSetting, self).__init__(parent)

        # self.setWindowIcon(AppIcon(32, "UserSetting"))
        self.layout = GridLayout()
        self.buildUI()
        self.setLayout(self.layout)

    def buildUI(self):

        password_section = self.change_pass_section()
        avatar_section = self.change_avatar_section()
        profile_section = self.change_profile_section()
        location_setion = self.change_location_section()

        self.layout.addWidget(avatar_section, 0, 0, 1, 1)
        self.layout.addWidget(password_section, 0, 1 , 1, 1)
        self.layout.addWidget(profile_section, 1, 0, 1, 1)
        self.layout.addWidget(location_setion, 1, 1, 1, 1)

    def change_avatar_section(self):

        try:
            self.username, token, cookie, remember = self.query.query_table('curUser')
        except (ValueError, IndexError):
            self.username = '******'

        avatar_groupBox, avatar_layout = GroupGrid('Change Avatar')

        self.avatar = Label()
        self.avatar.setPixmap(QPixmap.fromImage(QImage(get_avatar_image(self.username))))
        self.avatar.setScaledContents(True)
        self.avatar.setFixedSize(100, 100)

        change_avatar_btn = Button({'txt':'Change Avatar', 'cl': self.update_avatar})
        avatar_layout.addWidget(self.avatar)
        avatar_layout.addWidget(change_avatar_btn)

        return avatar_groupBox

    def change_pass_section(self):

        password_groupBox, password_layout = GroupGrid('Change Password')

        self.old_pass                   = LineEdit({'echo': 'password'})
        self.new_pass                   = LineEdit({'echo': 'password'})
        self.confirm_pass               = LineEdit({'echo': 'password'})
        change_pass_btn                 = Button({'txt': 'Change Password', 'cl': self.update_password})

        password_layout.addWidget(Label({'txt': 'Old Password'}), 0, 0, 1, 2)
        password_layout.addWidget(Label({'txt': 'New Password'}), 1, 0, 1, 2)
        password_layout.addWidget(Label({'txt': 'Confirm Password'}), 2, 0, 1, 2)
        password_layout.addWidget(self.old_pass, 0, 2, 1, 4)
        password_layout.addWidget(self.new_pass, 1, 2, 1, 4)
        password_layout.addWidget(self.confirm_pass, 2, 2, 1, 4)
        password_layout.addWidget(change_pass_btn, 3, 0, 1, 6)

        return password_groupBox

    def change_profile_section(self):

        profile_groupBox, profile_layout = GroupGrid("Change Profile")

        profile_layout.addWidget(Label({'txt': 'First Name'}), 0, 0, 1, 2)
        profile_layout.addWidget(Label({'txt': 'Last Name'}), 1, 0, 1, 2)
        profile_layout.addWidget(Label({'txt': 'Your Title'}), 2, 0, 1, 2)
        profile_layout.addWidget(Label({'txt': 'Email'}), 3, 0, 1, 2)
        profile_layout.addWidget(Label({'txt': 'Phone Number'}), 4, 0, 1, 2)

        self.firstnameField             = LineEdit()
        self.lastnameField              = LineEdit()
        self.titleField                 = LineEdit()
        self.emailField                 = LineEdit()
        self.phoneField                 = LineEdit()

        change_profile_btn = Button({'txt': "Update Profile", 'cl': self.update_profile})

        profile_layout.addWidget(self.firstnameField, 0, 2, 1, 4)
        profile_layout.addWidget(self.lastnameField, 1, 2, 1, 4)
        profile_layout.addWidget(self.titleField, 2, 2, 1, 4)
        profile_layout.addWidget(self.emailField, 3, 2, 1, 4)
        profile_layout.addWidget(self.phoneField, 4, 2, 1, 4)
        profile_layout.addWidget(change_profile_btn, 5, 0, 1, 6)

        return profile_groupBox

    def change_location_section(self):

        location_groupBox, location_layout = GroupGrid("Change Location")

        location_layout.addWidget(Label({'txt': 'Address Line 1'}), 0, 0, 1, 2)
        location_layout.addWidget(Label({'txt': 'Address Line 2'}), 1, 0, 1, 2)
        location_layout.addWidget(Label({'txt': 'Postal'}), 2, 0, 1, 2)
        location_layout.addWidget(Label({'txt': 'City'}), 3, 0, 1, 2)
        location_layout.addWidget(Label({'txt': 'Country'}), 4, 0, 1, 2)

        self.address1Field              = LineEdit()
        self.address2Field              = LineEdit()
        self.postalField                = LineEdit()
        self.cityField                  = LineEdit()
        self.countryField               = LineEdit()

        change_location_btn = Button({'txt': "Update Location", 'cl': self.update_location})

        location_layout.addWidget(self.address1Field, 0, 2, 1, 4)
        location_layout.addWidget(self.address2Field, 1, 2, 1, 4)
        location_layout.addWidget(self.postalField, 2, 2, 1, 4)
        location_layout.addWidget(self.cityField, 3, 2, 1, 4)
        location_layout.addWidget(self.countryField, 4, 2, 1, 4)
        location_layout.addWidget(change_location_btn, 5, 0, 1, 6)

        return location_groupBox

    def update_password(self):

        old_pass = text_to_hex(self.old_pass.text())
        new_pass = text_to_hex(self.new_pass.text())
        confirm_pass = text_to_hex(self.confirm_pass.text())

        if len(old_pass) == 0 or len(new_pass) == 0 or len(confirm_pass) == 0:
            MessageBox(self, title='Failed', level='critical', message=PW_BLANK, btn='ok')
            return
        elif new_pass is not confirm_pass:
            MessageBox(self, title='Failed', level='critical', message=PW_UNMATCH, btn='ok')
            return
        else:
            # checkPass = func.check_pw_match(self.username, old_pass)
            # if not checkPass:
            #     QMessageBox.critical(self, 'Failed', "Password not match")
            #     return
            # else:
            #     newpass = func.encode(self.newPassword.text())
            #     func.update_password(self.unix, newpass)
            #     QMessageBox.information(self, 'Updated', PW_CHANGED)
            pass

    def update_avatar(self):

        options = QFileDialog.Options()
        options |= QFileDialog.DontUseNativeDialog
        imgsDir = os.path.join(os.getenv(__envKey__), 'avatar')
        fileName, _ = QFileDialog.getOpenFileName(self, "Your Avatar", imgsDir, "All Files (*);;Img Files (*.jpg)",
                                                  options=options)
        if fileName:
            baseFileName = self.username + '.avatar.jpg'
            desPth = os.path.join(imgsDir, baseFileName)

            if desPth == fileName:
                pass
            elif os.path.exists(desPth):
                if os.path.exists(desPth + '.showLayout_old'):
                    os.remove(desPth + '.showLayout_old')

                os.rename(desPth, desPth + '.showLayout_old')
                resize_image(fileName, desPth)
                shutil.copy2(fileName, desPth)
                image = QPixmap.fromImage(QImage(desPth))
                self.avatar.setPixmap(image)
                self.avatar.update()
                self.settings.setValue(self.username, desPth)
                self.updateAvatar.emit(True)

    def update_profile(self):
        pass

    def update_location(self):
        pass
コード例 #3
0
ファイル: SignUp.py プロジェクト: CGFanTuan/damgteam
class SignUp(Widget):

    key = 'SignUp'

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

        self.setWindowIcon(AppIcon(32, "SignUp"))

        self.layout = GridLayout()
        self.buildUI()
        self.setLayout(self.layout)

    def buildUI(self):
        self.avatar_section()
        self.account_section()
        self.profile_section()
        self.location_section()
        self.security_section()
        self.buttons_section()

        self.layout.addWidget(Label({'txt': "ALL FIELD ARE REQUIRED!!!"}), 0,
                              0, 1, 6)
        self.layout.addWidget(self.avaSection, 1, 0, 1, 2)
        self.layout.addWidget(self.accSection, 1, 2, 1, 4)
        self.layout.addWidget(self.prfSection, 2, 0, 1, 6)
        self.layout.addWidget(self.conSection, 3, 0, 1, 6)
        self.layout.addWidget(self.serSection, 4, 0, 1, 6)
        self.layout.addWidget(self.btnSection, 5, 0, 1, 6)

        self.applySetting()

    def avatar_section(self):
        self.avaSection, avatar_grid = GroupGrid("Avatar")

        self.userAvatar = Label({
            'pxm': 'default',
            'scc': True,
            'sfs': [100, 100]
        })

        set_avatarBtn = Button({
            'txt': 'Set Avatar',
            'tt': 'Choose a showLayout_new avatar',
            'cl': self.setAvaClicked
        })

        avatar_grid.addWidget(self.userAvatar, 0, 0, 2, 2)
        avatar_grid.addWidget(set_avatarBtn, 2, 0, 1, 2)

    def account_section(self):
        self.accSection, account_grid = GroupGrid("Account")
        self.userField = LineEdit()
        self.pwField = LineEdit({'fn': 'password'})
        self.cfpwField = LineEdit({'fn': 'password'})

        account_grid.addWidget(Label({'txt': 'User Name'}), 0, 0, 1, 2)
        account_grid.addWidget(Label({'txt': 'Password'}), 1, 0, 1, 2)
        account_grid.addWidget(Label({'txt': 'Confirm Password'}), 2, 0, 1, 2)

        account_grid.addWidget(self.userField, 0, 3, 1, 4)
        account_grid.addWidget(self.pwField, 1, 3, 1, 4)
        account_grid.addWidget(self.cfpwField, 2, 3, 1, 4)

    def profile_section(self):
        self.prfSection, profile_grid = GroupGrid("Profile")

        profile_grid.addWidget(Label({'txt': 'First Name'}), 0, 0, 1, 2)
        profile_grid.addWidget(Label({'txt': 'Last Name'}), 1, 0, 1, 2)
        profile_grid.addWidget(Label({'txt': 'Your Title'}), 2, 0, 1, 2)
        profile_grid.addWidget(Label({'txt': 'Email'}), 3, 0, 1, 2)
        profile_grid.addWidget(Label({'txt': 'Phone Number'}), 4, 0, 1, 2)

        self.titleField = LineEdit()
        self.firstnameField = LineEdit()
        self.lastnameField = LineEdit()
        self.emailField = LineEdit()
        self.phoneField = LineEdit()

        profile_grid.addWidget(self.firstnameField, 0, 2, 1, 4)
        profile_grid.addWidget(self.lastnameField, 1, 2, 1, 4)
        profile_grid.addWidget(self.titleField, 2, 2, 1, 4)
        profile_grid.addWidget(self.emailField, 3, 2, 1, 4)
        profile_grid.addWidget(self.phoneField, 4, 2, 1, 4)

    def location_section(self):
        self.conSection, conGrid = GroupGrid("Location")

        conGrid.addWidget(Label({'txt': "Address Line 1"}), 0, 0, 1, 2)
        conGrid.addWidget(Label({'txt': "Address Line 2"}), 1, 0, 1, 2)
        conGrid.addWidget(Label({'txt': "Postal"}), 2, 0, 1, 2)
        conGrid.addWidget(Label({'txt': "City"}), 3, 0, 1, 2)
        conGrid.addWidget(Label({'txt': "Country"}), 4, 0, 1, 2)

        self.addressLine1 = LineEdit()
        self.addressLine2 = LineEdit()
        self.postalCode = LineEdit()
        self.city = LineEdit()
        self.country = LineEdit()

        conGrid.addWidget(self.addressLine1, 0, 2, 1, 4)
        conGrid.addWidget(self.addressLine2, 1, 2, 1, 4)
        conGrid.addWidget(self.city, 2, 2, 1, 4)
        conGrid.addWidget(self.postalCode, 3, 2, 1, 4)
        conGrid.addWidget(self.country, 4, 2, 1, 4)

    def security_section(self):

        self.serSection, questions_grid = GroupGrid("Security Question")

        self.ques1 = ComboBox(
            {'items': [str(i) for i in QUESTIONS.split('\n')]})
        self.answ2 = LineEdit()

        self.ques2 = ComboBox(
            {'items': [str(i) for i in QUESTIONS.split('\n')]})
        self.answ1 = LineEdit()

        questions_grid.addWidget(Label({'txt': 'Question 1'}), 0, 0, 1, 3)
        questions_grid.addWidget(Label({'txt': 'Answer 1'}), 1, 0, 1, 3)
        questions_grid.addWidget(Label({'txt': 'Question 2'}), 2, 0, 1, 3)
        questions_grid.addWidget(Label({'txt': 'Answer 2'}), 3, 0, 1, 3)

        questions_grid.addWidget(self.ques1, 0, 3, 1, 6)
        questions_grid.addWidget(self.answ1, 1, 3, 1, 6)
        questions_grid.addWidget(self.ques2, 2, 3, 1, 6)
        questions_grid.addWidget(self.answ2, 3, 3, 1, 6)

    def buttons_section(self):
        self.btnSection, btn_grid = GroupGrid()

        self.user_agree_checkBox = CheckBox(txt=USER_CHECK_REQUIRED)
        okBtn = Button({
            'txt': 'Create Account',
            'tt': 'Confirm to create an account',
            'cl': self.createBtnClicked
        })
        cancelBtn = Button({
            'txt':
            'Cancel',
            'tt':
            'Go back to Login stage',
            'cl':
            partial(self.signals.emit, 'showLayout', 'SignIn', 'SignIn')
        })
        quitBtn = Button({
            'txt': 'Quit',
            'tt': 'Quit the application',
            'cl': QApplication.quit
        })

        btn_grid.addWidget(self.user_agree_checkBox, 0, 0, 1, 6)
        btn_grid.addWidget(okBtn, 1, 0, 1, 2)
        btn_grid.addWidget(cancelBtn, 1, 2, 1, 2)
        btn_grid.addWidget(quitBtn, 1, 4, 1, 2)

    def setAvaClicked(self):
        options = QFileDialog.Options()
        options |= QFileDialog.DontUseNativeDialog
        self.rawAvatarPth, _ = QFileDialog.getOpenFileName(
            self,
            "Your Avatar",
            os.path.join('imgs', 'avatar'),
            "All Files (*);;Img Files (*.jpg)",
            options=options)

        if self.rawAvatarPth:
            self.userAvatar.setPixmap(
                QPixmap.fromImage(QImage(self.rawAvatarPth)))
            self.userAvatar.update()

    def createBtnClicked(self):
        if self.check_all_conditions():
            data = self.generate_user_data()
            MessageBox(self, "Failed", "information", WAIT_LAYOUT_COMPLETE,
                       'ok')
            return

    def collect_input(self):
        username = str(self.userField.text())
        password = str(self.pwField.text())
        confirm = str(self.cfpwField.text())
        firstname = str(self.firstnameField.text())
        lastname = str(self.lastnameField.text())
        email = str(self.emailField.text())
        phone = str(self.phoneField.text())
        address1 = str(self.addressLine1.text())
        address2 = str(self.addressLine2.text())
        postal = str(self.postalCode.text())
        city = str(self.city.text())
        country = str(self.country.text())
        answer1 = str(self.answ1.text())
        answer2 = str(self.answ2.text())
        return [
            username, password, confirm, firstname, lastname, email, phone,
            address1, address2, postal, city, country, answer1, answer2
        ]

    def check_all_conditions(self):
        if self.check_all_field_blank():
            if self.check_user_agreement():
                if self.check_pw_matching():
                    return True
        else:
            return False

    def check_all_field_blank(self):
        regInput = self.collect_input()
        secName = [
            "Username", "Password", "Confirm Password", "Firstname",
            "Lastname", "Email", "Phone", "Address line 1", "Address line 2",
            "Postal", "City", "Country", "Answer 1", "Answer 2"
        ]
        for section in regInput:
            if check_blank(section):
                return True
            else:
                QMessageBox.information(
                    self, "Fail", secName[regInput.index(section)] + "Blank",
                    QMessageBox.Ok)
                break

    def check_user_agreement(self):
        return self.user_agree_checkBox.checkState()

    def applySetting(self):
        self.resize(450, 900)

    def generate_user_data(self):
        regInput = self.collect_input()
        question1 = str(self.ques1.currentText())
        question2 = str(self.ques2.currentText())
        title = str(self.titleField.text()) or "Guess"

        token = getToken()
        timelog = getTime()
        sysInfo = get_user_location()
        productID = sysInfo['Product ID']
        ip, cityIP, countryIP = get_local_pc_info()
        unix = getUnix()
        datelog = getDate()
        pcOS = sysInfo['os']
        pcUser = sysInfo['pcUser']
        pcPython = sysInfo['python']

        if not os.path.exists(self.rawAvatarPth):
            avatar = get_avatar_image('default')
        else:
            avatar = self.rawAvatarPth

        data = [
            regInput[0], regInput[1], regInput[3], regInput[4], title,
            regInput[5], regInput[6], regInput[7], regInput[8], regInput[9],
            regInput[10], regInput[11], token, timelog, productID, ip, cityIP,
            countryIP, unix, question1, regInput[12], question2, regInput[13],
            datelog, pcOS, pcUser, pcPython, avatar
        ]
        return data

    def check_pw_matching(self):
        password = str(self.pwField.text())
        confirm = str(self.cfpwField.text())
        check_pass = check_match(password, confirm)
        if not check_pass:
            QMessageBox.information(self, "Warning", PW_UNMATCH,
                                    QMessageBox.Retry)
            return False
        return True

    def loginChanged(self, login):
        self._login = login

    @property
    def login(self):
        return self._login

    @login.setter
    def login(self, newVal):
        self._login = newVal