Salome HOME
new version with output MED file creation
[modules/filter.git] / src / FILTERGUI / FilterGUI.cxx
1 //  FILTER FILTERGUI : FILTER component GUI implemetation
2 //
3 //  Copyright (C) 2003  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 //
23 //
24 //  File   : FilterGUI.cxx
25 //  Module : FILTER
26
27 #include "utilities.h"
28 #include <FilterGUI_Selection.h>
29 #include "SelectParams.h"
30 #include "SelectField.h"
31 #include "FilterGUI.h"
32
33 // SALOME Includes
34 #include "Utils_ORB_INIT.hxx"
35 #include "Utils_SINGLETON.hxx"
36
37 #include <SALOME_LifeCycleCORBA.hxx>
38 #include <SALOME_InteractiveObject.hxx>
39 #include <SALOME_ListIO.hxx>
40 #include <SalomeApp_Tools.h>
41
42 #include <SUIT_MessageBox.h>
43 #include <SUIT_Tools.h>
44 #include <SUIT_FileDlg.h>
45 #include <SUIT_ResourceMgr.h>
46
47 #include <CAM_Application.h>
48 #include <SalomeApp_Application.h>
49 #include <SalomeApp_DataModel.h>
50 #include <SalomeApp_Study.h>
51 #include <SALOMEDSClient_Study.hxx>
52 #include <SALOMEDSClient_SObject.hxx>
53
54 #include <LightApp_SelectionMgr.h>
55
56 #include <OB_Browser.h>
57
58 // QT Includes
59 #include <qinputdialog.h>
60 #include <qmessagebox.h>
61 #include <qcursor.h>
62
63 //VRV: porting on Qt 3.0.5
64 #if QT_VERSION >= 0x030005
65 #include <qlineedit.h>
66 #endif
67 //VRV: porting on Qt 3.0.5
68
69 using namespace std;
70
71 static CORBA::ORB_var   _orb;
72
73 //=============================================================================
74 /*!
75  *
76  */
77 //=============================================================================
78 FilterGUI::FilterGUI() :
79   SalomeApp_Module( "FILTER" ),_sel(0)
80 {
81 }
82
83 //=============================================================================
84 /*!
85  *
86  */
87 //=============================================================================
88 void FilterGUI::createPopupItem( const int id,
89                               const QString& clients,
90                               const QString& types,
91                               const QString& theRule,
92                               const int pId )
93 {
94   int parentId = pId;
95   if( pId!=-1 )
96     parentId = popupMgr()->actionId( action( pId ) );
97
98   if( !popupMgr()->contains( popupMgr()->actionId( action( id ) ) ) )
99     popupMgr()->insert( action( id ), parentId, 0 );
100
101   QChar lc = QtxPopupMgr::Selection::defEquality();
102   QString rule = "(%1)";
103   if( !types.isEmpty() )
104     rule += " and (%2) and (%3)";
105
106   rule = rule.arg( QString( "client in {%1}" ).arg( clients ) );
107
108   if( !types.isEmpty() )
109   {
110     rule = rule.arg( QString( "%1>0" ).arg( QtxPopupMgr::Selection::defSelCountParam() ) );
111     rule = rule.arg( QString( "%1type in {%2}" ).arg( lc ).arg( types ) );
112   }
113   rule += theRule;
114   popupMgr()->setRule( action( id ), rule, true );
115 }
116
117 void FilterGUI::createFilterAction( const int id, const QString& po_id, const QString& icon_id )
118 {
119   QWidget* parent = application()->desktop();
120   SUIT_ResourceMgr* mgr = application()->resourceMgr();
121
122   QPixmap pix; QIconSet icon;
123   if( !icon_id.isEmpty() )
124     pix = mgr->loadPixmap( "FILTER", tr( icon_id ) );
125 //   else
126 //     pix = mgr->loadPixmap( "FILTER", tr( QString( "ICO_" )+po_id ) );
127
128   if ( !pix.isNull() )
129     icon = QIconSet( pix );
130
131   createAction( id, tr( "TOP_" + po_id ), icon, tr( "MEN_" + po_id ), tr( "STB_" + po_id ), 0, parent, false, this, SLOT( onGUIEvent() ) );
132 }
133
134 //=============================================================================
135 /*!
136  *
137  */
138 //=============================================================================
139 void FilterGUI::initialize( CAM_Application* app )
140 {
141   SalomeApp_Module::initialize( app );
142
143 //   QWidget* parent = application()->desktop();
144
145   createFilterAction(  111, "OPEN" );
146
147   createFilterAction( 936, "FILTERING" );
148
149   int fileId   = createMenu( tr( "MEN_FILE" ),   -1,  1 );
150   createMenu( 111, fileId, 11 );
151
152   int FilterId = createMenu( tr( "DATA REDUCTION" ), -1, 50, 10 );
153   createMenu( separator(), FilterId, 10 );
154   createMenu( 936, FilterId, 11 );
155
156 }
157
158 void FilterGUI::contextMenuPopup( const QString& client, QPopupMenu* menu, QString& /*title*/ )
159 {
160   FilterGUI_Selection sel;
161   SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( application() );
162   if( app )
163   {
164     sel.init( client, app->selectionMgr() );
165     popupMgr()->updatePopup( menu, &sel );
166   }
167 }
168
169 QString FilterGUI::engineIOR() const
170 {
171 //   SALOME_FILTER::MED_Gen_ptr aMedGen = InitMedGen();
172 //   if ( !CORBA::is_nil( aMedGen) )
173 //     return QString( getApp()->orb()->object_to_string( aMedGen ));
174   return QString( "" );
175 }
176
177 void FilterGUI::windows( QMap<int, int>& mappa ) const
178 {
179   mappa.insert( SalomeApp_Application::WT_ObjectBrowser, Qt::DockLeft );
180   mappa.insert( SalomeApp_Application::WT_PyConsole, Qt::DockBottom );
181 }
182
183
184 //=============================================================================
185 /*!
186  *
187  */
188 //=============================================================================
189 void FilterGUI::onGUIEvent()
190 {
191   const QObject* obj = sender();
192   if ( !obj || !obj->inherits( "QAction" ) )
193     return;
194   int id = actionId((QAction*)obj);
195   if ( id != -1 )
196     OnGUIEvent( id );
197 }
198
199 //=============================================================================
200 /*!
201  *
202  */
203 //=============================================================================
204 void FilterGUI::EmitSignalCloseAllDialogs()
205 {
206   emit SignalCloseAllDialogs();
207 }
208
209 //=============================================================================
210 /*!
211  *
212  */
213 //=============================================================================
214 bool FilterGUI::deactivateModule( SUIT_Study* study )
215 {
216   setMenuShown( false );
217   setToolShown( false );
218
219   disconnect( application()->desktop(), SIGNAL( windowActivated( SUIT_ViewWindow* ) ),
220              this, SLOT( onWindowActivated( SUIT_ViewWindow* ) ) );
221
222   EmitSignalCloseAllDialogs();
223
224   return SalomeApp_Module::deactivateModule( study );
225 }
226
227 //=============================================================================
228 /*!
229  *
230  */
231 //=============================================================================
232 bool FilterGUI::activateModule( SUIT_Study* study )
233 {
234   bool res = SalomeApp_Module::activateModule( study );
235
236   setMenuShown( true );
237   setToolShown( true );
238
239   connect( application()->desktop(), SIGNAL( windowActivated( SUIT_ViewWindow* ) ),
240           this, SLOT( onWindowActivated( SUIT_ViewWindow* ) ) );
241   return res;
242 }
243
244 //=============================================================================
245 /*!
246  *
247  */
248 //=============================================================================
249 void FilterGUI::onWindowActivated( SUIT_ViewWindow* )
250 {
251 }
252
253 //=============================================================================
254 /*!
255  *
256  */
257 //=============================================================================
258 bool FilterGUI::OnGUIEvent (int theCommandID)
259 {
260   setOrb();
261
262   SalomeApp_Study* myActiveStudy = dynamic_cast< SalomeApp_Study* >( application()->activeStudy() );
263   if( !myActiveStudy )
264     return false;
265
266   _PTR(Study) aStudy = myActiveStudy->studyDS();
267   //SALOME_NamingService* myNameService = parent->getNameService();
268
269   QString file;
270   QStringList filtersList ;
271
272   filtersList.append( tr("FILTER_MEN_IMPORT_MED") );
273   filtersList.append( tr("FILTER_MEN_ALL_FILES") ) ;
274
275   SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( application() );
276   if( !app )
277     return false;
278
279   switch (theCommandID)
280     {
281     case 936:
282       {
283         MESSAGE("command " << theCommandID << " activated");
284
285         if(_sel){
286           SelectParams *filter;
287           try {
288             application()->desktop()->setCursor(QCursor(Qt::WaitCursor));
289             filter = new SelectParams(this,_sel);
290             filter->exec();
291             application()->desktop()->setCursor(QCursor(Qt::ArrowCursor));
292             _sel = NULL;
293           }
294           catch ( SALOME_Exception& S_ex ) {
295             QMessageBox::information( application()->desktop(),
296                                       "Filtering",
297                                       "Unable to select parameters for filtering.\n"
298                                       "You must select a reference field in a MED file before." );
299           }
300         }
301         else{
302           MESSAGE("Select an input Field in MED file before filtering!!");
303           QMessageBox::information( application()->desktop(),
304                                     "Filtering",
305                                     "Unable to select parameters for filtering.\n"
306                                     "You must select a reference field in a MED file before." );
307         }
308         break;
309       }
310     case 111:
311       {
312         MESSAGE("command " << theCommandID << " activated");
313
314         // Selection du Fichier
315         file = SUIT_FileDlg::getFileName(application()->desktop(),
316                                         "",
317                                         filtersList,
318                                         tr("FILTER_MEN_IMPORT"),
319                                         true);
320         if (!file.isEmpty() ){
321           _sel = new SelectField(this,file);
322           if ( _sel->exec() == QDialog::Rejected ){
323             delete _sel;
324             _sel = NULL;
325           }
326         }
327
328         break;
329       }
330     }
331
332   app->updateActions(); //SRN: To update a Save button in the toolbar
333
334   return true;
335 }
336
337
338 //=============================================================================
339 /*!
340  *
341  */
342 //=============================================================================
343 bool FilterGUI::OnMousePress (QMouseEvent* pe ,
344                            SUIT_ViewWindow* wnd )
345 {
346   MESSAGE("FilterGUI::OnMousePress");
347   return true;
348 }
349
350 //=============================================================================
351 /*!
352  *
353  */
354 //=============================================================================
355 bool FilterGUI::OnMouseMove (QMouseEvent* pe ,
356                           SUIT_ViewWindow* wnd )
357 {
358   //   MESSAGE("FilterGUI::OnMouseMouve");
359   return true;
360 }
361
362 //=============================================================================
363 /*!
364  *
365  */
366 //=============================================================================
367 bool FilterGUI::OnKeyPress (QKeyEvent* pe,
368                          SUIT_ViewWindow* wnd)
369 {
370   MESSAGE("FilterGUI::OnKeyPress");
371   return true;
372 }
373
374 //=============================================================================
375 /*!
376  *
377  */
378 //=============================================================================
379 void FilterGUI::setOrb()
380 {
381   try {
382     ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance();
383     ASSERT(SINGLETON_<ORB_INIT>::IsAlreadyExisting());
384     _orb = init( 0 , 0 );
385   } catch (...) {
386     INFOS("internal error : orb not found");
387     _orb = 0;
388   }
389   ASSERT(! CORBA::is_nil(_orb));
390 }
391
392 extern "C" {
393   Standard_EXPORT CAM_Module* createModule() {
394     return new FilterGUI();
395   }
396 }