Salome HOME
Merge branch 'BR_v14_rc' into BR_SINUSX_FORMAT
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_ObjListBox.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_ObjListBox.h>
20 #include <HYDROGUI_ObjComboBox.h>
21 #include <HYDROGUI_Tool.h>
22 #include <QGridLayout>
23 #include <QListWidget>
24 #include <QLabel>
25 #include <QPushButton>
26
27 HYDROGUI_ObjListBox::HYDROGUI_ObjListBox( HYDROGUI_Module* theModule, const QString& theTitle, const ObjectKind& theType, QWidget* theParent )
28 : QWidget( theParent ),
29   myType( theType ),
30   myModule( theModule ),
31   myFilter( 0 )
32 {
33   QGridLayout* aBase = new QGridLayout( this );
34   aBase->setMargin( 0 );
35
36   QPushButton* anInclude = new QPushButton( tr( "INCLUDE" ), this );
37   QPushButton* anExclude = new QPushButton( tr( "EXCLUDE" ), this );
38
39   aBase->addWidget( anInclude, 0, 0 );
40   aBase->addWidget( anExclude, 0, 1 );
41   if( !theTitle.isEmpty() )
42     aBase->addWidget( new QLabel( theTitle, this ), 1, 0 );
43   aBase->addWidget( myList = new QListWidget( this ), 1, 1, 1, 2 );
44   aBase->setColumnStretch( 0, 0 );
45   aBase->setColumnStretch( 1, 0 );
46   aBase->setColumnStretch( 2, 1 );
47
48   myList->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Preferred ) );
49   myList->setSelectionMode( QListWidget::ExtendedSelection );
50
51   connect( anInclude, SIGNAL( clicked() ), this, SLOT( OnInclude() ) );
52   connect( anExclude, SIGNAL( clicked() ), this, SLOT( OnExclude() ) );
53 }
54
55 HYDROGUI_ObjListBox::~HYDROGUI_ObjListBox()
56 {
57 }
58
59 HYDROGUI_Module* HYDROGUI_ObjListBox::module() const
60 {
61     return myModule;
62 }
63
64 ObjectKind HYDROGUI_ObjListBox::objectType() const
65 {
66     return myType;
67 }
68
69 HYDROGUI_ObjComboBoxFilter* HYDROGUI_ObjListBox::objectFilter() const
70 {
71     return myFilter;
72 }
73
74 void HYDROGUI_ObjListBox::setObjectFilter( HYDROGUI_ObjComboBoxFilter* filter )
75 {
76     myFilter = filter;
77 }
78
79 void HYDROGUI_ObjListBox::reset()
80 {
81   myList->clear();
82   mySelection.Clear();
83 }
84
85 void HYDROGUI_ObjListBox::setSelectedObjects( const HYDROData_SequenceOfObjects& theObjects )
86 {
87   reset();
88   Append( theObjects );
89 }
90
91 void HYDROGUI_ObjListBox::setObjectsFromSelection()
92 {
93   setSelectedObjects( HYDROGUI_Tool::GetSelectedObjects( module() ) );
94 }
95
96 HYDROData_SequenceOfObjects HYDROGUI_ObjListBox::selectedObjects() const
97 {
98   return mySelection;
99 }
100
101 void HYDROGUI_ObjListBox::OnInclude()
102 {
103   Append( HYDROGUI_Tool::GetSelectedObjects( module() ) );
104 }
105
106 void HYDROGUI_ObjListBox::OnExclude()
107 {
108   QList<QListWidgetItem*> aSelection = myList->selectedItems();
109   foreach( QListWidgetItem* anItem, aSelection )
110   {
111     int anIndex = myList->row( anItem );
112     myList->takeItem( anIndex );
113     mySelection.Remove( anIndex, anIndex );
114   }
115 }
116
117 void HYDROGUI_ObjListBox::Append( const HYDROData_SequenceOfObjects& theObjects )
118 {
119   for( int i=theObjects.Lower(), n=theObjects.Upper(); i<=n; i++ )
120   {
121     Handle( HYDROData_Entity ) anObject = theObjects.Value( i );
122     bool isOK = !anObject.IsNull() && anObject->GetKind()==myType;
123     if( myFilter && isOK )
124       isOK = myFilter->isOk( anObject );
125
126     if( isOK )
127     {
128       QString aName = anObject->GetName();
129       myList->addItem( aName );
130       mySelection.Append( anObject );
131     }
132   }
133 }