-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWindowPositionManager.py
More file actions
129 lines (102 loc) · 4.27 KB
/
Copy pathWindowPositionManager.py
File metadata and controls
129 lines (102 loc) · 4.27 KB
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
"""
Window Position Manager
Manages saving and restoring window positions using QSettings
"""
from PySide6.QtCore import QSettings, QPoint
from PySide6.QtWidgets import QApplication
class WindowPositionManager:
"""Manages window position persistence using QSettings"""
@staticmethod
def get_settings():
"""Get QSettings instance for the application"""
return QSettings("CosmosCollection", "CosmosCollection")
@staticmethod
def save_window_position(window, window_key):
"""
Save window position and size to settings
Args:
window: QMainWindow or QDialog instance
window_key: Unique key to identify this window type (e.g., 'DSOTargetList')
"""
settings = WindowPositionManager.get_settings()
settings.setValue(f"{window_key}/geometry", window.saveGeometry())
settings.setValue(f"{window_key}/pos", window.pos())
settings.setValue(f"{window_key}/size", window.size())
@staticmethod
def restore_window_position(window, window_key):
"""
Restore window position and size from settings, or center if first time
Args:
window: QMainWindow or QDialog instance
window_key: Unique key to identify this window type (e.g., 'DSOTargetList')
Returns:
bool: True if position was restored, False if centered (first time)
"""
settings = WindowPositionManager.get_settings()
# Try to restore saved geometry first
geometry = settings.value(f"{window_key}/geometry")
if geometry and window.restoreGeometry(geometry):
return True
# Fallback: Try to restore position and size separately
pos = settings.value(f"{window_key}/pos")
size = settings.value(f"{window_key}/size")
if pos is not None and size is not None:
window.move(pos)
window.resize(size)
return True
# First time opening - center the window
WindowPositionManager.center_window(window)
return False
@staticmethod
def center_window(window):
"""
Center window on screen
Args:
window: QMainWindow or QDialog instance
"""
screen_geometry = QApplication.primaryScreen().geometry()
window_geometry = window.frameGeometry()
center_point = screen_geometry.center()
window_geometry.moveCenter(center_point)
window.move(window_geometry.topLeft())
class WindowPositionMixin:
"""
Mixin class to add position persistence to any QMainWindow or QDialog
Usage:
class MyWindow(WindowPositionMixin, QMainWindow):
WINDOW_POSITION_KEY = "MyWindow"
def __init__(self):
super().__init__()
self.setup_window_position()
"""
# Subclasses should define this
WINDOW_POSITION_KEY = None
def setup_window_position(self):
"""Initialize window position management"""
if self.WINDOW_POSITION_KEY is None:
raise ValueError("WINDOW_POSITION_KEY must be defined in subclass")
# Restore saved position or center if first time
WindowPositionManager.restore_window_position(self, self.WINDOW_POSITION_KEY)
def closeEvent(self, event):
"""Save window position when closing"""
if self.WINDOW_POSITION_KEY:
WindowPositionManager.save_window_position(self, self.WINDOW_POSITION_KEY)
# Call parent closeEvent if it exists
if hasattr(super(), 'closeEvent'):
super().closeEvent(event)
else:
event.accept()
def moveEvent(self, event):
"""Save window position when moved"""
if self.WINDOW_POSITION_KEY and self.isVisible():
WindowPositionManager.save_window_position(self, self.WINDOW_POSITION_KEY)
# Call parent moveEvent if it exists
if hasattr(super(), 'moveEvent'):
super().moveEvent(event)
def resizeEvent(self, event):
"""Save window size when resized"""
if self.WINDOW_POSITION_KEY and self.isVisible():
WindowPositionManager.save_window_position(self, self.WINDOW_POSITION_KEY)
# Call parent resizeEvent if it exists
if hasattr(super(), 'resizeEvent'):
super().resizeEvent(event)