Salome HOME
ModuleBase_WidgetChoice is extended to use in a combo box a list of values read from...
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetChoice.cpp
index ce664179fb40611472f9037f2c24d1cee49827a6..0930fa73b78497327fdb54d7b6d761a0f9174f46 100644 (file)
 #include <ModelAPI_AttributeInteger.h>
 #include <ModelAPI_Data.h>
 #include <Config_WidgetAPI.h>
+#include <Config_PropManager.h>
 
+#include <QDir>
+#include <QFile>
 #include <QWidget>
 #include <QLayout>
 #include <QLabel>
 #include <QButtonGroup>
 #include <QGroupBox>
 #include <QRadioButton>
+#include <QTextStream>
 #include <QToolButton>
 
+void getValues(const std::string& thePath, const std::string& theFileName,
+               QStringList& theValues)
+{
+  QString aFileName = thePath.c_str();
+  aFileName += QDir::separator();
+  aFileName += theFileName.c_str();
+
+  QFile aFile(aFileName);
+  if (!aFile.open(QIODevice::ReadOnly | QIODevice::Text))
+    return;
+
+  QTextStream aStream(&aFile);
+  while (!aStream.atEnd()) {
+    QString aLine = aStream.readLine();
+    if (!aLine.isEmpty())
+      theValues.append(aLine);
+  }
+}
 
 ModuleBase_WidgetChoice::ModuleBase_WidgetChoice(QWidget* theParent,
                                                  const Config_WidgetAPI* theData)
@@ -51,10 +73,20 @@ ModuleBase_WidgetChoice::ModuleBase_WidgetChoice(QWidget* theParent,
   foreach(QString aType, QString(aTypes.c_str()).split(' ')) {
     aList.append(translate(aType.toStdString()));
   }
+  if (aTypes.empty()) {
+    aList.clear();
+    std::string aFileName = theData->getProperty("file_name");
+    if (!aFileName.empty()) {
+      std::string aPath = Config_PropManager::string("Plugins", "combo_box_elements_path");
+      getValues(aPath, aFileName, aList);
+    }
+  }
 
   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 +129,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 +148,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)));
   }
 }
@@ -148,7 +184,13 @@ bool ModuleBase_WidgetChoice::restoreValueCustom()
       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());
     }