1 # Copyright (C) 2007-2019 CEA/DEN, EDF R&D, OPEN CASCADE
3 # Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 # This library is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU Lesser General Public
8 # License as published by the Free Software Foundation; either
9 # version 2.1 of the License, or (at your option) any later version.
11 # This library is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # Lesser General Public License for more details.
16 # You should have received a copy of the GNU Lesser General Public
17 # License along with this library; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 # Author : A. Bruneton
25 from pyqtside.QtGui import QApplication, QTabWidget
26 from pyqtside.QtGui import QAction, QMenu, QIcon, QPixmap, QDesktopWidget, QFileDialog
27 from pyqtside.QtCore import SIGNAL, SLOT, QObject, Slot, Signal
29 RESOURCE_DIR = "@SGPYQT_RES_DIR@"
31 class SalomePyQt(QObject):
32 """ A pure Qt implementation of the SgPyQt API (usually provided in the SALOME context)
33 This class can be used to mimick the true SALOME object without having to launch
36 currentTabChanged = Signal(int)
40 def __init__(self, mainWindow=None):
41 QObject.__init__(self)
42 self._mainWindow = mainWindow
43 self._tabWidget = QTabWidget()
44 self._tabWidget.setObjectName("TabWidget")
48 self._menuBar = self._mainWindow.menuBar()
49 self._mainWindow.setCentralWidget(self._tabWidget)
50 self.connect(self._tabWidget, SIGNAL("currentChanged(int)"), self, SLOT("onTabChanged(int)"))
51 self._blockSignal = False
54 return self._mainWindow
56 def getFileName(self, parent_widget, initial, filters, caption, do_open):
57 fil = ";;".join([str(f) for f in filters])
58 return QFileDialog.getSaveFileName(parent=parent_widget,
59 caption=caption, directory=initial, filter=fil);
62 def onTabChanged(self, index):
65 invDict = dict([(v, k) for k,v in self._viewIDs.items()])
66 if invDict.has_key(index):
67 self._blockSignal = True
68 self.currentTabChanged.emit(invDict[index])
69 self._blockSignal = False
71 def createView(self, name, widget):
72 self.START_VIEW_ID += 1
73 idx = self._tabWidget.insertTab(-1, widget, name)
74 self._viewIDs[self.START_VIEW_ID] = idx
75 return self.START_VIEW_ID
77 def activateView(self, viewID):
78 idx = self._viewIDs[viewID]
79 self._tabWidget.setCurrentIndex(idx)
81 def setViewVisible(self, viewID, isVis):
82 ## TODO: improve to really remove tab
84 self.activateView(viewID)
86 def closeView(self, viewID):
87 self._blockSignal = True
88 idxClosed = self._viewIDs[viewID]
89 # QTabWidget doesn't clean after itself when removing a tab
90 w = self._tabWidget.widget(idxClosed)
91 self._tabWidget.removeTab(idxClosed)
96 # Update the other tab indices which are now shifted:
97 for k, idx in self._viewIDs.items():
100 self._blockSignal = False
102 def setViewTitle(self, viewID, title):
103 idx = self._viewIDs[viewID]
104 self._tabWidget.setTabText(idx, title)
106 def createAction(self, id, short_name, long_name, tooltip, icon):
108 return QAction(QIcon(QPixmap(os.path.normpath(icon))),short_name, None)
110 def defaultMenuGroup(self):
113 def createMenu(self, name_or_action, pos_or_menu, menuId=-1, menuGroup=None):
114 if not self._mainWindow is None:
115 if isinstance(name_or_action, str):
116 return self.__createMenu1( name_or_action, pos_or_menu, menuId, menuGroup)
118 return self.__createMenu2(name_or_action, pos_or_menu)
120 def __createMenu1(self, name, pos, menuId, menuGroup):
121 menu = QMenu(name, self._menuBar)
122 self._menuBar.addMenu(menu)
125 def __createMenu2(self, action, menu):
126 menu.addAction(action)
128 def createSeparator(self):
131 def createTool(self, toolbar_name_or_action, toolbar=None):
132 if not self._mainWindow is None:
133 if isinstance(toolbar_name_or_action, str):
134 return self.__createTool1(toolbar_name_or_action)
136 return self.__createTool2(toolbar_name_or_action, toolbar)
138 def __createTool1(self, toolbar_name):
141 def __createTool2(self, action, toolbar):
144 def loadIcon(self, module_name, file_name):
146 mod_dir = os.getenv("%s_ROOT_DIR" % module_name)
147 mod_lc = module_name.lower()
148 res_path = os.path.join(mod_dir, RESOURCE_DIR, mod_lc, file_name)
149 # e.g. MODULE_ROOT_DIR/share/resource/module/image.png
150 return QIcon(res_path)