Back to index

salome-gui  6.5.0
SUIT_DataBrowser.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // This library is free software; you can redistribute it and/or
00004 // modify it under the terms of the GNU Lesser General Public
00005 // License as published by the Free Software Foundation; either
00006 // version 2.1 of the License.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // Lesser General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU Lesser General Public
00014 // License along with this library; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00016 //
00017 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00018 //
00019 
00020 // File   : SUIT_DataBrowser.cxx
00021 // Author : Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com)
00022 //
00023 #include "SUIT_DataBrowser.h"
00024 #include "SUIT_TreeModel.h"
00025 #include <QtxTreeView.h>
00026 
00027 #include <QShortcut>
00028 
00038 SUIT_DataBrowser::SUIT_DataBrowser( QWidget* parent )
00039 : OB_Browser( parent )
00040 {
00041   init( 0 );
00042 }
00043 
00049 SUIT_DataBrowser::SUIT_DataBrowser( SUIT_DataObject* root, QWidget* parent )
00050 : OB_Browser( parent )
00051 {
00052   init( root );
00053 }
00054 
00058 SUIT_DataBrowser::~SUIT_DataBrowser()
00059 {
00060 }
00061 
00066 QString SUIT_DataBrowser::popupClientType() const
00067 {
00068   return "ObjectBrowser";
00069 }
00070 
00075 SUIT_DataObject* SUIT_DataBrowser::root() const
00076 {
00077   SUIT_ProxyModel* m = qobject_cast<SUIT_ProxyModel*>( model() );
00078   return m ? m->root() : 0;
00079 }
00080 
00085 void SUIT_DataBrowser::setRoot( SUIT_DataObject* r )
00086 {
00087   SUIT_ProxyModel* m = qobject_cast<SUIT_ProxyModel*>( model() );
00088   if ( m ) 
00089     m->setRoot( r );
00090 }
00091 
00097 bool SUIT_DataBrowser::autoUpdate() const
00098 {
00099   SUIT_ProxyModel* m = qobject_cast<SUIT_ProxyModel*>( model() );
00100   return m ? m->autoUpdate() : false;
00101 }
00102 
00112 void SUIT_DataBrowser::setAutoUpdate( const bool on )
00113 {
00114   SUIT_ProxyModel* m = qobject_cast<SUIT_ProxyModel*>( model() );
00115   if ( m ) 
00116     m->setAutoUpdate( on );
00117 }
00118 
00123 bool SUIT_DataBrowser::updateModified() const
00124 {
00125   SUIT_ProxyModel* m = qobject_cast<SUIT_ProxyModel*>( model() );
00126   return m ? m->updateModified() : false;
00127 }
00128 
00133 void SUIT_DataBrowser::setUpdateModified( const bool on )
00134 {
00135   SUIT_ProxyModel* m = qobject_cast<SUIT_ProxyModel*>( model() );
00136   if ( m ) 
00137     m->setUpdateModified( on );
00138 }
00139 
00146 void SUIT_DataBrowser::updateTree( SUIT_DataObject* obj, const bool autoOpen )
00147 {
00148   SUIT_ProxyModel* m = qobject_cast<SUIT_ProxyModel*>( model() );
00149   if ( m ) {
00150     m->updateTree( obj );
00151     openLevels();
00152 
00153     if (myAutoSizeFirstColumn)
00154       adjustFirstColumnWidth();
00155     if (myAutoSizeColumns)
00156       adjustColumnsWidth();
00157   }
00158 }
00159 
00165 int SUIT_DataBrowser::shortcutKey(const int id) const
00166 {
00167   return myShortcutMap.value(id)->key();
00168 }
00169 
00177 void SUIT_DataBrowser::setShortcutKey( const int id, const int key )
00178 { 
00179   ShortcutMap::iterator it = myShortcutMap.find( key );
00180   if( it != myShortcutMap.end() )
00181     (*it)->setKey(key);
00182 }
00183 
00188 DataObjectList SUIT_DataBrowser::getSelected() const
00189 {
00190   DataObjectList lst;
00191   getSelected( lst );
00192   return lst;
00193 }
00194 
00200 void SUIT_DataBrowser::getSelected( DataObjectList& lst ) const
00201 {
00202   lst.clear();
00203 
00204   SUIT_ProxyModel* m = qobject_cast<SUIT_ProxyModel*>( model() );
00205 
00206   if ( m ) {
00207     QModelIndexList sel = selectedIndexes();
00208     QModelIndex idx;
00209   
00210     foreach( idx, sel ) {
00211       SUIT_DataObject* obj = m->object( idx );
00212       if ( obj )
00213         lst.append( obj );
00214     }
00215   }
00216 }
00217 
00224 void SUIT_DataBrowser::setSelected( const SUIT_DataObject* obj, const bool append )
00225 {
00226   SUIT_ProxyModel* m = qobject_cast<SUIT_ProxyModel*>( model() );
00227 
00228   if ( m ) {
00229     QModelIndex index = m->index( obj );
00230     if ( index.isValid() )
00231       select( index, true, append );
00232   }
00233 }
00234 
00238 bool modelIndexLessThan(const QModelIndex& lhs, const QModelIndex& rhs)
00239 {
00240   QModelIndex lhs_parent=lhs.parent();
00241   QModelIndex rhs_parent=rhs.parent();
00242   if(lhs_parent < rhs_parent)return true;
00243   if(lhs_parent == rhs_parent) return lhs < rhs;
00244   return false;
00245 }
00246 
00253 void SUIT_DataBrowser::setSelected( const DataObjectList& lst, const bool append )
00254 {
00255   SUIT_ProxyModel* m = qobject_cast<SUIT_ProxyModel*>( model() );
00256 
00257   if ( m ) {
00258     QModelIndexList indexes;
00259     SUIT_DataObject* obj;
00260 
00261     foreach( obj, lst ) {
00262       QModelIndex index = m->index( obj );
00263       if ( index.isValid() )
00264         indexes.append( index );
00265     }
00266     qSort(indexes.begin(), indexes.end(), modelIndexLessThan);
00267 
00268     select( indexes, true, append ); // if !indexes.isEmpty() ???
00269   }
00270 }
00271 
00276 void SUIT_DataBrowser::ensureVisible( SUIT_DataObject* obj )
00277 {
00278   if ( !obj )
00279     return;
00280 
00281   DataObjectList lst;
00282   lst.append( obj );
00283   ensureVisible( lst );
00284 }
00285 
00290 void SUIT_DataBrowser::ensureVisible( const DataObjectList& lst )
00291 {
00292   QtxTreeView* tv = treeView();
00293   SUIT_AbstractModel* treeModel = dynamic_cast<SUIT_AbstractModel*>( model() );
00294   if ( !tv || !treeModel )
00295     return;
00296 
00297   for ( DataObjectList::const_iterator it = lst.begin(); it != lst.end(); ++it )
00298   {
00299     QModelIndex idx = treeModel->index( *it );
00300     if ( idx.isValid() )
00301       tv->scrollTo( idx );
00302   }
00303 }
00304 
00309 void SUIT_DataBrowser::contextMenuPopup( QMenu* menu )
00310 {
00311   createPopupMenu( menu );
00312 }
00313 
00323 void SUIT_DataBrowser::setAutoSizeFirstColumn( const bool on )
00324 {
00325   myAutoSizeFirstColumn = on;
00326 }
00327 
00337 void SUIT_DataBrowser::setAutoSizeColumns( const bool on )
00338 {
00339   myAutoSizeColumns = on;
00340 }
00341 
00346 void SUIT_DataBrowser::contextMenuEvent( QContextMenuEvent* e )
00347 {
00348   contextMenuRequest( e );
00349 }
00350 
00359 void SUIT_DataBrowser::setResizeOnExpandItem( const bool on )
00360 {
00361   myResizeOnExpandItem = on;
00362 }
00363 
00368 void SUIT_DataBrowser::init( SUIT_DataObject* root )
00369 {
00370   SUIT_ProxyModel* m = new SUIT_ProxyModel( root, this );
00371   connect( m, SIGNAL( modelUpdated() ), this, SLOT( onModelUpdated() ) );
00372   
00373   setModel( m );
00374   setItemDelegate( qobject_cast<SUIT_ProxyModel*>( model() )->delegate() );
00375   connect( treeView(), SIGNAL( sortingEnabled( bool ) ), 
00376            model(),    SLOT( setSortingEnabled( bool ) ) );
00377   connect( treeView(), SIGNAL( clicked( const QModelIndex& ) ), 
00378            this,       SLOT( onClicked( const QModelIndex& ) ) );
00379   connect( treeView(), SIGNAL( doubleClicked( const QModelIndex& ) ), 
00380            this,       SLOT( onDblClicked( const QModelIndex& ) ) );
00381   connect( treeView(), SIGNAL( expanded( const QModelIndex& ) ), 
00382            this,       SLOT( onExpanded( const QModelIndex& ) ) );
00383   connect( this      , SIGNAL( requestRename() ),
00384           this      , SLOT ( onStartEditing() ));
00385 
00386   myShortcutMap.insert(UpdateShortcut , new QShortcut( Qt::Key_F5, this, SIGNAL( requestUpdate() ), SIGNAL( requestUpdate() ) ) );
00387   myShortcutMap.insert(RenameShortcut , new QShortcut( Qt::Key_F2, this, SIGNAL( requestRename() ), SIGNAL( requestRename() ) ) );
00388 
00389   myAutoSizeFirstColumn = true;
00390   myAutoSizeColumns = false;
00391   myResizeOnExpandItem = false;
00392 }
00393 
00428 void SUIT_DataBrowser::onModelUpdated()
00429 {
00430   setModified();
00431 }
00432 
00439 void SUIT_DataBrowser::onClicked( const QModelIndex& index )
00440 {
00441   SUIT_ProxyModel* m = qobject_cast<SUIT_ProxyModel*>( model() );
00442 
00443   if ( m ) {
00444     SUIT_DataObject* obj = m->object( index );
00445     if ( obj ) { 
00446       emit( clicked( obj ) );
00447       m->emitClicked(obj, index);
00448     }
00449   }
00450 }
00451 
00458 void SUIT_DataBrowser::onDblClicked( const QModelIndex& index )
00459 {
00460   SUIT_ProxyModel* m = qobject_cast<SUIT_ProxyModel*>( model() );
00461 
00462   if ( m ) {
00463     SUIT_DataObject* obj = m->object( index );
00464     if ( obj ) emit( doubleClicked( obj ) );
00465   }
00466 }
00467 
00472 void SUIT_DataBrowser::onExpanded( const QModelIndex& index )
00473 {
00474   if (myResizeOnExpandItem) {
00475     adjustFirstColumnWidth();
00476     adjustColumnsWidth();
00477   }
00478 }
00479 
00484 void SUIT_DataBrowser::onStartEditing() {
00485   DataObjectList sel = getSelected();
00486   SUIT_ProxyModel* m = qobject_cast<SUIT_ProxyModel*>( model() );
00487   if(treeView() && m && sel.count() == 1){
00488     treeView()->edit(m->index( sel.first(), SUIT_DataObject::NameId ));
00489   }
00490 }
00491