Salome HOME
df12686dd70485966f18143d96d28570036e0f52
[modules/geom.git] / src / MeasureGUI / MeasureGUI_CheckSelfIntersectionsDlg.cxx
1 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 // GEOM GEOMGUI : GUI for Geometry component
24 // File   : MeasureGUI_CheckSelfIntersectionsDlg.cxx
25 // Author : Vladimir KLYACHIN, Open CASCADE S.A.S. (vladimir.klyachin@opencascade.com)
26
27 #include "MeasureGUI_CheckSelfIntersectionsDlg.h"
28 #include "MeasureGUI_Widgets.h"
29
30 #include <SUIT_Session.h>
31 #include <SUIT_ResourceMgr.h>
32 #include <LightApp_SelectionMgr.h>
33 #include <SalomeApp_Tools.h>
34 #include <SalomeApp_Application.h>
35
36 #include <TopTools_IndexedMapOfShape.hxx>
37 #include <TopExp.hxx>
38 #include <TColStd_MapOfInteger.hxx>
39
40 #include <DlgRef.h>
41 #include <GeometryGUI.h>
42 #include <GEOMBase.h>
43 #include <GEOMImpl_Types.hxx>
44
45 #define TEXTEDIT_FONT_FAMILY "Courier"
46 #define TEXTEDIT_FONT_SIZE 11
47
48 //=================================================================================
49 // class    : MeasureGUI_CheckSelfIntersectionsDlg()
50 // purpose  : Constructs a MeasureGUI_CheckSelfIntersectionsDlg which is a child of 'parent', with the
51 //            name 'name' and widget flags set to 'f'.
52 //            The dialog will by default be modeless, unless you set 'modal' to
53 //            true to construct a modal dialog.
54 //=================================================================================
55 MeasureGUI_CheckSelfIntersectionsDlg::MeasureGUI_CheckSelfIntersectionsDlg (GeometryGUI* GUI, QWidget* parent)
56   : MeasureGUI_Skeleton(GUI, parent, false)
57 {
58   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
59   QPixmap image0 (aResMgr->loadPixmap("GEOM", tr("ICON_DLG_CHECK_SELF_INTERSECTIONS")));
60   QPixmap image1 (aResMgr->loadPixmap("GEOM", tr("ICON_SELECT")));
61
62   setWindowTitle(tr("GEOM_CHECK_SELF_INTERSECTIONS"));
63
64   /***************************************************************/
65   mainFrame()->GroupConstructors->setTitle(tr("GEOM_CHECK_SELF_INTERSECTIONS"));
66   mainFrame()->RadioButton1->setIcon(image0);
67
68   myGrp = new MeasureGUI_1Sel1TextView2ListBox (centralWidget());
69   myGrp->GroupBox1->setTitle(tr("GEOM_CHECK_INFOS"));
70   myGrp->TextLabel1->setText(tr("GEOM_OBJECT"));
71
72   myGrp->TextView1->setReadOnly(true);
73   QFont aFont (TEXTEDIT_FONT_FAMILY, TEXTEDIT_FONT_SIZE);
74   aFont.setStyleHint(QFont::TypeWriter, QFont::PreferAntialias);
75   myGrp->TextView1->setFont(aFont);
76
77   myGrp->PushButton1->setIcon(image1);
78   myGrp->LineEdit1->setReadOnly(true);
79
80   myGrp->TextLabel2->setText(tr("GEOM_CHECK_BLOCKS_COMPOUND_ERRORS"));
81   myGrp->TextLabel3->setText(tr("GEOM_CHECK_BLOCKS_COMPOUND_SUBSHAPES"));
82
83   myGrp->ListBox2->setSelectionMode(QAbstractItemView::ExtendedSelection);
84
85   QVBoxLayout* layout = new QVBoxLayout (centralWidget());
86   layout->setMargin(0); layout->setSpacing(6);
87   layout->addWidget(myGrp);
88
89   /***************************************************************/
90
91   myHelpFileName = "using_measurement_tools_page.html#check_self_intersections_anchor";
92
93   /* Initialisation */
94   Init();
95 }
96
97 //=================================================================================
98 // function : ~MeasureGUI_CheckSelfIntersectionsDlg()
99 // purpose  : Destroys the object and frees any allocated resources
100 //=================================================================================
101 MeasureGUI_CheckSelfIntersectionsDlg::~MeasureGUI_CheckSelfIntersectionsDlg()
102 {
103 }
104
105 //=================================================================================
106 // function : Init()
107 // purpose  :
108 //=================================================================================
109 void MeasureGUI_CheckSelfIntersectionsDlg::Init()
110 {
111   mySelBtn = myGrp->PushButton1;
112   mySelEdit = myGrp->LineEdit1;
113   MeasureGUI_Skeleton::Init();
114
115   connect(myGrp->ListBox1, SIGNAL(itemSelectionChanged()), SLOT(onErrorsListSelectionChanged()));
116   connect(myGrp->ListBox2, SIGNAL(itemSelectionChanged()), SLOT(onSubShapesListSelectionChanged()));
117 }
118
119 //=================================================================================
120 // function : processObject
121 // purpose  :
122 //=================================================================================
123 void MeasureGUI_CheckSelfIntersectionsDlg::processObject()
124 {
125   myGrp->ListBox1->clear();
126   myGrp->ListBox2->clear();
127   erasePreview();
128
129   if (myObj->_is_nil())
130     return;
131
132   QString aMsg ("");
133   GEOM::GEOM_IMeasureOperations_var anOper = GEOM::GEOM_IMeasureOperations::_narrow(getOperation());
134   bool isGood = false, isFailed = false;
135   int nbPairs = 0;
136   try {
137     isGood = anOper->CheckSelfIntersections(myObj, myInters);
138     nbPairs = myInters->length()/2;
139     if (nbPairs*2 != myInters->length()) {
140       isFailed = true;
141     }
142   }
143   catch (const SALOME::SALOME_Exception& e) {
144     SalomeApp_Tools::QtCatchCorbaException(e);
145     isFailed = true;
146   }
147   if (isFailed) {
148     aMsg += tr("GEOM_CHECK_SELF_INTERSECTIONS_FAILED");
149     myGrp->TextView1->setText(aMsg);
150     return;
151   }
152
153   // Status
154   if (isGood) {
155     aMsg += tr("GEOM_NO_SELF_INTERSECTIONS");
156   }
157   else {
158     aMsg += tr("GEOM_SELF_INTERSECTIONS_FOUND");
159   }
160   myGrp->TextView1->setText(aMsg);
161
162   // Pairs
163   QStringList aErrList;
164   QString aErrStr ("");
165   for (int i = 1; i <= nbPairs; i++) {
166     aErrStr = "Intersection # ";
167     aErrStr += QString::number(i);
168     aErrList.append(aErrStr);
169   }
170
171   myGrp->ListBox1->addItems(aErrList);
172 }
173
174 //=================================================================================
175 // function : onErrorsListSelectionChanged
176 // purpose  :
177 //=================================================================================
178 void MeasureGUI_CheckSelfIntersectionsDlg::onErrorsListSelectionChanged()
179 {
180   erasePreview();
181   int aCurItem = myGrp->ListBox1->currentRow();
182   if (aCurItem < 0)
183     return;
184
185   //int nbPairs = myInters->length()/2;
186
187   QStringList aSubShapeList;
188   TopoDS_Shape aSelShape;
189   if (!myObj->_is_nil() && GEOMBase::GetShape(myObj, aSelShape)) {
190     TopTools_IndexedMapOfShape anIndices;
191     TopExp::MapShapes(aSelShape, anIndices);
192
193     TopoDS_Shape aSubShape = anIndices.FindKey(myInters[aCurItem*2]);
194     QString aType = GEOMBase::GetShapeTypeString(aSubShape);
195     if (!aType.isEmpty())
196       aSubShapeList.append(QString("%1_%2").arg(aType).arg(myInters[aCurItem*2]));
197     aSubShape = anIndices.FindKey(myInters[aCurItem*2 + 1]);
198     aType = GEOMBase::GetShapeTypeString(aSubShape);
199     if (!aType.isEmpty())
200       aSubShapeList.append(QString("%1_%2").arg(aType).arg(myInters[aCurItem*2 + 1]));
201   }
202   myGrp->ListBox2->clear();
203   myGrp->ListBox2->addItems(aSubShapeList);
204 }
205
206 //=================================================================================
207 // function : onSubShapesListSelectionChanged
208 // purpose  :
209 //=================================================================================
210 void MeasureGUI_CheckSelfIntersectionsDlg::onSubShapesListSelectionChanged()
211 {
212   erasePreview();
213
214   // Current pair
215   int aErrCurItem = myGrp->ListBox1->currentRow();
216   if (aErrCurItem < 0)
217     return;
218
219   // Selected IDs
220   QList<int> aIds;
221   for (int i = 0, n = myGrp->ListBox2->count(); i < n; i++) {
222     if (myGrp->ListBox2->item(i)->isSelected())
223       aIds.append(i);
224   }
225   if (aIds.count() < 1)
226     return;
227
228   TopoDS_Shape aSelShape;
229   TopoDS_Shape aSubShape;
230   TopTools_IndexedMapOfShape anIndices;
231   if (!myObj->_is_nil() && GEOMBase::GetShape(myObj, aSelShape)) {
232     SALOME_Prs* aPrs = 0;
233     TopExp::MapShapes(aSelShape, anIndices);
234     QList<int>::iterator it;
235     for (it = aIds.begin(); it != aIds.end(); ++it) {
236       aSubShape = anIndices.FindKey(myInters[aErrCurItem*2 + (*it)]);
237       try {
238         getDisplayer()->SetColor(Quantity_NOC_RED);
239         getDisplayer()->SetWidth(3);
240         getDisplayer()->SetToActivate(false);
241         aPrs = !aSubShape.IsNull() ? getDisplayer()->BuildPrs(aSubShape) : 0;
242         if (aPrs)
243           displayPreview(aPrs, true);
244       }
245       catch (const SALOME::SALOME_Exception& e) {
246         SalomeApp_Tools::QtCatchCorbaException(e);
247       }
248     }
249   }
250 }