CMS 3D CMS Logo

MainWindow.py
Go to the documentation of this file.
1 from __future__ import absolute_import
2 import logging
3 import os
4 import math
5 
6 from PyQt4.QtCore import Qt, SIGNAL, QEvent, QPoint, QSize
7 from PyQt4.QtGui import QMainWindow, QTabWidget, QSizePolicy, QIcon
8 
9 from Vispa.Main.StartupScreen import StartupScreen
10 from . import Resources
11 
12 class MainWindow(QMainWindow):
13 
14  WINDOW_WIDTH = 800
15  WINDOW_HEIGHT = 600
16 
17  """ MainWindow """
18  def __init__(self, application=None, title="VISPA"):
19  #logging.debug(__name__ + ": __init__")
20 
21  self._justActivated = False
22  self._startupScreen = None
23  self._application = application
24  QMainWindow.__init__(self)
25 
26  self._tabWidget = QTabWidget(self)
27  self._tabWidget.setSizePolicy(QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding))
28  self._tabWidget.setUsesScrollButtons(True)
29  self.setCentralWidget(self._tabWidget)
30  if hasattr(self._tabWidget,"setTabsClosable"):
31  self._tabWidget.setTabsClosable(True)
32 
33  if "vispa" in title.lower():
34  self.createStartupScreen()
35 
36  self._fileMenu = self.menuBar().addMenu("&File")
37  self._editMenu = self.menuBar().addMenu("&Edit")
38  self._helpMenu = self.menuBar().addMenu("&Help")
39 
40  self._fileToolBar = self.addToolBar("File")
41 
42  self.ensurePolished()
43  self.setWindowIcon(QIcon(":/resources/vispabutton.png"))
44  self.setWindowTitle(title)
45  self.statusBar()
46 
47  self._loadIni()
48  if self._startupScreen:
49  self._startupScreen.raise_()
51 
52  def startupScreen(self):
53  return self._startupScreen
54 
55  def application(self):
56  return self._application
57 
58  def fileMenu(self):
59  return self._fileMenu
60 
61  def editMenu(self):
62  return self._editMenu
63 
64  def helpMenu(self):
65  return self._helpMenu
66 
67  def fileToolBar(self):
68  return self._fileToolBar
69 
70  def closeEvent(self, event):
71  """ Closes all tabs and exits program if succeeded.
72  """
73  logging.debug('MainWindow: closeEvent()')
74  self._application.closeAllFiles()
75  self._application.shutdownPlugins()
76  if len(self.application().tabControllers()) == 0:
77  event.accept()
78  self._saveIni()
79  else:
80  event.ignore()
81 
82  def addWindow(self, widget, width=None, height=None):
83  """ Add a new window and call the TabController to update the label of the window.
84  """
85  logging.debug('MainWindow: addWindow()')
86  widget.setMainWindow(self)
87  widget.setWindowFlags(Qt.Dialog)
88  widget.show()
89  if width and height:
90  widget.resize(width,height)
91  else:
92  widget.resize(self._tabWidget.size())
93  widget.controller().updateLabel()
94 
95  def addTab(self, widget):
96  """ Add a new tab to the TabWidget and call the TabController to update the label of the Tab.
97  """
98  #logging.debug('MainWindow: addTab()')
99  widget.setTabWidget(self._tabWidget)
100  widget.setMainWindow(self)
101  self._tabWidget.addTab(widget, '')
102  self._tabWidget.setCurrentWidget(widget)
103  widget.controller().updateLabel()
104 
105  def tabWidget(self):
106  return self._tabWidget
107 
108  def tabWidgets(self):
109  return [self._tabWidget.widget(i) for i in range(0, self._tabWidget.count())]
110 
111  def isTabWidget(self, widget):
112  return (self._tabWidget.indexOf(widget) >= 0)
113 
114  def _loadIni(self):
115  """ Load the window properties.
116  """
117  ini = self._application.ini()
118 
119  if ini.has_option("window", "width"):
120  width = ini.getint("window", "width")
121  else:
122  width = self.WINDOW_WIDTH
123  if ini.has_option("window", "height"):
124  height = ini.getint("window", "height")
125  else:
126  height = self.WINDOW_HEIGHT
127  self.resize(QSize(width, height))
128  if ini.has_option("window", "maximized"):
129  if ini.getboolean("window", "maximized"):
130  self.setWindowState(Qt.WindowMaximized)
131  if ini.has_option("window", "fullScreen"):
132  if ini.getboolean("window", "fullScreen"):
133  self.setWindowState(Qt.WindowFullScreen)
134 
135  def _saveIni(self):
136  """ Save the window properties.
137  """
138  ini = self._application.ini()
139  if not ini.has_section("window"):
140  ini.add_section("window")
141  if not self.isMaximized() and not self.isFullScreen():
142  ini.set("window", "width", str(self.width()))
143  ini.set("window", "height", str(self.height()))
144  ini.set("window", "maximized", str(self.isMaximized()))
145  ini.set("window", "fullScreen", str(self.isFullScreen()))
146  self._application.writeIni()
147 
148  def event(self, event):
149  """ Emits activated() signal if correct event occures and if correct changeEvent occured before.
150 
151  Also see changeEvent().
152  The Application shall connect to windowActivated().
153  """
154  QMainWindow.event(self, event)
155  if self._justActivated and event.type() == QEvent.LayoutRequest:
156  self._justActivated = False
157  self.emit(SIGNAL("windowActivated()"))
158  elif event.type()==QEvent.WindowActivate:
159  self.emit(SIGNAL("windowActivated()"))
160  return False
161 
162  def changeEvent(self, event):
163  """ Together with event() this function makes sure tabChanged() is called when the window is activated.
164  """
165  if event.type() == QEvent.ActivationChange and self.isActiveWindow():
166  self._justActivated = True
167 
168  def keyPressEvent(self, event):
169  """ On Escape cancel all running operations.
170  """
171  #logging.debug(__name__ + ": keyPressEvent")
172  if event.key() == Qt.Key_Escape:
173  self.application().cancel()
174  QMainWindow.keyPressEvent(self, event)
175 
176  def resizeEvent(self, event):
177  QMainWindow.resizeEvent(self, event)
179 
180  def setStartupScreenVisible(self, show):
181  if self._startupScreen:
182  self._startupScreen.setVisible(show)
183  #logging.debug(self.__class__.__name__ +": setStartupScreenVisible() %d" % self._startupScreen.isVisible())
184  if show:
186 
188  if not self._startupScreen:
189  return
190  boundingRect = self._startupScreen.boundingRect()
191  deltaWidth = self.width() - boundingRect.width() - 20
192  deltaHeight = self.height() - boundingRect.height() - 80
193 
194  if deltaWidth != 0 or deltaHeight != 0:
195  self._startupScreen.setMaximumSize(max(1, self._startupScreen.width() + deltaWidth), max(1, self._startupScreen.height() + deltaHeight))
196  boundingRect = self._startupScreen.boundingRect()
197  self._startupScreen.move(QPoint(0.5 * (self.width() - boundingRect.width()), 0.5 * (self.height() - boundingRect.height()) + 10) + self._startupScreen.pos() - boundingRect.topLeft())
198 
200  self._startupScreen = StartupScreen(self)
201 
202  def newAnalysisDesignerSlot(self, checked=False):
203  """ Creates new analysis designer tab if that plugin was loaded.
204  """
205  plugin = self.application().plugin("AnalysisDesigner")
206  if plugin:
207  plugin.newAnalysisDesignerTab()
208 
209  def newPxlSlot(self, checked=False):
210  """ Creates new pxl tab if that plugin was loaded.
211  """
212  plugin = self.application().plugin("Pxl")
213  if plugin:
214  plugin.newFile()
215 
216  def openAnalysisFileSlot(self, checked=False):
217  plugin = self.application().plugin("AnalysisDesigner")
218  if plugin:
219  currentRow=self._startupScreen._analysisDesignerRecentFilesList.currentRow()
220  if currentRow!=0:
221  files=self.application().recentFilesFromPlugin(plugin)
222  if currentRow<=len(files):
223  self.application().openFile(files[currentRow-1])
224  else:
225  filetypes = plugin.filetypes()
226  if len(filetypes) > 0:
227  self.application().openFileDialog(filetypes[0].fileDialogFilter())
228 
229  def openPxlFileSlot(self, checked=False):
230  plugin = self.application().plugin("Pxl")
231  if plugin:
232  currentRow=self._startupScreen._pxlEditorRecentFilesList.currentRow()
233  if currentRow!=0:
234  files=self.application().recentFilesFromPlugin(plugin)
235  if currentRow<=len(files):
236  self.application().openFile(files[currentRow-1])
237  else:
238  filetypes = plugin.filetypes()
239  if len(filetypes) > 0:
240  self.application().openFileDialog(filetypes[0].fileDialogFilter())
def isTabWidget(self, widget)
Definition: MainWindow.py:111
def openAnalysisFileSlot(self, checked=False)
Definition: MainWindow.py:216
def newPxlSlot(self, checked=False)
Definition: MainWindow.py:209
def __init__(self, application=None, title="VISPA")
Definition: MainWindow.py:18
unique_ptr< JetDefinition::Plugin > plugin
def setStartupScreenVisible(self, show)
Definition: MainWindow.py:180
def keyPressEvent(self, event)
Definition: MainWindow.py:168
def openPxlFileSlot(self, checked=False)
Definition: MainWindow.py:229
def newAnalysisDesignerSlot(self, checked=False)
Definition: MainWindow.py:202
def addWindow(self, widget, width=None, height=None)
Definition: MainWindow.py:82
#define str(s)