Salome HOME
Copyright update 2020
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetConcealedObjects.cpp
1 // Copyright (C) 2014-2020  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   int aSize1 = anAttributeList->size(false);
77   for (int i = 0, aSize = myView->rowCount(); i < aSize; i++) {
78     QCheckBox* aButton =
79         dynamic_cast<QCheckBox*>(myView->cellWidget(i, 0)->findChild<QCheckBox*>());
80     if (aButton->isChecked())
81       anAttributeList->append(myConcealedResults[i]);
82   }
83   int aSize = anAttributeList->size(false);
84   return true;
85 }
86
87 bool ModuleBase_WidgetConcealedObjects::restoreValueCustom()
88 {
89   FeaturePtr aBaseFeature;
90   ObjectPtr anObject;
91   if (myFeature) {
92     anObject = ModuleBase_Tools::getObject(myFeature->attribute(myBaseShapeAttribute));
93     if (anObject.get() != NULL)
94       aBaseFeature = ModelAPI_Feature::feature(anObject);
95   }
96   if (myBaseFeature != aBaseFeature) {
97     myView->setRowCount(0);
98     myConcealedResults.clear();
99     myBaseFeature = aBaseFeature;
100     if (myBaseFeature.get()) {
101       std::list<std::shared_ptr<ModelAPI_Result> > aResults;
102       std::set<ResultBodyPtr> aParents;
103       ModelAPI_Tools::getConcealedResults(myBaseFeature, aResults);
104       std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator anIt = aResults.begin(),
105                                                                    aLast = aResults.end();
106       for (; anIt != aLast; anIt++) {
107         ResultPtr aResult = *anIt;
108         if (myPickConcealedParents) {
109           // pick the parent result of the concealed object
110           ResultBodyPtr aRootParent = ModelAPI_Tools::bodyOwner(aResult, true);
111           if (aRootParent) {
112             if (aParents.find(aRootParent) == aParents.end()) {
113               aResult = aRootParent;
114               aParents.insert(aRootParent);
115             }
116             else // do not add parent compound once again
117               continue;
118           }
119         }
120
121         int aRowId = myView->rowCount();
122         addViewRow(aResult);
123         myConcealedResults[aRowId] = aResult;
124       }
125     }
126   }
127
128   DataPtr aData = myFeature->data();
129   AttributeRefListPtr anAttributeList = aData->reflist(attributeID());
130   int aSize = anAttributeList->size();
131   for (int i = 0, aSize = myView->rowCount(); i < aSize; i++) {
132     ResultPtr aResult = myConcealedResults[i];
133     QCheckBox* aButton =
134         dynamic_cast<QCheckBox*>(myView->cellWidget(i, 0)->findChild<QCheckBox*>());
135     bool isChecked = anAttributeList->isInList(aResult);
136
137     bool aBlocked = aButton->blockSignals(true);
138     aButton->setChecked(isChecked);
139     aButton->blockSignals(aBlocked);
140   }
141   return true;
142 }
143
144 QList<QWidget*> ModuleBase_WidgetConcealedObjects::getControls() const
145 {
146   QList<QWidget*> result;
147   result << myView;
148   return result;
149 }
150
151 void ModuleBase_WidgetConcealedObjects::addViewRow(
152                                 const std::shared_ptr<ModelAPI_Result>& theResult)
153 {
154   int anId = myView->rowCount();
155   myView->setRowCount(anId+1);
156
157   QWidget* aVisibilityWdg = new QWidget(this);
158   QVBoxLayout* aVisibilityLay = new QVBoxLayout(aVisibilityWdg);
159   aVisibilityLay->setContentsMargins(2, 2, 2, 2);
160   QCheckBox* aVisibilityBtn = new QCheckBox(aVisibilityWdg);
161   aVisibilityLay->addWidget(aVisibilityBtn, 0, Qt::AlignHCenter);
162   connect(aVisibilityBtn, SIGNAL(toggled(bool)), this, SLOT(onItemToggled(bool)));
163   aVisibilityBtn->setChecked(false);
164
165   myView->setCellWidget(anId, 0, aVisibilityWdg);
166   myView->setItem(anId, 1, new QTableWidgetItem(theResult->data()->name().c_str()));
167
168   if (anId == 1) {
169     myView->setColumnWidth(0, myView->verticalHeader()->defaultSectionSize());
170     myView->setColumnWidth(1, DEFAULT_NAME_COLUMN_WIDTH);
171   }
172 }
173
174 void ModuleBase_WidgetConcealedObjects::onItemToggled(bool theState)
175 {
176   emit valuesChanged();
177   updateObject(myFeature);
178 }
179