]> SALOME platform Git repositories - modules/shaper.git/commitdiff
Salome HOME
Provide a Choice control
authorvsv <vsv@opencascade.com>
Mon, 4 Dec 2017 13:31:19 +0000 (16:31 +0300)
committervsv <vsv@opencascade.com>
Mon, 4 Dec 2017 13:31:19 +0000 (16:31 +0300)
src/ModuleBase/CMakeLists.txt
src/ModuleBase/ModuleBase_ChoiceCtrl.cpp [new file with mode: 0644]
src/ModuleBase/ModuleBase_ChoiceCtrl.h [new file with mode: 0644]
src/ModuleBase/ModuleBase_WidgetChoice.cpp
src/ModuleBase/ModuleBase_WidgetChoice.h
src/XGUI/pictures/edge32.png [new file with mode: 0644]
src/XGUI/pictures/face32.png [new file with mode: 0644]
src/XGUI/pictures/solid32.png [new file with mode: 0644]
src/XGUI/pictures/vertex32.png [new file with mode: 0644]

index cb40f7222e31aa572d7f5efde477570c881dae8a..d536ef23d12246754133dcb4e2c66866f7a65235 100644 (file)
@@ -94,6 +94,7 @@ SET(PROJECT_HEADERS
   ModuleBase_IconFactory.h
   ModuleBase_Dialog.h
   ModuleBase_ModelDialogWidget.h
+  ModuleBase_ChoiceCtrl.h
 )
 
 SET(PROJECT_MOC_HEADERS
@@ -138,6 +139,7 @@ SET(PROJECT_MOC_HEADERS
   ModuleBase_WidgetSwitch.h
   ModuleBase_WidgetToolbox.h
   ModuleBase_WidgetValidated.h
+  ModuleBase_ChoiceCtrl.h
 )
 
 SET(PROJECT_SOURCES
@@ -200,6 +202,7 @@ SET(PROJECT_SOURCES
   ModuleBase_IconFactory.cpp
   ModuleBase_SelectionValidator.cpp
   ModuleBase_Dialog.cpp
+  ModuleBase_ChoiceCtrl.cpp
 )
 
 SET(PROJECT_LIBRARIES
diff --git a/src/ModuleBase/ModuleBase_ChoiceCtrl.cpp b/src/ModuleBase/ModuleBase_ChoiceCtrl.cpp
new file mode 100644 (file)
index 0000000..4d915b8
--- /dev/null
@@ -0,0 +1,175 @@
+// Copyright (C) 2014-2017  CEA/DEN, EDF R&D
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+//
+// See http://www.salome-platform.org/ or
+// email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
+//
+
+#include "ModuleBase_ChoiceCtrl.h"
+#include "ModuleBase_Tools.h"
+#include "ModuleBase_IconFactory.h"
+
+#include <QLayout>
+#include <QLabel>
+#include <QComboBox>
+#include <QGroupBox>
+#include <QButtonGroup>
+#include <QRadioButton>
+#include <QToolButton>
+
+ModuleBase_ChoiceCtrl::ModuleBase_ChoiceCtrl(QWidget* theParent,
+                                             const QStringList& theChoiceList,
+                                             const QStringList& theIconsList,
+                                             ControlType theType,
+                                             Qt::Orientation theButtonsDir)
+ : QWidget(theParent), myType(theType)
+{
+  QHBoxLayout* aLayout = new QHBoxLayout(this);
+  ModuleBase_Tools::adjustMargins(aLayout);
+
+  switch (myType) {
+  case RadioButtons:
+    {
+      myButtons = new QButtonGroup(this);
+      myGroupBox = new QGroupBox("", this);
+      aLayout->addWidget(myGroupBox);
+
+      QLayout* aBtnLayout = 0;
+      switch (theButtonsDir) {
+      case Qt::Horizontal:
+        aBtnLayout = new QHBoxLayout(myGroupBox);
+        break;
+      case Qt::Vertical:
+        aBtnLayout = new QVBoxLayout(myGroupBox);
+        break;
+      }
+      ModuleBase_Tools::adjustMargins(aBtnLayout);
+
+      if (theIconsList.length() == theChoiceList.length()) {
+        int aId = 0;
+        foreach(QString aBtnTxt, theChoiceList) {
+          QToolButton* aBtn = new QToolButton(myGroupBox);
+          aBtn->setFocusPolicy(Qt::StrongFocus);
+          aBtn->setCheckable(true);
+          aBtn->setToolTip(aBtnTxt);
+
+          QPixmap aIcon = ModuleBase_IconFactory::loadPixmap(theIconsList.at(aId));
+          aBtn->setIcon(aIcon);
+          aBtn->setIconSize(aIcon.size());
+
+          aBtnLayout->addWidget(aBtn);
+          myButtons->addButton(aBtn, aId++);
+        }
+      } else {
+        int aId = 0;
+        foreach(QString aBtnTxt, theChoiceList) {
+          QRadioButton* aBtn = new QRadioButton(aBtnTxt, myGroupBox);
+          aBtnLayout->addWidget(aBtn);
+          myButtons->addButton(aBtn, aId++);
+        }
+      }
+      connect(myButtons, SIGNAL(buttonClicked(int)), this, SIGNAL(valueChanged(int)));
+    }
+    break;
+  case ComboBox:
+    myLabel = new QLabel("", this);
+    aLayout->addWidget(myLabel);
+
+    //std::string aToolstr = theData->widgetTooltip();
+    //if (!aToolstr.empty()) {
+    //  myLabel->setToolTip(QString::fromStdString(aToolstr));
+    //}
+
+    myCombo = new QComboBox(this);
+    aLayout->addWidget(myCombo, 1);
+
+    myCombo->addItems(theChoiceList);
+    connect(myCombo, SIGNAL(currentIndexChanged(int)), this, SIGNAL(valueChanged(int)));
+    break;
+  }
+}
+
+void ModuleBase_ChoiceCtrl::setLabel(const QString& theText)
+{
+  switch (myType) {
+  case RadioButtons:
+    myGroupBox->setTitle(theText);
+    break;
+  case ComboBox:
+    myLabel->setText(theText);
+    break;
+  }
+}
+
+void ModuleBase_ChoiceCtrl::setLabelIcon(const QString& theIcon)
+{
+  if (myType == ComboBox)
+    myLabel->setPixmap(ModuleBase_IconFactory::loadPixmap(theIcon));
+}
+
+void ModuleBase_ChoiceCtrl::setValue(int theVal)
+{
+  switch (myType) {
+  case RadioButtons:
+    myButtons->button(theVal)->setChecked(true);
+    break;
+  case ComboBox:
+    myCombo->setCurrentIndex(theVal);
+    break;
+  }
+}
+
+void ModuleBase_ChoiceCtrl::setTooltip(QString theTip)
+{
+  if (myType == ComboBox)
+    myLabel->setToolTip(theTip);
+}
+
+int ModuleBase_ChoiceCtrl::value() const
+{
+  switch (myType) {
+  case RadioButtons:
+    return myButtons->checkedId();
+  case ComboBox:
+    return myCombo->currentIndex();
+  }
+  return -1;
+}
+
+bool ModuleBase_ChoiceCtrl::focusTo()
+{
+  if (myType == ComboBox)
+    ModuleBase_Tools::setFocus(myCombo, "ModuleBase_WidgetChoice::focusTo()");
+  else
+    return false;
+  return true;
+}
+
+QList<QWidget*> ModuleBase_ChoiceCtrl::getControls() const
+{
+  QList<QWidget*> aControls;
+  if (myType == ComboBox)
+    aControls.append(myCombo);
+  return aControls;
+}
+
+void ModuleBase_ChoiceCtrl::setChoiceList(const QStringList& theChoiceList)
+{
+  if (myType == ComboBox) {
+    myCombo->clear();
+    myCombo->addItems(theChoiceList);
+  }
+}
diff --git a/src/ModuleBase/ModuleBase_ChoiceCtrl.h b/src/ModuleBase/ModuleBase_ChoiceCtrl.h
new file mode 100644 (file)
index 0000000..3f9c5de
--- /dev/null
@@ -0,0 +1,77 @@
+// Copyright (C) 2014-2017  CEA/DEN, EDF R&D
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+//
+// See http://www.salome-platform.org/ or
+// email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
+//
+
+#ifndef ModuleBase_ChoiceCtrl_H
+#define ModuleBase_ChoiceCtrl_H
+
+#include "ModuleBase.h"
+
+#include <QWidget>
+#include <QStringList>
+#include <QList>
+
+class QLabel;
+class QComboBox;
+class QGroupBox;
+class QButtonGroup;
+
+class MODULEBASE_EXPORT ModuleBase_ChoiceCtrl: public QWidget
+{
+Q_OBJECT
+public:
+  enum ControlType {
+    RadioButtons,
+    ComboBox
+  };
+
+  ModuleBase_ChoiceCtrl(QWidget* theParent,
+                        const QStringList& theChoiceList,
+                        const QStringList& theIconsList,
+                        ControlType theType = RadioButtons,
+                        Qt::Orientation theButtonsDir = Qt::Horizontal);
+
+  void setLabel(const QString& theText);
+
+  void setLabelIcon(const QString& theIcon);
+
+  void setValue(int theVal);
+
+  void setTooltip(QString theTip);
+
+  int value() const;
+
+  bool focusTo();
+
+  QList<QWidget*> getControls() const;
+
+  void setChoiceList(const QStringList& theChoiceList);
+
+signals:
+  void valueChanged(int theVal);
+
+private:
+  ControlType myType;
+  QLabel* myLabel;
+  QComboBox* myCombo;
+  QGroupBox* myGroupBox;
+  QButtonGroup* myButtons;
+};
+
+#endif
\ No newline at end of file
index 7ef0cfb540f37b5c824e455c2b53eafe3da49fef..c259ed3247a00f3f87783b59bb5e973b5a62f654 100644 (file)
@@ -21,6 +21,7 @@
 #include "ModuleBase_WidgetChoice.h"
 #include "ModuleBase_Tools.h"
 #include "ModuleBase_IconFactory.h"
+#include "ModuleBase_ChoiceCtrl.h"
 
 #include <ModelAPI_AttributeInteger.h>
 #include <ModelAPI_AttributeStringArray.h>
 
 ModuleBase_WidgetChoice::ModuleBase_WidgetChoice(QWidget* theParent,
                                                  const Config_WidgetAPI* theData)
-: ModuleBase_ModelWidget(theParent, theData), myCombo(0), myButtons(0)
+: ModuleBase_ModelWidget(theParent, theData)//, myCombo(0), myButtons(0)
 {
-  QHBoxLayout* aLayout = new QHBoxLayout(this);
-  ModuleBase_Tools::adjustMargins(aLayout);
-
   QString aLabelText = translate(theData->widgetLabel());
   QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
   std::string aTypes = theData->getProperty("string_list");
@@ -62,72 +60,28 @@ ModuleBase_WidgetChoice::ModuleBase_WidgetChoice(QWidget* theParent,
 
   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")) {
-    myButtons = new QButtonGroup(this);
-    QGroupBox* aGroupBox = new QGroupBox(aLabelText, this);
-    aLayout->addWidget(aGroupBox);
-
-
-    QLayout* aBtnLayout = 0;
-    std::string aWgtDir = theData->getProperty("buttons_dir");
-    if (aWgtDir == "horizontal")
-      aBtnLayout = new QHBoxLayout(aGroupBox);
-    else
-      aBtnLayout = new QVBoxLayout(aGroupBox);
-    ModuleBase_Tools::adjustMargins(aBtnLayout);
-
-    std::string aIcons = theData->getProperty("icons_list");
-    QStringList aIconList = QString(aIcons.c_str()).split(' ');
-    if (aIconList.length() == aList.length()) {
-      int aId = 0;
-      foreach(QString aBtnTxt, aList) {
-        QToolButton* aBtn = new QToolButton(aGroupBox);
-        aBtn->setFocusPolicy(Qt::StrongFocus);
-        aBtn->setCheckable(true);
-        aBtn->setToolTip(aBtnTxt);
-
-        QPixmap aIcon = ModuleBase_IconFactory::loadPixmap(aIconList.at(aId));
-        aBtn->setIcon(aIcon);
-        aBtn->setIconSize(aIcon.size());
-
-        aBtnLayout->addWidget(aBtn);
-        myButtons->addButton(aBtn, aId++);
-      }
+  std::string aIcons = theData->getProperty("icons_list");
+  QStringList aIconList = QString(aIcons.c_str()).split(' ');
 
-    } else {
-      int aId = 0;
-      foreach(QString aBtnTxt, aList) {
-        QRadioButton* aBtn = new QRadioButton(aBtnTxt, aGroupBox);
-        aBtnLayout->addWidget(aBtn);
-        myButtons->addButton(aBtn, aId++);
-      }
-    }
-    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);
-    if (!aLabelIcon.isEmpty())
-      myLabel->setPixmap(ModuleBase_IconFactory::loadPixmap(aLabelIcon));
-    aLayout->addWidget(myLabel);
-
-    std::string aToolstr = theData->widgetTooltip();
-    if (!aToolstr.empty()) {
-      myLabel->setToolTip(QString::fromStdString(aToolstr));
-    }
+  std::string aWgtDir = theData->getProperty("buttons_dir");
 
-    myCombo = new QComboBox(this);
-    aLayout->addWidget(myCombo, 1);
+  QHBoxLayout* aLayout = new QHBoxLayout(this);
+  myChoiceCtrl =  new ModuleBase_ChoiceCtrl(this, aList, aIconList,
+    (aWgtType == "radiobuttons")? ModuleBase_ChoiceCtrl::RadioButtons : ModuleBase_ChoiceCtrl::ComboBox,
+    (aWgtDir == "horizontal")? Qt::Horizontal : Qt::Vertical);
+  myChoiceCtrl->setLabel(aLabelText);
 
-    myCombo->addItems(aList);
+  if (!aLabelIcon.isEmpty())
+    myChoiceCtrl->setLabelIcon(aLabelIcon);
 
-    if (aHasDefaultValue && aDefaultVal < aList.size())
-      myCombo->setCurrentIndex(aDefaultVal);
+  connect(myChoiceCtrl, SIGNAL(valueChanged(int)), this, SLOT(onCurrentIndexChanged(int)));
 
-    connect(myCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(onCurrentIndexChanged(int)));
-  }
+  int aCheckedId = aHasDefaultValue ? aDefaultVal : 0;
+  myChoiceCtrl->setValue(aCheckedId);
+  aLayout->addWidget(myChoiceCtrl);
 }
 
 ModuleBase_WidgetChoice::~ModuleBase_WidgetChoice()
@@ -139,10 +93,7 @@ bool ModuleBase_WidgetChoice::storeValueCustom()
   DataPtr aData = myFeature->data();
   std::shared_ptr<ModelAPI_AttributeInteger> aIntAttr = aData->integer(attributeID());
 
-  if (myCombo)
-    aIntAttr->setValue(myCombo->currentIndex());
-  else
-    aIntAttr->setValue(myButtons->checkedId());
+  aIntAttr->setValue(myChoiceCtrl->value());
   updateObject(myFeature);
   return true;
 }
@@ -153,54 +104,38 @@ bool ModuleBase_WidgetChoice::restoreValueCustom()
   std::shared_ptr<ModelAPI_AttributeInteger> aIntAttr = aData->integer(attributeID());
 
   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());
