Salome HOME
Issue #1860: fix end lines with spaces
[modules/shaper.git] / src / SHAPERGUI / SHAPERGUI_NestedButton.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 /*
4  * SHAPERGUI_NestedButton.cpp
5  *
6  *  Created on: Apr 13, 2015
7  *      Author: sbh
8  */
9
10 #include <SHAPERGUI_NestedButton.h>
11
12 #include <QAction>
13 #include <QFrame>
14 #include <QHBoxLayout>
15 #include <QToolButton>
16 #include <QToolBar>
17 #include <QEvent>
18
19 SHAPERGUI_NestedButton::SHAPERGUI_NestedButton(QObject* theParent,
20                                            const QList<QAction*>& theNestedActions)
21 : QWidgetAction(theParent),
22   myNestedActions(theNestedActions),
23   myAdditionalButtonsWidget(0),
24   myButtonFrame(0),
25   myThisButton(0)
26 {
27 }
28
29 SHAPERGUI_NestedButton::~SHAPERGUI_NestedButton()
30 {
31 }
32
33 void SHAPERGUI_NestedButton::showAdditionalButtons(bool isShow)
34 {
35   myAdditionalButtonsWidget->setVisible(isShow);
36   if (isShow) {
37     myButtonFrame->setFrameStyle(QFrame::WinPanel);
38     myButtonFrame->setFrameShadow(QFrame::Sunken);
39     myThisButton->setAutoRaise(false);
40   } else {
41     myButtonFrame->setFrameStyle(QFrame::NoFrame);
42     myButtonFrame->setFrameShadow(QFrame::Plain);
43     myThisButton->setAutoRaise(true);
44   }
45 }
46
47 QWidget * SHAPERGUI_NestedButton::createWidget(QWidget * theParent)
48 {
49   // the action has widget only in tool bar, in menu bar, the default
50   // action presentation is shown
51   QToolBar* aToolBar = dynamic_cast<QToolBar*>(theParent);
52   if (!aToolBar)
53     return 0;
54
55   myButtonFrame = new QFrame(theParent);
56   QHBoxLayout* aBoxLay = new QHBoxLayout(myButtonFrame);
57   aBoxLay->setContentsMargins(2, 0, 0, 0);
58   aBoxLay->setSpacing(1);
59   QSizePolicy aSizePolicy;
60   aSizePolicy.setControlType(QSizePolicy::ToolButton);
61   myButtonFrame->setSizePolicy(aSizePolicy);
62
63   myThisButton = new QToolButton(myButtonFrame);
64   myThisButton->setDefaultAction(this);
65   myThisButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
66   aBoxLay->addWidget(myThisButton, 1);
67
68   myAdditionalButtonsWidget = new QWidget(myButtonFrame);
69   QHBoxLayout* aAdditionalBoxLay = new QHBoxLayout(myAdditionalButtonsWidget);
70   aAdditionalBoxLay->setContentsMargins(0, 0, 0, 0);
71   aAdditionalBoxLay->setSpacing(1);
72   foreach (QAction* eachAct, myNestedActions) {
73     QToolButton* aButton = new QToolButton(myButtonFrame);
74     aButton->setDefaultAction(eachAct);
75     aButton->setAutoRaise(true);
76     aAdditionalBoxLay->addWidget(aButton);
77   }
78   myAdditionalButtonsWidget->setLayout(aAdditionalBoxLay);
79   aBoxLay->addWidget(myAdditionalButtonsWidget);
80
81   myButtonFrame->setLayout(aBoxLay);
82
83   showAdditionalButtons(false);
84   connect(this, SIGNAL(toggled(bool)), this, SLOT(showAdditionalButtons(bool)));
85   connect(this, SIGNAL(changed()), this, SLOT(actionStateChanged()));
86   return myButtonFrame;
87 }
88
89 bool SHAPERGUI_NestedButton::event(QEvent* theEvent)
90 {
91   if (theEvent->type() == QEvent::ActionChanged) {
92     if (myThisButton) {
93       myThisButton->setEnabled(isEnabled());
94       return true;
95     }
96   }
97   return QWidgetAction::event(theEvent);
98 }
99
100
101 void SHAPERGUI_NestedButton::actionStateChanged()
102 {
103   if (isEnabled()) {
104     QString s = "true";
105   } else {
106     QString s = "false";
107   }
108
109 }