Salome HOME
Fix for the issue #2753 : error when dump/load script
[modules/shaper.git] / src / PartSet / PartSet_IconFactory.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "PartSet_IconFactory.h"
22 #include "PartSet_Tools.h"
23 #include "PartSet_Module.h"
24 #include "XGUI_DataModel.h"
25
26 #include <XGUI_Workshop.h>
27 #include <XGUI_ObjectsBrowser.h>
28
29 #include <ModuleBase_ActionInfo.h>
30 #include <ModuleBase_Tools.h>
31
32 #include <ModelAPI_ResultPart.h>
33 #include <ModelAPI_ResultConstruction.h>
34 #include <ModelAPI_ResultBody.h>
35
36 #include <Config_FeatureMessage.h>
37 #include <Events_Loop.h>
38
39 #include <CollectionPlugin_Group.h>
40
41 QMap<QString, QString> PartSet_IconFactory::myIcons;
42
43 PartSet_IconFactory::PartSet_IconFactory(PartSet_Module* theModule)
44   : ModuleBase_IconFactory(), myModule(theModule)
45 {
46   Events_Loop::loop()->registerListener(this,
47     Events_Loop::eventByName(Config_FeatureMessage::GUI_EVENT()));
48 }
49
50
51 QIcon PartSet_IconFactory::getIcon(ObjectPtr theObj)
52 {
53   if (!theObj.get())
54     return QIcon();
55
56   std::string aGroup = theObj->groupName();
57   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
58   if (aFeature.get()) {
59     std::string aKind = aFeature->getKind();
60     QString aId(aKind.c_str());
61     if (!myIcons.contains(aId))
62       return QIcon();
63
64     QString anIconString = myIcons[aId];
65
66     ModelAPI_ExecState aState = aFeature->data()->execState();
67     switch (aState) {
68     case ModelAPI_StateMustBeUpdated:
69       return ModuleBase_Tools::composite(":icons/toWork.png", anIconString);
70       //anIcon = ModuleBase_Tools::lighter(anIconString);
71       break;
72     case ModelAPI_StateExecFailed:
73       return ModuleBase_Tools::composite(":icons/isFailed.png", anIconString);
74       break;
75     case ModelAPI_StateInvalidArgument:
76       return ModuleBase_Tools::composite(":icons/exec_state_invalid_parameters.png",
77         anIconString);
78       break;
79     default:
80       return loadIcon(anIconString);
81       break;
82     }
83   }
84
85   //if (theObj->data() && theObj->data()->execState() == ModelAPI_StateMustBeUpdated)
86   //  return QIcon(":pictures/constr_object_modified.png");
87
88   if (aGroup == ModelAPI_ResultPart::group())
89     return QIcon(":pictures/part_ico.png");
90
91   if (aGroup == ModelAPI_ResultConstruction::group())
92     return QIcon(":pictures/constr_object.png");
93
94   if (aGroup == ModelAPI_Folder::group()) {
95     static QString anIconString(":pictures/features_folder.png");
96     int aFirst = -1, aLast = -1;
97     PartSet_Tools::getFirstAndLastIndexInFolder(theObj, aFirst, aLast);
98     if ((aFirst != -1) && (aLast != -1)) {
99       int aNbItems = aLast - aFirst + 1;
100       if (aNbItems) {
101         XGUI_ObjectsBrowser* aObBrowser = myModule->getWorkshop()->objectBrowser();
102         XGUI_DataTree* aTree = aObBrowser->treeView();
103         QModelIndex aIndex = aTree->dataModel()->objectIndex(theObj, 0);
104         if (!aTree->isExpanded(aIndex)) {
105           DocumentPtr aDoc = theObj->document();
106           ObjectPtr aSubObj;
107           ModelAPI_ExecState aState;
108           bool aHasWarning = false;
109           for (int i = aFirst; i < aLast + 1; i++) {
110             aSubObj = aDoc->object(ModelAPI_Feature::group(), i);
111             aState = aSubObj->data()->execState();
112             if ((aState == ModelAPI_StateExecFailed) || (aState == ModelAPI_StateMustBeUpdated)) {
113               aHasWarning = true;
114               break;
115             }
116           }
117           if (aHasWarning) {
118             return QIcon(ModuleBase_Tools::composite(":icons/hasWarning.png", anIconString));
119           }
120         }
121       }
122     }
123     return loadIcon(anIconString);
124   }
125
126   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObj);
127   if (aResult.get()) {
128     GeomShapePtr aShape = aResult->shape();
129     if(aShape.get()) {
130       switch(aShape->shapeType()) {
131         case GeomAPI_Shape::COMPOUND: {
132           FeaturePtr aFeature = ModelAPI_Feature::feature(theObj);
133           if (aFeature.get() && aFeature->getKind() == CollectionPlugin_Group::ID()) {
134             switch (aShape->typeOfCompoundShapes()) {
135             case GeomAPI_Shape::VERTEX:
136               return QIcon(":icons/group_vertex.png");
137             case GeomAPI_Shape::EDGE:
138               return QIcon(":icons/group_edge.png");
139             case GeomAPI_Shape::FACE:
140               return QIcon(":icons/group_face.png");
141             case GeomAPI_Shape::SOLID:
142               return QIcon(":icons/group_solid.png");
143             }
144           }
145           ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aResult);
146           if (aBody.get() && aBody->isConnectedTopology())
147             return QIcon(":pictures/compoundofsolids.png");
148           return QIcon(":pictures/compound.png");
149         }
150         case GeomAPI_Shape::COMPSOLID: return QIcon(":pictures/compsolid.png");
151         case GeomAPI_Shape::SOLID:     return QIcon(":pictures/solid.png");
152         case GeomAPI_Shape::SHELL:     return QIcon(":pictures/shell.png");
153         case GeomAPI_Shape::FACE:      return QIcon(":pictures/face.png");
154         case GeomAPI_Shape::WIRE:      return QIcon(":pictures/wire.png");
155         case GeomAPI_Shape::EDGE:      return QIcon(":pictures/edge.png");
156         case GeomAPI_Shape::VERTEX:    return QIcon(":pictures/vertex.png");
157       }
158     }
159   }
160   return QIcon();
161 }
162
163 void PartSet_IconFactory::processEvent(const std::shared_ptr<Events_Message>& theMessage)
164 {
165   if (theMessage->eventID() ==
166       Events_Loop::loop()->eventByName(Config_FeatureMessage::GUI_EVENT())) {
167     std::shared_ptr<Config_FeatureMessage> aFeatureMsg =
168        std::dynamic_pointer_cast<Config_FeatureMessage>(theMessage);
169     if (!aFeatureMsg->isInternal()) {
170       ActionInfo aFeatureInfo;
171       aFeatureInfo.initFrom(aFeatureMsg);
172       // Remember features icons
173       myIcons[QString::fromStdString(aFeatureMsg->id())] = aFeatureInfo.iconFile;
174     }
175   }
176 }