Salome HOME
Make sources compilable under CentOS
[modules/shaper.git] / src / XGUI / XGUI_Preferences.cpp
index 81fae93d0a9776fdfc6188f7fc911eee78b329d9..4d91ceb34f95f03fff27d739069b10f19ec2c060 100644 (file)
@@ -5,12 +5,15 @@
 #include "XGUI_Preferences.h"
 #include "XGUI_Constants.h"
 
+#include <Config_PropManager.h>
+
 #include <SUIT_ResourceMgr.h>
 #include <SUIT_PreferenceMgr.h>
 
 #include <QLayout>
 #include <QApplication>
 #include <QDialogButtonBox>
+#include <QPushButton>
 
 const QString XGUI_Preferences::VIEWER_SECTION = "Viewer";
 const QString XGUI_Preferences::MENU_SECTION = "Menu";
@@ -38,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;
@@ -46,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())
-      aProp->setValue(qPrintable(aVal));
+    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());
   }
 }
 
@@ -61,13 +86,74 @@ void XGUI_Preferences::loadCustomProps()
     QStringList aParams = myResourceMgr->parameters(aSection);
     foreach (QString aParam, aParams)
     {
-      Config_PropManager::registerProp(qPrintable(aSection), qPrintable(aParam), "",
-                                       Config_Prop::Disabled,
-                                       qPrintable(myResourceMgr->stringValue(aSection, aParam)));
+      Config_Prop* aProp = Config_PropManager::registerProp(aSection.toStdString(),
+                                       aParam.toStdString(), "", Config_Prop::Disabled);
+      aProp->setValue(myResourceMgr->stringValue(aSection, aParam).toStdString());
     }
   }
 }
 
+
+void XGUI_Preferences::createEditContent(XGUI_IPrefMgr* thePref, int thePage)
+{
+  thePref->prefMgr()->setItemIcon(thePage, QIcon(":pictures/module.png"));
+  createCustomPage(thePref, thePage);
+}
+
+
+void XGUI_Preferences::createCustomPage(XGUI_IPrefMgr* thePref, int thePageId)
+{
+  SUIT_ResourceMgr* aResMgr = XGUI_Preferences::resourceMgr();
+  bool isResModified = false;
+
+  // Make a Tab from each section
+  std::list<std::string> aSections = Config_PropManager::getSections();
+  std::list<std::string>::const_iterator it;
+  for (it = aSections.cbegin(); it != aSections.cend(); ++it) {
+    Config_Properties aProps = Config_PropManager::getProperties(*it);
+    int aTab = thePref->prefMgr()->addItem(QString((*it).c_str()), thePageId);
+    thePref->prefMgr()->setItemProperty("columns", 2, aTab);
+
+    Config_Properties::const_iterator aIt;
+    for (aIt = aProps.cbegin(); aIt != aProps.cend(); ++aIt) {
+      Config_Prop* aProp = (*aIt);
+      // check that the property is defined
+      QString aSection(aProp->section().c_str());
+      QString aName(aProp->name().c_str());
+      if (!aResMgr->hasValue(aSection, aName)) {
+        aResMgr->setValue(aSection, aName, QString(aProp->value().c_str()));
+        isResModified = true;
+      }
+      // Add item
+      if (aProp->type() != Config_Prop::Disabled)
+        thePref->addPreference(QObject::tr(aProp->title().c_str()), aTab,
+                               (SUIT_PreferenceMgr::PrefItemType) aProp->type(),
+                               QString(aProp->section().c_str()), QString(aProp->name().c_str()));
+    }
+  }
+}
+
+//**********************************************************
+//**********************************************************
+//**********************************************************
+class XGUI_PrefMgr: public XGUI_IPrefMgr
+{
+public:
+  XGUI_PrefMgr(XGUI_PreferencesMgr* theMgr):myMgr(theMgr) {}
+
+  virtual int addPreference(const QString& theLbl, int pId, 
+                            SUIT_PreferenceMgr::PrefItemType theType,
+                            const QString& theSection, const QString& theName )
+  {
+    return myMgr->addItem(theLbl, pId, theType, theSection, theName);
+  }
+
+  virtual SUIT_PreferenceMgr* prefMgr() const { return myMgr; }
+
+private:
+  XGUI_PreferencesMgr* myMgr;
+};
+
 //**********************************************************
 //**********************************************************
 //**********************************************************
