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