1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: ModuleBase_Preferences.cpp
4 // Created: 07 Aug 2014
5 // Author: Vitaly SMETANNIKOV
7 #include "ModuleBase_Preferences.h"
8 //#include "ModuleBase_Constants.h"
10 #include <Config_PropManager.h>
12 #include <TopAbs_ShapeEnum.hxx>
14 #include <SUIT_ResourceMgr.h>
15 #include <SUIT_PreferenceMgr.h>
19 #include <QApplication>
20 #include <QDialogButtonBox>
21 #include <QPushButton>
23 const QString ModuleBase_Preferences::VIEWER_SECTION = "Viewer";
24 const QString ModuleBase_Preferences::MENU_SECTION = "Menu";
26 SUIT_ResourceMgr* ModuleBase_Preferences::myResourceMgr = 0;
28 SUIT_ResourceMgr* ModuleBase_Preferences::resourceMgr()
31 myResourceMgr = new SUIT_ResourceMgr("SHAPER");
32 myResourceMgr->setCurrentFormat("xml");
37 bool ModuleBase_Preferences::editPreferences(ModuleBase_Prefs& theModified)
39 ModuleBase_PreferencesDlg aDlg(resourceMgr(), QApplication::activeWindow());
41 if (aDlg.isChanged()) {
42 aDlg.modified(theModified);
43 resourceMgr()->save();
49 void ModuleBase_Preferences::updateConfigByResources()
51 Config_Properties aProps = Config_PropManager::getProperties();
52 Config_Properties::iterator aIt;
53 for (aIt = aProps.begin(); aIt != aProps.end(); ++aIt) {
54 Config_Prop* aProp = (*aIt);
55 QString aVal = myResourceMgr->stringValue(QString(aProp->section().c_str()),
56 QString(aProp->name().c_str()));
57 if (!aVal.isEmpty()) {
58 aProp->setValue(aVal.toStdString());
63 void ModuleBase_Preferences::updateResourcesByConfig()
65 Config_Properties aProps = Config_PropManager::getProperties();
66 Config_Properties::iterator aIt;
67 for (aIt = aProps.begin(); aIt != aProps.end(); ++aIt) {
68 Config_Prop* aProp = (*aIt);
69 myResourceMgr->setValue(QString(aProp->section().c_str()), QString(aProp->name().c_str()),
70 QString(aProp->value().c_str()));
74 void ModuleBase_Preferences::resetConfig()
76 Config_Properties aProps = Config_PropManager::getProperties();
77 Config_Properties::iterator aIt;
78 for (aIt = aProps.begin(); aIt != aProps.end(); ++aIt) {
79 Config_Prop* aProp = (*aIt);
80 aProp->setValue(aProp->defaultValue());
84 void ModuleBase_Preferences::loadCustomProps()
88 QStringList aSections = myResourceMgr->sections();
89 foreach (QString aSection, aSections)
91 QStringList aParams = myResourceMgr->parameters(aSection);
92 foreach (QString aParam, aParams)
94 Config_Prop* aProp = Config_PropManager::registerProp(aSection.toStdString(),
95 aParam.toStdString(), "", Config_Prop::Disabled);
96 aProp->setValue(myResourceMgr->stringValue(aSection, aParam).toStdString());
102 void ModuleBase_Preferences::createEditContent(ModuleBase_IPrefMgr* thePref, int thePage)
104 thePref->prefMgr()->setItemIcon(thePage, QIcon(":pictures/module.png"));
105 createCustomPage(thePref, thePage);
108 void ModuleBase_Preferences::resetResourcePreferences(SUIT_PreferenceMgr* thePref)
113 QtxResourceMgr::WorkingMode aPrev =
114 thePref->resourceMgr()->setWorkingMode(QtxResourceMgr::IgnoreUserValues);
116 thePref->resourceMgr()->setWorkingMode(aPrev);
119 void ModuleBase_Preferences::resetConfigPropPreferences(SUIT_PreferenceMgr* thePref)
122 updateResourcesByConfig();
124 // retrieve the reset resource values to the preferences items
125 Config_Properties aProps = Config_PropManager::getProperties();
126 Config_Properties::iterator aIt;
128 QStringList aSections;
129 for (aIt = aProps.begin(); aIt != aProps.end(); ++aIt) {
130 Config_Prop* aProp = (*aIt);
131 aValues.append(QString(aProp->name().c_str()));
132 if (!aSections.contains(aProp->section().c_str()))
133 aSections.append(aProp->section().c_str());
134 QtxPreferenceItem* anItem = thePref->findItem(QString(aProp->title().c_str()), true);
140 void ModuleBase_Preferences::createCustomPage(ModuleBase_IPrefMgr* thePref, int thePageId)
142 SUIT_ResourceMgr* aResMgr = ModuleBase_Preferences::resourceMgr();
143 bool isResModified = false;
145 // Make a Tab from each section
146 std::list<std::string> aSections = Config_PropManager::getSections();
147 std::list<std::string>::const_iterator it;
148 for (it = aSections.cbegin(); it != aSections.cend(); ++it) {
149 Config_Properties aProps = Config_PropManager::getProperties(*it);
150 int aTab = thePref->prefMgr()->addItem(QString((*it).c_str()), thePageId);
151 thePref->prefMgr()->setItemProperty("columns", 2, aTab);
153 Config_Properties::const_iterator aIt;
154 for (aIt = aProps.cbegin(); aIt != aProps.cend(); ++aIt) {
155 Config_Prop* aProp = (*aIt);
156 // check that the property is defined
157 QString aSection(aProp->section().c_str());
158 QString aName(aProp->name().c_str());
159 if (!aResMgr->hasValue(aSection, aName)) {
160 aResMgr->setValue(aSection, aName, QString(aProp->value().c_str()));
161 isResModified = true;
164 if (aProp->type() != Config_Prop::Disabled) {
165 SUIT_PreferenceMgr::PrefItemType aPrefType = SUIT_PreferenceMgr::Auto;
166 if (aProp->type() == Config_Prop::Directory) {
167 aPrefType = SUIT_PreferenceMgr::File;
169 aPrefType = (SUIT_PreferenceMgr::PrefItemType) aProp->type();
171 int anId = thePref->addPreference(QObject::tr(aProp->title().c_str()), aTab, aPrefType,
172 QString::fromStdString(aProp->section()),
173 QString::fromStdString(aProp->name()));
174 if(aProp->type() == Config_Prop::Directory) {
175 thePref->setItemProperty("path_type", Qtx::PT_Directory, anId);
183 * Implementation of preferences manager interface
185 class ModuleBase_PrefMgr: public ModuleBase_IPrefMgr
189 /// \param theMgr a preferences manager
190 ModuleBase_PrefMgr(ModuleBase_PreferencesMgr* theMgr):myMgr(theMgr) {}
192 virtual int addPreference(const QString& theLbl, int pId,
193 SUIT_PreferenceMgr::PrefItemType theType,
194 const QString& theSection, const QString& theName )
196 return myMgr->addItem(theLbl, pId, theType, theSection, theName);
199 virtual void setItemProperty( const QString& thePropName, const QVariant& theValue,
200 const int theId = -1) {
201 myMgr->setItemProperty(thePropName, theValue, theId);
204 virtual SUIT_PreferenceMgr* prefMgr() const { return myMgr; }
207 ModuleBase_PreferencesMgr* myMgr;
210 //**********************************************************
211 //**********************************************************
212 //**********************************************************
213 ModuleBase_PreferencesDlg::ModuleBase_PreferencesDlg(SUIT_ResourceMgr* theResurces, QWidget* theParent)
214 : QDialog(theParent),
217 setWindowTitle(tr("Edit preferences"));
219 QVBoxLayout* main = new QVBoxLayout(this);
223 myPreferences = new ModuleBase_PreferencesMgr(theResurces, this);
224 main->addWidget(myPreferences);
226 setFocusProxy(myPreferences);
227 myPreferences->setFrameStyle(QFrame::Box | QFrame::Sunken);
229 QDialogButtonBox* aBtnBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel |
230 QDialogButtonBox::Reset,
231 Qt::Horizontal, this);
232 QPushButton* aDefaultButton = aBtnBox->button(QDialogButtonBox::Reset);
233 aDefaultButton->setText(tr("Default"));
234 connect(aDefaultButton, SIGNAL(clicked()), this, SLOT(onDefault()));
236 main->addWidget(aBtnBox);
237 connect(aBtnBox, SIGNAL(accepted()), this, SLOT(accept()));
238 connect(aBtnBox, SIGNAL(rejected()), this, SLOT(reject()));
241 myPreferences->retrieve();
242 setMinimumSize(800, 200);
245 ModuleBase_PreferencesDlg::~ModuleBase_PreferencesDlg()
249 void ModuleBase_PreferencesDlg::createEditors()
251 int aPage = myPreferences->addItem(tr("Desktop"));
252 myPreferences->setItemIcon(aPage, QIcon(":pictures/view_prefs.png"));
254 createMenuPage(aPage);
255 createViewerPage(aPage);
257 aPage = myPreferences->addItem(tr("Module"));
258 myPreferences->setItemIcon(aPage, QIcon(":pictures/module.png"));
260 ModuleBase_PrefMgr aMgr(myPreferences);
261 ModuleBase_Preferences::createEditContent(&aMgr, aPage);
264 void ModuleBase_PreferencesDlg::createViewerPage(int thePageId)
266 int viewTab = myPreferences->addItem(tr("Viewer"), thePageId);
268 QStringList gradList;
269 gradList << tr("Horizontal gradient") << tr("Vertical gradient") << tr("First diagonal gradient")
270 << tr("Second diagonal gradient") << tr("First corner gradient")
271 << tr("Second corner gradient") << tr("Third corner gradient")
272 << tr("Fourth corner gradient");
274 QList<QVariant> idList;
275 for (int i = 0; i < gradList.size(); i++)
278 int bgGroup = myPreferences->addItem(tr("Background"), viewTab);
280 QString aImgFiles("Image files (*.bmp *.gif *.pix *.xwd *.rgb *.rs)");
282 int bgId = myPreferences->addItem(tr("Viewer 3d"), bgGroup, SUIT_PreferenceMgr::Background,
283 ModuleBase_Preferences::VIEWER_SECTION, "background");
284 myPreferences->setItemProperty("gradient_names", gradList, bgId);
285 myPreferences->setItemProperty("gradient_ids", idList, bgId);
286 myPreferences->setItemProperty("texture_enabled", true, bgId);
287 myPreferences->setItemProperty("texture_center_enabled", true, bgId);
288 myPreferences->setItemProperty("texture_tile_enabled", true, bgId);
289 myPreferences->setItemProperty("texture_stretch_enabled", true, bgId);
290 myPreferences->setItemProperty("custom_enabled", false, bgId);
291 myPreferences->setItemProperty("image_formats", aImgFiles, bgId);
293 // Create other parameters group in viewer tab
294 int otherGroup = myPreferences->addItem(tr("Default selection"), viewTab);
295 myPreferences->setItemProperty("columns", 3, otherGroup);
296 myPreferences->addItem(tr("Faces"), otherGroup,
297 SUIT_PreferenceMgr::Bool,
298 ModuleBase_Preferences::VIEWER_SECTION, "face-selection");
299 myPreferences->addItem(tr("Edges"), otherGroup,
300 SUIT_PreferenceMgr::Bool,
301 ModuleBase_Preferences::VIEWER_SECTION, "edge-selection");
302 myPreferences->addItem(tr("Vertices"), otherGroup,
303 SUIT_PreferenceMgr::Bool,
304 ModuleBase_Preferences::VIEWER_SECTION, "vertex-selection");
307 void ModuleBase_PreferencesDlg::createMenuPage(int thePageId)
309 int aMenuTab = myPreferences->addItem(tr("Main menu"), thePageId);
311 int aSizeGroup = myPreferences->addItem(tr("Size"), aMenuTab);
312 myPreferences->setItemProperty("columns", 1, aSizeGroup);
314 int aRowsNb = myPreferences->addItem(tr("Number of rows"), aSizeGroup,
315 SUIT_PreferenceMgr::IntSpin, ModuleBase_Preferences::MENU_SECTION,
317 myPreferences->setItemProperty("min", 1, aRowsNb);
318 myPreferences->setItemProperty("max", 6, aRowsNb);
320 myPreferences->addItem(tr("Show Status Bar"), aSizeGroup,
321 SUIT_PreferenceMgr::Bool, ModuleBase_Preferences::MENU_SECTION,
325 void ModuleBase_PreferencesDlg::accept()
327 myPreferences->store();
330 // Save custom properties
331 ModuleBase_Preferences::updateConfigByResources();
335 void ModuleBase_PreferencesDlg::modified(ModuleBase_Prefs& theModified) const
337 theModified = myPreferences->modified();
340 void ModuleBase_PreferencesDlg::onDefault()
342 // reset main resources. It throwns all resource manager items to the
343 // initial/default state. If there is no a default state of the item,
344 // it will be filled with an empty value. It concernerned to plugin
345 // config items, like visualization color. The main xml do not contains
346 // default values for them. So, it is important to reset the config
347 // properties after reseting the resources preferences.
348 ModuleBase_Preferences::resetResourcePreferences(myPreferences);
349 // reset plugin's resources. It fills the config resources with the default
350 // values, stores result in the resource manager and retrieve the preferences
351 // items with these values.
352 ModuleBase_Preferences::resetConfigPropPreferences(myPreferences);
355 //**********************************************************
356 //**********************************************************
357 //**********************************************************
358 void ModuleBase_PreferencesMgr::changedResources(const ResourceMap& theMap)
361 ResourceMap::ConstIterator it;
363 for (it = theMap.begin(); it != theMap.end(); ++it) {
364 ModuleBase_Pref aPref;
365 it.key()->resource(aPref.first, aPref.second);
366 myModified.append(aPref);