Salome HOME
d1b52ef89de0a8e267f7b1acfe0de975f232583c
[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
27   aMainLayout->addWidget( myButtonsFrame, 0 );
28   aMainLayout->addWidget( myStack, 1 );
29
30   myButtonsGroup = new QButtonGroup(this);
31   myButtonsGroup->setExclusive( true );
32   myButtonsLayout = new QHBoxLayout( myButtonsFrame );
33   myButtonsLayout->setMargin( 0 );
34   myButtonsLayout->setSpacing( 5 );
35   myButtonsLayout->addStretch( 1 );
36
37   connect( myStack, SIGNAL( currentChanged( int ) ), this, SIGNAL( currentChanged( int ) ) );
38   connect( myButtonsGroup, SIGNAL( buttonPressed( int ) ), this, SLOT( onButton( int ) ) );
39 }
40
41 ModuleBase_ToolBox::~ModuleBase_ToolBox()
42 {
43 }
44
45 void ModuleBase_ToolBox::addItem( QWidget* thePage, const QString& theName, const QPixmap& theIcon )
46 {
47   int anOldCount = myStack->count();
48
49   myStack->addWidget( thePage );
50
51   QToolButton* aButton = new QToolButton( myButtonsFrame );
52   aButton->setFocusPolicy(Qt::StrongFocus);
53   aButton->setCheckable( true );
54   aButton->setIcon( theIcon );
55   aButton->setIconSize( theIcon.size() );
56   aButton->setToolTip( theName );
57   aButton->setObjectName( theName );
58   myButtonsGroup->addButton( aButton, anOldCount );
59   myButtonsLayout->insertWidget( anOldCount, aButton );
60 }
61
62 int ModuleBase_ToolBox::count() const
63 {
64   return myStack->count();
65 }
66
67 int ModuleBase_ToolBox::currentIndex() const
68 {
69   return myStack->currentIndex();
70 }
71
72 void ModuleBase_ToolBox::setCurrentIndex( const int theIndex )
73 {
74   myStack->setCurrentIndex( theIndex );
75   myButtonsGroup->button( theIndex )->setChecked( true );
76 }
77
78 void ModuleBase_ToolBox::onButton( int theIndex )
79 {
80   myStack->setCurrentIndex( theIndex );
81 }
82
83 bool ModuleBase_ToolBox::isOffToolBoxParent(ModuleBase_ModelWidget* theWidget)
84 {
85   bool isOffToolBox = false;
86
87   QList<QWidget*> aControls = theWidget->getControls();
88   if (aControls.size() > 0) {
89     QWidget* aFirstControl = aControls.first();
90
91     QWidget* aWidget = aFirstControl;
92     QWidget* aParent = (QWidget*)aFirstControl->parent();
93     while (aParent) {
94       QStackedWidget* aStackedWidget = dynamic_cast<QStackedWidget*>(aParent);
95       if (aStackedWidget) {
96         int anIndex = aStackedWidget->currentIndex();
97         isOffToolBox = aStackedWidget->currentWidget() != aWidget;
98         break;
99       }
100       aWidget = aParent;
101       aParent = (QWidget*)aWidget->parent();
102     }
103   }
104   return isOffToolBox;
105 }