Salome HOME
refs #98 - Default color for constraints
authornds <natalia.donis@opencascade.com>
Fri, 24 Oct 2014 10:09:42 +0000 (14:09 +0400)
committernds <natalia.donis@opencascade.com>
Fri, 24 Oct 2014 10:09:42 +0000 (14:09 +0400)
refs #129 - old LightApprc.7.4.0 problem
The default values save mechanizm is implemented.

src/Config/Config_Prop.cpp
src/Config/Config_Prop.h
src/Config/Config_PropManager.cpp
src/NewGeom/NewGeom_Module.cpp
src/NewGeom/NewGeom_Module.h
src/SketchPlugin/SketchPlugin_Plugin.cpp
src/SketchPlugin/SketchPlugin_Sketch.cpp
src/XGUI/XGUI_Preferences.cpp
src/XGUI/XGUI_Preferences.h

index 7904021b62b22ec9bf520ecf554f51e3bea16900..6de7627b04eb94c056b0ed18b42c098900eebc82 100644 (file)
@@ -16,3 +16,10 @@ void Config_Prop::setValue(const std::string& theValue)
       new Events_Message(aChangedEvent, this)));
   }
 }
+
+void Config_Prop::setDefaultValue(const std::string& theValue)
+{
+  if (theValue != myDefaultValue) {
+    myDefaultValue = theValue;
+  }
+}
index f4ea0138b802d6173fbd213ae8998b83aa8c4036..4430bade8b68c4c6f655c2201a37c23f640e57b0 100644 (file)
@@ -45,16 +45,17 @@ class Config_Prop
    * \param theSection - name of section (domain of using) of the property.
    * \param theName - name (title) of the value.
    * \param theType - type of the value.
-   * \param theValue - initial value of the property.
+   * \param theDefaultValue - default value of the property. This is an initial property value
    */
   Config_Prop(const std::string& theSection, const std::string& theName,
-              const std::string& theTitle, PropType theType, const std::string& theValue)
+              const std::string& theTitle, PropType theType, const std::string& theDefaultValue)
   {
     mySection = theSection;
     myName = theName;
     myTitle = theTitle;
     myType = theType;
-    myValue = theValue;
+    myValue = theDefaultValue;
+    myDefaultValue = theDefaultValue;
   }
 
   std::string section() const
@@ -90,6 +91,13 @@ class Config_Prop
   }
 
   CONFIG_EXPORT void setValue(const std::string& theValue);
+  
+  std::string defaultValue() const
+  {
+    return myDefaultValue;
+  }
+
+  CONFIG_EXPORT void setDefaultValue(const std::string& theValue);
 
   bool operator==(const Config_Prop* theProp) const
   {
@@ -102,6 +110,7 @@ class Config_Prop
   std::string myTitle;
   PropType myType;
   std::string myValue;
+  std::string myDefaultValue;
 };
 
 typedef std::list<Config_Prop*> Config_Properties;
index c296cc864772f6b0c66a95cc8bd7b8d07f47898d..e6f800c27f649ba7c91b23f5c4ac701ff5a79e59 100644 (file)
@@ -10,22 +10,29 @@ double stringToDouble(const std::string& theDouble);
 
 Config_Properties Config_PropManager::myProps;
 
-bool Config_PropManager::registerProp(const std::string& theSection, const std::string& theName,
-                                      const std::string& theTitle, Config_Prop::PropType theType,
-                                      const std::string& theValue)
+Config_Prop* Config_PropManager::registerProp(const std::string& theSection, const std::string& theName,
+                                              const std::string& theTitle, Config_Prop::PropType theType,
+                                              const std::string& theDefaultValue)
 {
   Config_Prop* aProp = findProp(theSection, theName);
+
   if (aProp) {
+    if (aProp->value() == "") {
+      aProp->setValue(theDefaultValue);
+    }
+    if (aProp->defaultValue() == "") {
+      aProp->setDefaultValue(theDefaultValue);
+    }
     if (aProp->type() == Config_Prop::Disabled) {
       aProp->setType(theType);
       aProp->setTitle(theTitle);
-      return true;
     }
-    return false;
   }
-  aProp = new Config_Prop(theSection, theName, theTitle, theType, theValue);
-  myProps.push_back(aProp);
-  return true;
+  else {
+    aProp = new Config_Prop(theSection, theName, theTitle, theType, theDefaultValue);
+    myProps.push_back(aProp);
+  }
+  return aProp;
 }
 
 Config_Prop* Config_PropManager::findProp(const std::string& theSection, const std::string& theName)
