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", 2, aSizeGroup );
159 int aItemSizeItem = myPreferences->addItem( tr( "Item size" ),
161 SUIT_PreferenceMgr::Selector,
162 XGUI_Preferences::MENU_SECTION,
164 QStringList aValuesList;
165 QList<QVariant> anIndicesList;
166 aValuesList << tr("Small") << tr("Medium") << tr("Large");
167 anIndicesList << 20 << 25 << 30;
168 myPreferences->setItemProperty( "strings", aValuesList, aItemSizeItem );
169 myPreferences->setItemProperty( "indexes", anIndicesList, aItemSizeItem );
171 int aRowsNb = myPreferences->addItem( tr( "Number of rows" ),
173 SUIT_PreferenceMgr::IntSpin,
174 XGUI_Preferences::MENU_SECTION,
176 myPreferences->setItemProperty( "min", 1, aRowsNb );
177 myPreferences->setItemProperty( "max", 10, aRowsNb );
181 void XGUI_PreferencesDlg::createCustomPage(int thePageId)
183 SUIT_ResourceMgr* aResMgr = XGUI_Preferences::resourceMgr();
184 bool isResModified = false;
186 // Make a Tab from each section
187 std::list<std::string> aSections = Config_PropManager::getSections();
188 std::list<std::string>::const_iterator it;
189 for (it = aSections.cbegin(); it != aSections.cend(); ++it) {
190 Config_Properties aProps = Config_PropManager::getProperties(*it);
191 int aTab = myPreferences->addItem(QString((*it).c_str()), thePageId );
192 myPreferences->setItemProperty( "columns", 2, aTab );
194 Config_Properties::const_iterator aIt;
195 for (aIt = aProps.cbegin(); aIt != aProps.cend(); ++aIt) {
196 Config_Prop* aProp = (*aIt);
197 // check that the property is defined
198 QString aSection(aProp->section().c_str());
199 QString aName(aProp->name().c_str());
200 if (!aResMgr->hasValue(aSection, aName)) {
201 aResMgr->setValue(aSection, aName, QString(aProp->value().c_str()));
202 isResModified = true;
205 if (aProp->type() != Config_Prop::Disabled)
206 myPreferences->addItem( tr(aProp->title().c_str()), aTab,
207 (SUIT_PreferenceMgr::PrefItemType)aProp->type(),
208 QString(aProp->section().c_str()),
209 QString(aProp->name().c_str()) );
215 void XGUI_PreferencesDlg::accept()
217 myPreferences->store();
220 // Save custom properties
221 XGUI_Preferences::updateCustomProps();
226 void XGUI_PreferencesDlg::modified(XGUI_Prefs& theModified) const
228 theModified = myPreferences->modified();
233 //**********************************************************
234 //**********************************************************
235 //**********************************************************
236 void XGUI_PreferencesMgr::changedResources( const ResourceMap& theMap)
239 ResourceMap::ConstIterator it;
241 for (it = theMap.begin(); it != theMap.end(); ++it ) {
243 it.key()->resource( aPref.first, aPref.second );
244 myModified.append(aPref);