1 # Copyright (C) 2007-2021 CEA/DEN, EDF R&D
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU Lesser General Public
5 # License as published by the Free Software Foundation; either
6 # version 2.1 of the License, or (at your option) any later version.
8 # This library is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 # Lesser General Public License for more details.
13 # You should have received a copy of the GNU Lesser General Public
14 # License along with this library; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 # Author : A. Bruneton
22 from pyqtside.QtWidgets import QApplication, QTabWidget
23 from pyqtside.QtWidgets import QAction, QMenu, QDesktopWidget, QFileDialog
24 from pyqtside.QtGui import QIcon, QPixmap
25 from pyqtside.QtCore import QObject, pyqtSlot, pyqtSignal
27 RESOURCE_DIR = "@SGPYQT_RES_DIR@"
29 class SalomePyQt(QObject):
30 """ A pure Qt implementation of the SgPyQt API (usually provided in the SALOME context)
31 This class can be used to mimick the true SALOME object without having to launch
34 currentTabChanged = pyqtSignal(int)
38 def __init__(self, mainWindow=None):
39 QObject.__init__(self)
40 self._mainWindow = mainWindow
41 self._tabWidget = QTabWidget()
42 self._tabWidget.setObjectName("TabWidget")
46 self._menuBar = self._mainWindow.menuBar()
47 self._mainWindow.setCentralWidget(self._tabWidget)
48 self._tabWidget.currentChanged.connect(self.onTabChanged)
49 self._blockSignal = False
52 return self._mainWindow
54 def getFileName(self, parent_widget, initial, filters, caption, do_open):
55 fil = ";;".join([str(f) for f in filters])
56 return QFileDialog.getSaveFileName(parent=parent_widget,
57 caption=caption, directory=initial, filter=fil);
60 def onTabChanged(self, index):
63 invDict = dict([(v, k) for k,v in self._viewIDs.items()])
65 self._blockSignal = True
66 self.currentTabChanged.emit(invDict[index])
67 self._blockSignal = False
69 def createView(self, name, widget):
70 self.START_VIEW_ID += 1
71 idx = self._tabWidget.insertTab(-1, widget, name)
72 self._viewIDs[self.START_VIEW_ID] = idx
73 return self.START_VIEW_ID
75 def activateView(self, viewID):
76 idx = self._viewIDs[viewID]
77 self._tabWidget.setCurrentIndex(idx)
79 def setViewVisible(self, viewID, isVis):
80 ## TODO: improve to really remove tab
82 self.activateView(viewID)
84 def closeView(self, viewID):
85 self._blockSignal = True
86 idxClosed = self._viewIDs[viewID]
87 # QTabWidget doesn't clean after itself when removing a tab
88 w = self._tabWidget.widget(idxClosed)
89 self._tabWidget.removeTab(idxClosed)
94 # Update the other tab indices which are now shifted:
95 for k, idx in self._viewIDs.items():
98 self._blockSignal = False
100 def setViewTitle(self, viewID, title):
101 idx = self._viewIDs[viewID]
102 self._tabWidget.setTabText(idx, title)
104 def createAction(self, id, short_name, long_name, tooltip, icon):
106 return QAction(QIcon(QPixmap(os.path.normpath(icon))),short_name, None)
108 def defaultMenuGroup(self):
111 def createMenu(self, name_or_action, pos_or_menu, menuId=-1, menuGroup=None):
112 if not self._mainWindow is None:
113 if isinstance(name_or_action, str):
114 return self.__createMenu1( name_or_action, pos_or_menu, menuId, menuGroup)
116 return self.__createMenu2(name_or_action, pos_or_menu)
118 def __createMenu1(self, name, pos, menuId, menuGroup):
119 menu = QMenu(name, self._menuBar)
120 self._menuBar.addMenu(menu)
123 def __createMenu2(self, action, menu):
124 menu.addAction(action)
126 def createSeparator(self):
129 def createTool(self, toolbar_name_or_action, toolbar=None):
130 if not self._mainWindow is None:
131 if isinstance(toolbar_name_or_action, str):
132 return self.__createTool1(toolbar_name_or_action)
134 return self.__createTool2(toolbar_name_or_action, toolbar)
136 def __createTool1(self, toolbar_name):
139 def __createTool2(self, action, toolbar):
142 def loadIcon(self, module_name, file_name):
144 mod_dir = os.getenv("%s_ROOT_DIR" % module_name)
145 mod_lc = module_name.lower()
146 res_path = os.path.join(mod_dir, RESOURCE_DIR, mod_lc, file_name)
147 # e.g. MODULE_ROOT_DIR/share/resource/module/image.png
148 return QIcon(res_path)