Salome HOME
Support of nested option buttons
[modules/shaper.git] / src / ModuleBase / ModuleBase_ToolBox.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        ModuleBase_ToolBox.cpp
4 // Created:     10 August 2015
5 // Author:      Alexandre SOLOVYOV
6
7 #include <ModuleBase_ToolBox.h>
8 #include <ModuleBase_ModelWidget.h>
9
10 #include <QButtonGroup>
11 #include <QStackedWidget>
12 #include <QHBoxLayout>
13 #include <QVBoxLayout>
14 #include <QToolButton>
15
16 ModuleBase_ToolBox::ModuleBase_ToolBox( QWidget* theParent )
17   : QFrame( theParent )
18 {
19   QVBoxLayout* aMainLayout = new QVBoxLayout( this );
20   aMainLayout->setMargin(0);
21   aMainLayout->setSpacing(2);
22
23   myButtonsFrame = new QFrame( this );
24
25   myStack = new QStackedWidget( this );
26   myStack->setFrameStyle(QFrame::Box | QFrame::Raised);
27
28   aMainLayout->addWidget( myButtonsFrame, 0 );
29   aMainLayout->addWidget( myStack, 1 );
30
31   myButtonsGroup = new QButtonGroup(this);
32   myButtonsGroup->setExclusive( true );
33   myButtonsLayout = new QHBoxLayout( myButtonsFrame );
34   myButtonsLayout->setMargin( 0 );
35   myButtonsLayout->setSpacing( 5 );
36   myButtonsLayout->addStretch( 1 );
37
38   connect( myStack, SIGNAL( currentChanged( int ) ), this, SIGNAL( currentChanged( int ) ) );
39   connect( myButtonsGroup, SIGNAL( buttonPressed( int ) ), this, SLOT( onButton( int ) ) );
40 }
41
42 ModuleBase_ToolBox::~ModuleBase_ToolBox()
43 {
44 }
45
46 void ModuleBase_ToolBox::addItem( QWidget* thePage, const QString& theName, const QPixmap& theIcon )
47 {
48   int anOldCount = myStack->count();
49
50   myStack->addWidget( thePage );
51
52   QToolButton* aButton = new QToolButton( myButtonsFrame );
53   aButton->setFocusPolicy(Qt::StrongFocus);
54   aButton->setCheckable( true );
55   aButton->setIcon( theIcon );
56   aButton->setIconSize( theIcon.size() );
57   aButton->setToolTip( theName );
58   aButton->setObjectName( theName );
59   myButtonsGroup->addButton( aButton, anOldCount );
60   myButtonsLayout->insertWidget( anOldCount, aButton );
61 }
62
63 int ModuleBase_ToolBox::count() const
64 {
65   return myStack->count();
66 }
67
68 int ModuleBase_ToolBox::currentIndex() const
69 {
70   return myStack->currentIndex();
71 }
72
73 void ModuleBase_ToolBox::setCurrentIndex( const int theIndex )
74 {
75   myStack->setCurrentIndex( theIndex );
76   myButtonsGroup->button( theIndex )->setChecked( true );
77 }
78
79 void ModuleBase_ToolBox::onButton( int theIndex )
80 {
81   myStack->setCurrentIndex( theIndex );
82 }
83
84 bool ModuleBase_ToolBox::isOffToolBoxParent(ModuleBase_ModelWidget* theWidget)
85 {
86   bool isOffToolBox = false;
87
88   QList<QWidget*> aControls = theWidget->getControls();
89   if (aControls.size() > 0) {
90     QWidget* aFirstControl = aControls.first();
91
92     QWidget* aWidget = aFirstControl;
93     QWidget* aParent = (QWidget*)aFirstControl->parent();
94     while (aParent) {
95       QStackedWidget* aStackedWidget = dynamic_cast<QStackedWidget*>(aParent);
96       if (aStackedWidget) {
97         int anIndex = aStackedWidget->currentIndex();
98         isOffToolBox = aStackedWidget->currentWidget() != aWidget;
99         break;
100       }
101       aWidget = aParent;
102       aParent = (QWidget*)aWidget->parent();
103     }
104   }
105   return isOffToolBox;
106 }