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 <inspector/View_ToolBar.hxx>
18 
19 #include <Standard_WarningsDisable.hxx>
20 #include <QComboBox>
21 #include <QHBoxLayout>
22 #include <QLabel>
23 #include <QToolButton>
24 #include <QWidget>
25 #include <Standard_WarningsRestore.hxx>
26 
27 const int DEFAULT_COMBO_WIDTH_MINIMUM = 80;
28 const int DEFAULT_SPACING = 3;
29 
30 // =======================================================================
31 // function : Constructor
32 // purpose :
33 // =======================================================================
View_ToolBar(QWidget * theParent,const bool isUseKeepView)34 View_ToolBar::View_ToolBar (QWidget* theParent, const bool isUseKeepView)
35 : QObject (theParent), myDefaultContextType (-1)
36 {
37   myMainWindow = new QWidget (theParent);
38 
39   QHBoxLayout* aLay = new QHBoxLayout (myMainWindow);
40   aLay->setContentsMargins (0, 0, 0, 0);
41   aLay->setSpacing (DEFAULT_SPACING);
42 
43   QWidget* aViewSelectorWidget = new QWidget (myMainWindow);
44   QHBoxLayout* aViewSelectorLay = new QHBoxLayout (aViewSelectorWidget);
45   aViewSelectorLay->setContentsMargins (0, 0, 0, 0);
46   aViewSelectorLay->setContentsMargins (0, 0, 0, 0);
47   aViewSelectorLay->addWidget (new QLabel (tr ("Context: "), aViewSelectorWidget));
48   myViewSelector = new QComboBox (aViewSelectorWidget);
49   myViewSelector->setMinimumWidth (DEFAULT_COMBO_WIDTH_MINIMUM);
50   aViewSelectorLay->addWidget (myViewSelector);
51   aLay->addWidget (aViewSelectorWidget);
52   connect (myViewSelector, SIGNAL (activated (int)), this, SIGNAL (contextChanged()));
53 
54   myViewContextNames[View_ContextType_None] = tr ("None");
55   myViewContextNames[View_ContextType_Own] = tr ("Own");
56   myViewContextNames[View_ContextType_External] = tr ("External");
57 
58   myViewSelector->insertItem(View_ContextType_None, myViewContextNames[View_ContextType_None]);
59   myViewSelector->insertItem(View_ContextType_Own, myViewContextNames[View_ContextType_Own]);
60 
61   myViewSelector->setCurrentIndex(View_ContextType_Own);
62   myViewContexts[View_ContextType_None] = Handle(AIS_InteractiveContext)();
63   myViewContexts[View_ContextType_Own] = Handle(AIS_InteractiveContext)();
64   myViewContexts[View_ContextType_External] = Handle(AIS_InteractiveContext)();
65 
66   myActionsMap[View_ToolActionType_Trihedron] = new QToolButton (theParent);
67   myActionsMap[View_ToolActionType_Trihedron]->setIcon (QIcon (":/icons/trihedron.png"));
68   myActionsMap[View_ToolActionType_Trihedron]->setToolTip (tr ("Trihedron display"));
69   myActionsMap[View_ToolActionType_Trihedron]->setCheckable (true);
70   myActionsMap[View_ToolActionType_Trihedron]->setChecked (false);
71 
72   myActionsMap[View_ToolActionType_ViewCube] = new QToolButton (theParent);
73   myActionsMap[View_ToolActionType_ViewCube]->setIcon (QIcon (":/icons/view_cube.png"));
74   myActionsMap[View_ToolActionType_ViewCube]->setToolTip (tr ("View Cube display"));
75   myActionsMap[View_ToolActionType_ViewCube]->setCheckable (true);
76   myActionsMap[View_ToolActionType_ViewCube]->setChecked (false);
77 
78 
79   if (isUseKeepView)
80   {
81     myActionsMap[View_ToolActionType_KeepViewId] = new QToolButton (theParent);
82     myActionsMap[View_ToolActionType_KeepViewId]->setIcon (QIcon (":/icons/keep_view_on.png"));
83     myActionsMap[View_ToolActionType_KeepViewId]->setText (tr ("Multi"));
84     myActionsMap[View_ToolActionType_KeepViewId]->setToolTip (tr ("Keep View On: does not clear previously shown presentation"));
85     myActionsMap[View_ToolActionType_KeepViewId]->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
86     myActionsMap[View_ToolActionType_KeepViewId]->setCheckable (true);
87     myActionsMap[View_ToolActionType_KeepViewId]->setChecked (false);
88 
89     myActionsMap[View_ToolActionType_KeepViewOffId] = new QToolButton (theParent);
90     myActionsMap[View_ToolActionType_KeepViewOffId]->setIcon (QIcon (":/icons/keep_view_off.png"));
91     myActionsMap[View_ToolActionType_KeepViewOffId]->setText (QObject::tr ("Single"));
92     myActionsMap[View_ToolActionType_KeepViewOffId]->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
93     myActionsMap[View_ToolActionType_KeepViewOffId]->setToolTip (tr ("Keep View Off: clear previously shown presentation"));
94     myActionsMap[View_ToolActionType_KeepViewOffId]->setCheckable (true);
95     myActionsMap[View_ToolActionType_KeepViewOffId]->setChecked (true);
96 
97     myActionsMap[View_ToolActionType_ClearViewId] = new QToolButton (theParent);
98     myActionsMap[View_ToolActionType_ClearViewId]->setIcon (QIcon (":/icons/view_clear.png"));
99     myActionsMap[View_ToolActionType_ClearViewId]->setText (tr ( "Clear View"));
100     myActionsMap[View_ToolActionType_ClearViewId]->setToolTip (tr ("Remove all visualized presentations from view context"));
101   }
102 
103   for (QMap<View_ToolActionType, QToolButton*>::ConstIterator anActionsIt = myActionsMap.begin(),
104        anActionsLast = myActionsMap.end(); anActionsIt != anActionsLast; anActionsIt++)
105   {
106     QToolButton* aBtn = anActionsIt.value();
107     connect (aBtn, SIGNAL (clicked()), this, SLOT (onActionClicked()));
108     aLay->addWidget (aBtn);
109   }
110   aLay->addStretch (1);
111 }
112 
113 // =======================================================================
114 // function : SetContext
115 // purpose :
116 // =======================================================================
SetContext(View_ContextType theType,const Handle (AIS_InteractiveContext)& theContext)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 (Standard_Dump::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   if (myDefaultContextType >= 0 && myViewSelector->count() > myDefaultContextType)
132   {
133     // using default type during the first setting the external context
134     myViewSelector->setCurrentIndex (myDefaultContextType);
135     myDefaultContextType = -1;
136   }
137 }
138 
139 // =======================================================================
140 // function : CurrentContextType
141 // purpose :
142 // =======================================================================
CurrentContextType() const143 View_ContextType View_ToolBar::CurrentContextType() const
144 {
145   return (View_ContextType)myViewSelector->currentIndex();
146 }
147 
148 // =======================================================================
149 // function : SetCurrentContext
150 // purpose :
151 // =======================================================================
SetCurrentContextType(View_ContextType theType)152 void View_ToolBar::SetCurrentContextType (View_ContextType theType)
153 {
154   myViewSelector->setCurrentIndex ((int)theType);
155   emit contextChanged();
156 }
157 
158 // =======================================================================
159 // function : CurrentContext
160 // purpose :
161 // =======================================================================
Handle(AIS_InteractiveContext)162 Handle(AIS_InteractiveContext) View_ToolBar::CurrentContext() const
163 {
164   View_ContextType aCurrentType = (View_ContextType)myViewSelector->currentIndex();
165   return myViewContexts[aCurrentType];
166 }
167 
168 // =======================================================================
169 // function : IsActionChecked
170 // purpose :
171 // =======================================================================
IsActionChecked(const int theActionId) const172 bool View_ToolBar::IsActionChecked (const int theActionId) const
173 {
174   View_ToolActionType anActionId = (View_ToolActionType)theActionId;
175   return myActionsMap.contains (anActionId) ? myActionsMap[anActionId]->isChecked() : false;
176 }
177 
178 // =======================================================================
179 // function : SaveState
180 // purpose :
181 // =======================================================================
SaveState(View_ToolBar * theToolBar,QMap<QString,QString> & theItems,const QString & thePrefix)182 void View_ToolBar::SaveState (View_ToolBar* theToolBar,
183                               QMap<QString, QString>& theItems,
184                               const QString& thePrefix)
185 {
186   theItems[thePrefix + "context_type"] = QString::number (theToolBar->CurrentContextType());
187 }
188 
189 // =======================================================================
190 // function : RestoreState
191 // purpose :
192 // =======================================================================
RestoreState(View_ToolBar * theToolBar,const QString & theKey,const QString & theValue,const QString & thePrefix)193 bool View_ToolBar::RestoreState (View_ToolBar* theToolBar,
194                                  const QString& theKey, const QString& theValue,
195                                  const QString& thePrefix)
196 {
197   if (theKey == thePrefix + "context_type")
198   {
199     theToolBar->SetDefaultContextType ((View_ContextType)theValue.toInt());
200   }
201   else
202     return false;
203 
204   return true;
205 }
206 
207 // =======================================================================
208 // function : onActionClicked
209 // purpose :
210 // =======================================================================
onActionClicked()211 void View_ToolBar::onActionClicked()
212 {
213   int anId = -1;
214   QToolButton* aSenderBtn = (QToolButton*)sender();
215 
216   for (QMap<View_ToolActionType, QToolButton*>::ConstIterator anActionsIt = myActionsMap.begin(),
217        anActionsLast = myActionsMap.end(); anActionsIt != anActionsLast; anActionsIt++)
218   {
219     if (anActionsIt.value() == aSenderBtn)
220     {
221       anId = anActionsIt.key();
222       break;
223     }
224   }
225   if (anId != -1)
226     emit actionClicked (anId);
227 
228   if (anId == View_ToolActionType_KeepViewId || anId == View_ToolActionType_KeepViewOffId)
229   {
230     if (anId == View_ToolActionType_KeepViewId)
231       myActionsMap[View_ToolActionType_KeepViewOffId]->setChecked (!aSenderBtn->isChecked());
232     else
233       myActionsMap[View_ToolActionType_KeepViewId]->setChecked (!aSenderBtn->isChecked());
234   }
235 }
236