Salome HOME
Initial solution for bug #141: Stream object.
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_StreamDlg.cxx
1 // Copyright (C) 2007-2013  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.
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_StreamDlg.h"
24
25 #include "HYDROGUI_Tool.h"
26
27 #include <QComboBox>
28 #include <QGroupBox>
29 #include <QLabel>
30 #include <QLayout>
31 #include <QLineEdit>
32 #include <QListWidget>
33 #include <QPushButton>
34
35 HYDROGUI_StreamDlg::HYDROGUI_StreamDlg( HYDROGUI_Module* theModule, const QString& theTitle )
36 : HYDROGUI_InputPanel( theModule, theTitle )
37 {
38   // Stream name
39   myObjectNameGroup = new QGroupBox( tr( "STREAM_NAME" ), mainFrame() );
40
41   myObjectName = new QLineEdit( myObjectNameGroup );
42
43   QBoxLayout* aNameLayout = new QHBoxLayout( myObjectNameGroup );
44   aNameLayout->setMargin( 5 );
45   aNameLayout->setSpacing( 5 );
46   aNameLayout->addWidget( new QLabel( tr( "NAME" ), myObjectNameGroup ) );
47   aNameLayout->addWidget( myObjectName );
48
49   // Stream parameters
50   QGroupBox* aParamGroup = new QGroupBox( tr( "STREAM_PARAMETERS" ), mainFrame() );
51
52   myAxises = new QComboBox( aParamGroup );
53   myAxises->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
54   QBoxLayout* anAxisLayout = new QHBoxLayout();
55   anAxisLayout->addWidget( new QLabel( tr( "STREAM_HYDRAULIC_AXIS" ) ) );
56   anAxisLayout->addWidget( myAxises );
57
58   myProfiles = new QListWidget( aParamGroup );
59   myProfiles->setSelectionMode( QListWidget::MultiSelection );
60   myProfiles->setEditTriggers( QListWidget::NoEditTriggers );
61   myProfiles->setViewMode( QListWidget::ListMode );
62   myProfiles->setSortingEnabled( false );
63
64   myAddButton = new QPushButton( aParamGroup );
65   myAddButton->setText( tr("ADD") );
66   myRemoveButton = new QPushButton( aParamGroup );
67   myRemoveButton->setText( tr("REMOVE") );
68   QBoxLayout* aButtonsLayout = new QHBoxLayout();
69   aButtonsLayout->addWidget( myAddButton );
70   aButtonsLayout->addWidget( myRemoveButton );
71   aButtonsLayout->addStretch();
72  
73   QBoxLayout* aParamLayout = new QVBoxLayout();
74   aParamLayout->setMargin( 5 );
75   aParamLayout->setSpacing( 5 );
76   aParamLayout->addLayout( anAxisLayout );
77   aParamLayout->addWidget( myProfiles );
78   aParamLayout->addLayout( aButtonsLayout );
79
80   aParamGroup->setLayout( aParamLayout );
81
82   // Common
83   addWidget( myObjectNameGroup );
84   addWidget( aParamGroup );
85   addStretch();
86
87   // Connect signals and slots
88   connect( myAxises, SIGNAL( currentIndexChanged( const QString & ) ), 
89            this, SIGNAL( AxisChanged( const QString& ) ) );
90   connect( myAddButton, SIGNAL( clicked() ), this, SIGNAL( AddProfiles() ) );
91   connect( myRemoveButton, SIGNAL( clicked() ), this, SLOT( onRemoveProfiles() ) );
92 }
93
94 HYDROGUI_StreamDlg::~HYDROGUI_StreamDlg()
95 {
96 }
97
98 void HYDROGUI_StreamDlg::reset()
99 {
100   bool isBlocked = blockSignals( true );
101
102   myObjectName->clear();
103
104   myAxises->clear();
105   myProfiles->clear();
106   myAddButton->setEnabled( false );
107
108   blockSignals( isBlocked );
109
110   onStreamDefChanged();
111 }
112
113 void HYDROGUI_StreamDlg::setObjectName( const QString& theName )
114 {
115   myObjectName->setText( theName );
116 }
117
118 QString HYDROGUI_StreamDlg::getObjectName() const
119 {
120   return myObjectName->text();
121 }
122
123 void HYDROGUI_StreamDlg::setAxisNames( const QStringList& theAxises )
124 {
125   bool isBlocked = blockSignals( true );
126
127   myAxises->clear();
128   myAxises->addItems( theAxises );
129
130   blockSignals( isBlocked );
131 }
132
133 void HYDROGUI_StreamDlg::setAxisName( const QString& theName )
134 {
135   myAddButton->setEnabled( !myAxises->currentText().isEmpty() );
136
137   int aNewIdx = myAxises->findText( theName );
138   if ( aNewIdx != myAxises->currentIndex() )
139   {
140     myAxises->setCurrentIndex( aNewIdx );
141   }
142   else
143   {
144     onStreamDefChanged();
145   }
146 }
147
148 QString HYDROGUI_StreamDlg::getAxisName() const
149 {
150   return myAxises->currentText();
151 }
152
153 void HYDROGUI_StreamDlg::setSelectedProfiles( const QStringList& theProfiles )
154 {
155   bool isBlocked = blockSignals( true );
156
157   myProfiles->setUpdatesEnabled( false );
158   
159   myProfiles->clear();
160   for ( int i = 0, n = theProfiles.length(); i < n; ++i )
161   {
162     const QString& aProfileName = theProfiles.at( i );
163     QListWidgetItem* aListItem = new QListWidgetItem( aProfileName, myProfiles );
164     aListItem->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
165   }
166
167   myProfiles->setUpdatesEnabled( true );
168
169   blockSignals( isBlocked );
170
171   onStreamDefChanged();
172 }
173
174 QStringList HYDROGUI_StreamDlg::getSelectedProfiles() const
175 {
176   QStringList aProfiles;
177
178   for ( int i = 0, n = myProfiles->count(); i < n; ++i )
179   {
180     QListWidgetItem* aListItem = myProfiles->item( i );
181     if ( !aListItem )
182       continue;
183
184     QString aProfileName = aListItem->text();
185     aProfiles << aProfileName;
186   }
187
188   return aProfiles;
189 }
190
191 void HYDROGUI_StreamDlg::onStreamDefChanged()
192 {
193   if ( signalsBlocked() )
194     return;
195
196   emit CreatePreview();
197 }
198
199 void HYDROGUI_StreamDlg::onRemoveProfiles()
200 {
201   QStringList aSelectedProfiles;
202
203   QList<QListWidgetItem*> aSelectedItems = myProfiles->selectedItems();
204   foreach( const QListWidgetItem* anItem, aSelectedItems ) {
205     QString aProfileName = anItem->text();
206     if ( !aProfileName.isEmpty() ) {
207       aSelectedProfiles << aProfileName;
208     }
209   }
210
211   if ( !aSelectedProfiles.isEmpty() ) {
212     emit RemoveProfiles( aSelectedProfiles );
213   }
214 }