From 486c13a3d36f7eaefadacb4f3910cb21005db769 Mon Sep 17 00:00:00 2001 From: ingvar1995 Date: Thu, 24 May 2018 21:22:12 +0300 Subject: [PATCH] Login screen converted, create profile screen fixed --- toxygen/app.py | 33 ++++-- toxygen/ui/create_profile_screen.py | 21 ++-- toxygen/ui/login_screen.py | 109 ++++++------------- toxygen/ui/password_screen.py | 37 ++++--- toxygen/ui/views/create_profile_screen.ui | 38 +++++-- toxygen/ui/views/login_screen.ui | 124 ++++++++++++++++++++++ toxygen/ui/widgets.py | 15 ++- 7 files changed, 261 insertions(+), 116 deletions(-) create mode 100644 toxygen/ui/views/login_screen.ui diff --git a/toxygen/app.py b/toxygen/app.py index 2f18045..05a38d7 100644 --- a/toxygen/app.py +++ b/toxygen/app.py @@ -31,6 +31,7 @@ from network.tox_dns import ToxDns from history.history import History from file_transfers.file_transfers_messages_service import FileTransfersMessagesService from groups.groups_service import GroupsService +from ui.create_profile_screen import CreateProfileScreen import styles.style # TODO: dynamic loading @@ -165,7 +166,8 @@ class App: if result is None: return False if result.is_new_profile(): # create new profile - self._create_new_profile(result.profile_path) + if not self._create_new_profile(result.profile_path): + return False else: # load existing profile self._load_existing_profile(result.profile_path) self._path = result.profile_path @@ -233,16 +235,25 @@ class App: data = self._enter_pass(data) self._tox = self._create_tox(data) - def _create_new_profile(self, profile_path): - name = util.get_profile_name_from_path(profile_path) or 'toxygen_user' + def _create_new_profile(self, profile_name): + result = self._get_create_profile_screen_result() + if result is None: + return False + if result.save_into_default_folder: + profile_path = util.join_path(Settings.get_default_path(), profile_name + '.tox') + else: + profile_path = util.join_path(util.curr_directory(__file__), profile_name + '.tox') if os.path.isfile(profile_path): util_ui.message_box(util_ui.tr('Profile with this name already exists'), util_ui.tr('Error')) - return + return False + name = profile_name or 'toxygen_user' self._tox = tox_factory() self._tox.self_set_name(bytes(name, 'utf-8') if name else b'Toxygen User') self._tox.self_set_status_message(b'Toxing on Toxygen') - # TODO: set profile password + self._path = profile_path + if result.password: + self._toxes.set_password(result.password) self._settings = Settings(self._toxes, self._path.replace('.tox', '.json')) self._profile_manager = ProfileManager(self._settings, self._toxes, profile_path) try: @@ -252,12 +263,22 @@ class App: util.log('Profile creation exception: ' + str(ex)) text = util_ui.tr('Profile saving error! Does Toxygen have permission to write to this directory?') util_ui.message_box(text, util_ui.tr('Error')) - return + + return False current_language, supported_languages = self._get_languages() if current_language in supported_languages: self._settings['language'] = current_language self._settings.save() + return True + + def _get_create_profile_screen_result(self): + cps = CreateProfileScreen() + cps.show() + self._app.exec_() + + return cps.result + def _save_profile(self, data=None): data = data or self._tox.get_savedata() self._profile_manager.save_profile(data) diff --git a/toxygen/ui/create_profile_screen.py b/toxygen/ui/create_profile_screen.py index 08b85cf..9d7b503 100644 --- a/toxygen/ui/create_profile_screen.py +++ b/toxygen/ui/create_profile_screen.py @@ -29,15 +29,24 @@ class CreateProfileScreen(CenteredWidget, DialogWithResult): uic.loadUi(util.get_views_path('create_profile_screen'), self) self.center() self.createProfile.clicked.connect(self._create_profile) + self.retranslateUi() def retranslateUi(self): - self.defaultFolder.setPlaceholderText(util_ui.tr('Save in default folder')) - self.programFolder.setPlaceholderText(util_ui.tr('Save in program folder')) + self.setWindowTitle(util_ui.tr('New profile settings')) + self.defaultFolder.setText(util_ui.tr('Save in default folder')) + self.programFolder.setText(util_ui.tr('Save in program folder')) + self.password.setPlaceholderText(util_ui.tr('Password')) + self.confirmPassword.setPlaceholderText(util_ui.tr('Confirm password')) self.createProfile.setText(util_ui.tr('Create profile')) - self.passwordLabel.setText(util_ui.tr('Password:')) + self.passwordLabel.setText(util_ui.tr('Password (at least 8 symbols):')) def _create_profile(self): - if self.password.text() != self.confirmPassword.text(): - return # TODO : error - result = CreateProfileScreenResult(self.defaultFolder.isChecked(), self.password.text()) + password = self.password.text() + if password != self.confirmPassword.text(): + self.errorLabel.setText(util_ui.tr('Passwords do not match')) + return + if 0 < len(password) < 8: + self.errorLabel.setText(util_ui.tr('Password must be at least 8 symbols')) + return + result = CreateProfileScreenResult(self.defaultFolder.isChecked(), password) self.close_with_result(result) diff --git a/toxygen/ui/login_screen.py b/toxygen/ui/login_screen.py index bbafef9..946dc7a 100644 --- a/toxygen/ui/login_screen.py +++ b/toxygen/ui/login_screen.py @@ -1,20 +1,10 @@ from ui.widgets import * +from PyQt5 import uic +import utils.util as util +import utils.ui as util_ui import os.path -class NickEdit(LineEdit): - - def __init__(self, parent): - super().__init__(parent) - self.parent = parent - - def keyPressEvent(self, event): - if event.key() == QtCore.Qt.Key_Return: - self.parent.create_profile() - else: - super(NickEdit, self).keyPressEvent(event) - - class LoginScreenResult: def __init__(self, profile_path, load_as_default, password=None): @@ -46,75 +36,42 @@ class LoginScreen(CenteredWidget, DialogWithResult): def __init__(self): CenteredWidget.__init__(self) DialogWithResult.__init__(self) - self.initUI() + uic.loadUi(util.get_views_path('login_screen'), self) self.center() self._profiles = [] - - def initUI(self): - self.resize(400, 200) - self.setMinimumSize(QtCore.QSize(400, 200)) - self.setMaximumSize(QtCore.QSize(400, 200)) - self.new_profile = QtWidgets.QPushButton(self) - self.new_profile.setGeometry(QtCore.QRect(20, 150, 171, 27)) - self.new_profile.clicked.connect(self.create_profile) - self.label = QtWidgets.QLabel(self) - self.label.setGeometry(QtCore.QRect(20, 70, 101, 17)) - self.new_name = NickEdit(self) - self.new_name.setGeometry(QtCore.QRect(20, 100, 171, 31)) - self.load_profile = QtWidgets.QPushButton(self) - self.load_profile.setGeometry(QtCore.QRect(220, 150, 161, 27)) - self.load_profile.clicked.connect(self.load_existing_profile) - self.default = QtWidgets.QCheckBox(self) - self.default.setGeometry(QtCore.QRect(220, 110, 131, 22)) - self.groupBox = QtWidgets.QGroupBox(self) - self.groupBox.setGeometry(QtCore.QRect(210, 40, 181, 151)) - self.comboBox = QtWidgets.QComboBox(self.groupBox) - self.comboBox.setGeometry(QtCore.QRect(10, 30, 161, 27)) - self.groupBox_2 = QtWidgets.QGroupBox(self) - self.groupBox_2.setGeometry(QtCore.QRect(10, 40, 191, 151)) - self.toxygen = QtWidgets.QLabel(self) - self.toxygen.setGeometry(QtCore.QRect(160, 8, 90, 25)) - self.groupBox.raise_() - self.groupBox_2.raise_() - self.comboBox.raise_() - self.default.raise_() - self.load_profile.raise_() - self.new_name.raise_() - self.new_profile.raise_() - font = QtGui.QFont() - font.setFamily("Impact") - font.setPointSize(16) - self.toxygen.setFont(font) - self.toxygen.setObjectName("toxygen") - self.retranslateUi() - QtCore.QMetaObject.connectSlotsByName(self) + self._update_ui() def retranslateUi(self): - self.new_name.setPlaceholderText(QtWidgets.QApplication.translate("login", "Profile name")) - self.setWindowTitle(QtWidgets.QApplication.translate("login", "Log in")) - self.new_profile.setText(QtWidgets.QApplication.translate("login", "Create")) - self.label.setText(QtWidgets.QApplication.translate("login", "Profile name:")) - self.load_profile.setText(QtWidgets.QApplication.translate("login", "Load profile")) - self.default.setText(QtWidgets.QApplication.translate("login", "Use as default")) - self.groupBox.setTitle(QtWidgets.QApplication.translate("login", "Load existing profile")) - self.groupBox_2.setTitle(QtWidgets.QApplication.translate("login", "Create new profile")) - self.toxygen.setText(QtWidgets.QApplication.translate("login", "toxygen")) - - def create_profile(self): - self.type = 1 - self.name = self.new_name.text() - self.close() - - def load_existing_profile(self): - index = self.comboBox.currentIndex() - load_as_default = self.default.isChecked() - path = os.path.join(self._profiles[index][0], self._profiles[index][1] + '.tox') - result = LoginScreenResult(path, load_as_default) - self.close_with_result(result) + self.setWindowTitle(util_ui.tr('Log in')) + self.profileNameLineEdit.setPlaceholderText(util_ui.tr('Profile name')) + self.createProfilePushButton.setText(util_ui.tr('Create')) + self.loadProfilePushButton.setText(util_ui.tr('Load profile')) + self.defaultProfileCheckBox.setText(util_ui.tr('Use as default')) + self.existingProfileGroupBox.setTitle(util_ui.tr('Load existing profile')) + self.newProfileGroupBox.setTitle(util_ui.tr('Create new profile')) def update_select(self, profiles): profiles = sorted(profiles, key=lambda p: p[1]) self._profiles = list(profiles) - self.comboBox.addItems(list(map(lambda p: p[1], profiles))) - self.load_profile.setEnabled(len(profiles) > 0) + self.profilesComboBox.addItems(list(map(lambda p: p[1], profiles))) + self.loadProfilePushButton.setEnabled(len(profiles) > 0) + def _update_ui(self): + self.profileNameLineEdit = LineEditWithEnterSupport(self._create_profile, self) + self.profileNameLineEdit.setGeometry(QtCore.QRect(20, 100, 170, 30)) + self.retranslateUi() + self.createProfilePushButton.clicked.connect(self._create_profile) + self.loadProfilePushButton.clicked.connect(self._load_existing_profile) + + def _create_profile(self): + path = self.profileNameLineEdit.text() + load_as_default = self.defaultProfileCheckBox.isChecked() + result = LoginScreenResult(path, load_as_default) + self.close_with_result(result) + + def _load_existing_profile(self): + index = self.profilesComboBox.currentIndex() + load_as_default = self.defaultProfileCheckBox.isChecked() + path = util.join_path(self._profiles[index][0], self._profiles[index][1] + '.tox') + result = LoginScreenResult(path, load_as_default) + self.close_with_result(result) diff --git a/toxygen/ui/password_screen.py b/toxygen/ui/password_screen.py index bd6c146..bbae7ff 100644 --- a/toxygen/ui/password_screen.py +++ b/toxygen/ui/password_screen.py @@ -1,19 +1,20 @@ from ui.widgets import CenteredWidget, LineEdit, DialogWithResult from PyQt5 import QtCore, QtWidgets +import utils.ui as util_ui class PasswordArea(LineEdit): def __init__(self, parent): - super(PasswordArea, self).__init__(parent) - self.parent = parent + super().__init__(parent) + self._parent = parent self.setEchoMode(QtWidgets.QLineEdit.Password) def keyPressEvent(self, event): if event.key() == QtCore.Qt.Key_Return: - self.parent.button_click() + self._parent.button_click() else: - super(PasswordArea, self).keyPressEvent(event) + super().keyPressEvent(event) class PasswordScreenBase(CenteredWidget, DialogWithResult): @@ -37,7 +38,7 @@ class PasswordScreenBase(CenteredWidget, DialogWithResult): self.button = QtWidgets.QPushButton(self) self.button.setGeometry(QtCore.QRect(30, 90, 300, 30)) - self.button.setText('OK') + self.button.setText(util_ui.tr('OK')) self.button.clicked.connect(self.button_click) self.warning = QtWidgets.QLabel(self) @@ -59,15 +60,15 @@ class PasswordScreenBase(CenteredWidget, DialogWithResult): super(PasswordScreenBase, self).keyPressEvent(event) def retranslateUi(self): - self.setWindowTitle(QtWidgets.QApplication.translate("pass", "Enter password")) - self.enter_pass.setText(QtWidgets.QApplication.translate("pass", "Password:")) - self.warning.setText(QtWidgets.QApplication.translate("pass", "Incorrect password")) + self.setWindowTitle(util_ui.tr('Enter password')) + self.enter_pass.setText(util_ui.tr('Password:')) + self.warning.setText(util_ui.tr('Incorrect password')) class PasswordScreen(PasswordScreenBase): def __init__(self, encrypt, data): - super(PasswordScreen, self).__init__(encrypt) + super().__init__(encrypt) self._data = data def button_click(self): @@ -129,16 +130,15 @@ class SetProfilePasswordScreen(CenteredWidget): self.warning.setStyleSheet('QLabel { color: #BC1C1C; }') def retranslateUi(self): - self.setWindowTitle(QtWidgets.QApplication.translate("PasswordScreen", "Profile password")) + self.setWindowTitle(util_ui.tr('Profile password')) self.password.setPlaceholderText( - QtWidgets.QApplication.translate("PasswordScreen", "Password (at least 8 symbols)")) + util_ui.tr('Password (at least 8 symbols)')) self.confirm_password.setPlaceholderText( - QtWidgets.QApplication.translate("PasswordScreen", "Confirm password")) + util_ui.tr('Confirm password')) self.set_password.setText( - QtWidgets.QApplication.translate("PasswordScreen", "Set password")) - self.not_match.setText(QtWidgets.QApplication.translate("PasswordScreen", "Passwords do not match")) - self.warning.setText( - QtWidgets.QApplication.translate("PasswordScreen", "There is no way to recover lost passwords")) + util_ui.tr('Set password')) + self.not_match.setText(util_ui.tr('Passwords do not match')) + self.warning.setText(util_ui.tr('There is no way to recover lost passwords')) def new_password(self): if self.password.text() == self.confirm_password.text(): @@ -146,9 +146,8 @@ class SetProfilePasswordScreen(CenteredWidget): self._encrypt.set_password(self.password.text()) self.close() else: - self.not_match.setText( - QtWidgets.QApplication.translate("PasswordScreen", "Password must be at least 8 symbols")) + self.not_match.setText(util_ui.tr('Password must be at least 8 symbols')) self.not_match.setVisible(True) else: - self.not_match.setText(QtWidgets.QApplication.translate("PasswordScreen", "Passwords do not match")) + self.not_match.setText(util_ui.tr('Passwords do not match')) self.not_match.setVisible(True) diff --git a/toxygen/ui/views/create_profile_screen.ui b/toxygen/ui/views/create_profile_screen.ui index f9d3241..5407dca 100644 --- a/toxygen/ui/views/create_profile_screen.ui +++ b/toxygen/ui/views/create_profile_screen.ui @@ -7,19 +7,19 @@ 0 0 400 - 300 + 380 400 - 300 + 380 400 - 300 + 380 @@ -29,7 +29,7 @@ 30 - 240 + 290 341 51 @@ -47,6 +47,9 @@ 41 + + QLineEdit::Password + @@ -57,14 +60,17 @@ 41 + + QLineEdit::Password + 30 100 - 67 - 17 + 330 + 20 @@ -76,7 +82,7 @@ 30 10 - 112 + 330 23 @@ -92,7 +98,7 @@ 30 50 - 112 + 330 23 @@ -100,6 +106,22 @@ RadioButton + + + + 30 + 250 + 341 + 30 + + + + + + + Qt::AlignCenter + + diff --git a/toxygen/ui/views/login_screen.ui b/toxygen/ui/views/login_screen.ui new file mode 100644 index 0000000..a4c205c --- /dev/null +++ b/toxygen/ui/views/login_screen.ui @@ -0,0 +1,124 @@ + + + loginScreen + + + + 0 + 0 + 400 + 200 + + + + Form + + + + + 0 + 5 + 401 + 30 + + + + + Garuda + 16 + 75 + true + + + + Toxygen + + + Qt::AlignCenter + + + + + + 10 + 40 + 180 + 150 + + + + GroupBox + + + Qt::AlignCenter + + + + + 20 + 110 + 150 + 27 + + + + PushButton + + + + + + + 210 + 40 + 180 + 150 + + + + GroupBox + + + Qt::AlignCenter + + + + + 10 + 40 + 160 + 27 + + + + + + + 10 + 75 + 160 + 27 + + + + CheckBox + + + + + + 20 + 110 + 150 + 27 + + + + PushButton + + + + + + + diff --git a/toxygen/ui/widgets.py b/toxygen/ui/widgets.py index ec6c3e4..c25607b 100644 --- a/toxygen/ui/widgets.py +++ b/toxygen/ui/widgets.py @@ -52,7 +52,7 @@ class DialogWithResult(QtWidgets.QWidget): class LineEdit(QtWidgets.QLineEdit): def __init__(self, parent=None): - super(LineEdit, self).__init__(parent) + super().__init__(parent) def contextMenuEvent(self, event): menu = create_menu(self.createStandardContextMenu()) @@ -181,3 +181,16 @@ class MultilineEdit(CenteredWidget): def button_click(self): self.save(self.edit.toPlainText()) self.close() + + +class LineEditWithEnterSupport(LineEdit): + + def __init__(self, enter_action, parent=None): + super().__init__(parent) + self._action = enter_action + + def keyPressEvent(self, event): + if event.key() == QtCore.Qt.Key_Return: + self._action() + else: + super().keyPressEvent(event)