/qt/applications/workbench/workbench/config/__init__.py

https://github.com/mantidproject/mantid · Python · 127 lines · 61 code · 18 blank · 48 comment · 9 complexity · 45be7d8f5c8a3077a2d5c394e78c234e MD5 · raw file

  1. # Mantid Repository : https://github.com/mantidproject/mantid
  2. #
  3. # Copyright © 2017 ISIS Rutherford Appleton Laboratory UKRI,
  4. # NScD Oak Ridge National Laboratory, European Spallation Source,
  5. # Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
  6. # SPDX - License - Identifier: GPL - 3.0 +
  7. # This file is part of the mantid workbench.
  8. #
  9. #
  10. """ Main configuration module.
  11. A singleton instance called CONF is defined. Modules wishing to access the settings
  12. should import the CONF object as
  13. from workbench.config import CONF
  14. and use it to access the settings
  15. """
  16. # std imports
  17. from collections import namedtuple
  18. import os
  19. import sys
  20. # third-party imports
  21. from qtpy.QtCore import Qt, QSettings
  22. # local imports
  23. from .user import UserConfig
  24. # Type to hold properties for additional QMainWindow instances
  25. WindowConfig = namedtuple("WindowConfig", ("parent", "flags"))
  26. # -----------------------------------------------------------------------------
  27. # "Private" Constants
  28. # -----------------------------------------------------------------------------
  29. # Default parent for additional QMainWindow instances
  30. _ADDITIONAL_MAINWINDOWS_PARENT = None
  31. # -----------------------------------------------------------------------------
  32. # Public Constants
  33. # -----------------------------------------------------------------------------
  34. # The strings APPNAME, ORG_DOMAIN, ORGANIZATION are duplicated
  35. # in mantidqt/dialogs/errorreports/main.py
  36. ORGANIZATION = 'mantidproject'
  37. ORG_DOMAIN = 'mantidproject.org'
  38. APPNAME = 'mantidworkbench'
  39. DEFAULT_SCRIPT_CONTENT = ""
  40. DEFAULT_SCRIPT_CONTENT += "# import mantid algorithms, numpy and matplotlib" + os.linesep + \
  41. "from mantid.simpleapi import *" + os.linesep + \
  42. "import matplotlib.pyplot as plt" + os.linesep + \
  43. "import numpy as np" + os.linesep + os.linesep
  44. # Flags defining a standard Window
  45. WINDOW_STANDARD_FLAGS = Qt.WindowFlags(Qt.Window)
  46. # Flags defining our meaning of keeping figure windows on top.
  47. # On Windows the standard Qt.Window flags + setting a parent keeps the widget on top
  48. # Other OSs use the Qt.Tool type with a close button
  49. if sys.platform == 'win32':
  50. WINDOW_ONTOP_FLAGS = WINDOW_STANDARD_FLAGS
  51. elif sys.platform == 'darwin':
  52. WINDOW_ONTOP_FLAGS = (Qt.Tool | Qt.CustomizeWindowHint | Qt.WindowCloseButtonHint
  53. | Qt.WindowMinimizeButtonHint)
  54. else:
  55. WINDOW_ONTOP_FLAGS = (Qt.Tool | Qt.CustomizeWindowHint | Qt.WindowCloseButtonHint
  56. | Qt.WindowMinimizeButtonHint)
  57. # Iterable containing defaults for each configurable section of the code
  58. # General application settings are in the main section
  59. DEFAULTS = {
  60. 'MainWindow': {
  61. 'size': (1260, 740),
  62. 'position': (10, 10),
  63. },
  64. 'AdditionalWindows': {
  65. 'behaviour': "On top"
  66. },
  67. 'project': {
  68. 'prompt_save_on_close': True,
  69. 'prompt_save_editor_modified': True,
  70. 'prompt_on_deleting_workspace': False,
  71. 'save_altered_workspaces_only': False
  72. },
  73. 'Editors': {
  74. 'completion_enabled': True,
  75. }
  76. }
  77. # State encodes widget layout (among other things).
  78. # Increment this when the state of the next version is incompatible with the previous.
  79. SAVE_STATE_VERSION = 2
  80. # 'Singleton' instance
  81. QSettings.setDefaultFormat(QSettings.IniFormat)
  82. CONF = UserConfig(ORGANIZATION, APPNAME, defaults=DEFAULTS)
  83. # Configuration for additional MainWindow instances: matplotlib figures, custom user interfaces etc
  84. def get_window_config():
  85. """
  86. :return: A WindowConfig object describing the desired window configuration based on the current settings
  87. """
  88. try:
  89. windows_behaviour = CONF.get("AdditionalWindows", "behaviour", type=str)
  90. windows_on_top = True if windows_behaviour == "On top" else False
  91. except KeyError:
  92. windows_on_top = False
  93. if windows_on_top:
  94. parent = _ADDITIONAL_MAINWINDOWS_PARENT
  95. flags = WINDOW_ONTOP_FLAGS
  96. else:
  97. parent = None
  98. flags = WINDOW_STANDARD_FLAGS
  99. return WindowConfig(parent, flags)
  100. def set_additional_windows_parent(widget):
  101. """
  102. Sets the parent for any new MainWindow instances created and updates the existing QMainWindow instances
  103. :param widget: A QWidget that will act as a parent for a each new window
  104. """
  105. global _ADDITIONAL_MAINWINDOWS_PARENT
  106. _ADDITIONAL_MAINWINDOWS_PARENT = widget