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