Salome HOME
refs #1508
[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_Tool2.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   myModule( theModule ),
30   myFilter( 0 )
31 {
32   myTypes.append( theType );
33   Init(theTitle);
34 }
35
36 HYDROGUI_ObjListBox::HYDROGUI_ObjListBox( HYDROGUI_Module* theModule, const QString& theTitle, const QList<ObjectKind>& theTypes, QWidget* theParent )
37 : QWidget( theParent ),
38   myTypes( theTypes ),
39   myModule( theModule ),
40   myFilter( 0 )
41 {
42   Init(theTitle);
43 }
44
45 void HYDROGUI_ObjListBox::Init(const QString& theTitle)
46 {
47   QGridLayout* aBase = new QGridLayout( this );
48   aBase->setMargin( 0 );
49
50   QPushButton* anInclude = new QPushButton( tr( "INCLUDE" ), this );
51   QPushButton* anExclude = new QPushButton( tr( "EXCLUDE" ), this );
52
53   aBase->addWidget( anInclude, 0, 0 );
54   aBase->addWidget( anExclude, 0, 1 );
55   if( !theTitle.isEmpty() )
56     aBase->addWidget( new QLabel( theTitle, this ), 1, 0 );
57   aBase->addWidget( myList = new QListWidget( this ), 1, 1, 1, 2 );
58   aBase->setColumnStretch( 0, 0 );
59   aBase->setColumnStretch( 1, 0 );
60   aBase->setColumnStretch( 2, 1 );
61
62   myList->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Preferred ) );
63   myList->setSelectionMode( QListWidget::ExtendedSelection );
64
65   connect( anInclude, SIGNAL( clicked() ), this, SLOT( OnInclude() ) );
66   connect( anExclude, SIGNAL( clicked() ), this, SLOT( OnExclude() ) );
67 }
68
69 HYDROGUI_ObjListBox::~HYDROGUI_ObjListBox()
70 {
71 }
72
73 HYDROGUI_Module* HYDROGUI_ObjListBox::module() const
74 {
75     return myModule;
76 }
77
78 QList<ObjectKind> HYDROGUI_ObjListBox::objectTypes() const
79 {
80     return myTypes;
81 }
82
83 HYDROGUI_ObjComboBoxFilter* HYDROGUI_ObjListBox::objectFilter() const
84 {
85     return myFilter;
86 }
87
88 void HYDROGUI_ObjListBox::setObjectFilter( HYDROGUI_ObjComboBoxFilter* filter )
89 {
90     myFilter = filter;
91 }
92
93 void HYDROGUI_ObjListBox::reset()
94 {
95   myList->clear();
96   mySelection.Clear();
97   emit selectionChanged();
98 }
99
100 void HYDROGUI_ObjListBox::setSelectedObjects( const HYDROData_SequenceOfObjects& theObjects )
101 {
102   reset();
103   Append( theObjects );
104 }
105
106 void HYDROGUI_ObjListBox::setObjectsFromSelection()
107 {
108   setSelectedObjects( HYDROGUI_Tool::GetSelectedObjects( module() ) );
109 }
110
111 HYDROData_SequenceOfObjects HYDROGUI_ObjListBox::selectedObjects() const
112 {
113   return mySelection;
114 }
115
116 void HYDROGUI_ObjListBox::OnInclude()
117 {
118   Append( HYDROGUI_Tool::GetSelectedObjects( module() ) );
119   emit selectionChanged();
120 }
121
122 void HYDROGUI_ObjListBox::OnExclude()
123 {
124   QList<QListWidgetItem*> aSelection = myList->selectedItems();
125   foreach( QListWidgetItem* anItem, aSelection )
126   {
127     QString itemText = anItem->text();
128     for (int i=mySelection.Lower();i<=mySelection.Upper();i++)
129     {
130       QString name = mySelection(i)->GetName();
131       if (itemText == name)
132       {
133         mySelection.Remove(i);
134         break;
135       }
136     }
137     delete anItem;
138   }
139   emit selectionChanged();
140 }
141
142 void HYDROGUI_ObjListBox::Append( const HYDROData_SequenceOfObjects& theObjects )
143 {
144   for( int i=theObjects.Lower(), n=theObjects.Upper(); i<=n; i++ )
145   {
146     Handle( HYDROData_Entity ) anObject = theObjects.Value( i );
147     bool isOK = !anObject.IsNull() && myTypes.contains(anObject->GetKind());
148     if( myFilter && isOK )
149       isOK = myFilter->isOk( anObject );
150
151     if( isOK )
152     {
153       QString aName = anObject->GetName();
154       if (myList->findItems(aName, Qt::MatchExactly).size() == 0)
155       {
156         myList->addItem( aName );
157         mySelection.Append( anObject );
158       }
159     }
160   }
161 }