1 // Created on: 2021-04-27
2 // Created by: Natalia ERMOLAEVA
3 // Copyright (c) 2021 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/MessageModel_Actions.hxx>
18 #include <inspector/MessageModel_ItemReport.hxx>
19 #include <inspector/MessageModel_ItemRoot.hxx>
20 #include <inspector/MessageModel_ItemAlert.hxx>
21 #include <inspector/MessageModel_TreeModel.hxx>
22 #include <inspector/TInspectorAPI_PluginParameters.hxx>
23 #include <inspector/ViewControl_Tools.hxx>
25 #include <BRepBuilderAPI_MakeEdge.hxx>
26 #include <Message.hxx>
27 #include <Message_AlertExtended.hxx>
28 #include <Message_Messenger.hxx>
29 #include <Message_PrinterToReport.hxx>
30 #include <OSD_Chronometer.hxx>
31 #include <Quantity_Color.hxx>
32 #include <Quantity_ColorRGBA.hxx>
33 #include <TCollection_AsciiString.hxx>
34 #include <TopoDS_AlertAttribute.hxx>
36 #include <Standard_WarningsDisable.hxx>
38 #include <QFileDialog>
39 #include <QItemSelectionModel>
41 #include <QMessageBox>
43 #include <Standard_WarningsRestore.hxx>
45 // =======================================================================
46 // function : Constructor
48 // =======================================================================
49 MessageModel_Actions::MessageModel_Actions (QWidget* theParent,
50 MessageModel_TreeModel* theTreeModel,
51 QItemSelectionModel* theModel)
52 : QObject (theParent), myTreeModel (theTreeModel), mySelectionModel (theModel)
54 myActions.insert (MessageModel_ActionType_Activate,
55 ViewControl_Tools::CreateAction (tr ("Activate"), SLOT (OnActivateReport()), parent(), this));
56 myActions.insert (MessageModel_ActionType_Deactivate,
57 ViewControl_Tools::CreateAction (tr ("Deactivate"), SLOT (OnDeactivateReport()), parent(), this));
58 myActions.insert (MessageModel_ActionType_Clear,
59 ViewControl_Tools::CreateAction (tr ("Clear"), SLOT (OnClearReport()), parent(), this));
60 myActions.insert (MessageModel_ActionType_ExportToShapeView,
61 ViewControl_Tools::CreateAction (tr ("Export to ShapeView"), SLOT (OnExportToShapeView()), parent(), this));
64 // =======================================================================
65 // function : GetAction
67 // =======================================================================
68 QAction* MessageModel_Actions::GetAction (const MessageModel_ActionType& theType)
70 return myActions.contains (theType) ? myActions[theType] : 0;
73 // =======================================================================
74 // function : AddMenuActions
76 // =======================================================================
77 void MessageModel_Actions::AddMenuActions (const QModelIndexList& theSelectedIndices, QMenu* theMenu)
79 MessageModel_ItemRootPtr aRootItem;
80 MessageModel_ItemReportPtr aReportItem;
81 MessageModel_ItemAlertPtr anAlertItem;
82 for (QModelIndexList::const_iterator aSelIt = theSelectedIndices.begin(); aSelIt != theSelectedIndices.end(); aSelIt++)
84 QModelIndex anIndex = *aSelIt;
85 if (anIndex.column() != 0)
88 TreeModel_ItemBasePtr anItemBase = TreeModel_ModelBase::GetItemByIndex (anIndex);
92 aRootItem = itemDynamicCast<MessageModel_ItemRoot> (anItemBase);
96 aReportItem = itemDynamicCast<MessageModel_ItemReport> (anItemBase);
100 anAlertItem = itemDynamicCast<MessageModel_ItemAlert> (anItemBase);
105 if (aReportItem && !aReportItem->GetReport().IsNull())
107 theMenu->addAction (myActions[MessageModel_ActionType_Deactivate]);
108 theMenu->addAction (myActions[MessageModel_ActionType_Activate]);
109 theMenu->addAction (myActions[MessageModel_ActionType_Clear]);
111 else if (anAlertItem)
113 theMenu->addAction (myActions[MessageModel_ActionType_ExportToShapeView]);
116 theMenu->addSeparator();
119 // =======================================================================
120 // function : getSelectedReport
122 // =======================================================================
123 Handle(Message_Report) MessageModel_Actions::getSelectedReport (QModelIndex& theReportIndex) const
125 MessageModel_ItemReportPtr aReportItem;
126 QModelIndexList aSelectedIndices = mySelectionModel->selectedIndexes();
127 for (QModelIndexList::const_iterator aSelIt = aSelectedIndices.begin(); aSelIt != aSelectedIndices.end(); aSelIt++)
129 QModelIndex anIndex = *aSelIt;
130 if (anIndex.column() != 0)
133 TreeModel_ItemBasePtr anItemBase = TreeModel_ModelBase::GetItemByIndex (anIndex);
137 aReportItem = itemDynamicCast<MessageModel_ItemReport> (anItemBase);
138 theReportIndex = anIndex;
145 return aReportItem->GetReport();
148 // =======================================================================
149 // function : OnActivateReport
151 // =======================================================================
152 static Handle(Message_PrinterToReport) MyPrinterToReport;
153 static Message_SequenceOfPrinters MyDeactivatedPrinters;
155 void MessageModel_Actions::OnActivateReport()
157 if (MyPrinterToReport.IsNull())
158 MyPrinterToReport = new Message_PrinterToReport();
160 if (MyPrinterToReport->Report()->IsActiveInMessenger())
163 MyDeactivatedPrinters = Message::DefaultMessenger()->Printers();
164 Message::DefaultMessenger()->ChangePrinters().Clear();
166 Message::DefaultMessenger()->AddPrinter (MyPrinterToReport);
167 Message::DefaultReport()->UpdateActiveInMessenger();
169 myTreeModel->UpdateTreeModel();
172 // =======================================================================
173 // function : OnDeactivateReport
175 // =======================================================================
176 void MessageModel_Actions::OnDeactivateReport()
178 if (MyPrinterToReport.IsNull() || !MyPrinterToReport->Report()->IsActiveInMessenger())
181 Message::DefaultMessenger()->RemovePrinter (MyPrinterToReport);
182 Message::DefaultMessenger()->ChangePrinters().Assign (MyDeactivatedPrinters);
184 myTreeModel->UpdateTreeModel();
187 // =======================================================================
188 // function : OnClearReport
190 // =======================================================================
191 void MessageModel_Actions::OnClearReport()
193 QModelIndex aReportIndex;
194 Handle(Message_Report) aReport = getSelectedReport (aReportIndex);
195 if (aReport.IsNull())
199 myTreeModel->UpdateTreeModel();
202 // =======================================================================
203 // function : OnExportToShapeView
205 // =======================================================================
206 void MessageModel_Actions::OnExportToShapeView()
208 TCollection_AsciiString aPluginName ("TKShapeView");
210 NCollection_List<Handle(Standard_Transient)> aPluginParameters;
211 if (myParameters->FindParameters (aPluginName))
212 aPluginParameters = myParameters->Parameters (aPluginName);
213 NCollection_List<TCollection_AsciiString> anItemNames;
214 if (myParameters->FindSelectedNames (aPluginName))
215 anItemNames = myParameters->GetSelectedNames (aPluginName);
217 QModelIndexList aSelectedIndices = mySelectionModel->selectedIndexes();
218 QStringList anExportedPointers;
219 for (QModelIndexList::const_iterator aSelIt = aSelectedIndices.begin(); aSelIt != aSelectedIndices.end(); aSelIt++)
221 QModelIndex anIndex = *aSelIt;
222 if (anIndex.column() != 0)
225 TreeModel_ItemBasePtr anItemBase = TreeModel_ModelBase::GetItemByIndex (anIndex);
229 MessageModel_ItemAlertPtr anAlertItem = itemDynamicCast<MessageModel_ItemAlert> (anItemBase);
233 Handle(Message_Alert) anAlert = anAlertItem->GetAlert();
234 if (anAlert.IsNull())
237 Handle(Message_AlertExtended) anExtAlert = Handle(Message_AlertExtended)::DownCast (anAlert);
238 if (anExtAlert.IsNull())
241 Handle(Message_Attribute) anAttribute = anExtAlert->Attribute();
242 if (anAttribute.IsNull())
245 if (!anAttribute->IsKind (STANDARD_TYPE (TopoDS_AlertAttribute)))
248 const TopoDS_Shape aShape = Handle(TopoDS_AlertAttribute)::DownCast (anAttribute)->GetShape();
251 aPluginParameters.Append (aShape.TShape());
252 anItemNames.Append (TInspectorAPI_PluginParameters::ParametersToString (aShape));
254 anExportedPointers.append (Standard_Dump::GetPointerInfo (aShape.TShape(), true).ToCString());
257 if (anExportedPointers.empty())
259 myParameters->SetSelectedNames (aPluginName, anItemNames);
260 myParameters->SetParameters (aPluginName, aPluginParameters);
261 QMessageBox::information (0, "Information", QString ("TShapes '%1' are sent to %2 tool.")
262 .arg (anExportedPointers.join (", ")).arg (QString (aPluginName.ToCString())));