]> SALOME platform Git repositories - modules/gui.git/blob - tools/CurvePlot/src/python/test/SalomePyQt_MockUp.py.in
Salome HOME
Merge 'oscar/porting_to_V9' branch.
[modules/gui.git] / tools / CurvePlot / src / python / test / SalomePyQt_MockUp.py.in
1 # Copyright (C) 2007-2019  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, or (at your option) any later version.
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 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
29
30 RESOURCE_DIR = "@SGPYQT_RES_DIR@"
31
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 
35   SALOME
36   """  
37   currentTabChanged = pyqtSignal(int)
38   
39   START_VIEW_ID = 0
40   
41   def __init__(self, mainWindow=None):
42     QObject.__init__(self)
43     self._mainWindow = mainWindow
44     self._tabWidget = QTabWidget()
45     self._tabWidget.setObjectName("TabWidget")
46     self._viewIDs = {}
47     self._menuBar = None
48     if self._mainWindow:
49       self._menuBar = self._mainWindow.menuBar()
50       self._mainWindow.setCentralWidget(self._tabWidget)
51     self._tabWidget.currentChanged.connect(self.onTabChanged)
52     self._blockSignal = False
53   
54   def getDesktop(self):
55     return self._mainWindow
56   
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);
61   
62   @pyqtSlot(int)
63   def onTabChanged(self, index):
64     if self._blockSignal:
65       return
66     invDict = dict([(v, k) for k,v in self._viewIDs.items()])
67     if index in invDict:
68       self._blockSignal = True
69       self.currentTabChanged.emit(invDict[index])
70       self._blockSignal = False
71   
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
77   
78   def activateView(self, viewID):
79     idx = self._viewIDs[viewID]
80     self._tabWidget.setCurrentIndex(idx)
81   
82   def setViewVisible(self, viewID, isVis):
83     ## TODO: improve to really remove tab
84     if isVis:
85       self.activateView(viewID) 
86   
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)
93     try:
94       w.clearAll()
95     except:
96       pass
97     # Update the other tab indices which are now shifted:
98     for k, idx in self._viewIDs.items():
99       if idx > idxClosed:
100         self._viewIDs[k] -= 1
101     self._blockSignal = False
102   
103   def setViewTitle(self, viewID, title):
104     idx = self._viewIDs[viewID]
105     self._tabWidget.setTabText(idx, title)
106   
107   def createAction(self, id, short_name, long_name, tooltip, icon):
108     import os
109     return QAction(QIcon(QPixmap(os.path.normpath(icon))),short_name, None)
110   
111   def defaultMenuGroup(self):
112     return None
113   
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)
118       else:
119         return self.__createMenu2(name_or_action, pos_or_menu)
120     
121   def __createMenu1(self, name, pos, menuId, menuGroup):
122     menu = QMenu(name, self._menuBar)
123     self._menuBar.addMenu(menu)
124     return menu
125   
126   def __createMenu2(self, action, menu):
127     menu.addAction(action)
128   
129   def createSeparator(self):
130     return None
131   
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)
136       else:
137         return self.__createTool2(toolbar_name_or_action, toolbar)
138     
139   def __createTool1(self, toolbar_name):
140     return None
141   
142   def __createTool2(self, action, toolbar):
143     return None
144   
145   def loadIcon(self, module_name, file_name):
146     import os
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)