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,
215 : QDialog(theParent),
218 setWindowTitle(tr("Edit preferences"));
220 QVBoxLayout* main = new QVBoxLayout(this);
224 myPreferences = new ModuleBase_PreferencesMgr(theResurces, this);
225 main->addWidget(myPreferences);
227 setFocusProxy(myPreferences);
228 myPreferences->setFrameStyle(QFrame::Box | QFrame::Sunken);
230 QDialogButtonBox* aBtnBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel |
231 QDialogButtonBox::Reset,
232 Qt::Horizontal, this);
233 QPushButton* aDefaultButton = aBtnBox->button(QDialogButtonBox::Reset);
234 aDefaultButton->setText(tr("Default"));
235 connect(aDefaultButton, SIGNAL(clicked()), this, SLOT(onDefault()));
237 main->addWidget(aBtnBox);
238 connect(aBtnBox, SIGNAL(accepted()), this, SLOT(accept()));
239 connect(aBtnBox, SIGNAL(rejected()), this, SLOT(reject()));
242 myPreferences->retrieve();
243 setMinimumSize(800, 240);
246 ModuleBase_PreferencesDlg::~ModuleBase_PreferencesDlg()
250 void ModuleBase_PreferencesDlg::createEditors()
252 int aPage = myPreferences->addItem(tr("Desktop"));
253 myPreferences->setItemIcon(aPage, QIcon(":pictures/view_prefs.png"));
255 createMenuPage(aPage);
256 createViewerPage(aPage);
258 aPage = myPreferences->addItem(tr("Module"));
259 myPreferences->setItemIcon(aPage, QIcon(":pictures/module.png"));
261 ModuleBase_PrefMgr aMgr(myPreferences);
262 ModuleBase_Preferences::createEditContent(&aMgr, aPage);
265 void ModuleBase_PreferencesDlg::createViewerPage(int thePageId)
267 int viewTab = myPreferences->addItem(tr("Viewer"), thePageId);
269 QStringList gradList;
270 gradList << tr("Horizontal gradient") << tr("Vertical gradient") << tr("First diagonal gradient")
271 << tr("Second diagonal gradient") << tr("First corner gradient")
272 << tr("Second corner gradient") << tr("Third corner gradient")
273 << tr("Fourth corner gradient");
275 QList<QVariant> idList;
276 for (int i = 0; i < gradList.size(); i++)
279 int bgGroup = myPreferences->addItem(tr("Background"), viewTab);
281 QString aImgFiles("Image files (*.bmp *.gif *.pix *.xwd *.rgb *.rs)");
283 int bgId = myPreferences->addItem(tr("Viewer 3d"), bgGroup, SUIT_PreferenceMgr::Background,
284 ModuleBase_Preferences::VIEWER_SECTION, "background");
285 myPreferences->setItemProperty("gradient_names", gradList, bgId);
286 myPreferences->setItemProperty("gradient_ids", idList, bgId);
287 myPreferences->setItemProperty("texture_enabled", true, bgId);
288 myPreferences->setItemProperty("texture_center_enabled", true, bgId);
289 myPreferences->setItemProperty("texture_tile_enabled", true, bgId);
290 myPreferences->setItemProperty("texture_stretch_enabled", true, bgId);
291 myPreferences->setItemProperty("custom_enabled", false, bgId);
292 myPreferences->setItemProperty("image_formats", aImgFiles, bgId);
294 // Create other parameters group in viewer tab
295 int selectionGroup = myPreferences->addItem(tr("Default selection"), viewTab);
296 myPreferences->setItemProperty("columns", 3, selectionGroup);
297 myPreferences->addItem(tr("Faces"), selectionGroup,
298 SUIT_PreferenceMgr::Bool,
299 ModuleBase_Preferences::VIEWER_SECTION, "face-selection");
300 myPreferences->addItem(tr("Edges"), selectionGroup,
301 SUIT_PreferenceMgr::Bool,
302 ModuleBase_Preferences::VIEWER_SECTION, "edge-selection");
303 myPreferences->addItem(tr("Vertices"), selectionGroup,
304 SUIT_PreferenceMgr::Bool,
305 ModuleBase_Preferences::VIEWER_SECTION, "vertex-selection");
307 int sensitivityGroup = myPreferences->addItem(tr("Selection sensitivity"), viewTab);
308 myPreferences->setItemProperty("columns", 2, sensitivityGroup);
309 myPreferences->addItem(tr("Vertex"), sensitivityGroup, SUIT_PreferenceMgr::Double,
310 ModuleBase_Preferences::VIEWER_SECTION, "point-selection-sensitivity");
311 myPreferences->addItem(tr("Edge"), sensitivityGroup, SUIT_PreferenceMgr::Double,
312 ModuleBase_Preferences::VIEWER_SECTION, "edge-selection-sensitivity");
315 void ModuleBase_PreferencesDlg::createMenuPage(int thePageId)
317 int aMenuTab = myPreferences->addItem(tr("Main menu"), thePageId);
319 int aSizeGroup = myPreferences->addItem(tr("Size"), aMenuTab);
320 myPreferences->setItemProperty("columns", 1, aSizeGroup);
322 int aRowsNb = myPreferences->addItem(tr("Number of rows"), aSizeGroup,
323 SUIT_PreferenceMgr::IntSpin,
324 ModuleBase_Preferences::MENU_SECTION,
326 myPreferences->setItemProperty("min", 1, aRowsNb);
327 myPreferences->setItemProperty("max", 6, aRowsNb);
329 myPreferences->addItem(tr("Show Status Bar"), aSizeGroup,
330 SUIT_PreferenceMgr::Bool, ModuleBase_Preferences::MENU_SECTION,
334 void ModuleBase_PreferencesDlg::accept()
336 myPreferences->store();
339 // Save custom properties
340 ModuleBase_Preferences::updateConfigByResources();
344 void ModuleBase_PreferencesDlg::modified(ModuleBase_Prefs& theModified) const
346 theModified = myPreferences->modified();
349 void ModuleBase_PreferencesDlg::onDefault()
351 // reset main resources. It throwns all resource manager items to the
352 // initial/default state. If there is no a default state of the item,
353 // it will be filled with an empty value. It concernerned to plugin
354 // config items, like visualization color. The main xml do not contains
355 // default values for them. So, it is important to reset the config
356 // properties after reseting the resources preferences.
357 ModuleBase_Preferences::resetResourcePreferences(myPreferences);
358 // reset plugin's resources. It fills the config resources with the default
359 // values, stores result in the resource manager and retrieve the preferences
360 // items with these values.
361 ModuleBase_Preferences::resetConfigPropPreferences(myPreferences);
364 //**********************************************************
365 //**********************************************************
366 //**********************************************************
367 void ModuleBase_PreferencesMgr::changedResources(const ResourceMap& theMap)
370 ResourceMap::ConstIterator it;
372 for (it = theMap.begin(); it != theMap.end(); ++it) {
373 ModuleBase_Pref aPref;
374 it.key()->resource(aPref.first, aPref.second);
375 myModified.append(aPref);