2015-11-08 21:44:02 +01:00
|
|
|
#!/usr/bin/python2.7
|
2018-10-04 16:11:56 +02:00
|
|
|
"""
|
|
|
|
src/settingsmixin.py
|
|
|
|
====================
|
|
|
|
|
|
|
|
"""
|
2015-11-08 21:44:02 +01:00
|
|
|
|
|
|
|
from PyQt4 import QtCore, QtGui
|
|
|
|
|
2018-10-04 16:11:56 +02:00
|
|
|
|
2015-11-08 21:44:02 +01:00
|
|
|
class SettingsMixin(object):
|
2018-10-04 16:11:56 +02:00
|
|
|
"""Mixin for adding geometry and state saving between restarts."""
|
2015-11-08 21:44:02 +01:00
|
|
|
def warnIfNoObjectName(self):
|
2018-10-04 16:11:56 +02:00
|
|
|
"""
|
|
|
|
Handle objects which don't have a name. Currently it ignores them. Objects without a name can't have their
|
|
|
|
state/geometry saved as they don't have an identifier.
|
|
|
|
"""
|
2015-11-08 21:44:02 +01:00
|
|
|
if self.objectName() == "":
|
2018-10-04 16:11:56 +02:00
|
|
|
# .. todo:: logger
|
2015-11-08 21:44:02 +01:00
|
|
|
pass
|
2018-10-04 16:11:56 +02:00
|
|
|
|
2015-11-08 21:44:02 +01:00
|
|
|
def writeState(self, source):
|
2018-10-04 16:11:56 +02:00
|
|
|
"""Save object state (e.g. relative position of a splitter)"""
|
2015-11-08 21:44:02 +01:00
|
|
|
self.warnIfNoObjectName()
|
|
|
|
settings = QtCore.QSettings()
|
|
|
|
settings.beginGroup(self.objectName())
|
|
|
|
settings.setValue("state", source.saveState())
|
|
|
|
settings.endGroup()
|
2018-10-04 16:11:56 +02:00
|
|
|
|
2015-11-08 21:44:02 +01:00
|
|
|
def writeGeometry(self, source):
|
2018-10-04 16:11:56 +02:00
|
|
|
"""Save object geometry (e.g. window size and position)"""
|
2015-11-08 21:44:02 +01:00
|
|
|
self.warnIfNoObjectName()
|
|
|
|
settings = QtCore.QSettings()
|
|
|
|
settings.beginGroup(self.objectName())
|
|
|
|
settings.setValue("geometry", source.saveGeometry())
|
|
|
|
settings.endGroup()
|
2018-10-04 16:11:56 +02:00
|
|
|
|
2015-11-08 21:44:02 +01:00
|
|
|
def readGeometry(self, target):
|
2018-10-04 16:11:56 +02:00
|
|
|
"""Load object geometry"""
|
2015-11-08 21:44:02 +01:00
|
|
|
self.warnIfNoObjectName()
|
|
|
|
settings = QtCore.QSettings()
|
|
|
|
try:
|
|
|
|
geom = settings.value("/".join([str(self.objectName()), "geometry"]))
|
|
|
|
target.restoreGeometry(geom.toByteArray() if hasattr(geom, 'toByteArray') else geom)
|
2018-10-04 16:11:56 +02:00
|
|
|
except Exception:
|
2015-11-08 21:44:02 +01:00
|
|
|
pass
|
|
|
|
|
|
|
|
def readState(self, target):
|
2018-10-04 16:11:56 +02:00
|
|
|
"""Load object state"""
|
2015-11-08 21:44:02 +01:00
|
|
|
self.warnIfNoObjectName()
|
|
|
|
settings = QtCore.QSettings()
|
|
|
|
try:
|
|
|
|
state = settings.value("/".join([str(self.objectName()), "state"]))
|
|
|
|
target.restoreState(state.toByteArray() if hasattr(state, 'toByteArray') else state)
|
2018-10-04 16:11:56 +02:00
|
|
|
except Exception:
|
2015-11-08 21:44:02 +01:00
|
|
|
pass
|
|
|
|
|
2018-10-04 16:11:56 +02:00
|
|
|
|
2015-11-08 21:44:02 +01:00
|
|
|
class SMainWindow(QtGui.QMainWindow, SettingsMixin):
|
2018-10-04 16:11:56 +02:00
|
|
|
"""Main window with Settings functionality."""
|
2015-11-08 21:44:02 +01:00
|
|
|
def loadSettings(self):
|
2018-10-04 16:11:56 +02:00
|
|
|
"""Load main window settings."""
|
2015-11-08 21:44:02 +01:00
|
|
|
self.readGeometry(self)
|
|
|
|
self.readState(self)
|
2018-10-04 16:11:56 +02:00
|
|
|
|
2015-11-08 21:44:02 +01:00
|
|
|
def saveSettings(self):
|
2018-10-04 16:11:56 +02:00
|
|
|
"""Save main window settings"""
|
2015-11-08 21:44:02 +01:00
|
|
|
self.writeState(self)
|
|
|
|
self.writeGeometry(self)
|
|
|
|
|
|
|
|
|
|
|
|
class STableWidget(QtGui.QTableWidget, SettingsMixin):
|
2018-10-04 16:11:56 +02:00
|
|
|
"""Table widget with Settings functionality"""
|
|
|
|
# pylint: disable=too-many-ancestors
|
2015-11-08 21:44:02 +01:00
|
|
|
def loadSettings(self):
|
2018-10-04 16:11:56 +02:00
|
|
|
"""Load table settings."""
|
2015-11-08 21:44:02 +01:00
|
|
|
self.readState(self.horizontalHeader())
|
|
|
|
|
|
|
|
def saveSettings(self):
|
2018-10-04 16:11:56 +02:00
|
|
|
"""Save table settings."""
|
2015-11-08 21:44:02 +01:00
|
|
|
self.writeState(self.horizontalHeader())
|
|
|
|
|
2018-10-04 16:11:56 +02:00
|
|
|
|
2015-11-08 21:44:02 +01:00
|
|
|
class SSplitter(QtGui.QSplitter, SettingsMixin):
|
2018-10-04 16:11:56 +02:00
|
|
|
"""Splitter with Settings functionality."""
|
2015-11-08 21:44:02 +01:00
|
|
|
def loadSettings(self):
|
2018-10-04 16:11:56 +02:00
|
|
|
"""Load splitter settings"""
|
2015-11-08 21:44:02 +01:00
|
|
|
self.readState(self)
|
|
|
|
|
|
|
|
def saveSettings(self):
|
2018-10-04 16:11:56 +02:00
|
|
|
"""Save splitter settings."""
|
2015-11-12 00:33:57 +01:00
|
|
|
self.writeState(self)
|
2018-10-04 16:11:56 +02:00
|
|
|
|
2015-11-12 00:33:57 +01:00
|
|
|
|
|
|
|
class STreeWidget(QtGui.QTreeWidget, SettingsMixin):
|
2018-10-04 16:11:56 +02:00
|
|
|
"""Tree widget with settings functionality."""
|
|
|
|
# pylint: disable=too-many-ancestors
|
2015-11-12 00:33:57 +01:00
|
|
|
def loadSettings(self):
|
2018-10-04 16:11:56 +02:00
|
|
|
"""Load tree settings."""
|
|
|
|
# recurse children
|
|
|
|
# self.readState(self)
|
2015-11-12 00:33:57 +01:00
|
|
|
pass
|
|
|
|
|
|
|
|
def saveSettings(self):
|
2018-10-04 16:11:56 +02:00
|
|
|
"""Save tree settings"""
|
|
|
|
# recurse children
|
|
|
|
# self.writeState(self)
|
2015-11-12 00:33:57 +01:00
|
|
|
pass
|