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>
25 #include <inspector/TInspectorAPI_PluginParameters.hxx>
27 #include <AIS_InteractiveObject.hxx>
28 #include <AIS_Shape.hxx>
29 #include <BRep_Builder.hxx>
30 #include <BRepTools.hxx>
31 #include <TNaming_Builder.hxx>
32 #include <TNaming_Iterator.hxx>
33 #include <TNaming_NamedShape.hxx>
34 #include <TNaming_Tool.hxx>
35 #include <TopoDS_Shape.hxx>
36 #include <TopoDS_Compound.hxx>
38 #include <Standard_WarningsDisable.hxx>
39 #include <QFileDialog>
40 #include <QGridLayout>
42 #include <QHeaderView>
46 #include <QStyledItemDelegate>
50 #include <Standard_WarningsRestore.hxx>
52 const int COLUMN_EXPORT_WIDTH = 20;
53 const int COLUMN_TYPE_WIDTH = 70;
54 const int COLUMN_POINTER_WIDTH = 110;
55 const int COLUMN_REFERENCE_WIDTH = 90;
56 const int COLUMN_EVOLUTION_WIDTH = 90;
58 // =======================================================================
59 // function : Constructor
61 // =======================================================================
62 DFBrowserPane_TNamingNamedShape::DFBrowserPane_TNamingNamedShape()
63 : DFBrowserPane_AttributePane(), myEvolutionTableView (0), myHelperExport (0)
65 getPaneModel()->SetColumnCount (5);
67 myEvolutionPaneModel = new DFBrowserPane_AttributePaneModel();
68 myEvolutionPaneModel->SetColumnCount (11);
69 QList<int> anItalicColumns;
70 anItalicColumns << 2 << 6;
71 myEvolutionPaneModel->SetItalicColumns (anItalicColumns);
72 QItemSelectionModel* aSelectionModel = new QItemSelectionModel (myEvolutionPaneModel);
73 mySelectionModels.push_back (aSelectionModel);
76 // =======================================================================
77 // function : CreateWidget
79 // =======================================================================
80 QWidget* DFBrowserPane_TNamingNamedShape::CreateWidget (QWidget* theParent)
82 QWidget* aMainWidget = new QWidget (theParent);
83 aMainWidget->setVisible (false);
85 myTableView = new DFBrowserPane_TableView (aMainWidget);
86 myTableView->GetTableView()->verticalHeader()->setVisible (false);
87 myTableView->SetModel (myPaneModel);
88 QTableView* aTableView = myTableView->GetTableView();
89 aTableView->setSelectionBehavior (QAbstractItemView::SelectItems);
90 std::list<QItemSelectionModel*>::const_iterator aSelectionModelsIt = mySelectionModels.begin();
91 aTableView->setSelectionModel (*aSelectionModelsIt);
94 aTableView->horizontalHeader()->setStretchLastSection (false);
95 aTableView->setColumnWidth (3, COLUMN_EXPORT_WIDTH);
96 aTableView->setColumnWidth (4, COLUMN_EXPORT_WIDTH);
97 DFBrowserPane_ItemDelegateButton* anItemDelegate = new DFBrowserPane_ItemDelegateButton (aTableView,
98 ":/icons/export_shape.png");
101 anItemDelegate->SetFreeRows (aRows);
102 QObject::connect (anItemDelegate, SIGNAL (buttonPressed (const QModelIndex&)),
103 &myHelperExport, SLOT (OnButtonPressed (const QModelIndex&)));
104 aTableView->setItemDelegateForColumn (3, anItemDelegate);
106 DFBrowserPane_ItemDelegateButton* anItemDelegate2 = new DFBrowserPane_ItemDelegateButton (aTableView,
107 ":/icons/folder_export.png");
108 anItemDelegate2->SetFreeRows (aRows);
109 aTableView->setItemDelegateForColumn (4, anItemDelegate2);
111 myEvolutionTableView = new DFBrowserPane_TableView (aMainWidget);
112 myEvolutionTableView->SetModel (myEvolutionPaneModel);
113 aTableView = myEvolutionTableView->GetTableView();
115 aTableView->setSelectionModel (*aSelectionModelsIt);
117 aTableView->horizontalHeader()->setStretchLastSection (false);
118 aTableView->setColumnWidth (5, COLUMN_EXPORT_WIDTH);
119 aTableView->setColumnWidth (10, COLUMN_EXPORT_WIDTH);
121 anItemDelegate = new DFBrowserPane_ItemDelegateButton (myEvolutionTableView->GetTableView(), ":/icons/export_shape.png");
122 QObject::connect (anItemDelegate, SIGNAL (buttonPressed (const QModelIndex&)),
123 &myHelperExport, SLOT (OnButtonPressed (const QModelIndex&)));
124 myEvolutionTableView->GetTableView()->setItemDelegateForColumn (5, anItemDelegate);
126 anItemDelegate = new DFBrowserPane_ItemDelegateButton (myEvolutionTableView->GetTableView(), ":/icons/export_shape.png");
127 QObject::connect (anItemDelegate, SIGNAL (buttonPressed (const QModelIndex&)),
128 &myHelperExport, SLOT (OnButtonPressed (const QModelIndex&)));
129 myEvolutionTableView->GetTableView()->setItemDelegateForColumn (10, anItemDelegate);
131 QGridLayout* aLay = new QGridLayout (aMainWidget);
132 aLay->setContentsMargins (0, 0, 0, 0);
133 aLay->addWidget (myTableView, 0, 0);
134 aLay->addWidget (myEvolutionTableView, 1, 0);
139 // =======================================================================
142 // =======================================================================
143 void DFBrowserPane_TNamingNamedShape::Init (const Handle(TDF_Attribute)& theAttribute)
145 Handle(TNaming_NamedShape) aShapeAttr = Handle(TNaming_NamedShape)::DownCast (theAttribute);
146 myHelperExport.Clear();
148 QList<QVariant> aValues;
149 aValues << "Version" << QString::number (aShapeAttr->Version()) << "" << "" << "";
150 aValues << "Evolution" << DFBrowserPane_Tools::ToName (DB_NS_TYPE, aShapeAttr->Evolution()).ToCString() << "" << "" << "";
152 NCollection_List<TopoDS_Shape> aShapes;
153 QList<int> aFreeRows;
156 TopoDS_Shape aShape = aShapeAttr->Get();
157 TCollection_AsciiString aShapeInfo = !aShape.IsNull() ? DFBrowserPane_Tools::GetPointerInfo (aShape.TShape()) : "";
158 aValues << "Shape" << aShapeInfo.ToCString() << DFBrowserPane_Tools::ShapeTypeInfo (aShape) << "" << "";
159 aShapes.Append (aShape);
163 TopoDS_Shape aCurrentShape = TNaming_Tool::CurrentShape (aShapeAttr);
164 TCollection_AsciiString aCurrentShapeInfo = !aCurrentShape.IsNull() ?
165 DFBrowserPane_Tools::GetPointerInfo (aCurrentShape.TShape()) : "";
166 aValues << "CurrentShape" << aCurrentShapeInfo.ToCString()
167 << DFBrowserPane_Tools::ShapeTypeInfo (aCurrentShape) << "" << "";
168 aShapes.Append (aCurrentShape);
169 if (aCurrentShape.IsNull())
172 TopoDS_Shape anOriginalShape = TNaming_Tool::OriginalShape (aShapeAttr);
173 TCollection_AsciiString anOriginalShapeInfo = !anOriginalShape.IsNull() ?
174 DFBrowserPane_Tools::GetPointerInfo (anOriginalShape.TShape()) : "";
175 aValues << "OriginalShape" << anOriginalShapeInfo.ToCString()
176 << DFBrowserPane_Tools::ShapeTypeInfo (anOriginalShape) << "" << "";
177 aShapes.Append (anOriginalShape);
178 if (anOriginalShape.IsNull())
182 DFBrowserPane_AttributePaneModel* aModel = getPaneModel();
183 aModel->Init (aValues);
186 QTableView* aTableView = myTableView->GetTableView();
187 for (int i = 0; i < aModel->columnCount(); i++)
189 if (i == 3 || i == 4)
190 dynamic_cast<DFBrowserPane_ItemDelegateButton*>(aTableView->itemDelegateForColumn(3))->SetFreeRows (aFreeRows);
192 aTableView->resizeColumnToContents (i);
195 QModelIndexList anIndices;
197 for (NCollection_List<TopoDS_Shape>::Iterator aShapeIt (aShapes); aShapeIt.More(); aShapeIt.Next(), aRowId++)
199 if (aShapeIt.Value().IsNull())
202 anIndices << aModel->index (aRowId, 1) << aModel->index (aRowId, 2) <<
203 aModel->index (aRowId, 3) << aModel->index (aRowId, 4);
204 myHelperExport.AddShape (aShapeIt.Value(), anIndices);
207 // evolution table view filling
210 bool aHasModified = false;
211 for (TNaming_Iterator aShapeAttrIt (aShapeAttr); aShapeAttrIt.More(); aShapeAttrIt.Next(), aRowId++)
213 const TopoDS_Shape& anOldShape = aShapeAttrIt.OldShape();
214 const TopoDS_Shape& aNewShape = aShapeAttrIt.NewShape();
216 Handle(TNaming_NamedShape) anOldAttr = TNaming_Tool::NamedShape (anOldShape, aShapeAttr->Label());
217 aValues << DFBrowserPane_Tools::ToName (DB_NS_TYPE, aShapeAttrIt.Evolution()).ToCString()
218 << (aShapeAttrIt.IsModification() ? "modified" : "-");
219 aHasModified = aHasModified | aShapeAttrIt.IsModification();
224 if (!anOldAttr.IsNull())
226 TDF_Label anOldLabel = anOldAttr->Label();
227 if (!anOldLabel.IsNull())
228 aLabelInfo = QString (DFBrowserPane_Tools::GetEntry (anOldLabel).ToCString());
230 if (!aNewShape.IsNull())
231 aValues << DFBrowserPane_Tools::GetPointerInfo (aNewShape.TShape()->This()).ToCString()
232 << DFBrowserPane_Tools::ShapeTypeInfo (aNewShape)
235 aValues << "-" << "-" << "";
237 if (!anOldShape.IsNull())
238 aValues << DFBrowserPane_Tools::GetPointerInfo (anOldShape.TShape()->This()).ToCString()
239 << DFBrowserPane_Tools::ShapeTypeInfo (anOldShape)
243 aValues << "-" << "-" << "-" << "";
246 if (myEvolutionTableView)
248 myEvolutionTableView->setVisible (aValues.size() > 0);
249 myEvolutionTableView->GetTableView()->setColumnHidden (1, !aHasModified);
250 myEvolutionPaneModel->Init (aValues);
253 for (TNaming_Iterator aShapeAttrIt (aShapeAttr); aShapeAttrIt.More(); aShapeAttrIt.Next(), aRowId++)
255 const TopoDS_Shape& anOldShape = aShapeAttrIt.OldShape();
256 const TopoDS_Shape& aNewShape = aShapeAttrIt.NewShape();
258 if (!aNewShape.IsNull())
261 anIndices << myEvolutionPaneModel->index (aRowId, 3) << myEvolutionPaneModel->index (aRowId, 4)
262 << myEvolutionPaneModel->index (aRowId, 5);
263 myHelperExport.AddShape (aNewShape, anIndices);
265 if (!anOldShape.IsNull())
268 anIndices << myEvolutionPaneModel->index (aRowId, 7) << myEvolutionPaneModel->index (aRowId, 8)
269 << myEvolutionPaneModel->index (aRowId, 10);
270 myHelperExport.AddShape (anOldShape, anIndices);
273 for (int i = 0; i < myEvolutionPaneModel->columnCount(); i++)
275 if (i == 5 || i == 10) continue;
276 myEvolutionTableView->GetTableView()->resizeColumnToContents (i);
281 // =======================================================================
282 // function : GetValues
284 // =======================================================================
285 void DFBrowserPane_TNamingNamedShape::GetValues (const Handle(TDF_Attribute)&, QList<QVariant>&)
289 // =======================================================================
290 // function : GetAttributeInfo
292 // =======================================================================
293 QVariant DFBrowserPane_TNamingNamedShape::GetAttributeInfo (const Handle(TDF_Attribute)& theAttribute, int theRole,
296 if (theColumnId != 0)
297 return DFBrowserPane_AttributePane::GetAttributeInfo (theAttribute, theRole, theColumnId);
301 case Qt::DecorationRole: return QIcon (":/icons/named_shape.png");
302 case DFBrowserPane_ItemRole_Decoration_40x40: return QIcon (":/icons/named_shape_40x40.png");
303 case Qt::ForegroundRole:
306 Handle(TNaming_NamedShape) anAttribute = Handle(TNaming_NamedShape)::DownCast (theAttribute);
307 if (!anAttribute.IsNull())
308 aShape = anAttribute->Get();
310 return QColor (Qt::black);
312 return QColor (aShape.Orientation() == TopAbs_FORWARD ? Qt::darkGray :
313 aShape.Orientation() == TopAbs_REVERSED ? QColor (Qt::gray) : Qt::black);
318 return DFBrowserPane_AttributePane::GetAttributeInfo (theAttribute, theRole, theColumnId);
321 // =======================================================================
322 // function : GetShortAttributeInfo
324 // =======================================================================
325 void DFBrowserPane_TNamingNamedShape::GetShortAttributeInfo (const Handle(TDF_Attribute)& theAttribute,
326 QList<QVariant>& theValues)
328 Handle(TNaming_NamedShape) aShapeAttribute = Handle(TNaming_NamedShape)::DownCast (theAttribute);
330 if (aShapeAttribute->Get().IsNull())
331 theValues.append ("EMPTY SHAPE");
334 theValues.append (QString ("%1 : %2").arg (DFBrowserPane_Tools::ToName (DB_SHAPE_TYPE, aShapeAttribute->Get().ShapeType()).ToCString())
335 .arg (DFBrowserPane_Tools::ToName (DB_NS_TYPE, aShapeAttribute->Evolution()).ToCString()));
339 // =======================================================================
340 // function : GetSelectionKind
342 // =======================================================================
343 int DFBrowserPane_TNamingNamedShape::GetSelectionKind (QItemSelectionModel* theModel)
345 int aKind = DFBrowserPane_AttributePaneAPI::GetSelectionKind (theModel);
347 QTableView* aTableView = myTableView->GetTableView();
348 if (aTableView->selectionModel() != theModel)
351 QModelIndexList aSelectedIndices = theModel->selectedIndexes();
352 if (aSelectedIndices.size() != 1)
355 QModelIndex aSelectedIndex = aSelectedIndices.first();
356 int aRow = aSelectedIndex.row();
357 if (aRow == 0 || aRow == 1)
360 if (aSelectedIndex.column() == 4)
361 aKind = DFBrowserPane_SelectionKind_ExportToShapeViewer;
366 // =======================================================================
367 // function : GetSelectionParameters
369 // =======================================================================
370 void DFBrowserPane_TNamingNamedShape::GetSelectionParameters (QItemSelectionModel* theModel,
371 NCollection_List<Handle(Standard_Transient)>& theParameters,
372 NCollection_List<TCollection_AsciiString>& theItemNames)
374 QTableView* aTableView = myTableView->GetTableView();
375 if (aTableView->selectionModel() != theModel)
378 QModelIndexList aSelectedIndices = theModel->selectedIndexes();
379 if (aSelectedIndices.size() != 1)
382 QModelIndex aSelectedIndex = aSelectedIndices.first();
383 if (aSelectedIndex.column() != 4)
386 const TopoDS_Shape& aShape = myHelperExport.GetShape (aSelectedIndex);
389 theParameters.Append (aShape.TShape());
390 theItemNames.Append (TInspectorAPI_PluginParameters::ParametersToString (aShape));
393 // =======================================================================
394 // function : GetReferences
396 // =======================================================================
397 void DFBrowserPane_TNamingNamedShape::GetReferences (const Handle(TDF_Attribute)& theAttribute,
398 NCollection_List<TDF_Label>& theRefLabels,
399 Handle(Standard_Transient)& theRefPresentation)
401 if (!myEvolutionTableView)
403 QStringList aSelectedEntries = DFBrowserPane_TableView::GetSelectedColumnValues (
404 myEvolutionTableView->GetTableView(), 9);
406 Handle(TNaming_NamedShape) aShapeAttr = Handle(TNaming_NamedShape)::DownCast (theAttribute);
407 for (TNaming_Iterator aShapeAttrIt (aShapeAttr); aShapeAttrIt.More(); aShapeAttrIt.Next())
409 const TopoDS_Shape& anOldShape = aShapeAttrIt.OldShape();
411 Handle(TNaming_NamedShape) anOldAttr = TNaming_Tool::NamedShape (anOldShape, aShapeAttr->Label());
413 if (!anOldAttr.IsNull())
415 TDF_Label anOldLabel = anOldAttr->Label();
416 if (!anOldLabel.IsNull())
418 if (aSelectedEntries.contains (DFBrowserPane_Tools::GetEntry (anOldLabel).ToCString()))
419 theRefLabels.Append (anOldLabel);
423 TopoDS_Shape aShape = getSelectedShapes();
424 if (!aShape.IsNull())
425 theRefPresentation = new AIS_Shape (aShape);
428 // =======================================================================
429 // function : GetPresentation
431 // =======================================================================
432 Handle(Standard_Transient) DFBrowserPane_TNamingNamedShape::GetPresentation (const Handle(TDF_Attribute)& theAttribute)
434 Handle(Standard_Transient) aPresentation;
435 Handle(TNaming_NamedShape) aShapeAttr = Handle(TNaming_NamedShape)::DownCast (theAttribute);
436 if (aShapeAttr.IsNull())
437 return aPresentation;
439 TopoDS_Shape aShape = aShapeAttr->Get();
441 return aPresentation;
443 aPresentation = new AIS_Shape (aShape);
444 return aPresentation;
447 // =======================================================================
448 // function : getSelectedShapes
450 // =======================================================================
451 TopoDS_Shape DFBrowserPane_TNamingNamedShape::getSelectedShapes()
455 if (!myTableView && !myEvolutionTableView)
458 // table view selected shapes
459 QItemSelectionModel* aTableViewSelModel = myTableView->GetTableView()->selectionModel();
460 QModelIndexList anIndices = aTableViewSelModel->selectedIndexes();
462 BRep_Builder aBuilder;
463 TopoDS_Compound aComp;
464 aBuilder.MakeCompound (aComp);
465 bool aHasShapes = false;
466 for (QModelIndexList::const_iterator anIt = anIndices.begin(), aLast = anIndices.end(); anIt != aLast; anIt++)
468 QModelIndex anIndex = *anIt;
469 if (!myHelperExport.HasShape (anIndex))
471 aBuilder.Add (aComp, myHelperExport.GetShape (anIndex));
475 // evolution table selected shapes
476 aTableViewSelModel = myEvolutionTableView->GetTableView()->selectionModel();
478 anIndices = aTableViewSelModel->selectedIndexes();
479 for (QModelIndexList::const_iterator anIt = anIndices.begin(), aLast = anIndices.end(); anIt != aLast; anIt++)
481 QModelIndex anIndex = *anIt;
482 if (!myHelperExport.HasShape (anIndex))
484 aBuilder.Add (aComp, myHelperExport.GetShape (anIndex));