Orfeo Toolbox  3.16
otbWrapperQtWidgetStringListParameter.cxx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: ORFEO Toolbox
4  Language: C++
5  Date: $Date$
6  Version: $Revision$
7 
8 
9  Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
10  See OTBCopyright.txt for details.
11 
12 
13  This software is distributed WITHOUT ANY WARRANTY; without even
14  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  PURPOSE. See the above copyright notices for more information.
16 
17 =========================================================================*/
19 
20 namespace otb
21 {
22 namespace Wrapper
23 {
24 
26 : QtWidgetParameterBase(param, m),
27  m_StringListParam(param)
28 {
29  connect( this, SIGNAL(Change()), GetModel(), SLOT(NotifyUpdate()) );
30 }
31 
33 {
34 }
35 
37 {
38  m_LineEditList.clear();
39 
40  m_StringLayout = new QVBoxLayout();
41 
42  for(unsigned int i=0; i<m_StringListParam->GetValue().size(); i++)
43  {
44  QtStringSelectionWidget * stringSelection = new QtStringSelectionWidget();
45  stringSelection->setFixedHeight( 30 );
46  QString val(m_StringListParam->GetNthElement(i).c_str());
47  stringSelection->GetInput()->setText( m_StringListParam->GetNthElement(i).c_str() ); //val );
48  m_StringLayout->addWidget( stringSelection );
49  m_LineEditList.push_back(stringSelection);
50 
51  connect( stringSelection->GetInput(), SIGNAL(textChanged(const QString&)), this, SLOT(UpdateStringList()) );
52  connect( stringSelection->GetInput(), SIGNAL(textChanged(const QString&)), GetModel(), SLOT(NotifyUpdate()) );
53  }
54 
55  QGroupBox *mainGroup = new QGroupBox();
56  mainGroup->setLayout(m_StringLayout);
57  m_Scroll->setWidget(mainGroup);
58 
59  this->update();
60 }
61 
63 {
64  m_LineEditList.clear();
65  const unsigned int sp(2);
66  const unsigned int buttonSize(30);
67 
68  // Global layout
69  QHBoxLayout * hLayout = new QHBoxLayout;
70  hLayout->setSpacing(sp);
71  hLayout->setContentsMargins(sp, sp, sp, sp);
72 
73  if( m_StringListParam->GetRole() != Role_Output )
74  {
75  // Button layout
76  QVBoxLayout * buttonLayout = new QVBoxLayout;
77  buttonLayout->setSpacing(sp);
78  buttonLayout->setContentsMargins(sp, sp, sp, sp);
79 
80  QHBoxLayout * addSupLayout = new QHBoxLayout;
81  addSupLayout->setSpacing(sp);
82  addSupLayout->setContentsMargins(sp, sp, sp, sp);
83 
84  QHBoxLayout * upDownLayout = new QHBoxLayout;
85  upDownLayout->setSpacing(sp);
86  upDownLayout->setContentsMargins(sp, sp, sp, sp);
87 
88  // Add file button
89  QPushButton * addButton = new QPushButton;
90  addButton->setText("+");
91  addButton->setFixedWidth(buttonSize);
92  addButton->setToolTip("Add a string selector...");
93  connect( addButton, SIGNAL(clicked()), this, SLOT(AddString()) );
94  addSupLayout->addWidget(addButton);
95 
96  // Supress file button
97  QPushButton * supButton = new QPushButton;
98  supButton->setText("-");
99  supButton->setFixedWidth(buttonSize);
100  supButton->setToolTip("Supress the selected string...");
101  connect( supButton, SIGNAL(clicked()), this, SLOT(SupressString()) );
102  addSupLayout->addWidget(supButton);
103  buttonLayout->addLayout(addSupLayout);
104 
105  // Up file edit
106  QPushButton * upButton = new QPushButton;
107  upButton->setText("Up");
108  upButton->setFixedWidth(buttonSize);
109  upButton->setToolTip("Up the selected string in the list...");
110  connect( upButton, SIGNAL(clicked()), this, SLOT(UpString()) );
111  upDownLayout->addWidget(upButton);
112 
113  // Down file edit
114  QPushButton * downButton = new QPushButton;
115  downButton->setText("Down");
116  downButton->setFixedWidth(buttonSize);
117  downButton->setToolTip("Down the selected string in the list...");
118  connect( downButton, SIGNAL(clicked()), this, SLOT(DownString()) );
119  upDownLayout->addWidget(downButton);
120  buttonLayout->addLayout(upDownLayout);
121 
122  // Erase file edit
123  QPushButton * eraseButton = new QPushButton;
124  eraseButton->setText("Erase");
125  eraseButton->setFixedWidth(2*(buttonSize+sp));
126  eraseButton->setToolTip("Erase the selected string of the list...");
127  connect( eraseButton, SIGNAL(clicked()), this, SLOT(EraseString()) );
128  buttonLayout->addWidget(eraseButton);
129 
130  hLayout->addLayout(buttonLayout);
131  }
132 
133  QVBoxLayout * fileLayout = new QVBoxLayout();
134  fileLayout->setSpacing(0);
135 
136  QGroupBox *mainGroup = new QGroupBox();
137  mainGroup->setLayout(fileLayout);
138  QScrollArea * scroll = new QScrollArea();
139  scroll->setWidget(mainGroup);
140  scroll->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
141  scroll->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
142  scroll->setWidgetResizable(true);
143 
144  hLayout->addWidget(scroll);
145 
146 
147  this->setLayout(hLayout);
148 
149  //m_StringLayout = fileLayout;
150  m_HLayout = hLayout;
151  m_Scroll = scroll;
152 
153 }
154 
155 void
157 {
158  // save value
159  for(unsigned int j=0; j<m_StringListParam->GetValue().size(); j++ )
160  {
161  m_StringListParam->SetNthElement(j, m_LineEditList[j]->GetStringName());
162  }
163  emit Change();
164 }
165 
166 
167 void
169 {
170  if(m_LineEditList.size() < 2 )
171  return;
172 
173  m_StringLayout = new QVBoxLayout();
174  m_StringLayout->setSpacing(2);
175 
176  // Map link between old and new index in the list
177  std::map<unsigned int, unsigned int> idMap;
178 
179  // Init map
180  for(unsigned int i=0; i<m_LineEditList.size(); i++ )
181  {
182  idMap[i] = i;
183  }
184 
185  // If the first item is checked, uncheck it...
186  // It won't be moved
187  if( m_LineEditList[0]->IsChecked() )
188  {
189  m_LineEditList[0]->SetChecked(false);
190  }
191 
192 
193  // If other item are checked, up the index
194  // Starts at 1 because the first item mustn't move
195  for(unsigned int i=1; i<m_LineEditList.size(); i++ )
196  {
197  if( m_LineEditList[i]->IsChecked() )
198  {
199  unsigned int tmp = idMap[i];
200  idMap[i] = i-1;
201  idMap[idMap[i-1]] = tmp;
202  }
203  }
204 
205  this->UpdateStringList( idMap );
206 
207  this->RecreateStringList();
208 }
209 
210 void
212 {
213  if(m_LineEditList.size() < 2 )
214  return;
215 
216  m_StringLayout = new QVBoxLayout();
217  m_StringLayout->setSpacing(0);
218 
219  // Map link between old and new index in the list
220  std::map<unsigned int, unsigned int> idMap;
221 
222  // Init map
223  for(unsigned int i=0; i<m_LineEditList.size(); i++ )
224  {
225  idMap[i] = i;
226  }
227 
228  // If the last item is checked, uncheck it...
229  // It won't be moved
230  if( m_LineEditList[m_LineEditList.size()-1]->IsChecked() )
231  {
232  m_LineEditList[m_LineEditList.size()-1]->SetChecked(false);
233  }
234 
235 
236  // If other item are checked, up the index
237  // Stops at size-1 because the last item mustn't move
238  for(int i=m_LineEditList.size()-2; i>=0; i-- )
239  {
240  if( m_LineEditList[i]->IsChecked() )
241  {
242  unsigned int tmp = idMap[i];
243  idMap[i] = i+1;
244  idMap[idMap[i+1]] = tmp;
245  }
246  }
247 
248  this->UpdateStringList( idMap );
249 
250  this->RecreateStringList();
251 }
252 
253 
254 void
255 QtWidgetStringListParameter::UpdateStringList( std::map<unsigned int, unsigned int> idMap )
256 {
257  std::vector<QtStringSelectionWidget *> tmpList;
258  // Keys become values and inverse
259  std::map<unsigned int, unsigned int> idMapBis;
260  for(unsigned int i=0; i<idMap.size(); i++ )
261  {
262  idMapBis[ idMap[i] ] = i;
263  }
264 
265  // Create the new item list
266  for(unsigned int i=0; i<m_LineEditList.size(); i++ )
267  {
268  m_StringLayout->addWidget( m_LineEditList[ idMapBis[i] ] );
269  tmpList.push_back(m_LineEditList[ idMapBis[i] ]);
270  }
271 
272 
273  m_LineEditList = tmpList;
274  QGroupBox *mainGroup = new QGroupBox();
275  mainGroup->setLayout(m_StringLayout);
276  m_Scroll->setWidget(mainGroup);
277 
278  this->update();
279 
280  // notify of value change
281  QString key( m_StringListParam->GetKey() );
282  emit ParameterChanged(key);
283 }
284 
285 
286 void QtWidgetStringListParameter::SetString(const QString& value)
287 {
288  m_StringListParam->AddString(value.toAscii().constData());
289  m_StringListParam->SetUserValue(true);
290  QString key( m_StringListParam->GetKey() );
291  emit ParameterChanged(key);
292 }
293 
294 
295 void
297 {
298  m_StringLayout = new QVBoxLayout();
299  m_StringLayout->setSpacing(0);
300 
301  for(unsigned int i=0; i<m_LineEditList.size(); i++ )
302  {
303  m_StringLayout->addWidget( m_LineEditList[i] );
304  }
305 
306  QtStringSelectionWidget * stringInput = new QtStringSelectionWidget();
307  stringInput->setFixedHeight( 30 );
308  m_StringLayout->addWidget( stringInput );
309  m_LineEditList.push_back(stringInput);
310  m_StringListParam->AddNullElement();
311  connect( stringInput->GetInput(), SIGNAL(textChanged(const QString&)), this, SLOT(UpdateStringList()));
312  connect( stringInput->GetInput(), SIGNAL(textChanged(const QString&)), GetModel(), SLOT(NotifyUpdate()) );
313 
314  QGroupBox *mainGroup = new QGroupBox();
315  mainGroup->setLayout(m_StringLayout);
316  m_Scroll->setWidget(mainGroup);
317 
318 
319  this->update();
320 }
321 
322 
323 void
325 {
326  m_StringLayout = new QVBoxLayout();
327  m_StringLayout->setSpacing(0);
328  std::vector<QtStringSelectionWidget *> tmpList;
329  for(unsigned int i=0; i<m_LineEditList.size(); i++ )
330  {
331  if( !m_LineEditList[i]->IsChecked() )
332  {
333  m_StringLayout->addWidget( m_LineEditList[i] );
334  tmpList.push_back(m_LineEditList[i]);
335  }
336  }
337 
338  m_LineEditList = tmpList;
339 
340  QGroupBox *mainGroup = new QGroupBox();
341  mainGroup->setLayout(m_StringLayout);
342  m_Scroll->setWidget(mainGroup);
343 
344  this->update();
345  this->RecreateStringList();
346 }
347 
348 
349 void
351 {
352  m_LineEditList.clear();
353 
354  m_StringLayout = new QVBoxLayout();
355 
356  QtStringSelectionWidget * stringSelection = new QtStringSelectionWidget();
357  stringSelection->setFixedHeight( 30 );
358  m_StringLayout->addWidget( stringSelection );
359  m_LineEditList.push_back(stringSelection);
360  m_StringListParam->AddNullElement();
361  connect( stringSelection->GetInput(), SIGNAL(textChanged(const QString&)), this, SLOT(UpdateStringList()) );
362 
363  QGroupBox *mainGroup = new QGroupBox();
364  mainGroup->setLayout(m_StringLayout);
365  m_Scroll->setWidget(mainGroup);
366 
367  this->update();
368  this->RecreateStringList();
369 }
370 
371 
373 {
374  // save value
375  m_StringListParam->ClearValue();
376 
377  if( m_LineEditList.size() != 0)
378  {
379  for(unsigned int j=0; j<m_LineEditList.size(); j++ )
380  {
381  m_StringListParam->AddString(m_LineEditList[j]->GetStringName());
382  }
383 
384  emit Change();
385  // notify of value change
386  QString key( m_StringListParam->GetKey() );
387  emit ParameterChanged(key);
388  }
389 }
390 
391 
392 }
393 }

Generated at Sun May 19 2013 01:01:51 for Orfeo Toolbox with doxygen 1.8.3.1