index 8311442901c7f0a3f5345abbcdb0d9c6d7985ba1..5d614069f0268226d3613ee501afe2137f5c7c55 100644 (file)
@@ -87,6 +87,9 @@ void NewGeom_Module::initialize(CAM_Application* theApp)
   LightApp_Module::initialize(theApp);
 
   myWorkshop->startApplication();
+  LightApp_Application* anApp = dynamic_cast<LightApp_Application*>(theApp);
+  if (anApp)
+    connect(anApp, SIGNAL(preferenceResetToDefaults()), this, SLOT(onDefaultPreferences()));
 }
 
 //******************************************************
@@ -176,6 +179,17 @@ void NewGeom_Module::onViewManagerAdded(SUIT_ViewManager* theMgr)
   }
 }
 
+//******************************************************
+void NewGeom_Module::onDefaultPreferences()
+{
+  XGUI_Preferences::resetConfig();
+  XGUI_Preferences::updateResourcesByConfig();
+
+  LightApp_Preferences* pref = preferences();
+  if (pref)
+    pref->retrieve();
+}
+
 //******************************************************
 NewGeom_OCCSelector* NewGeom_Module::createSelector(SUIT_ViewManager* theMgr)
 {
@@ -326,7 +340,7 @@ void NewGeom_Module::createPreferences()
   LightApp_Preferences* pref = preferences();
   if (!pref)
     return;
-  XGUI_Preferences::updateCustomProps();
+  XGUI_Preferences::updateConfigByResources();
   QString aModName = moduleName();
 
   QtxPreferenceItem* item = pref->findItem(aModName, true );
@@ -348,8 +362,16 @@ void NewGeom_Module::preferencesChanged(const QString& theSection, const QString
 {
   SUIT_ResourceMgr* aResMgr = application()->resourceMgr();
   QString aVal = aResMgr->stringValue(theSection, theParam);
-  if (!aVal.isNull()) {
-    Config_Prop* aProp = Config_PropManager::findProp(theSection.toStdString(), theParam.toStdString());
-    aProp->setValue(aVal.toStdString());
+  Config_Prop* aProp = Config_PropManager::findProp(theSection.toStdString(), theParam.toStdString());
+  std::string aValue = aVal.toStdString();
+  if (aValue.empty()) {
+    aValue = aProp->defaultValue();
+    aResMgr->setValue(theSection, theParam, QString(aValue.c_str()));
+
+    LightApp_Preferences* pref = preferences();
+    if (pref)
+      pref->retrieve();
   }
+  aProp->setValue(aValue);
+
 }
index 9bda5d276e224f1856bd7ae543a08a5ed26093dd..9da3e31af221efd2fd7d52c19f2fbde2d92e1e34 100644 (file)
@@ -85,6 +85,7 @@ Q_OBJECT
 
  protected slots:
   virtual void onViewManagerAdded(SUIT_ViewManager* theMgr);
+  void onDefaultPreferences();
 
  protected:
   CAM_DataModel* createDataModel();
index e3a324300e78211516b20ba246356ec6b1c692f9..cf3cf283b9e6780efd3f882697d3a9c4182b00ef 100644 (file)
@@ -29,7 +29,7 @@ SketchPlugin_Plugin::SketchPlugin_Plugin()
   SessionPtr aMgr = ModelAPI_Session::get();
   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
   aFactory->registerValidator("SketchPlugin_DistanceAttr",
-                              new SketchPlugin_DistanceAttrValidator);
+                              new SketchPlugin_DistanceAttrValidator);  
   aFactory->registerValidator("SketchPlugin_DifferentObjects",
                               new SketchPlugin_DifferentObjectsValidator);
   aFactory->registerValidator("SketchPlugin_ResultPoint", new SketchPlugin_ResultPointValidator);
@@ -44,7 +44,7 @@ SketchPlugin_Plugin::SketchPlugin_Plugin()
                                    SKETCH_PLANE_COLOR);
   Config_PropManager::registerProp("Sketch planes", "planes_size", "Size", Config_Prop::Double,
                                    PLANE_SIZE);
-  Config_PropManager::registerProp("Sketch planes", "planes_thikness", "Thickness",
+  Config_PropManager::registerProp("Sketch planes", "planes_thickness", "Thickness",
                                    Config_Prop::Integer, SKETCH_WIDTH);
 
   Config_PropManager::registerProp("Visualization", "parallel_color", "Parallel constraint color",
index 19715af301878f9e0bc47cb25cebcef8a7071d07..19098b784e60b19f057bd9d27061ca147df93884 100644 (file)
@@ -194,7 +194,7 @@ AISObjectPtr SketchPlugin_Sketch::getAISObject(AISObjectPtr thePrevious)
       SKETCH_PLANE_COLOR);
       aAIS->setColor(aRGB[0], aRGB[1], aRGB[2]);
 
-      aAIS->setWidth(Config_PropManager::integer("Sketch planes", "planes_thikness",
+      aAIS->setWidth(Config_PropManager::integer("Sketch planes", "planes_thickness",
       SKETCH_WIDTH));
     }
     return aAIS;
index 81e0282ee7922bd0ac86aeb672517cf4e1abf85d..ed3782b110d0d5cdfddf7b60df4eb06ea878b873 100644 (file)
@@ -13,6 +13,7 @@
 #include <QLayout>
 #include <QApplication>
 #include <QDialogButtonBox>
+#include <QPushButton>
 
 const QString XGUI_Preferences::VIEWER_SECTION = "Viewer";
 const QString XGUI_Preferences::MENU_SECTION = "Menu";
@@ -40,7 +41,7 @@ bool XGUI_Preferences::editPreferences(XGUI_Prefs& theModified)
   return false;
 }
 
-void XGUI_Preferences::updateCustomProps()
+void XGUI_Preferences::updateConfigByResources()
 {
   Config_Properties aProps = Config_PropManager::getProperties();
   Config_Properties::iterator aIt;
@@ -48,8 +49,30 @@ void XGUI_Preferences::updateCustomProps()
     Config_Prop* aProp = (*aIt);
     QString aVal = myResourceMgr->stringValue(QString(aProp->section().c_str()),
                                               QString(aProp->name().c_str()));
-    if (!aVal.isNull())
+    if (!aVal.isEmpty()) {
       aProp->setValue(aVal.toStdString());
+    }
+  }
+}
+
+void XGUI_Preferences::updateResourcesByConfig()
+{
+  Config_Properties aProps = Config_PropManager::getProperties();
+  Config_Properties::iterator aIt;
+  for (aIt = aProps.begin(); aIt != aProps.end(); ++aIt) {
+    Config_Prop* aProp = (*aIt);
+    myResourceMgr->setValue(QString(aProp->section().c_str()), QString(aProp->name().c_str()),
+                            QString(aProp->value().c_str()));
+  }
+}
+
+void XGUI_Preferences::resetConfig()
+{
+  Config_Properties aProps = Config_PropManager::getProperties();
+  Config_Properties::iterator aIt;
+  for (aIt = aProps.begin(); aIt != aProps.end(); ++aIt) {
+    Config_Prop* aProp = (*aIt);
+    aProp->setValue(aProp->defaultValue());
   }
 }
 
@@ -63,9 +86,9 @@ void XGUI_Preferences::loadCustomProps()
     QStringList aParams = myResourceMgr->parameters(aSection);
     foreach (QString aParam, aParams)
     {
-      Config_PropManager::registerProp(aSection.toStdString(), aParam.toStdString(), "",
-                                       Config_Prop::Disabled,
-                                       myResourceMgr->stringValue(aSection, aParam).toStdString());
+      Config_Prop* aProp = Config_PropManager::registerProp(aSection.toStdString(),
+                                       aParam.toStdString(), "", Config_Prop::Disabled);
+      aProp->setValue(myResourceMgr->stringValue(aSection, aParam).toStdString());
     }
   }
 }
