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);
106 void ModuleBase_Preferences::resetResourcePreferences(SUIT_PreferenceMgr* thePref)
111 QtxResourceMgr::WorkingMode aPrev =
112 thePref->resourceMgr()->setWorkingMode(QtxResourceMgr::IgnoreUserValues);
114 thePref->resourceMgr()->setWorkingMode(aPrev);
117 void ModuleBase_Preferences::resetConfigPropPreferences(SUIT_PreferenceMgr* thePref)
120 updateResourcesByConfig();
122 // retrieve the reset resource values to the preferences items
123 Config_Properties aProps = Config_PropManager::getProperties();
124 Config_Properties::iterator aIt;
126 QStringList aSections;
127 for (aIt = aProps.begin(); aIt != aProps.end(); ++aIt) {
128 Config_Prop* aProp = (*aIt);
129 aValues.append(QString(aProp->name().c_str()));
130 if (!aSections.contains(aProp->section().c_str()))
131 aSections.append(aProp->section().c_str());
132 QtxPreferenceItem* anItem = thePref->findItem(QString(aProp->title().c_str()), true);
138 void ModuleBase_Preferences::createCustomPage(ModuleBase_IPrefMgr* thePref, int thePageId)
140 SUIT_ResourceMgr* aResMgr = ModuleBase_Preferences::resourceMgr();
141 bool isResModified = false;
143 // Make a Tab from each section
144 std::list<std::string> aSections = Config_PropManager::getSections();
145 std::list<std::string>::const_iterator it;
146 for (it = aSections.cbegin(); it != aSections.cend(); ++it) {
147 Config_Properties aProps = Config_PropManager::getProperties(*it);
148 int aTab = thePref->prefMgr()->addItem(QString((*it).c_str()), thePageId);
149 thePref->prefMgr()->setItemProperty("columns", 2, aTab);
151 Config_Properties::const_iterator aIt;
152 for (aIt = aProps.cbegin(); aIt != aProps.cend(); ++aIt) {
153 Config_Prop* aProp = (*aIt);
154 // check that the property is defined
155 QString aSection(aProp->section().c_str());
156 QString aName(aProp->name().c_str());
157 if (!aResMgr->hasValue(aSection, aName)) {
158 aResMgr->setValue(aSection, aName, QString(aProp->value().c_str()));
159 isResModified = true;
162 if (aProp->type() != Config_Prop::Disabled) {
163 SUIT_PreferenceMgr::PrefItemType aPrefType = SUIT_PreferenceMgr::Auto;
164 if (aProp->type() == Config_Prop::Directory) {
165 aPrefType = SUIT_PreferenceMgr::File;
167 aPrefType = (SUIT_PreferenceMgr::PrefItemType) aProp->type();
169 int anId = thePref->addPreference(QObject::tr(aProp->title().c_str()), aTab, aPrefType,
170 QString::fromStdString(aProp->section()),
171 QString::fromStdString(aProp->name()));
172 if(aProp->type() == Config_Prop::Directory) {
173 thePref->setItemProperty("path_type", Qtx::PT_Directory, anId);
181 * Implementation of preferences manager interface
183 class ModuleBase_PrefMgr: public ModuleBase_IPrefMgr
187 /// \param theMgr a preferences manager
188 ModuleBase_PrefMgr(ModuleBase_PreferencesMgr* theMgr):myMgr(theMgr) {}
190 virtual int addPreference(const QString& theLbl, int pId,
191 SUIT_PreferenceMgr::PrefItemType theType,
192 const QString& theSection, const QString& theName )
194 return myMgr->addItem(theLbl, pId, theType, theSection, theName);
197 virtual void setItemProperty( const QString& thePropName, const QVariant& theValue,
198 const int theId = -1) {
199 myMgr->setItemProperty(thePropName, theValue, theId);
202 virtual SUIT_PreferenceMgr* prefMgr() const { return myMgr; }
205 ModuleBase_PreferencesMgr* myMgr;
208 //**********************************************************
209 //**********************************************************
210 //**********************************************************
211 ModuleBase_PreferencesDlg::ModuleBase_PreferencesDlg(SUIT_ResourceMgr* theResurces, QWidget* theParent)
212 : QDialog(theParent),
215 setWindowTitle(tr("Edit preferences"));
217 QVBoxLayout* main = new QVBoxLayout(this);
221 myPreferences = new ModuleBase_PreferencesMgr(theResurces, this);
222 main->addWidget(myPreferences);
224 setFocusProxy(myPreferences);
225 myPreferences->setFrameStyle(QFrame::Box | QFrame::Sunken);
227 QDialogButtonBox* aBtnBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel |
228 QDialogButtonBox::Reset,
229 Qt::Horizontal, this);
230 QPushButton* aDefaultButton = aBtnBox->button(QDialogButtonBox::Reset);
231 aDefaultButton->setText(tr("Default"));
232 connect(aDefaultButton, SIGNAL(clicked()), this, SLOT(onDefault()));
234 main->addWidget(aBtnBox);
235 connect(aBtnBox, SIGNAL(accepted()), this, SLOT(accept()));
236 connect(aBtnBox, SIGNAL(rejected()), this, SLOT(reject()));
239 myPreferences->retrieve();
240 setMinimumSize(800, 200);
243 ModuleBase_PreferencesDlg::~ModuleBase_PreferencesDlg()
247 void ModuleBase_PreferencesDlg::createEditors()
249 int aPage = myPreferences->addItem(tr("Desktop"));
250 myPreferences->setItemIcon(aPage, QIcon(":pictures/view_prefs.png"));
252 createMenuPage(aPage);
253 createViewerPage(aPage);
255 aPage = myPreferences->addItem(tr("Module"));
256 myPreferences->setItemIcon(aPage, QIcon(":pictures/module.png"));
258 ModuleBase_PrefMgr aMgr(myPreferences);
259 ModuleBase_Preferences::createEditContent(&aMgr, aPage);
262 void ModuleBase_PreferencesDlg::createViewerPage(int thePageId)
264 int viewTab = myPreferences->addItem(tr("Viewer"), thePageId);
266 QStringList gradList;
267 gradList << tr("Horizontal gradient") << tr("Vertical gradient") << tr("First diagonal gradient")
268 << tr("Second diagonal gradient") << tr("First corner gradient")
269 << tr("Second corner gradient") << tr("Third corner gradient")
270 << tr("Fourth corner gradient");
272 QList<QVariant> idList;
273 for (int i = 0; i < gradList.size(); i++)
276 int bgGroup = myPreferences->addItem(tr("Background"), viewTab);
278 QString aImgFiles("Image files (*.bmp *.gif *.pix *.xwd *.rgb *.rs)");
280 int bgId = myPreferences->addItem(tr("Viewer 3d"), bgGroup, SUIT_PreferenceMgr::Background,
281 ModuleBase_Preferences::VIEWER_SECTION, "background");
282 myPreferences->setItemProperty("gradient_names", gradList, bgId);
283 myPreferences->setItemProperty("gradient_ids", idList, bgId);
284 myPreferences->setItemProperty("texture_enabled", true, bgId);
285 myPreferences->setItemProperty("texture_center_enabled", true, bgId);
286 myPreferences->setItemProperty("texture_tile_enabled", true, bgId);
287 myPreferences->setItemProperty("texture_stretch_enabled", true, bgId);
288 myPreferences->setItemProperty("custom_enabled", false, bgId);
289 myPreferences->setItemProperty("image_formats", aImgFiles, bgId);
292 void ModuleBase_PreferencesDlg::createMenuPage(int thePageId)
294 int aMenuTab = myPreferences->addItem(tr("Main menu"), thePageId);
296 int aSizeGroup = myPreferences->addItem(tr("Size"), aMenuTab);
297 myPreferences->setItemProperty("columns", 1, aSizeGroup);
299 int aRowsNb = myPreferences->addItem(tr("Number of rows"), aSizeGroup,
300 SUIT_PreferenceMgr::IntSpin, ModuleBase_Preferences::MENU_SECTION,
302 myPreferences->setItemProperty("min", 1, aRowsNb);
303 myPreferences->setItemProperty("max", 6, aRowsNb);
306 void ModuleBase_PreferencesDlg::accept()
308 myPreferences->store();
311 // Save custom properties
312 ModuleBase_Preferences::updateConfigByResources();
316 void ModuleBase_PreferencesDlg::modified(ModuleBase_Prefs& theModified) const
318 theModified = myPreferences->modified();
321 void ModuleBase_PreferencesDlg::onDefault()
323 // reset main resources. It throwns all resource manager items to the
324 // initial/default state. If there is no a default state of the item,
325 // it will be filled with an empty value. It concernerned to plugin
326 // config items, like visualization color. The main xml do not contains
327 // default values for them. So, it is important to reset the config
328 // properties after reseting the resources preferences.
329 ModuleBase_Preferences::resetResourcePreferences(myPreferences);
330 // reset plugin's resources. It fills the config resources with the default
331 // values, stores result in the resource manager and retrieve the preferences
332 // items with these values.
333 ModuleBase_Preferences::resetConfigPropPreferences(myPreferences);
336 //**********************************************************
337 //**********************************************************
338 //**********************************************************
339 void ModuleBase_PreferencesMgr::changedResources(const ResourceMap& theMap)
342 ResourceMap::ConstIterator it;
344 for (it = theMap.begin(); it != theMap.end(); ++it) {
345 ModuleBase_Pref aPref;
346 it.key()->resource(aPref.first, aPref.second);
347 myModified.append(aPref);