4 from __future__ import with_statement
11 from PyQt4 import QtGui
12 from PyQt4 import QtCore
16 from util import misc as misc_utils
20 _moduleLogger = logging.getLogger(__name__)
26 class REPLACEME(object):
29 os.path.dirname(__file__),
30 os.path.join(os.path.dirname(__file__), "../data"),
31 os.path.join(os.path.dirname(__file__), "../lib"),
32 '/usr/share/%s' % constants.__app_name__,
33 '/usr/lib/%s' % constants.__app_name__,
36 def __init__(self, app):
38 for dataPath in self._DATA_PATHS:
39 appIconPath = os.path.join(dataPath, "pixmaps", "%s.png" % constants.__app_name__)
40 if os.path.isfile(appIconPath):
41 self._dataPath = dataPath
44 raise RuntimeError("UI Descriptor not found!")
46 self._appIconPath = appIconPath
48 self._hiddenCategories = set()
49 self._hiddenUnits = {}
50 self._clipboard = QtGui.QApplication.clipboard()
52 self._mainWindow = None
54 self._fullscreenAction = QtGui.QAction(None)
55 self._fullscreenAction.setText("Fullscreen")
56 self._fullscreenAction.setCheckable(True)
57 self._fullscreenAction.setShortcut(QtGui.QKeySequence("CTRL+Enter"))
58 self._fullscreenAction.toggled.connect(self._on_toggle_fullscreen)
60 self._logAction = QtGui.QAction(None)
61 self._logAction.setText("Log")
62 self._logAction.setShortcut(QtGui.QKeySequence("CTRL+l"))
63 self._logAction.triggered.connect(self._on_log)
65 self._quitAction = QtGui.QAction(None)
66 self._quitAction.setText("Quit")
67 self._quitAction.setShortcut(QtGui.QKeySequence("CTRL+q"))
68 self._quitAction.triggered.connect(self._on_quit)
70 self._app.lastWindowClosed.connect(self._on_app_quit)
73 def load_settings(self):
75 with open(constants._user_settings_, "r") as settingsFile:
76 settings = simplejson.load(settingsFile)
78 _moduleLogger.info("No settings")
81 _moduleLogger.info("Settings were corrupt")
84 self._fullscreenAction.setChecked(settings.get("isFullScreen", False))
86 def save_settings(self):
88 "isFullScreen": self._fullscreenAction.isChecked(),
90 with open(constants._user_settings_, "w") as settingsFile:
91 simplejson.dump(settings, settingsFile)
94 def appIconPath(self):
95 return self._appIconPath
98 def fullscreenAction(self):
99 return self._fullscreenAction
103 return self._logAction
106 def quitAction(self):
107 return self._quitAction
109 def _close_windows(self):
110 if self._mainWindow is not None:
111 self._mainWindow.window.destroyed.disconnect(self._on_child_close)
112 self._mainWindow.close()
113 self._mainWindow = None
115 @misc_utils.log_exception(_moduleLogger)
116 def _on_app_quit(self, checked = False):
119 @misc_utils.log_exception(_moduleLogger)
120 def _on_child_close(self, obj = None):
121 self._mainWindow = None
123 @misc_utils.log_exception(_moduleLogger)
124 def _on_toggle_fullscreen(self, checked = False):
125 for window in self._walk_children():
126 window.set_fullscreen(checked)
128 @misc_utils.log_exception(_moduleLogger)
129 def _on_log(self, checked = False):
130 with open(constants._user_logpath_, "r") as f:
131 logLines = f.xreadlines()
132 log = "".join(logLines)
133 self._clipboard.setText(log)
135 @misc_utils.log_exception(_moduleLogger)
136 def _on_quit(self, checked = False):
137 self._close_windows()
140 class MainWindow(object):
142 def __init__(self, parent, app):
145 self._layout = QtGui.QVBoxLayout()
147 centralWidget = QtGui.QWidget()
148 centralWidget.setLayout(self._layout)
150 self._window = QtGui.QMainWindow(parent)
151 self._window.setAttribute(QtCore.Qt.WA_DeleteOnClose, True)
152 maeqt.set_autorient(self._window, True)
153 maeqt.set_stackable(self._window, True)
154 self._window.setWindowTitle("%s" % constants.__pretty_app_name__)
155 self._window.setWindowIcon(QtGui.QIcon(self._app.appIconPath))
156 self._window.setCentralWidget(centralWidget)
158 self._closeWindowAction = QtGui.QAction(None)
159 self._closeWindowAction.setText("Close")
160 self._closeWindowAction.setShortcut(QtGui.QKeySequence("CTRL+w"))
161 self._closeWindowAction.triggered.connect(self._on_close_window)
164 fileMenu = self._window.menuBar().addMenu("&File")
166 viewMenu = self._window.menuBar().addMenu("&View")
168 self._window.addAction(self._closeWindowAction)
169 self._window.addAction(self._app.quitAction)
170 self._window.addAction(self._app.fullscreenAction)
172 fileMenu = self._window.menuBar().addMenu("&Units")
173 fileMenu.addAction(self._closeWindowAction)
174 fileMenu.addAction(self._app.quitAction)
176 viewMenu = self._window.menuBar().addMenu("&View")
177 viewMenu.addAction(self._app.fullscreenAction)
179 self._window.addAction(self._app.logAction)
181 self.set_fullscreen(self._app.fullscreenAction.isChecked())
188 def walk_children(self):
193 for child in self.walk_children():
197 for child in self.walk_children():
202 for child in self.walk_children():
203 child.window.destroyed.disconnect(self._on_child_close)
207 def set_fullscreen(self, isFullscreen):
209 self._window.showFullScreen()
211 self._window.showNormal()
212 for child in self.walk_children():
213 child.set_fullscreen(isFullscreen)
215 @misc_utils.log_exception(_moduleLogger)
216 def _on_close_window(self, checked = True):
221 app = QtGui.QApplication([])
222 handle = REPLACEME(app)
226 if __name__ == "__main__":
227 logging.basicConfig(level = logging.DEBUG)
229 os.makedirs(constants._data_path_)