Salome HOME
eee2a5737e42b9d2a478b9129f756ed23fc061e0
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetConcealedObjects.cpp
1 // Copyright (C) 2014-2023  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 <ModuleBase_WidgetConcealedObjects.h>
21 #include <ModuleBase_Tools.h>
22
23 #include <ModelAPI_Result.h>
24 #include <ModelAPI_ResultBody.h>
25 #include <ModelAPI_AttributeReference.h>
26 #include <ModelAPI_AttributeRefList.h>
27 #include <ModelAPI_Session.h>
28 #include <ModelAPI_Validator.h>
29 #include <ModelAPI_Tools.h>
30
31 #include <Config_WidgetAPI.h>
32
33 #include <QGridLayout>
34
35 #include <QWidget>
36 #include <QTableWidget>
37 #include <QHeaderView>
38 #include <QCheckBox>
39 #include <QVBoxLayout>
40
41 #include <algorithm>
42
43 const int DEFAULT_NAME_COLUMN_WIDTH = 200;
44
45 ModuleBase_WidgetConcealedObjects::ModuleBase_WidgetConcealedObjects(QWidget* theParent,
46                                                      const Config_WidgetAPI* theData)
47 : ModuleBase_ModelWidget(theParent, theData)
48 {
49   myBaseShapeAttribute = theData->getProperty("base_shape_attribute");
50   std::string aPickParents = theData->getProperty("pick_concealed_parents");
51   std::transform(aPickParents.begin(), aPickParents.end(), aPickParents.begin(), ::tolower);
52   myPickConcealedParents = aPickParents == "1" || aPickParents == "true" || aPickParents == "yes";
53
54   QGridLayout* aMainLay = new QGridLayout(this);
55   ModuleBase_Tools::adjustMargins(aMainLay);
56
57   myView = new QTableWidget(this);
58   aMainLay->addWidget(myView);
59
60   myView->setColumnCount(2);
61   myView->horizontalHeader()->setVisible(false);
62   myView->verticalHeader()->setVisible(false);
63 }
64
65 ModuleBase_WidgetConcealedObjects::~ModuleBase_WidgetConcealedObjects()
66 {
67 }
68
69 bool ModuleBase_WidgetConcealedObjects::storeValueCustom()
70 {
71   if(!myFeature)
72     return false;
73   DataPtr aData = myFeature->data();
74   AttributeRefListPtr anAttributeList = aData->reflist(attributeID());
75   anAttributeList->clear();
76   for (int i = 0, aSize = myView->rowCount(); i < aSize; i++) {
77     QCheckBox* aButton =
78         dynamic_cast<QCheckBox*>(myView->cellWidget(i, 0)->findChild<QCheckBox*>());
79     if (aButton->isChecked())
80       anAttributeList->append(myConcealedResults[i]);
81   }
82   return true;
83 }
84
85 bool ModuleBase_WidgetConcealedObjects::restoreValueCustom()
86 {
87   FeaturePtr aBaseFeature;
88   ObjectPtr anObject;
89   if (myFeature) {
90     anObject = ModuleBase_Tools::getObject(myFeature->attribute(myBaseShapeAttribute));
91     if (anObject.get() != NULL)
92       aBaseFeature = ModelAPI_Feature::feature(anObject);
93   }
94   if (myBaseFeature != aBaseFeature) {
95     myView->setRowCount(0);
96     myConcealedResults.clear();
97     myBaseFeature = aBaseFeature;
98     if (myBaseFeature.get()) {
99       std::list<std::shared_ptr<ModelAPI_Result> > aResults;
100       std::set<ResultBodyPtr> aParents;
101       ModelAPI_Tools::getConcealedResults(myBaseFeature, aResults);
102       std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator anIt = aResults.begin(),
103                                                                    aLast = aResults.end();
104       for (; anIt != aLast; anIt++) {
105         ResultPtr aResult = *anIt;
106         if (myPickConcealedParents) {
107           // pick the parent result of the concealed object
108           ResultBodyPtr aRootParent = ModelAPI_Tools::bodyOwner(aResult, true);
109           if (aRootParent) {
110             if (aParents.find(aRootParent) == aParents.end()) {
111               aResult = aRootParent;
112               aParents.insert(aRootParent);
113             }
114             else // do not add parent compound once again
115               continue;
116           }
117         }
118
119         int aRowId = myView->rowCount();
120         addViewRow(aResult);
121         myConcealedResults[aRowId] = aResult;
122       }
123     }
124   }
125
126   DataPtr aData = myFeature->data();
127   AttributeRefListPtr anAttributeList = aData->reflist(attributeID());
128   for (int i = 0, aSize = myView->rowCount(); i < aSize; i++) {
129     ResultPtr aResult = myConcealedResults[i];
130     QCheckBox* aButton =
131         dynamic_cast<QCheckBox*>(myView->cellWidget(i, 0)->findChild<QCheckBox*>());
132     bool isChecked = anAttributeList->isInList(aResult);
133
134     bool aBlocked = aButton->blockSignals(true);
135     aButton->setChecked(isChecked);
136     aButton->blockSignals(aBlocked);
137   }
138   return true;
139 }
140
141 QList<QWidget*> ModuleBase_WidgetConcealedObjects::getControls() const
142 {
143   QList<QWidget*> result;
144   result << myView;
145   return result;
146 }
147
148 void ModuleBase_WidgetConcealedObjects::addViewRow(
149                                 const std::shared_ptr<ModelAPI_Result>& theResult)
150 {
151   int anId = myView->rowCount();
152   myView->setRowCount(anId+1);
153
154   QWidget* aVisibilityWdg = new QWidget(this);
155   QVBoxLayout* aVisibilityLay = new QVBoxLayout(aVisibilityWdg);
156   aVisibilityLay->setContentsMargins(2, 2, 2, 2);
157   QCheckBox* aVisibilityBtn = new QCheckBox(aVisibilityWdg);
158   aVisibilityLay->addWidget(aVisibilityBtn, 0, Qt::AlignHCenter);
159   connect(aVisibilityBtn, SIGNAL(toggled(bool)), this, SLOT(onItemToggled(bool)));
160   aVisibilityBtn->setChecked(false);
161
162   myView->setCellWidget(anId, 0, aVisibilityWdg);
163   myView->setItem(anId, 1,
164     new QTableWidgetItem(QString::fromStdWString(theResult->data()->name())));
165
166   if (anId == 1) {
167     myView->setColumnWidth(0, myView->verticalHeader()->defaultSectionSize());
168     myView->setColumnWidth(1, DEFAULT_NAME_COLUMN_WIDTH);
169   }
170 }
171
172 void ModuleBase_WidgetConcealedObjects::onItemToggled(bool /*theState*/)
173 {
174   emit valuesChanged();
175   updateObject(myFeature);
176 }
177