Skip to content

Commit

Permalink
feat: build stuff, path stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
leocov-dev committed Oct 13, 2024
1 parent 5e77654 commit d31eda4
Show file tree
Hide file tree
Showing 13 changed files with 210 additions and 129 deletions.
12 changes: 4 additions & 8 deletions examples/toolbar-app-project/src/Toolbar App.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
import argparse
import os


def main():
from toolbar_app import run

run.run()


if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--style", choices=["mac", "win", "nix"])
Expand All @@ -22,4 +15,7 @@ def main():
case "nix":
os.environ["PLATFORM_OVERRIDE"] = "Linux"

main()
# this is nested in here to allow the platform overrides above
from toolbar_app.app import SimpleApp

SimpleApp().launch()
79 changes: 79 additions & 0 deletions examples/toolbar-app-project/src/toolbar_app/app.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,91 @@
from pathlib import Path

from loguru import logger

from pyside_app_core.app.application_service import AppMetadata
from pyside_app_core.errors import excepthook
from pyside_app_core.log import configure_get_logger_func
from pyside_app_core.services.preferences_service import PreferencesService, PrefGroup, PrefItem, PrefSection
from pyside_app_core.ui.prefs import ComboItemWidget, PathWithPlaceholder
from pyside_app_core.ui.standard.error_dialog import ErrorDialog
from pyside_app_core.ui.widgets.base_app import BaseApp
from pyside_app_core.ui.widgets.file_picker import FilePickerType
from toolbar_app import __version__
from toolbar_app.main_window import SimpleMainWindow

configure_get_logger_func(lambda: logger)

excepthook.install_excepthook(ErrorDialog)
AppMetadata.init(
"com.example.simple-app",
"Simple App",
__version__,
icon_resource=":/tb/app/icon.png",
help_url="https://github.com/leocov-dev/pyside-app-core",
bug_report_url="https://github.com/leocov-dev/pyside-app-core/issues",
)


class SimpleApp(BaseApp):
def __init__(self, resources_rcc: Path | None = None):
super().__init__(resources_rcc=resources_rcc)

PreferencesService.add_prefs(
PrefSection(
"Application",
"app",
[
PrefGroup(
"Keep Between Sessions",
"remember",
[
PrefItem.new("Remember Position", "pos", True),
PrefItem.new("Remember Size", "size", False),
PrefItem.new("Remember Number", "float-num", 12.34),
PrefItem.new(
"Special Choice",
"spec-choice",
1,
widget_class=ComboItemWidget(["One", "Two", "Three"]),
),
],
),
PrefGroup(
"Paths",
"paths",
[
PrefItem.new(
"Default Path",
"path",
Path(),
widget_class=PathWithPlaceholder(
placeholder_text="my placeholder...",
kind=FilePickerType.DIR,
),
),
PrefItem.new(
"Another Path", "another-path", Path.home() / "one" / "two" / "three" / "file.txt"
),
],
),
],
),
PrefGroup(
"Developer",
"dev",
[
PrefItem.new("Debug Mode", "debug", False),
PrefItem.new("Debug Format", "debug-fmt", "some-format-string"),
PrefItem.new("Debug Level", "debug-lvl", 0),
PrefItem.new(
"My Selection",
"my-sel",
0,
widget_class=ComboItemWidget(["A", "B", "C", "D", "E"]),
),
],
),
)

def build_main_window(self):
return SimpleMainWindow()
62 changes: 9 additions & 53 deletions examples/toolbar-app-project/src/toolbar_app/main_window.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
from pathlib import Path

from PySide6.QtCore import QSize
from PySide6.QtGui import QAction
from PySide6.QtWidgets import QLabel, QVBoxLayout

