]> SALOME platform Git repositories - tools/solverlab.git/blob - CoreFlows/gui/SalomePyQt_MockUp.py.in
Salome HOME
Updated expected values in python unit tests
[tools/solverlab.git] / CoreFlows / gui / SalomePyQt_MockUp.py.in
1 #  Copyright (C) 2007-2010  CEA/DEN, EDF R&D, OPEN CASCADE
2 #
3 #  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 #  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 #
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.
10 #
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.
15 #
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
19 #
20 #  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 #
22 # Author : A. Bruneton
23 #
24
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
30
31 RESOURCE_DIR = "@SGPYQT_RES_DIR@"
32
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 
36   SALOME
37   """  
38   currentTabChanged = pyqtSignal(int)
39   
40   START_VIEW_ID = 0
41   
42   def __init__(self, mainWindow=None):
43     QObject.__init__(self)
44     self._mainWindow = mainWindow
45     self._tabWidget = QTabWidget()
46     self._tabWidget.setObjectName("TabWidget")
47     self._viewIDs = {}
48     self._menuBar = None
49     if self._mainWindow:
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
55   
56   def getDesktop(self):
57     return self._mainWindow
58   
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);
63   
64   @pyqtSlot(int)
65   def onTabChanged(self, index):
66     if self._blockSignal:
67       return
68     invDict = dict([(v, k) for k,v in self._viewIDs.items()])
69     if index in invDict:
70 #    if invDict.has_key(index):
71       self._blockSignal = True
72       self.currentTabChanged.emit(invDict[index])
73       self._blockSignal = False
74   
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
80   
81   def activateView(self, viewID):
82     idx = self._viewIDs[viewID]
83     self._tabWidget.setCurrentIndex(idx)
84   
85   def setViewVisible(self, viewID, isVis):
86     ## TODO: improve to really remove tab
87     if isVis:
88       self.activateView(viewID) 
89   
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)
96     try:
97       w.clearAll()
98     except:
99       pass
100     # Update the other tab indices which are now shifted:
101     for k, idx in self._viewIDs.items():
102       if idx > idxClosed:
103         self._viewIDs[k] -= 1
104     self._blockSignal = False
105   
106   def setViewTitle(self, viewID, title):
107     idx = self._viewIDs[viewID]
108     self._tabWidget.setTabText(idx, title)
109   
110   def createAction(self, id, short_name, long_name, tooltip, icon):
111     import os
112     return QAction(QIcon(QPixmap(os.path.normpath(icon))),short_name, None)
113   
114   def defaultMenuGroup(self):
115     return None
116   
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)
121       else:
122         return self.__createMenu2(name_or_action, pos_or_menu)
123     
124   def __createMenu1(self, name, pos, menuId, menuGroup):
125     menu = QMenu(name, self._menuBar)
126     self._menuBar.addMenu(menu)
127     return menu
128   
129   def __createMenu2(self, action, menu):
130     menu.addAction(action)
131   
132   def createSeparator(self):
133     return None
134   
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)
139       else:
140         return self.__createTool2(toolbar_name_or_action, toolbar)
141     
142   def __createTool1(self, toolbar_name):
143     return None
144   
145   def __createTool2(self, action, toolbar):
146     return None
147   
148   def loadIcon(self, module_name, file_name):
149     import os
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)