Salome HOME
eeb12e91ff4f7fa1d04d871af3ccbffb61388ef9
[modules/gui.git] / src / SalomeApp / SalomeApp_Tools.cxx
1 //  Copyright (C) 2007-2008  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 #include "SalomeApp_Tools.h"
23
24 #include <SUIT_Session.h>
25 #include <SUIT_Desktop.h>
26 #include <SUIT_MessageBox.h>
27
28 #include <utilities.h>
29
30 #include <QColor>
31 #include <QString>
32
33 #include <Quantity_Color.hxx>
34
35 /*!
36   Convert QColor to Quantity_Color, if QColor is valid.
37 */
38 Quantity_Color SalomeApp_Tools::color( const QColor& c )
39 {
40         Quantity_Color aColor;
41         if ( c.isValid() )
42                 aColor = Quantity_Color( c.red() / 255., c.green() / 255., c.blue() / 255., Quantity_TOC_RGB );
43         return aColor;
44 }
45
46 /*!
47   Convert Quantity_Color to QColor.
48 */
49 QColor SalomeApp_Tools::color( const Quantity_Color& c )
50 {
51         return QColor( (int)( c.Red() * 255 ), (int)( c.Green() * 255 ), (int)( c.Blue() * 255 ) );
52 }
53
54 /*!
55   Gets message on exception \a S_ex.
56 */
57 QString SalomeApp_Tools::ExceptionToString( const SALOME::SALOME_Exception& S_ex )
58 {
59   QString message;
60   
61   switch ( S_ex.details.type )
62   {
63   case SALOME::COMM:
64   case SALOME::INTERNAL_ERROR:
65     {
66             message = QString( S_ex.details.text );
67             QString source( S_ex.details.sourceFile );
68             QString line;
69             line.setNum( S_ex.details.lineNumber );
70             message = message + " \n" + source + " : " + line;
71       break;
72     }
73   case SALOME::BAD_PARAM:
74     {
75             message = QString( S_ex.details.text );
76 #ifdef _DEBUG_
77             QString source( S_ex.details.sourceFile );
78             QString line;
79             line.setNum( S_ex.details.lineNumber );
80             message = message + " \n" + source + " : " + line;
81 #endif
82             break;
83     }
84   default:
85     {
86             message = QString( "SALOME CORBA Exception Type invalid" );
87             QString source( S_ex.details.sourceFile );
88             QString line;
89             line.setNum( S_ex.details.lineNumber );
90             message = message + " \n" + source + " : " + line;
91             break;
92     }
93   }
94   return message;
95 }
96
97 /*!
98   Gets message box on exception \a S_ex.
99 */
100 void SalomeApp_Tools::QtCatchCorbaException( const SALOME::SALOME_Exception& S_ex )
101 {
102   QString message = ExceptionToString( S_ex );
103
104   QString title;
105   bool error = true;
106   switch ( S_ex.details.type )
107   {
108   case SALOME::COMM:
109   case SALOME::INTERNAL_ERROR:
110     title = QObject::tr( "Engine Error" );
111     break;
112   case SALOME::BAD_PARAM:
113     error = false;
114     title = QObject::tr( "Engine Warning" );
115           break;
116   default:
117     title = QObject::tr( "Internal SALOME Error" );
118     break;
119   }
120
121   if ( error )
122     SUIT_MessageBox::critical( SUIT_Session::session()->activeApplication()->desktop(),
123                                title, message );
124   else
125     SUIT_MessageBox::warning( SUIT_Session::session()->activeApplication()->desktop(),
126                               title, message );
127
128 }