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()
56 QStringList aSections = myResourceMgr->sections();
57 foreach (QString aSection, aSections)
59 QStringList aParams = myResourceMgr->parameters(aSection);
60 foreach (QString aParam, aParams)
62 Config_PropManager::registerProp(qPrintable(aSection), qPrintable(aParam), "",
63 Config_Prop::Disabled,
64 qPrintable(myResourceMgr->stringValue(aSection, aParam)));
69 //**********************************************************
70 //**********************************************************
71 //**********************************************************
72 XGUI_PreferencesDlg::XGUI_PreferencesDlg(SUIT_ResourceMgr* theResurces, QWidget* theParent)
76 setWindowTitle(tr("Edit preferences"));
78 QVBoxLayout* main = new QVBoxLayout(this);
82 myPreferences = new XGUI_PreferencesMgr(theResurces, this);
83 main->addWidget(myPreferences);
85 setFocusProxy(myPreferences);
86 myPreferences->setFrameStyle(QFrame::Box | QFrame::Sunken);
88 QDialogButtonBox* aBtnBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel,
89 Qt::Horizontal, this);
90 main->addWidget(aBtnBox);
91 connect(aBtnBox, SIGNAL(accepted()), this, SLOT(accept()));
92 connect(aBtnBox, SIGNAL(rejected()), this, SLOT(reject()));
95 myPreferences->retrieve();
96 setMinimumSize(800, 200);
99 XGUI_PreferencesDlg::~XGUI_PreferencesDlg()
103 void XGUI_PreferencesDlg::createEditors()
105 int aPage = myPreferences->addItem(tr("Desktop"));
106 myPreferences->setItemIcon(aPage, QIcon(":pictures/view_prefs.png"));
108 createMenuPage(aPage);
109 createViewerPage(aPage);
111 aPage = myPreferences->addItem(tr("Module"));
112 myPreferences->setItemIcon(aPage, QIcon(":pictures/module.png"));
113 createCustomPage(aPage);
116 void XGUI_PreferencesDlg::createViewerPage(int thePageId)
118 int viewTab = myPreferences->addItem(tr("Viewer"), thePageId);
120 QStringList gradList;
121 gradList << tr("Horizontal gradient") << tr("Vertical gradient") << tr("First diagonal gradient")
122 << tr("Second diagonal gradient") << tr("First corner gradient")
123 << tr("Second corner gradient") << tr("Third corner gradient")
124 << tr("Fourth corner gradient");
126 QList<QVariant> idList;
127 for (int i = 0; i < gradList.size(); i++)
130 int bgGroup = myPreferences->addItem(tr("Background"), viewTab);
132 QString aImgFiles("Image files (*.bmp *.gif *.pix *.xwd *.rgb *.rs)");
134 int bgId = myPreferences->addItem(tr("Viewer 3d"), bgGroup, SUIT_PreferenceMgr::Background,
135 XGUI_Preferences::VIEWER_SECTION, "background");
136 myPreferences->setItemProperty("gradient_names", gradList, bgId);
137 myPreferences->setItemProperty("gradient_ids", idList, bgId);
138 myPreferences->setItemProperty("texture_enabled", true, bgId);
139 myPreferences->setItemProperty("texture_center_enabled", true, bgId);
140 myPreferences->setItemProperty("texture_tile_enabled", true, bgId);
141 myPreferences->setItemProperty("texture_stretch_enabled", true, bgId);
142 myPreferences->setItemProperty("custom_enabled", false, bgId);
143 myPreferences->setItemProperty("image_formats", aImgFiles, bgId);
146 void XGUI_PreferencesDlg::createMenuPage(int thePageId)
148 int aMenuTab = myPreferences->addItem(tr("Main menu"), thePageId);
150 int aSizeGroup = myPreferences->addItem(tr("Size"), aMenuTab);
151 myPreferences->setItemProperty("columns", 1, aSizeGroup);
153 int aRowsNb = myPreferences->addItem(tr("Number of rows"), aSizeGroup,
154 SUIT_PreferenceMgr::IntSpin, XGUI_Preferences::MENU_SECTION,
156 myPreferences->setItemProperty("min", 1, aRowsNb);
157 myPreferences->setItemProperty("max", 6, aRowsNb);
160 void XGUI_PreferencesDlg::createCustomPage(int thePageId)
162 SUIT_ResourceMgr* aResMgr = XGUI_Preferences::resourceMgr();
163 bool isResModified = false;
165 // Make a Tab from each section
166 std::list<std::string> aSections = Config_PropManager::getSections();
167 std::list<std::string>::const_iterator it;
168 for (it = aSections.cbegin(); it != aSections.cend(); ++it) {
169 Config_Properties aProps = Config_PropManager::getProperties(*it);
170 int aTab = myPreferences->addItem(QString((*it).c_str()), thePageId);
171 myPreferences->setItemProperty("columns", 2, aTab);
173 Config_Properties::const_iterator aIt;
174 for (aIt = aProps.cbegin(); aIt != aProps.cend(); ++aIt) {
175 Config_Prop* aProp = (*aIt);
176 // check that the property is defined
177 QString aSection(aProp->section().c_str());
178 QString aName(aProp->name().c_str());
179 if (!aResMgr->hasValue(aSection, aName)) {
180 aResMgr->setValue(aSection, aName, QString(aProp->value().c_str()));
181 isResModified = true;
184 if (aProp->type() != Config_Prop::Disabled)
185 myPreferences->addItem(tr(aProp->title().c_str()), aTab,
186 (SUIT_PreferenceMgr::PrefItemType) aProp->type(),
187 QString(aProp->section().c_str()), QString(aProp->name().c_str()));
192 void XGUI_PreferencesDlg::accept()
194 myPreferences->store();
197 // Save custom properties
198 XGUI_Preferences::updateCustomProps();
202 void XGUI_PreferencesDlg::modified(XGUI_Prefs& theModified) const
204 theModified = myPreferences->modified();
207 //**********************************************************
208 //**********************************************************
209 //**********************************************************
210 void XGUI_PreferencesMgr::changedResources(const ResourceMap& theMap)
213 ResourceMap::ConstIterator it;
215 for (it = theMap.begin(); it != theMap.end(); ++it) {
217 it.key()->resource(aPref.first, aPref.second);
218 myModified.append(aPref);