0029025: TInspector include files are not installed to inc directory by CMake
[occt.git] / tools / DFBrowserPane / DFBrowserPane_TNamingNamedShape.cxx
1 // Created on: 2017-06-16
2 // Created by: Natalia ERMOLAEVA
3 // Copyright (c) 2017 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement. 
15
16 #include <inspector/DFBrowserPane_TNamingNamedShape.hxx>
17
18 #include <inspector/DFBrowserPane_AttributePaneModel.hxx>
19 #include <inspector/DFBrowserPane_ItemDelegateButton.hxx>
20 #include <inspector/DFBrowserPane_ItemRole.hxx>
21 #include <inspector/DFBrowserPane_HelperExport.hxx>
22 #include <inspector/DFBrowserPane_SelectionKind.hxx>
23 #include <inspector/DFBrowserPane_TableView.hxx>
24 #include <inspector/DFBrowserPane_Tools.hxx>
25
26 #include <AIS_InteractiveObject.hxx>
27 #include <AIS_Shape.hxx>
28 #include <BRep_Builder.hxx>
29 #include <BRepTools.hxx>
30 #include <TNaming_Builder.hxx>
31 #include <TNaming_Iterator.hxx>
32 #include <TNaming_NamedShape.hxx>
33 #include <TNaming_Tool.hxx>
34 #include <TopoDS_Shape.hxx>
35 #include <TopoDS_Compound.hxx>
36
37 #include <QFileDialog>
38 #include <QGridLayout>
39 #include <QEvent>
40 #include <QHeaderView>
41 #include <QIcon>
42 #include <QMap>
43 #include <QPainter>
44 #include <QStyledItemDelegate>
45 #include <QTableView>
46 #include <QVariant>
47 #include <QWidget>
48
49 const int COLUMN_EXPORT_WIDTH = 20;
50 const int COLUMN_TYPE_WIDTH = 70;
51 const int COLUMN_POINTER_WIDTH = 110;
52 const int COLUMN_REFERENCE_WIDTH = 90;
53 const int COLUMN_EVOLUTION_WIDTH = 90;
54
55 // =======================================================================
56 // function : Constructor
57 // purpose :
58 // =======================================================================
59 DFBrowserPane_TNamingNamedShape::DFBrowserPane_TNamingNamedShape()
60 : DFBrowserPane_AttributePane(), myEvolutionTableView (0), myHelperExport (0)
61 {
62   QList<QVariant> aHeaderValues;
63   aHeaderValues << "Value" << "Type" << "BREP" << "SV";
64   getPaneModel()->SetHeaderValues (aHeaderValues, Qt::Horizontal);
65   getPaneModel()->SetColumnCount (aHeaderValues.count());
66
67   aHeaderValues.clear();
68   aHeaderValues << "Version" << "Evolution" << "Shape" << "Current Shape" << "Original Shape";
69   getPaneModel()->SetHeaderValues (aHeaderValues, Qt::Vertical);
70
71   aHeaderValues.clear();
72   aHeaderValues << "New Shape" << "Type" << "" << "Old Shape" << "Type" << "Label" << "" << "Evolution" << "isModified";
73   myEvolutionPaneModel = new DFBrowserPane_AttributePaneModel();
74   myEvolutionPaneModel->SetHeaderValues (aHeaderValues, Qt::Horizontal);
75   myEvolutionPaneModel->SetColumnCount (aHeaderValues.count());
76   QItemSelectionModel* aSelectionModel = new QItemSelectionModel (myEvolutionPaneModel);
77   mySelectionModels.push_back (aSelectionModel);
78 }
79
80 // =======================================================================
81 // function : CreateWidget
82 // purpose :
83 // =======================================================================
84 QWidget* DFBrowserPane_TNamingNamedShape::CreateWidget (QWidget* theParent)
85 {
86   QWidget* aMainWidget = new QWidget (theParent);
87   aMainWidget->setVisible (false);
88
89   myTableView = new DFBrowserPane_TableView (aMainWidget);
90   myTableView->SetModel (myPaneModel);
91   QTableView* aTableView = myTableView->GetTableView();
92   aTableView->setSelectionBehavior (QAbstractItemView::SelectItems);
93   std::list<QItemSelectionModel*>::const_iterator aSelectionModelsIt = mySelectionModels.begin();
94   aTableView->setSelectionModel (*aSelectionModelsIt);
95   aSelectionModelsIt++;
96
97   aTableView->horizontalHeader()->setStretchLastSection (false);
98   aTableView->setColumnWidth (0, COLUMN_POINTER_WIDTH);
99   aTableView->setColumnWidth (1, COLUMN_TYPE_WIDTH);
100   aTableView->setColumnWidth (2, COLUMN_EXPORT_WIDTH);
101   aTableView->setColumnWidth (3, COLUMN_EXPORT_WIDTH);
102   aTableView->verticalHeader()->setVisible (true);
103   DFBrowserPane_ItemDelegateButton* anItemDelegate = new DFBrowserPane_ItemDelegateButton (aTableView,
104                                                                                           ":/icons/export_shape.png");
105   QList<int> aRows;
106   aRows << 0 << 1;
107   anItemDelegate->SetFreeRows (aRows);
108   QObject::connect (anItemDelegate, SIGNAL (buttonPressed (const QModelIndex&)),
109                     &myHelperExport, SLOT (OnButtonPressed (const QModelIndex&)));
110   aTableView->setItemDelegateForColumn (2, anItemDelegate);
111
112   DFBrowserPane_ItemDelegateButton* anItemDelegate2 = new DFBrowserPane_ItemDelegateButton (aTableView,
113                                                                                             ":/icons/folder_export.png");
114   anItemDelegate2->SetFreeRows (aRows);
115   aTableView->setItemDelegateForColumn (3, anItemDelegate2);
116
117   myEvolutionTableView = new DFBrowserPane_TableView (aMainWidget);
118   myEvolutionTableView->SetModel (myEvolutionPaneModel);
119   aTableView = myEvolutionTableView->GetTableView();
120
121   aTableView->setSelectionModel (*aSelectionModelsIt);
122
123   QList<int> aColumnWidths;
124   aColumnWidths << COLUMN_POINTER_WIDTH << COLUMN_TYPE_WIDTH << COLUMN_EXPORT_WIDTH << COLUMN_POINTER_WIDTH
125                 << COLUMN_TYPE_WIDTH << COLUMN_REFERENCE_WIDTH << COLUMN_EXPORT_WIDTH << COLUMN_EVOLUTION_WIDTH;
126   for (int aColumnId = 0, aCount = aColumnWidths.size(); aColumnId < aCount; aColumnId++)
127     aTableView->setColumnWidth (aColumnId, aColumnWidths[aColumnId]);
128
129   anItemDelegate = new DFBrowserPane_ItemDelegateButton (myEvolutionTableView->GetTableView(), ":/icons/export_shape.png");
130   QObject::connect (anItemDelegate, SIGNAL (buttonPressed (const QModelIndex&)),
131                     &myHelperExport, SLOT (OnButtonPressed (const QModelIndex&)));
132   myEvolutionTableView->GetTableView()->setItemDelegateForColumn (2, anItemDelegate);
133
134   anItemDelegate = new DFBrowserPane_ItemDelegateButton (myEvolutionTableView->GetTableView(), ":/icons/export_shape.png");
135   QObject::connect (anItemDelegate, SIGNAL (buttonPressed (const QModelIndex&)),
136                     &myHelperExport, SLOT (OnButtonPressed (const QModelIndex&)));
137   myEvolutionTableView->GetTableView()->setItemDelegateForColumn (6, anItemDelegate);
138
139   QGridLayout* aLay = new QGridLayout (aMainWidget);
140   aLay->setContentsMargins (0, 0, 0, 0);
141   aLay->addWidget (myTableView, 0, 0);
142   aLay->addWidget (myEvolutionTableView, 1, 0);
143
144   return aMainWidget;
145 }
146
147 // =======================================================================
148 // function : Init
149 // purpose :
150 // =======================================================================
151 void DFBrowserPane_TNamingNamedShape::Init (const Handle(TDF_Attribute)& theAttribute)
152 {
153   Handle(TNaming_NamedShape) aShapeAttr = Handle(TNaming_NamedShape)::DownCast (theAttribute);
154   myHelperExport.Clear();
155
156   // table view filling
157   QList<QVariant> aValues;
158   aValues << QString::number (aShapeAttr->Version()) << "" << "" << "";
159   aValues << DFBrowserPane_Tools::ToName (DB_NS_TYPE, aShapeAttr->Evolution()).ToCString() << "" << "" << "";
160
161   NCollection_List<TopoDS_Shape> aShapes;
162   {
163     TopoDS_Shape aShape = aShapeAttr->Get();
164     TCollection_AsciiString aShapeInfo = !aShape.IsNull() ? DFBrowserPane_Tools::GetPointerInfo (aShape.TShape()) : "";
165     aValues << aShapeInfo.ToCString() << DFBrowserPane_Tools::ShapeTypeInfo (aShape) << "" << "";
166     aShapes.Append (aShape);
167
168     TopoDS_Shape aCurrentShape = TNaming_Tool::CurrentShape (aShapeAttr);
169     TCollection_AsciiString aCurrentShapeInfo = !aCurrentShape.IsNull() ?
170                                     DFBrowserPane_Tools::GetPointerInfo (aCurrentShape.TShape()) : "";
171     aValues << aCurrentShapeInfo.ToCString() << DFBrowserPane_Tools::ShapeTypeInfo (aCurrentShape) << "" << "";
172     aShapes.Append (aCurrentShape);
173
174     TopoDS_Shape anOriginalShape = TNaming_Tool::OriginalShape (aShapeAttr);
175     TCollection_AsciiString anOriginalShapeInfo = !anOriginalShape.IsNull() ?
176               DFBrowserPane_Tools::GetPointerInfo (aShape.TShape()) : "";
177     aValues << anOriginalShapeInfo.ToCString() << DFBrowserPane_Tools::ShapeTypeInfo (anOriginalShape) << "" << "";
178     aShapes.Append (anOriginalShape);
179   }
180
181   DFBrowserPane_AttributePaneModel* aModel = getPaneModel();
182   aModel->Init (aValues);
183
184   QModelIndexList anIndices;
185   int aRowId = 2;
186   for (NCollection_List<TopoDS_Shape>::Iterator aShapeIt (aShapes); aShapeIt.More(); aShapeIt.Next(), aRowId++)
187   {
188     const TopoDS_Shape& aShape = aShapeIt.Value();
189     if (aShape.IsNull())
190       continue;
191
192     anIndices.clear();
193     anIndices << aModel->index (aRowId, 2) << aModel->index (aRowId, 3);
194     myHelperExport.AddShape (aShape, anIndices);
195   }
196
197   // evolution table view filling
198   aValues.clear();
199   aRowId = 0;
200   for (TNaming_Iterator aShapeAttrIt (aShapeAttr); aShapeAttrIt.More(); aShapeAttrIt.Next(), aRowId++)
201   {
202     const TopoDS_Shape& anOldShape = aShapeAttrIt.OldShape();
203     const TopoDS_Shape& aNewShape = aShapeAttrIt.NewShape();
204
205     Handle(TNaming_NamedShape) anOldAttr = TNaming_Tool::NamedShape (anOldShape, aShapeAttr->Label());
206     QString aLabelInfo;
207     if (!anOldAttr.IsNull())
208     {
209       TDF_Label anOldLabel = anOldAttr->Label();
210       if (!anOldLabel.IsNull())
211         aLabelInfo = QString (DFBrowserPane_Tools::GetEntry (anOldLabel).ToCString());
212     }
213     if (!aNewShape.IsNull())
214       aValues << DFBrowserPane_Tools::GetPointerInfo (aNewShape.TShape()->This()).ToCString()
215               << DFBrowserPane_Tools::ShapeTypeInfo (aNewShape)
216               << "";
217     else
218       aValues << "" << "" << "";
219     if (!anOldShape.IsNull())
220       aValues << DFBrowserPane_Tools::GetPointerInfo (anOldShape.TShape()->This()).ToCString()
221               << DFBrowserPane_Tools::ShapeTypeInfo (anOldShape)
222               << aLabelInfo
223               << "";
224     else
225       aValues << "" << "" << "" << "";
226     aValues << DFBrowserPane_Tools::ToName (DB_NS_TYPE, aShapeAttrIt.Evolution()).ToCString()
227             << (aShapeAttrIt.IsModification() ? "true" : "false");
228   }
229
230   if (myEvolutionTableView)
231   {
232     myEvolutionTableView->setVisible (aValues.size() > 0);
233     myEvolutionPaneModel->Init (aValues);
234
235     aRowId = 0;
236     for (TNaming_Iterator aShapeAttrIt (aShapeAttr); aShapeAttrIt.More(); aShapeAttrIt.Next(), aRowId++)
237     {
238       const TopoDS_Shape& anOldShape = aShapeAttrIt.OldShape();
239       const TopoDS_Shape& aNewShape = aShapeAttrIt.NewShape();
240
241       if (!aNewShape.IsNull())
242       {
243         anIndices.clear();
244         anIndices << myEvolutionPaneModel->index (aRowId, 0) << myEvolutionPaneModel->index (aRowId, 1)
245                   << myEvolutionPaneModel->index (aRowId, 2);
246         myHelperExport.AddShape (aNewShape, anIndices);
247       }
248       if (!anOldShape.IsNull())
249       {
250         anIndices.clear();
251         anIndices << myEvolutionPaneModel->index (aRowId, 3) << myEvolutionPaneModel->index (aRowId, 4)
252                   << myEvolutionPaneModel->index (aRowId, 5) << myEvolutionPaneModel->index (aRowId, 6);
253         myHelperExport.AddShape (anOldShape, anIndices);
254       }
255     }
256   }
257 }
258
259 // =======================================================================
260 // function : GetValues
261 // purpose :
262 // =======================================================================
263 void DFBrowserPane_TNamingNamedShape::GetValues (const Handle(TDF_Attribute)&, QList<QVariant>&)
264 {
265 }
266
267 // =======================================================================
268 // function : GetAttributeInfo
269 // purpose :
270 // =======================================================================
271 QVariant DFBrowserPane_TNamingNamedShape::GetAttributeInfo (const Handle(TDF_Attribute)& theAttribute, int theRole,
272                                                             int theColumnId)
273 {
274   if (theColumnId != 0)
275     return DFBrowserPane_AttributePane::GetAttributeInfo (theAttribute, theRole, theColumnId);
276
277   switch (theRole)
278   {
279     case Qt::DecorationRole: return QIcon (":/icons/named_shape.png");
280     case DFBrowserPane_ItemRole_Decoration_40x40: return QIcon (":/icons/named_shape_40x40.png");
281     case Qt::ForegroundRole:
282     {
283       TopoDS_Shape aShape;
284       Handle(TNaming_NamedShape) anAttribute = Handle(TNaming_NamedShape)::DownCast (theAttribute);
285       if (!anAttribute.IsNull())
286         aShape = anAttribute->Get();
287       if (aShape.IsNull())
288         return QColor (Qt::black);
289
290       return QColor (aShape.Orientation() == TopAbs_FORWARD ? Qt::darkGray :
291                      aShape.Orientation() == TopAbs_REVERSED ? QColor (Qt::gray) : Qt::black);
292     }
293     default:
294     break;
295   }
296   return DFBrowserPane_AttributePane::GetAttributeInfo (theAttribute, theRole, theColumnId);
297 }
298
299 // =======================================================================
300 // function : GetShortAttributeInfo
301 // purpose :
302 // =======================================================================
303 void DFBrowserPane_TNamingNamedShape::GetShortAttributeInfo (const Handle(TDF_Attribute)& theAttribute,
304                                                              QList<QVariant>& theValues)
305 {
306   Handle(TNaming_NamedShape) aShapeAttribute = Handle(TNaming_NamedShape)::DownCast (theAttribute);
307
308   if (aShapeAttribute->Get().IsNull())
309     theValues.append ("EMPTY SHAPE");
310   else
311   {
312     theValues.append (QString ("%1 : %2").arg (DFBrowserPane_Tools::ToName (DB_SHAPE_TYPE, aShapeAttribute->Get().ShapeType()).ToCString())
313                                          .arg (DFBrowserPane_Tools::ToName (DB_NS_TYPE, aShapeAttribute->Evolution()).ToCString()));
314   }
315 }
316
317 // =======================================================================
318 // function : GetSelectionKind
319 // purpose :
320 // =======================================================================
321 int DFBrowserPane_TNamingNamedShape::GetSelectionKind (QItemSelectionModel* theModel)
322 {
323   int aKind = DFBrowserPane_AttributePaneAPI::GetSelectionKind (theModel);
324
325   QTableView* aTableView = myTableView->GetTableView();
326   if (aTableView->selectionModel() != theModel)
327     return aKind;
328
329   QModelIndexList aSelectedIndices = theModel->selectedIndexes();
330   if (aSelectedIndices.size() != 1)
331     return aKind;
332
333   QModelIndex aSelectedIndex = aSelectedIndices.first();
334   int aRow = aSelectedIndex.row();
335   if (aRow == 0 || aRow == 1)
336     return aKind;
337
338   if (aSelectedIndex.column() == 3)
339     aKind = DFBrowserPane_SelectionKind_ExportToShapeViewer;
340
341   return aKind;
342 }
343
344 // =======================================================================
345 // function : GetSelectionParameters
346 // purpose :
347 // =======================================================================
348 void DFBrowserPane_TNamingNamedShape::GetSelectionParameters (QItemSelectionModel* theModel,
349                                                               NCollection_List<Handle(Standard_Transient)>& theParameters)
350 {
351   QTableView* aTableView = myTableView->GetTableView();
352   if (aTableView->selectionModel() != theModel)
353     return;
354
355   QModelIndexList aSelectedIndices = theModel->selectedIndexes();
356   if (aSelectedIndices.size() != 1)
357     return;
358
359   QModelIndex aSelectedIndex = aSelectedIndices.first();
360   if (aSelectedIndex.column() != 3)
361     return;
362
363   const TopoDS_Shape& aShape = myHelperExport.GetShape (aSelectedIndex);
364   if (aShape.IsNull())
365     return;
366   theParameters.Append (aShape.TShape());
367 }
368
369 // =======================================================================
370 // function : GetReferences
371 // purpose :
372 // =======================================================================
373 void DFBrowserPane_TNamingNamedShape::GetReferences (const Handle(TDF_Attribute)& theAttribute,
374                                                      NCollection_List<TDF_Label>& theRefLabels,
375                                                      Handle(Standard_Transient)& theRefPresentation)
376 {
377   if (!myEvolutionTableView)
378     return;
379   QStringList aSelectedEntries = DFBrowserPane_TableView::GetSelectedColumnValues (
380                                                           myEvolutionTableView->GetTableView(), 5);
381
382   Handle(TNaming_NamedShape) aShapeAttr = Handle(TNaming_NamedShape)::DownCast (theAttribute);
383   for (TNaming_Iterator aShapeAttrIt (aShapeAttr); aShapeAttrIt.More(); aShapeAttrIt.Next())
384   {
385     const TopoDS_Shape& anOldShape = aShapeAttrIt.OldShape();
386
387     Handle(TNaming_NamedShape) anOldAttr = TNaming_Tool::NamedShape (anOldShape, aShapeAttr->Label());
388     QString aLabelInfo;
389     if (!anOldAttr.IsNull())
390     {
391       TDF_Label anOldLabel = anOldAttr->Label();
392       if (!anOldLabel.IsNull())
393       {
394         if (aSelectedEntries.contains (DFBrowserPane_Tools::GetEntry (anOldLabel).ToCString()))
395         theRefLabels.Append (anOldLabel);
396       }
397     }
398   }
399   TopoDS_Shape aShape = getSelectedShapes();
400   if (!aShape.IsNull())
401     theRefPresentation = new AIS_Shape (aShape);
402 }
403
404 // =======================================================================
405 // function : GetPresentation
406 // purpose :
407 // =======================================================================
408 Handle(Standard_Transient) DFBrowserPane_TNamingNamedShape::GetPresentation (const Handle(TDF_Attribute)& theAttribute)
409 {
410   Handle(Standard_Transient) aPresentation;
411   Handle(TNaming_NamedShape) aShapeAttr = Handle(TNaming_NamedShape)::DownCast (theAttribute);
412   if (aShapeAttr.IsNull())
413     return aPresentation;
414
415   TopoDS_Shape aShape = aShapeAttr->Get();
416   if (aShape.IsNull())
417     return aPresentation;
418
419   aPresentation = new AIS_Shape (aShape);
420   return aPresentation;
421 }
422
423 // =======================================================================
424 // function : getSelectedShapes
425 // purpose :
426 // =======================================================================
427 TopoDS_Shape DFBrowserPane_TNamingNamedShape::getSelectedShapes()
428 {
429   TopoDS_Shape aShape;
430
431   if (!myTableView && !myEvolutionTableView)
432     return aShape;
433
434   // table view selected shapes
435   QItemSelectionModel* aTableViewSelModel = myTableView->GetTableView()->selectionModel();
436   QModelIndexList anIndices = aTableViewSelModel->selectedIndexes();
437
438   BRep_Builder aBuilder;
439   TopoDS_Compound aComp;
440   aBuilder.MakeCompound (aComp);
441   bool aHasShapes = false;
442   for (QModelIndexList::const_iterator anIt = anIndices.begin(), aLast = anIndices.end(); anIt != aLast; anIt++)
443   {
444     QModelIndex anIndex = *anIt;
445     if (!myHelperExport.HasShape (anIndex))
446       continue;
447     aBuilder.Add (aComp, myHelperExport.GetShape (anIndex));
448     aHasShapes = true;
449   }
450
451   // evolution table selected shapes
452   aTableViewSelModel = myEvolutionTableView->GetTableView()->selectionModel();
453   anIndices.clear();
454   anIndices = aTableViewSelModel->selectedIndexes();
455   for (QModelIndexList::const_iterator anIt = anIndices.begin(), aLast = anIndices.end(); anIt != aLast; anIt++)
456   {
457     QModelIndex anIndex = *anIt;
458     if (!myHelperExport.HasShape (anIndex))
459       continue;
460     aBuilder.Add (aComp, myHelperExport.GetShape (anIndex));
461     aHasShapes = true;
462   }
463   if (aHasShapes)
464     aShape = aComp;
465   return aShape;
466 }