1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: ModuleBase_Preferences.cpp
4 // Created: 07 Aug 2014
5 // Author: Vitaly SMETANNIKOV
7 #include "ModuleBase_Preferences.h"
8 //#include "ModuleBase_Constants.h"
10 #include <Model_ResultBody.h>
11 #include <Model_ResultGroup.h>
12 #include <Model_ResultConstruction.h>
14 #include <Config_PropManager.h>
16 #include <SUIT_ResourceMgr.h>
17 #include <SUIT_PreferenceMgr.h>
21 #include <QApplication>
22 #include <QDialogButtonBox>
23 #include <QPushButton>
25 const QString ModuleBase_Preferences::VIEWER_SECTION = "Viewer";
26 const QString ModuleBase_Preferences::MENU_SECTION = "Menu";
28 SUIT_ResourceMgr* ModuleBase_Preferences::myResourceMgr = 0;
30 SUIT_ResourceMgr* ModuleBase_Preferences::resourceMgr()
33 myResourceMgr = new SUIT_ResourceMgr("NewGeom");
34 myResourceMgr->setCurrentFormat("xml");
39 bool ModuleBase_Preferences::editPreferences(ModuleBase_Prefs& theModified)
41 ModuleBase_PreferencesDlg aDlg(resourceMgr(), QApplication::activeWindow());
43 if (aDlg.isChanged()) {
44 aDlg.modified(theModified);
45 resourceMgr()->save();
51 void ModuleBase_Preferences::updateConfigByResources()
53 Config_Properties aProps = Config_PropManager::getProperties();
54 Config_Properties::iterator aIt;
55 for (aIt = aProps.begin(); aIt != aProps.end(); ++aIt) {
56 Config_Prop* aProp = (*aIt);
57 QString aVal = myResourceMgr->stringValue(QString(aProp->section().c_str()),
58 QString(aProp->name().c_str()));
59 if (!aVal.isEmpty()) {
60 aProp->setValue(aVal.toStdString());
65 void ModuleBase_Preferences::updateResourcesByConfig()
67 Config_Properties aProps = Config_PropManager::getProperties();
68 Config_Properties::iterator aIt;
69 for (aIt = aProps.begin(); aIt != aProps.end(); ++aIt) {
70 Config_Prop* aProp = (*aIt);
71 myResourceMgr->setValue(QString(aProp->section().c_str()), QString(aProp->name().c_str()),
72 QString(aProp->value().c_str()));
76 void ModuleBase_Preferences::resetConfig()
78 Config_Properties aProps = Config_PropManager::getProperties();
79 Config_Properties::iterator aIt;
80 for (aIt = aProps.begin(); aIt != aProps.end(); ++aIt) {
81 Config_Prop* aProp = (*aIt);
82 aProp->setValue(aProp->defaultValue());
86 void ModuleBase_Preferences::loadCustomProps()
90 QStringList aSections = myResourceMgr->sections();
91 foreach (QString aSection, aSections)
93 QStringList aParams = myResourceMgr->parameters(aSection);
94 foreach (QString aParam, aParams)
96 Config_Prop* aProp = Config_PropManager::registerProp(aSection.toStdString(),
97 aParam.toStdString(), "", Config_Prop::Disabled);
98 aProp->setValue(myResourceMgr->stringValue(aSection, aParam).toStdString());
104 void ModuleBase_Preferences::createEditContent(ModuleBase_IPrefMgr* thePref, int thePage)
106 thePref->prefMgr()->setItemIcon(thePage, QIcon(":pictures/module.png"));
107 createCustomPage(thePref, thePage);
111 void ModuleBase_Preferences::createCustomPage(ModuleBase_IPrefMgr* thePref, int thePageId)
113 SUIT_ResourceMgr* aResMgr = ModuleBase_Preferences::resourceMgr();
114 bool isResModified = false;
116 // Make a Tab from each section
117 std::list<std::string> aSections = Config_PropManager::getSections();
118 std::list<std::string>::const_iterator it;
119 for (it = aSections.cbegin(); it != aSections.cend(); ++it) {
120 Config_Properties aProps = Config_PropManager::getProperties(*it);
121 int aTab = thePref->prefMgr()->addItem(QString((*it).c_str()), thePageId);
122 thePref->prefMgr()->setItemProperty("columns", 2, aTab);
124 Config_Properties::const_iterator aIt;
125 for (aIt = aProps.cbegin(); aIt != aProps.cend(); ++aIt) {
126 Config_Prop* aProp = (*aIt);
127 // check that the property is defined
128 QString aSection(aProp->section().c_str());
129 QString aName(aProp->name().c_str());
130 if (!aResMgr->hasValue(aSection, aName)) {
131 aResMgr->setValue(aSection, aName, QString(aProp->value().c_str()));
132 isResModified = true;
135 if (aProp->type() != Config_Prop::Disabled) {
136 SUIT_PreferenceMgr::PrefItemType aPrefType = SUIT_PreferenceMgr::Auto;
137 if (aProp->type() == Config_Prop::Directory) {
138 aPrefType = SUIT_PreferenceMgr::File;
140 aPrefType = (SUIT_PreferenceMgr::PrefItemType) aProp->type();
142 int anId = thePref->addPreference(QObject::tr(aProp->title().c_str()), aTab, aPrefType,
143 QString::fromStdString(aProp->section()),
144 QString::fromStdString(aProp->name()));
145 if(aProp->type() == Config_Prop::Directory) {
146 thePref->setItemProperty("path_type", Qtx::PT_Directory, anId);
154 * Implementation of preferences manager interface
156 class ModuleBase_PrefMgr: public ModuleBase_IPrefMgr
160 /// \param theMgr a preferences manager
161 ModuleBase_PrefMgr(ModuleBase_PreferencesMgr* theMgr):myMgr(theMgr) {}
163 virtual int addPreference(const QString& theLbl, int pId,
164 SUIT_PreferenceMgr::PrefItemType theType,
165 const QString& theSection, const QString& theName )
167 return myMgr->addItem(theLbl, pId, theType, theSection, theName);
170 virtual void setItemProperty( const QString& thePropName, const QVariant& theValue,
171 const int theId = -1) {
172 myMgr->setItemProperty(thePropName, theValue, theId);
175 virtual SUIT_PreferenceMgr* prefMgr() const { return myMgr; }
178 ModuleBase_PreferencesMgr* myMgr;
181 //**********************************************************
182 //**********************************************************
183 //**********************************************************
184 ModuleBase_PreferencesDlg::ModuleBase_PreferencesDlg(SUIT_ResourceMgr* theResurces, QWidget* theParent)
185 : QDialog(theParent),
188 setWindowTitle(tr("Edit preferences"));
190 QVBoxLayout* main = new QVBoxLayout(this);
194 myPreferences = new ModuleBase_PreferencesMgr(theResurces, this);
195 main->addWidget(myPreferences);
197 setFocusProxy(myPreferences);
198 myPreferences->setFrameStyle(QFrame::Box | QFrame::Sunken);
200 QDialogButtonBox* aBtnBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel |
201 QDialogButtonBox::Reset,
202 Qt::Horizontal, this);
203 QPushButton* aDefaultButton = aBtnBox->button(QDialogButtonBox::Reset);
204 aDefaultButton->setText(tr("Default"));
205 connect(aDefaultButton, SIGNAL(clicked()), this, SLOT(onDefault()));
207 main->addWidget(aBtnBox);
208 connect(aBtnBox, SIGNAL(accepted()), this, SLOT(accept()));
209 connect(aBtnBox, SIGNAL(rejected()), this, SLOT(reject()));
212 myPreferences->retrieve();
213 setMinimumSize(800, 200);
216 ModuleBase_PreferencesDlg::~ModuleBase_PreferencesDlg()
220 void ModuleBase_PreferencesDlg::createEditors()
222 int aPage = myPreferences->addItem(tr("Desktop"));
223 myPreferences->setItemIcon(aPage, QIcon(":pictures/view_prefs.png"));
225 createMenuPage(aPage);
226 createViewerPage(aPage);
228 aPage = myPreferences->addItem(tr("Module"));
229 myPreferences->setItemIcon(aPage, QIcon(":pictures/module.png"));
231 ModuleBase_PrefMgr aMgr(myPreferences);
232 ModuleBase_Preferences::createEditContent(&aMgr, aPage);
235 void ModuleBase_PreferencesDlg::createViewerPage(int thePageId)
237 int viewTab = myPreferences->addItem(tr("Viewer"), thePageId);
239 QStringList gradList;
240 gradList << tr("Horizontal gradient") << tr("Vertical gradient") << tr("First diagonal gradient")
241 << tr("Second diagonal gradient") << tr("First corner gradient")
242 << tr("Second corner gradient") << tr("Third corner gradient")
243 << tr("Fourth corner gradient");
245 QList<QVariant> idList;
246 for (int i = 0; i < gradList.size(); i++)
249 int bgGroup = myPreferences->addItem(tr("Background"), viewTab);
251 QString aImgFiles("Image files (*.bmp *.gif *.pix *.xwd *.rgb *.rs)");
253 int bgId = myPreferences->addItem(tr("Viewer 3d"), bgGroup, SUIT_PreferenceMgr::Background,
254 ModuleBase_Preferences::VIEWER_SECTION, "background");
255 myPreferences->setItemProperty("gradient_names", gradList, bgId);
256 myPreferences->setItemProperty("gradient_ids", idList, bgId);
257 myPreferences->setItemProperty("texture_enabled", true, bgId);
258 myPreferences->setItemProperty("texture_center_enabled", true, bgId);
259 myPreferences->setItemProperty("texture_tile_enabled", true, bgId);
260 myPreferences->setItemProperty("texture_stretch_enabled", true, bgId);
261 myPreferences->setItemProperty("custom_enabled", false, bgId);
262 myPreferences->setItemProperty("image_formats", aImgFiles, bgId);
264 //Config_PropManager::registerProp("Visualization", "object_default_color", "Object color",
265 // Config_Prop::Color, "#ffffff");
267 Config_PropManager::registerProp("Visualization", "result_body_color", "Body color",
268 Config_Prop::Color, Model_ResultBody::DEFAULT_COLOR());
269 Config_PropManager::registerProp("Visualization", "result_group_color", "Group color",
270 Config_Prop::Color, Model_ResultGroup::DEFAULT_COLOR());
271 Config_PropManager::registerProp("Visualization", "result_construction_color", "Construction color",
272 Config_Prop::Color, Model_ResultConstruction::DEFAULT_COLOR());
275 void ModuleBase_PreferencesDlg::createMenuPage(int thePageId)
277 int aMenuTab = myPreferences->addItem(tr("Main menu"), thePageId);
279 int aSizeGroup = myPreferences->addItem(tr("Size"), aMenuTab);
280 myPreferences->setItemProperty("columns", 1, aSizeGroup);
282 int aRowsNb = myPreferences->addItem(tr("Number of rows"), aSizeGroup,
283 SUIT_PreferenceMgr::IntSpin, ModuleBase_Preferences::MENU_SECTION,
285 myPreferences->setItemProperty("min", 1, aRowsNb);
286 myPreferences->setItemProperty("max", 6, aRowsNb);
289 void ModuleBase_PreferencesDlg::accept()
291 myPreferences->store();
294 // Save custom properties
295 ModuleBase_Preferences::updateConfigByResources();
299 void ModuleBase_PreferencesDlg::modified(ModuleBase_Prefs& theModified) const
301 theModified = myPreferences->modified();
304 void ModuleBase_PreferencesDlg::onDefault()
306 // reset main resources
307 //#ifdef SALOME_750 // until SALOME 7.5.0 is released
308 QtxResourceMgr::WorkingMode aPrev =
309 myPreferences->resourceMgr()->setWorkingMode(QtxResourceMgr::IgnoreUserValues);
310 myPreferences->retrieve();
311 myPreferences->resourceMgr()->setWorkingMode(aPrev);
313 // reset plugin's resources
314 ModuleBase_Preferences::resetConfig();
315 ModuleBase_Preferences::updateResourcesByConfig();
317 myPreferences->retrieve();
320 //**********************************************************
321 //**********************************************************
322 //**********************************************************
323 void ModuleBase_PreferencesMgr::changedResources(const ResourceMap& theMap)
326 ResourceMap::ConstIterator it;
328 for (it = theMap.begin(); it != theMap.end(); ++it) {
329 ModuleBase_Pref aPref;
330 it.key()->resource(aPref.first, aPref.second);
331 myModified.append(aPref);