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