1 // Copyright (C) 2014-2019 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "ModuleBase_Preferences.h"
21 //#include "ModuleBase_Constants.h"
23 #include <Config_PropManager.h>
25 #include <TopAbs_ShapeEnum.hxx>
27 #include <SUIT_ResourceMgr.h>
28 #include <SUIT_PreferenceMgr.h>
32 #include <QApplication>
33 #include <QDialogButtonBox>
34 #include <QPushButton>
36 const QString ModuleBase_Preferences::VIEWER_SECTION = "Viewer";
37 const QString ModuleBase_Preferences::MENU_SECTION = "Menu";
39 SUIT_ResourceMgr* ModuleBase_Preferences::myResourceMgr = 0;
41 SUIT_ResourceMgr* ModuleBase_Preferences::resourceMgr()
44 myResourceMgr = new SUIT_ResourceMgr("SHAPER");
45 myResourceMgr->setCurrentFormat("xml");
50 bool ModuleBase_Preferences::editPreferences(ModuleBase_Prefs& theModified)
52 ModuleBase_PreferencesDlg aDlg(resourceMgr(), QApplication::activeWindow());
54 if (aDlg.isChanged()) {
55 aDlg.modified(theModified);
56 resourceMgr()->save();
62 void ModuleBase_Preferences::updateConfigByResources()
64 Config_Properties aProps = Config_PropManager::getProperties();
65 Config_Properties::iterator aIt;
66 for (aIt = aProps.begin(); aIt != aProps.end(); ++aIt) {
67 Config_Prop* aProp = (*aIt);
68 QString aVal = myResourceMgr->stringValue(QString(aProp->section().c_str()),
69 QString(aProp->name().c_str()));
70 if (!aVal.isEmpty()) {
71 aProp->setValue(aVal.toStdString());
76 void ModuleBase_Preferences::updateResourcesByConfig()
78 Config_Properties aProps = Config_PropManager::getProperties();
79 Config_Properties::iterator aIt;
80 for (aIt = aProps.begin(); aIt != aProps.end(); ++aIt) {
81 Config_Prop* aProp = (*aIt);
82 myResourceMgr->setValue(QString(aProp->section().c_str()), QString(aProp->name().c_str()),
83 QString(aProp->value().c_str()));
87 void ModuleBase_Preferences::resetConfig()
89 Config_Properties aProps = Config_PropManager::getProperties();
90 Config_Properties::iterator aIt;
91 for (aIt = aProps.begin(); aIt != aProps.end(); ++aIt) {
92 Config_Prop* aProp = (*aIt);
93 aProp->setValue(aProp->defaultValue());
97 void ModuleBase_Preferences::loadCustomProps()
101 QStringList aSections = myResourceMgr->sections();
102 foreach (QString aSection, aSections)
104 QStringList aParams = myResourceMgr->parameters(aSection);
105 foreach (QString aParam, aParams)
107 Config_Prop* aProp = Config_PropManager::registerProp(aSection.toStdString(),
108 aParam.toStdString(), "", Config_Prop::Disabled);
109 aProp->setValue(myResourceMgr->stringValue(aSection, aParam).toStdString());
115 void ModuleBase_Preferences::createEditContent(ModuleBase_IPrefMgr* thePref, int thePage)
117 thePref->prefMgr()->setItemIcon(thePage, QIcon(":pictures/module.png"));
118 createCustomPage(thePref, thePage);
121 void ModuleBase_Preferences::resetResourcePreferences(SUIT_PreferenceMgr* thePref)
126 QtxResourceMgr::WorkingMode aPrev =
127 thePref->resourceMgr()->setWorkingMode(QtxResourceMgr::IgnoreUserValues);
129 thePref->resourceMgr()->setWorkingMode(aPrev);
132 void ModuleBase_Preferences::resetConfigPropPreferences(SUIT_PreferenceMgr* thePref)
135 updateResourcesByConfig();
137 // retrieve the reset resource values to the preferences items
138 Config_Properties aProps = Config_PropManager::getProperties();
139 Config_Properties::iterator aIt;
141 QStringList aSections;
142 for (aIt = aProps.begin(); aIt != aProps.end(); ++aIt) {
143 Config_Prop* aProp = (*aIt);
144 aValues.append(QString(aProp->name().c_str()));
145 if (!aSections.contains(aProp->section().c_str()))
146 aSections.append(aProp->section().c_str());
147 QtxPreferenceItem* anItem = thePref->findItem(QString(aProp->title().c_str()), true);
153 void ModuleBase_Preferences::createCustomPage(ModuleBase_IPrefMgr* thePref, int thePageId)
155 SUIT_ResourceMgr* aResMgr = ModuleBase_Preferences::resourceMgr();
156 bool isResModified = false;
158 // Make a Tab from each section
159 std::list<std::string> aSections = Config_PropManager::getSections();
160 std::list<std::string>::const_iterator it;
161 for (it = aSections.cbegin(); it != aSections.cend(); ++it) {
162 Config_Properties aProps = Config_PropManager::getProperties(*it);
163 int aTab = thePref->prefMgr()->addItem(QString((*it).c_str()), thePageId);
164 thePref->prefMgr()->setItemProperty("columns", 2, aTab);
166 Config_Properties::const_iterator aIt;
167 for (aIt = aProps.cbegin(); aIt != aProps.cend(); ++aIt) {
168 Config_Prop* aProp = (*aIt);
169 // check that the property is defined
170 QString aSection(aProp->section().c_str());
171 QString aName(aProp->name().c_str());
172 if (!aResMgr->hasValue(aSection, aName)) {
173 aResMgr->setValue(aSection, aName, QString(aProp->value().c_str()));
174 isResModified = true;
177 if (aProp->type() != Config_Prop::Disabled) {
178 SUIT_PreferenceMgr::PrefItemType aPrefType = SUIT_PreferenceMgr::Auto;
179 if (aProp->type() == Config_Prop::Directory) {
180 aPrefType = SUIT_PreferenceMgr::File;
182 aPrefType = (SUIT_PreferenceMgr::PrefItemType) aProp->type();
184 int anId = thePref->addPreference(QObject::tr(aProp->title().c_str()), aTab, aPrefType,
185 QString::fromStdString(aProp->section()),
186 QString::fromStdString(aProp->name()));
187 if(aProp->type() == Config_Prop::Directory) {
188 thePref->setItemProperty("path_type", Qtx::PT_Directory, anId);
190 if (aPrefType == SUIT_PreferenceMgr::DblSpin) {
191 if (aProp->min() != "") {
192 double aMin = QString(aProp->min().c_str()).toDouble();
193 thePref->setItemProperty("min", aMin, anId);
195 if (aProp->max() != "") {
196 double aMax = QString(aProp->max().c_str()).toDouble();
197 thePref->setItemProperty("max", aMax, anId);
206 * Implementation of preferences manager interface
208 class ModuleBase_PrefMgr: public ModuleBase_IPrefMgr
212 /// \param theMgr a preferences manager
213 ModuleBase_PrefMgr(ModuleBase_PreferencesMgr* theMgr):myMgr(theMgr) {}
215 virtual int addPreference(const QString& theLbl, int pId,
216 SUIT_PreferenceMgr::PrefItemType theType,
217 const QString& theSection, const QString& theName )
219 return myMgr->addItem(theLbl, pId, theType, theSection, theName);
222 virtual void setItemProperty( const QString& thePropName, const QVariant& theValue,
223 const int theId = -1) {
224 myMgr->setItemProperty(thePropName, theValue, theId);
227 virtual SUIT_PreferenceMgr* prefMgr() const { return myMgr; }
230 ModuleBase_PreferencesMgr* myMgr;
233 //**********************************************************
234 //**********************************************************
235 //**********************************************************
236 ModuleBase_PreferencesDlg::ModuleBase_PreferencesDlg(SUIT_ResourceMgr* theResurces,
238 : QDialog(theParent),
241 setWindowTitle(tr("Edit preferences"));
243 QVBoxLayout* main = new QVBoxLayout(this);
247 myPreferences = new ModuleBase_PreferencesMgr(theResurces, this);
248 main->addWidget(myPreferences);
250 setFocusProxy(myPreferences);
251 myPreferences->setFrameStyle(QFrame::Box | QFrame::Sunken);
253 QDialogButtonBox* aBtnBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel |
254 QDialogButtonBox::Reset,
255 Qt::Horizontal, this);
256 QPushButton* aDefaultButton = aBtnBox->button(QDialogButtonBox::Reset);
257 aDefaultButton->setText(tr("Default"));
258 connect(aDefaultButton, SIGNAL(clicked()), this, SLOT(onDefault()));
260 main->addWidget(aBtnBox);
261 connect(aBtnBox, SIGNAL(accepted()), this, SLOT(accept()));
262 connect(aBtnBox, SIGNAL(rejected()), this, SLOT(reject()));
265 myPreferences->retrieve();
266 setMinimumSize(800, 240);
269 ModuleBase_PreferencesDlg::~ModuleBase_PreferencesDlg()
273 void ModuleBase_PreferencesDlg::createEditors()
275 int aPage = myPreferences->addItem(tr("Desktop"));
276 myPreferences->setItemIcon(aPage, QIcon(":pictures/view_prefs.png"));
278 createMenuPage(aPage);
279 createViewerPage(aPage);
281 aPage = myPreferences->addItem(tr("Module"));
282 myPreferences->setItemIcon(aPage, QIcon(":pictures/module.png"));
284 ModuleBase_PrefMgr aMgr(myPreferences);
285 ModuleBase_Preferences::createEditContent(&aMgr, aPage);
288 void ModuleBase_PreferencesDlg::createViewerPage(int thePageId)
290 int viewTab = myPreferences->addItem(tr("Viewer"), thePageId);
292 QStringList gradList;
293 gradList << tr("Horizontal gradient") << tr("Vertical gradient") << tr("First diagonal gradient")
294 << tr("Second diagonal gradient") << tr("First corner gradient")
295 << tr("Second corner gradient") << tr("Third corner gradient")
296 << tr("Fourth corner gradient");
298 QList<QVariant> idList;
299 for (int i = 0; i < gradList.size(); i++)
302 int bgGroup = myPreferences->addItem(tr("Background"), viewTab);
304 QString aImgFiles("Image files (*.bmp *.gif *.pix *.xwd *.rgb *.rs)");
306 int bgId = myPreferences->addItem(tr("Viewer 3d"), bgGroup, SUIT_PreferenceMgr::Background,
307 ModuleBase_Preferences::VIEWER_SECTION, "background");
308 myPreferences->setItemProperty("gradient_names", gradList, bgId);
309 myPreferences->setItemProperty("gradient_ids", idList, bgId);
310 myPreferences->setItemProperty("texture_enabled", true, bgId);
311 myPreferences->setItemProperty("texture_center_enabled", true, bgId);
312 myPreferences->setItemProperty("texture_tile_enabled", true, bgId);
313 myPreferences->setItemProperty("texture_stretch_enabled", true, bgId);
314 myPreferences->setItemProperty("custom_enabled", false, bgId);
315 myPreferences->setItemProperty("image_formats", aImgFiles, bgId);
317 // Create other parameters group in viewer tab
318 int selectionGroup = myPreferences->addItem(tr("Default selection"), viewTab);
319 myPreferences->setItemProperty("columns", 3, selectionGroup);
320 myPreferences->addItem(tr("Faces"), selectionGroup,
321 SUIT_PreferenceMgr::Bool,
322 ModuleBase_Preferences::VIEWER_SECTION, "face-selection");
323 myPreferences->addItem(tr("Edges"), selectionGroup,
324 SUIT_PreferenceMgr::Bool,
325 ModuleBase_Preferences::VIEWER_SECTION, "edge-selection");
326 myPreferences->addItem(tr("Vertices"), selectionGroup,
327 SUIT_PreferenceMgr::Bool,
328 ModuleBase_Preferences::VIEWER_SECTION, "vertex-selection");
330 int sensitivityGroup = myPreferences->addItem(tr("Selection sensitivity"), viewTab);
331 myPreferences->setItemProperty("columns", 2, sensitivityGroup);
332 myPreferences->addItem(tr("Vertex"), sensitivityGroup, SUIT_PreferenceMgr::DblSpin,
333 ModuleBase_Preferences::VIEWER_SECTION, "point-selection-sensitivity");
334 myPreferences->addItem(tr("Edge"), sensitivityGroup, SUIT_PreferenceMgr::DblSpin,
335 ModuleBase_Preferences::VIEWER_SECTION, "edge-selection-sensitivity");
338 void ModuleBase_PreferencesDlg::createMenuPage(int thePageId)
340 int aMenuTab = myPreferences->addItem(tr("Main menu"), thePageId);
342 int aSizeGroup = myPreferences->addItem(tr("Size"), aMenuTab);
343 myPreferences->setItemProperty("columns", 1, aSizeGroup);
345 int aRowsNb = myPreferences->addItem(tr("Number of rows"), aSizeGroup,
346 SUIT_PreferenceMgr::IntSpin,
347 ModuleBase_Preferences::MENU_SECTION,
349 myPreferences->setItemProperty("min", 1, aRowsNb);
350 myPreferences->setItemProperty("max", 6, aRowsNb);
352 myPreferences->addItem(tr("Show Status Bar"), aSizeGroup,
353 SUIT_PreferenceMgr::Bool, ModuleBase_Preferences::MENU_SECTION,
357 void ModuleBase_PreferencesDlg::accept()
359 myPreferences->store();
362 // Save custom properties
363 ModuleBase_Preferences::updateConfigByResources();
367 void ModuleBase_PreferencesDlg::modified(ModuleBase_Prefs& theModified) const
369 theModified = myPreferences->modified();
372 void ModuleBase_PreferencesDlg::onDefault()
374 // reset main resources. It throwns all resource manager items to the
375 // initial/default state. If there is no a default state of the item,
376 // it will be filled with an empty value. It concernerned to plugin
377 // config items, like visualization color. The main xml do not contains
378 // default values for them. So, it is important to reset the config
379 // properties after reseting the resources preferences.
380 ModuleBase_Preferences::resetResourcePreferences(myPreferences);
381 // reset plugin's resources. It fills the config resources with the default
382 // values, stores result in the resource manager and retrieve the preferences
383 // items with these values.
384 ModuleBase_Preferences::resetConfigPropPreferences(myPreferences);
387 //**********************************************************
388 //**********************************************************
389 //**********************************************************
390 void ModuleBase_PreferencesMgr::changedResources(const ResourceMap& theMap)
393 ResourceMap::ConstIterator it;
395 for (it = theMap.begin(); it != theMap.end(); ++it) {
396 ModuleBase_Pref aPref;
397 it.key()->resource(aPref.first, aPref.second);
398 myModified.append(aPref);