Salome HOME
refs #514: add 'Cursor for specific operations' section into preferences of HYDRO...
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_ObjComboBox.cxx
1 // Copyright (C) 2007-2015  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, or (at your option) any later version.
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 <QTimer>
32 #include <QLabel>
33 #include <QLayout>
34 #include <QComboBox>
35
36 #include <HYDROData_Iterator.h>
37 #include <HYDROData_Document.h>
38
39 HYDROGUI_ObjComboBox::HYDROGUI_ObjComboBox( HYDROGUI_Module* theModule, const QString& theTitle, const ObjectKind& theType, QWidget* theParent )
40     : QWidget( theParent ),
41     myType( theType ),
42     myModule( theModule ),
43     myFilter( 0 )
44 {
45     QBoxLayout* base = new QHBoxLayout( this );
46     base->setMargin( 0 );
47
48     if ( !theTitle.isEmpty() )
49         base->addWidget( new QLabel( theTitle, this ) );
50     base->addWidget( myObject = new QComboBox( this ) );
51
52     myObject->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Preferred ) );
53
54     // Connect signals and slots
55     connect( myObject, SIGNAL( currentIndexChanged( int ) ), this, SLOT( onIndexChanged( int ) ) );
56     connect( selectionMgr(), SIGNAL( selectionChanged() ), this, SLOT( onSelectionChanged() ) );
57 }
58
59 HYDROGUI_ObjComboBox::~HYDROGUI_ObjComboBox()
60 {
61 }
62
63 HYDROGUI_Module* HYDROGUI_ObjComboBox::module() const
64 {
65     return myModule;
66 }
67
68 ObjectKind HYDROGUI_ObjComboBox::objectType() const
69 {
70     return myType;
71 }
72
73 HYDROGUI_ObjComboBoxFilter* HYDROGUI_ObjComboBox::objectFilter() const
74 {
75     return myFilter;
76 }
77
78 void HYDROGUI_ObjComboBox::setObjectFilter( HYDROGUI_ObjComboBoxFilter* filter )
79 {
80     myFilter = filter;
81 }
82
83 void HYDROGUI_ObjComboBox::reset()
84 {
85     bool block = myObject->signalsBlocked();
86     myObject->blockSignals( true );
87
88     myObject->clear();
89     myObject->addItems( objectNames() );
90     myObject->setCurrentIndex( -1 );
91
92     myObject->blockSignals( block );
93
94     onIndexChanged( myObject->currentIndex() );
95     updateSelection();
96 }
97
98 void HYDROGUI_ObjComboBox::setSectedObject( const QString& theName )
99 {
100     int aNewIdx = myObject->findText( theName );
101     if ( aNewIdx != myObject->currentIndex() )
102         myObject->setCurrentIndex( aNewIdx );
103 }
104
105 QString HYDROGUI_ObjComboBox::selectedObject() const
106 {
107     return myObject->currentText();
108 }
109
110 SUIT_SelectionMgr* HYDROGUI_ObjComboBox::selectionMgr() const
111 {
112     SUIT_SelectionMgr* aSelMgr = 0;
113     if ( module() )
114     {
115         LightApp_Application* app = module()->getApp();
116         if ( app )
117             aSelMgr = app->selectionMgr();
118     }
119     return aSelMgr;
120 }
121
122 QStringList HYDROGUI_ObjComboBox::objectNames() const
123 {
124     QStringList aNames;
125     for ( HYDROData_Iterator it( HYDROData_Document::Document( module()->getStudyId() ), objectType() ); it.More(); it.Next() )
126     {
127         if ( !objectFilter() || objectFilter()->isOk( it.Current() ) )
128             aNames.append( it.Current()->GetName() );
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 }