@@ -150,8 +173,13 @@ XGUI_PreferencesDlg::XGUI_PreferencesDlg(SUIT_ResourceMgr* theResurces, QWidget*
   setFocusProxy(myPreferences);
   myPreferences->setFrameStyle(QFrame::Box | QFrame::Sunken);
 
-  QDialogButtonBox* aBtnBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel,
+  QDialogButtonBox* aBtnBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel |
+                                                   QDialogButtonBox::Reset,
                                                    Qt::Horizontal, this);
+  QPushButton* aDefaultButton = aBtnBox->button(QDialogButtonBox::Reset);
+  aDefaultButton->setText(tr("Default"));
+  connect(aDefaultButton, SIGNAL(clicked()), this, SLOT(onDefault()));
+
   main->addWidget(aBtnBox);
   connect(aBtnBox, SIGNAL(accepted()), this, SLOT(accept()));
   connect(aBtnBox, SIGNAL(rejected()), this, SLOT(reject()));
@@ -230,7 +258,7 @@ void XGUI_PreferencesDlg::accept()
   myIsChanged = true;
 
   // Save custom properties
-  XGUI_Preferences::updateCustomProps();
+  XGUI_Preferences::updateConfigByResources();
   QDialog::accept();
 }
 
@@ -239,6 +267,21 @@ void XGUI_PreferencesDlg::modified(XGUI_Prefs& theModified) const
   theModified = myPreferences->modified();
 }
 
