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 <Config_PropManager.h>
10 #include <SUIT_ResourceMgr.h>
11 #include <SUIT_PreferenceMgr.h>
14 #include <QApplication>
15 #include <QDialogButtonBox>
17 const QString XGUI_Preferences::VIEWER_SECTION = "Viewer";
18 const QString XGUI_Preferences::MENU_SECTION = "Menu";
20 SUIT_ResourceMgr* XGUI_Preferences::myResourceMgr = 0;
22 SUIT_ResourceMgr* XGUI_Preferences::resourceMgr()
25 myResourceMgr = new SUIT_ResourceMgr("NewGeom");
26 myResourceMgr->setCurrentFormat("xml");
31 bool XGUI_Preferences::editPreferences(XGUI_Prefs& theModified)
33 XGUI_PreferencesDlg aDlg(resourceMgr(), QApplication::activeWindow());
35 if (aDlg.isChanged()) {
36 aDlg.modified(theModified);
37 resourceMgr()->save();
43 void XGUI_Preferences::updateCustomProps()
45 Config_Properties aProps = Config_PropManager::getProperties();
46 Config_Properties::iterator aIt;
47 for (aIt = aProps.begin(); aIt != aProps.end(); ++aIt) {
48 Config_Prop* aProp = (*aIt);
49 QString aVal = myResourceMgr->stringValue(QString(aProp->section().c_str()),
50 QString(aProp->name().c_str()));
52 aProp->setValue(aVal.toStdString());
56 void XGUI_Preferences::loadCustomProps()
60 QStringList aSections = myResourceMgr->sections();
61 foreach (QString aSection, aSections)
63 QStringList aParams = myResourceMgr->parameters(aSection);
64 foreach (QString aParam, aParams)
66 Config_PropManager::registerProp(aSection.toStdString(), aParam.toStdString(), "",
67 Config_Prop::Disabled,
68 myResourceMgr->stringValue(aSection, aParam).toStdString());
74 void XGUI_Preferences::createEditContent(XGUI_IPrefMgr* thePref, int thePage)
76 thePref->prefMgr()->setItemIcon(thePage, QIcon(":pictures/module.png"));
77 createCustomPage(thePref, thePage);
81 void XGUI_Preferences::createCustomPage(XGUI_IPrefMgr* thePref, int thePageId)
83 SUIT_ResourceMgr* aResMgr = XGUI_Preferences::resourceMgr();
84 bool isResModified = false;
86 // Make a Tab from each section
87 std::list<std::string> aSections = Config_PropManager::getSections();
88 std::list<std::string>::const_iterator it;
89 for (it = aSections.cbegin(); it != aSections.cend(); ++it) {
90 Config_Properties aProps = Config_PropManager::getProperties(*it);
91 int aTab = thePref->prefMgr()->addItem(QString((*it).c_str()), thePageId);
92 thePref->prefMgr()->setItemProperty("columns", 2, aTab);
94 Config_Properties::const_iterator aIt;
95 for (aIt = aProps.cbegin(); aIt != aProps.cend(); ++aIt) {
96 Config_Prop* aProp = (*aIt);
97 // check that the property is defined
98 QString aSection(aProp->section().c_str());
99 QString aName(aProp->name().c_str());
100 if (!aResMgr->hasValue(aSection, aName)) {
101 aResMgr->setValue(aSection, aName, QString(aProp->value().c_str()));
102 isResModified = true;
105 if (aProp->type() != Config_Prop::Disabled)
106 thePref->addPreference(QObject::tr(aProp->title().c_str()), aTab,
107 (SUIT_PreferenceMgr::PrefItemType) aProp->type(),
108 QString(aProp->section().c_str()), QString(aProp->name().c_str()));
113 //**********************************************************
114 //**********************************************************
115 //**********************************************************
116 class XGUI_PrefMgr: public XGUI_IPrefMgr
119 XGUI_PrefMgr(XGUI_PreferencesMgr* theMgr):myMgr(theMgr) {}
121 virtual int addPreference(const QString& theLbl, int pId,
122 SUIT_PreferenceMgr::PrefItemType theType,
123 const QString& theSection, const QString& theName )
125 return myMgr->addItem(theLbl, pId, theType, theSection, theName);
128 virtual SUIT_PreferenceMgr* prefMgr() const { return myMgr; }
131 XGUI_PreferencesMgr* myMgr;
134 //**********************************************************
135 //**********************************************************
136 //**********************************************************
137 XGUI_PreferencesDlg::XGUI_PreferencesDlg(SUIT_ResourceMgr* theResurces, QWidget* theParent)
138 : QDialog(theParent),
141 setWindowTitle(tr("Edit preferences"));
143 QVBoxLayout* main = new QVBoxLayout(this);
147 myPreferences = new XGUI_PreferencesMgr(theResurces, this);
148 main->addWidget(myPreferences);
150 setFocusProxy(myPreferences);
151 myPreferences->setFrameStyle(QFrame::Box | QFrame::Sunken);
153 QDialogButtonBox* aBtnBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel,
154 Qt::Horizontal, this);
155 main->addWidget(aBtnBox);
156 connect(aBtnBox, SIGNAL(accepted()), this, SLOT(accept()));
157 connect(aBtnBox, SIGNAL(rejected()), this, SLOT(reject()));
160 myPreferences->retrieve();
161 setMinimumSize(800, 200);
164 XGUI_PreferencesDlg::~XGUI_PreferencesDlg()
168 void XGUI_PreferencesDlg::createEditors()
170 int aPage = myPreferences->addItem(tr("Desktop"));
171 myPreferences->setItemIcon(aPage, QIcon(":pictures/view_prefs.png"));
173 createMenuPage(aPage);
174 createViewerPage(aPage);
176 aPage = myPreferences->addItem(tr("Module"));
177 myPreferences->setItemIcon(aPage, QIcon(":pictures/module.png"));
179 XGUI_PrefMgr aMgr(myPreferences);
180 XGUI_Preferences::createEditContent(&aMgr, aPage);
183 void XGUI_PreferencesDlg::createViewerPage(int thePageId)
185 int viewTab = myPreferences->addItem(tr("Viewer"), thePageId);
187 QStringList gradList;
188 gradList << tr("Horizontal gradient") << tr("Vertical gradient") << tr("First diagonal gradient")
189 << tr("Second diagonal gradient") << tr("First corner gradient")
190 << tr("Second corner gradient") << tr("Third corner gradient")
191 << tr("Fourth corner gradient");
193 QList<QVariant> idList;
194 for (int i = 0; i < gradList.size(); i++)
197 int bgGroup = myPreferences->addItem(tr("Background"), viewTab);
199 QString aImgFiles("Image files (*.bmp *.gif *.pix *.xwd *.rgb *.rs)");
201 int bgId = myPreferences->addItem(tr("Viewer 3d"), bgGroup, SUIT_PreferenceMgr::Background,
202 XGUI_Preferences::VIEWER_SECTION, "background");
203 myPreferences->setItemProperty("gradient_names", gradList, bgId);
204 myPreferences->setItemProperty("gradient_ids", idList, bgId);
205 myPreferences->setItemProperty("texture_enabled", true, bgId);
206 myPreferences->setItemProperty("texture_center_enabled", true, bgId);
207 myPreferences->setItemProperty("texture_tile_enabled", true, bgId);
208 myPreferences->setItemProperty("texture_stretch_enabled", true, bgId);
209 myPreferences->setItemProperty("custom_enabled", false, bgId);
210 myPreferences->setItemProperty("image_formats", aImgFiles, bgId);
213 void XGUI_PreferencesDlg::createMenuPage(int thePageId)
215 int aMenuTab = myPreferences->addItem(tr("Main menu"), thePageId);
217 int aSizeGroup = myPreferences->addItem(tr("Size"), aMenuTab);
218 myPreferences->setItemProperty("columns", 1, aSizeGroup);
220 int aRowsNb = myPreferences->addItem(tr("Number of rows"), aSizeGroup,
221 SUIT_PreferenceMgr::IntSpin, XGUI_Preferences::MENU_SECTION,
223 myPreferences->setItemProperty("min", 1, aRowsNb);
224 myPreferences->setItemProperty("max", 6, aRowsNb);
227 void XGUI_PreferencesDlg::accept()
229 myPreferences->store();
232 // Save custom properties
233 XGUI_Preferences::updateCustomProps();
237 void XGUI_PreferencesDlg::modified(XGUI_Prefs& theModified) const
239 theModified = myPreferences->modified();
242 //**********************************************************
243 //**********************************************************
244 //**********************************************************
245 void XGUI_PreferencesMgr::changedResources(const ResourceMap& theMap)
248 ResourceMap::ConstIterator it;
250 for (it = theMap.begin(); it != theMap.end(); ++it) {
252 it.key()->resource(aPref.first, aPref.second);
253 myModified.append(aPref);