]> SALOME platform Git repositories - modules/hydrosolver.git/blob - src/salome_hydro/gui_utils.py
Salome HOME
Cleanup
[modules/hydrosolver.git] / src / salome_hydro / gui_utils.py
1 #  Copyright (C) 2012-2013 EDF
2 #
3 #  This file is part of SALOME HYDRO module.
4 #
5 #  SALOME HYDRO module is free software: you can redistribute it and/or modify
6 #  it under the terms of the GNU General Public License as published by
7 #  the Free Software Foundation, either version 3 of the License, or
8 #  (at your option) any later version.
9 #
10 #  SALOME HYDRO module is distributed in the hope that it will be useful,
11 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 #  GNU General Public License for more details.
14 #
15 #  You should have received a copy of the GNU General Public License
16 #  along with SALOME HYDRO module.  If not, see <http://www.gnu.org/licenses/>.
17
18 import os
19 #from PyQt4 import QtCore, QtGui
20 from PyQt5.QtWidgets import QApplication
21 from PyQt5.QtGui  import QCursor
22 from PyQt5.QtCore import Qt
23
24 import salome
25 from salome.kernel.studyedit import getStudyEditor
26
27
28 class HSGUIException(Exception):
29   """
30   Exception class used to display user-friendly messages in a dialog box for
31   standard errors (missing files, etc.).
32   """
33   pass
34
35
36 class OverrideCursorCM:
37   """
38   This class is a context manager that can be used to set an override cursor
39   in a QT application and automatically restore the base cursor
40   """
41   def __init__(self, cursor):
42     self.cursor = cursor
43
44   def __enter__(self):
45     #QtGui.QApplication.setOverrideCursor(self.cursor)
46     QApplication.setOverrideCursor(self.cursor)
47
48   def __exit__(self, exc_type, exc_value, traceback):
49     #QtGui.QApplication.restoreOverrideCursor()
50     QApplication.restoreOverrideCursor()
51
52 #wait_cursor = OverrideCursorCM(QCursor(QtCore.Qt.WaitCursor))
53 wait_cursor = OverrideCursorCM(QCursor(Qt.WaitCursor))
54
55
56 def get_and_check_selected_file_path():
57   """
58   Get the first selected item in the object browser, check that its "Comment"
59   attribute contains a valid file path, and return this file path.
60   """
61   ed = getStudyEditor()
62   if len(salome.sg.getAllSelected()) == 0 : return None
63   sobj = ed.study.FindObjectID(salome.sg.getSelected(0))
64   filepath = sobj.GetComment()
65   if not os.path.isfile(filepath):
66     #raise HSGUIException(QtGui.QApplication.translate("get_and_check_selected_file_path",
67     raise HSGUIException(QApplication.translate("get_and_check_selected_file_path",
68                                                  "File %1 does not exist").arg(filepath))
69   return filepath