# -*- coding: utf-8 -*-

from PyQt5 import QtCore, QtGui, QtWidgets

import random
from captcha.image import ImageCaptcha


class Ui_LoginDialog(object):
	def setupUi(self, LoginDialog):
		LoginDialog.setObjectName("LoginDialog")
		LoginDialog.resize(700, 500)
		icon = QtGui.QIcon()
		icon.addPixmap(QtGui.QPixmap(":/mainwindow/images/mainwindow/money-bag.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
		LoginDialog.setWindowIcon(icon)
		self.verticalLayout = QtWidgets.QVBoxLayout(LoginDialog)
		self.verticalLayout.setContentsMargins(0, 0, 0, 0)
		self.verticalLayout.setObjectName("verticalLayout")
		self.stackedWidget = QtWidgets.QStackedWidget(LoginDialog)
		self.stackedWidget.setObjectName("stackedWidget")
		self.page_login = QtWidgets.QWidget()
		self.page_login.setObjectName("page_login")
		self.verticalLayout_3 = QtWidgets.QVBoxLayout(self.page_login)
		self.verticalLayout_3.setContentsMargins(0, 0, 0, 0)
		self.verticalLayout_3.setSpacing(0)
		self.verticalLayout_3.setObjectName("verticalLayout_3")
		self.background = QtWidgets.QLabel(self.page_login)
		self.background.setMinimumSize(QtCore.QSize(0, 150))
		self.background.setStyleSheet("border-image: url(:/login/images/login/login.png);")
		self.background.setObjectName("background")
		self.verticalLayout_3.addWidget(self.background)
		self.progress_bar = QtWidgets.QLabel(self.page_login)
		self.progress_bar.setMaximumSize(QtCore.QSize(16777215, 10))
		self.progress_bar.setStyleSheet("background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(200, 200, 200, 255), stop:0.343137 rgba(200, 200, 200, 255), stop:0.607843 rgba(255, 170, 0, 255), stop:0.813725 rgba(200, 200, 200, 255), stop:1 rgba(200, 200, 200, 255));")
		self.progress_bar.setObjectName("progress_bar")
		self.verticalLayout_3.addWidget(self.progress_bar)
		spacerItem = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
		self.verticalLayout_3.addItem(spacerItem)
		self.widget_login_info = QtWidgets.QWidget(self.page_login)
		self.widget_login_info.setObjectName("widget_login_info")
		self.gridLayout_3 = QtWidgets.QGridLayout(self.widget_login_info)
		self.gridLayout_3.setContentsMargins(80, -1, 80, -1)
		self.gridLayout_3.setObjectName("gridLayout_3")
		self.label_account_number = QtWidgets.QLabel(self.widget_login_info)
		self.label_account_number.setObjectName("label_account_number")
		self.gridLayout_3.addWidget(self.label_account_number, 0, 0, 1, 1)
		self.account_number = QtWidgets.QLineEdit(self.widget_login_info)
		self.account_number.setClearButtonEnabled(True)
		self.account_number.setObjectName("account_number")
		self.gridLayout_3.addWidget(self.account_number, 0, 1, 1, 1)
		self.remember_account_number = QtWidgets.QCheckBox(self.widget_login_info)
		self.remember_account_number.setFocusPolicy(QtCore.Qt.NoFocus)
		self.remember_account_number.setChecked(False)
		self.remember_account_number.setObjectName("remember_account_number")
		self.gridLayout_3.addWidget(self.remember_account_number, 0, 2, 1, 1)
		self.label_password = QtWidgets.QLabel(self.widget_login_info)
		self.label_password.setObjectName("label_password")
		self.gridLayout_3.addWidget(self.label_password, 1, 0, 1, 1)
		self.password = QtWidgets.QLineEdit(self.widget_login_info)
		
		# 字符过滤
		regx = QtCore.QRegExp('^[^\u4e00-\u9fa5\s]{0,}$')
		validator = QtGui.QRegExpValidator(regx, self.password)
		self.password.setValidator(validator)
		
		self.password.setEchoMode(QtWidgets.QLineEdit.Password)
		self.password.setClearButtonEnabled(True)
		self.password.setObjectName("password")
		self.gridLayout_3.addWidget(self.password, 1, 1, 1, 1)
		self.remember_password = QtWidgets.QCheckBox(self.widget_login_info)
		self.remember_password.setFocusPolicy(QtCore.Qt.NoFocus)
		self.remember_password.setObjectName("remember_password")
		self.gridLayout_3.addWidget(self.remember_password, 1, 2, 1, 1)
		self.label_verification_code = QtWidgets.QLabel(self.widget_login_info)
		self.label_verification_code.setObjectName("label_verification_code")
		self.gridLayout_3.addWidget(self.label_verification_code, 2, 0, 1, 1)
		self.verification_code = QtWidgets.QLineEdit(self.widget_login_info)
		
		# 字符过滤
		regx = QtCore.QRegExp('^[A-Za-z0-9]+$')
		validator = QtGui.QRegExpValidator(regx, self.verification_code)
		self.verification_code.setValidator(validator)		
		
		self.verification_code.setFocusPolicy(QtCore.Qt.StrongFocus)
		self.verification_code.setMaxLength(4)
		self.verification_code.setClearButtonEnabled(True)
		self.verification_code.setObjectName("verification_code")
		self.gridLayout_3.addWidget(self.verification_code, 2, 1, 1, 1)
		
		# 使用自定义label
		self.picture_verification_code = ClickedLabel(self, self.widget_login_info)
		
		self.picture_verification_code.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
		
		# 生成验证码
		self.str_verification_code, pixmap_image_code = self.generateVerificationCode()
		pixmap = QtGui.QPixmap(pixmap_image_code)
		scaled_pixmap = pixmap.scaled(87, 21, QtCore.Qt.IgnoreAspectRatio)
		self.picture_verification_code.setPixmap(scaled_pixmap)
		
		self.picture_verification_code.setObjectName("picture_verification_code")
		self.gridLayout_3.addWidget(self.picture_verification_code, 2, 2, 1, 1)
		self.verticalLayout_3.addWidget(self.widget_login_info)
		self.widget_user_type = QtWidgets.QWidget(self.page_login)
		self.widget_user_type.setObjectName("widget_user_type")
		self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.widget_user_type)
		self.horizontalLayout_2.setContentsMargins(80, -1, 80, -1)
		self.horizontalLayout_2.setObjectName("horizontalLayout_2")
		self.user_type = QtWidgets.QLabel(self.widget_user_type)
		self.user_type.setObjectName("user_type")
		self.horizontalLayout_2.addWidget(self.user_type)
		self.emp = QtWidgets.QRadioButton(self.widget_user_type)
		self.emp.setFocusPolicy(QtCore.Qt.NoFocus)
		self.emp.setChecked(True)
		self.emp.setObjectName("emp")
		self.horizontalLayout_2.addWidget(self.emp)
		self.admin = QtWidgets.QRadioButton(self.widget_user_type)
		self.admin.setFocusPolicy(QtCore.Qt.NoFocus)
		self.admin.setObjectName("admin")
		self.horizontalLayout_2.addWidget(self.admin)
		self.horizontalLayout_2.setStretch(0, 2)
		self.horizontalLayout_2.setStretch(1, 3)
		self.horizontalLayout_2.setStretch(2, 3)
		self.verticalLayout_3.addWidget(self.widget_user_type)
		spacerItem1 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
		self.verticalLayout_3.addItem(spacerItem1)
		self.widget_login_button = QtWidgets.QWidget(self.page_login)
		self.widget_login_button.setObjectName("widget_login_button")
		self.horizontalLayout_3 = QtWidgets.QHBoxLayout(self.widget_login_button)
		self.horizontalLayout_3.setObjectName("horizontalLayout_3")
		spacerItem2 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
		self.horizontalLayout_3.addItem(spacerItem2)
		self.login = QtWidgets.QPushButton(self.widget_login_button)
		self.login.setMinimumSize(QtCore.QSize(200, 50))
		self.login.setFocusPolicy(QtCore.Qt.NoFocus)
		self.login.setAutoDefault(False)
		self.login.setDefault(False)
		self.login.setObjectName("login")
		self.horizontalLayout_3.addWidget(self.login)
		spacerItem3 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
		self.horizontalLayout_3.addItem(spacerItem3)
		self.verticalLayout_3.addWidget(self.widget_login_button)
		spacerItem4 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
		self.verticalLayout_3.addItem(spacerItem4)
		self.widget_clicked_label = QtWidgets.QWidget(self.page_login)
		self.widget_clicked_label.setObjectName("widget_clicked_label")
		self.horizontalLayout_4 = QtWidgets.QHBoxLayout(self.widget_clicked_label)
		self.horizontalLayout_4.setObjectName("horizontalLayout_4")
		
		# 使用自定义label
		self.database = ClickedLabel(self, self.widget_clicked_label)
		
		self.database.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
		self.database.setAlignment(QtCore.Qt.AlignCenter)
		self.database.setObjectName("database")
		self.horizontalLayout_4.addWidget(self.database)
		spacerItem5 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
		self.horizontalLayout_4.addItem(spacerItem5)
		
		# 使用自定义label
		self.manual = ClickedLabel(self, self.widget_clicked_label)
		
		self.manual.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
		self.manual.setAlignment(QtCore.Qt.AlignCenter)
		self.manual.setObjectName("manual")
		self.horizontalLayout_4.addWidget(self.manual)
		self.verticalLayout_3.addWidget(self.widget_clicked_label)
		self.stackedWidget.addWidget(self.page_login)
		self.page_database = QtWidgets.QWidget()
		self.page_database.setObjectName("page_database")
		self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.page_database)
		self.verticalLayout_2.setObjectName("verticalLayout_2")
		spacerItem6 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
		self.verticalLayout_2.addItem(spacerItem6)
		self.widget_database_info = QtWidgets.QWidget(self.page_database)
		self.widget_database_info.setObjectName("widget_database_info")
		self.gridLayout_2 = QtWidgets.QGridLayout(self.widget_database_info)
		self.gridLayout_2.setObjectName("gridLayout_2")
		self.server_name = QtWidgets.QLineEdit(self.widget_database_info)
		self.server_name.setObjectName("server_name")
		self.gridLayout_2.addWidget(self.server_name, 0, 1, 1, 1)
		self.label_username = QtWidgets.QLabel(self.widget_database_info)
		self.label_username.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing|QtCore.Qt.AlignVCenter)
		self.label_username.setObjectName("label_username")
		self.gridLayout_2.addWidget(self.label_username, 2, 0, 1, 1)
		self.database_password = QtWidgets.QLineEdit(self.widget_database_info)
		self.database_password.setEchoMode(QtWidgets.QLineEdit.Password)
		self.database_password.setObjectName("database_password")
		self.gridLayout_2.addWidget(self.database_password, 3, 1, 1, 1)
		self.label_server_name = QtWidgets.QLabel(self.widget_database_info)
		self.label_server_name.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing|QtCore.Qt.AlignVCenter)
		self.label_server_name.setObjectName("label_server_name")
		self.gridLayout_2.addWidget(self.label_server_name, 0, 0, 1, 1)
		self.label_database_password = QtWidgets.QLabel(self.widget_database_info)
		self.label_database_password.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing|QtCore.Qt.AlignVCenter)
		self.label_database_password.setObjectName("label_database_password")
		self.gridLayout_2.addWidget(self.label_database_password, 3, 0, 1, 1)
		self.username = QtWidgets.QLineEdit(self.widget_database_info)
		self.username.setObjectName("username")
		self.gridLayout_2.addWidget(self.username, 2, 1, 1, 1)
		self.label_server_name_2 = QtWidgets.QLabel(self.widget_database_info)
		self.label_server_name_2.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing|QtCore.Qt.AlignVCenter)
		self.label_server_name_2.setObjectName("label_server_name_2")
		self.gridLayout_2.addWidget(self.label_server_name_2, 1, 0, 1, 1)
		self.database_name = QtWidgets.QLineEdit(self.widget_database_info)
		self.database_name.setObjectName("database_name")
		self.gridLayout_2.addWidget(self.database_name, 1, 1, 1, 1)
		self.verticalLayout_2.addWidget(self.widget_database_info)
		spacerItem7 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
		self.verticalLayout_2.addItem(spacerItem7)
		self.widget_buttons = QtWidgets.QWidget(self.page_database)
		self.widget_buttons.setObjectName("widget_buttons")
		self.horizontalLayout = QtWidgets.QHBoxLayout(self.widget_buttons)
		self.horizontalLayout.setObjectName("horizontalLayout")
		spacerItem8 = QtWidgets.QSpacerItem(26, 25, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
		self.horizontalLayout.addItem(spacerItem8)
		self.confirm = QtWidgets.QPushButton(self.widget_buttons)
		self.confirm.setFocusPolicy(QtCore.Qt.NoFocus)
		self.confirm.setAutoDefault(False)
		self.confirm.setObjectName("confirm")
		self.horizontalLayout.addWidget(self.confirm)
		spacerItem9 = QtWidgets.QSpacerItem(26, 25, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
		self.horizontalLayout.addItem(spacerItem9)
		self.go_back_1 = QtWidgets.QPushButton(self.widget_buttons)
		self.go_back_1.setFocusPolicy(QtCore.Qt.NoFocus)
		self.go_back_1.setObjectName("go_back_1")
		self.horizontalLayout.addWidget(self.go_back_1)
		spacerItem10 = QtWidgets.QSpacerItem(26, 25, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
		self.horizontalLayout.addItem(spacerItem10)
		self.verticalLayout_2.addWidget(self.widget_buttons)
		spacerItem11 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
		self.verticalLayout_2.addItem(spacerItem11)
		self.stackedWidget.addWidget(self.page_database)
		self.page_manual = QtWidgets.QWidget()
		self.page_manual.setObjectName("page_manual")
		self.gridLayout = QtWidgets.QGridLayout(self.page_manual)
		self.gridLayout.setObjectName("gridLayout")
		spacerItem12 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
		self.gridLayout.addItem(spacerItem12, 0, 1, 1, 1)
		spacerItem13 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
		self.gridLayout.addItem(spacerItem13, 1, 0, 1, 1)
		self.widget_labels = QtWidgets.QWidget(self.page_manual)
		self.widget_labels.setObjectName("widget_labels")
		self.verticalLayout_5 = QtWidgets.QVBoxLayout(self.widget_labels)
		self.verticalLayout_5.setObjectName("verticalLayout_5")
		self.label_1 = QtWidgets.QLabel(self.widget_labels)
		self.label_1.setObjectName("label_1")
		self.verticalLayout_5.addWidget(self.label_1)
		self.label_2 = QtWidgets.QLabel(self.widget_labels)
		self.label_2.setObjectName("label_2")
		self.verticalLayout_5.addWidget(self.label_2)
		self.label_3 = QtWidgets.QLabel(self.widget_labels)
		self.label_3.setObjectName("label_3")
		self.verticalLayout_5.addWidget(self.label_3)
		self.gridLayout.addWidget(self.widget_labels, 1, 1, 1, 1)
		spacerItem14 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
		self.gridLayout.addItem(spacerItem14, 1, 2, 1, 1)
		spacerItem15 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
		self.gridLayout.addItem(spacerItem15, 2, 1, 1, 1)
		self.go_back_2 = QtWidgets.QPushButton(self.page_manual)
		self.go_back_2.setFocusPolicy(QtCore.Qt.NoFocus)
		self.go_back_2.setAutoDefault(False)
		self.go_back_2.setObjectName("go_back_2")
		self.gridLayout.addWidget(self.go_back_2, 3, 1, 1, 1)
		spacerItem16 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
		self.gridLayout.addItem(spacerItem16, 4, 1, 1, 1)
		self.stackedWidget.addWidget(self.page_manual)
		self.verticalLayout.addWidget(self.stackedWidget)

		self.retranslateUi(LoginDialog)
		self.stackedWidget.setCurrentIndex(0)
		QtCore.QMetaObject.connectSlotsByName(LoginDialog)
		LoginDialog.setTabOrder(self.account_number, self.password)
		LoginDialog.setTabOrder(self.password, self.verification_code)
		LoginDialog.setTabOrder(self.verification_code, self.server_name)
		LoginDialog.setTabOrder(self.server_name, self.database_name)
		LoginDialog.setTabOrder(self.database_name, self.username)
		LoginDialog.setTabOrder(self.username, self.database_password)
		LoginDialog.setTabOrder(self.database_password, self.go_back_1)
		LoginDialog.setTabOrder(self.go_back_1, self.go_back_2)
		LoginDialog.setTabOrder(self.go_back_2, self.confirm)

	def retranslateUi(self, LoginDialog):
		_translate = QtCore.QCoreApplication.translate
		LoginDialog.setWindowTitle(_translate("LoginDialog", "工资管理系统"))
		self.label_account_number.setText(_translate("LoginDialog", "账  号："))
		self.account_number.setPlaceholderText(_translate("LoginDialog", "请输入账号"))
		self.remember_account_number.setText(_translate("LoginDialog", "记住账号"))
		self.label_password.setText(_translate("LoginDialog", "密  码："))
		self.password.setPlaceholderText(_translate("LoginDialog", "请输入密码"))
		self.remember_password.setText(_translate("LoginDialog", "记住密码"))
		self.label_verification_code.setText(_translate("LoginDialog", "验证码："))
		self.verification_code.setPlaceholderText(_translate("LoginDialog", "请输入验证码（区分大小写）"))
		self.user_type.setText(_translate("LoginDialog", "用户类型："))
		self.emp.setText(_translate("LoginDialog", "普通员工"))
		self.admin.setText(_translate("LoginDialog", "财务管理员"))
		self.login.setText(_translate("LoginDialog", "登录"))
		self.database.setText(_translate("LoginDialog", "<u>数据库设置</u>"))
		self.manual.setText(_translate("LoginDialog", "<u>使用说明</u>"))
		self.label_username.setText(_translate("LoginDialog", "用户名："))
		self.label_server_name.setText(_translate("LoginDialog", "服务器名称："))
		self.label_database_password.setText(_translate("LoginDialog", "密码："))
		self.label_server_name_2.setText(_translate("LoginDialog", "数据库名称："))
		self.confirm.setText(_translate("LoginDialog", "确认"))
		self.go_back_1.setText(_translate("LoginDialog", "返回"))
		self.label_1.setText(_translate("LoginDialog", "1、设置数据连接信息。"))
		self.label_2.setText(_translate("LoginDialog", "2、填写账号密码以及验证码（区分大小写）。"))
		self.label_3.setText(_translate("LoginDialog", "3、选择需要登录的用户类型。"))
		self.go_back_2.setText(_translate("LoginDialog", "返回"))

	def generateVerificationCode(self, len=4):
		''' 随机生成4位的验证码 '''
		list_code = [] 
		for i in range(10): # 0-9数字
			list_code.append(str(i))
		for i in range(65, 91): # 对应从“A”到“Z”的ASCII码
			list_code.append(chr(i))
		for i in range(97, 123): #对应从“a”到“z”的ASCII码
			list_code.append(chr(i))
		slice = random.sample(list_code, len)  # 从list中随机获取4个元素，作为一个验证码返回
		verification_code = ''.join(slice) # list to str
		
		img = ImageCaptcha()
		image_code = img.generate_image(verification_code)
		pixmap_image_code = image_code.toqpixmap()
		
		return verification_code, pixmap_image_code
		
		
# 通过重载事件实现可点击的Label控件
class ClickedLabel(QtWidgets.QLabel):
	def __init__(self, login, parent=None):
		super(ClickedLabel, self).__init__(parent)
		self.login = login
		
	def mousePressEvent(self, event):
		if event.button() == QtCore.Qt.LeftButton:
			if '数据库设置' in self.text():
				# 清除所有焦点
				self.login.account_number.clearFocus()
				self.login.password.clearFocus()
				self.login.verification_code.clearFocus()
				# 换页
				self.login.stackedWidget.setCurrentIndex(1)
				
			elif '使用说明' in self.text():
				# 清除所有焦点
				self.login.account_number.clearFocus()
				self.login.password.clearFocus()
				self.login.verification_code.clearFocus()
				# 换页
				self.login.stackedWidget.setCurrentIndex(2)
			else:
				# 换验证码
				self.login.str_verification_code, pixmap_image_code = self.login.generateVerificationCode()
				pixmap = QtGui.QPixmap(pixmap_image_code)
				scaled_pixmap = pixmap.scaled(87, 21, QtCore.Qt.IgnoreAspectRatio)
				self.login.picture_verification_code.setPixmap(scaled_pixmap)		


import images_rc

if __name__ == "__main__":
	import sys
	app = QtWidgets.QApplication(sys.argv)
	LoginDialog = QtWidgets.QDialog()
	ui = Ui_LoginDialog()
	ui.setupUi(LoginDialog)
	LoginDialog.show()
	sys.exit(app.exec_())

