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