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_ObjComboBox.h"
21 #include "HYDROGUI_Tool.h"
22 #include "HYDROGUI_Module.h"
24 #include <LightApp_Application.h>
25 #include <LightApp_SelectionMgr.h>
32 #include <HYDROData_Iterator.h>
33 #include <HYDROData_Document.h>
35 HYDROGUI_ObjComboBox::HYDROGUI_ObjComboBox( HYDROGUI_Module* theModule, const QString& theTitle, const ObjectKind& theType, QWidget* theParent )
36 : QWidget( theParent ),
38 myModule( theModule ),
41 QBoxLayout* base = new QHBoxLayout( this );
44 if ( !theTitle.isEmpty() )
45 base->addWidget( new QLabel( theTitle, this ) );
46 base->addWidget( myObject = new QComboBox( this ) );
48 myObject->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Preferred ) );
50 // Connect signals and slots
51 connect( myObject, SIGNAL( currentIndexChanged( int ) ), this, SLOT( onIndexChanged( int ) ) );
52 connect( selectionMgr(), SIGNAL( selectionChanged() ), this, SLOT( onSelectionChanged() ) );
55 HYDROGUI_ObjComboBox::~HYDROGUI_ObjComboBox()
59 HYDROGUI_Module* HYDROGUI_ObjComboBox::module() const
64 ObjectKind HYDROGUI_ObjComboBox::objectType() const
69 HYDROGUI_ObjComboBoxFilter* HYDROGUI_ObjComboBox::objectFilter() const
74 void HYDROGUI_ObjComboBox::setObjectFilter( HYDROGUI_ObjComboBoxFilter* filter )
79 void HYDROGUI_ObjComboBox::reset()
81 bool block = myObject->signalsBlocked();
82 myObject->blockSignals( true );
86 myObject->addItems( objectNames( myObjects ) );
87 myObject->setCurrentIndex( -1 );
89 myObject->blockSignals( block );
91 onIndexChanged( myObject->currentIndex() );
95 void HYDROGUI_ObjComboBox::setSelectedObject( const QString& theName )
97 int aNewIdx = myObject->findText( theName );
98 if ( aNewIdx != myObject->currentIndex() )
99 myObject->setCurrentIndex( aNewIdx );
102 QString HYDROGUI_ObjComboBox::selectedObject() const
104 return myObject->currentText();
107 SUIT_SelectionMgr* HYDROGUI_ObjComboBox::selectionMgr() const
109 SUIT_SelectionMgr* aSelMgr = 0;
112 LightApp_Application* app = module()->getApp();
114 aSelMgr = app->selectionMgr();
119 QStringList HYDROGUI_ObjComboBox::objectNames( HYDROData_SequenceOfObjects& theObjects ) const
122 for ( HYDROData_Iterator it( HYDROData_Document::Document( module()->getStudyId() ), objectType() ); it.More(); it.Next() )
124 if ( !objectFilter() || objectFilter()->isOk( it.Current() ) )
126 theObjects.Append( it.Current() );
127 aNames.append( it.Current()->GetName() );
133 void HYDROGUI_ObjComboBox::onIndexChanged( int idx )
138 emit objectSelected( myObject->itemText( idx ) );
141 void HYDROGUI_ObjComboBox::onSelectionChanged()
143 if ( !objectFilter() || objectFilter()->isActive( this ) )
144 QTimer::singleShot( 0, this, SLOT( updateSelection() ) );
147 void HYDROGUI_ObjComboBox::updateSelection()
150 Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::GetSelectedObject( module() );
151 if ( !anObject.IsNull() )
152 idx = myObject->findText( anObject->GetName() );
154 if ( idx >= 0 && myObject->currentIndex() != idx )
156 myObject->setCurrentIndex( idx );
157 emit objectSelected( myObject->itemText( idx ) );
161 Handle( HYDROData_Entity ) HYDROGUI_ObjComboBox::GetObject() const
163 int anIndex = myObject->currentIndex();
164 if( anIndex>=0 && anIndex<myObjects.Length() )
165 return myObjects.Value( myObjects.Lower() + anIndex );
167 Handle( HYDROData_Entity )();