CMS 3D CMS Logo

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