Salome HOME
Copyright update 2022
[modules/shaper.git] / src / PartSet / PartSet_IconFactory.cpp
1 // Copyright (C) 2014-2022  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 email : webmaster.salome@opencascade.com
18 //
19
20 #include "PartSet_IconFactory.h"
21 #include "PartSet_Tools.h"
22 #include "PartSet_Module.h"
23 #include "XGUI_DataModel.h"
24
25 #include <XGUI_Workshop.h>
26 #include <XGUI_ObjectsBrowser.h>
27
28 #include <ModuleBase_ActionInfo.h>
29 #include <ModuleBase_Tools.h>
30
31 #include <ModelAPI_ResultGroup.h>
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 QMap<QString, QString> PartSet_IconFactory::myIcons;
40
41 PartSet_IconFactory::PartSet_IconFactory(PartSet_Module* theModule)
42   : ModuleBase_IconFactory(), myModule(theModule)
43 {
44   Events_Loop::loop()->registerListener(this,
45     Events_Loop::eventByName(Config_FeatureMessage::GUI_EVENT()));
46 }
47
48
49 QIcon PartSet_IconFactory::getIcon(ObjectPtr theObj)
50 {
51   if (!theObj.get())
52     return QIcon();
53
54   std::string aGroup = theObj->groupName();
55   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
56   if (aFeature.get()) {
57     std::string aKind = aFeature->getKind();
58     QString aId(aKind.c_str());
59     if (!myIcons.contains(aId))
60       return QIcon();
61
62     QString anIconString = myIcons[aId];
63
64     ModelAPI_ExecState aState = aFeature->data()->execState();
65     switch (aState) {
66     case ModelAPI_StateMustBeUpdated:
67       return ModuleBase_Tools::composite(":icons/toWork.png", anIconString);
68       //anIcon = ModuleBase_Tools::lighter(anIconString);
69       break;
70     case ModelAPI_StateExecFailed:
71       return ModuleBase_Tools::composite(":icons/isFailed.png", anIconString);
72       break;
73     case ModelAPI_StateInvalidArgument:
74       return ModuleBase_Tools::composite(":icons/exec_state_invalid_parameters.png",
75         anIconString);
76       break;
77     default:
78       return loadIcon(anIconString);
79       break;
80     }
81   }
82
83   //if (theObj->data() && theObj->data()->execState() == ModelAPI_StateMustBeUpdated)
84   //  return QIcon(":pictures/constr_object_modified.png");
85
86   if (aGroup == ModelAPI_ResultPart::group())
87     return QIcon(":pictures/part_ico.png");
88
89   if (aGroup == ModelAPI_ResultConstruction::group())
90     return QIcon(":pictures/constr_object.png");
91
92   if (aGroup == ModelAPI_Folder::group()) {
93     static QString anIconString(":pictures/features_folder.png");
94     int aFirst = -1, aLast = -1;
95     PartSet_Tools::getFirstAndLastIndexInFolder(theObj, aFirst, aLast);
96     if ((aFirst != -1) && (aLast != -1)) {
97       int aNbItems = aLast - aFirst + 1;
98       if (aNbItems) {
99         XGUI_ObjectsBrowser* aObBrowser = myModule->getWorkshop()->objectBrowser();
100         XGUI_DataTree* aTree = aObBrowser->treeView();
101         QModelIndex aIndex = aTree->dataModel()->objectIndex(theObj, 0);
102         if (!aTree->isExpanded(aIndex)) {
103           DocumentPtr aDoc = theObj->document();
104           ObjectPtr aSubObj;
105           ModelAPI_ExecState aState;
106           bool aHasWarning = false;
107           for (int i = aFirst; i < aLast + 1; i++) {
108             aSubObj = aDoc->object(ModelAPI_Feature::group(), i);
109             aState = aSubObj->data()->execState();
110             if ((aState == ModelAPI_StateExecFailed) || (aState == ModelAPI_StateMustBeUpdated)) {
111               aHasWarning = true;
112               break;
113             }
114           }
115           if (aHasWarning) {
116             return QIcon(ModuleBase_Tools::composite(":icons/hasWarning.png", anIconString));
117           }
118         }
119       }
120     }
121     return loadIcon(anIconString);
122   }
123
124   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObj);
125   if (aResult.get()) {
126     GeomShapePtr aShape = aResult->shape();
127     if(aShape.get()) {
128       switch(aShape->shapeType()) {
129         case GeomAPI_Shape::COMPOUND: {
130           if (aResult->groupName() == ModelAPI_ResultGroup::group()) {
131             switch (aShape->typeOfCompoundShapes()) {
132             case GeomAPI_Shape::VERTEX:
133               return QIcon(":icons/group_vertex.png");
134             case GeomAPI_Shape::EDGE:
135               return QIcon(":icons/group_edge.png");
136             case GeomAPI_Shape::FACE:
137               return QIcon(":icons/group_face.png");
138             case GeomAPI_Shape::SOLID:
139               return QIcon(":icons/group_solid.png");
140             default: // [to avoid compilation warning]
141               break;
142             }
143           }
144           ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aResult);
145           if (aBody.get() && aBody->isConnectedTopology())
146             return QIcon(":pictures/compoundofsolids.png");
147           return QIcon(":pictures/compound.png");
148         }
149         case GeomAPI_Shape::COMPSOLID: return QIcon(":pictures/compsolid.png");
150         case GeomAPI_Shape::SOLID:     return QIcon(":pictures/solid.png");
151         case GeomAPI_Shape::SHELL:     return QIcon(":pictures/shell.png");
152         case GeomAPI_Shape::FACE:      return QIcon(":pictures/face.png");
153         case GeomAPI_Shape::WIRE:      return QIcon(":pictures/wire.png");
154         case GeomAPI_Shape::EDGE:      return QIcon(":pictures/edge.png");
155         case GeomAPI_Shape::VERTEX:    return QIcon(":pictures/vertex.png");
156         default: // [to avoid compilation warning]
157           break;
158       }
159     }
160   }
161   return QIcon();
162 }
163
164 void PartSet_IconFactory::processEvent(const std::shared_ptr<Events_Message>& theMessage)
165 {
166   if (theMessage->eventID() ==
167       Events_Loop::loop()->eventByName(Config_FeatureMessage::GUI_EVENT())) {
168     std::shared_ptr<Config_FeatureMessage> aFeatureMsg =
169        std::dynamic_pointer_cast<Config_FeatureMessage>(theMessage);
170     ActionInfo aFeatureInfo;
171     aFeatureInfo.initFrom(aFeatureMsg);
172     // Remember features icons
173     myIcons[QString::fromStdString(aFeatureMsg->id())] = aFeatureInfo.iconFile;
174   }
175 }