Salome HOME
Copyright update: 2016
[plugins/xtplugin.git] / src / XTPlugin_GUI.cxx
1 // Copyright (C) 2014-2016  OPEN CASCADE
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, or (at your option) any later version.
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/ or email : webmaster.salome@opencascade.com
18 //
19
20 // internal includes
21 #include "XTPlugin_GUI.h"
22 #include "XTPlugin_IOperations_i.hh"
23
24 // GUI includes
25 #include <SUIT_Desktop.h>
26 #include <SUIT_FileDlg.h>
27 #include <SUIT_MessageBox.h>
28 #include <SUIT_OverrideCursor.h>
29 #include <SUIT_Tools.h>
30 #include <LightApp_SelectionMgr.h>
31 #include <SalomeApp_Application.h>
32 #include <SalomeApp_Study.h>
33 #include <SALOME_ListIO.hxx>
34
35 // GEOM includes
36 #include "GeometryGUI.h"
37 #include "GEOM_Operation.h"
38 #include "GEOMBase.h"
39 #include "GEOM_Displayer.h"
40
41 #include <SALOMEconfig.h>
42 #include CORBA_SERVER_HEADER(XTPlugin)
43
44 //=======================================================================
45 // function : XTPlugin_GUI()
46 // purpose  : Constructor
47 //=======================================================================
48 XTPlugin_GUI::XTPlugin_GUI( GeometryGUI* parent ) : GEOMPluginGUI( parent )
49 {
50 }
51
52 //=======================================================================
53 // function : ~XTPlugin_GUI
54 // purpose  : Destructor
55 //=======================================================================
56 XTPlugin_GUI::~XTPlugin_GUI()
57 {
58 }
59
60 //=======================================================================
61 // function : OnGUIEvent()
62 // purpose  : 
63 //=======================================================================
64 bool XTPlugin_GUI::OnGUIEvent( int theCommandID, SUIT_Desktop* parent )
65 {
66   QString cmd;
67   switch ( theCommandID ) {
68   case 1:
69     cmd = "Import_XT";
70   default:
71     break;
72   }
73   return OnGUIEvent( cmd, parent );
74 }
75
76 //=======================================================================
77 // function : OnGUIEvent()
78 // purpose  :
79 //=======================================================================
80 bool XTPlugin_GUI::OnGUIEvent( const QString& theCommandID, SUIT_Desktop* parent )
81 {
82   bool result = false;
83
84   if ( theCommandID == "Import_XT" )
85   {
86     result = importXT( parent );
87   }
88   else
89   {
90     getGeometryGUI()->getApp()->putInfo( tr("GEOM_PRP_COMMAND").arg( theCommandID ) );
91   }
92
93   return result;
94
95 }
96
97 //=======================================================================
98 // function : importXT
99 // purpose  :
100 //=======================================================================
101 bool XTPlugin_GUI::importXT( SUIT_Desktop* parent )
102 {
103   SalomeApp_Application* app = getGeometryGUI()->getApp();
104   if ( !app ) return false;
105   SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*> ( app->activeStudy() );
106   if ( !study ) return false;
107
108   SALOMEDS::Study_var dsStudy = GeometryGUI::ClientStudyToStudy( study->studyDS() );
109   GEOM::GEOM_IOperations_var op = GeometryGUI::GetGeomGen()->GetPluginOperations( dsStudy->StudyId(), "XTPluginEngine" );
110   XTPlugin::IXTOperations_var xtOp = XTPlugin::IXTOperations::_narrow( op );
111   if ( CORBA::is_nil( xtOp ) ) return false;
112
113   QStringList fileNames = app->getOpenFileNames( SUIT_FileDlg::getLastVisitedPath().isEmpty() ? QDir::currentPath() : QString(""),
114                                                  tr( "XT_FILES" ),
115                                                  tr( "IMPORT_TITLE" ),
116                                                  parent );
117   if ( fileNames.count() > 0 )
118   {
119     QStringList entryList;
120     QStringList errors;
121
122     foreach( QString fileName, fileNames )
123     {
124       SUIT_OverrideCursor wc;
125       GEOM_Operation transaction( app, xtOp.in() );
126
127       try
128       {
129         app->putInfo( tr( "GEOM_PRP_LOADING" ).arg( fileName ) );
130         transaction.start();
131         GEOM::ListOfGO_var result = xtOp->ImportXT( fileName.toUtf8().constData() );
132         if ( result->length() > 0 && xtOp->IsDone() )
133         {
134           GEOM::GEOM_Object_var main = result[0];
135           QString publishName = GEOMBase::GetDefaultName( SUIT_Tools::file( fileName, true ) );
136           SALOMEDS::SObject_var so = GeometryGUI::GetGeomGen()->PublishInStudy( dsStudy,
137                                                   SALOMEDS::SObject::_nil(),
138                                                   main.in(),
139                                                   publishName.toUtf8().constData() );
140
141           entryList.append( so->GetID() );
142           transaction.commit();
143           GEOM_Displayer( study ).Display( main.in() );
144        }
145        else
146        {
147          transaction.abort();
148          errors.append( QString( "%1 : %2" ).arg( fileName ).arg( xtOp->GetErrorCode() ) );
149        }
150      }
151      catch( const SALOME::SALOME_Exception& e )
152      {
153        transaction.abort();
154      }
155    }
156    getGeometryGUI()->updateObjBrowser( true );
157    app->browseObjects( entryList );
158
159    if ( errors.count() > 0 )
160    {
161      SUIT_MessageBox::critical( parent,
162                                 tr( "GEOM_ERROR" ),
163                                 tr( "GEOM_IMPORT_ERRORS" ) + "\n" + errors.join( "\n" ) );
164    }
165   }
166   return fileNames.count() > 0;
167 }
168
169 //=====================================================================================
170 // EXPORTED METHODS
171 //=====================================================================================
172 extern "C"
173 {
174 #ifdef WIN32
175   __declspec( dllexport )
176 #endif
177   GEOMGUI* GetLibGUI( GeometryGUI* parent )
178   {
179     return new XTPlugin_GUI( parent );
180   }
181 }