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