from pyside_app_core import log
from pyside_app_core.services.preferences_service import PreferencesService, PrefGroup, PrefItem, PrefSection
from pyside_app_core.ui.prefs.preferences_utility_widgets import ComboItemWidget
from pyside_app_core.services.preferences_service import PreferencesService
from pyside_app_core.ui.standard import MainWindow
from pyside_app_core.ui.widgets.connection_manager import ConnectionManager
from pyside_app_core.ui.widgets.core_icon import CoreIcon
Expand All @@ -22,55 +19,6 @@ def __init__(self) -> None:
# ------------------------------------------------------------------------------
self.setMinimumSize(QSize(480, 240))

PreferencesService.add_prefs(
PrefSection(
"Application",
"app",
[
PrefGroup(
"Keep Between Sessions",
"remember",
[
PrefItem.new("Remember Position", "pos", True),
PrefItem.new("Remember Size", "size", False),
PrefItem.new("Remember Number", "float-num", 12.34),
PrefItem.new(
"Special Choice",
"spec-choice",
1,
widget_class=ComboItemWidget(["One", "Two", "Three"]),
),
],
),
PrefGroup(
"Paths",
"paths",
[
PrefItem.new("Default Path", "path", Path.home() / "one" / "two" / "three" / "four"),
PrefItem.new(
"Another Path", "another-path", Path.home() / "one" / "two" / "three" / "file.txt"
),
],
),
],
),
PrefGroup(
"Developer",
"dev",
[
PrefItem.new("Debug Mode", "debug", False),
PrefItem.new("Debug Format", "debug-fmt", "some-format-string"),
PrefItem.new("Debug Level", "debug-lvl", 0),
PrefItem.new(
"My Selection",
"my-sel",
0,
widget_class=ComboItemWidget(["A", "B", "C", "D", "E"]),
),
],
),
)

log.debug(PreferencesService.instance())

self._menus()
Expand Down Expand Up @@ -135,6 +83,14 @@ def _raise() -> None:
):
pass

with _tool_bar.add_action(
"Clear Settings",
CoreIcon(
":/tb/icons/one-point-circle.svg",
),
) as clear_action:
clear_action.triggered.connect(lambda: PreferencesService.clear_all())

_tool_bar.add_stretch()

