Salome HOME
2b8ae4ad5539d0829a10d7e790687e581ec068eb
[modules/gui.git] / src / LightApp / LightApp_ShowHideOp.cxx
1 // Copyright (C) 2007-2015  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 #include "LightApp_ShowHideOp.h"
24 #include "LightApp_Application.h"
25 #include "LightApp_Module.h"
26 #include "LightApp_Study.h"
27 #include "LightApp_Displayer.h"
28
29 #include "LightApp_SelectionMgr.h"
30 #include "LightApp_Selection.h"
31
32 #ifndef DISABLE_SALOMEOBJECT
33   #include <SALOME_ListIO.hxx>
34 #endif
35
36 /*!
37   Constructor
38 */
39 LightApp_ShowHideOp::LightApp_ShowHideOp( ActionType type )
40 : LightApp_Operation(),
41   myActionType( type )
42 {
43 }
44
45 /*!
46   Destructor
47 */
48 LightApp_ShowHideOp::~LightApp_ShowHideOp()
49 {
50 }
51
52 /*!
53   Makes show/hide operation
54 */
55 void LightApp_ShowHideOp::startOperation()
56 {
57   LightApp_Application* app = dynamic_cast<LightApp_Application*>( application() );
58   LightApp_Study* study = app ? dynamic_cast<LightApp_Study*>( app->activeStudy() ) : 0;
59   if( !app || !study )
60   {
61     abort();
62     return;
63   }
64
65   LightApp_SelectionMgr* mgr = app->selectionMgr();
66   LightApp_Module* mod = dynamic_cast<LightApp_Module*>( app->activeModule() );
67   if( !mod )
68     return;
69
70   LightApp_Selection* sel = mod->createSelection();
71   if( !sel )
72     return;
73
74   sel->init( "", mgr );
75   if( sel->count()==0 && myActionType!=ERASE_ALL )
76   {
77     abort();
78     return;
79   }
80
81   if( myActionType==DISPLAY_ONLY || myActionType==ERASE_ALL )
82   {
83     //ERASE ALL
84     QStringList comps;
85     study->components( comps );
86     QStringList::const_iterator anIt = comps.begin(), aLast = comps.end();
87     for( ; anIt!=aLast; anIt++ )
88     {
89       LightApp_Displayer* disp = LightApp_Displayer::FindDisplayer( app->moduleTitle( *anIt ), false );
90       if( disp )
91         disp->EraseAll( false, false, 0 );
92     }
93     if( myActionType==ERASE_ALL )
94     {
95       // Temporary displayer just to update viewer!
96       LightApp_Displayer ld;
97       ld.UpdateViewer();
98       commit();
99       return;
100     }
101   }
102
103   QString mod_name;
104   if( sel->count()>0 )
105   {
106     QString aStr =  sel->parameter( 0, "displayer" ).toString();
107     mod_name = app->moduleTitle( aStr );
108   }
109   else if( app->activeModule() )
110     mod_name = app->moduleTitle( app->activeModule()->name() );
111
112   LightApp_Displayer* d = LightApp_Displayer::FindDisplayer( mod_name, true );
113   if( !d )
114   {
115     abort();
116     return;
117   }
118
119   QStringList entries;
120
121 #ifndef DISABLE_SALOMEOBJECT
122   SALOME_ListIO selObjs;
123   mgr->selectedObjects( selObjs );
124   SALOME_ListIteratorOfListIO anIt( selObjs );
125   for( ; anIt.More(); anIt.Next() )
126     if( !anIt.Value().IsNull() )
127 #else
128   QStringList selObjs;
129   mgr->selectedObjects( selObjs );
130   QStringList::const_iterator anIt = selObjs.begin(), aLast = selObjs.end();
131   for( ; ; anIt!=aLast )
132 #endif
133     {
134       QString entry = 
135 #ifndef DISABLE_SALOMEOBJECT
136         anIt.Value()->getEntry();
137 #else
138         *anIt;
139 #endif
140
141       if( study->isComponent( entry ) )
142         study->children( entry, entries );
143       else
144         entries.append( entry );
145     }
146
147   // be sure to use real object entries
148   QStringList objEntries;
149   QStringList::const_iterator it = entries.begin(), last = entries.end();
150   for ( ; it!=last; ++it )
151     objEntries.append( study->referencedToEntry( *it ) ); 
152   
153   if( myActionType==DISPLAY || myActionType==DISPLAY_ONLY ) {
154     d->Display( objEntries, false, 0 );
155     mgr->setSelectedObjects(selObjs);
156   }
157   else if( myActionType==ERASE ) {
158     d->Erase( objEntries, false, false, 0 );
159   }
160   
161   d->UpdateViewer();
162   commit();
163 }