CMS 3D CMS Logo

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