Skip to content
This repository has been archived by the owner on Sep 20, 2024. It is now read-only.

Commit

Permalink
extraction can be called on whole settings
Browse files Browse the repository at this point in the history
  • Loading branch information
iLLiCiTiT committed Jun 10, 2022
1 parent c77f9ce commit e21caf1
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 45 deletions.
108 changes: 65 additions & 43 deletions openpype/tools/settings/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,70 @@

from .widgets import ExpandingWidget
from .lib import create_deffered_value_change_timer
from .constants import DEFAULT_PROJECT_LABEL
from .constants import (
DEFAULT_PROJECT_LABEL,
SETTINGS_PATH_KEY,
ROOT_KEY,
VALUE_KEY,
SAVE_TIME_KEY,
PROJECT_NAME_KEY,
)

_MENU_SEPARATOR_REQ = object()
SETTINGS_PATH_KEY = "__settings_path__"
ROOT_KEY = "__root_key__"
VALUE_KEY = "__value__"
SAVE_TIME_KEY = "__extracted__"
PROJECT_NAME_KEY = "__project_name__"


class BaseWidget(QtWidgets.QWidget):
class ExtractHelper:
_last_save_dir = os.path.expanduser("~")
allow_actions = True

@staticmethod
def get_last_save_dir():
return BaseWidget._last_save_dir
@classmethod
def get_last_save_dir(cls):
return cls._last_save_dir

@staticmethod
def set_last_save_dir(save_dir):
BaseWidget._last_save_dir = save_dir
@classmethod
def set_last_save_dir(cls, save_dir):
cls._last_save_dir = save_dir

@classmethod
def ask_for_save_filepath(cls, parent):
dialog = QtWidgets.QFileDialog(
parent,
"Save settings values",
cls.get_last_save_dir(),
"Values (*.json)"
)
# dialog.setOption(dialog.DontUseNativeDialog)
dialog.setAcceptMode(dialog.AcceptSave)
if dialog.exec() != dialog.Accepted:
return

selected_urls = dialog.selectedUrls()
if not selected_urls:
return

filepath = selected_urls[0].toLocalFile()
if not filepath:
return

if not filepath.lower().endswith(".json"):
filepath += ".json"
return filepath

@classmethod
def extract_settings_to_json(cls, filepath, settings_data, project_name):
now = datetime.datetime.now()
settings_data[SAVE_TIME_KEY] = now.strftime("%Y-%m-%d %H:%M:%S")
if project_name != 0:
settings_data[PROJECT_NAME_KEY] = project_name

with open(filepath, "w") as stream:
json.dump(settings_data, stream, indent=4)

new_dir = os.path.dirname(filepath)
cls.set_last_save_dir(new_dir)


class BaseWidget(QtWidgets.QWidget):
allow_actions = True

def __init__(self, category_widget, entity, entity_widget):
self.category_widget = category_widget
Expand Down Expand Up @@ -266,45 +309,24 @@ def copy_value():
return [(action, copy_value)]

def _extract_to_file(self):
dialog = QtWidgets.QFileDialog(
self,
"Save settings values",
self.get_last_save_dir(),
"Values (*.json)"
)
# dialog.setOption(dialog.DontUseNativeDialog)
dialog.setAcceptMode(dialog.AcceptSave)
if dialog.exec() != dialog.Accepted:
return

selected_urls = dialog.selectedUrls()
if not selected_urls:
return

filepath = selected_urls[0].toLocalFile()
filepath = ExtractHelper.ask_for_save_filepath(self)
if not filepath:
return

if not filepath.lower().endswith(".json"):
filepath += ".json"

settings_data = self._get_mime_data_from_entity()
now = datetime.datetime.now()
settings_data[SAVE_TIME_KEY] = now.strftime("%Y-%m-%d %H:%M:%S")
project_name = 0
if hasattr(self.category_widget, "project_name"):
settings_data[PROJECT_NAME_KEY] = self.category_widget.project_name
project_name = self.category_widget.project_name

with open(filepath, "w") as stream:
json.dump(settings_data, stream, indent=4)

new_dir = os.path.dirname(filepath)
self.set_last_save_dir(new_dir)
ExtractHelper.extract_settings_to_json(
filepath, settings_data, project_name
)

def _extract_value_to_file_actions(self, menu):
save_action = QtWidgets.QAction("Extract to file", menu)
extract_action = QtWidgets.QAction("Extract to file", menu)
return [
_MENU_SEPARATOR_REQ,
(save_action, self._extract_to_file)
(extract_action, self._extract_to_file)
]

def _parse_source_data_for_paste(self, data):
Expand Down
35 changes: 33 additions & 2 deletions openpype/tools/settings/settings/categories.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,15 @@
SystemSettingsBreadcrumbs,
ProjectSettingsBreadcrumbs
)

from .base import GUIWidget
from .constants import (
SETTINGS_PATH_KEY,
ROOT_KEY,
VALUE_KEY,
)
from .base import (
ExtractHelper,
GUIWidget,
)
from .list_item_widget import ListWidget
from .list_strict_widget import ListStrictWidget
from .dict_mutable_widget import DictMutableKeysWidget
Expand Down Expand Up @@ -627,11 +634,35 @@ def add_context_actions(self, menu):
self._on_context_version_trigger
)
submenu.addAction(action)

menu.addMenu(submenu)

extract_action = QtWidgets.QAction("Extract to file", menu)
extract_action.triggered.connect(self._on_extract_to_file)

menu.addAction(extract_action)

def _on_context_version_trigger(self, version):
self._on_source_version_change(version)

def _on_extract_to_file(self):
filepath = ExtractHelper.ask_for_save_filepath(self)
if not filepath:
return

settings_data = {
SETTINGS_PATH_KEY: self.entity.root_key,
ROOT_KEY: self.entity.root_key,
VALUE_KEY: self.entity.value
}
project_name = 0
if hasattr(self, "project_name"):
project_name = self.project_name

ExtractHelper.extract_settings_to_json(
filepath, settings_data, project_name
)

def _on_reset_crash(self):
self.save_btn.setEnabled(False)

Expand Down
13 changes: 13 additions & 0 deletions openpype/tools/settings/settings/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
PROJECT_IS_SELECTED_ROLE = QtCore.Qt.UserRole + 3
PROJECT_VERSION_ROLE = QtCore.Qt.UserRole + 4

# Save/Extract keys
SETTINGS_PATH_KEY = "__settings_path__"
ROOT_KEY = "__root_key__"
VALUE_KEY = "__value__"
SAVE_TIME_KEY = "__extracted__"
PROJECT_NAME_KEY = "__project_name__"

__all__ = (
"DEFAULT_PROJECT_LABEL",
Expand All @@ -15,4 +21,11 @@
"PROJECT_IS_ACTIVE_ROLE",
"PROJECT_IS_SELECTED_ROLE",
"PROJECT_VERSION_ROLE",

"SETTINGS_PATH_KEY",
"ROOT_KEY",
"SETTINGS_PATH_KEY",
"VALUE_KEY",
"SAVE_TIME_KEY",
"PROJECT_NAME_KEY",
)

0 comments on commit e21caf1

Please sign in to comment.