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