1 // Created on: 2017-06-16
2 // Created by: Natalia ERMOLAEVA
3 // Copyright (c) 2017 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
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.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
17 #include <View_ToolBar.hxx>
20 #include <QHBoxLayout>
22 #include <QToolButton>
25 const int DEFAULT_COMBO_WIDTH_MINIMUM = 80;
26 const int DEFAULT_SPACING = 3;
28 // =======================================================================
29 // function : Constructor
31 // =======================================================================
32 View_ToolBar::View_ToolBar (QWidget* theParent)
35 myMainWindow = new QWidget (theParent);
37 QHBoxLayout* aLay = new QHBoxLayout (myMainWindow);
38 aLay->setContentsMargins (0, 0, 0, 0);
39 aLay->setSpacing (DEFAULT_SPACING);
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()));
52 myViewContextNames[View_ContextType_None] = tr ("None");
53 myViewContextNames[View_ContextType_Own] = tr ("Own");
54 myViewContextNames[View_ContextType_External] = tr ("External");
56 myViewSelector->insertItem(View_ContextType_None, myViewContextNames[View_ContextType_None]);
57 myViewSelector->insertItem(View_ContextType_Own, myViewContextNames[View_ContextType_Own]);
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)();
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);
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);
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"));
83 for (QMap<View_ToolActionType, QToolButton*>::ConstIterator anActionsIt = myActionsMap.begin(),
84 anActionsLast = myActionsMap.end(); anActionsIt != anActionsLast; anActionsIt++)
86 QToolButton* aBtn = anActionsIt.value();
87 connect (aBtn, SIGNAL (clicked()), this, SLOT (onActionClicked()));
88 aLay->addWidget (aBtn);
93 TCollection_AsciiString getPointerInfo (const Handle(Standard_Transient)& thePointer, const bool isShortInfo)
95 std::ostringstream aPtrStr;
96 aPtrStr << thePointer.operator->();
98 return aPtrStr.str().c_str();
100 TCollection_AsciiString anInfoPtr (aPtrStr.str().c_str());
101 for (int aSymbolId = 1; aSymbolId < anInfoPtr.Length(); aSymbolId++)
103 if (anInfoPtr.Value(aSymbolId) != '0')
105 anInfoPtr = anInfoPtr.SubString(aSymbolId, anInfoPtr.Length());
106 anInfoPtr.Prepend("0x");
110 return aPtrStr.str().c_str();
113 // =======================================================================
114 // function : SetContext
116 // =======================================================================
117 void View_ToolBar::SetContext (View_ContextType theType, const Handle(AIS_InteractiveContext)& theContext)
119 myViewContexts[theType] = theContext;
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);
129 myViewSelector->setItemText (theType, aViewContextName);
132 // =======================================================================
133 // function : getCurrentContextType
135 // =======================================================================
136 View_ContextType View_ToolBar::GetCurrentContextType() const
138 return (View_ContextType)myViewSelector->currentIndex();
141 // =======================================================================
142 // function : GetCurrentContext
144 // =======================================================================
145 Handle(AIS_InteractiveContext) View_ToolBar::GetCurrentContext() const
147 View_ContextType aCurrentType = (View_ContextType)myViewSelector->currentIndex();
148 return myViewContexts[aCurrentType];
151 // =======================================================================
152 // function : IsActionChecked
154 // =======================================================================
155 bool View_ToolBar::IsActionChecked (const int theActionId) const
157 View_ToolActionType anActionId = (View_ToolActionType)theActionId;
158 return myActionsMap.contains (anActionId) ? myActionsMap[anActionId]->isChecked() : false;
161 // =======================================================================
162 // function : onActionClicked
164 // =======================================================================
165 void View_ToolBar::onActionClicked()
168 QToolButton* aSenderBtn = (QToolButton*)sender();
170 for (QMap<View_ToolActionType, QToolButton*>::ConstIterator anActionsIt = myActionsMap.begin(),
171 anActionsLast = myActionsMap.end(); anActionsIt != anActionsLast; anActionsIt++)
173 if (anActionsIt.value() == aSenderBtn)
175 anId = anActionsIt.key();
180 emit actionClicked (anId);
182 if (anId == View_ToolActionType_KeepViewId || anId == View_ToolActionType_KeepViewOffId)
184 if (anId == View_ToolActionType_KeepViewId)
185 myActionsMap[View_ToolActionType_KeepViewOffId]->setChecked (!aSenderBtn->isChecked());
187 myActionsMap[View_ToolActionType_KeepViewId]->setChecked (!aSenderBtn->isChecked());