From d9e159417ddd3ffaa81b2a368f8a3951d51fae06 Mon Sep 17 00:00:00 2001 From: Inso <insomniak.fr@gmail.com> Date: Mon, 11 May 2015 11:09:25 +0200 Subject: [PATCH] Adding preferences dialog ( Issue #53 ) --- res/ui/mainwindow.ui | 39 +++++++----- res/ui/preferences.ui | 106 ++++++++++++++++++++++++++++++++ src/cutecoin/core/app.py | 41 ++++++++++-- src/cutecoin/gui/mainwindow.py | 19 +++--- src/cutecoin/gui/preferences.py | 47 ++++++++++++++ 5 files changed, 219 insertions(+), 33 deletions(-) create mode 100644 res/ui/preferences.ui create mode 100644 src/cutecoin/gui/preferences.py diff --git a/res/ui/mainwindow.ui b/res/ui/mainwindow.ui index fa2f0717..5e41ed46 100644 --- a/res/ui/mainwindow.ui +++ b/res/ui/mainwindow.ui @@ -33,7 +33,7 @@ <x>0</x> <y>0</y> <width>681</width> - <height>31</height> + <height>29</height> </rect> </property> <widget class="QMenu" name="menu_account"> @@ -42,17 +42,17 @@ </property> <widget class="QMenu" name="menu_change_account"> <property name="title"> - <string>Open</string> + <string>&Open</string> </property> </widget> <addaction name="action_add_account"/> <addaction name="menu_change_account"/> <addaction name="action_configure_parameters"/> - <addaction name="action_set_as_default"/> <addaction name="separator"/> <addaction name="action_export"/> <addaction name="action_import"/> <addaction name="separator"/> + <addaction name="actionPreferences"/> <addaction name="actionAbout"/> <addaction name="action_quit"/> </widget> @@ -62,7 +62,7 @@ </property> <widget class="QMenu" name="menu_contacts_list"> <property name="title"> - <string>Contacts</string> + <string>&Contacts</string> </property> <addaction name="separator"/> </widget> @@ -94,7 +94,7 @@ </action> <action name="action_add_a_contact"> <property name="text"> - <string>Add a contact</string> + <string>&Add a contact</string> </property> </action> <action name="actionSend_a_message"> @@ -129,7 +129,7 @@ </action> <action name="action_add_account"> <property name="text"> - <string>Add</string> + <string>&Add</string> </property> </action> <action name="action_save"> @@ -139,7 +139,7 @@ </action> <action name="action_quit"> <property name="text"> - <string>Quit</string> + <string>&Quit</string> </property> </action> <action name="actionAccount"> @@ -149,37 +149,42 @@ </action> <action name="actionTransfer_money"> <property name="text"> - <string>Transfer money</string> + <string>&Transfer money</string> </property> </action> <action name="action_configure_parameters"> <property name="text"> - <string>Configure</string> + <string>&Configure</string> </property> </action> <action name="action_import"> <property name="text"> - <string>Import</string> + <string>&Import</string> </property> </action> <action name="action_export"> <property name="text"> - <string>Export</string> + <string>&Export</string> </property> </action> <action name="actionCertification"> <property name="text"> - <string>Certification</string> + <string>&Certification</string> </property> </action> <action name="action_set_as_default"> <property name="text"> - <string>Set as default</string> + <string>&Set as default</string> </property> </action> <action name="actionAbout"> <property name="text"> - <string>About</string> + <string>A&bout</string> + </property> + </action> + <action name="actionPreferences"> + <property name="text"> + <string>&Preferences</string> </property> </action> </widget> @@ -316,10 +321,10 @@ </hints> </connection> <connection> - <sender>action_set_as_default</sender> + <sender>actionPreferences</sender> <signal>triggered()</signal> <receiver>MainWindow</receiver> - <slot>set_as_default_account()</slot> + <slot>open_preferences_dialog()</slot> <hints> <hint type="sourcelabel"> <x>-1</x> @@ -358,7 +363,7 @@ <slot>refresh_wallet_content(QModelIndex)</slot> <slot>export_account()</slot> <slot>open_certification_dialog()</slot> - <slot>set_as_default_account()</slot> + <slot>open_preferences_dialog()</slot> <slot>open_about_popup()</slot> </slots> </ui> diff --git a/res/ui/preferences.ui b/res/ui/preferences.ui new file mode 100644 index 00000000..3db30d4f --- /dev/null +++ b/res/ui/preferences.ui @@ -0,0 +1,106 @@ +<?xml version="1.0" encoding="UTF-8"?> +<ui version="4.0"> + <class>PreferencesDialog</class> + <widget class="QDialog" name="PreferencesDialog"> + <property name="geometry"> + <rect> + <x>0</x> + <y>0</y> + <width>400</width> + <height>300</height> + </rect> + </property> + <property name="windowTitle"> + <string>Dialog</string> + </property> + <layout class="QVBoxLayout" name="verticalLayout"> + <item> + <layout class="QHBoxLayout" name="horizontalLayout"> + <item> + <widget class="QLabel" name="label"> + <property name="text"> + <string>Default account</string> + </property> + </widget> + </item> + <item> + <widget class="QComboBox" name="combo_account"/> + </item> + </layout> + </item> + <item> + <layout class="QHBoxLayout" name="horizontalLayout_3"> + <item> + <widget class="QLabel" name="label_3"> + <property name="text"> + <string>Default referential</string> + </property> + </widget> + </item> + <item> + <widget class="QComboBox" name="combo_referential"/> + </item> + </layout> + </item> + <item> + <layout class="QHBoxLayout" name="horizontalLayout_2"> + <item> + <widget class="QLabel" name="label_2"> + <property name="text"> + <string>Language</string> + </property> + </widget> + </item> + <item> + <widget class="QComboBox" name="combo_language"/> + </item> + </layout> + </item> + <item> + <widget class="QDialogButtonBox" name="buttonBox"> + <property name="orientation"> + <enum>Qt::Horizontal</enum> + </property> + <property name="standardButtons"> + <set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set> + </property> + </widget> + </item> + </layout> + </widget> + <resources/> + <connections> + <connection> + <sender>buttonBox</sender> + <signal>accepted()</signal> + <receiver>PreferencesDialog</receiver> + <slot>accept()</slot> + <hints> + <hint type="sourcelabel"> + <x>248</x> + <y>254</y> + </hint> + <hint type="destinationlabel"> + <x>157</x> + <y>274</y> + </hint> + </hints> + </connection> + <connection> + <sender>buttonBox</sender> + <signal>rejected()</signal> + <receiver>PreferencesDialog</receiver> + <slot>reject()</slot> + <hints> + <hint type="sourcelabel"> + <x>316</x> + <y>260</y> + </hint> + <hint type="destinationlabel"> + <x>286</x> + <y>274</y> + </hint> + </hints> + </connection> + </connections> +</ui> diff --git a/src/cutecoin/core/app.py b/src/cutecoin/core/app.py index 0eb10608..a2585666 100644 --- a/src/cutecoin/core/app.py +++ b/src/cutecoin/core/app.py @@ -42,7 +42,6 @@ class Application(QObject): ''' super().__init__() self.accounts = {} - self.default_account = "" self.current_account = None self.monitor = None self.available_version = __version__ @@ -50,6 +49,10 @@ class Application(QObject): self._network_manager = QNetworkAccessManager() self._network_manager.finished.connect(self.read_available_version) self.get_last_version() + self.preferences = {'account': "", + 'lang': 'English', + 'ref': 'Units' + } self.load() def get_account(self, name): @@ -125,12 +128,11 @@ class Application(QObject): no error is raised. ''' self.load_persons() + self.load_preferences() try: logging.debug("Loading data...") with open(config.parameters['data'], 'r') as json_data: data = json.load(json_data) - if 'default_account' in data.keys(): - self.default_account = data['default_account'] for account_name in data['local_accounts']: self.accounts[account_name] = None except FileNotFoundError: @@ -207,6 +209,36 @@ class Application(QObject): else: os.remove(wallet_path) + def load_preferences(self): + ''' + Load the preferences. + ''' + + try: + preferences_path = os.path.join(config.parameters['home'], + 'preferences') + with open(preferences_path, 'r') as json_data: + data = json.load(json_data) + self.preferences = data + except FileNotFoundError: + pass + + def save_preferences(self, preferences): + ''' + Save the preferences. + + :param preferences: A dict containing the keys/values of the preferences + ''' + assert('lang' in preferences) + assert('account' in preferences) + assert('ref' in preferences) + + self.preferences = preferences + preferences_path = os.path.join(config.parameters['home'], + 'preferences') + with open(preferences_path, 'w') as outfile: + json.dump(preferences, outfile, indent=4) + def save(self, account): ''' Save an account @@ -347,8 +379,7 @@ class Application(QObject): :return: The accounts of the app to format as json ''' - data = {'default_account': self.default_account, - 'local_accounts': self.jsonify_accounts()} + data = {'local_accounts': self.jsonify_accounts()} return data def get_last_version(self): diff --git a/src/cutecoin/gui/mainwindow.py b/src/cutecoin/gui/mainwindow.py index d3ad012c..4cb547ca 100644 --- a/src/cutecoin/gui/mainwindow.py +++ b/src/cutecoin/gui/mainwindow.py @@ -20,6 +20,7 @@ from .import_account import ImportAccountDialog from .certification import CertificationDialog from .password_asker import PasswordAskerDialog from ..tools.exceptions import NoPeerAvailable +from .preferences import PreferencesDialog from .homescreen import HomeScreenWidget from ..core.account import Account from ..__init__ import __version__ @@ -214,6 +215,10 @@ class MainWindow(QMainWindow, Ui_MainWindow): else: self.refresh() + def open_preferences_dialog(self): + dialog = PreferencesDialog(self.app) + result = dialog.exec_() + def open_about_popup(self): """ Open about popup window @@ -296,12 +301,6 @@ class MainWindow(QMainWindow, Ui_MainWindow): delete_action.setData(contact) delete_action.triggered.connect(self.delete_contact) - def set_as_default_account(self): - self.app.default_account = self.app.current_account.name - logging.debug(self.app.current_account) - self.app.save(self.app.current_account) - self.action_set_as_default.setEnabled(False) - def refresh(self): ''' Refresh main window @@ -327,8 +326,6 @@ class MainWindow(QMainWindow, Ui_MainWindow): self.currencies_tabwidget.show() logging.debug(self.tr("Hide homescreen")) self.homescreen.hide() - self.action_set_as_default.setEnabled(self.app.current_account.name - != self.app.default_account) self.password_asker = PasswordAskerDialog(self.app.current_account) self.combo_referential.blockSignals(True) @@ -336,7 +333,7 @@ class MainWindow(QMainWindow, Ui_MainWindow): self.combo_referential.addItems(sorted(Account.referentials.keys())) self.combo_referential.setEnabled(True) self.combo_referential.blockSignals(False) - self.combo_referential.setCurrentText(self.app.current_account.referential) + self.combo_referential.setCurrentText(self.app.preferences['ref']) self.menu_contacts.setEnabled(True) self.action_configure_parameters.setEnabled(True) self.menu_actions.setEnabled(True) @@ -382,7 +379,7 @@ class MainWindow(QMainWindow, Ui_MainWindow): def showEvent(self, event): super().showEvent(event) if not self.initialized: - if self.app.default_account != "": + if self.app.preferences['account'] != "": logging.debug("Loading default account") - self.action_change_account(self.app.default_account) + self.action_change_account(self.app.preferences['account']) self.initialized = True diff --git a/src/cutecoin/gui/preferences.py b/src/cutecoin/gui/preferences.py new file mode 100644 index 00000000..6e06a7cc --- /dev/null +++ b/src/cutecoin/gui/preferences.py @@ -0,0 +1,47 @@ +''' +Created on 11 mai 2015 + +@author: inso +''' + +import logging + +from ..core.account import Account +from PyQt5.QtWidgets import QDialog + +from ..gen_resources.preferences_uic import Ui_PreferencesDialog + + +class PreferencesDialog(QDialog, Ui_PreferencesDialog): + + ''' + A dialog to get password. + ''' + + def __init__(self, app): + ''' + Constructor + ''' + super().__init__() + self.setupUi(self) + self.app = app + self.combo_account.addItem("") + for account_name in self.app.accounts.keys(): + self.combo_account.addItem(account_name) + self.combo_account.setCurrentText(self.app.preferences['account']) + for ref in Account.referentials: + self.combo_referential.addItem(ref) + for lang in ('English', 'Français'): + self.combo_language.addItem(lang) + + def accept(self): + pref = {'account': self.combo_account.currentText(), + 'lang': self.combo_account.currentText(), + 'ref': self.combo_referential.currentText()} + self.app.save_preferences(pref) + super().accept() + + def reject(self): + super().reject() + + -- GitLab