-          }
+    bool isBlocked = myChoiceCtrl->blockSignals(true);
+    if (!myStringListAttribute.empty()) {
+      AttributeStringArrayPtr aStrAttr = aData->stringArray(myStringListAttribute);
+      QStringList aChoiceList;
+      if (aStrAttr) {
+        for (int i = 0; i < aStrAttr->size(); i++) {
+          aChoiceList << aStrAttr->value(i).c_str();
         }
+        myChoiceCtrl->setChoiceList(aChoiceList);
       }
-      myCombo->setCurrentIndex(aIntAttr->value());
-      myCombo->blockSignals(isBlocked);
-    } else {
-      bool isBlocked = myButtons->blockSignals(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());
     }
+    if (aIntAttr->isInitialized())
+      myChoiceCtrl->setValue(aIntAttr->value());
+    else {
+      bool aHasDefaultValue;
+      int aDefaultVal = QString::fromStdString(getDefaultValue()).toInt(&aHasDefaultValue);
+      myChoiceCtrl->setValue(aHasDefaultValue ? aDefaultVal : 0);
+    }
+    myChoiceCtrl->blockSignals(isBlocked);
+    emit itemSelected(this, aIntAttr->value());
   }
   return true;
 }
 
 bool ModuleBase_WidgetChoice::focusTo()
 {
-  if (myCombo)
-    ModuleBase_Tools::setFocus(myCombo, "ModuleBase_WidgetChoice::focusTo()");
-  else
-    return false;
-  return true;
+  return myChoiceCtrl->focusTo();
 }
 
 QList<QWidget*> ModuleBase_WidgetChoice::getControls() const
 {
-  QList<QWidget*> aControls;
-  if (myCombo)
-    aControls.append(myCombo);
-  //else {
-  //  //foreach(QAbstractButton* aBtn, myButtons->buttons())
-  //  //if (myButtons->checkedId() != -1)
-  //  //  aControls.append(myButtons->button(myButtons->checkedId()));
-  //}
-  return aControls;
+  return myChoiceCtrl->getControls();
 }
 
 QString ModuleBase_WidgetChoice::getPropertyPanelTitle(int theIndex)
