0027398: Integrate Qt Browser Widget to Open CASCADE Technology
[occt.git] / tools / View / View_ToolBar.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                                                           
17 #include <View_ToolBar.hxx>
18
19 #include <QComboBox>
20 #include <QHBoxLayout>
21 #include <QLabel>
22 #include <QToolButton>
23 #include <QWidget>
24
25 const int DEFAULT_COMBO_WIDTH_MINIMUM = 80;
26 const int DEFAULT_SPACING = 3;
27
28 // =======================================================================
29 // function : Constructor
30 // purpose :
31 // =======================================================================
32 View_ToolBar::View_ToolBar (QWidget* theParent)
33 : QObject (theParent)
34 {
35   myMainWindow = new QWidget (theParent);
36
37   QHBoxLayout* aLay = new QHBoxLayout (myMainWindow);
38   aLay->setContentsMargins (0, 0, 0, 0);
39   aLay->setSpacing (DEFAULT_SPACING);
40
41   QWidget* aViewSelectorWidget = new QWidget (myMainWindow);
42   QHBoxLayout* aViewSelectorLay = new QHBoxLayout (aViewSelectorWidget);
43   aViewSelectorLay->setContentsMargins (0, 0, 0, 0);
44   aViewSelectorLay->setContentsMargins (0, 0, 0, 0);
45   aViewSelectorLay->addWidget (new QLabel (tr ("View: "), aViewSelectorWidget));
46   myViewSelector = new QComboBox (aViewSelectorWidget);
47   myViewSelector->setMinimumWidth (DEFAULT_COMBO_WIDTH_MINIMUM);
48   aViewSelectorLay->addWidget (myViewSelector);
49   aLay->addWidget (aViewSelectorWidget);
50   connect (myViewSelector, SIGNAL (activated (int)), this, SIGNAL (contextChanged()));
51
52   myViewContextNames[View_ContextType_None] = tr ("None");
53   myViewContextNames[View_ContextType_Own] = tr ("Own");
54   myViewContextNames[View_ContextType_External] = tr ("External");
55
56   myViewSelector->insertItem(View_ContextType_None, myViewContextNames[View_ContextType_None]);
57   myViewSelector->insertItem(View_ContextType_Own, myViewContextNames[View_ContextType_Own]);
58
59   myViewSelector->setCurrentIndex(View_ContextType_Own);
60   myViewContexts[View_ContextType_None] = Handle(AIS_InteractiveContext)();
61   myViewContexts[View_ContextType_Own] = Handle(AIS_InteractiveContext)();
62   myViewContexts[View_ContextType_External] = Handle(AIS_InteractiveContext)();
63
64   myActionsMap[View_ToolActionType_KeepViewId] = new QToolButton (theParent);
65   myActionsMap[View_ToolActionType_KeepViewId]->setIcon (QIcon (":/icons/keep_view_on.png"));
66   myActionsMap[View_ToolActionType_KeepViewId]->setText (tr ("Keep View On"));
67   myActionsMap[View_ToolActionType_KeepViewId]->setToolTip (tr ("Keep View On: does not clear previously shown presentation"));
68   myActionsMap[View_ToolActionType_KeepViewId]->setCheckable (true);
69   myActionsMap[View_ToolActionType_KeepViewId]->setChecked (false);
70
71   myActionsMap[View_ToolActionType_KeepViewOffId] = new QToolButton (theParent);
72   myActionsMap[View_ToolActionType_KeepViewOffId]->setIcon (QIcon (":/icons/keep_view_off.png"));
73   myActionsMap[View_ToolActionType_KeepViewOffId]->setText (QObject::tr ("Keep View Off"));
74   myActionsMap[View_ToolActionType_KeepViewOffId]->setToolTip (tr ("Keep View Off: clear previously shown presentation"));
75   myActionsMap[View_ToolActionType_KeepViewOffId]->setCheckable (true);
76   myActionsMap[View_ToolActionType_KeepViewOffId]->setChecked (true);
77   
78   myActionsMap[View_ToolActionType_ClearViewId] = new QToolButton (theParent);
79   myActionsMap[View_ToolActionType_ClearViewId]->setIcon (QIcon (":/icons/view_clear.png"));
80   myActionsMap[View_ToolActionType_ClearViewId]->setText (tr ( "Clear View"));
81   myActionsMap[View_ToolActionType_ClearViewId]->setToolTip (tr ("Remove all visualized presentations from view context"));
82
83   for (QMap<View_ToolActionType, QToolButton*>::ConstIterator anActionsIt = myActionsMap.begin(),
84        anActionsLast = myActionsMap.end(); anActionsIt != anActionsLast; anActionsIt++)
85   {
86     QToolButton* aBtn = anActionsIt.value();
87     connect (aBtn, SIGNAL (clicked()), this, SLOT (onActionClicked()));
88     aLay->addWidget (aBtn);
89   }
90   aLay->addStretch (1);
91 }
92
93 TCollection_AsciiString getPointerInfo (const Handle(Standard_Transient)& thePointer, const bool isShortInfo)
94 {
95   std::ostringstream aPtrStr;
96   aPtrStr << thePointer.operator->();
97   if (!isShortInfo)
98     return aPtrStr.str().c_str();
99
100   TCollection_AsciiString anInfoPtr (aPtrStr.str().c_str());
101   for (int aSymbolId = 1; aSymbolId < anInfoPtr.Length(); aSymbolId++)
102   {
103     if (anInfoPtr.Value(aSymbolId) != '0')
104     {
105       anInfoPtr = anInfoPtr.SubString(aSymbolId, anInfoPtr.Length());
106       anInfoPtr.Prepend("0x");
107       return anInfoPtr;
108     }
109   }
110   return aPtrStr.str().c_str();
111 }
112
113 // =======================================================================
114 // function : SetContext
115 // purpose :
116 // =======================================================================
117 void View_ToolBar::SetContext (View_ContextType theType, const Handle(AIS_InteractiveContext)& theContext)
118 {
119   myViewContexts[theType] = theContext;
120
121   QString aViewContextName = myViewContextNames[theType];
122   if (!theContext.IsNull())
123     aViewContextName = QString ("%1 : [%2]").arg (myViewContextNames[theType])
124                                             .arg (getPointerInfo (theContext, true).ToCString());
125   // there are only "Own" and "None" items
126   if (!theContext.IsNull() && theType == View_ContextType_External && myViewSelector->count() == 2)
127     myViewSelector->insertItem (View_ContextType_External, aViewContextName);
128   else
129     myViewSelector->setItemText (theType, aViewContextName);
130 }
131
132 // =======================================================================
133 // function : getCurrentContextType
134 // purpose :
135 // =======================================================================
136 View_ContextType View_ToolBar::GetCurrentContextType() const
137 {
138   return (View_ContextType)myViewSelector->currentIndex();
139 }
140
141 // =======================================================================
142 // function : GetCurrentContext
143 // purpose :
144 // =======================================================================
145 Handle(AIS_InteractiveContext) View_ToolBar::GetCurrentContext() const
146 {
147   View_ContextType aCurrentType = (View_ContextType)myViewSelector->currentIndex();
148   return myViewContexts[aCurrentType];
149 }
150
151 // =======================================================================
152 // function : IsActionChecked
153 // purpose :
154 // =======================================================================
155 bool View_ToolBar::IsActionChecked (const int theActionId) const
156 {
157   View_ToolActionType anActionId = (View_ToolActionType)theActionId;
158   return myActionsMap.contains (anActionId) ? myActionsMap[anActionId]->isChecked() : false;
159 }
160
161 // =======================================================================
162 // function : onActionClicked
163 // purpose :
164 // =======================================================================
165 void View_ToolBar::onActionClicked()
166 {
167   int anId = -1;
168   QToolButton* aSenderBtn = (QToolButton*)sender();
169
170   for (QMap<View_ToolActionType, QToolButton*>::ConstIterator anActionsIt = myActionsMap.begin(),
171        anActionsLast = myActionsMap.end(); anActionsIt != anActionsLast; anActionsIt++)
172   {
173     if (anActionsIt.value() == aSenderBtn)
174     {
175       anId = anActionsIt.key();
176       break;
177     }
178   }
179   if (anId != -1)
180     emit actionClicked (anId);
181
182   if (anId == View_ToolActionType_KeepViewId || anId == View_ToolActionType_KeepViewOffId)
183   {
184     if (anId == View_ToolActionType_KeepViewId)
185       myActionsMap[View_ToolActionType_KeepViewOffId]->setChecked (!aSenderBtn->isChecked());
186     else
187       myActionsMap[View_ToolActionType_KeepViewId]->setChecked (!aSenderBtn->isChecked());
188   }
189 }