Bug Summary

File:OMOptim/OMOptim/build/../Core/Modelica/ModItemsTree.cpp
Warning:line 397, column 27
Called C++ object pointer is null

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 ModItemsTree.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 "ModItemsTree.h"
42#include "Project.h"
43
44
45
46ModItemsTree::ModItemsTree(Project* project,ModLoader* modLoader,MOomc* moomc,QObject *parent)
47 : QAbstractItemModel(parent)
48{
49 _modLoader = modLoader;
50 _moomc = moomc;
51 _rootElement = new ModItem(_moomc);
52 _project = project;
53 _enabled = true;
54
55 _showComponents = true;
56
57#if (QT_VERSION((5<<16)|(9<<8)|(5)) >= QT_VERSION_CHECK(5, 0, 0)((5<<16)|(0<<8)|(0)))
58 // no more setSupportedDragActions in Qt5, override supportedDropActions instead
59#else
60 setSupportedDragActions(Qt::CopyAction | Qt::MoveAction);
61#endif
62
63
64}
65
66ModItemsTree::~ModItemsTree()
67{
68 clear();
69 delete _rootElement;
70}
71
72
73void ModItemsTree::clear()
74{
75 beginResetModel();
76 _rootElement->clear();
77 endResetModel();
78}
79
80
81bool ModItemsTree::addChild(ModItem* parent,ModItem* child)
82{
83 bool ok;
84 QModelIndex parentIndex = indexOf(parent);
85 beginInsertRows(parentIndex,parent->childCount(),parent->childCount());
86 ok = parent->addChild(child);
87 endInsertRows();
88 return ok;
89}
90
91void ModItemsTree::readFromOMCWThread(ModItem* parent,int depthMax, QString direction, int curDepth)
92{
93
94 // load the models icon
95 ModItemsLoader modItemsLoader(this,parent,depthMax,direction,curDepth);
96 modItemsLoader.run();
97 // still not working properly on all platforms
98 // while (modItemsLoader->isRunning())
99 // {
100 // qApp->processEvents(QEventLoop::ExcludeUserInputEvents);
101 // }
102
103}
104
105void ModItemsTree::addExeModel(ModItem* parent, QString name, QFileInfo inputFile, QFileInfo exeFile)
106{
107 ModItem* newExeElement = new ExeModel(name,exeFile,inputFile);
108 addChild(parent, newExeElement);
109 parent->setChildrenReaden(true);
110 parent->emitModified();
111}
112
113void ModItemsTree::emitDataChanged()
114{
115
116 if(_rootElement->childCount()>0)
117 {
118 QModelIndex first = indexOf(_rootElement->child(0));
119 QModelIndex last = indexOf(_rootElement->child(_rootElement->childCount()-1));
120 emit dataChanged(first,last);
121 }
122
123 emit dataChanged(indexOf(_rootElement),indexOf(_rootElement));
124
125}
126
127
128
129
130/**
131* This function fills parent with its children. To achieve this, it calls MOOmc functions.
132* @arg depthMax : maximum depth filling should go (each time functions goes in children, grand children...
133* Depth is increased by one.
134* @arg curDepth : current depth.
135* @arg direction : allows to drive looking function along a path. This is especially used
136* to look for deep modelas without covering all items.
137*/
138void ModItemsTree::readFromOmc(ModItem* parent,int depthMax,QString direction,int curDepth)
139{
140 if(parent->_readMutex.tryLock() && _project->useOmc())
141 {
142 if((curDepth<=depthMax)&&!parent->childrenReaden())
143 {
144 ModItem* newElement;
145
146
147
148 QString childrenDirection = direction.section(".",curDepth+1,curDepth+1);
149
150 bool readPackages = (parent->getClassRestr()==Modelica::PACKAGE) || (parent->getClassRestr()==Modelica::GENERIC);
151 bool readModels = (parent->getClassRestr()==Modelica::PACKAGE) || (parent->getClassRestr()==Modelica::GENERIC);
152 bool readComponents = (parent->getClassRestr()==Modelica::MODEL) || (parent->getClassRestr()==Modelica::GENERIC) || (parent->getClassRestr()==Modelica::COMPONENT);
153 bool readRecords = (parent->getClassRestr()==Modelica::PACKAGE) || (parent->getClassRestr()==Modelica::MODEL) ||(parent->getClassRestr()==Modelica::GENERIC)|| (parent->getClassRestr()==Modelica::COMPONENT);
154 bool readClasses = readModels;
155
156 QString fullParentName = parent->name(ModItem::FULL);
157 QString parentClassName = parent->getModClassName();
158 QString prefix;
159 if(!fullParentName.isEmpty())
160 prefix = fullParentName+".";
161
162 // read packages
163 if(readPackages)
164 {
165 QStringList packageNames = _moomc->getPackages(fullParentName);
166 for(int i=0;i<packageNames.size();i++)
167 {
168 newElement = new ModPackage(_moomc,parent,prefix+packageNames.at(i));
169 if(addChild(parent,newElement))
170 if((childrenDirection=="") || (childrenDirection==packageNames.at(i)))
171 readFromOMCWThread(newElement,depthMax,direction,curDepth+1);
172 }
173 }
174
175 // read models
176 if(readModels)
177 {
178 QStringList modelNames = _moomc->getModels(fullParentName);
179 for(int i=0;i<modelNames.size();i++)
180 {
181 newElement = new ModModel(_moomc,parent,prefix+modelNames.at(i));
182 if(addChild(parent,newElement))
183 if((childrenDirection=="") || (childrenDirection==modelNames.at(i)))
184 readFromOMCWThread(newElement,depthMax,direction,curDepth+1);
185 }
186 }
187
188 // read records
189 if(readRecords)
190 {
191 QStringList recordNames = _moomc->getRecords(fullParentName);
192 for(int i=0;i<recordNames.size();i++)
193 {
194 newElement = new ModRecord(_moomc,parent,prefix+recordNames.at(i));
195 if(addChild(parent,newElement))
196 if((childrenDirection=="") || (childrenDirection==recordNames.at(i)))
197 readFromOMCWThread(newElement,depthMax,direction,curDepth+1);
198 }
199 }
200
201 // read classes
202 if(readClasses)
203 {
204 QStringList classNames = _moomc->getClasses(fullParentName);
205 for(int i=0;i<classNames.size();i++)
206 {
207 newElement = new ModItem(_moomc,parent,prefix+classNames.at(i));
208 if(addChild(parent,newElement))
209 if((childrenDirection=="") || (childrenDirection==classNames.at(i)))
210 readFromOMCWThread(newElement,depthMax,direction,curDepth+1);
211 }
212 }
213
214 //read components
215 if(readComponents)
216 {
217 QStringList compNames;
218 QStringList compClasses;
219 _moomc->getContainedComponents(parentClassName,compNames,compClasses,true);
220 for(int i=0;i<compNames.size();i++)
221 {
222 newElement = new ModComponent(_moomc,parent,prefix+compNames.at(i),compClasses.at(i));
223 if(addChild(parent,newElement))
224 if((childrenDirection=="") || (childrenDirection==compClasses.at(i)))
225 readFromOMCWThread(newElement,depthMax,direction,curDepth+1);
226 }
227 }
228 parent->setChildrenReaden(true);
229 parent->emitModified();
230
231
232 }
233 parent->_readMutex.unlock();
234 }
235}
236
237/**
238 * Another version of readFromOmc.
239 * @sa readFromOmc
240 * @todo benchmark reading functions
241 */
242void ModItemsTree::readFromOmcV2(ModItem* parent,int depthMax,QString direction,int curDepth)
243{
244 if(parent->_readMutex.tryLock() && _project->useOmc())
245 {
246 if((curDepth<=depthMax)&&!parent->childrenReaden())
247 {
248 QString childrenDirection = direction.section(".",curDepth+1,curDepth+1);
249 QString fullParentClass = parent->getModClassName();
250
251 QString fullParentName = parent->name(ModItem::FULL);
252 QString prefix;
253 if(!fullParentName.isEmpty())
254 prefix = fullParentName+".";
255
256
257 ModItem* newElement;
258
259 //read packages, models and components
260 QStringList packagesClasses,modelsClasses,recordNames,compsNames,compsClasses;
261 _moomc->readElementInfos(fullParentClass,packagesClasses,modelsClasses,recordNames,compsNames,compsClasses);
262
263 // get inherited components
264 QStringList inheritedCompNames,inheritedCompClasses;
265 _moomc->getInheritedComponents(fullParentClass,inheritedCompNames,inheritedCompClasses);
266 compsNames << inheritedCompNames;
267 compsClasses << inheritedCompClasses;
268
269 // adding child packages and read them
270 for(int i=0;i<packagesClasses.size();i++)
271 {
272 newElement = new ModPackage(_moomc,parent,prefix+packagesClasses.at(i));
273 if(addChild(parent,newElement))
274 if((childrenDirection=="") || (childrenDirection==packagesClasses.at(i)))
275 readFromOmcV2(newElement,depthMax,direction,curDepth+1);
276 }
277
278 // adding child models and read them
279 for(int i=0;i<modelsClasses.size();i++)
280 {
281 newElement = new ModModel(_moomc,parent,prefix+modelsClasses.at(i));
282 if(addChild(parent,newElement))
283 if((childrenDirection=="") || (childrenDirection==modelsClasses.at(i)))
284 readFromOmcV2(newElement,depthMax,direction,curDepth+1);
285 }
286
287 // adding child components and read them
288 for(int i=0;i<compsNames.size();i++)
289 {
290 newElement = new ModComponent(_moomc,parent,prefix+compsNames.at(i),compsClasses.at(i));
291 if(addChild(parent,newElement))
292 if((childrenDirection=="") || (childrenDirection==compsClasses.at(i)))
293 readFromOmcV2(newElement,depthMax,direction,curDepth+1);
294 }
295
296 parent->setChildrenReaden(true);
297 parent->emitModified();
298
299
300 }
301 parent->_readMutex.unlock();
302 }
303}
304
305/**
306 * Another version of readFromOmc.
307 * @sa readFromOmc
308 * @todo benchmark reading functions
309 */
310void ModItemsTree::readFromOmcV3(ModItem* parent,int depthMax,QString direction,int curDepth)
311{
312 if(parent->_readMutex.tryLock() && _project->useOmc())
313 {
314 if((curDepth<=depthMax)&&!parent->childrenReaden())
315 {
316 QString childrenDirection = direction.section(".",curDepth+1,curDepth+1);
317 QString fullParentClass = parent->getModClassName();
318 QString fullParentName = parent->name(ModItem::FULL);
319
320
321 QString prefix;
322 if(!fullParentName.isEmpty())
323 prefix = fullParentName+".";
324
325 ModItem* newElement;
326
327 //read packages, models and components
328 QStringList packagesClasses,modelsClasses,recordsNames,compsNames,compsClasses;
329 bool foundInLib = false;
330
331
332 ModItem* _corrLibComp = findInDescendants(fullParentName);
333 if(_corrLibComp)
334 {
335 // If class is already loaded in package, use its information
336 foundInLib = true;
337 childrenInfos(_corrLibComp,packagesClasses,modelsClasses,recordsNames,compsNames,compsClasses);
338 }
339 else
340 {
341 // Call parent->_moomc
342 _moomc->readElementInfos(fullParentClass,packagesClasses,modelsClasses,recordsNames,compsNames,compsClasses);
343 // get inherited components
344 QStringList inheritedCompNames,inheritedCompClasses;
345 _moomc->getInheritedComponents(fullParentClass,inheritedCompNames,inheritedCompClasses);
346 compsNames << inheritedCompNames;
347 compsClasses << inheritedCompClasses;
348 }
349
350
351 // adding child packages and read them
352 for(int i=0;i<packagesClasses.size();i++)
353 {
354 newElement = new ModPackage(_moomc,parent,prefix+packagesClasses.at(i));
355 if(addChild(parent,newElement))
356 if((childrenDirection=="") || (childrenDirection==packagesClasses.at(i)))
357 readFromOmcV3(newElement,depthMax,direction,curDepth+1);
358 }
359
360 // adding child models and read them
361 for(int i=0;i<modelsClasses.size();i++)
362 {
363 newElement = new ModModel(_moomc,parent,prefix+modelsClasses.at(i));
364 if(addChild(parent,newElement))
365 if((childrenDirection=="") || (childrenDirection==modelsClasses.at(i)))
366 readFromOmcV3(newElement,depthMax,direction,curDepth+1);
367 }
368
369 // adding child components and read them
370 for(int i=0;i<compsNames.size();i++)
371 {
372 newElement = new ModComponent(_moomc,parent,prefix+compsNames.at(i),compsClasses.at(i));
373 if(addChild(parent,newElement))
374 if((childrenDirection=="") || (childrenDirection==compsClasses.at(i)))
375 readFromOmcV3(newElement,depthMax,direction,curDepth+1);
376 }
377
378 parent->setChildrenReaden(true);
379 parent->emitModified();
380
381 }
382 parent->_readMutex.unlock();
383 }
384}
385
386
387
388/**
389 * Finding function : within parent, look for children whom fullname equals argument
390 */
391ModItem* ModItemsTree::findInDescendants(QString fullName,ModItem* parent)
392{
393 if(parent==NULL__null)
2
Assuming pointer value is null
3
Assuming 'parent' is equal to NULL
4
Taking true branch
394 parent = _rootElement;
5
Null pointer value stored to 'parent'
395
396 ModItem* curChild;
397 QString curFullName = parent->name(ModItem::FULL);
6
Called C++ object pointer is null
398
399 int curDepth = parent->depth();
400 int lookingDepth = fullName.split(".").size()-1;
401
402 // check if it is this component
403 if(curFullName == fullName)
404 return parent;
405
406 //first check name compatibility
407 if(fullName.indexOf(curFullName)!=0)
408 return NULL__null;
409
410 // then check children are readen
411 if(!parent->childrenReaden())
412 {
413 // if not, check in its direction
414 readFromOmc(parent,lookingDepth,fullName,curDepth);
415 }
416
417 //get child name to visit
418 //QString childShortName = fullName.section(".",curDepth,curDepth);
419 QString childShortName = fullName;
420 // first remove parent name
421 // A ajouter la une condition if not executable else childShortName = curFullName !!!!!!
422 if(!fullName.endsWith(".exe"))
423 {
424 childShortName.remove(QRegExp("^"+curFullName+"\\."));
425 // then take first section
426 childShortName = childShortName.section(".",0,0);
427
428
429 // looking in children
430 for(int iChild=0;iChild<parent->childCount();iChild++)
431 {
432 curChild = parent->child(iChild);
433 if(curChild->name(ModItem::SHORT)==childShortName)
434 return findInDescendants(fullName,curChild);
435 }
436 }
437 else
438 {
439 for(int iChild=0;iChild<parent->childCount();iChild++)
440 {
441 curChild = parent->child(iChild);
442 if(curChild->name(ModItem::FULL)==childShortName)
443 return findInDescendants(fullName,curChild);
444 }
445
446
447 }
448 return NULL__null;
449}
450
451/**
452 * Finding function : returns all ModItem whom classname equals argument className.
453 */
454QList<ModItem*> ModItemsTree::findInDescendantsByClass(QString className,ModItem* parent)
455{
456 if(parent==NULL__null)
457 parent = _rootElement;
458
459 ModItem* curClass;
460 QList<ModItem*> curClasses;
461 int iChild;
462
463 int curDepth = parent->depth();
464
465
466
467 // then check children are readen
468 if(!parent->childrenReaden())
469 {
470 // if not, check in its direction
471 readFromOMCWThread(parent,curDepth+1,QString(),curDepth);
472 }
473
474 // looking if one child corresponds
475 for(iChild=0;iChild<parent->childCount();iChild++)
476 {
477 curClass = parent->child(iChild);
478 if(curClass->getModClassName()==className)
479 curClasses.push_back(curClass);
480
481 //look deeper in children
482 curClasses << findInDescendantsByClass(className,curClass);
483 }
484
485 return curClasses;
486
487}
488
489/**
490 * Finding function : returns all ModItem whom class equals or inherits className.
491 */
492QList<ModItem*> ModItemsTree::findInDescendantsByInheritedClass(QString className,ModItem* parent)
493{
494 if(parent==NULL__null)
495 parent = _rootElement;
496
497 ModItem* curChild;
498 QString curChildClass;
499 QList<ModItem*> result;
500 int iChild;
501
502 int curDepth = parent->depth();
503
504 // then check children are readen
505 if(!parent->childrenReaden())
506 {
507 // if not, check in its direction
508 readFromOMCWThread(parent,curDepth+1,QString(),curDepth);
509 }
510
511 // looking if one child corresponds
512 for(iChild=0;iChild<parent->childCount();iChild++)
513 {
514 curChild = parent->child(iChild);
515 curChildClass = curChild->getModClassName();
516 if((curChildClass==className)||_moomc->inherits(curChildClass,className))
517 result.push_back(curChild);
518
519 //look deeper in children
520 result << findInDescendantsByInheritedClass(className,curChild);
521 }
522
523 return result;
524}
525
526
527/**
528 * Finding function : returns all components whom classname inherits argument className.
529 */
530QList<ModItem*> ModItemsTree::findCompOfInheritingClassInDescendants(QString className,ModItem* parent)
531{
532 if(parent==NULL__null)
533 parent = _rootElement;
534
535 ModItem* curComponent;
536 QList<ModItem*> curComponents;
537 int iChild;
538 QString curCompClass;
539
540 int curDepth = parent->depth();
541
542 // then check children are readen
543 if(!parent->childrenReaden())
544 {
545 // if not, check in its direction
546 readFromOMCWThread(parent,curDepth+1,QString(),curDepth);
547 }
548
549 int nbCompChild = parent->compChildCount();
550 int nbModelChild = parent->modelChildCount();
551 int nbPackageChild = parent->packageChildCount();
552
553 // looking if one child corresponds
554 for(iChild=0;iChild<nbCompChild;iChild++)
555 {
556 curComponent = parent->compChild(iChild);
557 curCompClass = curComponent->getModClassName();
558 if((curCompClass==className)||_moomc->inherits(curCompClass,className))
559 curComponents.push_back(curComponent);
560 }
561
562 //look deeper in components children
563 iChild=0;
564 while(iChild<nbCompChild)
565 {
566 curComponents << findCompOfInheritingClassInDescendants(className,parent->compChild(iChild));
567 iChild++;
568 }
569
570 //look deeper in models children
571 iChild=0;
572 while(iChild<nbModelChild)
573 {
574 curComponents << findCompOfInheritingClassInDescendants(className,parent->modelChild(iChild));
575 iChild++;
576 }
577
578 //look deeper in packages children
579 iChild=0;
580 while(iChild<nbPackageChild)
581 {
582 curComponents << findCompOfInheritingClassInDescendants(className,parent->packageChild(iChild));
583 iChild++;
584 }
585
586 return curComponents;
587}
588
589/**
590 * Finding function : returns all components whom classname inherits argument className.
591 * In a multimap since there are different classNames here.
592 * @param classFilter : stop seeking in parent class does not go through this filter
593 */
594void ModItemsTree::findCompOfInheritingClassesInDescendants(QStringList classNames,ModItem* parent,QMultiMap<QString,ModItem*> &result,QRegExp classFilter)
595{
596 if(parent==NULL__null)
597 return;
598
599 ModItem* curComponent;
600
601 int iChild;
602 QString curCompClass;
603
604 QString parentClass = parent->getModClassName();
605 if(classFilter.isValid() && !parentClass.contains(classFilter))
606 return ;// no need to carry on
607
608 int curDepth = parent->depth();
609 // then check children are readen
610 if(!parent->childrenReaden())
611 {
612 // if not, check in its direction
613 readFromOMCWThread(parent,curDepth+1,QString(),curDepth);
614 }
615
616 int nbCompChild = parent->compChildCount();
617 int nbModelChild = parent->modelChildCount();
618 int nbPackageChild = parent->packageChildCount();
619
620 // looking if one child corresponds
621 for(iChild=0;iChild<nbCompChild;iChild++)
622 {
623 curComponent = parent->compChild(iChild);
624 curCompClass = curComponent->getModClassName();
625
626 foreach(QString className, classNames)for (auto _container_ = QtPrivate::qMakeForeachContainer(classNames
); _container_.control && _container_.i != _container_
.e; ++_container_.i, _container_.control ^= 1) for (QString className
= *_container_.i; _container_.control; _container_.control =
0)
627 {
628 if((curCompClass==className)||_moomc->inherits(curCompClass,className))
629 result.insert(className,curComponent);
630 }
631 }
632
633 //look deeper in components children
634 iChild=0;
635 while(iChild<nbCompChild)
636 {
637 findCompOfInheritingClassesInDescendants(classNames,parent->compChild(iChild),result);
638 iChild++;
639 }
640
641 //look deeper in models children
642 iChild=0;
643 while(iChild<nbModelChild)
644 {
645 findCompOfInheritingClassesInDescendants(classNames,parent->modelChild(iChild),result);
646 iChild++;
647 }
648
649 //look deeper in packages children
650 iChild=0;
651 while(iChild<nbPackageChild)
652 {
653 findCompOfInheritingClassesInDescendants(classNames,parent->packageChild(iChild),result);
654 iChild++;
655 }
656}
657
658void ModItemsTree::findCompOfInheritingClassesInDescendants2(const QStringList & classNames,QString parentName,QString parentClass,QMultiMap<QString,QString> &result,QRegExp classFilter)
659{
660
661 if(classFilter.isValid() && !parentClass.contains(classFilter))
662 return ;// no need to carry on
663
664
665 QStringList compShortNames,compClasses;
666 QString childLongName;
667 // quick version but does not catch everything
668 _moomc->getContainedComponents(parentClass,compShortNames,compClasses,true);
669
670 foreach(QString className, classNames)for (auto _container_ = QtPrivate::qMakeForeachContainer(classNames
); _container_.control && _container_.i != _container_
.e; ++_container_.i, _container_.control ^= 1) for (QString className
= *_container_.i; _container_.control; _container_.control =
0)
671 {
672 for(int i=0;i<compClasses.size();i++)
673 {
674 if(!classFilter.isValid() || parentClass.contains(classFilter))
675 {
676 if((compClasses.at(i)==className)||_moomc->inherits(compClasses.at(i),className))
677 {
678 childLongName = parentClass.isEmpty() ? compShortNames.at(i) : parentName+"."+compShortNames.at(i);
679 result.insert(className,childLongName);
680 }
681 }
682 }
683 }
684
685 ModItem* compItem;
686 for(int i=0;i<compClasses.size();i++)
687 {
688 childLongName = parentClass.isEmpty() ? compShortNames.at(i) : parentName+"."+compShortNames.at(i);
689 findCompOfInheritingClassesInDescendants2(classNames,childLongName,compClasses.at(i),result,classFilter);
690 }
691}
692
693
694
695/**
696 * Finding function : returns all components whom classname equals argument className.
697 */
698QList<ModItem*> ModItemsTree::findCompOfClassInDescendants(QString className,ModItem* parent)
699{
700 if(parent==NULL__null)
701 parent = _rootElement;
702
703 ModItem* curComponent;
704 QList<ModItem*> curComponents;
705 int iChild;
706
707 int curDepth = parent->depth();
708 // then check children are readen
709 if(!parent->childrenReaden())
710 {
711 // if not, check in its direction
712 readFromOMCWThread(parent,curDepth+1,QString(),curDepth);
713 }
714
715 int nbCompChild = parent->compChildCount();
716 int nbModelChild = parent->modelChildCount();
717 int nbPackageChild = parent->packageChildCount();
718
719 // looking if one child corresponds
720 for(iChild=0;iChild<nbCompChild;iChild++)
721 {
722 curComponent = parent->compChild(iChild);
723 if(curComponent->getFieldValue(ModComponent::CLASSNAME)==className)
724 curComponents.push_back(curComponent);
725 }
726
727 //look deeper in components children
728 iChild=0;
729 while(iChild<nbCompChild)
730 {
731 curComponents << findCompOfClassInDescendants(className,parent->compChild(iChild));
732 iChild++;
733 }
734
735 //look deeper in models children
736 iChild=0;
737 while(iChild<nbModelChild)
738 {
739 curComponents << findCompOfClassInDescendants(className,parent->modelChild(iChild));
740 iChild++;
741 }
742
743 //look deeper in packages children
744 iChild=0;
745 while(iChild<nbPackageChild)
746 {
747 curComponents << findCompOfClassInDescendants(className,parent->packageChild(iChild));
748 iChild++;
749 }
750
751 return curComponents;
752}
753
754/**
755 * Returns whether or not a ModItem is in this tree.
756 */
757bool ModItemsTree::isInDescendants(QString fullName,ModItem* parent)
758{
759 ModItem* foundClass = findInDescendants(fullName,parent);
760 return (bool)(foundClass);
761}
762
763
764int ModItemsTree::columnCount(const QModelIndex &parent) const
765{
766 //return ModItem::nbFields;
767 return 1;
768}
769
770QVariant ModItemsTree::data(const QModelIndex &index, int role) const
771{
772 if(_enabled)
773 {
774 if (!index.isValid())
775 return QVariant();
776
777
778 if (role != Qt::DisplayRole && role !=Qt::ToolTipRole && role != Qt::DecorationRole)
779 return QVariant();
780
781 if(index.column()<0 || index.column()>=ModItem::nbFields)
782 return QVariant();
783
784 ModItem *item = static_cast<ModItem*>(index.internalPointer());
785
786 if(item)
787 {
788 if(role==Qt::DecorationRole)
789 return getModelicaNodeIcon(item);
790
791 // fullfiling is now done in ::fetchmore
792 // if(!item->childrenReaden())
793 // readFromOmcV2(item,1);
794
795 if (role == Qt::ToolTipRole)
796 {
797 QString tooltip = item->getStrToolTip();
798 return tooltip;
799 }
800
801 // if display, only display short name (since hierarchy is visible)
802 if((role == Qt::DisplayRole) && (index.column()==ModItem::NAME))
803 return item->name(ModItem::SHORT);
804
805 return item->getFieldValue(index.column(),role);
806 }
807 else
808 {
809 return QVariant();
810 }
811 }
812 else
813 return QVariant();
814}
815
816
817QMimeData* ModItemsTree::mimeData(const QModelIndexList &indexes) const
818{
819 QMimeData *mimeData = new QMimeData();
820 QString csv;
821
822 // select only first column indexes (since selection is made by row)
823 QModelIndexList rowIndexes;
824 QList<ModItem*> items;
825 for(int i=0;i<indexes.size();i++)
826 {
827 if(indexes.at(i).column()==0)
828 {
829 rowIndexes.push_back(indexes.at(i));
830 items.push_back((ModItem*)indexes.at(i).internalPointer());
831 }
832 }
833
834 // Remove children of contained parents -> avoid twice copying
835 QList<ModItem*> uniqueItems;
836 for(int i=0;i<items.size();i++)
837 {
838 // if(!items.contains(items.at(i)->parent()) || (items.at(i)->parent()==_rootElement))
839 uniqueItems.push_back(items.at(i));
840 }
841
842 // create text data
843 for(int i=0;i<uniqueItems.size();i++)
844 {
845 ModItem* item = uniqueItems.at(i);
846 csv.push_back(item->name(ModItem::FULL)+"\n");
847 }
848 if(csv.size()>0)
849 csv.remove(csv.size()-QString("\n").size(),QString("\n").size());
850 mimeData->setText(csv);
851 mimeData->setData("application/ModItemName",csv.toAsciitoLatin1());
852 return mimeData;
853}
854
855
856Qt::ItemFlags ModItemsTree::flags(const QModelIndex &index) const
857{
858 if(!_enabled)
859 return Qt::NoItemFlags;
860
861 if (!index.isValid())
862 return Qt::ItemIsEnabled;
863
864 return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled;
865
866}
867
868QVariant ModItemsTree::headerData(int section, Qt::Orientation orientation,
869 int role) const
870{
871 if(!_enabled)
872 return QVariant();
873
874 if(section<0 || section>=ModItem::nbFields)
875 return QVariant();
876
877 if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
878 return ModItem::sFieldName(section,role);
879
880 return QVariant();
881}
882
883
884QModelIndex ModItemsTree::index(int row, int column, const QModelIndex &parent)
885const
886{
887 if(!_enabled)
888 return QModelIndex();
889
890 if(!hasIndex(row,column,parent))
891 return QModelIndex();
892
893 ModItem *parentComponent;
894
895 if (!parent.isValid())
896 parentComponent = _rootElement;
897 else
898 parentComponent = static_cast<ModItem*>(parent.internalPointer());
899
900 // looking in children
901 // int nbPacks = parentComponent->packageChildCount();
902 // int nbModels = parentComponent->modelChildCount();
903 // int nbComps = parentComponent->compChildCount();
904
905 if(row<0 || row>= parentComponent->childCount())
906 return QModelIndex();
907
908 ModItem *childElement = parentComponent->child(row);
909
910 if (!childElement)
911 return QModelIndex();
912
913 if(!_showComponents&&(childElement->getClassRestr()==Modelica::COMPONENT))
914 return QModelIndex();
915 else
916 return createIndex(row, column, childElement);
917}
918
919QModelIndex ModItemsTree::indexOf(ModItem* item,int column)
920{
921 if(item==_rootElement)
922 return createIndex(0,column,item);
923 else
924 return createIndex(item->indexInParent(),column,item);
925}
926
927QModelIndex ModItemsTree::parent(const QModelIndex &index) const
928{
929 if(!_enabled)
930 return QModelIndex();
931
932 if (!index.isValid())
933 return QModelIndex();
934
935 ModItem *childElement = static_cast<ModItem*>(index.internalPointer());
936 if (childElement == _rootElement)
937 return QModelIndex();
938
939 ModItem *parentElement = NULL__null;
940 parentElement = childElement->parentItem();
941
942 if (parentElement == _rootElement)
943 return QModelIndex();
944
945 ModItem *grandParentElement = NULL__null;
946 grandParentElement = parentElement->parentItem();
947
948 int iC = grandParentElement->children().indexOf(parentElement);
949
950 if(iC==-1)
951 {
952 // ERROR
953 return QModelIndex();
954 }
955
956 return createIndex(iC, 0, parentElement);
957}
958bool ModItemsTree::hasChildren ( const QModelIndex & parent ) const
959{
960 bool hasChildren;
961 bool triedToFind;
962
963 if(!_enabled)
964 return false;
965
966 ModItem *parentElement;
967
968 if (parent.column() > 0)
969 return false;
970
971 if (!parent.isValid())
972 parentElement = rootElement();
973 else
974 parentElement = static_cast<ModItem*>(parent.internalPointer());
975
976 if(parentElement->childrenReaden())
977 {
978 if(_showComponents)
979 return (parentElement->childCount()>0);
980 else
981 return (parentElement->childCount()-parentElement->compChildCount()>0);
982 }
983 else
984 {
985 triedToFind = true;
986 hasChildren = false;
987
988 QStringList children = _moomc->getClassNames(parentElement->name());
989 children.removeAll(QString());
990 if(!children.isEmpty())
991 hasChildren = true;
992 else if(parentElement->getClassRestr()==Modelica::MODEL)
993 {
994 if(!_showComponents)
995 {
996 hasChildren = false;
997 triedToFind = false;
998 }
999 else
1000 {
1001 // look if has component children
1002 QStringList compNames;
1003 QStringList compClasses;
1004 _moomc->getContainedComponents(parentElement->getModClassName(),compNames,compClasses,true);
1005 hasChildren = (!compNames.isEmpty());
1006 }
1007 }
1008 if(triedToFind && !hasChildren)
1009 parentElement->setChildrenReaden(true); // avoid re-reading
1010
1011 return hasChildren;
1012 }
1013}
1014int ModItemsTree::rowCount(const QModelIndex &parent) const
1015{
1016 if(!_enabled)
1017 return 0;
1018
1019 ModItem *parentElement;
1020
1021 if (parent.column() > 0)
1022 return 0;
1023
1024 if (!parent.isValid())
1025 parentElement = _rootElement;
1026 else
1027 parentElement = static_cast<ModItem*>(parent.internalPointer());
1028
1029 int count = parentElement->childCount();
1030
1031 if(!_showComponents)
1032 count-=parentElement->compChildCount();
1033
1034 return count;
1035}
1036
1037/**
1038* \brief
1039* Return first ModModel in item parents (including himself).
1040* This function can for instance be used to know which model compile
1041* in order to update item value.
1042* @param itemName : full name of item whom who are looking parent model
1043*/
1044ModItem *ModItemsTree::modelOf(QString itemName)
1045{
1046 ModItem* item = findInDescendants(itemName);
1047 return modelOf(item);
1048}
1049/**
1050* \brief
1051* Return first ModModel in item parents (including himself).
1052* This function can for instance be used to know which model compile
1053* in order to update item value.
1054*/
1055ModModel* ModItemsTree::modelOf(ModItem* item)
1056{
1057 if(item==NULL__null)
1058 return NULL__null;
1059
1060 if(item->getClassRestr()==Modelica::MODEL)
1061 return dynamic_cast<ModModel*>(item);
1062 else
1063 return dynamic_cast<ModModel*>(modelOf(item->parentItem()));
1064}
1065
1066
1067ModItem* ModItemsTree::findItem(QString fullName)
1068{
1069 return findInDescendants(fullName,_rootElement);
1
Calling 'ModItemsTree::findInDescendants'
1070}
1071
1072
1073
1074void ModItemsTree::childrenInfos(ModItem* parent,QStringList &packagesClasses,QStringList &modelsClasses,QStringList &recordsClasses,QStringList &compsNames,QStringList &compsClasses)
1075{
1076 // read children if necessary
1077 if(!parent->childrenReaden())
1078 {
1079 int curDepth = parent->depth();
1080 readFromOmcV2(parent,curDepth+1,QString(),curDepth);
1081 }
1082
1083 int nbCompChild = parent->compChildCount();
1084 int nbModelChild = parent->modelChildCount();
1085 int nbPackageChild = parent->packageChildCount();
1086 int nbRecordChild = parent->recordChildCount();
1087
1088 //clear
1089 packagesClasses.clear();
1090 modelsClasses.clear();
1091 compsClasses.clear();
1092 compsNames.clear();
1093 recordsClasses.clear();
1094
1095 // packages
1096 for(int i=0;i<nbPackageChild;i++)
1097 packagesClasses.push_back(parent->packageChild(i)->name(ModItem::SHORT));
1098
1099 // models
1100 for(int i=0;i<nbModelChild;i++)
1101 modelsClasses.push_back(parent->modelChild(i)->name(ModItem::SHORT));
1102
1103 // records
1104 for(int i=0;i<nbRecordChild;i++)
1105 recordsClasses.push_back(parent->modelChild(i)->name(ModItem::SHORT));
1106
1107 // components
1108 for(int i=0;i<nbCompChild;i++)
1109 {
1110 compsClasses.push_back(parent->compChild(i)->getFieldValue(ModComponent::CLASSNAME).toString());
1111 compsNames.push_back(parent->compChild(i)->name(ModItem::SHORT));
1112 }
1113}
1114
1115
1116// get all ports of component parent (not only those connected)
1117QList<ModItem*> ModItemsTree::getPorts(ModItem* parent)
1118{
1119 QList<ModItem*> ports;
1120
1121 int curDepth = parent->depth();
1122 if(!parent->childrenReaden())
1123 {
1124 readFromOmc(parent,curDepth+1,QString(),curDepth);
1125 }
1126
1127 int nbCompChild = parent->compChildCount();
1128 for(int i=0; i<nbCompChild;i++)
1129 {
1130 ModComponent* curComp = (ModComponent*)parent->compChild(i);
1131 if(_moomc->isConnector(curComp->getFieldValue(ModComponent::CLASSNAME).toString()))
1132 ports.push_back(curComp);
1133 }
1134 return ports;
1135}
1136
1137QStringList ModItemsTree::getPorts(ModItem* parent,ModItem::NameFormat format)
1138{
1139 QList<ModItem*> _ports = getPorts(parent);
1140
1141 QStringList _portsNames;
1142 for(int i=0;i<_ports.size();i++)
1143 _portsNames.push_back(_ports.at(i)->name(format));
1144
1145 return _portsNames;
1146}
1147
1148
1149QIcon ModItemsTree::getModelicaNodeIcon(ModItem* ModItem) const
1150{
1151 switch (ModItem->getClassRestr())
1152 {
1153 case Modelica::MODEL:
1154 return QIcon(":/icons/model-icon.png");
1155 case Modelica::CLASS:
1156 return QIcon(":/icons/class-icon.png");
1157 case Modelica::CONNECTOR:
1158 return QIcon(":/icons/connector-icon.png");
1159 case Modelica::RECORD:
1160 return QIcon(":/icons/record-icon.png");
1161 case Modelica::BLOCK:
1162 return QIcon(":/icons/block-icon.png");
1163 case Modelica::FUNCTION:
1164 return QIcon(":/icons/function-icon.png");
1165 case Modelica::PACKAGE:
1166 return QIcon(":/icons/package-icon.png");
1167 case Modelica::TYPE:
1168 return QIcon(":/icons/type-icon.png");
1169 case Modelica::EXECUTABLE:
1170 return QIcon(":/icons/exe-icon.png");
1171 default :
1172 return QIcon();
1173 }
1174}
1175
1176bool ModItemsTree::canFetchMore ( const QModelIndex & parent ) const
1177{
1178 ModItem *item = static_cast<ModItem*>(parent.internalPointer());
1179
1180 if(!_enabled)
1181 return false;
1182
1183 if (!parent.isValid())
1184 return false;
1185
1186 if(parent.column()<0 || parent.column()>=ModItem::nbFields)
1187 return false;
1188
1189 // ModItem *item = static_cast<ModItem*>(parent.internalPointer());
1190
1191 if(item && !item->childrenReaden())
1192 return true;
1193 else
1194 return false;
1195}
1196
1197void ModItemsTree::fetchMore ( const QModelIndex & parent )
1198{
1199 if(!_enabled)
1200 return;
1201
1202 if (!parent.isValid())
1203 return;
1204
1205 ModItem *item = static_cast<ModItem*>(parent.internalPointer());
1206
1207 if(item)
1208 {
1209 if(!item->childrenReaden())
1210 readFromOMCWThread(item,_modLoader->getDepthMax());
1211 }
1212}
1213
1214void ModItemsTree::setShowComponent(bool showComponents)
1215{
1216 if(showComponents!=_showComponents)
1217 {
1218 beginResetModel();
1219 _showComponents = showComponents;
1220 endResetModel();
1221 }
1222}
1223
1224//Qt::DropActions ModItemsTree::supportedDropActions() const
1225//{
1226// return Qt::CopyAction | Qt::MoveAction;
1227//}
1228
1229
1230
1231ModItemsLoader::ModItemsLoader(ModItemsTree* modItemsTree,ModItem* parent,int depthMax, QString direction , int curDepth )
1232{
1233 _modItemsTree = modItemsTree;
1234 _parent = parent;
1235 _depthMax = depthMax;
1236 _direction = direction;
1237 _curDepth = curDepth;
1238
1239
1240 connect(this, SIGNAL(readFromOmc(ModItem*,int,QString,int))"2""readFromOmc(ModItem*,int,QString,int)",
1241 _modItemsTree, SLOT(readFromOmc(ModItem*,int, QString,int))"1""readFromOmc(ModItem*,int, QString,int)");
1242 // connect(this, SIGNAL(started()), this, SLOT(hasStarted()));
1243 // connect(this, SIGNAL(finished()), this, SLOT(hasFinished()));
1244}
1245
1246
1247
1248void ModItemsLoader::run()
1249{
1250 emit readFromOmc(_parent,_depthMax,_direction,_curDepth);
1251}