246 lines
		
	
	
		
			8.6 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			246 lines
		
	
	
		
			8.6 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
from PySide6.QtWidgets import (
 | 
						|
    QWidget, QLabel, QLineEdit, QPushButton, QVBoxLayout, QMessageBox,
 | 
						|
    QHBoxLayout, QSpacerItem, QSizePolicy
 | 
						|
)
 | 
						|
from PySide6.QtCore import Qt
 | 
						|
from common_lib.utils.validators import validate_username, validate_password
 | 
						|
 | 
						|
class LoginView(QWidget):
 | 
						|
    def __init__(self, on_login):
 | 
						|
        super().__init__()
 | 
						|
        self.on_login = on_login
 | 
						|
        self.setWindowTitle("yobble messenger")
 | 
						|
        self.setFixedSize(400, 550)
 | 
						|
 | 
						|
        self.is_dark_theme = True
 | 
						|
        self.is_registration = False
 | 
						|
 | 
						|
        self.init_ui()
 | 
						|
        self.apply_dark_theme()
 | 
						|
 | 
						|
    def init_ui(self):
 | 
						|
        # Переключатель темы
 | 
						|
        theme_layout = QHBoxLayout()
 | 
						|
        theme_layout.setAlignment(Qt.AlignRight)
 | 
						|
        self.theme_button = QPushButton("🌞")
 | 
						|
        self.theme_button.setFixedWidth(50)
 | 
						|
        self.theme_button.clicked.connect(self.toggle_theme)
 | 
						|
        theme_layout.addSpacerItem(QSpacerItem(40, 20, QSizePolicy.Expanding, QSizePolicy.Minimum))
 | 
						|
        theme_layout.addWidget(self.theme_button)
 | 
						|
 | 
						|
        # Основная часть
 | 
						|
        self.main_layout = QVBoxLayout()
 | 
						|
        self.main_layout.setAlignment(Qt.AlignCenter)
 | 
						|
        self.main_layout.setContentsMargins(40, 40, 40, 40)
 | 
						|
 | 
						|
        self.title = QLabel("Авторизация")
 | 
						|
        self.title.setAlignment(Qt.AlignCenter)
 | 
						|
        self.title.setStyleSheet("font-size: 20px; font-weight: bold;")
 | 
						|
        self.main_layout.addWidget(self.title)
 | 
						|
        self.main_layout.addSpacing(20)
 | 
						|
 | 
						|
        self.init_login_form()
 | 
						|
        self.init_register_form()
 | 
						|
        self.show_login_form()
 | 
						|
 | 
						|
        # Компоновка
 | 
						|
        full_layout = QVBoxLayout(self)
 | 
						|
        full_layout.addLayout(theme_layout)
 | 
						|
        full_layout.addStretch()
 | 
						|
        full_layout.addLayout(self.main_layout)
 | 
						|
        full_layout.addStretch()
 | 
						|
        self.setLayout(full_layout)
 | 
						|
 | 
						|
    def init_login_form(self):
 | 
						|
        self.login_input = QLineEdit()
 | 
						|
        self.login_input.setPlaceholderText("Логин")
 | 
						|
        self.login_input.setFixedHeight(40)
 | 
						|
 | 
						|
        self.password_input = QLineEdit()
 | 
						|
        self.password_input.setPlaceholderText("Пароль")
 | 
						|
        self.password_input.setEchoMode(QLineEdit.Password)
 | 
						|
        self.password_input.setFixedHeight(40)
 | 
						|
 | 
						|
        self.login_button = QPushButton("Войти")
 | 
						|
        self.login_button.setFixedHeight(40)
 | 
						|
        self.login_button.clicked.connect(self.handle_login)
 | 
						|
 | 
						|
        self.register_switch = QPushButton("Нет аккаунта? Регистрация")
 | 
						|
        self.register_switch.setFlat(True)
 | 
						|
        self.register_switch.clicked.connect(self.show_register_form)
 | 
						|
 | 
						|
    def init_register_form(self):
 | 
						|
        self.name_input = QLineEdit()
 | 
						|
        self.name_input.setPlaceholderText("Имя")
 | 
						|
        self.name_input.setFixedHeight(40)
 | 
						|
 | 
						|
        self.reg_login_input = QLineEdit()
 | 
						|
        self.reg_login_input.setPlaceholderText("Логин")
 | 
						|
        self.reg_login_input.setFixedHeight(40)
 | 
						|
 | 
						|
        self.reg_password_input = QLineEdit()
 | 
						|
        self.reg_password_input.setPlaceholderText("Пароль")
 | 
						|
        self.reg_password_input.setEchoMode(QLineEdit.Password)
 | 
						|
        self.reg_password_input.setFixedHeight(40)
 | 
						|
 | 
						|
        self.confirm_password_input = QLineEdit()
 | 
						|
        self.confirm_password_input.setPlaceholderText("Повторите пароль")
 | 
						|
        self.confirm_password_input.setEchoMode(QLineEdit.Password)
 | 
						|
        self.confirm_password_input.setFixedHeight(40)
 | 
						|
 | 
						|
        self.invite_code_input = QLineEdit()
 | 
						|
        self.invite_code_input.setPlaceholderText("Инвайт-код")
 | 
						|
        self.invite_code_input.setFixedHeight(40)
 | 
						|
 | 
						|
        self.register_button = QPushButton("Зарегистрироваться")
 | 
						|
        self.register_button.setFixedHeight(40)
 | 
						|
        self.register_button.clicked.connect(self.handle_register)
 | 
						|
 | 
						|
        self.login_switch = QPushButton("Уже есть аккаунт? Войти")
 | 
						|
        self.login_switch.setFlat(True)
 | 
						|
        self.login_switch.clicked.connect(self.show_login_form)
 | 
						|
 | 
						|
    def show_login_form(self):
 | 
						|
        self.is_registration = False
 | 
						|
        self.title.setText("Авторизация")
 | 
						|
        self.clear_form()
 | 
						|
 | 
						|
        # Очистка layout
 | 
						|
        self.clear_main_layout()
 | 
						|
 | 
						|
        self.main_layout.addWidget(self.login_input)
 | 
						|
        self.main_layout.addWidget(self.password_input)
 | 
						|
        self.main_layout.addWidget(self.login_button)
 | 
						|
        self.main_layout.addSpacing(10)
 | 
						|
        self.main_layout.addWidget(self.register_switch)
 | 
						|
 | 
						|
    def show_register_form(self):
 | 
						|
        self.is_registration = True
 | 
						|
        self.title.setText("Регистрация")
 | 
						|
        self.clear_form()
 | 
						|
 | 
						|
        self.clear_main_layout()
 | 
						|
 | 
						|
        self.main_layout.addWidget(self.name_input)
 | 
						|
        self.main_layout.addWidget(self.reg_login_input)
 | 
						|
        self.main_layout.addWidget(self.reg_password_input)
 | 
						|
        self.main_layout.addWidget(self.confirm_password_input)
 | 
						|
        self.main_layout.addWidget(self.invite_code_input)
 | 
						|
        self.main_layout.addWidget(self.register_button)
 | 
						|
        self.main_layout.addSpacing(10)
 | 
						|
        self.main_layout.addWidget(self.login_switch)
 | 
						|
 | 
						|
    def clear_form(self):
 | 
						|
        for field in [
 | 
						|
            self.login_input, self.password_input,
 | 
						|
            self.name_input, self.reg_login_input,
 | 
						|
            self.reg_password_input, self.confirm_password_input,
 | 
						|
            self.invite_code_input
 | 
						|
        ]:
 | 
						|
            field.setText("")
 | 
						|
 | 
						|
    def clear_main_layout(self):
 | 
						|
        while self.main_layout.count() > 2:  # сохраняем title и spacing
 | 
						|
            child = self.main_layout.takeAt(2)
 | 
						|
            if child.widget():
 | 
						|
                child.widget().setParent(None)
 | 
						|
 | 
						|
    def handle_login(self):
 | 
						|
        login = self.login_input.text()
 | 
						|
        password = self.password_input.text()
 | 
						|
 | 
						|
        if login == "root" and password == "123":
 | 
						|
            self.on_login(login)
 | 
						|
        else:
 | 
						|
            QMessageBox.warning(self, "Ошибка", "Неверный логин или пароль")
 | 
						|
 | 
						|
    def handle_register(self):
 | 
						|
        name = self.name_input.text()
 | 
						|
        login = self.reg_login_input.text()
 | 
						|
        password = self.reg_password_input.text()
 | 
						|
        confirm = self.confirm_password_input.text()
 | 
						|
        invite = self.invite_code_input.text()
 | 
						|
 | 
						|
        if not all([name, login, password, confirm, invite]):
 | 
						|
            QMessageBox.warning(self, "Ошибка", "Заполните все поля")
 | 
						|
            return
 | 
						|
 | 
						|
        if password != confirm:
 | 
						|
            QMessageBox.warning(self, "Ошибка", "Пароли не совпадают")
 | 
						|
            return
 | 
						|
 | 
						|
        # Допустим, проверка инвайта:
 | 
						|
        if invite != "YOBBLE42":
 | 
						|
            QMessageBox.warning(self, "Ошибка", "Неверный инвайт-код")
 | 
						|
            return
 | 
						|
 | 
						|
        QMessageBox.information(self, "Успех", f"Регистрация прошла успешно для {name}")
 | 
						|
        self.show_login_form()
 | 
						|
 | 
						|
    def toggle_theme(self):
 | 
						|
        self.is_dark_theme = not self.is_dark_theme
 | 
						|
        if self.is_dark_theme:
 | 
						|
            self.apply_dark_theme()
 | 
						|
        else:
 | 
						|
            self.apply_light_theme()
 | 
						|
 | 
						|
    def apply_dark_theme(self):
 | 
						|
        self.setStyleSheet("""
 | 
						|
            QWidget {
 | 
						|
                background-color: #2e2e2e;
 | 
						|
                color: white;
 | 
						|
            }
 | 
						|
            QLineEdit {
 | 
						|
                background-color: #444;
 | 
						|
                color: white;
 | 
						|
                border: 1px solid #666;
 | 
						|
                border-radius: 5px;
 | 
						|
                padding: 5px;
 | 
						|
            }
 | 
						|
            QPushButton {
 | 
						|
                background-color: #555;
 | 
						|
                color: white;
 | 
						|
                border: none;
 | 
						|
                border-radius: 5px;
 | 
						|
            }
 | 
						|
            QPushButton:hover {
 | 
						|
                background-color: #777;
 | 
						|
            }
 | 
						|
            QPushButton:flat {
 | 
						|
                background-color: transparent;
 | 
						|
                color: #aaa;
 | 
						|
                text-decoration: underline;
 | 
						|
            }
 | 
						|
        """)
 | 
						|
        self.theme_button.setText("🌙")
 | 
						|
 | 
						|
    def apply_light_theme(self):
 | 
						|
        self.setStyleSheet("""
 | 
						|
            QWidget {
 | 
						|
                background-color: #f0f0f0;
 | 
						|
                color: #222;
 | 
						|
            }
 | 
						|
            QLineEdit {
 | 
						|
                background-color: white;
 | 
						|
                color: black;
 | 
						|
                border: 1px solid #ccc;
 | 
						|
                border-radius: 5px;
 | 
						|
                padding: 5px;
 | 
						|
            }
 | 
						|
            QPushButton {
 | 
						|
                background-color: #e0e0e0;
 | 
						|
                color: black;
 | 
						|
                border: none;
 | 
						|
                border-radius: 5px;
 | 
						|
            }
 | 
						|
            QPushButton:hover {
 | 
						|
                background-color: #d0d0d0;
 | 
						|
            }
 | 
						|
            QPushButton:flat {
 | 
						|
                background-color: transparent;
 | 
						|
                color: #666;
 | 
						|
                text-decoration: underline;
 | 
						|
            }
 | 
						|
        """)
 | 
						|
        self.theme_button.setText("🌞")
 |