Salome HOME
Hide faces: checking presentation on having any shapes using a whole container of...
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetChoice.cpp
index ce664179fb40611472f9037f2c24d1cee49827a6..7ef0cfb540f37b5c824e455c2b53eafe3da49fef 100644 (file)
 #include "ModuleBase_IconFactory.h"
 
 #include <ModelAPI_AttributeInteger.h>
+#include <ModelAPI_AttributeStringArray.h>
 #include <ModelAPI_Data.h>
 #include <Config_WidgetAPI.h>
+#include <Config_PropManager.h>
 
 #include <QWidget>
 #include <QLayout>
@@ -35,7 +37,6 @@
 #include <QRadioButton>
 #include <QToolButton>
 
-
 ModuleBase_WidgetChoice::ModuleBase_WidgetChoice(QWidget* theParent,
                                                  const Config_WidgetAPI* theData)
 : ModuleBase_ModelWidget(theParent, theData), myCombo(0), myButtons(0)
@@ -51,10 +52,16 @@ ModuleBase_WidgetChoice::ModuleBase_WidgetChoice(QWidget* theParent,
   foreach(QString aType, QString(aTypes.c_str()).split(' ')) {
     aList.append(translate(aType.toStdString()));
   }
-
+  if (aTypes.empty()) {
+    myStringListAttribute = theData->getProperty("string_list_attribute");
+    if (!myStringListAttribute.empty())
+      aList.clear();
+  }
   if (theData->getBooleanAttribute("use_in_title", false))
     myButtonTitles = aList;
 
+  bool aHasDefaultValue;
+  int aDefaultVal = QString::fromStdString(getDefaultValue()).toInt(&aHasDefaultValue);
   // Widget type can be combobox or radiobuttons
   std::string aWgtType = theData->getProperty("widget_type");
   if ((aWgtType.length() > 0) && (aWgtType == "radiobuttons")) {
@@ -97,7 +104,8 @@ ModuleBase_WidgetChoice::ModuleBase_WidgetChoice(QWidget* theParent,
         myButtons->addButton(aBtn, aId++);
       }
     }
-    myButtons->button(0)->setChecked(true);
+    int aCheckedId = aHasDefaultValue ? aDefaultVal : 0;
+    myButtons->button(aDefaultVal)->setChecked(true);
     connect(myButtons, SIGNAL(buttonClicked(int)), this, SLOT(onCurrentIndexChanged(int)));
   } else {
     myLabel = new QLabel(aLabelText, this);
@@ -115,6 +123,9 @@ ModuleBase_WidgetChoice::ModuleBase_WidgetChoice(QWidget* theParent,
 
     myCombo->addItems(aList);
 
+    if (aHasDefaultValue && aDefaultVal < aList.size())
+      myCombo->setCurrentIndex(aDefaultVal);
+
     connect(myCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(onCurrentIndexChanged(int)));
   }
 }
@@ -144,11 +155,25 @@ bool ModuleBase_WidgetChoice::restoreValueCustom()
   if (aIntAttr->value() != -1) {
     if (myCombo) {
       bool isBlocked = myCombo->blockSignals(true);
+      if (myCombo->count() == 0 && !myStringListAttribute.empty()) {
+        AttributeStringArrayPtr aStrAttr = aData->stringArray(myStringListAttribute);
+        if (aStrAttr) {
+          for (int i = 0; i < aStrAttr->size(); i++) {
+            myCombo->insertItem(i, aStrAttr->value(i).c_str());
+          }
+        }
+      }
       myCombo->setCurrentIndex(aIntAttr->value());
       myCombo->blockSignals(isBlocked);
     } else {
       bool isBlocked = myButtons->blockSignals(true);
-      myButtons->button(aIntAttr->value())->setChecked(true);
+      if (aIntAttr->isInitialized())
+        myButtons->button(aIntAttr->value())->setChecked(true);
+      else {
+        bool aHasDefaultValue;
+        int aDefaultVal = QString::fromStdString(getDefaultValue()).toInt(&aHasDefaultValue);
+        myButtons->button(aHasDefaultValue ? aDefaultVal : 0)->setChecked(true);
+      }
       myButtons->blockSignals(isBlocked);
       emit itemSelected(this, aIntAttr->value());
     }