Salome HOME
Issue #1005: To improve user-friendship of error-messages for features and attributes
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetAction.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        ModuleBase_WidgetAction.cpp
4 // Created:     15 Apr 2016
5 // Author:      Natalia Ermolaeva
6
7 #include <ModuleBase_WidgetAction.h>
8 #include <ModuleBase_Tools.h>
9 #include <ModuleBase_IconFactory.h>
10
11 #include <Config_WidgetAPI.h>
12
13 #include <QWidget>
14 #include <QHBoxLayout>
15 #include <QToolButton>
16
17 #define DEFAULT_TOOL_TIP_WIDTH 300
18
19 ModuleBase_WidgetAction::ModuleBase_WidgetAction(QWidget* theParent,
20                                                  const Config_WidgetAPI* theData)
21 : ModuleBase_ModelWidget(theParent, theData),
22   myActionID(attributeID())
23 {
24   setAttributeID(""); // To prevent errors. Action not stored as attribtue in feature.
25   QHBoxLayout* aControlLay = new QHBoxLayout(this);
26   ModuleBase_Tools::adjustMargins(aControlLay);
27
28   myButton = new QToolButton(this);
29   QString aText = translate(theData->widgetLabel());
30   QString aToolTip = ModuleBase_Tools::wrapTextByWords(translate(theData->widgetTooltip()),
31                                                        myButton,
32                                                        DEFAULT_TOOL_TIP_WIDTH);
33
34   myButton->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
35   aControlLay->addWidget(myButton);
36
37   QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
38   if (!aLabelIcon.isEmpty())
39     myButton->setIcon(ModuleBase_IconFactory::loadPixmap(aLabelIcon));
40   else
41     myButton->setText(aText);
42   myButton->setToolTip(aToolTip);
43
44   connect(myButton, SIGNAL(clicked(bool)), this, SLOT(onActionClicked()));
45 }
46
47 ModuleBase_WidgetAction::~ModuleBase_WidgetAction()
48 {
49 }
50
51 bool ModuleBase_WidgetAction::focusTo()
52 {
53   return false;
54 }
55
56 QList<QWidget*> ModuleBase_WidgetAction::getControls() const
57 {
58   QList<QWidget*> aList;
59   aList.append(myButton);
60   return aList;
61 }
62
63 bool ModuleBase_WidgetAction::storeValueCustom()
64 {
65   return true;
66 }
67
68 bool ModuleBase_WidgetAction::restoreValueCustom()
69 {
70   return true;
71 }
72
73 void ModuleBase_WidgetAction::onActionClicked()
74 {
75   if (myFeature->customAction(myActionID))
76     updateObject(myFeature);
77 }