Salome HOME
Wrap tool tip text by word in Widget action
[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
10 #include <Config_WidgetAPI.h>
11
12 #include <QWidget>
13 #include <QHBoxLayout>
14 #include <QToolButton>
15
16 #define DEFAULT_TOOL_TIP_WIDTH 300
17
18 ModuleBase_WidgetAction::ModuleBase_WidgetAction(QWidget* theParent,
19                                                  const Config_WidgetAPI* theData)
20 : ModuleBase_ModelWidget(theParent, theData)
21 {
22   QHBoxLayout* aControlLay = new QHBoxLayout(this);
23   ModuleBase_Tools::adjustMargins(aControlLay);
24
25   myActionID = attributeID();
26   setAttributeID("");
27
28   QString aText = QString::fromStdString(theData->widgetLabel());
29   QString aToolTip = ModuleBase_Tools::wrapTextByWords(
30               QString::fromStdString(theData->widgetTooltip()), myButton, DEFAULT_TOOL_TIP_WIDTH);
31
32   myButton = new QToolButton(this);
33   myButton->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
34   aControlLay->addWidget(myButton);
35
36   QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
37   if (!aLabelIcon.isEmpty())
38     myButton->setIcon(QPixmap(aLabelIcon));
39   else
40     myButton->setText(aText);
41   myButton->setToolTip(aToolTip);
42
43   connect(myButton, SIGNAL(clicked(bool)), this, SLOT(onActionClicked()));
44 }
45
46 ModuleBase_WidgetAction::~ModuleBase_WidgetAction()
47 {
48 }
49
50 QList<QWidget*> ModuleBase_WidgetAction::getControls() const
51 {
52   QList<QWidget*> aList;
53   aList.append(myButton);
54   return aList;
55 }
56
57 bool ModuleBase_WidgetAction::storeValueCustom()
58 {
59   return true;
60 }
61
62 bool ModuleBase_WidgetAction::restoreValueCustom()
63 {
64   return true;
65 }
66
67 void ModuleBase_WidgetAction::onActionClicked()
68 {
69   if (myFeature->customAction(myActionID))
70     updateObject(myFeature);
71 }