Bug Summary

File:OMOptim/OMOptimBasis/build/../GUI/Dialogs/MOParametersDlg.cpp
Warning:line 291, column 8
Potential leak of memory pointed to by 'curLayout'

Annotated Source Code

[?] Use j/k keys for keyboard navigation

1// $Id$
2/**
3 * This file is part of OpenModelica.
4 *
5 * Copyright (c) 1998-CurrentYear, Open Source Modelica Consortium (OSMC),
6 * c/o Linköpings universitet, Department of Computer and Information Science,
7 * SE-58183 Linköping, Sweden.
8 *
9 * All rights reserved.
10 *
11 * THIS PROGRAM IS PROVIDED UNDER THE TERMS OF GPL VERSION 3 LICENSE OR
12 * THIS OSMC PUBLIC LICENSE (OSMC-PL).
13 * ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES RECIPIENT'S ACCEPTANCE
14 * OF THE OSMC PUBLIC LICENSE OR THE GPL VERSION 3, ACCORDING TO RECIPIENTS CHOICE.
15 *
16 * The OpenModelica software and the Open Source Modelica
17 * Consortium (OSMC) Public License (OSMC-PL) are obtained
18 * from OSMC, either from the above address,
19 * from the URLs: http://www.ida.liu.se/projects/OpenModelica or
20 * http://www.openmodelica.org, and in the OpenModelica distribution.
21 * GNU version 3 is obtained from: http://www.gnu.org/copyleft/gpl.html.
22 *
23 * This program is distributed WITHOUT ANY WARRANTY; without
24 * even the implied warranty of MERCHANTABILITY or FITNESS
25 * FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
26 * IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF OSMC-PL.
27 *
28 * See the full OSMC Public License conditions for more details.
29 *
30 * Main contributor 2010, Hubert Thierot, CEP - ARMINES (France)
31 * Main contributor 2010, Hubert Thierot, CEP - ARMINES (France)
32
33 @file MOParametersDlg.cpp
34 @brief Comments for file documentation.
35 @author Hubert Thieriot, hubert.thieriot@mines-paristech.fr
36 Company : CEP - ARMINES (France)
37 http://www-cep.ensmp.fr/english/
38 @version
39*/
40
41#include "MOParametersDlg.h"
42#include "MOTableView.h"
43#include "qsciencespinbox.h"
44#include "LowTools.h"
45
46void WidgetParameters::onSelectFileClicked()
47{
48 QPushButton* button = dynamic_cast<QPushButton*>(sender());
49
50 QLineEdit* line = _pathsMap.value(button,NULL__null);
51
52 if(line)
53 {
54 QString filename = QFileDialog::getOpenFileName(this);
55 if(!filename.isEmpty())
56 line->setText(filename);
57 }
58}
59
60void WidgetParameters::onSelectFolderClicked()
61{
62 QPushButton* button = dynamic_cast<QPushButton*>(sender());
63
64 QLineEdit* line = _pathsMap.value(button,NULL__null);
65
66 if(line)
67 {
68 QString filename = QFileDialog::getExistingDirectory(this);
69 if(!filename.isEmpty())
70 line->setText(filename);
71 }
72}
73
74void WidgetParameters::setDefaultValues()
75{
76 QVariant defaultValue;
77 MOParameter* curParam;
78 QWidget* curWidget;
79
80
81 for(int i=0;i<_mapValueWidgets.keys().size();i++)
82 {
83 // curIndex = _mapValueWidgets.keys().at(i);
84 // iParam = _localParameters->findItem(curIndex,MOParameter::INDEX);
85 curParam = _mapValueWidgets.keys().at(i);
86 curWidget = _mapValueWidgets.value(curParam,NULL__null);
87
88 // get default value
89 defaultValue = curParam->getFieldValue(MOParameter::DEFAULTVALUE);
90
91 setValue(curWidget,defaultValue);
92 }
93
94}
95
96
97
98/**
99 * useDirectLink: if true, widget works directly on parameters. Otherwise,
100 * it works on a copy.
101 *
102 */
103WidgetParameters::WidgetParameters(MOParameters *parameters,bool useDirectLink, bool editable)
104{
105 this->setLocale(QLocale::C);
106 this->setWindowTitle("Parameters");
107 // if(parameters->size()==0)
108 // {
109 // // simulate an accept() (doesn't work in ctor)
110 // // no need to wait for a click on OK !
111 // setResult(QDialog::Accepted);
112 // QApplication::postEvent( this, new QCloseEvent() );
113 // }
114
115 _useDirectLink = useDirectLink;
116 if(_useDirectLink)
1
Assuming the condition is false
2
Taking false branch
117 _localParameters = parameters;
118 else
119 _localParameters = parameters->clone();
120
121 _editable = editable;
122
123 QGridLayout *newLayout = buildLayoutFromParameters();
3
Calling 'WidgetParameters::buildLayoutFromParameters'
124 this->setLayout(newLayout);
125
126}
127
128WidgetParameters::~WidgetParameters()
129{
130 if(!_useDirectLink)
131 delete _localParameters;
132}
133
134
135QGridLayout* WidgetParameters::buildLayoutFromParameters()
136{
137 //Adding Layout
138 QGridLayout *mainLayout = new QGridLayout(this);
139
140 // get groups
141 QMultiMap<QString,MOParameter*> groupmap = _localParameters->groupmap();
142 QStringList groups = groupmap.uniqueKeys();
143
144
145 QStringList paramNames;
146 for(int i=0;i<_localParameters->size();i++)
4
Assuming the condition is false
5
Loop condition is false. Execution continues on line 149
147 paramNames.push_back(_localParameters->at(i)->name());
148
149 QPushButton *newPush;
150 MOParameter* parameter;
151 MOParameterListed *paramList;
152 QList<MOParameter*> groupParameters;
153 QGridLayout *curLayout;
154 QGroupBox *curBox;
155
156 // create group box
157 for(int iG=0;iG<groups.size();iG++)
6
Assuming the condition is true
7
Loop condition is true. Entering loop body
14
Assuming the condition is true
15
Loop condition is true. Entering loop body
22
Assuming the condition is true
23
Loop condition is true. Entering loop body
31
Assuming the condition is false
32
Loop condition is false. Execution continues on line 291
158 {
159 int iRow=0;
160 if(groups.size()>1)
8
Assuming the condition is false
9
Taking false branch
16
Assuming the condition is false
17
Taking false branch
24
Assuming the condition is true
25
Taking true branch
161 {
162 curBox = new QGroupBox(groups.at(iG),this);
163 curLayout = new QGridLayout(curBox);
26
Memory is allocated
164 }
165 else
166 curLayout = mainLayout;
167
168 groupParameters = groupmap.values(groups.at(iG));
169
170 // to reproduce parameters order, start from the end
171 // it seems MultiMap behaves like a pile
172 for(int iP=groupParameters.size()-1;iP>=0;iP--)
10
Assuming 'iP' is < 0
11
Loop condition is false. Execution continues on line 281
18
Assuming 'iP' is < 0
19
Loop condition is false. Execution continues on line 281
27
Assuming 'iP' is < 0
28
Loop condition is false. Execution continues on line 281
173 {
174 parameter = groupParameters.at(iP);
175 // add setting
176 QString dispName;
177 if(parameter->name().contains("/"))
178 dispName = parameter->name().section("/",1,-1);
179 else
180 dispName = parameter->name();
181
182 curLayout->addWidget(new QLabel(parameter->description()),iRow,0);
183 //boxLayout->addWidget(new QLabel(dispName),iRow,0);
184
185 int type = parameter->getFieldValue(MOParameter::TYPE).toInt();
186 QWidget *valueWidget;
187 QVariant value = parameter->getFieldValue(MOParameter::VALUE);
188
189 switch(type)
190 {
191 case MOParameter::STRING :
192 valueWidget = new QLineEdit(this);
193 ((QLineEdit*)valueWidget)->setText(value.toString());
194 connect(((QLineEdit*)valueWidget),SIGNAL(textChanged(QString))"2""textChanged(QString)",this,SLOT(onValueChanged())"1""onValueChanged()");
195 break;
196 case MOParameter::FILEPATH :
197 valueWidget = new QLineEdit(this);
198 ((QLineEdit*)valueWidget)->setText(value.toString());
199 connect(((QLineEdit*)valueWidget),SIGNAL(textChanged(QString))"2""textChanged(QString)",this,SLOT(onValueChanged())"1""onValueChanged()");
200 // add button
201 newPush = new QPushButton("...",this);
202 newPush->setSizePolicy(QSizePolicy::Maximum,QSizePolicy::Preferred);
203 _pathsMap.insert(newPush,((QLineEdit*)valueWidget));
204 curLayout->addWidget(newPush,iRow,2);
205 connect(newPush,SIGNAL(clicked())"2""clicked()",this,SLOT(onSelectFileClicked())"1""onSelectFileClicked()");
206 break;
207 case MOParameter::FOLDERPATH :
208 valueWidget = new QLineEdit(this);
209 ((QLineEdit*)valueWidget)->setText(value.toString());
210 connect(((QLineEdit*)valueWidget),SIGNAL(textChanged(QString))"2""textChanged(QString)",this,SLOT(onValueChanged())"1""onValueChanged()");
211 //add button
212 newPush = new QPushButton("...",this);
213 newPush->setSizePolicy(QSizePolicy::Maximum,QSizePolicy::Preferred);
214 _pathsMap.insert(newPush,((QLineEdit*)valueWidget));
215 curLayout->addWidget(newPush,iRow,2);
216 connect(newPush,SIGNAL(clicked())"2""clicked()",this,SLOT(onSelectFolderClicked())"1""onSelectFolderClicked()");
217 break;
218
219 case MOParameter::DOUBLE :
220 valueWidget = new QScienceSpinBox(this);
221 ((QScienceSpinBox*)valueWidget)->setMinimum(parameter->getFieldValue(MOParameter::MIN).toDouble());
222 ((QScienceSpinBox*)valueWidget)->setMaximum(parameter->getFieldValue(MOParameter::MAX).toDouble());
223 ((QScienceSpinBox*)valueWidget)->setDecimals(10);
224 ((QScienceSpinBox*)valueWidget)->setValue(value.toDouble());
225 connect(((QScienceSpinBox*)valueWidget),SIGNAL(valueChanged(double))"2""valueChanged(double)",this,SLOT(onValueChanged())"1""onValueChanged()");
226 break;
227 case MOParameter::INT :
228 valueWidget = new QSpinBox(this);
229 ((QSpinBox*)valueWidget)->setMinimum(parameter->getFieldValue(MOParameter::MIN).toInt());
230 ((QSpinBox*)valueWidget)->setMaximum(parameter->getFieldValue(MOParameter::MAX).toInt());
231 ((QSpinBox*)valueWidget)->setValue(value.toInt());
232 connect(((QSpinBox*)valueWidget),SIGNAL(valueChanged(int))"2""valueChanged(int)",this,SLOT(onValueChanged())"1""onValueChanged()");
233 break;
234 case MOParameter::BOOL :
235 valueWidget = new QCheckBox(this);
236 Qt::CheckState state;
237 if(value.toBool())
238 state = Qt::Checked;
239 else
240 state = Qt::Unchecked;
241 connect(((QCheckBox*)valueWidget),SIGNAL(stateChanged(int))"2""stateChanged(int)",this,SLOT(onValueChanged())"1""onValueChanged()");
242 ((QCheckBox*)valueWidget)->setCheckState(state);
243 break;
244 case MOParameter::LIST :
245 //if is a list, param should be a MOParameterListed
246 valueWidget = new QComboBox(this);
247 paramList = dynamic_cast<MOParameterListed*>(parameter);
248 if(paramList)
249 {
250 //adding list items in qcombobox
251 for(int iValue = 0 ; iValue<paramList->mapList().keys().size();iValue++)
252 {
253 ((QComboBox*)valueWidget)->addItem(
254 paramList->mapList().values().at(iValue),
255 paramList->mapList().keys().at(iValue));
256 }
257 // set current index
258 ((QComboBox*)valueWidget)->setCurrentIndex(((QComboBox*)valueWidget)->findData(value));
259 connect(((QComboBox*)valueWidget),SIGNAL(currentIndexChanged(int))"2""currentIndexChanged(int)",this,SLOT(onValueChanged())"1""onValueChanged()");
260 }
261 break;
262 default :
263 valueWidget = new QLineEdit(this);
264 ((QLineEdit*)valueWidget)->setText(value.toString());
265 connect(((QLineEdit*)valueWidget),SIGNAL(textChanged(QString))"2""textChanged(QString)",this,SLOT(onValueChanged())"1""onValueChanged()");
266 break;
267
268 }
269
270 curLayout->addWidget(valueWidget,iRow,1);
271 valueWidget->setEnabled(_editable);
272
273 // store (to save data when click ok)
274 _mapValueWidgets.insert(parameter,valueWidget);
275 _paramNames.push_back(parameter->name());
276 _paramTypes.push_back(type);
277
278 iRow++;
279 }
280
281 if(groups.size()>1)
12
Assuming the condition is false
13
Taking false branch
20
Assuming the condition is false
21
Taking false branch
29
Assuming the condition is false
30
Taking false branch
282 {
283 curBox->setLayout(curLayout);
284 mainLayout->addWidget(curBox);
285 }
286 }
287
288
289
290
291 if(_editable)
33
Potential leak of memory pointed to by 'curLayout'
292 updateEnabled(); //update
293
294 return mainLayout;
295}
296
297void WidgetParameters::onValueChanged()
298{
299
300 QWidget* widgetChanged = dynamic_cast<QWidget*>(sender());
301
302 // update value
303 MOParameter* param = _mapValueWidgets.key(widgetChanged,NULL__null);
304 if(param)
305 param->setFieldValue(MOParameter::VALUE,getValue(widgetChanged));
306
307 // update enabled widgets
308 if(widgetChanged)
309 updateEnabled();
310}
311
312
313
314void WidgetParameters::updateEnabled()
315{
316 MOParameter* curParam;
317 QWidget* curWidget;
318 if(_editable)
319 {
320 for(int i=0;i<_localParameters->size();i++)
321 {
322 curParam = _localParameters->at(i);
323 curWidget = _mapValueWidgets.value(curParam,NULL__null);
324
325 if(curWidget)
326 {
327 curWidget->setEnabled(_localParameters->shouldBeEnabled(curParam->name()));
328 }
329 }
330 }
331}
332
333QVariant WidgetParameters::getValue(QWidget* curWidget)
334{
335
336 QLineEdit* lineEdit = dynamic_cast<QLineEdit*>(curWidget);
337 if(lineEdit)
338 return lineEdit->text();
339
340 QScienceSpinBox* dblspinbox = dynamic_cast<QScienceSpinBox*>(curWidget);
341 if(dblspinbox)
342 return dblspinbox->value();
343
344 QSpinBox* spinbox = dynamic_cast<QSpinBox*>(curWidget);
345 if(spinbox)
346 return spinbox->value();
347
348 QCheckBox* checkbox = dynamic_cast<QCheckBox*>(curWidget);
349 if(checkbox)
350 return checkbox->isChecked();
351
352 QComboBox* combo = dynamic_cast<QComboBox*>(curWidget);
353 if(combo)
354 return combo->itemData(combo->currentIndex());
355}
356
357void WidgetParameters::setValue(QWidget* curWidget,QVariant value)
358{
359
360 QLineEdit* lineEdit = dynamic_cast<QLineEdit*>(curWidget);
361 if(lineEdit)
362 lineEdit->setText(value.toString());
363
364 QScienceSpinBox* dblspinbox = dynamic_cast<QScienceSpinBox*>(curWidget);
365 if(dblspinbox)
366 dblspinbox->setValue(value.toDouble());
367
368 QSpinBox* spinbox = dynamic_cast<QSpinBox*>(curWidget);
369 if(spinbox)
370 spinbox->setValue(value.toInt());
371
372 QCheckBox* checkbox = dynamic_cast<QCheckBox*>(curWidget);
373 if(checkbox)
374 checkbox->setChecked(value.toBool());
375
376 QComboBox* combo = dynamic_cast<QComboBox*>(curWidget);
377 if(combo)
378 {
379 combo->setCurrentIndex(combo->findData(value));
380 }
381
382}
383
384
385void MOParametersDlg::pushedOk()
386{
387 _orgParameters->cloneFromOtherVector(_widget->localParameters());
388 accept();
389}
390
391void MOParametersDlg::pushedCancel()
392{
393 reject();
394}
395
396void MOParametersDlg::pushedDefault()
397{
398 _widget->setDefaultValues();
399}
400
401MOParametersDlg::MOParametersDlg(MOParameters *parameters, bool editable)
402{
403 // save parameters
404 _orgParameters = parameters;
405
406 // create layout
407 this->setLayout(new QVBoxLayout(this));
408
409 // add parameters widget
410 // no direct link : widget works on a copy. Only when clicked on Ok,
411 // parameters values are updated.
412 _widget = new WidgetParameters(parameters,false,editable);
413 this->layout()->addWidget(_widget);
414
415 // add buttons
416 QHBoxLayout *buttonsLayout = new QHBoxLayout(this);
417 QPushButton *pushOk = new QPushButton("Ok",this);
418 buttonsLayout->addItem(new QSpacerItem(20, 40, QSizePolicy::Expanding, QSizePolicy::Minimum));
419 buttonsLayout->addWidget(pushOk);
420 connect(pushOk,SIGNAL(clicked())"2""clicked()",this,SLOT(pushedOk())"1""pushedOk()");
421
422 if(editable)
423 {
424 QPushButton *pushDefault = new QPushButton("Restore default",this);
425 connect(pushDefault,SIGNAL(clicked())"2""clicked()",this,SLOT(pushedDefault())"1""pushedDefault()");
426 QPushButton *pushCancel = new QPushButton("Cancel",this);
427 connect(pushCancel,SIGNAL(clicked())"2""clicked()",this,SLOT(pushedCancel())"1""pushedCancel()");
428
429 buttonsLayout->addWidget(pushDefault);
430 buttonsLayout->addWidget(pushCancel);
431 }
432
433 pushOk->setDefault(true);
434
435 QWidget *buttonsWidget = new QWidget(this);
436 buttonsWidget->setLayout(buttonsLayout);
437
438 this->layout()->addWidget(buttonsWidget);
439}
440
441MOParametersDlg::~MOParametersDlg()
442{
443 delete _widget;
444}