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