Salome HOME
de712233dab1132596b4028ef6e5cbbfa7d4d95e
[modules/geom.git] / src / MeasureGUI / MeasureGUI_CheckSelfIntersectionsDlg.cxx
1 // Copyright (C) 2007-2014  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, or (at your option) any later version.
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
26 #include "MeasureGUI_CheckSelfIntersectionsDlg.h"
27 #include "MeasureGUI_Widgets.h"
28
29 #include <SUIT_Session.h>
30 #include <SUIT_ResourceMgr.h>
31 #include <LightApp_SelectionMgr.h>
32 #include <SalomeApp_Tools.h>
33 #include <SalomeApp_Application.h>
34
35 #include <TopTools_IndexedMapOfShape.hxx>
36 #include <TopExp.hxx>
37 #include <TColStd_MapOfInteger.hxx>
38
39 #include <DlgRef.h>
40 #include <GeometryGUI.h>
41 #include <GEOMBase.h>
42 #include <GEOMImpl_Types.hxx>
43
44 #define TEXTEDIT_FONT_FAMILY "Courier"
45 #define TEXTEDIT_FONT_SIZE 11
46
47 //=================================================================================
48 // class    : MeasureGUI_CheckSelfIntersectionsDlg()
49 // purpose  : Constructs a MeasureGUI_CheckSelfIntersectionsDlg which is a child of 'parent', with the
50 //            name 'name' and widget flags set to 'f'.
51 //            The dialog will by default be modeless, unless you set 'modal' to
52 //            true to construct a modal dialog.
53 //=================================================================================
54 MeasureGUI_CheckSelfIntersectionsDlg::MeasureGUI_CheckSelfIntersectionsDlg (GeometryGUI* GUI, QWidget* parent)
55   : MeasureGUI_Skeleton(GUI, parent, false)
56 {
57   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
58   QPixmap image0 (aResMgr->loadPixmap("GEOM", tr("ICON_DLG_CHECK_SELF_INTERSECTIONS")));
59   QPixmap image1 (aResMgr->loadPixmap("GEOM", tr("ICON_SELECT")));
60
61   setWindowTitle(tr("GEOM_CHECK_SELF_INTERSECTIONS"));
62
63   /***************************************************************/
64   mainFrame()->GroupConstructors->setTitle(tr("GEOM_CHECK_SELF_INTERSECTIONS"));
65   mainFrame()->RadioButton1->setIcon(image0);
66
67   myGrp = new MeasureGUI_1Sel1TextView2ListBox (centralWidget());
68   myGrp->GroupBox1->setTitle(tr("GEOM_CHECK_INFOS"));
69   myGrp->TextLabel1->setText(tr("GEOM_OBJECT"));
70
71   myGrp->TextView1->setReadOnly(true);
72   QFont aFont (TEXTEDIT_FONT_FAMILY, TEXTEDIT_FONT_SIZE);
73   aFont.setStyleHint(QFont::TypeWriter, QFont::PreferAntialias);
74   myGrp->TextView1->setFont(aFont);
75
76   myGrp->PushButton1->setIcon(image1);
77   myGrp->LineEdit1->setReadOnly(true);
78
79   myGrp->TextLabel2->setText(tr("GEOM_CHECK_BLOCKS_COMPOUND_ERRORS"));
80   myGrp->TextLabel3->setText(tr("GEOM_CHECK_BLOCKS_COMPOUND_SUBSHAPES"));
81
82   myGrp->ListBox2->setSelectionMode(QAbstractItemView::ExtendedSelection);
83
84   QVBoxLayout* layout = new QVBoxLayout (centralWidget());
85   layout->setMargin(0); layout->setSpacing(6);
86   layout->addWidget(myGrp);
87
88   /***************************************************************/
89
90   myHelpFileName = "check_self_intersections_page.html";
91
92   /* Initialisation */
93   Init();
94 }
95
96 //=================================================================================
97 // function : ~MeasureGUI_CheckSelfIntersectionsDlg()
98 // purpose  : Destroys the object and frees any allocated resources
99 //=================================================================================
100 MeasureGUI_CheckSelfIntersectionsDlg::~MeasureGUI_CheckSelfIntersectionsDlg()
101 {
102 }
103
104 //=================================================================================
105 // function : Init()
106 // purpose  :
107 //=================================================================================
108 void MeasureGUI_CheckSelfIntersectionsDlg::Init()
109 {
110   mySelBtn = myGrp->PushButton1;
111   mySelEdit = myGrp->LineEdit1;
112   MeasureGUI_Skeleton::Init();
113
114   connect(myGrp->ListBox1, SIGNAL(itemSelectionChanged()), SLOT(onErrorsListSelectionChanged()));
115   connect(myGrp->ListBox2, SIGNAL(itemSelectionChanged()), SLOT(onSubShapesListSelectionChanged()));
116 }
117
118 //=================================================================================
119 // function : processObject
120 // purpose  :
121 //=================================================================================
122 void MeasureGUI_CheckSelfIntersectionsDlg::processObject()
123 {
124   myGrp->ListBox1->clear();
125   myGrp->ListBox2->clear();
126   erasePreview();
127
128   if (myObj->_is_nil())
129     return;
130
131   QString aMsg ("");
132   GEOM::GEOM_IMeasureOperations_var anOper = GEOM::GEOM_IMeasureOperations::_narrow(getOperation());
133   bool isGood = false, isFailed = false;
134   int nbPairs = 0;
135   try {
136     isGood = anOper->CheckSelfIntersections(myObj, myInters);
137     nbPairs = myInters->length()/2;
138     if (nbPairs*2 != myInters->length()) {
139       isFailed = true;
140     }
141   }
142   catch (const SALOME::SALOME_Exception& e) {
143     SalomeApp_Tools::QtCatchCorbaException(e);
144     isFailed = true;
145   }
146
147   if (!anOper->IsDone() && myInters->length() == 0) {
148     aMsg += tr(anOper->GetErrorCode());
149     myGrp->TextView1->setText(aMsg);
150     return;
151   }
152   else if (isFailed) {
153     aMsg += tr("GEOM_CHECK_SELF_INTERSECTIONS_FAILED");
154     myGrp->TextView1->setText(aMsg);
155     return;
156   }
157
158   // Status
159   if (isGood) {
160     aMsg += tr("GEOM_NO_SELF_INTERSECTIONS");
161   }
162   else {
163     aMsg += tr("GEOM_SELF_INTERSECTIONS_FOUND");
164   }
165
166   if (!anOper->IsDone()) {
167     aMsg += "\n\n";
168     aMsg += tr("GEOM_CHECK_SELF_INTERSECTIONS_ERRORS");
169   }
170
171   myGrp->TextView1->setText(aMsg);
172
173   // Pairs
174   QStringList aErrList;
175   QString aErrStr ("");
176   for (int i = 1; i <= nbPairs; i++) {
177     aErrStr = "Intersection # ";
178     aErrStr += QString::number(i);
179     aErrList.append(aErrStr);
180   }
181
182   myGrp->ListBox1->addItems(aErrList);
183 }
184
185 //=================================================================================
186 // function : onErrorsListSelectionChanged
187 // purpose  :
188 //=================================================================================
189 void MeasureGUI_CheckSelfIntersectionsDlg::onErrorsListSelectionChanged()
190 {
191   erasePreview();
192   int aCurItem = myGrp->ListBox1->currentRow();
193   if (aCurItem < 0)
194     return;
195
196   //int nbPairs = myInters->length()/2;
197
198   QStringList aSubShapeList;
199   TopoDS_Shape aSelShape;
200   if (!myObj->_is_nil() && GEOMBase::GetShape(myObj, aSelShape)) {
201     TopTools_IndexedMapOfShape anIndices;
202     TopExp::MapShapes(aSelShape, anIndices);
203
204     TopoDS_Shape aSubShape = anIndices.FindKey(myInters[aCurItem*2]);
205     QString aType = GEOMBase::GetShapeTypeString(aSubShape);
206     if (!aType.isEmpty())
207       aSubShapeList.append(QString("%1_%2").arg(aType).arg(myInters[aCurItem*2]));
208     aSubShape = anIndices.FindKey(myInters[aCurItem*2 + 1]);
209     aType = GEOMBase::GetShapeTypeString(aSubShape);
210     if (!aType.isEmpty())
211       aSubShapeList.append(QString("%1_%2").arg(aType).arg(myInters[aCurItem*2 + 1]));
212   }
213   myGrp->ListBox2->clear();
214   myGrp->ListBox2->addItems(aSubShapeList);
215 }
216
217 //=================================================================================
218 // function : onSubShapesListSelectionChanged
219 // purpose  :
220 //=================================================================================
221 void MeasureGUI_CheckSelfIntersectionsDlg::onSubShapesListSelectionChanged()
222 {
223   erasePreview();
224
225   // Current pair
226   int aErrCurItem = myGrp->ListBox1->currentRow();
227   if (aErrCurItem < 0)
228     return;
229
230   // Selected IDs
231   QList<int> aIds;
232   for (int i = 0, n = myGrp->ListBox2->count(); i < n; i++) {
233     if (myGrp->ListBox2->item(i)->isSelected())
234       aIds.append(i);
235   }
236   if (aIds.count() < 1)
237     return;
238
239   TopoDS_Shape aSelShape;
240   TopoDS_Shape aSubShape;
241   TopTools_IndexedMapOfShape anIndices;
242   if (!myObj->_is_nil() && GEOMBase::GetShape(myObj, aSelShape)) {
243     SALOME_Prs* aPrs = 0;
244     TopExp::MapShapes(aSelShape, anIndices);
245     QList<int>::iterator it;
246     for (it = aIds.begin(); it != aIds.end(); ++it) {
247       aSubShape = anIndices.FindKey(myInters[aErrCurItem*2 + (*it)]);
248       try {
249         getDisplayer()->SetColor(Quantity_NOC_RED);
250         getDisplayer()->SetWidth(3);
251         getDisplayer()->SetToActivate(false);
252         aPrs = !aSubShape.IsNull() ? getDisplayer()->BuildPrs(aSubShape) : 0;
253         if (aPrs)
254           displayPreview(aPrs, true);
255       }
256       catch (const SALOME::SALOME_Exception& e) {
257         SalomeApp_Tools::QtCatchCorbaException(e);
258       }
259     }
260   }
261 }