Salome HOME
refs #568: use ordered list view with selection synchronized with object browser...
[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_Tool.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     myObject->addItems( objectNames() );
86     myObject->setCurrentIndex( -1 );
87
88     myObject->blockSignals( block );
89
90     onIndexChanged( myObject->currentIndex() );
91     updateSelection();
92 }
93
94 void HYDROGUI_ObjComboBox::setSectedObject( const QString& theName )
95 {
96     int aNewIdx = myObject->findText( theName );
97     if ( aNewIdx != myObject->currentIndex() )
98         myObject->setCurrentIndex( aNewIdx );
99 }
100
101 QString HYDROGUI_ObjComboBox::selectedObject() const
102 {
103     return myObject->currentText();
104 }
105
106 SUIT_SelectionMgr* HYDROGUI_ObjComboBox::selectionMgr() const
107 {
108     SUIT_SelectionMgr* aSelMgr = 0;
109     if ( module() )
110     {
111         LightApp_Application* app = module()->getApp();
112         if ( app )
113             aSelMgr = app->selectionMgr();
114     }
115     return aSelMgr;
116 }
117
118 QStringList HYDROGUI_ObjComboBox::objectNames() const
119 {
120     QStringList aNames;
121     for ( HYDROData_Iterator it( HYDROData_Document::Document( module()->getStudyId() ), objectType() ); it.More(); it.Next() )
122     {
123         if ( !objectFilter() || objectFilter()->isOk( it.Current() ) )
124             aNames.append( it.Current()->GetName() );
125     }
126     return aNames;
127 }
128
129 void HYDROGUI_ObjComboBox::onIndexChanged( int idx )
130 {
131     if ( idx < 0 )
132         return;
133
134     emit objectSelected( myObject->itemText( idx ) );
135 }
136
137 void HYDROGUI_ObjComboBox::onSelectionChanged()
138 {
139     if ( !objectFilter() || objectFilter()->isActive( this ) )
140         QTimer::singleShot( 0, this, SLOT( updateSelection() ) );
141 }
142
143 void HYDROGUI_ObjComboBox::updateSelection()
144 {
145     int idx = -1;
146     Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::GetSelectedObject( module() );
147     if ( !anObject.IsNull() )
148         idx = myObject->findText( anObject->GetName() );
149
150     if ( idx >= 0 && myObject->currentIndex() != idx )
151     {
152         myObject->setCurrentIndex( idx );
153         emit objectSelected( myObject->itemText( idx ) );
154     }
155 }