Salome HOME
Merge branch 'BR_H2018_2' of https://codev-tuleap.cea.fr/plugins/git/salome/hydro...
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_ObjComboBox.cxx
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.
6 //
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.
11 //
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
15 //
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
17 //
18
19 #include "HYDROGUI_ObjComboBox.h"
20
21 #include "HYDROGUI_Tool2.h"
22 #include "HYDROGUI_Module.h"
23
24 #include <LightApp_Application.h>
25 #include <LightApp_SelectionMgr.h>
26
27 #include <QTimer>
28 #include <QLabel>
29 #include <QLayout>
30 #include <QComboBox>
31
32 #include <HYDROData_Iterator.h>
33 #include <HYDROData_Document.h>
34
35 HYDROGUI_ObjComboBox::HYDROGUI_ObjComboBox( HYDROGUI_Module* theModule, const QString& theTitle, const ObjectKind& theType, QWidget* theParent )
36     : QWidget( theParent ),
37     myType( theType ),
38     myModule( theModule ),
39     myFilter( 0 )
40 {
41     QBoxLayout* base = new QHBoxLayout( this );
42     base->setMargin( 0 );
43
44     if ( !theTitle.isEmpty() )
45         base->addWidget( new QLabel( theTitle, this ) );
46     base->addWidget( myObject = new QComboBox( this ) );
47
48     myObject->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Preferred ) );
49
50     // Connect signals and slots
51     connect( myObject, SIGNAL( currentIndexChanged( int ) ), this, SLOT( onIndexChanged( int ) ) );
52     connect( selectionMgr(), SIGNAL( selectionChanged() ), this, SLOT( onSelectionChanged() ) );
53 }
54
55 HYDROGUI_ObjComboBox::~HYDROGUI_ObjComboBox()
56 {
57 }
58
59 HYDROGUI_Module* HYDROGUI_ObjComboBox::module() const
60 {
61     return myModule;
62 }
63
64 ObjectKind HYDROGUI_ObjComboBox::objectType() const
65 {
66     return myType;
67 }
68
69 HYDROGUI_ObjComboBoxFilter* HYDROGUI_ObjComboBox::objectFilter() const
70 {
71     return myFilter;
72 }
73
74 void HYDROGUI_ObjComboBox::setObjectFilter( HYDROGUI_ObjComboBoxFilter* filter )
75 {
76     myFilter = filter;
77 }
78
79 void HYDROGUI_ObjComboBox::reset()
80 {
81     bool block = myObject->signalsBlocked();
82     myObject->blockSignals( true );
83
84     myObject->clear();
85     myObjects.Clear();
86     myObject->addItems( objectNames( myObjects ) );
87     myObject->setCurrentIndex( -1 );
88
89     myObject->blockSignals( block );
90
91     onIndexChanged( myObject->currentIndex() );
92     updateSelection();
93 }
94
95 void HYDROGUI_ObjComboBox::setSelectedObject( const QString& theName )
96 {
97     int aNewIdx = myObject->findText( theName );
98     if ( aNewIdx != myObject->currentIndex() )
99         myObject->setCurrentIndex( aNewIdx );
100 }
101
102 QString HYDROGUI_ObjComboBox::selectedObject() const
103 {
104     return myObject->currentText();
105 }
106
107 SUIT_SelectionMgr* HYDROGUI_ObjComboBox::selectionMgr() const
108 {
109     SUIT_SelectionMgr* aSelMgr = 0;
110     if ( module() )
111     {
112         LightApp_Application* app = module()->getApp();
113         if ( app )
114             aSelMgr = app->selectionMgr();
115     }
116     return aSelMgr;
117 }
118
119 QStringList HYDROGUI_ObjComboBox::objectNames( HYDROData_SequenceOfObjects& theObjects ) const
120 {
121     QStringList aNames;
122     for ( HYDROData_Iterator it( HYDROData_Document::Document( module()->getStudyId() ), objectType() ); it.More(); it.Next() )
123     {
124         if ( !objectFilter() || objectFilter()->isOk( it.Current() ) )
125         {
126           theObjects.Append( it.Current() );
127             aNames.append( it.Current()->GetName() );
128         }
129     }
130     return aNames;
131 }
132
133 void HYDROGUI_ObjComboBox::onIndexChanged( int idx )
134 {
135     if ( idx < 0 )
136         return;
137
138     emit objectSelected( myObject->itemText( idx ) );
139 }
140
141 void HYDROGUI_ObjComboBox::onSelectionChanged()
142 {
143     if ( !objectFilter() || objectFilter()->isActive( this ) )
144         QTimer::singleShot( 0, this, SLOT( updateSelection() ) );
145 }
146
147 void HYDROGUI_ObjComboBox::updateSelection()
148 {
149     int idx = -1;
150     Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::GetSelectedObject( module() );
151     if ( !anObject.IsNull() )
152         idx = myObject->findText( anObject->GetName() );
153
154     if ( idx >= 0 && myObject->currentIndex() != idx )
155     {
156         myObject->setCurrentIndex( idx );
157         emit objectSelected( myObject->itemText( idx ) );
158     }
159 }
160
161 Handle( HYDROData_Entity ) HYDROGUI_ObjComboBox::GetObject() const
162 {
163   int anIndex = myObject->currentIndex();
164   if( anIndex>=0 && anIndex<myObjects.Length() )
165     return myObjects.Value( myObjects.Lower() + anIndex );
166   else
167     return Handle( HYDROData_Entity )();
168 }
169