From 66af250951f489574b0229b9c6a5b18a1b358ac6 Mon Sep 17 00:00:00 2001 From: nds Date: Fri, 24 Oct 2014 14:09:42 +0400 Subject: [PATCH] refs #98 - Default color for constraints refs #129 - old LightApprc.7.4.0 problem The default values save mechanizm is implemented. --- src/Config/Config_Prop.cpp | 7 +++ src/Config/Config_Prop.h | 15 +++++-- src/Config/Config_PropManager.cpp | 23 ++++++---- src/NewGeom/NewGeom_Module.cpp | 30 +++++++++++-- src/NewGeom/NewGeom_Module.h | 1 + src/SketchPlugin/SketchPlugin_Plugin.cpp | 4 +- src/SketchPlugin/SketchPlugin_Sketch.cpp | 2 +- src/XGUI/XGUI_Preferences.cpp | 57 +++++++++++++++++++++--- src/XGUI/XGUI_Preferences.h | 14 +++++- 9 files changed, 126 insertions(+), 27 deletions(-) diff --git a/src/Config/Config_Prop.cpp b/src/Config/Config_Prop.cpp index 7904021b6..6de7627b0 100644 --- a/src/Config/Config_Prop.cpp +++ b/src/Config/Config_Prop.cpp @@ -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; + } +} diff --git a/src/Config/Config_Prop.h b/src/Config/Config_Prop.h index f4ea0138b..4430bade8 100644 --- a/src/Config/Config_Prop.h +++ b/src/Config/Config_Prop.h @@ -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_Properties; diff --git a/src/Config/Config_PropManager.cpp b/src/Config/Config_PropManager.cpp index c296cc864..e6f800c27 100644 --- a/src/Config/Config_PropManager.cpp +++ b/src/Config/Config_PropManager.cpp @@ -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) diff --git a/src/NewGeom/NewGeom_Module.cpp b/src/NewGeom/NewGeom_Module.cpp index 831144290..5d614069f 100644 --- a/src/NewGeom/NewGeom_Module.cpp +++ b/src/NewGeom/NewGeom_Module.cpp @@ -87,6 +87,9 @@ void NewGeom_Module::initialize(CAM_Application* theApp) LightApp_Module::initialize(theApp); myWorkshop->startApplication(); + LightApp_Application* anApp = dynamic_cast(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); + } diff --git a/src/NewGeom/NewGeom_Module.h b/src/NewGeom/NewGeom_Module.h index 9bda5d276..9da3e31af 100644 --- a/src/NewGeom/NewGeom_Module.h +++ b/src/NewGeom/NewGeom_Module.h @@ -85,6 +85,7 @@ Q_OBJECT protected slots: virtual void onViewManagerAdded(SUIT_ViewManager* theMgr); + void onDefaultPreferences(); protected: CAM_DataModel* createDataModel(); diff --git a/src/SketchPlugin/SketchPlugin_Plugin.cpp b/src/SketchPlugin/SketchPlugin_Plugin.cpp index e3a324300..cf3cf283b 100644 --- a/src/SketchPlugin/SketchPlugin_Plugin.cpp +++ b/src/SketchPlugin/SketchPlugin_Plugin.cpp @@ -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", diff --git a/src/SketchPlugin/SketchPlugin_Sketch.cpp b/src/SketchPlugin/SketchPlugin_Sketch.cpp index 19715af30..19098b784 100644 --- a/src/SketchPlugin/SketchPlugin_Sketch.cpp +++ b/src/SketchPlugin/SketchPlugin_Sketch.cpp @@ -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; diff --git a/src/XGUI/XGUI_Preferences.cpp b/src/XGUI/XGUI_Preferences.cpp index 81e0282ee..ed3782b11 100644 --- a/src/XGUI/XGUI_Preferences.cpp +++ b/src/XGUI/XGUI_Preferences.cpp @@ -13,6 +13,7 @@ #include #include #include +#include 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(); +} + //********************************************************** //********************************************************** //********************************************************** diff --git a/src/XGUI/XGUI_Preferences.h b/src/XGUI/XGUI_Preferences.h index a6fbc8a2b..3241d4142 100644 --- a/src/XGUI/XGUI_Preferences.h +++ b/src/XGUI/XGUI_Preferences.h @@ -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(); -- 2.39.2