Skip to content

Commit

Permalink
Channel Master:adding save/load user settings using Qsettings #74
Browse files Browse the repository at this point in the history
  • Loading branch information
miquelcampos committed Apr 18, 2023
1 parent 4c165b3 commit 128073e
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 10 deletions.
40 changes: 39 additions & 1 deletion release/scripts/mgear/animbits/channel_master.py
Expand Up @@ -19,9 +19,12 @@
import importlib


class ChannelMaster(MayaQWidgetDockableMixin, QtWidgets.QDialog):
class ChannelMaster(
MayaQWidgetDockableMixin, QtWidgets.QDialog, pyqt.SettingsMixin
):
def __init__(self, parent=None):
super(ChannelMaster, self).__init__(parent)
pyqt.SettingsMixin.__init__(self)

self.setAttribute(QtCore.Qt.WA_DeleteOnClose, True)

Expand Down Expand Up @@ -64,6 +67,41 @@ def __init__(self, parent=None):

self.add_callback()

# Usert settings to store
# Create the user_settings dictionary with tuples containing the
# UI elements and their default values.
self.user_settings = {
"channelMaster_use_node_namespace_action": (
self.use_node_namespace_action,
True,
),
"channelMaster_use_only_local_data_action": (
self.use_only_local_data_action,
False,
),
"channelMaster_display_sync_graph_action": (
self.display_sync_graph_action,
False,
),
"channelMaster_display_fullname_action": (
self.display_fullname_action,
False,
),
"channelMaster_scrubbing_update_action": (
self.scrubbing_update_action,
False,
),
"channelMaster_key_all_tabs_action": (
self.key_all_tabs_action,
False,
),
"channelMaster_copypaste_all_channels_action": (
self.copypaste_all_channels_action,
False,
),
}
self.load_settings()

def add_callback(self):
self.cb_manager.selectionChangedCB(
"Channel_Master_selection_CB", self.selection_change
Expand Down
83 changes: 74 additions & 9 deletions release/scripts/mgear/core/pyqt.py
Expand Up @@ -8,11 +8,13 @@
import maya.OpenMayaUI as omui
import pymel.core as pm
from pymel import versions
from maya import cmds

from mgear.vendor.Qt import QtWidgets
from mgear.vendor.Qt import QtCompat
from mgear.vendor.Qt import QtGui
from mgear.vendor.Qt import QtSvg
from mgear.vendor.Qt import QtCore
from .six import PY2

UI_EXT = "ui"
Expand All @@ -34,6 +36,7 @@ def _qt_import(binding, shi=False, cui=False):
from PySide2 import QtGui, QtCore, QtWidgets
import shiboken2 as shiboken
from shiboken2 import wrapInstance

if versions.current() < 20220000:
from pyside2uic import compileUi

Expand All @@ -50,6 +53,7 @@ def _qt_import(binding, shi=False, cui=False):
import PyQt4.QtGui as QtWidgets
from sip import wrapinstance as wrapInstance
from PyQt4.uic import compileUi

print("Warning: 'shiboken' is not supported in 'PyQt4' Qt binding")
shiboken = None

Expand Down Expand Up @@ -104,8 +108,9 @@ def ui2py(filePath=None, *args):
dialogStyle=2,
fileMode=1,
startingDirectory=startDir,
fileFilter='PyQt Designer (*%s)' % UI_EXT,
okc="Compile to .py")
fileFilter="PyQt Designer (*%s)" % UI_EXT,
okc="Compile to .py",
)
if not filePath:
return False
filePath = filePath[0]
Expand All @@ -115,7 +120,7 @@ def ui2py(filePath=None, *args):
if not filePath.endswith(UI_EXT):
filePath += UI_EXT
compiledFilePath = filePath[:-2] + "py"
pyfile = open(compiledFilePath, 'w')
pyfile = open(compiledFilePath, "w")
compileUi(filePath, pyfile, False, 4, False)
pyfile.close()

Expand Down Expand Up @@ -200,7 +205,7 @@ def deleteInstances(dialog, checkinstance):
for obj in mayaMainWindow.children():
if isinstance(obj, checkinstance):
if obj.widget().objectName() == dialog.toolName:
print(('Deleting instance {0}'.format(obj)))
print(("Deleting instance {0}".format(obj)))
mayaMainWindow.removeDockWidget(obj)
obj.setParent(None)
obj.deleteLater()
Expand All @@ -217,7 +222,7 @@ def fakeTranslate(*args):


def position_window(window):
""" set the position for the windonw
"""set the position for the windonw
Function borrowed from Cesar Saez QuickLauncher
Args:
Expand Down Expand Up @@ -293,8 +298,7 @@ def get_top_level_widgets(class_name=None, object_name=None):


def get_icon_path(icon_name=None):
""" Gets the directory path to the icon
"""
"""Gets the directory path to the icon"""

file_dir = os.path.dirname(__file__)

Expand All @@ -308,8 +312,7 @@ def get_icon_path(icon_name=None):


def get_icon(icon, size=24):
"""get svg icon from icon resources folder as a pixel map
"""
"""get svg icon from icon resources folder as a pixel map"""
img = get_icon_path("{}.svg".format(icon))
svg_renderer = QtSvg.QSvgRenderer(img)
image = QtGui.QImage(size, size, QtGui.QImage.Format_ARGB32)
Expand Down Expand Up @@ -352,3 +355,65 @@ def dpi_scale(value, default=96, min_=1, max_=2):
# int, float: scaled value
"""
return value * max(min_, min(get_logicaldpi() / float(default), max_))


#############################################
# use QSettings store/load class
#############################################


class SettingsMixin(object):
def __init__(self, parent=None):
self.settings = self.create_qsettings_object()
self.user_settings = {}

def create_qsettings_object(self):
prefs_folder = cmds.internalVar(userPrefDir=True)
settings_file_path = os.path.join(
prefs_folder, "mGear_user_settings.ini"
)
return QtCore.QSettings(settings_file_path, QtCore.QSettings.IniFormat)

def load_settings(self):
for key, (widget, default_value) in self.user_settings.items():
value = self.settings.value(
key, defaultValue=default_value, type=type(default_value)
)
self._set_widget_value(widget, value)
self._connect_widget_signal(widget)

def save_settings(self):
for key, (widget, _) in self.user_settings.items():
value = self._get_widget_value(widget)
self.settings.setValue(key, value)
self.settings.sync()

def _get_widget_value(self, widget):
if isinstance(widget, (QtWidgets.QCheckBox, QtWidgets.QAction)):
return widget.isChecked()
elif isinstance(widget, QtWidgets.QComboBox):
return widget.currentIndex()
elif isinstance(widget, QtWidgets.QLineEdit):
return widget.text()
# Add support for other widget types as needed.
# ...

def _set_widget_value(self, widget, value):
if isinstance(widget, (QtWidgets.QCheckBox, QtWidgets.QAction)):
widget.setChecked(value)
elif isinstance(widget, QtWidgets.QComboBox):
widget.setCurrentIndex(int(value))
elif isinstance(widget, QtWidgets.QLineEdit):
widget.setText(value)
# Add support for other widget types as needed.
# ...

def _connect_widget_signal(self, widget):
if isinstance(widget, (QtWidgets.QCheckBox, QtWidgets.QAction)):
widget.toggled.connect(self.save_settings)
elif isinstance(widget, QtWidgets.QComboBox):
widget.currentIndexChanged.connect(self.save_settings)
elif isinstance(widget, QtWidgets.QLineEdit):
widget.textChanged.connect(self.save_settings)
# Add support for other widget types as needed.
# ...

0 comments on commit 128073e

Please sign in to comment.