Salome HOME
refs #430: incorrect coordinates in dump polyline
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_DataBrowser.cxx
1 // Copyright (C) 2007-2013  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
23 #include "HYDROGUI_DataBrowser.h"
24 #include "HYDROGUI_Module.h"
25 #include "HYDROGUI_DataObject.h"
26
27 #include <LightApp_Application.h>
28 #include <LightApp_OBSelector.h>
29 #include <LightApp_SelectionMgr.h>
30 #include <QHeaderView>
31 #include <QtxSearchTool.h>
32 #include <QtxTreeView.h>
33 #include <SUIT_DataObject.h>
34 #include <SUIT_ResourceMgr.h>
35 #include <SUIT_SelectionMgr.h>
36 #include <SUIT_DataObjectIterator.h>
37         
38 #define VISIBILITY_COLUMN_WIDTH 25
39
40
41 #include <SUIT_Selector.h>
42 #include <SUIT_DataOwner.h>
43
44 #include <QObject>
45
46 class SUIT_DataBrowser;
47 class LightApp_DataObject;
48
49
50 #include "LightApp_DataOwner.h"
51 #include "LightApp_DataObject.h"
52 #include "LightApp_Application.h"
53 #include <SUIT_DataBrowser.h>
54 #include <SUIT_Session.h>
55 #include <SUIT_DataObjectIterator.h>
56 #include <QTime>
57 #include <time.h>
58
59
60 // The selector is redefined in order to correct the selection in the browser.
61 // The main modification is to call fillEntries without the selector modified
62 // time compare. The modified time is result of the clock() method.
63 // On Linux, the method clock() returns the same values with some delay. So, it is possible,
64 // that time has the same value, but the browser has already other objects.
65 // So, the obsole entries can be in the saved entries by the filled method.
66 // May be it will be improved in the latest version of GUI_SRC.
67 // This redefinition is done for tag V7_3_0 of GUI_SRC.
68 class HYDROGUI_OBSelector : public LightApp_OBSelector
69 {
70 public:
71   HYDROGUI_OBSelector( SUIT_DataBrowser*, SUIT_SelectionMgr* );
72   virtual ~HYDROGUI_OBSelector();
73
74 protected:
75   virtual void       getSelection( SUIT_DataOwnerPtrList& ) const;
76   virtual void       setSelection( const SUIT_DataOwnerPtrList& );
77
78 private:
79   void               fillEntries( QMap<QString, LightApp_DataObject*>& );
80
81 private:
82   SUIT_DataOwnerPtrList               mySelectedList;
83   QMap<QString, LightApp_DataObject*> myEntries;
84 };
85
86 HYDROGUI_OBSelector::HYDROGUI_OBSelector( SUIT_DataBrowser* ob, SUIT_SelectionMgr* mgr )
87 : LightApp_OBSelector( ob, mgr )
88 {
89 }
90
91 /*!
92   \brief Destructor.
93 */
94 HYDROGUI_OBSelector::~HYDROGUI_OBSelector()
95 {
96 }
97
98 /*!
99   \brief Get list of currently selected objects.
100   \param theList list to be filled with the selected objects owners
101   \ This method is necessary to fill the cach containter mySelectedList
102   \ It is the same as in LightApp_OBSelector
103 */
104 void HYDROGUI_OBSelector::getSelection( SUIT_DataOwnerPtrList& theList ) const
105 {
106   SUIT_DataBrowser* aBrowser = browser();
107
108   if ( mySelectedList.count() == 0 ) {
109     SUIT_Session* session = SUIT_Session::session();
110     SUIT_Application* sapp = session ? session->activeApplication() : 0;
111     LightApp_Application* app = dynamic_cast<LightApp_Application*>( sapp );
112     if( !app || !aBrowser )
113       return;
114
115     DataObjectList objlist;
116     aBrowser->getSelected( objlist );
117     HYDROGUI_OBSelector* that = (HYDROGUI_OBSelector*)this;
118     QListIterator<SUIT_DataObject*> it( objlist );
119     while ( it.hasNext() ) {
120       LightApp_DataObject* obj = dynamic_cast<LightApp_DataObject*>( it.next() );
121       if ( obj && app->checkDataObject( obj) ) {
122 #ifndef DISABLE_SALOMEOBJECT
123         Handle(SALOME_InteractiveObject) aSObj = new SALOME_InteractiveObject
124           ( obj->entry().toLatin1().constData(),
125             obj->componentDataType().toLatin1().constData(),
126             obj->name().toLatin1().constData() );
127         LightApp_DataOwner* owner = new LightApp_DataOwner( aSObj  );
128 #else
129         LightApp_DataOwner* owner = new LightApp_DataOwner( obj->entry() );
130 #endif
131         that->mySelectedList.append( SUIT_DataOwnerPtr( owner ) );
132       }
133     }
134   }
135   theList = mySelectedList;
136 }
137
138 /*!
139   \brief Set selection.
140   \param theList list of the object owners to be set selected
141   \ It is the same as in LightApp_OBSelector. The difference is in the row with
142   \ the modification time check.
143 */
144 void HYDROGUI_OBSelector::setSelection( const SUIT_DataOwnerPtrList& theList )
145 {
146   SUIT_DataBrowser* aBrowser = browser();
147   if ( !aBrowser )
148     return;
149
150   // this is the difference to LightApp_OBSelector. For this, this class is redefined
151   //if( myEntries.count() == 0 || myModifiedTime < aBrowser->getModifiedTime() )
152   {
153     fillEntries( myEntries );
154   }
155
156   DataObjectList objList;
157   for ( SUIT_DataOwnerPtrList::const_iterator it = theList.begin(); 
158         it != theList.end(); ++it ) {
159     const LightApp_DataOwner* owner = dynamic_cast<const LightApp_DataOwner*>( (*it).operator->() );
160
161     if ( owner && myEntries.contains( owner->entry() ) )
162       objList.append( myEntries[owner->entry()] );
163   }
164
165   aBrowser->setSelected( objList );
166   mySelectedList.clear();
167 }
168
169 /*!
170   \brief Fill map of the data objects currently shown in the Object Browser.
171   \param entries map to be filled
172   \ It is the same as in LightApp_OBSelector
173 */
174 void HYDROGUI_OBSelector::fillEntries( QMap<QString, LightApp_DataObject*>& entries )
175 {
176   entries.clear();
177
178   SUIT_DataBrowser* aBrowser = browser();
179   if ( !aBrowser )
180     return;
181
182   for ( SUIT_DataObjectIterator it( aBrowser->root(),
183                                     SUIT_DataObjectIterator::DepthLeft ); it.current(); ++it ) {
184     LightApp_DataObject* obj = dynamic_cast<LightApp_DataObject*>( it.current() );
185     if ( obj )
186       entries.insert( obj->entry(), obj );
187   }
188
189   setModified();
190 }
191
192
193 HYDROGUI_DataBrowser::HYDROGUI_DataBrowser( HYDROGUI_Module* theModule, SUIT_DataObject* theRoot, QWidget* theParent )
194 : SUIT_DataBrowser( theRoot, theParent ), myModule( theModule )
195 {
196   SUIT_ResourceMgr* resMgr = theModule->getApp()->resourceMgr();
197
198   if ( ( !theRoot ) && theModule )
199   {
200     // Initialize the root with the module data model
201     setRoot( new CAM_ModuleObject( theModule->dataModel(), NULL ) );
202   }
203
204   setSortMenuEnabled( true );
205   setAutoUpdate( true );
206   setUpdateModified( true );
207
208   if ( resMgr->hasValue( "ObjectBrowser", "auto_hide_search_tool" ) )
209     searchTool()->enableAutoHide( resMgr->booleanValue( "ObjectBrowser", "auto_hide_search_tool" ) );
210
211   setWindowTitle( tr( "OBJECT_BROWSER" ) );
212   connect( this, SIGNAL( requestUpdate() ), theModule->getApp(), SLOT( onRefresh() ) );
213
214   QString EntryCol = QObject::tr( "ENTRY_COLUMN" );
215   QString RefObjCol = tr( "REF_OBJECT_COLUMN" );
216   QString AltitudeCol = tr( "ALTITUDE_COLUMN" );
217
218   SUIT_AbstractModel* treeModel = dynamic_cast<SUIT_AbstractModel*>( model() );
219   //RKV: treeModel->setSearcher( theModule->getApp() );
220   treeModel->setSearcher( this ); //RKV
221   treeModel->registerColumn( 0, EntryCol, LightApp_DataObject::EntryId );
222   treeModel->setAppropriate( EntryCol, Qtx::Toggled );
223   treeModel->registerColumn( 0, RefObjCol, HYDROGUI_DataObject::RefObjectId );
224   treeModel->setAppropriate( RefObjCol, Qtx::Toggled );
225   treeModel->registerColumn( 0, AltitudeCol, HYDROGUI_DataObject::AltitudeObjId );
226   treeModel->setAppropriate( AltitudeCol, Qtx::Toggled );
227
228   // Mantis issue 0020136: Drag&Drop in OB
229   SUIT_ProxyModel* proxyModel = dynamic_cast<SUIT_ProxyModel*>(treeModel);
230   if ( proxyModel ) {
231     connect( proxyModel, 
232       SIGNAL( dropped( const QList<SUIT_DataObject*>&, SUIT_DataObject*, int, Qt::DropAction ) ),
233       SIGNAL( dropped( const QList<SUIT_DataObject*>&, SUIT_DataObject*, int, Qt::DropAction ) ) );
234
235     //// Connect signal emitted after editing for updating after objects renaming
236     SUIT_TreeModel* aMiniModel = dynamic_cast<SUIT_TreeModel*>( proxyModel->sourceModel() );
237     if ( aMiniModel )
238     {
239       connect( aMiniModel, SIGNAL( dataChanged( const QModelIndex &, const QModelIndex & ) ), 
240         SIGNAL( dataChanged() ) );
241     }
242
243     // Do updating also in the module's main object browser.
244     if ( theModule )
245     {
246       SUIT_DataBrowser* aModulBrowser = theModule->getApp()->objectBrowser();
247       if ( aModulBrowser )
248       {
249         SUIT_ProxyModel* aPModel = dynamic_cast<SUIT_ProxyModel*>(aModulBrowser->model());
250         if ( aPModel )
251         {
252           SUIT_TreeModel* aModel = dynamic_cast<SUIT_TreeModel*>(aPModel->sourceModel());
253           //connect( proxyModel, SIGNAL( dataChanged( const QModelIndex &, const QModelIndex & ) ), 
254           //  aPModel, SIGNAL( dataChanged( const QModelIndex &, const QModelIndex & ) ) );
255           //connect( proxyModel, SIGNAL( dataChanged( const QModelIndex &, const QModelIndex & ) ), 
256           //  aModel, SIGNAL( dataChanged( const QModelIndex &, const QModelIndex & ) ) );
257           connect( proxyModel, SIGNAL( modelUpdated() ), aModel, SIGNAL( modelUpdated() ) );
258         }
259       }
260     }
261   }
262
263   // temporary commented
264   /*
265   OB_ListView* ob_list = dynamic_cast<OB_ListView*>( const_cast<QListView*>( listView() ) );
266   if( ob_list )
267     ob_list->setColumnMaxWidth( 0, theModule->getApp()->desktop()->width()/4 );
268
269   setFilter( new LightApp_OBFilter( theModule->getApp()->selectionMgr() ) );
270   */
271
272   // Create OBSelector
273   new HYDROGUI_OBSelector( this, theModule->getApp()->selectionMgr() );
274
275   treeView()->header()->setResizeMode(SUIT_DataObject::VisibilityId, QHeaderView::Fixed);
276   treeView()->header()->moveSection(SUIT_DataObject::NameId,SUIT_DataObject::VisibilityId);
277   treeView()->setColumnWidth(SUIT_DataObject::VisibilityId, VISIBILITY_COLUMN_WIDTH);
278   treeView()->hideColumn( SUIT_DataObject::VisibilityId );
279   treeView()->hideColumn( LightApp_DataObject::EntryId );
280   //RKV: connectPopupRequest( theModule->getApp(), SLOT( onConnectPopupRequest( SUIT_PopupClient*, QContextMenuEvent* ) ) );
281 }
282
283 HYDROGUI_DataBrowser::~HYDROGUI_DataBrowser()
284 {
285 }
286
287 SUIT_DataObject* HYDROGUI_DataBrowser::findObject( const QString& theEntry ) const
288 {
289   LightApp_DataObject* aCurObj;
290   for ( SUIT_DataObjectIterator it( root(), SUIT_DataObjectIterator::DepthLeft ); it.current(); ++it ) {
291     aCurObj = dynamic_cast<LightApp_DataObject*>( it.current() );
292     if ( aCurObj && aCurObj->entry() == theEntry )
293       return aCurObj;
294   }
295   return NULL;
296 }