Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #18 Added support for application settings
- Loading branch information
1 parent
f0e0352
commit ac66ce9
Showing
10 changed files
with
195 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
{ | ||
"files": ["main.py","form.ui","src/friendlypics2/data/ui/main_window.ui","src/friendlypics2/data/ui/form.ui","pinterest_dump.ui","src/friendlypics2/data/ui/pinterest_dump.ui","src/friendlypics2/data/ui/about_dlg.ui"] | ||
"files": ["src/friendlypics2/data/ui/form.ui","src/friendlypics2/data/ui/pinterest_dump.ui","pinterest_dump.ui","src/friendlypics2/data/ui/about_dlg.ui","src/friendlypics2/data/ui/main_window.ui","main.py","form.ui","src/friendlypics2/data/ui/settings_dlg.ui"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,8 @@ | |
"qtpy", | ||
"pyside2", | ||
"friendlypins", | ||
"pyyaml", | ||
"appdirs" | ||
], | ||
"DEV_DEPENDENCIES" : [ | ||
"pytest", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<ui version="4.0"> | ||
<class>settings_dialog</class> | ||
<widget class="QDialog" name="settings_dialog"> | ||
<property name="windowModality"> | ||
<enum>Qt::ApplicationModal</enum> | ||
</property> | ||
<property name="geometry"> | ||
<rect> | ||
<x>0</x> | ||
<y>0</y> | ||
<width>507</width> | ||
<height>451</height> | ||
</rect> | ||
</property> | ||
<property name="windowTitle"> | ||
<string>Settings</string> | ||
</property> | ||
<property name="modal"> | ||
<bool>true</bool> | ||
</property> | ||
<layout class="QVBoxLayout" name="verticalLayout"> | ||
<item> | ||
<widget class="QTreeView" name="settings_view"/> | ||
</item> | ||
<item> | ||
<layout class="QHBoxLayout" name="horizontalLayout"> | ||
<item> | ||
<widget class="QPushButton" name="cancel_button"> | ||
<property name="sizePolicy"> | ||
<sizepolicy hsizetype="Fixed" vsizetype="Fixed"> | ||
<horstretch>0</horstretch> | ||
<verstretch>0</verstretch> | ||
</sizepolicy> | ||
</property> | ||
<property name="text"> | ||
<string>&Cancel</string> | ||
</property> | ||
<property name="autoDefault"> | ||
<bool>false</bool> | ||
</property> | ||
<property name="default"> | ||
<bool>true</bool> | ||
</property> | ||
</widget> | ||
</item> | ||
<item> | ||
<widget class="QPushButton" name="save_button"> | ||
<property name="sizePolicy"> | ||
<sizepolicy hsizetype="Fixed" vsizetype="Fixed"> | ||
<horstretch>0</horstretch> | ||
<verstretch>0</verstretch> | ||
</sizepolicy> | ||
</property> | ||
<property name="text"> | ||
<string>&Save</string> | ||
</property> | ||
</widget> | ||
</item> | ||
</layout> | ||
</item> | ||
</layout> | ||
</widget> | ||
<resources/> | ||
<connections/> | ||
</ui> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
"""Logic for the application settings UI""" | ||
import logging | ||
from qtpy.QtWidgets import QDialog | ||
from qtpy.QtCore import Slot, QSettings | ||
from friendlypics2.misc.gui_helpers import load_ui | ||
|
||
|
||
class SettingsDialog(QDialog): | ||
"""Logic for managing application settings dialog""" | ||
def __init__(self, parent, settings): | ||
super().__init__(parent) | ||
self._settings = settings | ||
self._log = logging.getLogger(__name__) | ||
self.settings = QSettings() | ||
self._load_ui() | ||
|
||
def _load_ui(self): | ||
"""Internal helper method that configures the UI for the dialog""" | ||
load_ui("settings_dlg.ui", self) | ||
|
||
self.cancel_button.clicked.connect(self.close) | ||
self.save_button.clicked.connect(self._save_clicked) | ||
|
||
# TODO: populate the settings tree view | ||
# TODO: make a custom "model" class to render the AppSettings class | ||
self.settings_view.add_item | ||
|
||
# Center the about box on the parent window | ||
parent_geom = self.parent().geometry() | ||
self.move(parent_geom.center() - self.rect().center()) | ||
|
||
@Slot() | ||
def _save_clicked(self): | ||
"""Callback for when the user clicks the save button""" | ||
self._log.debug("Saving") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
"""Interface for persisting application settings""" | ||
import logging | ||
from pathlib import Path | ||
import json | ||
|
||
import yaml | ||
from appdirs import user_config_dir | ||
from friendlypics2.version import __version__ | ||
|
||
|
||
class AppSettings: | ||
"""Interface for accessing and persisting application setings""" | ||
def __init__(self): | ||
self._log = logging.getLogger(__name__) | ||
temp = user_config_dir("Friendly Pics 2", "The Friendly Coder", __version__) | ||
self._filename = Path(temp).joinpath("appsettings.yml") | ||
self._log.debug(self._filename) | ||
|
||
if self._filename.exists(): | ||
self._data = yaml.safe_load(self._filename.read_text()) | ||
else: | ||
self._data = dict() | ||
self._data["file_version"] = "1.0" | ||
|
||
def __str__(self): | ||
return json.dumps(self._data, indent=4) | ||
|
||
@property | ||
def path(self): | ||
"""Location of the config file managed by this class""" | ||
return self._filename | ||
|
||
@property | ||
def pinterest_user(self): | ||
"""str: user to authenticate with to Pinterest""" | ||
return self._data.get("services", dict()).get("pinterest", dict()).get("username") | ||
|
||
@pinterest_user.setter | ||
def pinterest_user(self, value): | ||
# TODO: consider saving config data every time a setter is accessed | ||
self._data["services"]["pinterest"]["username"] = value | ||
|
||
@property | ||
def file_version(self): | ||
"""str: gets the schema version for the config file""" | ||
assert "file_version" in self._data | ||
return self._data.get("file_version") | ||
|
||
def save(self): | ||
"""Saves the current contents of the app settings for later reference""" | ||
with self._filename.open("w") as config_file: | ||
yaml.safe_dump(self._data, config_file) |