@@ -87,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()));
@@ -112,7 +203,9 @@ void XGUI_PreferencesDlg::createEditors()
 
   aPage = myPreferences->addItem(tr("Module"));
   myPreferences->setItemIcon(aPage, QIcon(":pictures/module.png"));
-  createCustomPage(aPage);
+
+  XGUI_PrefMgr aMgr(myPreferences);
+  XGUI_Preferences::createEditContent(&aMgr, aPage);
 }
 
 void XGUI_PreferencesDlg::createViewerPage(int thePageId)
@@ -159,45 +252,13 @@ void XGUI_PreferencesDlg::createMenuPage(int thePageId)
   myPreferences->setItemProperty("max", 6, aRowsNb);
 }
 
-void XGUI_PreferencesDlg::createCustomPage(int thePageId)
-{
-  SUIT_ResourceMgr* aResMgr = XGUI_Preferences::resourceMgr();
-  bool isResModified = false;
-
-  // Make a Tab from each section
-  std::list<std::string> aSections = Config_PropManager::getSections();
-  std::list<std::string>::const_iterator it;
-  for (it = aSections.cbegin(); it != aSections.cend(); ++it) {
-    Config_Properties aProps = Config_PropManager::getProperties(*it);
-    int aTab = myPreferences->addItem(QString((*it).c_str()), thePageId);
-    myPreferences->setItemProperty("columns", 2, aTab);
-
-    Config_Properties::const_iterator aIt;
-    for (aIt = aProps.cbegin(); aIt != aProps.cend(); ++aIt) {
-      Config_Prop* aProp = (*aIt);
-      // check that the property is defined
-      QString aSection(aProp->section().c_str());
-      QString aName(aProp->name().c_str());
-      if (!aResMgr->hasValue(aSection, aName)) {
-        aResMgr->setValue(aSection, aName, QString(aProp->value().c_str()));
-        isResModified = true;
-      }
-      // Add item
-      if (aProp->type() != Config_Prop::Disabled)
-        myPreferences->addItem(tr(aProp->title().c_str()), aTab,
-                               (SUIT_PreferenceMgr::PrefItemType) aProp->type(),
-                               QString(aProp->section().c_str()), QString(aProp->name().c_str()));
-    }
-  }
-}
-
 void XGUI_PreferencesDlg::accept()
 {
   myPreferences->store();
   myIsChanged = true;
 
   // Save custom properties
-  XGUI_Preferences::updateCustomProps();
+  XGUI_Preferences::updateConfigByResources();
   QDialog::accept();
 }
 
@@ -206,6 +267,22 @@ void XGUI_PreferencesDlg::modified(XGUI_Prefs& theModified) const
   theModified = myPreferences->modified();
 }
 
+void XGUI_PreferencesDlg::onDefault()
+{
+  // reset main resources
+#ifdef WIN32
+  QtxResourceMgr::WorkingMode aPrev =
+      myPreferences->resourceMgr()->setWorkingMode(QtxResourceMgr::IgnoreUserValues);
+  myPreferences->retrieve();
+  myPreferences->resourceMgr()->setWorkingMode(aPrev);
+#endif
+  // reset plugin's resources
+  XGUI_Preferences::resetConfig();
+  XGUI_Preferences::updateResourcesByConfig();
+
+  myPreferences->retrieve();
+}
+
 //**********************************************************
 //**********************************************************
 //**********************************************************