Salome HOME
491caa9287b1bf30eb396b6a8cb74d7f0e397b11
[modules/shaper.git] / src / SHAPERGUI / SHAPERGUI_NestedButton.cpp
1 // Copyright (C) 2014-2021  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 /*
21  * SHAPERGUI_NestedButton.cpp
22  *
23  *  Created on: Apr 13, 2015
24  *      Author: sbh
25  */
26
27 #include <SHAPERGUI_NestedButton.h>
28
29 #include <QAction>
30 #include <QFrame>
31 #include <QHBoxLayout>
32 #include <QToolButton>
33 #include <QToolBar>
34 #include <QEvent>
35
36 SHAPERGUI_NestedButton::SHAPERGUI_NestedButton(QObject* theParent,
37                                            const QList<QAction*>& theNestedActions)
38 : QWidgetAction(theParent),
39   myNestedActions(theNestedActions),
40   myAdditionalButtonsWidget(0),
41   myButtonFrame(0),
42   myThisButton(0)
43 {
44 }
45
46 SHAPERGUI_NestedButton::~SHAPERGUI_NestedButton()
47 {
48 }
49
50 void SHAPERGUI_NestedButton::showAdditionalButtons(bool isShow)
51 {
52   myAdditionalButtonsWidget->setVisible(isShow);
53   if (isShow) {
54     myButtonFrame->setFrameStyle(QFrame::WinPanel);
55     myButtonFrame->setFrameShadow(QFrame::Sunken);
56     myThisButton->setAutoRaise(false);
57   } else {
58     myButtonFrame->setFrameStyle(QFrame::NoFrame);
59     myButtonFrame->setFrameShadow(QFrame::Plain);
60     myThisButton->setAutoRaise(true);
61   }
62 }
63
64 QWidget * SHAPERGUI_NestedButton::createWidget(QWidget * theParent)
65 {
66   // the action has widget only in tool bar, in menu bar, the default
67   // action presentation is shown
68   QToolBar* aToolBar = dynamic_cast<QToolBar*>(theParent);
69   if (!aToolBar)
70     return 0;
71
72   myButtonFrame = new QFrame(theParent);
73   QHBoxLayout* aBoxLay = new QHBoxLayout(myButtonFrame);
74   aBoxLay->setContentsMargins(2, 0, 0, 0);
75   aBoxLay->setSpacing(1);
76   QSizePolicy aSizePolicy;
77   aSizePolicy.setControlType(QSizePolicy::ToolButton);
78   myButtonFrame->setSizePolicy(aSizePolicy);
79
80   myThisButton = new QToolButton(myButtonFrame);
81   myThisButton->setDefaultAction(this);
82   myThisButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
83   aBoxLay->addWidget(myThisButton, 1);
84
85   myAdditionalButtonsWidget = new QWidget(myButtonFrame);
86   QHBoxLayout* aAdditionalBoxLay = new QHBoxLayout(myAdditionalButtonsWidget);
87   aAdditionalBoxLay->setContentsMargins(0, 0, 0, 0);
88   aAdditionalBoxLay->setSpacing(1);
89   foreach (QAction* eachAct, myNestedActions) {
90     QToolButton* aButton = new QToolButton(myButtonFrame);
91     aButton->setDefaultAction(eachAct);
92     aButton->setAutoRaise(true);
93     aAdditionalBoxLay->addWidget(aButton);
94   }
95   myAdditionalButtonsWidget->setLayout(aAdditionalBoxLay);
96   aBoxLay->addWidget(myAdditionalButtonsWidget);
97
98   myButtonFrame->setLayout(aBoxLay);
99
100   showAdditionalButtons(false);
101   connect(this, SIGNAL(toggled(bool)), this, SLOT(showAdditionalButtons(bool)));
102   connect(this, SIGNAL(changed()), this, SLOT(actionStateChanged()));
103   return myButtonFrame;
104 }
105
106 bool SHAPERGUI_NestedButton::event(QEvent* theEvent)
107 {
108   if (theEvent->type() == QEvent::ActionChanged) {
109     if (myThisButton) {
110       myThisButton->setEnabled(isEnabled());
111       return true;
112     }
113   }
114   return QWidgetAction::event(theEvent);
115 }
116
117
118 void SHAPERGUI_NestedButton::actionStateChanged()
119 {
120   if (isEnabled()) {
121     QString s = "true";
122   } else {
123     QString s = "false";
124   }
125
126 }