Salome HOME
Updated copyright comment
[modules/gui.git] / tools / CurvePlot / src / python / test / SalomePyQt_MockUp.py.in
1 # Copyright (C) 2007-2024  CEA, EDF
2 #
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.
7 #
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.
12 #
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
16 #
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 #
19 # Author : A. Bruneton
20 #
21
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
26
27 RESOURCE_DIR = "@SGPYQT_RES_DIR@"
28
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 
32   SALOME
33   """  
34   currentTabChanged = pyqtSignal(int)
35   
36   START_VIEW_ID = 0
37   
38   def __init__(self, mainWindow=None):
39     QObject.__init__(self)
40     self._mainWindow = mainWindow
41     self._tabWidget = QTabWidget()
42     self._tabWidget.setObjectName("TabWidget")
43     self._viewIDs = {}
44     self._menuBar = None
45     if self._mainWindow:
46       self._menuBar = self._mainWindow.menuBar()
47       self._mainWindow.setCentralWidget(self._tabWidget)
48     self._tabWidget.currentChanged.connect(self.onTabChanged)
49     self._blockSignal = False
50   
51   def getDesktop(self):
52     return self._mainWindow
53   
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);
58   
59   @pyqtSlot(int)
60   def onTabChanged(self, index):
61     if self._blockSignal:
62       return
63     invDict = dict([(v, k) for k,v in self._viewIDs.items()])
64     if index in invDict:
65       self._blockSignal = True
66       self.currentTabChanged.emit(invDict[index])
67       self._blockSignal = False
68   
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
74   
75   def activateView(self, viewID):
76     idx = self._viewIDs[viewID]
77     self._tabWidget.setCurrentIndex(idx)
78   
79   def setViewVisible(self, viewID, isVis):
80     ## TODO: improve to really remove tab
81     if isVis:
82       self.activateView(viewID) 
83   
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)
90     try:
91       w.clearAll()
92     except:
93       pass
94     # Update the other tab indices which are now shifted:
95     for k, idx in self._viewIDs.items():
96       if idx > idxClosed:
97         self._viewIDs[k] -= 1
98     self._blockSignal = False
99   
100   def setViewTitle(self, viewID, title):
101     idx = self._viewIDs[viewID]
102     self._tabWidget.setTabText(idx, title)
103   
104   def createAction(self, id, short_name, long_name, tooltip, icon):
105     import os
106     return QAction(QIcon(QPixmap(os.path.normpath(icon))),short_name, None)
107   
108   def defaultMenuGroup(self):
109     return None
110   
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)
115       else:
116         return self.__createMenu2(name_or_action, pos_or_menu)
117     
118   def __createMenu1(self, name, pos, menuId, menuGroup):
119     menu = QMenu(name, self._menuBar)
120     self._menuBar.addMenu(menu)
121     return menu
122   
123   def __createMenu2(self, action, menu):
124     menu.addAction(action)
125   
126   def createSeparator(self):
127     return None
128   
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)
133       else:
134         return self.__createTool2(toolbar_name_or_action, toolbar)
135     
136   def __createTool1(self, toolbar_name):
137     return None
138   
139   def __createTool2(self, action, toolbar):
140     return None
141   
142   def loadIcon(self, module_name, file_name):
143     import os
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)