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 <Standard_WarningsDisable.hxx>
38 #include <QFileDialog>
39 #include <QGridLayout>
41 #include <QHeaderView>
45 #include <QStyledItemDelegate>
49 #include <Standard_WarningsRestore.hxx>
51 const int COLUMN_EXPORT_WIDTH = 20;
52 const int COLUMN_TYPE_WIDTH = 70;
53 const int COLUMN_POINTER_WIDTH = 110;
54 const int COLUMN_REFERENCE_WIDTH = 90;
55 const int COLUMN_EVOLUTION_WIDTH = 90;
57 // =======================================================================
58 // function : Constructor
60 // =======================================================================
61 DFBrowserPane_TNamingNamedShape::DFBrowserPane_TNamingNamedShape()
62 : DFBrowserPane_AttributePane(), myEvolutionTableView (0), myHelperExport (0)
64 getPaneModel()->SetColumnCount (5);
66 myEvolutionPaneModel = new DFBrowserPane_AttributePaneModel();
67 myEvolutionPaneModel->SetColumnCount (11);
68 QList<int> anItalicColumns;
69 anItalicColumns << 2 << 6;
70 myEvolutionPaneModel->SetItalicColumns (anItalicColumns);
71 QItemSelectionModel* aSelectionModel = new QItemSelectionModel (myEvolutionPaneModel);
72 mySelectionModels.push_back (aSelectionModel);
75 // =======================================================================
76 // function : CreateWidget
78 // =======================================================================
79 QWidget* DFBrowserPane_TNamingNamedShape::CreateWidget (QWidget* theParent)
81 QWidget* aMainWidget = new QWidget (theParent);
82 aMainWidget->setVisible (false);
84 myTableView = new DFBrowserPane_TableView (aMainWidget);
85 myTableView->GetTableView()->verticalHeader()->setVisible (false);
86 myTableView->SetModel (myPaneModel);
87 QTableView* aTableView = myTableView->GetTableView();
88 aTableView->setSelectionBehavior (QAbstractItemView::SelectItems);
89 std::list<QItemSelectionModel*>::const_iterator aSelectionModelsIt = mySelectionModels.begin();
90 aTableView->setSelectionModel (*aSelectionModelsIt);
93 aTableView->horizontalHeader()->setStretchLastSection (false);
94 aTableView->setColumnWidth (3, COLUMN_EXPORT_WIDTH);
95 aTableView->setColumnWidth (4, COLUMN_EXPORT_WIDTH);
96 DFBrowserPane_ItemDelegateButton* anItemDelegate = new DFBrowserPane_ItemDelegateButton (aTableView,
97 ":/icons/export_shape.png");
100 anItemDelegate->SetFreeRows (aRows);
101 QObject::connect (anItemDelegate, SIGNAL (buttonPressed (const QModelIndex&)),
102 &myHelperExport, SLOT (OnButtonPressed (const QModelIndex&)));
103 aTableView->setItemDelegateForColumn (3, anItemDelegate);
105 DFBrowserPane_ItemDelegateButton* anItemDelegate2 = new DFBrowserPane_ItemDelegateButton (aTableView,
106 ":/icons/folder_export.png");
107 anItemDelegate2->SetFreeRows (aRows);
108 aTableView->setItemDelegateForColumn (4, anItemDelegate2);
110 myEvolutionTableView = new DFBrowserPane_TableView (aMainWidget);
111 myEvolutionTableView->SetModel (myEvolutionPaneModel);
112 aTableView = myEvolutionTableView->GetTableView();
114 aTableView->setSelectionModel (*aSelectionModelsIt);
116 aTableView->horizontalHeader()->setStretchLastSection (false);
117 aTableView->setColumnWidth (5, COLUMN_EXPORT_WIDTH);
118 aTableView->setColumnWidth (10, COLUMN_EXPORT_WIDTH);
120 anItemDelegate = new DFBrowserPane_ItemDelegateButton (myEvolutionTableView->GetTableView(), ":/icons/export_shape.png");
121 QObject::connect (anItemDelegate, SIGNAL (buttonPressed (const QModelIndex&)),
122 &myHelperExport, SLOT (OnButtonPressed (const QModelIndex&)));
123 myEvolutionTableView->GetTableView()->setItemDelegateForColumn (5, anItemDelegate);
125 anItemDelegate = new DFBrowserPane_ItemDelegateButton (myEvolutionTableView->GetTableView(), ":/icons/export_shape.png");
126 QObject::connect (anItemDelegate, SIGNAL (buttonPressed (const QModelIndex&)),
127 &myHelperExport, SLOT (OnButtonPressed (const QModelIndex&)));
128 myEvolutionTableView->GetTableView()->setItemDelegateForColumn (10, anItemDelegate);
130 QGridLayout* aLay = new QGridLayout (aMainWidget);
131 aLay->setContentsMargins (0, 0, 0, 0);
132 aLay->addWidget (myTableView, 0, 0);
133 aLay->addWidget (myEvolutionTableView, 1, 0);
138 // =======================================================================
141 // =======================================================================
142 void DFBrowserPane_TNamingNamedShape::Init (const Handle(TDF_Attribute)& theAttribute)
144 Handle(TNaming_NamedShape) aShapeAttr = Handle(TNaming_NamedShape)::DownCast (theAttribute);
145 myHelperExport.Clear();
147 QList<QVariant> aValues;
148 aValues << "Version" << QString::number (aShapeAttr->Version()) << "" << "" << "";
149 aValues << "Evolution" << DFBrowserPane_Tools::ToName (DB_NS_TYPE, aShapeAttr->Evolution()).ToCString() << "" << "" << "";
151 NCollection_List<TopoDS_Shape> aShapes;
152 QList<int> aFreeRows;
155 TopoDS_Shape aShape = aShapeAttr->Get();
156 TCollection_AsciiString aShapeInfo = !aShape.IsNull() ? DFBrowserPane_Tools::GetPointerInfo (aShape.TShape()) : "";
157 aValues << "Shape" << aShapeInfo.ToCString() << DFBrowserPane_Tools::ShapeTypeInfo (aShape) << "" << "";
158 aShapes.Append (aShape);
162 TopoDS_Shape aCurrentShape = TNaming_Tool::CurrentShape (aShapeAttr);
163 TCollection_AsciiString aCurrentShapeInfo = !aCurrentShape.IsNull() ?
164 DFBrowserPane_Tools::GetPointerInfo (aCurrentShape.TShape()) : "";
165 aValues << "CurrentShape" << aCurrentShapeInfo.ToCString()
166 << DFBrowserPane_Tools::ShapeTypeInfo (aCurrentShape) << "" << "";
167 aShapes.Append (aCurrentShape);
168 if (aCurrentShape.IsNull())
171 TopoDS_Shape anOriginalShape = TNaming_Tool::OriginalShape (aShapeAttr);
172 TCollection_AsciiString anOriginalShapeInfo = !anOriginalShape.IsNull() ?
173 DFBrowserPane_Tools::GetPointerInfo (anOriginalShape.TShape()) : "";
174 aValues << "OriginalShape" << anOriginalShapeInfo.ToCString()
175 << DFBrowserPane_Tools::ShapeTypeInfo (anOriginalShape) << "" << "";
176 aShapes.Append (anOriginalShape);
177 if (anOriginalShape.IsNull())
181 DFBrowserPane_AttributePaneModel* aModel = getPaneModel();
182 aModel->Init (aValues);
185 QTableView* aTableView = myTableView->GetTableView();
186 for (int i = 0; i < aModel->columnCount(); i++)
188 if (i == 3 || i == 4)
189 dynamic_cast<DFBrowserPane_ItemDelegateButton*>(aTableView->itemDelegateForColumn(3))->SetFreeRows (aFreeRows);
191 aTableView->resizeColumnToContents (i);
194 QModelIndexList anIndices;
196 for (NCollection_List<TopoDS_Shape>::Iterator aShapeIt (aShapes); aShapeIt.More(); aShapeIt.Next(), aRowId++)
198 if (aShapeIt.Value().IsNull())
201 anIndices << aModel->index (aRowId, 1) << aModel->index (aRowId, 2) <<
202 aModel->index (aRowId, 3) << aModel->index (aRowId, 4);
203 myHelperExport.AddShape (aShapeIt.Value(), anIndices);
206 // evolution table view filling
209 bool aHasModified = false;
210 for (TNaming_Iterator aShapeAttrIt (aShapeAttr); aShapeAttrIt.More(); aShapeAttrIt.Next(), aRowId++)
212 const TopoDS_Shape& anOldShape = aShapeAttrIt.OldShape();
213 const TopoDS_Shape& aNewShape = aShapeAttrIt.NewShape();
215 Handle(TNaming_NamedShape) anOldAttr = TNaming_Tool::NamedShape (anOldShape, aShapeAttr->Label());
216 aValues << DFBrowserPane_Tools::ToName (DB_NS_TYPE, aShapeAttrIt.Evolution()).ToCString()
217 << (aShapeAttrIt.IsModification() ? "modified" : "-");
218 aHasModified = aHasModified | aShapeAttrIt.IsModification();
223 if (!anOldAttr.IsNull())
225 TDF_Label anOldLabel = anOldAttr->Label();
226 if (!anOldLabel.IsNull())
227 aLabelInfo = QString (DFBrowserPane_Tools::GetEntry (anOldLabel).ToCString());
229 if (!aNewShape.IsNull())
230 aValues << DFBrowserPane_Tools::GetPointerInfo (aNewShape.TShape()->This()).ToCString()
231 << DFBrowserPane_Tools::ShapeTypeInfo (aNewShape)
234 aValues << "-" << "-" << "";
236 if (!anOldShape.IsNull())
237 aValues << DFBrowserPane_Tools::GetPointerInfo (anOldShape.TShape()->This()).ToCString()
238 << DFBrowserPane_Tools::ShapeTypeInfo (anOldShape)
242 aValues << "-" << "-" << "-" << "";
245 if (myEvolutionTableView)
247 myEvolutionTableView->setVisible (aValues.size() > 0);
248 myEvolutionTableView->GetTableView()->setColumnHidden (1, !aHasModified);
249 myEvolutionPaneModel->Init (aValues);
252 for (TNaming_Iterator aShapeAttrIt (aShapeAttr); aShapeAttrIt.More(); aShapeAttrIt.Next(), aRowId++)
254 const TopoDS_Shape& anOldShape = aShapeAttrIt.OldShape();
255 const TopoDS_Shape& aNewShape = aShapeAttrIt.NewShape();
257 if (!aNewShape.IsNull())
260 anIndices << myEvolutionPaneModel->index (aRowId, 3) << myEvolutionPaneModel->index (aRowId, 4)
261 << myEvolutionPaneModel->index (aRowId, 5);
262 myHelperExport.AddShape (aNewShape, anIndices);
264 if (!anOldShape.IsNull())
267 anIndices << myEvolutionPaneModel->index (aRowId, 7) << myEvolutionPaneModel->index (aRowId, 8)
268 << myEvolutionPaneModel->index (aRowId, 10);
269 myHelperExport.AddShape (anOldShape, anIndices);
272 for (int i = 0; i < myEvolutionPaneModel->columnCount(); i++)
274 if (i == 5 || i == 10) continue;
275 myEvolutionTableView->GetTableView()->resizeColumnToContents (i);
280 // =======================================================================
281 // function : GetValues
283 // =======================================================================
284 void DFBrowserPane_TNamingNamedShape::GetValues (const Handle(TDF_Attribute)&, QList<QVariant>&)
288 // =======================================================================
289 // function : GetAttributeInfo
291 // =======================================================================
292 QVariant DFBrowserPane_TNamingNamedShape::GetAttributeInfo (const Handle(TDF_Attribute)& theAttribute, int theRole,
295 if (theColumnId != 0)
296 return DFBrowserPane_AttributePane::GetAttributeInfo (theAttribute, theRole, theColumnId);
300 case Qt::DecorationRole: return QIcon (":/icons/named_shape.png");
301 case DFBrowserPane_ItemRole_Decoration_40x40: return QIcon (":/icons/named_shape_40x40.png");
302 case Qt::ForegroundRole:
305 Handle(TNaming_NamedShape) anAttribute = Handle(TNaming_NamedShape)::DownCast (theAttribute);
306 if (!anAttribute.IsNull())
307 aShape = anAttribute->Get();
309 return QColor (Qt::black);
311 return QColor (aShape.Orientation() == TopAbs_FORWARD ? Qt::darkGray :
312 aShape.Orientation() == TopAbs_REVERSED ? QColor (Qt::gray) : Qt::black);
317 return DFBrowserPane_AttributePane::GetAttributeInfo (theAttribute, theRole, theColumnId);
320 // =======================================================================
321 // function : GetShortAttributeInfo
323 // =======================================================================
324 void DFBrowserPane_TNamingNamedShape::GetShortAttributeInfo (const Handle(TDF_Attribute)& theAttribute,
325 QList<QVariant>& theValues)
327 Handle(TNaming_NamedShape) aShapeAttribute = Handle(TNaming_NamedShape)::DownCast (theAttribute);
329 if (aShapeAttribute->Get().IsNull())
330 theValues.append ("EMPTY SHAPE");
333 theValues.append (QString ("%1 : %2").arg (DFBrowserPane_Tools::ToName (DB_SHAPE_TYPE, aShapeAttribute->Get().ShapeType()).ToCString())
334 .arg (DFBrowserPane_Tools::ToName (DB_NS_TYPE, aShapeAttribute->Evolution()).ToCString()));
338 // =======================================================================
339 // function : GetSelectionKind
341 // =======================================================================
342 int DFBrowserPane_TNamingNamedShape::GetSelectionKind (QItemSelectionModel* theModel)
344 int aKind = DFBrowserPane_AttributePaneAPI::GetSelectionKind (theModel);
346 QTableView* aTableView = myTableView->GetTableView();
347 if (aTableView->selectionModel() != theModel)
350 QModelIndexList aSelectedIndices = theModel->selectedIndexes();
351 if (aSelectedIndices.size() != 1)
354 QModelIndex aSelectedIndex = aSelectedIndices.first();
355 int aRow = aSelectedIndex.row();
356 if (aRow == 0 || aRow == 1)
359 if (aSelectedIndex.column() == 4)
360 aKind = DFBrowserPane_SelectionKind_ExportToShapeViewer;
365 // =======================================================================
366 // function : GetSelectionParameters
368 // =======================================================================
369 void DFBrowserPane_TNamingNamedShape::GetSelectionParameters (QItemSelectionModel* theModel,
370 NCollection_List<Handle(Standard_Transient)>& theParameters)
372 QTableView* aTableView = myTableView->GetTableView();
373 if (aTableView->selectionModel() != theModel)
376 QModelIndexList aSelectedIndices = theModel->selectedIndexes();
377 if (aSelectedIndices.size() != 1)
380 QModelIndex aSelectedIndex = aSelectedIndices.first();
381 if (aSelectedIndex.column() != 4)
384 const TopoDS_Shape& aShape = myHelperExport.GetShape (aSelectedIndex);
387 theParameters.Append (aShape.TShape());
390 // =======================================================================
391 // function : GetReferences
393 // =======================================================================
394 void DFBrowserPane_TNamingNamedShape::GetReferences (const Handle(TDF_Attribute)& theAttribute,
395 NCollection_List<TDF_Label>& theRefLabels,
396 Handle(Standard_Transient)& theRefPresentation)
398 if (!myEvolutionTableView)
400 QStringList aSelectedEntries = DFBrowserPane_TableView::GetSelectedColumnValues (
401 myEvolutionTableView->GetTableView(), 9);
403 Handle(TNaming_NamedShape) aShapeAttr = Handle(TNaming_NamedShape)::DownCast (theAttribute);
404 for (TNaming_Iterator aShapeAttrIt (aShapeAttr); aShapeAttrIt.More(); aShapeAttrIt.Next())
406 const TopoDS_Shape& anOldShape = aShapeAttrIt.OldShape();
408 Handle(TNaming_NamedShape) anOldAttr = TNaming_Tool::NamedShape (anOldShape, aShapeAttr->Label());
410 if (!anOldAttr.IsNull())
412 TDF_Label anOldLabel = anOldAttr->Label();
413 if (!anOldLabel.IsNull())
415 if (aSelectedEntries.contains (DFBrowserPane_Tools::GetEntry (anOldLabel).ToCString()))
416 theRefLabels.Append (anOldLabel);
420 TopoDS_Shape aShape = getSelectedShapes();
421 if (!aShape.IsNull())
422 theRefPresentation = new AIS_Shape (aShape);
425 // =======================================================================
426 // function : GetPresentation
428 // =======================================================================
429 Handle(Standard_Transient) DFBrowserPane_TNamingNamedShape::GetPresentation (const Handle(TDF_Attribute)& theAttribute)
431 Handle(Standard_Transient) aPresentation;
432 Handle(TNaming_NamedShape) aShapeAttr = Handle(TNaming_NamedShape)::DownCast (theAttribute);
433 if (aShapeAttr.IsNull())
434 return aPresentation;
436 TopoDS_Shape aShape = aShapeAttr->Get();
438 return aPresentation;
440 aPresentation = new AIS_Shape (aShape);
441 return aPresentation;
444 // =======================================================================
445 // function : getSelectedShapes
447 // =======================================================================
448 TopoDS_Shape DFBrowserPane_TNamingNamedShape::getSelectedShapes()
452 if (!myTableView && !myEvolutionTableView)
455 // table view selected shapes
456 QItemSelectionModel* aTableViewSelModel = myTableView->GetTableView()->selectionModel();
457 QModelIndexList anIndices = aTableViewSelModel->selectedIndexes();
459 BRep_Builder aBuilder;
460 TopoDS_Compound aComp;
461 aBuilder.MakeCompound (aComp);
462 bool aHasShapes = false;
463 for (QModelIndexList::const_iterator anIt = anIndices.begin(), aLast = anIndices.end(); anIt != aLast; anIt++)
465 QModelIndex anIndex = *anIt;
466 if (!myHelperExport.HasShape (anIndex))
468 aBuilder.Add (aComp, myHelperExport.GetShape (anIndex));
472 // evolution table selected shapes
473 aTableViewSelModel = myEvolutionTableView->GetTableView()->selectionModel();
475 anIndices = aTableViewSelModel->selectedIndexes();
476 for (QModelIndexList::const_iterator anIt = anIndices.begin(), aLast = anIndices.end(); anIt != aLast; anIt++)
478 QModelIndex anIndex = *anIt;
479 if (!myHelperExport.HasShape (anIndex))
481 aBuilder.Add (aComp, myHelperExport.GetShape (anIndex));