Salome HOME
Merge branch 'abn/paravis_rearch'
[modules/gui.git] / src / LightApp / LightApp_Module.cxx
1 // Copyright (C) 2007-2014  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, or (at your option) any later version.
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 // File:      LightApp_Module.cxx
24 // Created:   6/20/2005 16:30:56 AM
25 // Author:    OCC team
26
27 #include "LightApp_Module.h"
28
29 #include "CAM_Application.h"
30
31 #include "LightApp_Application.h"
32 #include "LightApp_DataModel.h"
33 #include "LightApp_DataObject.h"
34 #include "LightApp_Study.h"
35 #include "LightApp_Preferences.h"
36 #include "LightApp_Selection.h"
37 #include "LightApp_Operation.h"
38 #include "LightApp_SwitchOp.h"
39 #include "LightApp_UpdateFlags.h"
40 #include "LightApp_ShowHideOp.h"
41 #include "LightApp_SelectionMgr.h"
42
43 #include <SUIT_Study.h>
44 #include <SUIT_DataObject.h>
45 #include <SUIT_DataBrowser.h>
46 #include <SUIT_Operation.h>
47 #include <SUIT_ViewManager.h>
48 #include <SUIT_ResourceMgr.h>
49 #include <SUIT_ShortcutMgr.h>
50 #include <SUIT_Desktop.h>
51 #include <SUIT_TreeModel.h>
52 #include <SUIT_Session.h>
53
54 #ifndef DISABLE_SALOMEOBJECT
55 #include <SALOME_ListIO.hxx>
56 #include <SALOME_ListIteratorOfListIO.hxx>
57 #endif
58
59 #ifndef DISABLE_VTKVIEWER
60 #ifndef DISABLE_SALOMEOBJECT
61   #include <SVTK_ViewWindow.h>
62   #include <SVTK_ViewModel.h>
63 #else
64   #include <VTKViewer_ViewWindow.h>
65 #endif
66   #include <VTKViewer_ViewModel.h>
67 #endif
68 #ifndef DISABLE_OCCVIEWER
69   #include <OCCViewer_ViewWindow.h>
70   #include <OCCViewer_ViewPort3d.h>
71 #ifndef DISABLE_SALOMEOBJECT
72   #include <SOCC_ViewModel.h>
73 #else
74   #include <OCCViewer_ViewModel.h>
75 #endif
76 #endif
77 #ifndef DISABLE_GLVIEWER
78   #include <GLViewer_ViewFrame.h>
79   #include <GLViewer_ViewPort.h>
80 #endif
81 #ifndef DISABLE_PVVIEWER
82   #include <PVViewer_ViewManager.h>
83   #include <PVViewer_ViewWindow.h>
84 #endif
85 #ifndef DISABLE_PLOT2DVIEWER
86   #include <Plot2d_ViewWindow.h>
87   #include <Plot2d_ViewFrame.h>
88 #ifndef DISABLE_SALOMEOBJECT
89   #include <SPlot2d_ViewModel.h>
90 #else
91   #include <Plot2d_ViewModel.h>
92 #endif
93 #endif
94
95 #include <QtxPopupMgr.h>
96
97 #include <QVariant>
98 #include <QString>
99 #include <QStringList>
100 #include <QAction>
101
102 #include <iostream>
103
104 /*!Constructor.*/
105 LightApp_Module::LightApp_Module( const QString& name )
106 : CAM_Module( name ),
107   myPopupMgr( 0 ),
108   mySwitchOp( 0 ),
109   myDisplay( -1 ),
110   myErase( -1 ),
111   myDisplayOnly( -1 ),
112   myEraseAll( -1 ),
113   myIsFirstActivate( true )
114 {
115 }
116
117 /*!Destructor.*/
118 LightApp_Module::~LightApp_Module()
119 {
120   if ( mySwitchOp )
121     delete mySwitchOp;
122 }
123
124 /*!Initialize module.*/
125 void LightApp_Module::initialize( CAM_Application* app )
126 {
127   CAM_Module::initialize( app );
128
129   SUIT_ResourceMgr* resMgr = app ? app->resourceMgr() : 0;
130   if ( resMgr )
131     resMgr->raiseTranslators( name() );
132 }
133
134 /*!NOT IMPLEMENTED*/
135 void LightApp_Module::windows( QMap<int, int>& ) const
136 {
137 }
138
139 /*!NOT IMPLEMENTED*/
140 void LightApp_Module::viewManagers( QStringList& ) const
141 {
142 }
143
144 /*!Context menu popup.*/
145 void LightApp_Module::contextMenuPopup( const QString& client, QMenu* menu, QString& /*title*/ )
146 {
147   LightApp_Selection* sel = createSelection();
148   sel->init( client, getApp()->selectionMgr() );
149
150   popupMgr()->setSelection( sel );
151   popupMgr()->setMenu( menu );
152   popupMgr()->updateMenu();
153 }
154
155 /*!Update object browser.
156  * For updating model or whole object browser use update() method can be used.
157 */
158 void LightApp_Module::updateObjBrowser( bool theIsUpdateDataModel, 
159                                         SUIT_DataObject* theDataObject )
160 {
161   bool upd = getApp()->objectBrowser()->autoUpdate();
162   getApp()->objectBrowser()->setAutoUpdate( false );
163
164   if( theIsUpdateDataModel ){
165     if( CAM_DataModel* aDataModel = dataModel() ){
166       if ( LightApp_DataModel* aModel = dynamic_cast<LightApp_DataModel*>( aDataModel ) ) {
167         //SUIT_DataObject* aParent = NULL;
168         //if(theDataObject && theDataObject != aDataModel->root())
169         //  aParent = theDataObject->parent();
170
171         LightApp_DataObject* anObject = dynamic_cast<LightApp_DataObject*>(theDataObject);
172         LightApp_Study* aStudy = dynamic_cast<LightApp_Study*>(getApp()->activeStudy());
173         aModel->update( anObject, aStudy );
174       }
175     }
176   }
177
178   getApp()->objectBrowser()->setAutoUpdate( upd );
179   getApp()->objectBrowser()->updateTree( 0, false );
180 }
181
182 /*!NOT IMPLEMENTED*/
183 void LightApp_Module::selectionChanged()
184 {
185 }
186
187 /*! \brief If return false, selection will be cleared at module activation
188  */
189 bool LightApp_Module::isSelectionCompatible()
190 {
191   // return true if selected objects belong to this module
192   bool isCompatible = true;
193 #ifndef DISABLE_SALOMEOBJECT
194   SALOME_ListIO selected;
195   if ( LightApp_SelectionMgr *Sel = getApp()->selectionMgr() )
196     Sel->selectedObjects( selected );
197
198   LightApp_Study* aStudy = dynamic_cast<LightApp_Study*>( getApp()->activeStudy() );
199   LightApp_DataObject* aRoot = dynamic_cast<LightApp_DataObject*>( dataModel()->root() );
200   if ( aStudy && aRoot ) {
201     // my data type
202     QString moduleDataType = aRoot->componentDataType();
203     // check data type of selection
204     SALOME_ListIteratorOfListIO It( selected );
205     for ( ; isCompatible && It.More(); It.Next()) {
206       Handle(SALOME_InteractiveObject)& io = It.Value();
207       isCompatible = ( aStudy->componentDataType( io->getEntry() ) == moduleDataType );
208     }
209   }
210 #endif
211   return isCompatible;
212 }
213
214 /*!Activate module.*/
215 bool LightApp_Module::activateModule( SUIT_Study* study )
216 {
217   bool res = CAM_Module::activateModule( study );
218
219   if ( !isSelectionCompatible() )// PAL19290, PAL18352
220     getApp()->selectionMgr()->clearSelected();
221
222   if ( res && application() && application()->resourceMgr() )
223     application()->resourceMgr()->raiseTranslators( name() );
224
225   connect( application(), SIGNAL( viewManagerAdded( SUIT_ViewManager* ) ),
226            this, SLOT( onViewManagerAdded( SUIT_ViewManager* ) ) );
227   connect( application(), SIGNAL( viewManagerRemoved( SUIT_ViewManager* ) ),
228            this, SLOT( onViewManagerRemoved( SUIT_ViewManager* ) ) );
229
230   if ( mySwitchOp == 0 )
231     mySwitchOp = new LightApp_SwitchOp( this );
232
233   // Enable Display and Erase actions
234   if ( action(myDisplay) )
235     action(myDisplay)->setEnabled(true);
236   if ( action(myErase) )
237     action(myErase)->setEnabled(true);
238
239   application()->shortcutMgr()->setSectionEnabled( moduleName() );
240
241   /*  BUG 0020498 : The Entry column is always shown at module activation
242       The registration of column is moved into LightApp_Application
243
244   QString EntryCol = QObject::tr( "ENTRY_COLUMN" );
245   LightApp_DataModel* m = dynamic_cast<LightApp_DataModel*>( dataModel() );
246   if( m )
247   {
248     SUIT_AbstractModel* treeModel = dynamic_cast<SUIT_AbstractModel*>( getApp()->objectBrowser()->model() );
249     m->registerColumn( getApp()->objectBrowser(), EntryCol, LightApp_DataObject::EntryId );
250     treeModel->setAppropriate( EntryCol, Qtx::Toggled );
251   }*/
252
253   if ( myIsFirstActivate ) {
254     updateModuleVisibilityState();
255     myIsFirstActivate = false;
256   }
257   
258   return res;
259 }
260
261 /*!Deactivate module.*/
262 bool LightApp_Module::deactivateModule( SUIT_Study* study )
263 {
264   delete mySwitchOp;
265   mySwitchOp = 0;
266
267   disconnect( application(), SIGNAL( viewManagerAdded( SUIT_ViewManager* ) ),
268               this, SLOT( onViewManagerAdded( SUIT_ViewManager* ) ) );
269   disconnect( application(), SIGNAL( viewManagerRemoved( SUIT_ViewManager* ) ),
270               this, SLOT( onViewManagerRemoved( SUIT_ViewManager* ) ) );
271
272   // abort all operations
273   MapOfOperation::const_iterator anIt;
274   for( anIt = myOperations.begin(); anIt != myOperations.end(); anIt++ ) {
275     anIt.value()->abort();
276   }
277
278   // Disable Display and Erase action
279   if ( action(myDisplay) )
280     action(myDisplay)->setEnabled(false);
281   if ( action(myErase) )
282     action(myErase)->setEnabled(false);
283
284   application()->shortcutMgr()->setSectionEnabled( moduleName(), false );
285   
286   /*  BUG 0020498 : The Entry column is always shown at module activation
287   QString EntryCol = QObject::tr( "ENTRY_COLUMN" );
288   LightApp_DataModel* m = dynamic_cast<LightApp_DataModel*>( dataModel() );
289   if( m )
290   {
291     SUIT_AbstractModel* treeModel = dynamic_cast<SUIT_AbstractModel*>( getApp()->objectBrowser()->model() );
292
293     treeModel->setAppropriate( EntryCol, Qtx::Shown );
294     m->unregisterColumn( getApp()->objectBrowser(), EntryCol );
295   }
296   */
297   return CAM_Module::deactivateModule( study );
298 }
299
300 /*! Redefined to reset internal flags valid for study instance */
301 void LightApp_Module::studyClosed( SUIT_Study* theStudy )
302 {
303   CAM_Module::studyClosed( theStudy );
304   
305   myIsFirstActivate = true;
306   
307   LightApp_Application* app = dynamic_cast<LightApp_Application*>(application());
308   if ( app ) {
309     SUIT_DataBrowser* ob = app->objectBrowser();
310     if ( ob && ob->model() )
311       disconnect( ob->model(), SIGNAL( clicked( SUIT_DataObject*, int ) ),
312                   this, SLOT( onObjectClicked( SUIT_DataObject*, int ) ) );
313   }
314 }
315
316 /*!NOT IMPLEMENTED*/
317 void LightApp_Module::MenuItem()
318 {
319 }
320
321 /*!NOT IMPLEMENTED*/
322 void LightApp_Module::createPreferences()
323 {
324 }
325
326 /*!NOT IMPLEMENTED*/
327 void LightApp_Module::preferencesChanged( const QString&, const QString& )
328 {
329 }
330
331 /*!NOT IMPLEMENTED*/
332 void LightApp_Module::message( const QString& )
333 {
334 }
335
336 /*!Gets application.*/
337 LightApp_Application* LightApp_Module::getApp() const
338 {
339   return (LightApp_Application*)application();
340 }
341
342 /*!
343  * \brief Update something in accordance with update flags
344  * \param theFlags - update flags
345 *
346 * Update viewer or/and object browser etc. in accordance with update flags ( see
347 * LightApp_UpdateFlags enumeration ). Derived modules can redefine this method for their
348 * own purposes
349 */
350 void LightApp_Module::update( const int theFlags )
351 {
352   if ( theFlags & UF_Model )
353   {
354     if( CAM_DataModel* aDataModel = dataModel() )
355       if( LightApp_DataModel* aModel = dynamic_cast<LightApp_DataModel*>( aDataModel ) )
356         aModel->update( 0, dynamic_cast<LightApp_Study*>( getApp()->activeStudy() ) );
357   }
358
359   if ( theFlags & UF_ObjBrowser )
360     getApp()->objectBrowser()->updateTree( 0 );
361
362   if ( theFlags & UF_Controls )
363     updateControls();
364   if ( theFlags & UF_Viewer )
365   {
366     if ( SUIT_ViewManager* viewMgr = getApp()->activeViewManager() )
367       if ( SUIT_ViewWindow* viewWnd = viewMgr->getActiveView() )
368       {
369 #ifndef DISABLE_VTKVIEWER
370 #ifndef DISABLE_SALOMEOBJECT
371         if ( viewWnd->inherits( "SVTK_ViewWindow" ) )
372           ( (SVTK_ViewWindow*)viewWnd )->Repaint();
373 #else
374         if ( viewWnd->inherits( "VTKViewer_ViewWindow" ) )
375           ( (VTKViewer_ViewWindow*)viewWnd )->Repaint();
376 #endif
377 #endif
378 #ifndef DISABLE_OCCVIEWER
379         if ( viewWnd->inherits( "OCCViewer_ViewWindow" ) )
380           ( (OCCViewer_ViewWindow*)viewWnd )->getViewPort()->onUpdate();
381 #endif
382 #ifndef DISABLE_PLOT2DVIEWER
383         if ( viewWnd->inherits( "Plot2d_ViewWindow" ) )
384           ( (Plot2d_ViewWindow*)viewWnd )->getViewFrame()->Repaint();
385 #endif
386 #ifndef DISABLE_GLVIEWER
387         if ( viewWnd->inherits( "GLViewer_ViewFrame" ) )
388           ( (GLViewer_ViewFrame*)viewWnd )->getViewPort()->onUpdate();
389 #endif
390 #ifndef DISABLE_PVVIEWER
391 //        if ( viewWnd->inherits( "PVViewer_ViewWindow" ) )
392 //          ( (PVViewer_ViewWindow*)viewWnd )->getViewPort()->onUpdate();
393 #endif
394       }
395   }
396 }
397 /*!
398  * \brief Updates controls
399 *
400 * Updates (i.e. disable/enable) controls states (menus, tool bars etc.). This method is
401 * called from update( UF_Controls ). You may redefine it in concrete module.
402 */
403 void LightApp_Module::updateControls()
404 {
405 }
406
407 /*!Create new instance of data model and return it.*/
408 CAM_DataModel* LightApp_Module::createDataModel()
409 {
410   return new LightApp_DataModel( this );
411 }
412
413 /*!Create and return instance of LightApp_Selection.*/
414 LightApp_Selection* LightApp_Module::createSelection() const
415 {
416   return new LightApp_Selection();
417 }
418
419 /*!NOT IMPLEMENTED*/
420 void LightApp_Module::onModelOpened()
421 {
422 }
423
424 /*!NOT IMPLEMENTED*/
425 void LightApp_Module::onModelSaved()
426 {
427 }
428
429 /*!NOT IMPLEMENTED*/
430 void LightApp_Module::onModelClosed()
431 {
432 }
433
434 /*!Gets popup manager.(create if not exist)*/
435 QtxPopupMgr* LightApp_Module::popupMgr()
436 {
437   if ( !myPopupMgr )
438   {
439     myPopupMgr = new QtxPopupMgr( 0, this );
440
441     QPixmap p;
442     SUIT_Desktop* d = application()->desktop();
443     
444     QAction 
445       *disp = createAction( -1, tr( "TOP_SHOW" ), p, tr( "MEN_SHOW" ), tr( "STB_SHOW" ),
446                             0, d, false, this, SLOT( onShowHide() ), QString("General:Show object(s)") ),
447       *erase = createAction( -1, tr( "TOP_HIDE" ), p, tr( "MEN_HIDE" ), tr( "STB_HIDE" ),
448                              0, d, false, this, SLOT( onShowHide() ) , QString("General:Hide object(s)") ),
449       *dispOnly = createAction( -1, tr( "TOP_DISPLAY_ONLY" ), p, tr( "MEN_DISPLAY_ONLY" ), tr( "STB_DISPLAY_ONLY" ),
450                                 0, d, false, this, SLOT( onShowHide() ) ),
451       *eraseAll = createAction( -1, tr( "TOP_ERASE_ALL" ), p, tr( "MEN_ERASE_ALL" ), tr( "STB_ERASE_ALL" ),
452                                 0, d, false, this, SLOT( onShowHide() ) );
453     myDisplay     = actionId( disp );
454     myErase       = actionId( erase );
455     myDisplayOnly = actionId( dispOnly );
456     myEraseAll    = actionId( eraseAll );
457
458     myPopupMgr->insert( disp, -1, 0 ); 
459     myPopupMgr->insert( erase, -1, 0 );
460     myPopupMgr->insert( dispOnly, -1, 0 );
461     myPopupMgr->insert( eraseAll, -1, 0 );
462     myPopupMgr->insert( separator(), -1, 0 );
463
464     QString oneAndNotActive = "( count( $component ) = 1 ) and ( not( activeModule in $component ) )";
465     QString uniform = "true in $canBeDisplayed and %1 and ( activeModule = '%2' )";
466     uniform = uniform.arg( oneAndNotActive ).arg( name() );
467     myPopupMgr->setRule( disp, /*QString( "( not isVisible ) and " ) + */ uniform, QtxPopupMgr::VisibleRule );
468     myPopupMgr->setRule( erase, /*QString( "( isVisible ) and " ) + */ uniform, QtxPopupMgr::VisibleRule );
469     myPopupMgr->setRule( dispOnly, uniform, QtxPopupMgr::VisibleRule );
470
471     QStringList viewers;
472
473 #ifndef DISABLE_OCCVIEWER
474 #ifndef DISABLE_SALOMEOBJECT
475     viewers.append( SOCC_Viewer::Type() );
476 #else
477     viewers.append( OCCViewer_Viewer::Type() );
478 #endif
479 #endif
480 #ifndef DISABLE_VTKVIEWER
481 #ifndef DISABLE_SALOMEOBJECT
482     viewers.append( SVTK_Viewer::Type() );
483 #else
484     viewers.append( VTKViewer_Viewer::Type() );
485 #endif
486 #endif
487 #ifndef DISABLE_PLOT2DVIEWER
488 #ifndef DISABLE_SALOMEOBJECT
489     viewers.append( SPlot2d_Viewer::Type() );
490 #else
491     viewers.append( Plot2d_Viewer::Type() );
492 #endif
493 #endif
494
495     if( !viewers.isEmpty() )
496     {
497       QString strViewers = "{ ", temp = "'%1' ";
498       QStringList::const_iterator anIt = viewers.begin(), aLast = viewers.end();
499       for( ; anIt!=aLast; anIt++ )
500         strViewers+=temp.arg( *anIt );
501       strViewers+="}";
502       myPopupMgr->setRule( eraseAll, QString( "client in %1" ).arg( strViewers ), QtxPopupMgr::VisibleRule );
503     }
504   }
505   return myPopupMgr;
506 }
507
508 /*!Gets preferences.*/
509 LightApp_Preferences* LightApp_Module::preferences() const
510 {
511   LightApp_Preferences* pref = 0;
512   if ( getApp() )
513     pref = getApp()->preferences();
514   return pref;
515 }
516
517 /*!Add preference to preferences.*/
518 int LightApp_Module::addPreference( const QString& label )
519 {
520   LightApp_Preferences* pref = preferences();
521   if ( !pref )
522     return -1;
523
524   int catId = pref->addPreference( moduleName(), -1 );
525   if ( catId == -1 )
526     return -1;
527
528   return pref->addPreference( label, catId );
529 }
530
531 /*!Add preference to preferences.*/
532 int LightApp_Module::addPreference( const QString& label, const int pId, const int type,
533                                     const QString& section, const QString& param )
534 {
535   LightApp_Preferences* pref = preferences();
536   if ( !pref )
537     return -1;
538
539   return pref->addPreference( moduleName(), label, pId, type, section, param );
540 }
541
542 /*!Gets property of preferences.*/
543 QVariant LightApp_Module::preferenceProperty( const int id, const QString& prop ) const
544 {
545   QVariant var;
546   LightApp_Preferences* pref = preferences();
547   if ( pref )
548     var = pref->itemProperty( prop, id );
549   return var;
550 }
551
552 /*!Set property of preferences.*/
553 void LightApp_Module::setPreferenceProperty( const int id, const QString& prop, const QVariant& var )
554 {
555   LightApp_Preferences* pref = preferences();
556   if ( pref )
557     pref->setItemProperty( prop, var, id );
558 }
559
560 /*!
561  * \brief Starts operation with given identifier
562   * \param id - identifier of operation to be started
563 *
564 * Module stores operations in map. This method starts operation by id.
565 * If operation isn't in map, then it will be created by createOperation method
566 * and will be inserted to map
567 */
568 void LightApp_Module::startOperation( const int id )
569 {
570   LightApp_Operation* op = 0;
571   if( myOperations.contains( id ) && reusableOperation( id ) )
572     op = myOperations[ id ];
573   else
574   {
575     op = createOperation( id );
576     if( op )
577     {
578       myOperations.insert( id, op );
579       op->setModule( this );
580       connect( op, SIGNAL( stopped( SUIT_Operation* ) ), this, SLOT( onOperationStopped( SUIT_Operation* ) ) );
581       connect( op, SIGNAL( destroyed() ), this, SLOT( onOperationDestroyed() ) );
582     }
583   }
584
585   if( op )
586   {
587     // be sure that operation has correct study pointer
588     op->setStudy( application() ? application()->activeStudy() : 0 );
589     op->start();
590   }
591 }
592
593 /*!
594  * \brief Creates operation with given identifier
595   * \param id - identifier of operation to be started
596   * \return Pointer on created operation or NULL if operation is not created
597 *
598 * Creates operation with given id. You should not call this method, it will be called
599 * automatically from startOperation. You may redefine this method in concrete module to
600 * create operations. 
601 */
602 LightApp_Operation* LightApp_Module::createOperation( const int id ) const
603 {
604   if( id==-1 )
605     return 0;
606
607   if( id==myDisplay )
608     return new LightApp_ShowHideOp( LightApp_ShowHideOp::DISPLAY );
609   else if( id==myErase )
610     return new LightApp_ShowHideOp( LightApp_ShowHideOp::ERASE );
611   else if( id==myDisplayOnly )
612     return new LightApp_ShowHideOp( LightApp_ShowHideOp::DISPLAY_ONLY );
613   else if( id==myEraseAll )
614     return new LightApp_ShowHideOp( LightApp_ShowHideOp::ERASE_ALL );
615   else
616     return 0;
617 }
618
619 /*!
620  * \brief Virtual protected slot called when operation stopped
621  * \param theOp - stopped operation
622 *
623 * Virtual protected slot called when operation stopped. Redefine this slot if you want to
624 * perform actions after stopping operation
625 */
626 void LightApp_Module::onOperationStopped( SUIT_Operation* /*theOp*/ )
627 {
628 }
629
630 /*!
631  * \brief Virtual protected slot called when operation destroyed
632   * \param theOp - destroyed operation
633 *
634 * Virtual protected slot called when operation destroyed. Redefine this slot if you want to
635 * perform actions after destroying operation. Base implementation removes pointer on
636 * destroyed operation from the map of operations
637 */
638 void LightApp_Module::onOperationDestroyed()
639 {
640   const QObject* s = sender();
641   if( s && s->inherits( "LightApp_Operation" ) )
642   {
643     const LightApp_Operation* op = ( LightApp_Operation* )s;
644     MapOfOperation::const_iterator anIt = myOperations.begin(),
645                                    aLast = myOperations.end();
646     for( ; anIt!=aLast; anIt++ )
647       if( anIt.value()==op )
648       {
649         myOperations.remove( anIt.key() );
650         break;
651       }
652   }
653 }
654
655 /*!
656   Must be redefined in order to use standard displayer mechanism
657   \return displayer of module
658 */
659 LightApp_Displayer* LightApp_Module::displayer()
660 {
661   return 0;
662 }
663
664 /*!
665   SLOT: called on activating of standard operations show/hide
666 */
667 void LightApp_Module::onShowHide()
668 {
669   if( !sender()->inherits( "QAction" ) || !popupMgr() )
670     return;
671
672   QAction* act = ( QAction* )sender();
673   int id = actionId( act );
674   if( id!=-1 )
675     startOperation( id );
676 }
677
678 /*!
679   virtual SLOT: called on view manager adding
680 */
681 void LightApp_Module::onViewManagerAdded( SUIT_ViewManager* )
682 {
683 }
684
685 /*!
686   virtual SLOT: called on view manager removing
687 */
688 void LightApp_Module::onViewManagerRemoved( SUIT_ViewManager* )
689 {
690 }
691
692 /*!
693   \brief Returns instance of operation by its id; if there is no operation
694   corresponding to this id, null pointer is returned
695   \param id - operation id 
696   \return operation instance
697 */
698 LightApp_Operation* LightApp_Module::operation( const int id ) const
699 {
700   return myOperations.contains( id ) ? myOperations[id] : 0;
701 }
702
703 /*!
704   virtual method called to manage the same operations
705 */
706 bool LightApp_Module::reusableOperation( const int id )
707 {
708  return true;
709
710
711 /*!
712   virtual method
713   \return true if module can copy the current selection
714 */
715 bool LightApp_Module::canCopy() const
716 {
717   return false;
718 }
719
720 /*!
721   virtual method
722   \return true if module can paste previously copied data
723 */
724 bool LightApp_Module::canPaste() const
725 {
726   return false;
727 }
728
729 /*!
730   virtual method
731   \brief Copies the current selection into clipboard
732 */
733 void LightApp_Module::copy()
734 {
735 }
736
737 /*!
738   virtual method
739   \brief Pastes the current data in the clipboard
740 */
741 void LightApp_Module::paste()
742 {
743 }
744
745 /*!
746   virtual method
747   \return true if module allows dragging the given object
748 */
749 bool LightApp_Module::isDraggable( const SUIT_DataObject* /*what*/ ) const
750 {
751   return false;
752 }
753
754 /*!
755   virtual method
756   \return true if module allows dropping one or more objects (currently selected) on the object \c where
757 */
758 bool LightApp_Module::isDropAccepted( const SUIT_DataObject* /*where*/ ) const
759 {
760   return false;
761 }
762
763 /*!
764   virtual method
765   Complete drag-n-drop operation by processing objects \a what being dragged, dropped to the line \a row
766   within the object \a where. The drop action being performed is specified by \a action.
767 */
768 void LightApp_Module::dropObjects( const DataObjectList& /*what*/, SUIT_DataObject* /*where*/,
769                                    const int /*row*/, Qt::DropAction /*action*/ )
770 {
771 }
772
773 /*!
774   \brief Return \c true if object can be renamed
775 */
776 bool LightApp_Module::renameAllowed( const QString& /*entry*/ ) const
777 {
778   return false;
779 }
780
781 /*!
782   Rename object by entry.
783   \param entry entry of the object
784   \param name new name of the object
785   \brief Return \c true if rename operation finished successfully, \c false otherwise.
786 */
787 bool LightApp_Module::renameObject( const QString& /*entry*/, const QString& /*name*/ )
788 {
789   return false;
790 }
791
792 /*!
793   Update visibility state for data objects
794 */
795 void LightApp_Module::updateModuleVisibilityState()
796 {
797   // update visibility state of objects
798   LightApp_Application* app = dynamic_cast<LightApp_Application*>(SUIT_Session::session()->activeApplication());
799   if ( !app ) return;
800   
801   SUIT_DataBrowser* ob = app->objectBrowser();
802   if ( !ob || !ob->model() ) return;
803
804   // connect to click on item
805   connect( ob->model(), SIGNAL( clicked( SUIT_DataObject*, int ) ),
806            this, SLOT( onObjectClicked( SUIT_DataObject*, int ) ), Qt::UniqueConnection );
807
808   SUIT_DataObject* rootObj = ob->root();
809   if ( !rootObj ) return;
810   
811   DataObjectList listObj = rootObj->children( true );
812   
813   SUIT_ViewModel* vmod = 0;
814   if ( SUIT_ViewManager* vman = app->activeViewManager() )
815     vmod = vman->getViewModel();
816   app->updateVisibilityState( listObj, vmod );
817 }
818
819 /*!
820  * \brief Virtual public slot
821  *
822  * This method is called after the object inserted into data view to update their visibility state
823  * This is default implementation
824  */
825 void LightApp_Module::onObjectClicked( SUIT_DataObject* theObject, int theColumn )
826 {
827   if ( !isActiveModule() ) return;
828
829   // change visibility of object
830   if ( !theObject || theColumn != SUIT_DataObject::VisibilityId ) return;
831
832   LightApp_Study* study = dynamic_cast<LightApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
833   if ( !study ) return;
834
835   LightApp_DataObject* lo = dynamic_cast<LightApp_DataObject*>( theObject );
836   if ( !lo ) return;
837   
838   // detect action index (from LightApp level)
839   int id = -1;
840   
841   if ( study->visibilityState( lo->entry() ) == Qtx::ShownState )
842     id = myErase;
843   else if ( study->visibilityState( lo->entry() ) == Qtx::HiddenState )
844     id = myDisplay;
845   
846   if ( id != -1 )
847     startOperation( id );
848 }