1 # Copyright (C) 2007-2010 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.
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 PyQt5.QtWidgets import QApplication, QTabWidget
26 from PyQt5.QtWidgets import QAction, QMenu, QDesktopWidget, QFileDialog
27 from PyQt5.QtGui import QIcon, QPixmap
28 #from PyQt5.QtCore import SIGNAL, SLOT, QObject, pyqtSlot, pyqtSignal
29 from PyQt5.QtCore import QObject, pyqtSlot, pyqtSignal
31 RESOURCE_DIR = "@SGPYQT_RES_DIR@"
33 class SalomePyQt(QObject):
34 """ A pure Qt implementation of the SgPyQt API (usually provided in the SALOME context)
35 This class can be used to mimick the true SALOME object without having to launch
38 currentTabChanged = pyqtSignal(int)
42 def __init__(self, mainWindow=None):
43 QObject.__init__(self)
44 self._mainWindow = mainWindow
45 self._tabWidget = QTabWidget()
46 self._tabWidget.setObjectName("TabWidget")
50 self._menuBar = self._mainWindow.menuBar()
51 self._mainWindow.setCentralWidget(self._tabWidget)
52 # self.connect(self._tabWidget, SIGNAL("currentChanged(int)"), self, SLOT("onTabChanged(int)"))
53 self._tabWidget.currentChanged.connect(self.onTabChanged)
54 self._blockSignal = False
57 return self._mainWindow
59 def getFileName(self, parent_widget, initial, filters, caption, do_open):
60 fil = ";;".join([str(f) for f in filters])
61 return QFileDialog.getOpenFileName(parent=parent_widget,
62 caption=caption, directory=initial, filter=fil);
65 def onTabChanged(self, index):
68 invDict = dict([(v, k) for k,v in self._viewIDs.items()])
70 # if invDict.has_key(index):
71 self._blockSignal = True
72 self.currentTabChanged.emit(invDict[index])
73 self._blockSignal = False
75 def createView(self, name, widget):
76 self.START_VIEW_ID += 1
77 idx = self._tabWidget.insertTab(-1, widget, name)
78 self._viewIDs[self.START_VIEW_ID] = idx
79 return self.START_VIEW_ID
81 def activateView(self, viewID):
82 idx = self._viewIDs[viewID]
83 self._tabWidget.setCurrentIndex(idx)
85 def setViewVisible(self, viewID, isVis):
86 ## TODO: improve to really remove tab
88 self.activateView(viewID)
90 def closeView(self, viewID):
91 self._blockSignal = True
92 idxClosed = self._viewIDs[viewID]
93 # QTabWidget doesn't clean after itself when removing a tab
94 w = self._tabWidget.widget(idxClosed)
95 self._tabWidget.removeTab(idxClosed)
100 # Update the other tab indices which are now shifted:
101 for k, idx in self._viewIDs.items():
103 self._viewIDs[k] -= 1
104 self._blockSignal = False
106 def setViewTitle(self, viewID, title):
107 idx = self._viewIDs[viewID]
108 self._tabWidget.setTabText(idx, title)
110 def createAction(self, id, short_name, long_name, tooltip, icon):
112 return QAction(QIcon(QPixmap(os.path.normpath(icon))),short_name, None)
114 def defaultMenuGroup(self):
117 def createMenu(self, name_or_action, pos_or_menu, menuId=-1, menuGroup=None):
118 if not self._mainWindow is None:
119 if isinstance(name_or_action, str):
120 return self.__createMenu1( name_or_action, pos_or_menu, menuId, menuGroup)
122 return self.__createMenu2(name_or_action, pos_or_menu)
124 def __createMenu1(self, name, pos, menuId, menuGroup):
125 menu = QMenu(name, self._menuBar)
126 self._menuBar.addMenu(menu)
129 def __createMenu2(self, action, menu):
130 menu.addAction(action)
132 def createSeparator(self):
135 def createTool(self, toolbar_name_or_action, toolbar=None):
136 if not self._mainWindow is None:
137 if isinstance(toolbar_name_or_action, str):
138 return self.__createTool1(toolbar_name_or_action)
140 return self.__createTool2(toolbar_name_or_action, toolbar)
142 def __createTool1(self, toolbar_name):
145 def __createTool2(self, action, toolbar):
148 def loadIcon(self, module_name, file_name):
150 mod_dir = os.getenv("%s_ROOT_DIR" % module_name)
151 mod_lc = module_name.lower()
152 res_path = os.path.join(mod_dir, RESOURCE_DIR, mod_lc, file_name)
153 # e.g. MODULE_ROOT_DIR/share/resource/module/image.png
154 return QIcon(res_path)