1 // Copyright (C) 2014-2017 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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "ModuleBase_Preferences.h"
22 //#include "ModuleBase_Constants.h"
24 #include <Config_PropManager.h>
26 #include <TopAbs_ShapeEnum.hxx>
28 #include <SUIT_ResourceMgr.h>
29 #include <SUIT_PreferenceMgr.h>
33 #include <QApplication>
34 #include <QDialogButtonBox>
35 #include <QPushButton>
37 const QString ModuleBase_Preferences::VIEWER_SECTION = "Viewer";
38 const QString ModuleBase_Preferences::MENU_SECTION = "Menu";
40 SUIT_ResourceMgr* ModuleBase_Preferences::myResourceMgr = 0;
42 SUIT_ResourceMgr* ModuleBase_Preferences::resourceMgr()
45 myResourceMgr = new SUIT_ResourceMgr("SHAPER");
46 myResourceMgr->setCurrentFormat("xml");
51 bool ModuleBase_Preferences::editPreferences(ModuleBase_Prefs& theModified)
53 ModuleBase_PreferencesDlg aDlg(resourceMgr(), QApplication::activeWindow());
55 if (aDlg.isChanged()) {
56 aDlg.modified(theModified);
57 resourceMgr()->save();
63 void ModuleBase_Preferences::updateConfigByResources()
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 QString aVal = myResourceMgr->stringValue(QString(aProp->section().c_str()),
70 QString(aProp->name().c_str()));
71 if (!aVal.isEmpty()) {
72 aProp->setValue(aVal.toStdString());
77 void ModuleBase_Preferences::updateResourcesByConfig()
79 Config_Properties aProps = Config_PropManager::getProperties();
80 Config_Properties::iterator aIt;
81 for (aIt = aProps.begin(); aIt != aProps.end(); ++aIt) {
82 Config_Prop* aProp = (*aIt);
83 myResourceMgr->setValue(QString(aProp->section().c_str()), QString(aProp->name().c_str()),
84 QString(aProp->value().c_str()));
88 void ModuleBase_Preferences::resetConfig()
90 Config_Properties aProps = Config_PropManager::getProperties();
91 Config_Properties::iterator aIt;
92 for (aIt = aProps.begin(); aIt != aProps.end(); ++aIt) {
93 Config_Prop* aProp = (*aIt);
94 aProp->setValue(aProp->defaultValue());
98 void ModuleBase_Preferences::loadCustomProps()
102 QStringList aSections = myResourceMgr->sections();
103 foreach (QString aSection, aSections)
105 QStringList aParams = myResourceMgr->parameters(aSection);
106 foreach (QString aParam, aParams)
108 Config_Prop* aProp = Config_PropManager::registerProp(aSection.toStdString(),
109 aParam.toStdString(), "", Config_Prop::Disabled);
110 aProp->setValue(myResourceMgr->stringValue(aSection, aParam).toStdString());
116 void ModuleBase_Preferences::createEditContent(ModuleBase_IPrefMgr* thePref, int thePage)
118 thePref->prefMgr()->setItemIcon(thePage, QIcon(":pictures/module.png"));
119 createCustomPage(thePref, thePage);
122 void ModuleBase_Preferences::resetResourcePreferences(SUIT_PreferenceMgr* thePref)
127 QtxResourceMgr::WorkingMode aPrev =
128 thePref->resourceMgr()->setWorkingMode(QtxResourceMgr::IgnoreUserValues);
130 thePref->resourceMgr()->setWorkingMode(aPrev);
133 void ModuleBase_Preferences::resetConfigPropPreferences(SUIT_PreferenceMgr* thePref)
136 updateResourcesByConfig();
138 // retrieve the reset resource values to the preferences items
139 Config_Properties aProps = Config_PropManager::getProperties();
140 Config_Properties::iterator aIt;
142 QStringList aSections;
143 for (aIt = aProps.begin(); aIt != aProps.end(); ++aIt) {
144 Config_Prop* aProp = (*aIt);
145 aValues.append(QString(aProp->name().c_str()));
146 if (!aSections.contains(aProp->section().c_str()))
147 aSections.append(aProp->section().c_str());
148 QtxPreferenceItem* anItem = thePref->findItem(QString(aProp->title().c_str()), true);
154 void ModuleBase_Preferences::createCustomPage(ModuleBase_IPrefMgr* thePref, int thePageId)
156 SUIT_ResourceMgr* aResMgr = ModuleBase_Preferences::resourceMgr();
157 bool isResModified = false;
159 // Make a Tab from each section
160 std::list<std::string> aSections = Config_PropManager::getSections();
161 std::list<std::string>::const_iterator it;
162 for (it = aSections.cbegin(); it != aSections.cend(); ++it) {
163 Config_Properties aProps = Config_PropManager::getProperties(*it);
164 int aTab = thePref->prefMgr()->addItem(QString((*it).c_str()), thePageId);
165 thePref->prefMgr()->setItemProperty("columns", 2, aTab);
167 Config_Properties::const_iterator aIt;
168 for (aIt = aProps.cbegin(); aIt != aProps.cend(); ++aIt) {
169 Config_Prop* aProp = (*aIt);
170 // check that the property is defined
171 QString aSection(aProp->section().c_str());
172 QString aName(aProp->name().c_str());
173 if (!aResMgr->hasValue(aSection, aName)) {
174 aResMgr->setValue(aSection, aName, QString(aProp->value().c_str()));
175 isResModified = true;
178 if (aProp->type() != Config_Prop::Disabled) {
179 SUIT_PreferenceMgr::PrefItemType aPrefType = SUIT_PreferenceMgr::Auto;
180 if (aProp->type() == Config_Prop::Directory) {
181 aPrefType = SUIT_PreferenceMgr::File;
183 aPrefType = (SUIT_PreferenceMgr::PrefItemType) aProp->type();
185 int anId = thePref->addPreference(QObject::tr(aProp->title().c_str()), aTab, aPrefType,
186 QString::fromStdString(aProp->section()),
187 QString::fromStdString(aProp->name()));
188 if(aProp->type() == Config_Prop::Directory) {
189 thePref->setItemProperty("path_type", Qtx::PT_Directory, anId);
191 if (aPrefType == SUIT_PreferenceMgr::DblSpin) {
192 if (aProp->min() != "") {
193 double aMin = QString(aProp->min().c_str()).toDouble();
194 thePref->setItemProperty("min", aMin, anId);
196 if (aProp->max() != "") {
197 double aMax = QString(aProp->max().c_str()).toDouble();
198 thePref->setItemProperty("max", aMax, anId);
207 * Implementation of preferences manager interface
209 class ModuleBase_PrefMgr: public ModuleBase_IPrefMgr
213 /// \param theMgr a preferences manager
214 ModuleBase_PrefMgr(ModuleBase_PreferencesMgr* theMgr):myMgr(theMgr) {}
216 virtual int addPreference(const QString& theLbl, int pId,
217 SUIT_PreferenceMgr::PrefItemType theType,
218 const QString& theSection, const QString& theName )
220 return myMgr->addItem(theLbl, pId, theType, theSection, theName);
223 virtual void setItemProperty( const QString& thePropName, const QVariant& theValue,
224 const int theId = -1) {
225 myMgr->setItemProperty(thePropName, theValue, theId);
228 virtual SUIT_PreferenceMgr* prefMgr() const { return myMgr; }
231 ModuleBase_PreferencesMgr* myMgr;
234 //**********************************************************
235 //**********************************************************
236 //**********************************************************
237 ModuleBase_PreferencesDlg::ModuleBase_PreferencesDlg(SUIT_ResourceMgr* theResurces,
239 : QDialog(theParent),
242 setWindowTitle(tr("Edit preferences"));
244 QVBoxLayout* main = new QVBoxLayout(this);
248 myPreferences = new ModuleBase_PreferencesMgr(theResurces, this);
249 main->addWidget(myPreferences);
251 setFocusProxy(myPreferences);
252 myPreferences->setFrameStyle(QFrame::Box | QFrame::Sunken);
254 QDialogButtonBox* aBtnBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel |
255 QDialogButtonBox::Reset,
256 Qt::Horizontal, this);
257 QPushButton* aDefaultButton = aBtnBox->button(QDialogButtonBox::Reset);
258 aDefaultButton->setText(tr("Default"));
259 connect(aDefaultButton, SIGNAL(clicked()), this, SLOT(onDefault()));
261 main->addWidget(aBtnBox);
262 connect(aBtnBox, SIGNAL(accepted()), this, SLOT(accept()));
263 connect(aBtnBox, SIGNAL(rejected()), this, SLOT(reject()));
266 myPreferences->retrieve();
267 setMinimumSize(800, 240);
270 ModuleBase_PreferencesDlg::~ModuleBase_PreferencesDlg()
274 void ModuleBase_PreferencesDlg::createEditors()
276 int aPage = myPreferences->addItem(tr("Desktop"));
277 myPreferences->setItemIcon(aPage, QIcon(":pictures/view_prefs.png"));
279 createMenuPage(aPage);
280 createViewerPage(aPage);
282 aPage = myPreferences->addItem(tr("Module"));
283 myPreferences->setItemIcon(aPage, QIcon(":pictures/module.png"));
285 ModuleBase_PrefMgr aMgr(myPreferences);
286 ModuleBase_Preferences::createEditContent(&aMgr, aPage);
289 void ModuleBase_PreferencesDlg::createViewerPage(int thePageId)
291 int viewTab = myPreferences->addItem(tr("Viewer"), thePageId);
293 QStringList gradList;
294 gradList << tr("Horizontal gradient") << tr("Vertical gradient") << tr("First diagonal gradient")
295 << tr("Second diagonal gradient") << tr("First corner gradient")
296 << tr("Second corner gradient") << tr("Third corner gradient")
297 << tr("Fourth corner gradient");
299 QList<QVariant> idList;
300 for (int i = 0; i < gradList.size(); i++)
303 int bgGroup = myPreferences->addItem(tr("Background"), viewTab);
305 QString aImgFiles("Image files (*.bmp *.gif *.pix *.xwd *.rgb *.rs)");
307 int bgId = myPreferences->addItem(tr("Viewer 3d"), bgGroup, SUIT_PreferenceMgr::Background,
308 ModuleBase_Preferences::VIEWER_SECTION, "background");
309 myPreferences->setItemProperty("gradient_names", gradList, bgId);
310 myPreferences->setItemProperty("gradient_ids", idList, bgId);
311 myPreferences->setItemProperty("texture_enabled", true, bgId);
312 myPreferences->setItemProperty("texture_center_enabled", true, bgId);
313 myPreferences->setItemProperty("texture_tile_enabled", true, bgId);
314 myPreferences->setItemProperty("texture_stretch_enabled", true, bgId);
315 myPreferences->setItemProperty("custom_enabled", false, bgId);
316 myPreferences->setItemProperty("image_formats", aImgFiles, bgId);
318 // Create other parameters group in viewer tab
319 int selectionGroup = myPreferences->addItem(tr("Default selection"), viewTab);
320 myPreferences->setItemProperty("columns", 3, selectionGroup);
321 myPreferences->addItem(tr("Faces"), selectionGroup,
322 SUIT_PreferenceMgr::Bool,
323 ModuleBase_Preferences::VIEWER_SECTION, "face-selection");
324 myPreferences->addItem(tr("Edges"), selectionGroup,
325 SUIT_PreferenceMgr::Bool,
326 ModuleBase_Preferences::VIEWER_SECTION, "edge-selection");
327 myPreferences->addItem(tr("Vertices"), selectionGroup,
328 SUIT_PreferenceMgr::Bool,
329 ModuleBase_Preferences::VIEWER_SECTION, "vertex-selection");
331 int sensitivityGroup = myPreferences->addItem(tr("Selection sensitivity"), viewTab);
332 myPreferences->setItemProperty("columns", 2, sensitivityGroup);
333 myPreferences->addItem(tr("Vertex"), sensitivityGroup, SUIT_PreferenceMgr::DblSpin,
334 ModuleBase_Preferences::VIEWER_SECTION, "point-selection-sensitivity");
335 myPreferences->addItem(tr("Edge"), sensitivityGroup, SUIT_PreferenceMgr::DblSpin,
336 ModuleBase_Preferences::VIEWER_SECTION, "edge-selection-sensitivity");
339 void ModuleBase_PreferencesDlg::createMenuPage(int thePageId)
341 int aMenuTab = myPreferences->addItem(tr("Main menu"), thePageId);
343 int aSizeGroup = myPreferences->addItem(tr("Size"), aMenuTab);
344 myPreferences->setItemProperty("columns", 1, aSizeGroup);
346 int aRowsNb = myPreferences->addItem(tr("Number of rows"), aSizeGroup,
347 SUIT_PreferenceMgr::IntSpin,
348 ModuleBase_Preferences::MENU_SECTION,
350 myPreferences->setItemProperty("min", 1, aRowsNb);
351 myPreferences->setItemProperty("max", 6, aRowsNb);
353 myPreferences->addItem(tr("Show Status Bar"), aSizeGroup,
354 SUIT_PreferenceMgr::Bool, ModuleBase_Preferences::MENU_SECTION,
358 void ModuleBase_PreferencesDlg::accept()
360 myPreferences->store();
363 // Save custom properties
364 ModuleBase_Preferences::updateConfigByResources();
368 void ModuleBase_PreferencesDlg::modified(ModuleBase_Prefs& theModified) const
370 theModified = myPreferences->modified();
373 void ModuleBase_PreferencesDlg::onDefault()
375 // reset main resources. It throwns all resource manager items to the
376 // initial/default state. If there is no a default state of the item,
377 // it will be filled with an empty value. It concernerned to plugin
378 // config items, like visualization color. The main xml do not contains
379 // default values for them. So, it is important to reset the config
380 // properties after reseting the resources preferences.
381 ModuleBase_Preferences::resetResourcePreferences(myPreferences);
382 // reset plugin's resources. It fills the config resources with the default
383 // values, stores result in the resource manager and retrieve the preferences
384 // items with these values.
385 ModuleBase_Preferences::resetConfigPropPreferences(myPreferences);
388 //**********************************************************
389 //**********************************************************
390 //**********************************************************
391 void ModuleBase_PreferencesMgr::changedResources(const ResourceMap& theMap)
394 ResourceMap::ConstIterator it;
396 for (it = theMap.begin(); it != theMap.end(); ++it) {
397 ModuleBase_Pref aPref;
398 it.key()->resource(aPref.first, aPref.second);
399 myModified.append(aPref);