1 // Created on: 2021-04-27
2 // Created by: Natalia ERMOLAEVA
3 // Copyright (c) 2021 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/MessageModel_Actions.hxx>
17 
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>
24 
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>
35 
36 #include <Standard_WarningsDisable.hxx>
37 #include <QAction>
38 #include <QFileDialog>
39 #include <QItemSelectionModel>
40 #include <QMenu>
41 #include <QMessageBox>
42 #include <QWidget>
43 #include <Standard_WarningsRestore.hxx>
44 
45 // =======================================================================
46 // function : Constructor
47 // purpose :
48 // =======================================================================
MessageModel_Actions(QWidget * theParent,MessageModel_TreeModel * theTreeModel,QItemSelectionModel * theModel)49 MessageModel_Actions::MessageModel_Actions (QWidget* theParent,
50                                             MessageModel_TreeModel* theTreeModel,
51                                             QItemSelectionModel* theModel)
52 : QObject (theParent), myTreeModel (theTreeModel), mySelectionModel (theModel)
53 {
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));
62 }
63 
64 // =======================================================================
65 // function : GetAction
66 // purpose :
67 // =======================================================================
GetAction(const MessageModel_ActionType & theType)68 QAction* MessageModel_Actions::GetAction (const MessageModel_ActionType& theType)
69 {
70   return myActions.contains (theType) ? myActions[theType] : 0;
71 }
72 
73 // =======================================================================
74 // function : AddMenuActions
75 // purpose :
76 // =======================================================================
AddMenuActions(const QModelIndexList & theSelectedIndices,QMenu * theMenu)77 void MessageModel_Actions::AddMenuActions (const QModelIndexList& theSelectedIndices, QMenu* theMenu)
78 {
79   MessageModel_ItemRootPtr aRootItem;
80   MessageModel_ItemReportPtr aReportItem;
81   MessageModel_ItemAlertPtr anAlertItem;
82   for (QModelIndexList::const_iterator aSelIt = theSelectedIndices.begin(); aSelIt != theSelectedIndices.end(); aSelIt++)
83   {
84     QModelIndex anIndex = *aSelIt;
85     if (anIndex.column() != 0)
86       continue;
87 
88     TreeModel_ItemBasePtr anItemBase = TreeModel_ModelBase::GetItemByIndex (anIndex);
89     if (!anItemBase)
90       continue;
91 
92     aRootItem = itemDynamicCast<MessageModel_ItemRoot> (anItemBase);
93     if (aRootItem)
94       break;
95 
96     aReportItem = itemDynamicCast<MessageModel_ItemReport> (anItemBase);
97     if (aReportItem)
98       break;
99 
100     anAlertItem = itemDynamicCast<MessageModel_ItemAlert> (anItemBase);
101     if (anAlertItem)
102       break;
103   }
104 
105   if (aReportItem && !aReportItem->GetReport().IsNull())
106   {
107     theMenu->addAction (myActions[MessageModel_ActionType_Deactivate]);
108     theMenu->addAction (myActions[MessageModel_ActionType_Activate]);
109     theMenu->addAction (myActions[MessageModel_ActionType_Clear]);
110   }
111   else if (anAlertItem)
112   {
113     theMenu->addAction (myActions[MessageModel_ActionType_ExportToShapeView]);
114   }
115 
116   theMenu->addSeparator();
117 }
118 
119 // =======================================================================
120 // function : getSelectedReport
121 // purpose :
122 // =======================================================================
Handle(Message_Report)123 Handle(Message_Report) MessageModel_Actions::getSelectedReport (QModelIndex& theReportIndex) const
124 {
125   MessageModel_ItemReportPtr aReportItem;
126   QModelIndexList aSelectedIndices = mySelectionModel->selectedIndexes();
127   for (QModelIndexList::const_iterator aSelIt = aSelectedIndices.begin(); aSelIt != aSelectedIndices.end(); aSelIt++)
128   {
129     QModelIndex anIndex = *aSelIt;
130     if (anIndex.column() != 0)
131       continue;
132 
133     TreeModel_ItemBasePtr anItemBase = TreeModel_ModelBase::GetItemByIndex (anIndex);
134     if (!anItemBase)
135       continue;
136 
137     aReportItem = itemDynamicCast<MessageModel_ItemReport> (anItemBase);
138     theReportIndex = anIndex;
139     if (aReportItem)
140       break;
141   }
142   if (!aReportItem)
143     return NULL;
144 
145   return aReportItem->GetReport();
146 }
147 
148 // =======================================================================
149 // function : OnActivateReport
150 // purpose :
151 // =======================================================================
152 static Handle(Message_PrinterToReport) MyPrinterToReport;
153 static Message_SequenceOfPrinters MyDeactivatedPrinters;
154 
OnActivateReport()155 void MessageModel_Actions::OnActivateReport()
156 {
157   if (MyPrinterToReport.IsNull())
158     MyPrinterToReport = new Message_PrinterToReport();
159 
160   if (MyPrinterToReport->Report()->IsActiveInMessenger())
161     return;
162 
163   MyDeactivatedPrinters = Message::DefaultMessenger()->Printers();
164   Message::DefaultMessenger()->ChangePrinters().Clear();
165 
166   Message::DefaultMessenger()->AddPrinter (MyPrinterToReport);
167   Message::DefaultReport()->UpdateActiveInMessenger();
168 
169   myTreeModel->UpdateTreeModel();
170 }
171 
172 // =======================================================================
173 // function : OnDeactivateReport
174 // purpose :
175 // =======================================================================
OnDeactivateReport()176 void MessageModel_Actions::OnDeactivateReport()
177 {
178   if (MyPrinterToReport.IsNull() || !MyPrinterToReport->Report()->IsActiveInMessenger())
179     return;
180 
181   Message::DefaultMessenger()->RemovePrinter (MyPrinterToReport);
182   Message::DefaultMessenger()->ChangePrinters().Assign (MyDeactivatedPrinters);
183 
184   myTreeModel->UpdateTreeModel();
185 }
186 
187 // =======================================================================
188 // function : OnClearReport
189 // purpose :
190 // =======================================================================
OnClearReport()191 void MessageModel_Actions::OnClearReport()
192 {
193   QModelIndex aReportIndex;
194   Handle(Message_Report) aReport = getSelectedReport (aReportIndex);
195   if (aReport.IsNull())
196     return;
197 
198   aReport->Clear();
199   myTreeModel->UpdateTreeModel();
200 }
201 
202 // =======================================================================
203 // function : OnExportToShapeView
204 // purpose :
205 // =======================================================================
OnExportToShapeView()206 void MessageModel_Actions::OnExportToShapeView()
207 {
208   TCollection_AsciiString aPluginName ("TKShapeView");
209 
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);
216 
217   QModelIndexList aSelectedIndices = mySelectionModel->selectedIndexes();
218   QStringList anExportedPointers;
219   for (QModelIndexList::const_iterator aSelIt = aSelectedIndices.begin(); aSelIt != aSelectedIndices.end(); aSelIt++)
220   {
221     QModelIndex anIndex = *aSelIt;
222     if (anIndex.column() != 0)
223       continue;
224 
225     TreeModel_ItemBasePtr anItemBase = TreeModel_ModelBase::GetItemByIndex (anIndex);
226     if (!anItemBase)
227       continue;
228 
229     MessageModel_ItemAlertPtr anAlertItem = itemDynamicCast<MessageModel_ItemAlert> (anItemBase);
230     if (!anAlertItem)
231       continue;
232 
233     Handle(Message_Alert) anAlert = anAlertItem->GetAlert();
234     if (anAlert.IsNull())
235       continue;
236 
237     Handle(Message_AlertExtended) anExtAlert = Handle(Message_AlertExtended)::DownCast (anAlert);
238     if (anExtAlert.IsNull())
239       continue;
240 
241     Handle(Message_Attribute) anAttribute = anExtAlert->Attribute();
242     if (anAttribute.IsNull())
243       continue;
244 
245     if (!anAttribute->IsKind (STANDARD_TYPE (TopoDS_AlertAttribute)))
246       continue;
247 
248     const TopoDS_Shape aShape = Handle(TopoDS_AlertAttribute)::DownCast (anAttribute)->GetShape();
249     if (aShape.IsNull())
250       continue;
251     aPluginParameters.Append (aShape.TShape());
252     anItemNames.Append (TInspectorAPI_PluginParameters::ParametersToString (aShape));
253 
254     anExportedPointers.append (Standard_Dump::GetPointerInfo (aShape.TShape(), true).ToCString());
255   }
256 
257   if (anExportedPointers.empty())
258       return;
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())));
263 }
264