Salome HOME
a396cdf5435967e867d959c1def92f001e0f7602
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetAction.cpp
1 // Copyright (C) 2014-2024  CEA, EDF
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 #include <ModuleBase_WidgetAction.h>
21 #include <ModuleBase_Tools.h>
22 #include <ModuleBase_IconFactory.h>
23
24 #include <Config_WidgetAPI.h>
25
26 #include <QWidget>
27 #include <QHBoxLayout>
28 #include <QToolButton>
29
30 #define DEFAULT_TOOL_TIP_WIDTH 300
31
32 ModuleBase_WidgetAction::ModuleBase_WidgetAction(QWidget* theParent,
33                                                  const Config_WidgetAPI* theData)
34 : ModuleBase_ModelWidget(theParent, theData),
35   myActionID(attributeID())
36 {
37   setAttributeID(""); // To prevent errors. Action not stored as attribtue in feature.
38   QHBoxLayout* aControlLay = new QHBoxLayout(this);
39   ModuleBase_Tools::adjustMargins(aControlLay);
40
41   myButton = new QToolButton(this);
42   QString aText = translate(theData->widgetLabel());
43   QString aToolTip = ModuleBase_Tools::wrapTextByWords(translate(theData->widgetTooltip()),
44                                                        myButton,
45                                                        DEFAULT_TOOL_TIP_WIDTH);
46
47   myButton->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
48   aControlLay->addWidget(myButton);
49
50   QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
51   if (!aLabelIcon.isEmpty())
52     myButton->setIcon(ModuleBase_IconFactory::loadPixmap(aLabelIcon));
53   else
54     myButton->setText(aText);
55   myButton->setToolTip(aToolTip);
56
57   connect(myButton, SIGNAL(clicked(bool)), this, SLOT(onActionClicked()));
58 }
59
60 ModuleBase_WidgetAction::~ModuleBase_WidgetAction()
61 {
62 }
63
64 bool ModuleBase_WidgetAction::focusTo()
65 {
66   return false;
67 }
68
69 QList<QWidget*> ModuleBase_WidgetAction::getControls() const
70 {
71   QList<QWidget*> aList;
72   aList.append(myButton);
73   return aList;
74 }
75
76 bool ModuleBase_WidgetAction::storeValueCustom()
77 {
78   return true;
79 }
80
81 bool ModuleBase_WidgetAction::restoreValueCustom()
82 {
83   return true;
84 }
85
86 void ModuleBase_WidgetAction::onActionClicked()
87 {
88   if (myFeature->customAction(myActionID))
89     updateObject(myFeature);
90 }