1 // Copyright (C) 2014-2015 EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 // Lesser General Public License for more details.
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include "HYDROGUI_DataBrowser.h"
20 #include "HYDROGUI_Module.h"
21 #include "HYDROGUI_DataObject.h"
23 #include <LightApp_Application.h>
24 #include <LightApp_OBSelector.h>
25 #include <LightApp_SelectionMgr.h>
26 #include <QHeaderView>
27 #include <QtxSearchTool.h>
28 #include <QtxTreeView.h>
29 #include <SUIT_DataObject.h>
30 #include <SUIT_ResourceMgr.h>
31 #include <SUIT_SelectionMgr.h>
32 #include <SUIT_DataObjectIterator.h>
34 #define VISIBILITY_COLUMN_WIDTH 25
37 #include <SUIT_Selector.h>
38 #include <SUIT_DataOwner.h>
44 class SUIT_DataBrowser;
45 class LightApp_DataObject;
48 #include "LightApp_DataOwner.h"
49 #include "LightApp_DataObject.h"
50 #include "LightApp_Application.h"
51 #include <SUIT_DataBrowser.h>
52 #include <SUIT_Session.h>
53 #include <SUIT_DataObjectIterator.h>
58 // The selector is redefined in order to correct the selection in the browser.
59 // The main modification is to call fillEntries without the selector modified
60 // time compare. The modified time is result of the clock() method.
61 // On Linux, the method clock() returns the same values with some delay. So, it is possible,
62 // that time has the same value, but the browser has already other objects.
63 // So, the obsole entries can be in the saved entries by the filled method.
64 // May be it will be improved in the latest version of GUI_SRC.
65 // This redefinition is done for tag V7_3_0 of GUI_SRC.
66 class HYDROGUI_OBSelector : public LightApp_OBSelector
69 HYDROGUI_OBSelector( SUIT_DataBrowser*, SUIT_SelectionMgr* );
70 virtual ~HYDROGUI_OBSelector();
73 virtual void getSelection( SUIT_DataOwnerPtrList& ) const;
74 virtual void setSelection( const SUIT_DataOwnerPtrList& );
77 void fillEntries( QMap<QString, LightApp_DataObject*>& );
80 SUIT_DataOwnerPtrList mySelectedList;
81 QMap<QString, LightApp_DataObject*> myEntries;
84 HYDROGUI_OBSelector::HYDROGUI_OBSelector( SUIT_DataBrowser* ob, SUIT_SelectionMgr* mgr )
85 : LightApp_OBSelector( ob, mgr )
92 HYDROGUI_OBSelector::~HYDROGUI_OBSelector()
97 \brief Get list of currently selected objects.
98 \param theList list to be filled with the selected objects owners
99 \ This method is necessary to fill the cach containter mySelectedList
100 \ It is the same as in LightApp_OBSelector
102 void HYDROGUI_OBSelector::getSelection( SUIT_DataOwnerPtrList& theList ) const
104 SUIT_DataBrowser* aBrowser = browser();
106 if ( mySelectedList.count() == 0 ) {
107 SUIT_Session* session = SUIT_Session::session();
108 SUIT_Application* sapp = session ? session->activeApplication() : 0;
109 LightApp_Application* app = dynamic_cast<LightApp_Application*>( sapp );
110 if( !app || !aBrowser )
113 DataObjectList objlist;
114 aBrowser->getSelected( objlist );
115 HYDROGUI_OBSelector* that = (HYDROGUI_OBSelector*)this;
116 QListIterator<SUIT_DataObject*> it( objlist );
117 while ( it.hasNext() ) {
118 LightApp_DataObject* obj = dynamic_cast<LightApp_DataObject*>( it.next() );
119 if ( obj && app->checkDataObject( obj) ) {
120 #ifndef DISABLE_SALOMEOBJECT
121 Handle(SALOME_InteractiveObject) aSObj = new SALOME_InteractiveObject
122 ( obj->entry().toLatin1().constData(),
123 obj->componentDataType().toLatin1().constData(),
124 obj->name().toLatin1().constData() );
125 LightApp_DataOwner* owner = new LightApp_DataOwner( aSObj );
127 LightApp_DataOwner* owner = new LightApp_DataOwner( obj->entry() );
129 that->mySelectedList.append( SUIT_DataOwnerPtr( owner ) );
133 theList = mySelectedList;
137 \brief Set selection.
138 \param theList list of the object owners to be set selected
139 \ It is the same as in LightApp_OBSelector. The difference is in the row with
140 \ the modification time check.
142 void HYDROGUI_OBSelector::setSelection( const SUIT_DataOwnerPtrList& theList )
144 SUIT_DataBrowser* aBrowser = browser();
148 // this is the difference to LightApp_OBSelector. For this, this class is redefined
149 //if( myEntries.count() == 0 || myModifiedTime < aBrowser->getModifiedTime() )
151 fillEntries( myEntries );
154 DataObjectList objList;
155 for ( SUIT_DataOwnerPtrList::const_iterator it = theList.begin();
156 it != theList.end(); ++it ) {
157 const LightApp_DataOwner* owner = dynamic_cast<const LightApp_DataOwner*>( (*it).operator->() );
159 if ( owner && myEntries.contains( owner->entry() ) )
160 objList.append( myEntries[owner->entry()] );
163 aBrowser->setSelected( objList );
164 mySelectedList.clear();
168 \brief Fill map of the data objects currently shown in the Object Browser.
169 \param entries map to be filled
170 \ It is the same as in LightApp_OBSelector
172 void HYDROGUI_OBSelector::fillEntries( QMap<QString, LightApp_DataObject*>& entries )
176 SUIT_DataBrowser* aBrowser = browser();
180 for ( SUIT_DataObjectIterator it( aBrowser->root(),
181 SUIT_DataObjectIterator::DepthLeft ); it.current(); ++it ) {
182 LightApp_DataObject* obj = dynamic_cast<LightApp_DataObject*>( it.current() );
184 entries.insert( obj->entry(), obj );
191 HYDROGUI_DataBrowser::HYDROGUI_DataBrowser( HYDROGUI_Module* theModule,
192 SUIT_DataObject* theRoot,
194 bool theLandCover/* = false*/)
195 : SUIT_DataBrowser( theRoot, theParent ), myModule( theModule )
197 SUIT_ResourceMgr* resMgr = theModule->getApp()->resourceMgr();
199 if ( ( !theRoot ) && theModule )
201 // Initialize the root with the module data model
202 setRoot( new CAM_ModuleObject( theModule->dataModel(), NULL ) );
205 setSortMenuEnabled( true );
206 setAutoUpdate( true );
207 setUpdateModified( true );
209 if ( resMgr->hasValue( "ObjectBrowser", "auto_hide_search_tool" ) )
210 searchTool()->enableAutoHide( resMgr->booleanValue( "ObjectBrowser", "auto_hide_search_tool" ) );
212 setWindowTitle( tr( "OBJECT_BROWSER" ) );
213 connect( this, SIGNAL( requestUpdate() ), theModule->getApp(), SLOT( onRefresh() ) );
215 QString EntryCol = QObject::tr( "ENTRY_COLUMN" );
216 QString RefObjCol = tr( "REF_OBJECT_COLUMN" );
219 AltitudeCol = tr( "LAND_COVER_COLUMN" );
221 AltitudeCol = tr( "ALTITUDE_COLUMN" );
223 SUIT_AbstractModel* treeModel = dynamic_cast<SUIT_AbstractModel*>( model() );
224 //RKV: treeModel->setSearcher( theModule->getApp() );
225 treeModel->setSearcher( this ); //RKV
226 treeModel->registerColumn( 0, EntryCol, LightApp_DataObject::EntryId );
227 treeModel->setAppropriate( EntryCol, Qtx::Toggled );
228 treeModel->registerColumn( 0, RefObjCol, HYDROGUI_DataObject::RefObjectId );
229 treeModel->setAppropriate( RefObjCol, Qtx::Toggled );
230 treeModel->registerColumn( 0, AltitudeCol, HYDROGUI_DataObject::AltitudeObjId );
231 treeModel->setAppropriate( AltitudeCol, Qtx::Toggled );
233 // Mantis issue 0020136: Drag&Drop in OB
234 SUIT_ProxyModel* proxyModel = dynamic_cast<SUIT_ProxyModel*>(treeModel);
237 SIGNAL( dropped( const QList<SUIT_DataObject*>&, SUIT_DataObject*, int, Qt::DropAction ) ),
238 SIGNAL( dropped( const QList<SUIT_DataObject*>&, SUIT_DataObject*, int, Qt::DropAction ) ) );
240 //// Connect signal emitted after editing for updating after objects renaming
241 SUIT_TreeModel* aMiniModel = dynamic_cast<SUIT_TreeModel*>( proxyModel->sourceModel() );
244 connect( aMiniModel, SIGNAL( dataChanged( const QModelIndex &, const QModelIndex & ) ),
245 SIGNAL( dataChanged() ) );
248 // Do updating also in the module's main object browser.
251 SUIT_DataBrowser* aModulBrowser = theModule->getApp()->objectBrowser();
254 SUIT_ProxyModel* aPModel = dynamic_cast<SUIT_ProxyModel*>(aModulBrowser->model());
257 SUIT_TreeModel* aModel = dynamic_cast<SUIT_TreeModel*>(aPModel->sourceModel());
258 //connect( proxyModel, SIGNAL( dataChanged( const QModelIndex &, const QModelIndex & ) ),
259 // aPModel, SIGNAL( dataChanged( const QModelIndex &, const QModelIndex & ) ) );
260 //connect( proxyModel, SIGNAL( dataChanged( const QModelIndex &, const QModelIndex & ) ),
261 // aModel, SIGNAL( dataChanged( const QModelIndex &, const QModelIndex & ) ) );
262 connect( proxyModel, SIGNAL( modelUpdated() ), aModel, SIGNAL( modelUpdated() ) );
268 // temporary commented
270 OB_ListView* ob_list = dynamic_cast<OB_ListView*>( const_cast<QListView*>( listView() ) );
272 ob_list->setColumnMaxWidth( 0, theModule->getApp()->desktop()->width()/4 );
274 setFilter( new LightApp_OBFilter( theModule->getApp()->selectionMgr() ) );
278 new HYDROGUI_OBSelector( this, theModule->getApp()->selectionMgr() );
280 treeView()->header()->setSectionResizeMode(SUIT_DataObject::VisibilityId, QHeaderView::Fixed);
281 treeView()->header()->moveSection(SUIT_DataObject::NameId,SUIT_DataObject::VisibilityId);
282 treeView()->setColumnWidth(SUIT_DataObject::VisibilityId, VISIBILITY_COLUMN_WIDTH);
283 treeView()->hideColumn( SUIT_DataObject::VisibilityId );
284 treeView()->hideColumn( LightApp_DataObject::EntryId );
285 connectPopupRequest( theModule->getApp(), SLOT( onConnectPopupRequest( SUIT_PopupClient*, QContextMenuEvent* ) ) );
288 HYDROGUI_DataBrowser::~HYDROGUI_DataBrowser()
292 SUIT_DataObject* HYDROGUI_DataBrowser::findObject( const QString& theEntry ) const
294 LightApp_DataObject* aCurObj;
295 for ( SUIT_DataObjectIterator it( root(), SUIT_DataObjectIterator::DepthLeft ); it.current(); ++it ) {
296 aCurObj = dynamic_cast<LightApp_DataObject*>( it.current() );
297 if ( aCurObj && aCurObj->entry() == theEntry )
304 \brief Switch read only mode for the Object Browser.
305 \param theIsReadOnly if true - read only mode will be turned on
307 void HYDROGUI_DataBrowser::setReadOnly( const bool theIsReadOnly )
309 //TODO: to be reimplemented
311 // Enable/disable edit triggers
312 foreach ( QTreeView* aView, findChildren<QTreeView*>() ) {
313 aView->setDragEnabled ( !theIsReadOnly );
314 aView->setEditTriggers ( theIsReadOnly ?
315 QAbstractItemView::NoEditTriggers :
316 QAbstractItemView::DoubleClicked );
319 // Enable/disable rename shortcut
320 QList<QShortcut*> aShortcuts = findChildren<QShortcut*>();
321 QShortcut* aShortcut;
322 foreach( aShortcut, aShortcuts ) {
323 if ( aShortcut->key() == QKeySequence( shortcutKey( RenameShortcut ) ) ) {
324 aShortcut->setEnabled( !theIsReadOnly );
329 void HYDROGUI_DataBrowser::createPopupMenu( QMenu* theMenu )
332 DataObjectList aSelection = getSelected();
333 bool isOnlyZones = aSelection.size() > 0;
334 foreach( SUIT_DataObject* anObj, aSelection )
336 HYDROGUI_DataObject* aHydroObj = dynamic_cast<HYDROGUI_DataObject*>( anObj );
339 Handle(HYDROData_Entity) aModelObj = aHydroObj->modelObject();
340 if( !aModelObj.IsNull() )
342 isOnlyZones = aModelObj->GetKind()==KIND_ZONE;
346 SUIT_DataObject* aParentObj = aHydroObj->parent();
349 isOnlyZones = aParentObj->childCount() > 1;
368 theMenu->addAction( tr( "ZONE_TO_NEW_REGION" ), this, SIGNAL( newRegion() ) );