+void XGUI_PreferencesDlg::onDefault()
+{
+  // reset main resources
+  QtxResourceMgr::WorkingMode aPrev = myPreferences->resourceMgr()->setWorkingMode
+                                               (QtxResourceMgr::IgnoreUserValues);
+  myPreferences->retrieve();
+  myPreferences->resourceMgr()->setWorkingMode(aPrev);
+
+  // reset plugin's resources
+  XGUI_Preferences::resetConfig();
+  XGUI_Preferences::updateResourcesByConfig(false);
+
+  myPreferences->retrieve();
+}
+
 //**********************************************************
 //**********************************************************
 //**********************************************************
index a6fbc8a2b2faa2675bb944ff7506e5b531b43745..3241d41423675f2b423629db3e2b2b13be102c0c 100644 (file)
@@ -35,8 +35,15 @@ class XGUI_EXPORT XGUI_Preferences
   /// It is used in case of necessity to define external resource manager (not NewGeom)
   static void setResourceMgr(SUIT_ResourceMgr* theMgr) { myResourceMgr = theMgr; }
 
-  /// Updates properties defined by module from SUIT_ResourceMgr to Config_PropManager
-  static void updateCustomProps();
+  /// Updates Config_PropManager properties by module from SUIT_ResourceMgr
+  static void updateConfigByResources();
+
+  /// Updates SUIT_ResourceMgr values by Config_PropManager properties
+  /// \param theUpdateOnlyInvalid flag to update only invalid values, if it is false, all are updated
+  static void updateResourcesByConfig();
+
+  /// Set default values to the Config_PropManager properties
+  static void resetConfig();
 
   /// Loads properties defined by module to Config_PropManager
   static void loadCustomProps();
@@ -97,6 +104,9 @@ Q_OBJECT
  public slots:
   virtual void accept();
 
+protected slots:
+  void onDefault();
+
  private:
    /// Create editors for aplication properties
    void createEditors();