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>
16 const QString XGUI_Preferences::VIEWER_SECTION = "Viewer";
17 const QString XGUI_Preferences::MENU_SECTION = "Menu";
20 SUIT_ResourceMgr* XGUI_Preferences::myResourceMgr = 0;
23 SUIT_ResourceMgr* XGUI_Preferences::resourceMgr()
26 myResourceMgr = new SUIT_ResourceMgr("NewGeom");
27 myResourceMgr->setCurrentFormat("xml");
32 bool XGUI_Preferences::editPreferences(XGUI_Prefs& theModified)
34 XGUI_PreferencesDlg aDlg(resourceMgr(), QApplication::activeWindow());
36 if (aDlg.isChanged()) {
37 aDlg.modified(theModified);
38 resourceMgr()->save();
45 void XGUI_Preferences::updateCustomProps()
47 Config_Properties aProps = Config_PropManager::getProperties();
48 Config_Properties::iterator aIt;
49 for (aIt = aProps.begin(); aIt != aProps.end(); ++ aIt) {
50 Config_Prop* aProp = (*aIt);
51 QString aVal = myResourceMgr->stringValue(QString(aProp->section().c_str()),
52 QString(aProp->name().c_str()));
54 aProp->setValue(qPrintable(aVal));
59 void XGUI_Preferences::loadCustomProps()
61 QStringList aSections = myResourceMgr->sections();
62 foreach (QString aSection, aSections) {
63 QStringList aParams = myResourceMgr->parameters(aSection);
64 foreach (QString aParam, aParams) {
65 Config_PropManager::registerProp(qPrintable(aSection),
67 "", Config_Prop::Disabled,
68 qPrintable(myResourceMgr->stringValue(aSection, aParam)));
75 //**********************************************************
76 //**********************************************************
77 //**********************************************************
78 XGUI_PreferencesDlg::XGUI_PreferencesDlg(SUIT_ResourceMgr* theResurces, QWidget* theParent)
79 : QDialog(theParent), myIsChanged(false)
81 setWindowTitle( tr("Edit preferences") );
83 QVBoxLayout* main = new QVBoxLayout(this);
85 main->setSpacing( 5 );
87 myPreferences = new XGUI_PreferencesMgr(theResurces, this);
88 main->addWidget( myPreferences );
90 setFocusProxy( myPreferences );
91 myPreferences->setFrameStyle( QFrame::Box | QFrame::Sunken );
93 QDialogButtonBox* aBtnBox = new QDialogButtonBox(
94 QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, this);
95 main->addWidget(aBtnBox);
96 connect(aBtnBox, SIGNAL(accepted()), this, SLOT(accept()));
97 connect(aBtnBox, SIGNAL(rejected()), this, SLOT(reject()));
100 myPreferences->retrieve();
101 setMinimumSize(800, 200);
104 XGUI_PreferencesDlg::~XGUI_PreferencesDlg()
108 void XGUI_PreferencesDlg::createEditors()
110 int aPage = myPreferences->addItem(tr("Desktop"));
111 myPreferences->setItemIcon(aPage, QIcon(":pictures/view_prefs.png"));
113 createMenuPage(aPage);
114 createViewerPage(aPage);
116 aPage = myPreferences->addItem(tr("Module"));
117 myPreferences->setItemIcon(aPage, QIcon(":pictures/module.png"));
118 createCustomPage(aPage);
121 void XGUI_PreferencesDlg::createViewerPage(int thePageId)
123 int viewTab = myPreferences->addItem( tr("Viewer"), thePageId );
125 QStringList gradList;
126 gradList << tr("Horizontal gradient") << tr("Vertical gradient") <<
127 tr("First diagonal gradient") << tr("Second diagonal gradient") <<
128 tr("First corner gradient") << tr("Second corner gradient") <<
129 tr("Third corner gradient") << tr("Fourth corner gradient");
131 QList<QVariant> idList;
132 for (int i = 0; i < gradList.size(); i++)
135 int bgGroup = myPreferences->addItem( tr( "Background" ), viewTab );
137 QString aImgFiles("Image files (*.bmp *.gif *.pix *.xwd *.rgb *.rs)");
139 int bgId = myPreferences->addItem( tr("Viewer 3d" ), bgGroup,
140 SUIT_PreferenceMgr::Background,
141 XGUI_Preferences::VIEWER_SECTION, "background" );
142 myPreferences->setItemProperty( "gradient_names", gradList, bgId );
143 myPreferences->setItemProperty( "gradient_ids", idList, bgId );
144 myPreferences->setItemProperty( "texture_enabled", true, bgId );
145 myPreferences->setItemProperty( "texture_center_enabled", true, bgId );
146 myPreferences->setItemProperty( "texture_tile_enabled", true, bgId );
147 myPreferences->setItemProperty( "texture_stretch_enabled", true, bgId );
148 myPreferences->setItemProperty( "custom_enabled", false, bgId );
149 myPreferences->setItemProperty( "image_formats", aImgFiles, bgId );
152 void XGUI_PreferencesDlg::createMenuPage(int thePageId)
154 int aMenuTab = myPreferences->addItem(tr("Main menu"), thePageId );
156 int aSizeGroup = myPreferences->addItem(tr("Size"), aMenuTab );
157 myPreferences->setItemProperty( "columns", 1, aSizeGroup );
159 int aRowsNb = myPreferences->addItem( tr( "Number of rows" ),
161 SUIT_PreferenceMgr::IntSpin,
162 XGUI_Preferences::MENU_SECTION,
164 myPreferences->setItemProperty( "min", 1, aRowsNb );
165 myPreferences->setItemProperty( "max", 6, aRowsNb );
169 void XGUI_PreferencesDlg::createCustomPage(int thePageId)
171 SUIT_ResourceMgr* aResMgr = XGUI_Preferences::resourceMgr();
172 bool isResModified = false;
174 // Make a Tab from each section
175 std::list<std::string> aSections = Config_PropManager::getSections();
176 std::list<std::string>::const_iterator it;
177 for (it = aSections.cbegin(); it != aSections.cend(); ++it) {
178 Config_Properties aProps = Config_PropManager::getProperties(*it);
179 int aTab = myPreferences->addItem(QString((*it).c_str()), thePageId );
180 myPreferences->setItemProperty( "columns", 2, aTab );
182 Config_Properties::const_iterator aIt;
183 for (aIt = aProps.cbegin(); aIt != aProps.cend(); ++aIt) {
184 Config_Prop* aProp = (*aIt);
185 // check that the property is defined
186 QString aSection(aProp->section().c_str());
187 QString aName(aProp->name().c_str());
188 if (!aResMgr->hasValue(aSection, aName)) {
189 aResMgr->setValue(aSection, aName, QString(aProp->value().c_str()));
190 isResModified = true;
193 if (aProp->type() != Config_Prop::Disabled)
194 myPreferences->addItem( tr(aProp->title().c_str()), aTab,
195 (SUIT_PreferenceMgr::PrefItemType)aProp->type(),
196 QString(aProp->section().c_str()),
197 QString(aProp->name().c_str()) );
203 void XGUI_PreferencesDlg::accept()
205 myPreferences->store();
208 // Save custom properties
209 XGUI_Preferences::updateCustomProps();
214 void XGUI_PreferencesDlg::modified(XGUI_Prefs& theModified) const
216 theModified = myPreferences->modified();
221 //**********************************************************
222 //**********************************************************
223 //**********************************************************
224 void XGUI_PreferencesMgr::changedResources( const ResourceMap& theMap)
227 ResourceMap::ConstIterator it;
229 for (it = theMap.begin(); it != theMap.end(); ++it ) {
231 it.key()->resource( aPref.first, aPref.second );
232 myModified.append(aPref);