0029018: Documentation - Provide user guide for Qt browser
[occt.git] / tools / DFBrowser / DFBrowser_ItemDocument.cxx
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_ItemDocument.hxx>
17
18 #include <inspector/DFBrowser_Item.hxx>
19 #include <inspector/DFBrowser_ItemApplication.hxx>
20 #include <inspector/DFBrowser_ItemDocument.hxx>
21 #include <inspector/DFBrowser_Tools.hxx>
22
23 #include <inspector/DFBrowserPane_AttributePane.hxx>
24 #include <inspector/DFBrowserPane_ItemRole.hxx>
25
26 #include <QIcon>
27 #include <QLabel>
28 #include <QObject>
29
30 // =======================================================================
31 // function : GetLabel
32 // purpose :
33 // =======================================================================
34 TDF_Label DFBrowser_ItemDocument::GetLabel() const
35 {
36   TDF_Label aLabel;
37   if (myDocument.IsNull())
38     getDocument();
39
40   if (!myDocument.IsNull())
41     aLabel = myDocument->Main().Root();
42
43   if (!aLabel.IsNull())
44     aLabel = aLabel.Root();
45
46   return aLabel;
47 }
48
49 // =======================================================================
50 // function : getDocument
51 // purpose :
52 // =======================================================================
53 const Handle(TDocStd_Document)& DFBrowser_ItemDocument::getDocument() const
54 {
55   initItem();
56   return myDocument;
57 }
58
59 // =======================================================================
60 // function : initValue
61 // purpose :
62 // =======================================================================
63 QVariant DFBrowser_ItemDocument::initValue (const int theItemRole) const
64 {
65   if (theItemRole == Qt::DisplayRole ||
66       theItemRole == Qt::EditRole ||
67       theItemRole == DFBrowserPane_ItemRole_DisplayExtended ||
68       theItemRole == DFBrowserPane_ItemRole_ToolTipExtended)
69     return DFBrowser_Tools::GetLabelInfo (GetLabel());
70   if (theItemRole == Qt::DecorationRole)
71     return DFBrowser_Tools::GetLabelIcon (GetLabel());
72
73   return QVariant();
74 }
75
76 // =======================================================================
77 // function : createChild
78 // purpose :
79 // =======================================================================
80 TreeModel_ItemBasePtr DFBrowser_ItemDocument::createChild (int theRow, int theColumn)
81 {
82   TreeModel_ItemBasePtr anItem = DFBrowser_Item::CreateItem (currentItem(), theRow, theColumn);
83   DFBrowser_ItemBasePtr aBaseItem = itemDynamicCast<DFBrowser_ItemBase> (anItem);
84   aBaseItem->SetModule (GetModule());
85
86   return anItem;
87 }
88
89 // =======================================================================
90 // function : Init
91 // purpose :
92 // =======================================================================
93 void DFBrowser_ItemDocument::Init()
94 {
95   DFBrowser_ItemApplicationPtr aParentItem = itemDynamicCast<DFBrowser_ItemApplication> (Parent());
96   if (!aParentItem)
97     return;
98
99   const Handle(TDocStd_Application)& anApplication = aParentItem->GetApplication();
100   // items can exist only by items with not empty label
101   if (anApplication.IsNull())
102     return;
103
104   int aRowId = Row();
105
106   int aDocumentId = -1;
107   for (Standard_Integer aDocId = 1, aNbDoc = anApplication->NbDocuments(); aDocId <= aNbDoc && aDocumentId < 0; aDocId++)
108   {
109     if (aDocId - 1 == aRowId)
110       aDocumentId = aDocId;
111   }
112   if (aDocumentId > 0)
113   {
114     Handle(TDocStd_Document) aDocument;
115     anApplication->GetDocument (aDocumentId, aDocument);
116     setDocument (aDocument);
117   }
118   else
119     setDocument (Handle(TDocStd_Document)());
120
121   TreeModel_ItemBase::Init();
122 }
123
124 // =======================================================================
125 // function : reset
126 // purpose :
127 // =======================================================================
128 void DFBrowser_ItemDocument::Reset()
129 {
130   Handle(TDocStd_Document) aDocument;
131   setDocument (aDocument);
132
133   DFBrowser_ItemBase::Reset();
134 }
135
136 // =======================================================================
137 // function : initItem
138 // purpose :
139 // =======================================================================
140 void DFBrowser_ItemDocument::initItem() const
141 {
142   if (IsInitialized())
143     return;
144
145   const_cast<DFBrowser_ItemDocument*>(this)->Init();
146 }