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