with _tool_bar.add_action(
Expand Down
26 changes: 0 additions & 26 deletions examples/toolbar-app-project/src/toolbar_app/run.py

This file was deleted.

7 changes: 2 additions & 5 deletions src/pyside_app_build/_pysidedeploy_spec_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,12 @@ def build_deploy_spec(

extra_args = [
"--quiet",
"--no-progressbar",
"--assume-yes-for-downloads",
"--noinclude-qt-translations",
"--static-libpython=no",
"--report=compilation-report.xml",
f"--include-package-data={','.join(extra_package_data)}" if extra_package_data else "",
f"--include-package-data={','.join(extra_data_dirs)}" if extra_data_dirs else "",
# *[f"{d}" for d in extra_package_data],
# *[f"--include-data-dir={d}" for d in extra_data_dirs]
*[f"--include-package-data={d}" for d in (extra_package_data or [])],
*[f"--include-data-dir={d}" for d in (extra_data_dirs or [])],
]

data: PySideSpec = {
Expand Down
4 changes: 4 additions & 0 deletions src/pyside_app_build/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,14 @@ def _clean(self) -> None:
def get_version_api(self) -> dict[str, Callable[..., str]]:
return {
"standard": self._build_standard,
"debug": self._build_debug,
}

# --------------------------------------------------------------------------

def _build_debug(self, _: str, **__: Any) -> str:
return self._build_standard(_, **__)

def _build_standard(self, _: str, **__: Any) -> str:
self._clean()

Expand Down
15 changes: 13 additions & 2 deletions src/pyside_app_core/services/preferences_service/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ def new(
value_item.setEditable(False)
value_item.setCheckable(True)
value_item.setCheckState(Qt.CheckState.Checked if default_value else Qt.CheckState.Unchecked)
if issubclass(_type, Path):
value_item.setText("" if default_value == Path() else str(default_value))
value_item.setData(None if default_value == Path() else default_value, Qt.ItemDataRole.UserRole)
else:
value_item.setText(str(default_value))
value_item.setData(default_value, Qt.ItemDataRole.UserRole)
Expand Down Expand Up @@ -104,8 +107,11 @@ def type_(self) -> type:
@property
def value(self) -> Any:
if issubclass(self.type_, bool):
return Qt.CheckState(self.value_item.checkState()) == Qt.CheckState.Checked
return self.value_item.data(Qt.ItemDataRole.UserRole)
value = Qt.CheckState(self.value_item.checkState()) == Qt.CheckState.Checked
else:
value = self.value_item.data(Qt.ItemDataRole.UserRole)
log.debug(f"PrefItem.value -> {value}")
return value

@property
def fqdn(self) -> str:
Expand All @@ -121,6 +127,7 @@ def widget_class(self) -> type[ItemWidgetInterface]:
)

def set_value(self, value: Any) -> None:
log.debug(f"PrefItem.set_value({value})")
if issubclass(self.type_, bool):
self.value_item.setCheckState(Qt.CheckState.Checked if value else Qt.CheckState.Unchecked)
else:
Expand Down Expand Up @@ -209,6 +216,10 @@ def __iter__(self) -> Iterator[PrefSection | PrefGroup]:
self.item(row, 0),
)

def clear_all_prefs(self) -> None:
log.warning("Clearing all settings...")
self._settings.clear()

def appendRows(self, items: Sequence[PrefSection | PrefGroup]) -> None:
for item in items:
self.invisibleRootItem().appendRow(item)
Expand Down
4 changes: 4 additions & 0 deletions src/pyside_app_core/services/preferences_service/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ def model(cls) -> PreferencesModel:
def fqdn_to_pref(cls, fqdn: str) -> PrefItem | PrefGroup | PrefSection | None:
return cls.instance()._fqdn_to_item(fqdn)

@classmethod
def clear_all(cls) -> None:
cls.instance()._model.clear_all_prefs()

def __init__(self) -> None:
super().__init__(parent=QCoreApplication.instance())

Expand Down
4 changes: 4 additions & 0 deletions src/pyside_app_core/ui/prefs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
_PathItemWidget,
_StringItemWidget,
)
from pyside_app_core.ui.prefs.preferences_utility_widgets import (
ComboItemWidget,
PathWithPlaceholder,
)


def auto(_type: type) -> type[ItemWidget]:
Expand Down
19 changes: 12 additions & 7 deletions src/pyside_app_core/ui/prefs/preferences_default_widgets.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from pathlib import Path
from typing import Any, Protocol

from PySide6.QtWidgets import (
Expand Down Expand Up @@ -81,14 +82,14 @@ def __init__(self, item: PrefItemInterface, parent: QWidget | None = None):


class _PathItemWidget(ItemWidget):
def __init__(self, item: PrefItemInterface, parent: QWidget | None = None):
super().__init__(item, parent)

path = item.value
@classmethod
def make_config(cls, path: Path | None) -> DirConfig | FileConfig:
config: DirConfig | FileConfig
if path.is_dir():
if path and path.is_dir():
config = DirConfig(
caption="Pick a directory", starting_directory=path, options=QFileDialog.Option.ShowDirsOnly
caption="Pick a directory",
starting_directory=path,
options=QFileDialog.Option.ShowDirsOnly,
)
else:
config = FileConfig(
Expand All @@ -97,9 +98,13 @@ def __init__(self, item: PrefItemInterface, parent: QWidget | None = None):
options=QFileDialog.Option.ReadOnly,
selection_filter="All Files (*)",
)
return config

def __init__(self, item: PrefItemInterface[Path], parent: QWidget | None = None):
super().__init__(item, parent)

fp = FilePicker(
config=config,
config=self.make_config(item.value),
parent=self,
)
fp.set_file_path(item.value)
Expand Down
Loading

0 comments on commit d31eda4

Please sign in to comment.