1 // Created on: 2017-06-16
2 // Created by: Natalia ERMOLAEVA
3 // Copyright (c) 2017 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
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.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #include <inspector/DFBrowserPane_TNamingNamedShape.hxx>
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>
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>
37 #include <QFileDialog>
38 #include <QGridLayout>
40 #include <QHeaderView>
44 #include <QStyledItemDelegate>
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;
55 // =======================================================================
56 // function : Constructor
58 // =======================================================================
59 DFBrowserPane_TNamingNamedShape::DFBrowserPane_TNamingNamedShape()
60 : DFBrowserPane_AttributePane(), myEvolutionTableView (0), myHelperExport (0)
62 QList<QVariant> aHeaderValues;
63 aHeaderValues << "Value" << "Type" << "BREP" << "SV";
64 getPaneModel()->SetHeaderValues (aHeaderValues, Qt::Horizontal);
65 getPaneModel()->SetColumnCount (aHeaderValues.count());
67 aHeaderValues.clear();
68 aHeaderValues << "Version" << "Evolution" << "Shape" << "Current Shape" << "Original Shape";
69 getPaneModel()->SetHeaderValues (aHeaderValues, Qt::Vertical);
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);
80 // =======================================================================
81 // function : CreateWidget
83 // =======================================================================
84 QWidget* DFBrowserPane_TNamingNamedShape::CreateWidget (QWidget* theParent)
86 QWidget* aMainWidget = new QWidget (theParent);
87 aMainWidget->setVisible (false);
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);
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");
107 anItemDelegate->SetFreeRows (aRows);
108 QObject::connect (anItemDelegate, SIGNAL (buttonPressed (const QModelIndex&)),
109 &myHelperExport, SLOT (OnButtonPressed (const QModelIndex&)));
110 aTableView->setItemDelegateForColumn (2, anItemDelegate);
112 DFBrowserPane_ItemDelegateButton* anItemDelegate2 = new DFBrowserPane_ItemDelegateButton (aTableView,
113 ":/icons/folder_export.png");
114 anItemDelegate2->SetFreeRows (aRows);
115 aTableView->setItemDelegateForColumn (3, anItemDelegate2);
117 myEvolutionTableView = new DFBrowserPane_TableView (aMainWidget);
118 myEvolutionTableView->SetModel (myEvolutionPaneModel);
119 aTableView = myEvolutionTableView->GetTableView();
121 aTableView->setSelectionModel (*aSelectionModelsIt);
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]);
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);
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);
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);
147 // =======================================================================
150 // =======================================================================
151 void DFBrowserPane_TNamingNamedShape::Init (const Handle(TDF_Attribute)& theAttribute)
153 Handle(TNaming_NamedShape) aShapeAttr = Handle(TNaming_NamedShape)::DownCast (theAttribute);
154 myHelperExport.Clear();
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() << "" << "" << "";
161 NCollection_List<TopoDS_Shape> aShapes;
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);
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);
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);
181 DFBrowserPane_AttributePaneModel* aModel = getPaneModel();
182 aModel->Init (aValues);
184 QModelIndexList anIndices;
186 for (NCollection_List<TopoDS_Shape>::Iterator aShapeIt (aShapes); aShapeIt.More(); aShapeIt.Next(), aRowId++)
188 const TopoDS_Shape& aShape = aShapeIt.Value();
193 anIndices << aModel->index (aRowId, 2) << aModel->index (aRowId, 3);
194 myHelperExport.AddShape (aShape, anIndices);
197 // evolution table view filling
200 for (TNaming_Iterator aShapeAttrIt (aShapeAttr); aShapeAttrIt.More(); aShapeAttrIt.Next(), aRowId++)
202 const TopoDS_Shape& anOldShape = aShapeAttrIt.OldShape();
203 const TopoDS_Shape& aNewShape = aShapeAttrIt.NewShape();
205 Handle(TNaming_NamedShape) anOldAttr = TNaming_Tool::NamedShape (anOldShape, aShapeAttr->Label());
207 if (!anOldAttr.IsNull())
209 TDF_Label anOldLabel = anOldAttr->Label();
210 if (!anOldLabel.IsNull())
211 aLabelInfo = QString (DFBrowserPane_Tools::GetEntry (anOldLabel).ToCString());
213 if (!aNewShape.IsNull())
214 aValues << DFBrowserPane_Tools::GetPointerInfo (aNewShape.TShape()->This()).ToCString()
215 << DFBrowserPane_Tools::ShapeTypeInfo (aNewShape)
218 aValues << "" << "" << "";
219 if (!anOldShape.IsNull())
220 aValues << DFBrowserPane_Tools::GetPointerInfo (anOldShape.TShape()->This()).ToCString()
221 << DFBrowserPane_Tools::ShapeTypeInfo (anOldShape)
225 aValues << "" << "" << "" << "";
226 aValues << DFBrowserPane_Tools::ToName (DB_NS_TYPE, aShapeAttrIt.Evolution()).ToCString()
227 << (aShapeAttrIt.IsModification() ? "true" : "false");
230 if (myEvolutionTableView)
232 myEvolutionTableView->setVisible (aValues.size() > 0);
233 myEvolutionPaneModel->Init (aValues);
236 for (TNaming_Iterator aShapeAttrIt (aShapeAttr); aShapeAttrIt.More(); aShapeAttrIt.Next(), aRowId++)
238 const TopoDS_Shape& anOldShape = aShapeAttrIt.OldShape();
239 const TopoDS_Shape& aNewShape = aShapeAttrIt.NewShape();
241 if (!aNewShape.IsNull())
244 anIndices << myEvolutionPaneModel->index (aRowId, 0) << myEvolutionPaneModel->index (aRowId, 1)
245 << myEvolutionPaneModel->index (aRowId, 2);
246 myHelperExport.AddShape (aNewShape, anIndices);
248 if (!anOldShape.IsNull())
251 anIndices << myEvolutionPaneModel->index (aRowId, 3) << myEvolutionPaneModel->index (aRowId, 4)
252 << myEvolutionPaneModel->index (aRowId, 5) << myEvolutionPaneModel->index (aRowId, 6);
253 myHelperExport.AddShape (anOldShape, anIndices);
259 // =======================================================================
260 // function : GetValues
262 // =======================================================================
263 void DFBrowserPane_TNamingNamedShape::GetValues (const Handle(TDF_Attribute)&, QList<QVariant>&)
267 // =======================================================================
268 // function : GetAttributeInfo
270 // =======================================================================
271 QVariant DFBrowserPane_TNamingNamedShape::GetAttributeInfo (const Handle(TDF_Attribute)& theAttribute, int theRole,
274 if (theColumnId != 0)
275 return DFBrowserPane_AttributePane::GetAttributeInfo (theAttribute, theRole, theColumnId);
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:
284 Handle(TNaming_NamedShape) anAttribute = Handle(TNaming_NamedShape)::DownCast (theAttribute);
285 if (!anAttribute.IsNull())
286 aShape = anAttribute->Get();
288 return QColor (Qt::black);
290 return QColor (aShape.Orientation() == TopAbs_FORWARD ? Qt::darkGray :
291 aShape.Orientation() == TopAbs_REVERSED ? QColor (Qt::gray) : Qt::black);
296 return DFBrowserPane_AttributePane::GetAttributeInfo (theAttribute, theRole, theColumnId);
299 // =======================================================================
300 // function : GetShortAttributeInfo
302 // =======================================================================
303 void DFBrowserPane_TNamingNamedShape::GetShortAttributeInfo (const Handle(TDF_Attribute)& theAttribute,
304 QList<QVariant>& theValues)
306 Handle(TNaming_NamedShape) aShapeAttribute = Handle(TNaming_NamedShape)::DownCast (theAttribute);
308 if (aShapeAttribute->Get().IsNull())
309 theValues.append ("EMPTY SHAPE");
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()));
317 // =======================================================================
318 // function : GetSelectionKind
320 // =======================================================================
321 int DFBrowserPane_TNamingNamedShape::GetSelectionKind (QItemSelectionModel* theModel)
323 int aKind = DFBrowserPane_AttributePaneAPI::GetSelectionKind (theModel);
325 QTableView* aTableView = myTableView->GetTableView();
326 if (aTableView->selectionModel() != theModel)
329 QModelIndexList aSelectedIndices = theModel->selectedIndexes();
330 if (aSelectedIndices.size() != 1)
333 QModelIndex aSelectedIndex = aSelectedIndices.first();
334 int aRow = aSelectedIndex.row();
335 if (aRow == 0 || aRow == 1)
338 if (aSelectedIndex.column() == 3)
339 aKind = DFBrowserPane_SelectionKind_ExportToShapeViewer;
344 // =======================================================================
345 // function : GetSelectionParameters
347 // =======================================================================
348 void DFBrowserPane_TNamingNamedShape::GetSelectionParameters (QItemSelectionModel* theModel,
349 NCollection_List<Handle(Standard_Transient)>& theParameters)
351 QTableView* aTableView = myTableView->GetTableView();
352 if (aTableView->selectionModel() != theModel)
355 QModelIndexList aSelectedIndices = theModel->selectedIndexes();
356 if (aSelectedIndices.size() != 1)
359 QModelIndex aSelectedIndex = aSelectedIndices.first();
360 if (aSelectedIndex.column() != 3)
363 const TopoDS_Shape& aShape = myHelperExport.GetShape (aSelectedIndex);
366 theParameters.Append (aShape.TShape());
369 // =======================================================================
370 // function : GetReferences
372 // =======================================================================
373 void DFBrowserPane_TNamingNamedShape::GetReferences (const Handle(TDF_Attribute)& theAttribute,
374 NCollection_List<TDF_Label>& theRefLabels,
375 Handle(Standard_Transient)& theRefPresentation)
377 if (!myEvolutionTableView)
379 QStringList aSelectedEntries = DFBrowserPane_TableView::GetSelectedColumnValues (
380 myEvolutionTableView->GetTableView(), 5);
382 Handle(TNaming_NamedShape) aShapeAttr = Handle(TNaming_NamedShape)::DownCast (theAttribute);
383 for (TNaming_Iterator aShapeAttrIt (aShapeAttr); aShapeAttrIt.More(); aShapeAttrIt.Next())
385 const TopoDS_Shape& anOldShape = aShapeAttrIt.OldShape();
387 Handle(TNaming_NamedShape) anOldAttr = TNaming_Tool::NamedShape (anOldShape, aShapeAttr->Label());
389 if (!anOldAttr.IsNull())
391 TDF_Label anOldLabel = anOldAttr->Label();
392 if (!anOldLabel.IsNull())
394 if (aSelectedEntries.contains (DFBrowserPane_Tools::GetEntry (anOldLabel).ToCString()))
395 theRefLabels.Append (anOldLabel);
399 TopoDS_Shape aShape = getSelectedShapes();
400 if (!aShape.IsNull())
401 theRefPresentation = new AIS_Shape (aShape);
404 // =======================================================================
405 // function : GetPresentation
407 // =======================================================================
408 Handle(Standard_Transient) DFBrowserPane_TNamingNamedShape::GetPresentation (const Handle(TDF_Attribute)& theAttribute)
410 Handle(Standard_Transient) aPresentation;
411 Handle(TNaming_NamedShape) aShapeAttr = Handle(TNaming_NamedShape)::DownCast (theAttribute);
412 if (aShapeAttr.IsNull())
413 return aPresentation;
415 TopoDS_Shape aShape = aShapeAttr->Get();
417 return aPresentation;
419 aPresentation = new AIS_Shape (aShape);
420 return aPresentation;
423 // =======================================================================
424 // function : getSelectedShapes
426 // =======================================================================
427 TopoDS_Shape DFBrowserPane_TNamingNamedShape::getSelectedShapes()
431 if (!myTableView && !myEvolutionTableView)
434 // table view selected shapes
435 QItemSelectionModel* aTableViewSelModel = myTableView->GetTableView()->selectionModel();
436 QModelIndexList anIndices = aTableViewSelModel->selectedIndexes();
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++)
444 QModelIndex anIndex = *anIt;
445 if (!myHelperExport.HasShape (anIndex))
447 aBuilder.Add (aComp, myHelperExport.GetShape (anIndex));
451 // evolution table selected shapes
452 aTableViewSelModel = myEvolutionTableView->GetTableView()->selectionModel();
454 anIndices = aTableViewSelModel->selectedIndexes();
455 for (QModelIndexList::const_iterator anIt = anIndices.begin(), aLast = anIndices.end(); anIt != aLast; anIt++)
457 QModelIndex anIndex = *anIt;
458 if (!myHelperExport.HasShape (anIndex))
460 aBuilder.Add (aComp, myHelperExport.GetShape (anIndex));