Salome HOME
Integration of CurvePlot tool in GUI.
[modules/gui.git] / tools / CurvePlot / src / python / test / 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 pyqtside.QtGui import QApplication, QTabWidget 
26 from pyqtside.QtGui import QAction, QMenu, QIcon, QPixmap, QDesktopWidget, QFileDialog
27 from pyqtside.QtCore import SIGNAL, SLOT, QObject, Slot, Signal
28
29 RESOURCE_DIR = "@SGPYQT_RES_DIR@"
30
31 class SalomePyQt(QObject):
32   """ A pure Qt implementation of the SgPyQt API (usually provided in the SALOME context)
33   This class can be used to mimick the true SALOME object without having to launch 
34   SALOME
35   """  
36   currentTabChanged = Signal(int)
37   
38   START_VIEW_ID = 0
39   
40   def __init__(self, mainWindow=None):
41     QObject.__init__(self)
42     self._mainWindow = mainWindow
43     self._tabWidget = QTabWidget()
44     self._tabWidget.setObjectName("TabWidget")
45     self._viewIDs = {}
46     self._menuBar = None
47     if self._mainWindow:
48       self._menuBar = self._mainWindow.menuBar()
49       self._mainWindow.setCentralWidget(self._tabWidget)
50     self.connect(self._tabWidget, SIGNAL("currentChanged(int)"), self, SLOT("onTabChanged(int)"))
51     self._blockSignal = False
52   
53   def getDesktop(self):
54     return self._mainWindow
55   
56   def getFileName(self, parent_widget, initial, filters, caption, do_open):
57     fil = ";;".join([str(f) for f in filters])
58     return QFileDialog.getOpenFileName(parent=parent_widget,
59                                        caption=caption, directory=initial, filter=fil);
60   
61   @Slot(int)
62   def onTabChanged(self, index):
63     if self._blockSignal:
64       return
65     invDict = dict([(v, k) for k,v in self._viewIDs.items()])
66     if invDict.has_key(index):
67       self._blockSignal = True
68       self.currentTabChanged.emit(invDict[index])
69       self._blockSignal = False
70   
71   def createView(self, name, widget):
72     self.START_VIEW_ID += 1
73     idx = self._tabWidget.insertTab(-1, widget, name)
74     self._viewIDs[self.START_VIEW_ID] = idx 
75     return self.START_VIEW_ID
76   
77   def activateView(self, viewID):
78     idx = self._viewIDs[viewID]
79     self._tabWidget.setCurrentIndex(idx)
80   
81   def setViewVisible(self, viewID, isVis):
82     ## TODO: improve to really remove tab
83     if isVis:
84       self.activateView(viewID) 
85   
86   def closeView(self, viewID):
87     self._blockSignal = True
88     idxClosed = self._viewIDs[viewID]
89     # QTabWidget doesn't clean after itself when removing a tab
90     w = self._tabWidget.widget(idxClosed)  
91     self._tabWidget.removeTab(idxClosed)
92     try:
93       w.clearAll()
94     except:
95       pass
96     # Update the other tab indices which are now shifted:
97     for k, idx in self._viewIDs.items():
98       if idx > idxClosed:
99         self._viewIDs[k] -= 1
100     self._blockSignal = False
101   
102   def setViewTitle(self, viewID, title):
103     idx = self._viewIDs[viewID]
104     self._tabWidget.setTabText(idx, title)
105   
106   def createAction(self, id, short_name, long_name, tooltip, icon):
107     import os
108     return QAction(QIcon(QPixmap(os.path.normpath(icon))),short_name, None)
109   
110   def defaultMenuGroup(self):
111     return None
112   
113   def createMenu(self, name_or_action, pos_or_menu, menuId=-1, menuGroup=None):
114     if not self._mainWindow is None:
115       if isinstance(name_or_action, str):
116         return self.__createMenu1( name_or_action, pos_or_menu, menuId, menuGroup)
117       else:
118         return self.__createMenu2(name_or_action, pos_or_menu)
119     
120   def __createMenu1(self, name, pos, menuId, menuGroup):
121     menu = QMenu(name, self._menuBar)
122     self._menuBar.addMenu(menu)
123     return menu
124   
125   def __createMenu2(self, action, menu):
126     menu.addAction(action)
127   
128   def createSeparator(self):
129     return None
130   
131   def createTool(self, toolbar_name_or_action, toolbar=None):
132     if not self._mainWindow is None:
133       if isinstance(toolbar_name_or_action, str):
134         return self.__createTool1(toolbar_name_or_action)
135       else:
136         return self.__createTool2(toolbar_name_or_action, toolbar)
137     
138   def __createTool1(self, toolbar_name):
139     return None
140   
141   def __createTool2(self, action, toolbar):
142     return None
143   
144   def loadIcon(self, module_name, file_name):
145     import os
146     mod_dir = os.getenv("%s_ROOT_DIR" % module_name)
147     mod_lc = module_name.lower()
148     res_path = os.path.join(mod_dir, RESOURCE_DIR, mod_lc, file_name)
149     # e.g. MODULE_ROOT_DIR/share/resource/module/image.png
150     return QIcon(res_path)