# Copyright (C) 2007-2022 CEA/DEN, EDF R&D # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public # License as published by the Free Software Foundation; either # version 2.1 of the License, or (at your option) any later version. # # This library is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public # License along with this library; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com # # Author : A. Bruneton # from pyqtside.QtWidgets import QApplication, QTabWidget from pyqtside.QtWidgets import QAction, QMenu, QDesktopWidget, QFileDialog from pyqtside.QtGui import QIcon, QPixmap from pyqtside.QtCore import QObject, pyqtSlot, pyqtSignal RESOURCE_DIR = "@SGPYQT_RES_DIR@" class SalomePyQt(QObject): """ A pure Qt implementation of the SgPyQt API (usually provided in the SALOME context) This class can be used to mimick the true SALOME object without having to launch SALOME """ currentTabChanged = pyqtSignal(int) START_VIEW_ID = 0 def __init__(self, mainWindow=None): QObject.__init__(self) self._mainWindow = mainWindow self._tabWidget = QTabWidget() self._tabWidget.setObjectName("TabWidget") self._viewIDs = {} self._menuBar = None if self._mainWindow: self._menuBar = self._mainWindow.menuBar() self._mainWindow.setCentralWidget(self._tabWidget) self._tabWidget.currentChanged.connect(self.onTabChanged) self._blockSignal = False def getDesktop(self): return self._mainWindow def getFileName(self, parent_widget, initial, filters, caption, do_open): fil = ";;".join([str(f) for f in filters]) return QFileDialog.getSaveFileName(parent=parent_widget, caption=caption, directory=initial, filter=fil); @pyqtSlot(int) def onTabChanged(self, index): if self._blockSignal: return invDict = dict([(v, k) for k,v in self._viewIDs.items()]) if index in invDict: self._blockSignal = True self.currentTabChanged.emit(invDict[index]) self._blockSignal = False def createView(self, name, widget): self.START_VIEW_ID += 1 idx = self._tabWidget.insertTab(-1, widget, name) self._viewIDs[self.START_VIEW_ID] = idx return self.START_VIEW_ID def activateView(self, viewID): idx = self._viewIDs[viewID] self._tabWidget.setCurrentIndex(idx) def setViewVisible(self, viewID, isVis): ## TODO: improve to really remove tab if isVis: self.activateView(viewID) def closeView(self, viewID): self._blockSignal = True idxClosed = self._viewIDs[viewID] # QTabWidget doesn't clean after itself when removing a tab w = self._tabWidget.widget(idxClosed) self._tabWidget.removeTab(idxClosed) try: w.clearAll() except: pass # Update the other tab indices which are now shifted: for k, idx in self._viewIDs.items(): if idx > idxClosed: self._viewIDs[k] -= 1 self._blockSignal = False def setViewTitle(self, viewID, title): idx = self._viewIDs[viewID] self._tabWidget.setTabText(idx, title) def createAction(self, id, short_name, long_name, tooltip, icon): import os return QAction(QIcon(QPixmap(os.path.normpath(icon))),short_name, None) def defaultMenuGroup(self): return None def createMenu(self, name_or_action, pos_or_menu, menuId=-1, menuGroup=None): if not self._mainWindow is None: if isinstance(name_or_action, str): return self.__createMenu1( name_or_action, pos_or_menu, menuId, menuGroup) else: return self.__createMenu2(name_or_action, pos_or_menu) def __createMenu1(self, name, pos, menuId, menuGroup): menu = QMenu(name, self._menuBar) self._menuBar.addMenu(menu) return menu def __createMenu2(self, action, menu): menu.addAction(action) def createSeparator(self): return None def createTool(self, toolbar_name_or_action, toolbar=None): if not self._mainWindow is None: if isinstance(toolbar_name_or_action, str): return self.__createTool1(toolbar_name_or_action) else: return self.__createTool2(toolbar_name_or_action, toolbar) def __createTool1(self, toolbar_name): return None def __createTool2(self, action, toolbar): return None def loadIcon(self, module_name, file_name): import os mod_dir = os.getenv("%s_ROOT_DIR" % module_name) mod_lc = module_name.lower() res_path = os.path.join(mod_dir, RESOURCE_DIR, mod_lc, file_name) # e.g. MODULE_ROOT_DIR/share/resource/module/image.png return QIcon(res_path)