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