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.QtWidgets import QApplication, QTabWidget
26 from pyqtside.QtWidgets import QAction, QMenu, QDesktopWidget, QFileDialog
27 from pyqtside.QtGui import QIcon, QPixmap
28 from pyqtside.QtCore import QObject, pyqtSlot, pyqtSignal
30 RESOURCE_DIR = "@SGPYQT_RES_DIR@"
32 class SalomePyQt(QObject):
33 """ A pure Qt implementation of the SgPyQt API (usually provided in the SALOME context)
34 This class can be used to mimick the true SALOME object without having to launch
37 currentTabChanged = pyqtSignal(int)
41 def __init__(self, mainWindow=None):
42 QObject.__init__(self)
43 self._mainWindow = mainWindow
44 self._tabWidget = QTabWidget()
45 self._tabWidget.setObjectName("TabWidget")
49 self._menuBar = self._mainWindow.menuBar()
50 self._mainWindow.setCentralWidget(self._tabWidget)
51 self._tabWidget.currentChanged.connect(self.onTabChanged)
52 self._blockSignal = False
55 return self._mainWindow
57 def getFileName(self, parent_widget, initial, filters, caption, do_open):
58 fil = ";;".join([str(f) for f in filters])
59 return QFileDialog.getSaveFileName(parent=parent_widget,
60 caption=caption, directory=initial, filter=fil);
63 def onTabChanged(self, index):
66 invDict = dict([(v, k) for k,v in self._viewIDs.items()])
68 self._blockSignal = True
69 self.currentTabChanged.emit(invDict[index])
70 self._blockSignal = False
72 def createView(self, name, widget):
73 self.START_VIEW_ID += 1
74 idx = self._tabWidget.insertTab(-1, widget, name)
75 self._viewIDs[self.START_VIEW_ID] = idx
76 return self.START_VIEW_ID
78 def activateView(self, viewID):
79 idx = self._viewIDs[viewID]
80 self._tabWidget.setCurrentIndex(idx)
82 def setViewVisible(self, viewID, isVis):
83 ## TODO: improve to really remove tab
85 self.activateView(viewID)
87 def closeView(self, viewID):
88 self._blockSignal = True
89 idxClosed = self._viewIDs[viewID]
90 # QTabWidget doesn't clean after itself when removing a tab
91 w = self._tabWidget.widget(idxClosed)
92 self._tabWidget.removeTab(idxClosed)
97 # Update the other tab indices which are now shifted:
98 for k, idx in self._viewIDs.items():
100 self._viewIDs[k] -= 1
101 self._blockSignal = False
103 def setViewTitle(self, viewID, title):
104 idx = self._viewIDs[viewID]
105 self._tabWidget.setTabText(idx, title)
107 def createAction(self, id, short_name, long_name, tooltip, icon):
109 return QAction(QIcon(QPixmap(os.path.normpath(icon))),short_name, None)
111 def defaultMenuGroup(self):
114 def createMenu(self, name_or_action, pos_or_menu, menuId=-1, menuGroup=None):
115 if not self._mainWindow is None:
116 if isinstance(name_or_action, str):
117 return self.__createMenu1( name_or_action, pos_or_menu, menuId, menuGroup)
119 return self.__createMenu2(name_or_action, pos_or_menu)
121 def __createMenu1(self, name, pos, menuId, menuGroup):
122 menu = QMenu(name, self._menuBar)
123 self._menuBar.addMenu(menu)
126 def __createMenu2(self, action, menu):
127 menu.addAction(action)
129 def createSeparator(self):
132 def createTool(self, toolbar_name_or_action, toolbar=None):
133 if not self._mainWindow is None:
134 if isinstance(toolbar_name_or_action, str):
135 return self.__createTool1(toolbar_name_or_action)
137 return self.__createTool2(toolbar_name_or_action, toolbar)
139 def __createTool1(self, toolbar_name):
142 def __createTool2(self, action, toolbar):
145 def loadIcon(self, module_name, file_name):
147 mod_dir = os.getenv("%s_ROOT_DIR" % module_name)
148 mod_lc = module_name.lower()
149 res_path = os.path.join(mod_dir, RESOURCE_DIR, mod_lc, file_name)
150 # e.g. MODULE_ROOT_DIR/share/resource/module/image.png
151 return QIcon(res_path)