1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 // File : SUIT_DataBrowser.cxx
23 // Author : Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com)
25 #include "SUIT_DataBrowser.h"
26 #include "SUIT_TreeModel.h"
27 #include <QtxTreeView.h>
32 \class SUIT_DataBrowser
33 \brief Object browser customization.
38 \param parent parent widget
40 SUIT_DataBrowser::SUIT_DataBrowser( QWidget* parent )
41 : OB_Browser( parent )
48 \param root root data object
49 \param parent parent widget
51 SUIT_DataBrowser::SUIT_DataBrowser( SUIT_DataObject* root, QWidget* parent )
52 : OB_Browser( parent )
60 SUIT_DataBrowser::~SUIT_DataBrowser()
65 \brief Get popup menu client type.
66 \return popup client type
68 QString SUIT_DataBrowser::popupClientType() const
70 return "ObjectBrowser";
74 \brief Get root object.
77 SUIT_DataObject* SUIT_DataBrowser::root() const
79 SUIT_ProxyModel* m = qobject_cast<SUIT_ProxyModel*>( model() );
80 return m ? m->root() : 0;
84 \brief Set root object.
85 \param r new root object
87 void SUIT_DataBrowser::setRoot( SUIT_DataObject* r )
89 SUIT_ProxyModel* m = qobject_cast<SUIT_ProxyModel*>( model() );
95 \brief Get 'auto-update tree' flag value.
96 \return 'auto-update tree' flag value
97 \sa setAutoUpdate(), updateTree()
99 bool SUIT_DataBrowser::autoUpdate() const
101 SUIT_ProxyModel* m = qobject_cast<SUIT_ProxyModel*>( model() );
102 return m ? m->autoUpdate() : false;
106 \brief Set 'auto-update tree' flag value.
108 If this flag is set to \c true (by default), the object browser is updated
109 automatically when data tree is changed.
111 \param on 'auto-update tree' flag value
112 \sa autoUpdate(), updateTree()
114 void SUIT_DataBrowser::setAutoUpdate( const bool on )
116 SUIT_ProxyModel* m = qobject_cast<SUIT_ProxyModel*>( model() );
118 m->setAutoUpdate( on );
122 \brief Update object browser starting from the object \obj;
123 open all branches automatically if \a autoOpen is \c true.
124 \param obj starting object for updating
125 \param autoOpen if \c true automatically open branches
127 void SUIT_DataBrowser::updateTree( SUIT_DataObject* obj, const bool autoOpen )
129 SUIT_ProxyModel* m = qobject_cast<SUIT_ProxyModel*>( model() );
131 m->updateTree( obj );
134 if (myAutoSizeFirstColumn)
135 adjustFirstColumnWidth();
136 if (myAutoSizeColumns)
137 adjustColumnsWidth();
142 \brief Get current key accelerator used for the
143 object browser update operation.
144 \return current key accelerator
145 \sa setUpdateKey(), requestUpdate()
147 int SUIT_DataBrowser::updateKey() const
149 return myShortcut->key();
153 \brief Assign the key accelerator to be used for the
154 object browser update operation.
156 By default, \c [F5] key is assigned for the update operation.
157 To disable the accelerator, pass 0 to this method.
159 \param key new key accelerator
160 \sa updateKey(), requestUpdate()
162 void SUIT_DataBrowser::setUpdateKey( const int key )
164 myShortcut->setKey( key );
168 \brief Get list of selected data objects.
169 \return list of the currently selected data objects
171 DataObjectList SUIT_DataBrowser::getSelected() const
179 \brief Get list of selected data objects.
181 \param lst list to be filled with the currently selected data objects
183 void SUIT_DataBrowser::getSelected( DataObjectList& lst ) const
187 SUIT_ProxyModel* m = qobject_cast<SUIT_ProxyModel*>( model() );
190 QModelIndexList sel = selectedIndexes();
193 foreach( idx, sel ) {
194 SUIT_DataObject* obj = m->object( idx );
202 \brief Set selected object.
203 \param obj data object to set selected
204 \param append if \c true, the object is added to the current selection;
205 otherwise the previous selection is first cleared
207 void SUIT_DataBrowser::setSelected( const SUIT_DataObject* obj, const bool append )
209 SUIT_ProxyModel* m = qobject_cast<SUIT_ProxyModel*>( model() );
212 QModelIndex index = m->index( obj );
213 if ( index.isValid() )
214 select( index, true, append );
219 \brief Set list of selected data objects.
220 \param lst list of the data object to set selected
221 \param append if \c true, the objects are added to the current selection;
222 otherwise the previous selection is first cleared
224 void SUIT_DataBrowser::setSelected( const DataObjectList& lst, const bool append )
226 SUIT_ProxyModel* m = qobject_cast<SUIT_ProxyModel*>( model() );
229 QModelIndexList indexes;
230 SUIT_DataObject* obj;
232 foreach( obj, lst ) {
233 QModelIndex index = m->index( obj );
234 if ( index.isValid() )
235 indexes.append( index );
237 select( indexes, true, append ); // if !indexes.isEmpty() ???
242 \brief Add custom actions to the popup menu.
243 \param menu popup menu
245 void SUIT_DataBrowser::contextMenuPopup( QMenu* menu )
247 createPopupMenu( menu );
251 \brief Set 'auto-size first column' flag value.
253 If this flag is set to \c true (by default), the first column width is resized
256 \param on 'auto-size first column' flag value
257 \sa setAutoSizeColumns()
259 void SUIT_DataBrowser::setAutoSizeFirstColumn( const bool on )
261 myAutoSizeFirstColumn = on;
265 \brief Set 'auto-size columns' flag value.
267 If this flag is set to \c true (by default is false), columns width except
268 the first column is resized to its contents.
270 \param on 'auto-size columns' flag value
271 \sa setAutoSizeFirstColumn()
273 void SUIT_DataBrowser::setAutoSizeColumns( const bool on )
275 myAutoSizeColumns = on;
279 \brief Process context menu request event.
280 \param e context menu event
282 void SUIT_DataBrowser::contextMenuEvent( QContextMenuEvent* e )
284 contextMenuRequest( e );
288 \brief Set 'resize on expand item' flag value.
290 If this flag is set to \c true (by default is false), after
291 expanding an item columns will be resized to its contents.
293 \param on 'resize on expand item' flag value
295 void SUIT_DataBrowser::setResizeOnExpandItem( const bool on )
297 myResizeOnExpandItem = on;
301 \brief Initialize object browser.
302 \param root root data object
304 void SUIT_DataBrowser::init( SUIT_DataObject* root )
306 SUIT_ProxyModel* m = new SUIT_ProxyModel( root, this );
307 connect( m, SIGNAL( modelUpdated() ), this, SLOT( onModelUpdated() ) );
310 setItemDelegate( qobject_cast<SUIT_ProxyModel*>( model() )->delegate() );
311 connect( treeView(), SIGNAL( sortingEnabled( bool ) ),
312 model(), SLOT( setSortingEnabled( bool ) ) );
313 connect( treeView(), SIGNAL( clicked( const QModelIndex& ) ),
314 this, SLOT( onClicked( const QModelIndex& ) ) );
315 connect( treeView(), SIGNAL( doubleClicked( const QModelIndex& ) ),
316 this, SLOT( onDblClicked( const QModelIndex& ) ) );
317 connect( treeView(), SIGNAL( expanded( const QModelIndex& ) ),
318 this, SLOT( onExpanded( const QModelIndex& ) ) );
319 myShortcut = new QShortcut( Qt::Key_F5, this, SIGNAL( requestUpdate() ), SIGNAL( requestUpdate() ) );
321 myAutoSizeFirstColumn = true;
322 myAutoSizeColumns = false;
323 myResizeOnExpandItem = false;
327 \fn void SUIT_DataBrowser::requestUpdate();
328 \brief The signal is emitted when the key accelerator
329 assigned for the update operation is pressed by the user.
331 By default, \c [F5] key is assigned for the update operation.
332 The key accelerator can be changed with the setUpdateKey() method.
334 \sa updateKey(), setUpdateKey()
338 \fn void SUIT_DataBrowser::clicked( SUIT_DataObject* o );
339 \brief This signal is emitted when a mouse button is clicked.
341 The data object the mouse was clicked on is specified by \a o.
342 The signal is only emitted when the object is valid.
344 \param o data object which is clicked
348 \fn void SUIT_DataBrowser::doubleClicked( SUIT_DataObject* o );
349 \brief This signal is emitted when a mouse button is double-clicked.
351 The data object the mouse was double-clicked on is specified by \a o.
352 The signal is only emitted when the object is valid.
354 \param o data object which is double-clicked
358 \brief Update internal modification time just after data model update
360 void SUIT_DataBrowser::onModelUpdated()
366 \brief Called when item is clicked in the tree view
369 Emits signal clicked( SUIT_DataObject* );
371 void SUIT_DataBrowser::onClicked( const QModelIndex& index )
373 SUIT_ProxyModel* m = qobject_cast<SUIT_ProxyModel*>( model() );
376 SUIT_DataObject* obj = m->object( index );
377 if ( obj ) emit( clicked( obj ) );
382 \brief Called when item is double-clicked in the tree view
385 Emits signal doubleClicked( SUIT_DataObject* );
387 void SUIT_DataBrowser::onDblClicked( const QModelIndex& index )
389 SUIT_ProxyModel* m = qobject_cast<SUIT_ProxyModel*>( model() );
392 SUIT_DataObject* obj = m->object( index );
393 if ( obj ) emit( doubleClicked( obj ) );
398 \brief Called when item specified by index is expanded.
401 void SUIT_DataBrowser::onExpanded( const QModelIndex& index )
403 if (myResizeOnExpandItem) {
404 adjustFirstColumnWidth();
405 adjustColumnsWidth();