Salome HOME
Update from BR_V5_DEV 13Feb2009
[modules/gui.git] / src / LightApp / LightApp_DataModel.cxx
1 //  Copyright (C) 2007-2008  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.
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 // File:      LightApp_DataModel.cxx
23 // Created:   10/25/2004 10:36:06 AM
24 // Author:    Sergey LITONIN
25 //
26 #include "LightApp_DataModel.h"
27 #include "LightApp_Study.h"
28 #include "LightApp_DataObject.h"
29 #include "LightApp_Module.h"
30 #include "LightApp_Application.h"
31
32 #include <SUIT_TreeModel.h>
33 #include <SUIT_DataBrowser.h>
34 #include <SUIT_DataObject.h>
35
36 /*!
37   Constructor
38 */
39 LightApp_DataModel::LightApp_DataModel( CAM_Module* theModule )
40 : CAM_DataModel( theModule )
41 {
42   myGroupId = 0;
43   if( module() )
44         myGroupId = qHash( module()->name() );
45 }
46
47 /*!
48   Destructor
49 */
50 LightApp_DataModel::~LightApp_DataModel()
51 {
52 }
53
54 /*!
55   Emit opened()
56 */
57 bool LightApp_DataModel::open( const QString&, CAM_Study* study, QStringList )
58 {
59   emit opened(); //TODO: is it really needed? to be removed maybe...
60   return true;
61 }
62
63 /*!
64   Emit saved()
65 */
66 bool LightApp_DataModel::save( QStringList& )
67 {
68   emit saved();
69   return true;
70 }
71
72 /*!
73   Emit saved()
74 */
75 bool LightApp_DataModel::saveAs( const QString&, CAM_Study*, QStringList& )
76 {
77   emit saved();
78   return true;
79 }
80
81 /*!
82   Emit closed()
83 */
84 bool LightApp_DataModel::close()
85 {
86   emit closed();
87   return true;
88 }
89
90 /*!
91   Build whole data model tree
92 */
93 void LightApp_DataModel::build()
94 {
95 }
96
97 /*!
98   Updates data model presentation in some widgets (for example, in object browser
99 */
100 void LightApp_DataModel::updateWidgets()
101 {
102   LightApp_Application* app = dynamic_cast<LightApp_Application*>( module()->application() );
103   if ( app )
104     app->objectBrowser()->updateTree( 0, false );
105 }
106
107 /*!
108   Default behaviour of data model update for light modules
109 */
110 void LightApp_DataModel::update( LightApp_DataObject*, LightApp_Study* )
111 {
112   LightApp_ModuleObject* modelRoot = dynamic_cast<LightApp_ModuleObject*>( root() );
113   DataObjectList ch;
114   QMap<SUIT_DataObject*,int> aMap;
115   if( modelRoot )
116   {
117     ch = modelRoot->children();
118     QListIterator<SUIT_DataObject*> it( ch );
119     while ( it.hasNext() )
120       it.next()->setParent( 0 );
121   }
122
123   build();
124
125   modelRoot = dynamic_cast<LightApp_ModuleObject*>( root() );
126   if( modelRoot )
127   {
128     DataObjectList new_ch = modelRoot->children();
129     QListIterator<SUIT_DataObject*> it1( new_ch );
130     while ( it1.hasNext() )
131       aMap.insert( it1.next(), 0 );
132   }
133
134   updateWidgets();
135
136   QListIterator<SUIT_DataObject*> it( ch );
137   while ( it.hasNext() ) {
138     SUIT_DataObject* aDO = it.next();
139     if( !aMap.contains( aDO ) )
140       delete aDO;
141   }
142 }
143
144 /*!
145   \return corresponding module 
146 */
147 LightApp_Module* LightApp_DataModel::getModule() const
148 {
149   return dynamic_cast<LightApp_Module*>( module() );
150 }
151
152 /*!
153   \return corresponding  study
154 */
155 LightApp_Study* LightApp_DataModel::getStudy() const
156 {
157   LightApp_RootObject* aRoot = dynamic_cast<LightApp_RootObject*>( root()->root() );
158   if ( !aRoot )
159     return 0;
160   return aRoot->study();
161 }
162
163 /*!
164   default implementation, always returns false so as not to mask study's isModified()
165 */
166 bool LightApp_DataModel::isModified() const
167 {
168   return false;
169 }
170
171 /*!
172   default implementation, always returns true so as not to mask study's isSaved()
173 */
174 bool LightApp_DataModel::isSaved() const
175 {
176   return true;
177 }
178
179 /*!
180   \return data model group id used for custom columns creation
181 */
182 int LightApp_DataModel::groupId() const
183 {
184   return myGroupId;
185 }
186
187 /*!
188   Register custom column in the object browser
189   \param browser - object browser where new column should be created
190   \param name - translated column name
191   \param custom_id - custom column identificator passed into data object's methods text(), icon() etc
192 */
193 void LightApp_DataModel::registerColumn( SUIT_DataBrowser* browser, const QString& name, const int custom_id )
194 {
195   SUIT_AbstractModel* m = dynamic_cast<SUIT_AbstractModel*>( browser ? browser->model() : 0 );
196   if( m )
197     m->registerColumn( groupId(), name, custom_id );
198 }
199
200 /*!
201   Remove registered custom column from the object browser
202   \param browser - object browser where new column should be created
203   \param name - translated column name
204 */
205 void LightApp_DataModel::unregisterColumn( SUIT_DataBrowser* browser, const QString& name )
206 {
207   SUIT_AbstractModel* m = dynamic_cast<SUIT_AbstractModel*>( browser ? browser->model() : 0 );
208   if( m )
209         m->unregisterColumn( groupId(), name );
210 }