1 // File: XGUI_Preferences.cpp
2 // Created: 07 Aug 2014
3 // Author: Vitaly SMETANNIKOV
5 #include "XGUI_Preferences.h"
6 #include "XGUI_Constants.h"
8 #include <SUIT_ResourceMgr.h>
9 #include <SUIT_PreferenceMgr.h>
12 #include <QApplication>
13 #include <QDialogButtonBox>
15 const QString XGUI_Preferences::VIEWER_SECTION = "Viewer";
16 const QString XGUI_Preferences::MENU_SECTION = "Menu";
18 SUIT_ResourceMgr* XGUI_Preferences::myResourceMgr = 0;
20 SUIT_ResourceMgr* XGUI_Preferences::resourceMgr()
23 myResourceMgr = new SUIT_ResourceMgr("NewGeom");
24 myResourceMgr->setCurrentFormat("xml");
29 bool XGUI_Preferences::editPreferences(XGUI_Prefs& theModified)
31 XGUI_PreferencesDlg aDlg(resourceMgr(), QApplication::activeWindow());
33 if (aDlg.isChanged()) {
34 aDlg.modified(theModified);
35 resourceMgr()->save();
41 void XGUI_Preferences::updateCustomProps()
43 Config_Properties aProps = Config_PropManager::getProperties();
44 Config_Properties::iterator aIt;
45 for (aIt = aProps.begin(); aIt != aProps.end(); ++aIt) {
46 Config_Prop* aProp = (*aIt);
47 QString aVal = myResourceMgr->stringValue(QString(aProp->section().c_str()),
48 QString(aProp->name().c_str()));
50 aProp->setValue(qPrintable(aVal));
54 void XGUI_Preferences::loadCustomProps()
58 QStringList aSections = myResourceMgr->sections();
59 foreach (QString aSection, aSections)
61 QStringList aParams = myResourceMgr->parameters(aSection);
62 foreach (QString aParam, aParams)
64 Config_PropManager::registerProp(qPrintable(aSection), qPrintable(aParam), "",
65 Config_Prop::Disabled,
66 qPrintable(myResourceMgr->stringValue(aSection, aParam)));
71 //**********************************************************
72 //**********************************************************
73 //**********************************************************
74 XGUI_PreferencesDlg::XGUI_PreferencesDlg(SUIT_ResourceMgr* theResurces, QWidget* theParent)
78 setWindowTitle(tr("Edit preferences"));
80 QVBoxLayout* main = new QVBoxLayout(this);
84 myPreferences = new XGUI_PreferencesMgr(theResurces, this);
85 main->addWidget(myPreferences);
87 setFocusProxy(myPreferences);
88 myPreferences->setFrameStyle(QFrame::Box | QFrame::Sunken);
90 QDialogButtonBox* aBtnBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel,
91 Qt::Horizontal, this);
92 main->addWidget(aBtnBox);
93 connect(aBtnBox, SIGNAL(accepted()), this, SLOT(accept()));
94 connect(aBtnBox, SIGNAL(rejected()), this, SLOT(reject()));
97 myPreferences->retrieve();
98 setMinimumSize(800, 200);
101 XGUI_PreferencesDlg::~XGUI_PreferencesDlg()
105 void XGUI_PreferencesDlg::createEditors()
107 int aPage = myPreferences->addItem(tr("Desktop"));
108 myPreferences->setItemIcon(aPage, QIcon(":pictures/view_prefs.png"));
110 createMenuPage(aPage);
111 createViewerPage(aPage);
113 aPage = myPreferences->addItem(tr("Module"));
114 myPreferences->setItemIcon(aPage, QIcon(":pictures/module.png"));
115 createCustomPage(aPage);
118 void XGUI_PreferencesDlg::createViewerPage(int thePageId)
120 int viewTab = myPreferences->addItem(tr("Viewer"), thePageId);
122 QStringList gradList;
123 gradList << tr("Horizontal gradient") << tr("Vertical gradient") << tr("First diagonal gradient")
124 << tr("Second diagonal gradient") << tr("First corner gradient")
125 << tr("Second corner gradient") << tr("Third corner gradient")
126 << tr("Fourth corner gradient");
128 QList<QVariant> idList;
129 for (int i = 0; i < gradList.size(); i++)
132 int bgGroup = myPreferences->addItem(tr("Background"), viewTab);
134 QString aImgFiles("Image files (*.bmp *.gif *.pix *.xwd *.rgb *.rs)");
136 int bgId = myPreferences->addItem(tr("Viewer 3d"), bgGroup, SUIT_PreferenceMgr::Background,
137 XGUI_Preferences::VIEWER_SECTION, "background");
138 myPreferences->setItemProperty("gradient_names", gradList, bgId);
139 myPreferences->setItemProperty("gradient_ids", idList, bgId);
140 myPreferences->setItemProperty("texture_enabled", true, bgId);
141 myPreferences->setItemProperty("texture_center_enabled", true, bgId);
142 myPreferences->setItemProperty("texture_tile_enabled", true, bgId);
143 myPreferences->setItemProperty("texture_stretch_enabled", true, bgId);
144 myPreferences->setItemProperty("custom_enabled", false, bgId);
145 myPreferences->setItemProperty("image_formats", aImgFiles, bgId);
148 void XGUI_PreferencesDlg::createMenuPage(int thePageId)
150 int aMenuTab = myPreferences->addItem(tr("Main menu"), thePageId);
152 int aSizeGroup = myPreferences->addItem(tr("Size"), aMenuTab);
153 myPreferences->setItemProperty("columns", 1, aSizeGroup);
155 int aRowsNb = myPreferences->addItem(tr("Number of rows"), aSizeGroup,
156 SUIT_PreferenceMgr::IntSpin, XGUI_Preferences::MENU_SECTION,
158 myPreferences->setItemProperty("min", 1, aRowsNb);
159 myPreferences->setItemProperty("max", 6, aRowsNb);
162 void XGUI_PreferencesDlg::createCustomPage(int thePageId)
164 SUIT_ResourceMgr* aResMgr = XGUI_Preferences::resourceMgr();
165 bool isResModified = false;
167 // Make a Tab from each section
168 std::list<std::string> aSections = Config_PropManager::getSections();
169 std::list<std::string>::const_iterator it;
170 for (it = aSections.cbegin(); it != aSections.cend(); ++it) {
171 Config_Properties aProps = Config_PropManager::getProperties(*it);
172 int aTab = myPreferences->addItem(QString((*it).c_str()), thePageId);
173 myPreferences->setItemProperty("columns", 2, aTab);
175 Config_Properties::const_iterator aIt;
176 for (aIt = aProps.cbegin(); aIt != aProps.cend(); ++aIt) {
177 Config_Prop* aProp = (*aIt);
178 // check that the property is defined
179 QString aSection(aProp->section().c_str());
180 QString aName(aProp->name().c_str());
181 if (!aResMgr->hasValue(aSection, aName)) {
182 aResMgr->setValue(aSection, aName, QString(aProp->value().c_str()));
183 isResModified = true;
186 if (aProp->type() != Config_Prop::Disabled)
187 myPreferences->addItem(tr(aProp->title().c_str()), aTab,
188 (SUIT_PreferenceMgr::PrefItemType) aProp->type(),
189 QString(aProp->section().c_str()), QString(aProp->name().c_str()));
194 void XGUI_PreferencesDlg::accept()
196 myPreferences->store();
199 // Save custom properties
200 XGUI_Preferences::updateCustomProps();
204 void XGUI_PreferencesDlg::modified(XGUI_Prefs& theModified) const
206 theModified = myPreferences->modified();
209 //**********************************************************
210 //**********************************************************
211 //**********************************************************
212 void XGUI_PreferencesMgr::changedResources(const ResourceMap& theMap)
215 ResourceMap::ConstIterator it;
217 for (it = theMap.begin(); it != theMap.end(); ++it) {
219 it.key()->resource(aPref.first, aPref.second);
220 myModified.append(aPref);