]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_IconFactory.cpp
Salome HOME
2b811848733c1bbe2b93af5829b7aa8ef43762e4
[modules/shaper.git] / src / PartSet / PartSet_IconFactory.cpp
1 // Copyright (C) 2014-2019  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_ResultPart.h>
32 #include <ModelAPI_ResultConstruction.h>
33 #include <ModelAPI_ResultBody.h>
34
35 #include <Config_FeatureMessage.h>
36 #include <Events_Loop.h>
37
38 #include <CollectionPlugin_Group.h>
39
40 QMap<QString, QString> PartSet_IconFactory::myIcons;
41
42 PartSet_IconFactory::PartSet_IconFactory(PartSet_Module* theModule)
43   : ModuleBase_IconFactory(), myModule(theModule)
44 {
45   Events_Loop::loop()->registerListener(this,
46     Events_Loop::eventByName(Config_FeatureMessage::GUI_EVENT()));
47 }
48
49
50 QIcon PartSet_IconFactory::getIcon(ObjectPtr theObj)
51 {
52   if (!theObj.get())
53     return QIcon();
54
55   std::string aGroup = theObj->groupName();
56   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
57   if (aFeature.get()) {
58     std::string aKind = aFeature->getKind();
59     QString aId(aKind.c_str());
60     if (!myIcons.contains(aId))
61       return QIcon();
62
63     QString anIconString = myIcons[aId];
64
65     ModelAPI_ExecState aState = aFeature->data()->execState();
66     switch (aState) {
67     case ModelAPI_StateMustBeUpdated:
68       return ModuleBase_Tools::composite(":icons/toWork.png", anIconString);
69       //anIcon = ModuleBase_Tools::lighter(anIconString);
70       break;
71     case ModelAPI_StateExecFailed:
72       return ModuleBase_Tools::composite(":icons/isFailed.png", anIconString);
73       break;
74     case ModelAPI_StateInvalidArgument:
75       return ModuleBase_Tools::composite(":icons/exec_state_invalid_parameters.png",
76         anIconString);
77       break;
78     default:
79       return loadIcon(anIconString);
80       break;
81     }
82   }
83
84   //if (theObj->data() && theObj->data()->execState() == ModelAPI_StateMustBeUpdated)
85   //  return QIcon(":pictures/constr_object_modified.png");
86
87   if (aGroup == ModelAPI_ResultPart::group())
88     return QIcon(":pictures/part_ico.png");
89
90   if (aGroup == ModelAPI_ResultConstruction::group())
91     return QIcon(":pictures/constr_object.png");
92
93   if (aGroup == ModelAPI_Folder::group()) {
94     static QString anIconString(":pictures/features_folder.png");
95     int aFirst = -1, aLast = -1;
96     PartSet_Tools::getFirstAndLastIndexInFolder(theObj, aFirst, aLast);
97     if ((aFirst != -1) && (aLast != -1)) {
98       int aNbItems = aLast - aFirst + 1;
99       if (aNbItems) {
100         XGUI_ObjectsBrowser* aObBrowser = myModule->getWorkshop()->objectBrowser();
101         XGUI_DataTree* aTree = aObBrowser->treeView();
102         QModelIndex aIndex = aTree->dataModel()->objectIndex(theObj, 0);
103         if (!aTree->isExpanded(aIndex)) {
104           DocumentPtr aDoc = theObj->document();
105           ObjectPtr aSubObj;
106           ModelAPI_ExecState aState;
107           bool aHasWarning = false;
108           for (int i = aFirst; i < aLast + 1; i++) {
109             aSubObj = aDoc->object(ModelAPI_Feature::group(), i);
110             aState = aSubObj->data()->execState();
111             if ((aState == ModelAPI_StateExecFailed) || (aState == ModelAPI_StateMustBeUpdated)) {
112               aHasWarning = true;
113               break;
114             }
115           }
116           if (aHasWarning) {
117             return QIcon(ModuleBase_Tools::composite(":icons/hasWarning.png", anIconString));
118           }
119         }
120       }
121     }
122     return loadIcon(anIconString);
123   }
124
125   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObj);
126   if (aResult.get()) {
127     GeomShapePtr aShape = aResult->shape();
128     if(aShape.get()) {
129       switch(aShape->shapeType()) {
130         case GeomAPI_Shape::COMPOUND: {
131           FeaturePtr aFeature = ModelAPI_Feature::feature(theObj);
132           if (aFeature.get() && aFeature->getKind() == CollectionPlugin_Group::ID()) {
133             switch (aShape->typeOfCompoundShapes()) {
134             case GeomAPI_Shape::VERTEX:
135               return QIcon(":icons/group_vertex.png");
136             case GeomAPI_Shape::EDGE:
137               return QIcon(":icons/group_edge.png");
138             case GeomAPI_Shape::FACE:
139               return QIcon(":icons/group_face.png");
140             case GeomAPI_Shape::SOLID:
141               return QIcon(":icons/group_solid.png");
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       }
157     }
158   }
159   return QIcon();
160 }
161
162 void PartSet_IconFactory::processEvent(const std::shared_ptr<Events_Message>& theMessage)
163 {
164   if (theMessage->eventID() ==
165       Events_Loop::loop()->eventByName(Config_FeatureMessage::GUI_EVENT())) {
166     std::shared_ptr<Config_FeatureMessage> aFeatureMsg =
167        std::dynamic_pointer_cast<Config_FeatureMessage>(theMessage);
168     if (!aFeatureMsg->isInternal()) {
169       ActionInfo aFeatureInfo;
170       aFeatureInfo.initFrom(aFeatureMsg);
171       // Remember features icons
172       myIcons[QString::fromStdString(aFeatureMsg->id())] = aFeatureInfo.iconFile;
173     }
174   }
175 }