Salome HOME
3b199309b8ba5f5e4116feef745b802d028cc85c
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_ObjComboBox.cxx
1 // Copyright (C) 2007-2013  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
23 #include "HYDROGUI_ObjComboBox.h"
24
25 #include "HYDROGUI_Tool.h"
26 #include "HYDROGUI_Module.h"
27
28 #include <LightApp_Application.h>
29 #include <LightApp_SelectionMgr.h>
30
31 #include <QLabel>
32 #include <QLayout>
33 #include <QComboBox>
34
35 #include <HYDROData_Iterator.h>
36 #include <HYDROData_Document.h>
37
38 HYDROGUI_ObjComboBox::HYDROGUI_ObjComboBox( HYDROGUI_Module* theModule, const QString& theTitle, const ObjectKind& theType, QWidget* theParent )
39     : QWidget( theParent ),
40     myType( theType ),
41     myModule( theModule ),
42     myFilter( 0 )
43 {
44     QBoxLayout* base = new QHBoxLayout( this );
45     base->setMargin( 0 );
46
47     base->addWidget( new QLabel( theTitle, this ) );
48     base->addWidget( myObject = new QComboBox( this ) );
49
50     myObject->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Preferred ) );
51
52     // Connect signals and slots
53     connect( myObject, SIGNAL( currentIndexChanged( int ) ), this, SLOT( onIndexChanged( int ) ) );
54     connect( selectionMgr(), SIGNAL( selectionChanged() ), this, SLOT( onSelectionChanged() ) );
55 }
56
57 HYDROGUI_ObjComboBox::~HYDROGUI_ObjComboBox()
58 {
59 }
60
61 HYDROGUI_Module* HYDROGUI_ObjComboBox::module() const
62 {
63     return myModule;
64 }
65
66 ObjectKind HYDROGUI_ObjComboBox::objectType() const
67 {
68     return myType;
69 }
70
71 HYDROGUI_ObjComboBoxFilter* HYDROGUI_ObjComboBox::objectFilter() const
72 {
73     return myFilter;
74 }
75
76 void HYDROGUI_ObjComboBox::setObjectFilter( HYDROGUI_ObjComboBoxFilter* filter )
77 {
78     myFilter = filter;
79 }
80
81 void HYDROGUI_ObjComboBox::reset()
82 {
83     myObject->clear();
84     myObject->addItems( objectNames() );
85     onSelectionChanged();
86 }
87
88 void HYDROGUI_ObjComboBox::setSectedObject( const QString& theName )
89 {
90     int aNewIdx = myObject->findText( theName );
91     if ( aNewIdx != myObject->currentIndex() )
92         myObject->setCurrentIndex( aNewIdx );
93 }
94
95 QString HYDROGUI_ObjComboBox::selectedObject() const
96 {
97     return myObject->currentText();
98 }
99
100 SUIT_SelectionMgr* HYDROGUI_ObjComboBox::selectionMgr() const
101 {
102     SUIT_SelectionMgr* aSelMgr = 0;
103     if ( module() )
104     {
105         LightApp_Application* app = module()->getApp();
106         if ( app )
107             aSelMgr = app->selectionMgr();
108     }
109     return aSelMgr;
110 }
111
112 QStringList HYDROGUI_ObjComboBox::objectNames() const
113 {
114     QStringList aNames;
115     for ( HYDROData_Iterator it( HYDROData_Document::Document( module()->getStudyId() ), objectType() ); it.More(); it.Next() )
116     {
117         if ( !objectFilter() || objectFilter()->isOk( it.Current() ) )
118             aNames.append( it.Current()->GetName() );
119     }
120     return aNames;
121 }
122
123 void HYDROGUI_ObjComboBox::onIndexChanged( int idx )
124 {
125     if ( idx < 0 )
126         return;
127
128     emit objectSelected( myObject->itemText( idx ) );
129 }
130
131 void HYDROGUI_ObjComboBox::onSelectionChanged()
132 {
133     int idx = -1;
134     Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::GetSelectedObject( module() );
135     if ( !anObject.IsNull() )
136         idx = myObject->findText( anObject->GetName() );
137
138     myObject->setCurrentIndex( idx );
139     if ( idx >= 0 )
140         emit objectSelected( myObject->itemText( idx ) );
141 }