]> SALOME platform Git repositories - modules/gui.git/blob - src/SUIT/SUIT_DataBrowser.cxx
Salome HOME
715f3a6fd1df5e5e84ff6a12ca6e1c0c243106b5
[modules/gui.git] / src / SUIT / SUIT_DataBrowser.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   : SUIT_DataBrowser.cxx
23 // Author : Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com)
24 //
25 #include "SUIT_DataBrowser.h"
26 #include "SUIT_TreeModel.h"
27 #include <QtxTreeView.h>
28
29 #include <QShortcut>
30
31 /*!
32   \class SUIT_DataBrowser
33   \brief Object browser customization.
34 */
35
36 /*!
37   \brief Constructor.
38   \param parent parent widget
39 */
40 SUIT_DataBrowser::SUIT_DataBrowser( QWidget* parent )
41 : OB_Browser( parent )
42 {
43   init( 0 );
44 }
45
46 /*!
47   \brief Constructor.
48   \param root root data object
49   \param parent parent widget
50 */
51 SUIT_DataBrowser::SUIT_DataBrowser( SUIT_DataObject* root, QWidget* parent )
52 : OB_Browser( parent )
53 {
54   init( root );
55 }
56
57 /*!
58   \brief Destructor.
59 */
60 SUIT_DataBrowser::~SUIT_DataBrowser()
61 {
62 }
63
64 /*!
65   \brief Get popup menu client type.
66   \return popup client type
67 */
68 QString SUIT_DataBrowser::popupClientType() const
69 {
70   return "ObjectBrowser";
71 }
72
73 /*!
74   \brief Get root object.
75   \return root object
76 */
77 SUIT_DataObject* SUIT_DataBrowser::root() const
78 {
79   SUIT_ProxyModel* m = qobject_cast<SUIT_ProxyModel*>( model() );
80   return m ? m->root() : 0;
81 }
82
83 /*!
84   \brief Set root object.
85   \param r new root object
86 */
87 void SUIT_DataBrowser::setRoot( SUIT_DataObject* r )
88 {
89   SUIT_ProxyModel* m = qobject_cast<SUIT_ProxyModel*>( model() );
90   if ( m ) 
91     m->setRoot( r );
92 }
93
94 /*!
95   \brief Get 'auto-update tree' flag value.
96   \return 'auto-update tree' flag value
97   \sa setAutoUpdate(), updateTree()
98 */
99 bool SUIT_DataBrowser::autoUpdate() const
100 {
101   SUIT_ProxyModel* m = qobject_cast<SUIT_ProxyModel*>( model() );
102   return m ? m->autoUpdate() : false;
103 }
104
105 /*!
106   \brief Set 'auto-update tree' flag value.
107
108   If this flag is set to \c true (by default), the object browser is updated
109   automatically when data tree is changed.
110
111   \param on 'auto-update tree' flag value
112   \sa autoUpdate(), updateTree()
113 */
114 void SUIT_DataBrowser::setAutoUpdate( const bool on )
115 {
116   SUIT_ProxyModel* m = qobject_cast<SUIT_ProxyModel*>( model() );
117   if ( m ) 
118     m->setAutoUpdate( on );
119 }
120
121 /*!
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
126 */
127 void SUIT_DataBrowser::updateTree( SUIT_DataObject* obj, const bool autoOpen )
128 {
129   SUIT_ProxyModel* m = qobject_cast<SUIT_ProxyModel*>( model() );
130   if ( m ) {
131     m->updateTree( obj );
132     openLevels();
133
134     if (myAutoSizeFirstColumn)
135       adjustFirstColumnWidth();
136     if (myAutoSizeColumns)
137       adjustColumnsWidth();
138   }
139 }
140
141 /*!
142   \brief Get current key accelerator used for the 
143   object browser update operation.
144   \return current key accelerator
145   \sa setUpdateKey(), requestUpdate()
146 */
147 int SUIT_DataBrowser::updateKey() const
148 {
149   return myShortcut->key();
150 }
151
152 /*!
153   \brief Assign the key accelerator to be used for the 
154   object browser update operation.
155
156   By default, \c [F5] key is assigned for the update operation.
157   To disable the accelerator, pass 0 to this method.
158
159   \param key new key accelerator
160   \sa updateKey(), requestUpdate()
161 */
162 void SUIT_DataBrowser::setUpdateKey( const int key )
163 {
164   myShortcut->setKey( key );
165 }
166
167 /*!
168   \brief Get list of selected data objects.
169   \return list of the currently selected data objects
170 */
171 DataObjectList SUIT_DataBrowser::getSelected() const
172 {
173   DataObjectList lst;
174   getSelected( lst );
175   return lst;
176 }
177
178 /*!
179   \brief Get list of selected data objects.
180   \overload
181   \param lst list to be filled with the currently selected data objects
182 */
183 void SUIT_DataBrowser::getSelected( DataObjectList& lst ) const
184 {
185   lst.clear();
186
187   SUIT_ProxyModel* m = qobject_cast<SUIT_ProxyModel*>( model() );
188
189   if ( m ) {
190     QModelIndexList sel = selectedIndexes();
191     QModelIndex idx;
192   
193     foreach( idx, sel ) {
194       SUIT_DataObject* obj = m->object( idx );
195       if ( obj )
196         lst.append( obj );
197     }
198   }
199 }
200
201 /*!
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
206 */
207 void SUIT_DataBrowser::setSelected( const SUIT_DataObject* obj, const bool append )
208 {
209   SUIT_ProxyModel* m = qobject_cast<SUIT_ProxyModel*>( model() );
210
211   if ( m ) {
212     QModelIndex index = m->index( obj );
213     if ( index.isValid() )
214       select( index, true, append );
215   }
216 }
217
218 /*!
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
223 */
224 void SUIT_DataBrowser::setSelected( const DataObjectList& lst, const bool append )
225 {
226   SUIT_ProxyModel* m = qobject_cast<SUIT_ProxyModel*>( model() );
227
228   if ( m ) {
229     QModelIndexList indexes;
230     SUIT_DataObject* obj;
231
232     foreach( obj, lst ) {
233       QModelIndex index = m->index( obj );
234       if ( index.isValid() )
235         indexes.append( index );
236     }
237     select( indexes, true, append ); // if !indexes.isEmpty() ???
238   }
239 }
240
241 /*!
242   \brief Add custom actions to the popup menu.
243   \param menu popup menu
244 */
245 void SUIT_DataBrowser::contextMenuPopup( QMenu* menu )
246 {
247   createPopupMenu( menu );
248 }
249
250 /*!
251   \brief Set 'auto-size first column' flag value.
252
253   If this flag is set to \c true (by default), the first column width is resized
254   to its contents.
255
256   \param on 'auto-size first column' flag value
257   \sa setAutoSizeColumns()
258 */
259 void SUIT_DataBrowser::setAutoSizeFirstColumn( const bool on )
260 {
261   myAutoSizeFirstColumn = on;
262 }
263
264 /*!
265   \brief Set 'auto-size columns' flag value.
266
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.
269
270   \param on 'auto-size columns' flag value
271   \sa setAutoSizeFirstColumn()
272 */
273 void SUIT_DataBrowser::setAutoSizeColumns( const bool on )
274 {
275   myAutoSizeColumns = on;
276 }
277
278 /*!
279   \brief Process context menu request event.
280   \param e context menu event
281 */
282 void SUIT_DataBrowser::contextMenuEvent( QContextMenuEvent* e )
283 {
284   contextMenuRequest( e );
285 }
286
287 /*!
288   \brief Set 'resize on expand item' flag value.
289
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.
292
293   \param on 'resize on expand item' flag value
294 */
295 void SUIT_DataBrowser::setResizeOnExpandItem( const bool on )
296 {
297   myResizeOnExpandItem = on;
298 }
299
300 /*!
301   \brief Initialize object browser.
302   \param root root data object
303 */
304 void SUIT_DataBrowser::init( SUIT_DataObject* root )
305 {
306   SUIT_ProxyModel* m = new SUIT_ProxyModel( root, this );
307   connect( m, SIGNAL( modelUpdated() ), this, SLOT( onModelUpdated() ) );
308   
309   setModel( m );
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() ) );
320
321   myAutoSizeFirstColumn = true;
322   myAutoSizeColumns = false;
323   myResizeOnExpandItem = false;
324 }
325
326 /*!
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.
330
331   By default, \c [F5] key is assigned for the update operation.
332   The key accelerator can be changed with the setUpdateKey() method.
333
334   \sa updateKey(), setUpdateKey()
335 */
336
337 /*!
338   \fn void SUIT_DataBrowser::clicked( SUIT_DataObject* o );
339   \brief This signal is emitted when a mouse button is clicked.
340
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.
343
344   \param o data object which is clicked
345 */
346
347 /*!
348   \fn void SUIT_DataBrowser::doubleClicked( SUIT_DataObject* o );
349   \brief This signal is emitted when a mouse button is double-clicked.
350
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.
353
354   \param o data object which is double-clicked
355 */
356
357 /*!
358   \brief Update internal modification time just after data model update
359 */
360 void SUIT_DataBrowser::onModelUpdated()
361 {
362   setModified();
363 }
364
365 /*!
366   \brief Called when item is clicked in the tree view
367   \internal
368   
369   Emits signal clicked( SUIT_DataObject* );
370 */
371 void SUIT_DataBrowser::onClicked( const QModelIndex& index )
372 {
373   SUIT_ProxyModel* m = qobject_cast<SUIT_ProxyModel*>( model() );
374
375   if ( m ) {
376     SUIT_DataObject* obj = m->object( index );
377     if ( obj ) emit( clicked( obj ) );
378   }
379 }
380
381 /*!
382   \brief Called when item is double-clicked in the tree view
383   \internal
384   
385   Emits signal doubleClicked( SUIT_DataObject* );
386 */
387 void SUIT_DataBrowser::onDblClicked( const QModelIndex& index )
388 {
389   SUIT_ProxyModel* m = qobject_cast<SUIT_ProxyModel*>( model() );
390
391   if ( m ) {
392     SUIT_DataObject* obj = m->object( index );
393     if ( obj ) emit( doubleClicked( obj ) );
394   }
395 }
396
397 /*!
398   \brief Called when item specified by index is expanded.
399   \internal
400 */
401 void SUIT_DataBrowser::onExpanded( const QModelIndex& index )
402 {
403   if (myResizeOnExpandItem) {
404     adjustFirstColumnWidth();
405     adjustColumnsWidth();
406   }
407 }
408