1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
|
# coding: utf-8
# /*##########################################################################
# Copyright (C) 2016-2018 European Synchrotron Radiation Facility
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# ############################################################################*/
"""Context shared through all the application"""
__authors__ = ["V. Valls"]
__license__ = "MIT"
__date__ = "10/05/2019"
import weakref
import logging
import functools
import os
from silx.gui import qt
from ..utils import stringutil
_logger = logging.getLogger(__name__)
class ApplicationContext(object):
__instance = None
@staticmethod
def _releaseSingleton():
ApplicationContext.__instance = None
@staticmethod
def instance():
"""
:rtype: CalibrationContext
"""
assert(ApplicationContext.__instance is not None)
return ApplicationContext.__instance
def __init__(self, settings=None):
assert(ApplicationContext.__instance is None)
self.__parent = None
self.__dialogStates = {}
self.__dialogGeometry = {}
self.__settings = settings
ApplicationContext.__instance = self
def saveSettings(self):
"""Save the settings of all the application"""
# Synchronize the file storage
self.__settings.sync()
def restoreWindowLocationSettings(self, groupName, window):
"""Restore the window settings using this settings object
:param qt.QSettings settings: Initialized settings
"""
settings = self.__settings
if settings is None:
_logger.debug("Settings not set")
return
settings.beginGroup(groupName)
size = settings.value("size", qt.QSize())
pos = settings.value("pos", qt.QPoint())
isFullScreen = settings.value("full-screen", False)
try:
if not isinstance(isFullScreen, bool):
isFullScreen = stringutil.to_bool(isFullScreen)
except ValueError:
isFullScreen = False
settings.endGroup()
if not pos.isNull():
window.move(pos)
if not size.isNull():
window.resize(size)
if isFullScreen:
window.showFullScreen()
def saveWindowLocationSettings(self, groupName, window):
"""Save the window settings to this settings object
:param qt.QSettings settings: Initialized settings
"""
settings = self.__settings
if settings is None:
_logger.debug("Settings not set")
return
isFullScreen = bool(window.windowState() & qt.Qt.WindowFullScreen)
if isFullScreen:
# show in normal to catch the normal geometry
window.showNormal()
settings.beginGroup(groupName)
settings.setValue("size", window.size())
settings.setValue("pos", window.pos())
settings.setValue("full-screen", isFullScreen)
settings.endGroup()
if isFullScreen:
window.showFullScreen()
def setParent(self, parent):
self.__parent = weakref.ref(parent)
def parent(self):
if self.__parent is None:
return None
return self.__parent()
def __configureDialog(self, dialog):
dialogState = self.__dialogStates.get(type(dialog), None)
if dialogState is None:
currentDirectory = os.getcwd()
dialog.setDirectory(currentDirectory)
else:
dialog.restoreState(dialogState)
geometry = self.__dialogGeometry.get(type(dialog), None)
if geometry is not None:
dialog.setGeometry(geometry)
def __saveDialogState(self, dialog):
self.__dialogStates[type(dialog)] = dialog.saveState()
self.__dialogGeometry[type(dialog)] = dialog.geometry()
def createFileDialog(self, parent, previousFile=None):
"""Create a file dialog configured with a default path.
:rtype: qt.QFileDialog
"""
dialog = qt.QFileDialog(parent)
dialog.finished.connect(functools.partial(self.__saveDialogState, dialog))
self.__configureDialog(dialog)
if previousFile is not None:
if os.path.exists(previousFile):
if os.path.isdir(previousFile):
directory = previousFile
else:
directory = os.path.dirname(previousFile)
dialog.setDirectory(directory)
return dialog
def createImageFileDialog(self, parent, previousFile=None):
"""Create an image file dialog configured with a default path.
:rtype: silx.gui.dialog.ImageFileDialog.ImageFileDialog
"""
from silx.gui.dialog.ImageFileDialog import ImageFileDialog
dialog = ImageFileDialog(parent)
dialog.finished.connect(functools.partial(self.__saveDialogState, dialog))
if hasattr(self, "getRawColormap"):
colormap = self.getRawColormap()
colormap = colormap.copy()
colormap.setVRange(None, None)
dialog.setColormap(colormap)
self.__configureDialog(dialog)
if previousFile is not None:
dialog.selectUrl(previousFile)
return dialog
|