Salome HOME
Add copyright header according to request of CEA from 06.06.2017
[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 <ModuleBase_ActionInfo.h>
23 #include <ModuleBase_Tools.h>
24
25 #include <ModelAPI_ResultPart.h>
26 #include <ModelAPI_ResultConstruction.h>
27 #include <ModelAPI_ResultBody.h>
28
29 #include <Config_FeatureMessage.h>
30 #include <Events_Loop.h>
31
32 QMap<QString, QString> PartSet_IconFactory::myIcons;
33
34 PartSet_IconFactory::PartSet_IconFactory():ModuleBase_IconFactory()
35 {
36   Events_Loop::loop()->registerListener(this,
37     Events_Loop::eventByName(Config_FeatureMessage::GUI_EVENT()));
38 }
39
40
41 QIcon PartSet_IconFactory::getIcon(ObjectPtr theObj)
42 {
43   QIcon anIcon;
44
45   if (!theObj.get())
46     return anIcon;
47
48   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
49   if (aFeature.get()) {
50     std::string aKind = aFeature->getKind();
51     QString aId(aKind.c_str());
52     if (!myIcons.contains(aId))
53       return anIcon;
54
55     QString anIconString = myIcons[aId];
56
57     ModelAPI_ExecState aState = aFeature->data()->execState();
58     switch(aState) {
59       case ModelAPI_StateDone:
60       case ModelAPI_StateNothing: {
61         anIcon = loadIcon(anIconString);
62       }
63       break;
64       case ModelAPI_StateMustBeUpdated: {
65         anIcon = ModuleBase_Tools::lighter(anIconString);
66       }
67       break;
68       case ModelAPI_StateExecFailed: {
69         anIcon = ModuleBase_Tools::composite(":icons/exec_state_failed.png", anIconString);
70       }
71       break;
72       case ModelAPI_StateInvalidArgument: {
73         anIcon = ModuleBase_Tools::composite(":icons/exec_state_invalid_parameters.png",
74                                              anIconString);
75       }
76       break;
77       default: break;
78     }
79   }
80
81   if (theObj->data() && theObj->data()->execState() == ModelAPI_StateMustBeUpdated)
82     return QIcon(":pictures/constr_object_modified.png");
83
84   std::string aGroup = theObj->groupName();
85   if (aGroup == ModelAPI_ResultPart::group())
86     return QIcon(":pictures/part_ico.png");
87
88   if (aGroup == ModelAPI_ResultConstruction::group())
89     return QIcon(":pictures/constr_object.png");
90
91   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObj);
92   if (aResult.get()) {
93     GeomShapePtr aShape = aResult->shape();
94     if(aShape.get()) {
95       switch(aShape->shapeType()) {
96         case GeomAPI_Shape::COMPOUND: {
97           ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aResult);
98           if (aBody.get() && aBody->isConnectedTopology())
99             return QIcon(":pictures/compoundofsolids.png");
100           return QIcon(":pictures/compound.png");
101         }
102         case GeomAPI_Shape::COMPSOLID: return QIcon(":pictures/compsolid.png");
103         case GeomAPI_Shape::SOLID:     return QIcon(":pictures/solid.png");
104         case GeomAPI_Shape::SHELL:     return QIcon(":pictures/shell.png");
105         case GeomAPI_Shape::FACE:      return QIcon(":pictures/face.png");
106         case GeomAPI_Shape::WIRE:      return QIcon(":pictures/wire.png");
107         case GeomAPI_Shape::EDGE:      return QIcon(":pictures/edge.png");
108         case GeomAPI_Shape::VERTEX:    return QIcon(":pictures/vertex.png");
109       }
110     }
111   }
112   return anIcon;
113 }
114
115 void PartSet_IconFactory::processEvent(const std::shared_ptr<Events_Message>& theMessage)
116 {
117   if (theMessage->eventID() ==
118       Events_Loop::loop()->eventByName(Config_FeatureMessage::GUI_EVENT())) {
119     std::shared_ptr<Config_FeatureMessage> aFeatureMsg =
120        std::dynamic_pointer_cast<Config_FeatureMessage>(theMessage);
121     if (!aFeatureMsg->isInternal()) {
122       ActionInfo aFeatureInfo;
123       aFeatureInfo.initFrom(aFeatureMsg);
124       // Remember features icons
125       myIcons[QString::fromStdString(aFeatureMsg->id())] = aFeatureInfo.iconFile;
126     }
127   }
128 }