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 <Config_PropManager.h>
12 #include <SUIT_ResourceMgr.h>
13 #include <SUIT_PreferenceMgr.h>
17 #include <QApplication>
18 #include <QDialogButtonBox>
19 #include <QPushButton>
21 const QString ModuleBase_Preferences::VIEWER_SECTION = "Viewer";
22 const QString ModuleBase_Preferences::MENU_SECTION = "Menu";
24 SUIT_ResourceMgr* ModuleBase_Preferences::myResourceMgr = 0;
26 SUIT_ResourceMgr* ModuleBase_Preferences::resourceMgr()
29 myResourceMgr = new SUIT_ResourceMgr("NewGeom");
30 myResourceMgr->setCurrentFormat("xml");
35 bool ModuleBase_Preferences::editPreferences(ModuleBase_Prefs& theModified)
37 ModuleBase_PreferencesDlg aDlg(resourceMgr(), QApplication::activeWindow());
39 if (aDlg.isChanged()) {
40 aDlg.modified(theModified);
41 resourceMgr()->save();
47 void ModuleBase_Preferences::updateConfigByResources()
49 Config_Properties aProps = Config_PropManager::getProperties();
50 Config_Properties::iterator aIt;
51 for (aIt = aProps.begin(); aIt != aProps.end(); ++aIt) {
52 Config_Prop* aProp = (*aIt);
53 QString aVal = myResourceMgr->stringValue(QString(aProp->section().c_str()),
54 QString(aProp->name().c_str()));
55 if (!aVal.isEmpty()) {
56 aProp->setValue(aVal.toStdString());
61 void ModuleBase_Preferences::updateResourcesByConfig()
63 Config_Properties aProps = Config_PropManager::getProperties();
64 Config_Properties::iterator aIt;
65 for (aIt = aProps.begin(); aIt != aProps.end(); ++aIt) {
66 Config_Prop* aProp = (*aIt);
67 myResourceMgr->setValue(QString(aProp->section().c_str()), QString(aProp->name().c_str()),
68 QString(aProp->value().c_str()));
72 void ModuleBase_Preferences::resetConfig()
74 Config_Properties aProps = Config_PropManager::getProperties();
75 Config_Properties::iterator aIt;
76 for (aIt = aProps.begin(); aIt != aProps.end(); ++aIt) {
77 Config_Prop* aProp = (*aIt);
78 aProp->setValue(aProp->defaultValue());
82 void ModuleBase_Preferences::loadCustomProps()
86 QStringList aSections = myResourceMgr->sections();
87 foreach (QString aSection, aSections)
89 QStringList aParams = myResourceMgr->parameters(aSection);
90 foreach (QString aParam, aParams)
92 Config_Prop* aProp = Config_PropManager::registerProp(aSection.toStdString(),
93 aParam.toStdString(), "", Config_Prop::Disabled);
94 aProp->setValue(myResourceMgr->stringValue(aSection, aParam).toStdString());
100 void ModuleBase_Preferences::createEditContent(ModuleBase_IPrefMgr* thePref, int thePage)
102 thePref->prefMgr()->setItemIcon(thePage, QIcon(":pictures/module.png"));
103 createCustomPage(thePref, thePage);
107 void ModuleBase_Preferences::createCustomPage(ModuleBase_IPrefMgr* thePref, int thePageId)
109 SUIT_ResourceMgr* aResMgr = ModuleBase_Preferences::resourceMgr();
110 bool isResModified = false;
112 // Make a Tab from each section
113 std::list<std::string> aSections = Config_PropManager::getSections();
114 std::list<std::string>::const_iterator it;
115 for (it = aSections.cbegin(); it != aSections.cend(); ++it) {
116 Config_Properties aProps = Config_PropManager::getProperties(*it);
117 int aTab = thePref->prefMgr()->addItem(QString((*it).c_str()), thePageId);
118 thePref->prefMgr()->setItemProperty("columns", 2, aTab);
120 Config_Properties::const_iterator aIt;
121 for (aIt = aProps.cbegin(); aIt != aProps.cend(); ++aIt) {
122 Config_Prop* aProp = (*aIt);
123 // check that the property is defined
124 QString aSection(aProp->section().c_str());
125 QString aName(aProp->name().c_str());
126 if (!aResMgr->hasValue(aSection, aName)) {
127 aResMgr->setValue(aSection, aName, QString(aProp->value().c_str()));
128 isResModified = true;
131 if (aProp->type() != Config_Prop::Disabled) {
132 SUIT_PreferenceMgr::PrefItemType aPrefType = SUIT_PreferenceMgr::Auto;
133 if (aProp->type() == Config_Prop::Directory) {
134 aPrefType = SUIT_PreferenceMgr::File;
136 aPrefType = (SUIT_PreferenceMgr::PrefItemType) aProp->type();
138 int anId = thePref->addPreference(QObject::tr(aProp->title().c_str()), aTab, aPrefType,
139 QString::fromStdString(aProp->section()),
140 QString::fromStdString(aProp->name()));
141 if(aProp->type() == Config_Prop::Directory) {
142 thePref->setItemProperty("path_type", Qtx::PT_Directory, anId);
150 * Implementation of preferences manager interface
152 class ModuleBase_PrefMgr: public ModuleBase_IPrefMgr
156 /// \param theMgr a preferences manager
157 ModuleBase_PrefMgr(ModuleBase_PreferencesMgr* theMgr):myMgr(theMgr) {}
159 virtual int addPreference(const QString& theLbl, int pId,
160 SUIT_PreferenceMgr::PrefItemType theType,
161 const QString& theSection, const QString& theName )
163 return myMgr->addItem(theLbl, pId, theType, theSection, theName);
166 virtual void setItemProperty( const QString& thePropName, const QVariant& theValue,
167 const int theId = -1) {
168 myMgr->setItemProperty(thePropName, theValue, theId);
171 virtual SUIT_PreferenceMgr* prefMgr() const { return myMgr; }
174 ModuleBase_PreferencesMgr* myMgr;
177 //**********************************************************
178 //**********************************************************
179 //**********************************************************
180 ModuleBase_PreferencesDlg::ModuleBase_PreferencesDlg(SUIT_ResourceMgr* theResurces, QWidget* theParent)
181 : QDialog(theParent),
184 setWindowTitle(tr("Edit preferences"));
186 QVBoxLayout* main = new QVBoxLayout(this);
190 myPreferences = new ModuleBase_PreferencesMgr(theResurces, this);
191 main->addWidget(myPreferences);
193 setFocusProxy(myPreferences);
194 myPreferences->setFrameStyle(QFrame::Box | QFrame::Sunken);
196 QDialogButtonBox* aBtnBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel |
197 QDialogButtonBox::Reset,
198 Qt::Horizontal, this);
199 QPushButton* aDefaultButton = aBtnBox->button(QDialogButtonBox::Reset);
200 aDefaultButton->setText(tr("Default"));
201 connect(aDefaultButton, SIGNAL(clicked()), this, SLOT(onDefault()));
203 main->addWidget(aBtnBox);
204 connect(aBtnBox, SIGNAL(accepted()), this, SLOT(accept()));
205 connect(aBtnBox, SIGNAL(rejected()), this, SLOT(reject()));
208 myPreferences->retrieve();
209 setMinimumSize(800, 200);
212 ModuleBase_PreferencesDlg::~ModuleBase_PreferencesDlg()
216 void ModuleBase_PreferencesDlg::createEditors()
218 int aPage = myPreferences->addItem(tr("Desktop"));
219 myPreferences->setItemIcon(aPage, QIcon(":pictures/view_prefs.png"));
221 createMenuPage(aPage);
222 createViewerPage(aPage);
224 aPage = myPreferences->addItem(tr("Module"));
225 myPreferences->setItemIcon(aPage, QIcon(":pictures/module.png"));
227 ModuleBase_PrefMgr aMgr(myPreferences);
228 ModuleBase_Preferences::createEditContent(&aMgr, aPage);
231 void ModuleBase_PreferencesDlg::createViewerPage(int thePageId)
233 int viewTab = myPreferences->addItem(tr("Viewer"), thePageId);
235 QStringList gradList;
236 gradList << tr("Horizontal gradient") << tr("Vertical gradient") << tr("First diagonal gradient")
237 << tr("Second diagonal gradient") << tr("First corner gradient")
238 << tr("Second corner gradient") << tr("Third corner gradient")
239 << tr("Fourth corner gradient");
241 QList<QVariant> idList;
242 for (int i = 0; i < gradList.size(); i++)
245 int bgGroup = myPreferences->addItem(tr("Background"), viewTab);
247 QString aImgFiles("Image files (*.bmp *.gif *.pix *.xwd *.rgb *.rs)");
249 int bgId = myPreferences->addItem(tr("Viewer 3d"), bgGroup, SUIT_PreferenceMgr::Background,
250 ModuleBase_Preferences::VIEWER_SECTION, "background");
251 myPreferences->setItemProperty("gradient_names", gradList, bgId);
252 myPreferences->setItemProperty("gradient_ids", idList, bgId);
253 myPreferences->setItemProperty("texture_enabled", true, bgId);
254 myPreferences->setItemProperty("texture_center_enabled", true, bgId);
255 myPreferences->setItemProperty("texture_tile_enabled", true, bgId);
256 myPreferences->setItemProperty("texture_stretch_enabled", true, bgId);
257 myPreferences->setItemProperty("custom_enabled", false, bgId);
258 myPreferences->setItemProperty("image_formats", aImgFiles, bgId);
261 void ModuleBase_PreferencesDlg::createMenuPage(int thePageId)
263 int aMenuTab = myPreferences->addItem(tr("Main menu"), thePageId);
265 int aSizeGroup = myPreferences->addItem(tr("Size"), aMenuTab);
266 myPreferences->setItemProperty("columns", 1, aSizeGroup);
268 int aRowsNb = myPreferences->addItem(tr("Number of rows"), aSizeGroup,
269 SUIT_PreferenceMgr::IntSpin, ModuleBase_Preferences::MENU_SECTION,
271 myPreferences->setItemProperty("min", 1, aRowsNb);
272 myPreferences->setItemProperty("max", 6, aRowsNb);
275 void ModuleBase_PreferencesDlg::accept()
277 myPreferences->store();
280 // Save custom properties
281 ModuleBase_Preferences::updateConfigByResources();
285 void ModuleBase_PreferencesDlg::modified(ModuleBase_Prefs& theModified) const
287 theModified = myPreferences->modified();
290 void ModuleBase_PreferencesDlg::onDefault()
292 // reset main resources
293 //#ifdef SALOME_750 // until SALOME 7.5.0 is released
294 QtxResourceMgr::WorkingMode aPrev =
295 myPreferences->resourceMgr()->setWorkingMode(QtxResourceMgr::IgnoreUserValues);
296 myPreferences->retrieve();
297 myPreferences->resourceMgr()->setWorkingMode(aPrev);
299 // reset plugin's resources
300 ModuleBase_Preferences::resetConfig();
301 ModuleBase_Preferences::updateResourcesByConfig();
303 myPreferences->retrieve();
306 //**********************************************************
307 //**********************************************************
308 //**********************************************************
309 void ModuleBase_PreferencesMgr::changedResources(const ResourceMap& theMap)
312 ResourceMap::ConstIterator it;
314 for (it = theMap.begin(); it != theMap.end(); ++it) {
315 ModuleBase_Pref aPref;
316 it.key()->resource(aPref.first, aPref.second);
317 myModified.append(aPref);