index a6ac35cfcf3eb5aa6205a21de6791d44ff202342..de5b0e5657878a9aa15fbc33a58f8d11cb211253 100644 (file)
@@ -25,9 +25,7 @@
 #include "ModuleBase_ModelWidget.h"
 
 class QWidget;
-class QLabel;
-class QComboBox;
-class QButtonGroup;
+class ModuleBase_ChoiceCtrl;
 
 /**
 * \ingroup GUI
@@ -92,11 +90,12 @@ private slots:
 
 private:
   /// The label
-  QLabel* myLabel;
+  //QLabel* myLabel;
 
   /// The control
-  QComboBox* myCombo;
-  QButtonGroup* myButtons;
+  //QComboBox* myCombo;
+  //QButtonGroup* myButtons;
+  ModuleBase_ChoiceCtrl* myChoiceCtrl;
 
   // XML definition of titles
   QStringList myButtonTitles;
diff --git a/src/XGUI/pictures/edge32.png b/src/XGUI/pictures/edge32.png
new file mode 100644 (file)
index 0000000..cb8bc1d
Binary files /dev/null and b/src/XGUI/pictures/edge32.png differ
diff --git a/src/XGUI/pictures/face32.png b/src/XGUI/pictures/face32.png
new file mode 100644 (file)
index 0000000..44bc549
Binary files /dev/null and b/src/XGUI/pictures/face32.png differ
diff --git a/src/XGUI/pictures/solid32.png b/src/XGUI/pictures/solid32.png
new file mode 100644 (file)
index 0000000..6db2ffd
Binary files /dev/null and b/src/XGUI/pictures/solid32.png differ
diff --git a/src/XGUI/pictures/vertex32.png b/src/XGUI/pictures/vertex32.png
new file mode 100644 (file)
index 0000000..edce228
Binary files /dev/null and b/src/XGUI/pictures/vertex32.png differ