Salome HOME
126ff7ab019e5606cd8288e64b2be962c4ebcef3
[modules/gui.git] / src / LightApp / LightApp_DataModel.cxx
1 // Copyright (C) 2007-2022  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_DataModel.cxx
24 // Created:   10/25/2004 10:36:06 AM
25 // Author:    Sergey LITONIN
26 //
27 #include "LightApp_DataModel.h"
28 #include "LightApp_Study.h"
29 #include "LightApp_DataObject.h"
30 #include "LightApp_Module.h"
31 #include "LightApp_Application.h"
32
33 #include <SUIT_TreeModel.h>
34 #include <SUIT_DataBrowser.h>
35 #include <SUIT_DataObject.h>
36
37 /*!
38   Constructor
39 */
40 LightApp_DataModel::LightApp_DataModel( CAM_Module* theModule )
41 : CAM_DataModel( theModule )
42 {
43   myGroupId = 0;
44   if( module() )
45         myGroupId = qHash( module()->name() );
46 }
47
48 /*!
49   Destructor
50 */
51 LightApp_DataModel::~LightApp_DataModel()
52 {
53 }
54
55 /*!
56   Emit opened()
57 */
58 bool LightApp_DataModel::open( const QString&, CAM_Study*, QStringList )
59 {
60   emit opened(); //TODO: is it really needed? to be removed maybe...
61   return true;
62 }
63
64 /*!
65   Emit saved()
66 */
67 bool LightApp_DataModel::save( QStringList& )
68 {
69   emit saved();
70   return true;
71 }
72
73 /*!
74   Emit saved()
75 */
76 bool LightApp_DataModel::saveAs( const QString&, CAM_Study*, QStringList& )
77 {
78   emit saved();
79   return true;
80 }
81
82 /*!
83   Does nothing by default. Should be redefined in light modules
84   that want to participate in "Dump study" operation.
85 */
86 bool LightApp_DataModel::dumpPython( const QString&, CAM_Study*, bool, QStringList& )
87 {
88   return true;
89 }
90
91 /*!
92   Emit closed()
93 */
94 bool LightApp_DataModel::close()
95 {
96   emit closed();
97   return true;
98 }
99
100 /*!
101   Build whole data model tree
102 */
103 void LightApp_DataModel::build()
104 {
105 }
106
107 /*!
108   Updates data model presentation in some widgets (for example, in object browser
109 */
110 void LightApp_DataModel::updateWidgets()
111 {
112   LightApp_Application* app = dynamic_cast<LightApp_Application*>( module()->application() );
113   if ( app && app->objectBrowser() )
114     app->objectBrowser()->updateTree( 0, false );
115 }
116
117 /*!
118   Default behaviour of data model update for light modules
119 */
120 void LightApp_DataModel::update( LightApp_DataObject*, LightApp_Study* )
121 {
122   // san: Previously modelRoot was casted to LightApp_ModuleObject*,
123   // BUT this is incorrect: in full SALOME the model root has different type.
124   // Hopefully LightApp_DataObject* is sufficient here.
125   LightApp_DataObject* modelRoot = dynamic_cast<LightApp_DataObject*>( root() );
126   DataObjectList ch;
127   QMap<SUIT_DataObject*,int> aMap;
128   if( modelRoot )
129   {
130     ch = modelRoot->children();
131     QListIterator<SUIT_DataObject*> it( ch );
132     while ( it.hasNext() )
133       it.next()->setParent( 0 );
134   }
135
136   build();
137
138   modelRoot = dynamic_cast<LightApp_DataObject*>( root() );
139   if( modelRoot )
140   {
141     DataObjectList new_ch = modelRoot->children();
142     QListIterator<SUIT_DataObject*> it1( new_ch );
143     while ( it1.hasNext() )
144       aMap.insert( it1.next(), 0 );
145   }
146
147   updateWidgets();
148
149   QListIterator<SUIT_DataObject*> it( ch );
150   while ( it.hasNext() ) {
151     SUIT_DataObject* aDO = it.next();
152     if( !aMap.contains( aDO ) )
153       delete aDO;
154   }
155 }
156
157 /*!
158   \return corresponding module 
159 */
160 LightApp_Module* LightApp_DataModel::getModule() const
161 {
162   return dynamic_cast<LightApp_Module*>( module() );
163 }
164
165 /*!
166   \return corresponding  study
167 */
168 LightApp_Study* LightApp_DataModel::getStudy() const
169 {
170   LightApp_RootObject* aRoot = dynamic_cast<LightApp_RootObject*>( root()->root() );
171   if ( !aRoot )
172     return 0;
173   return aRoot->study();
174 }
175
176 /*!
177   default implementation, always returns false so as not to mask study's isModified()
178 */
179 bool LightApp_DataModel::isModified() const
180 {
181   return false;
182 }
183
184 /*!
185   default implementation, always returns true so as not to mask study's isSaved()
186 */
187 bool LightApp_DataModel::isSaved() const
188 {
189   return true;
190 }
191
192 /*!
193   \return data model group id used for custom columns creation
194 */
195 int LightApp_DataModel::groupId() const
196 {
197   return myGroupId;
198 }
199
200 /*!
201   Register custom column in the object browser
202   \param browser - object browser where new column should be created
203   \param name - translated column name
204   \param custom_id - custom column identificator passed into data object's methods text(), icon() etc
205 */
206 void LightApp_DataModel::registerColumn( SUIT_DataBrowser* browser, const QString& name, const int custom_id )
207 {
208   SUIT_AbstractModel* m = dynamic_cast<SUIT_AbstractModel*>( browser ? browser->model() : 0 );
209   if( m )
210     m->registerColumn( groupId(), name, custom_id );
211 }
212
213 /*!
214   Remove registered custom column from the object browser
215   \param browser - object browser where new column should be created
216   \param name - translated column name
217 */
218 void LightApp_DataModel::unregisterColumn( SUIT_DataBrowser* browser, const QString& name )
219 {
220   SUIT_AbstractModel* m = dynamic_cast<SUIT_AbstractModel*>( browser ? browser->model() : 0 );
221   if( m )
222     m->unregisterColumn( groupId(), name );
223 }
224
225 /*!
226   Creates the data model's root (module object) using the study services.
227   This is important because different study classes use different moduel object classes.
228   Therefore creation of the module object cannot be done at the data model level
229   where the type of the current study instance should not be known.
230   The module object returned by this method should be then passed to the model's setRoot().
231   \return the module object instance corresponding to the study type
232   \sa CAM_DataModel class
233 */
234 CAM_ModuleObject* LightApp_DataModel::createModuleObject( SUIT_DataObject* theRoot ) const
235 {
236   LightApp_RootObject* aStudyRoot = dynamic_cast<LightApp_RootObject*>( theRoot );
237   if ( !aStudyRoot )
238     return 0;
239
240   LightApp_Study* aStudy = aStudyRoot->study();
241   if ( aStudy )
242     return aStudy->createModuleObject( const_cast<LightApp_DataModel*>( this ), 
243                                        theRoot );
244   return 0;
245 }