1 // Created on: 2017-06-16
2 // Created by: Natalia ERMOLAEVA
3 // Copyright (c) 2017 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/DFBrowser_DumpView.hxx>
17 
18 #include <inspector/DFBrowser_Item.hxx>
19 #include <inspector/DFBrowser_Window.hxx>
20 #include <inspector/DFBrowser_TreeLevelView.hxx>
21 #include <OSD_OpenFile.hxx>
22 #include <inspector/TreeModel_ModelBase.hxx>
23 
24 #include <Standard_WarningsDisable.hxx>
25 #include <QAbstractItemModel>
26 #include <QDir>
27 #include <QFile>
28 #include <QPlainTextEdit>
29 #include <QTextStream>
30 #include <QWidget>
31 #include <Standard_WarningsRestore.hxx>
32 
33 // =======================================================================
34 // function : onSelectionChanged
35 // purpose :
36 // =======================================================================
OnTreeViewSelectionChanged(const QItemSelection & theSelected,const QItemSelection &)37 void DFBrowser_DumpView::OnTreeViewSelectionChanged (const QItemSelection& theSelected,
38                                                      const QItemSelection&)
39 {
40   myTextEdit->setVisible (false);
41   myTextEdit->clear();
42 
43   QModelIndexList aSelectedIndices = theSelected.indexes();
44   QModelIndexList aFirstColumnSelectedIndices;
45   for (QModelIndexList::const_iterator aSelIt = aSelectedIndices.begin(); aSelIt != aSelectedIndices.end(); aSelIt++)
46   {
47     QModelIndex anIndex = *aSelIt;
48     if (anIndex.column() == 0)
49       aFirstColumnSelectedIndices.append (anIndex);
50   }
51   if (aFirstColumnSelectedIndices.size() != 1)
52     return;
53 
54   QString aDumpInfo;
55   const QModelIndex& anIndex = aFirstColumnSelectedIndices.first();
56   TreeModel_ItemBasePtr anItemBase = TreeModel_ModelBase::GetItemByIndex (anIndex);
57   DFBrowser_ItemPtr anItem;
58   if (anItemBase)
59     anItem = itemDynamicCast<DFBrowser_Item> (anItemBase);
60 
61   if (!anItem)
62     return;
63 
64   TCollection_AsciiString aFileName = DFBrowser_Window::TmpDirectory();
65   aFileName += "/dfbrowser.txt";
66   // print dump to file(not in a string stream because result might be too long)
67   std::ofstream aFileStream;
68   OSD_OpenStream(aFileStream, aFileName, std::ios::out);
69   if (anItem->HasAttribute())
70   {
71     Handle(TDF_Attribute) anAttribute = anItem->GetAttribute();
72     if (!anAttribute.IsNull())
73       anAttribute->Dump(aFileStream);
74   }
75   else if (anItem->HasLabel())
76     anItem->GetLabel().Dump(aFileStream);
77   aFileStream.close();
78 
79   // read dumped file to fill view
80   QFile aFile (aFileName.ToCString());
81   if (!aFile.open (QIODevice::ReadOnly | QIODevice::Text))
82       return;
83   QTextStream aStream (&aFile);
84   while (!aStream.atEnd())
85   {
86     aDumpInfo.append (QString ("%1\n").arg (aStream.readLine()));
87   }
88   aFile.close();
89   QDir aDir;
90   aDir.remove (aFileName.ToCString());
91   if (!aDumpInfo.isEmpty())
92   {
93     myTextEdit->setVisible (true);
94     myTextEdit->setPlainText (aDumpInfo);
95   }
96 }
97