Salome HOME
Ajout des copyrigths dans les fichiers __init__.py
[modules/adao.git] / src / daSalome / daGUI / daGuiImpl / adaoGuiHelper.py
1 # -*- coding: utf-8 -*-
2 #  Copyright (C) 2010 EDF R&D
3 #
4 #  This library is free software; you can redistribute it and/or
5 #  modify it under the terms of the GNU Lesser General Public
6 #  License as published by the Free Software Foundation; either
7 #  version 2.1 of the License.
8 #
9 #  This library is distributed in the hope that it will be useful,
10 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 #  Lesser General Public License for more details.
13 #
14 #  You should have received a copy of the GNU Lesser General Public
15 #  License along with this library; if not, write to the Free Software
16 #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 #
18 #  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #
20
21 __author__="aribes/gboulant"
22
23 import salome
24 # Get SALOME PyQt interface
25 import SalomePyQt
26 __sgPyQt = SalomePyQt.SalomePyQt()
27
28 import adaoModuleHelper
29 from PyQt4 import QtGui,QtCore
30
31 def waitCursor():
32     QtGui.QApplication.setOverrideCursor(QtGui.QCursor(QtCore.Qt.WaitCursor))
33
34 def restoreCursor():
35     QtGui.QApplication.restoreOverrideCursor()
36
37 def gui_warning(parent, msg="An error occurs" ):
38     """
39     This function displays a message dialog box displaying the specified message.
40     """
41     QtGui.QMessageBox.warning( parent, "Alerte", msg)
42
43 def getActiveStudyId():
44     """
45     This function returns the id of the active study. The concept of active study
46     makes sens only in the GUI context.
47     """
48     return __sgPyQt.getStudyId()
49
50
51 def getActiveStudy():
52     """
53     This function returns the active study reference. The concept of active study
54     makes sens only in the GUI context.
55     """
56     studyId = getActiveStudyId()()
57     study = adaoModuleHelper.getStudyManager().GetStudyByID( studyId )
58     return study
59
60 def refreshObjectBrowser():
61     """
62     Refresh the graphical representation of the SALOME study, in case where the
63     GUI is working.
64     """
65     if salome.sg is not None:
66         salome.sg.updateObjBrowser(0)
67
68 def selectItem(salomeStudyItem):
69   if salome.sg is not None:
70     salome.sg.ClearIObjects()
71     salome.sg.AddIObject(salomeStudyItem)
72
73 def getSelectedItem(salomeStudyId=getActiveStudyId()):
74     """
75     Get the current selection. If more than one item are selected, the
76     only first is considered. The object is return (not the id).
77     The item can be of any type, no control is done in this function.
78     """
79     if salome.sg is None:
80         raise Exception("GuiHelper.getSelectedItem can't be used without the GUI context")
81
82     salomeStudy = adaoModuleHelper.getStudyManager().GetStudyByID( salomeStudyId )
83
84     item = None
85     listEntries=salome.sg.getAllSelected()
86     if len(listEntries) >= 1:
87         entry = listEntries[0]
88         item = salomeStudy.FindObjectID( entry )
89
90     return item
91
92 def getAllSelected(salomeStudyId):
93     """
94     Returns all selected items in the specified study.
95     """
96     if salome.sg is None:
97         raise OmaException("getSelectedItem can't be used without the GUI context", OmaException.TYPES.DEVEL)
98
99     study = adaoModuleHelper.getStudyManager().GetStudyByID( salomeStudyId )
100     selcount = salome.sg.SelectedCount()
101     seltypes = {}
102     for i in range( selcount ):
103         __incObjToMap( seltypes, adaoModuleHelper.getObjectID( study, salome.sg.getSelected( i ) ) )
104         pass
105     return selcount, seltypes
106
107 def __incObjToMap( m, id ):
108     """
109     Increment object counter in the specified map.
110     Not to be used outside this module.
111     """
112     if id not in m: m[id] = 0
113     m[id] += 1
114     pass
115
116 def getDesktop():
117     """
118     Returns the active Desktop. Usefull to set the relative position of a dialog box
119     """
120     return __sgPyQt.getDesktop()
121
122 def warning(msg):
123     """
124     This function displays a message dialog box displaying the specified message.
125     """
126     gui_warning(getDesktop(),msg)