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);
197 * Implementation of preferences manager interface
199 class ModuleBase_PrefMgr: public ModuleBase_IPrefMgr
203 /// \param theMgr a preferences manager
204 ModuleBase_PrefMgr(ModuleBase_PreferencesMgr* theMgr):myMgr(theMgr) {}
206 virtual int addPreference(const QString& theLbl, int pId,
207 SUIT_PreferenceMgr::PrefItemType theType,
208 const QString& theSection, const QString& theName )
210 return myMgr->addItem(theLbl, pId, theType, theSection, theName);
213 virtual void setItemProperty( const QString& thePropName, const QVariant& theValue,
214 const int theId = -1) {
215 myMgr->setItemProperty(thePropName, theValue, theId);
218 virtual SUIT_PreferenceMgr* prefMgr() const { return myMgr; }
221 ModuleBase_PreferencesMgr* myMgr;
224 //**********************************************************
225 //**********************************************************
226 //**********************************************************
227 ModuleBase_PreferencesDlg::ModuleBase_PreferencesDlg(SUIT_ResourceMgr* theResurces,
229 : QDialog(theParent),
232 setWindowTitle(tr("Edit preferences"));
234 QVBoxLayout* main = new QVBoxLayout(this);
238 myPreferences = new ModuleBase_PreferencesMgr(theResurces, this);
239 main->addWidget(myPreferences);
241 setFocusProxy(myPreferences);
242 myPreferences->setFrameStyle(QFrame::Box | QFrame::Sunken);
244 QDialogButtonBox* aBtnBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel |
245 QDialogButtonBox::Reset,
246 Qt::Horizontal, this);
247 QPushButton* aDefaultButton = aBtnBox->button(QDialogButtonBox::Reset);
248 aDefaultButton->setText(tr("Default"));
249 connect(aDefaultButton, SIGNAL(clicked()), this, SLOT(onDefault()));
251 main->addWidget(aBtnBox);
252 connect(aBtnBox, SIGNAL(accepted()), this, SLOT(accept()));
253 connect(aBtnBox, SIGNAL(rejected()), this, SLOT(reject()));
256 myPreferences->retrieve();
257 setMinimumSize(800, 240);
260 ModuleBase_PreferencesDlg::~ModuleBase_PreferencesDlg()
264 void ModuleBase_PreferencesDlg::createEditors()
266 int aPage = myPreferences->addItem(tr("Desktop"));
267 myPreferences->setItemIcon(aPage, QIcon(":pictures/view_prefs.png"));
269 createMenuPage(aPage);
270 createViewerPage(aPage);
272 aPage = myPreferences->addItem(tr("Module"));
273 myPreferences->setItemIcon(aPage, QIcon(":pictures/module.png"));
275 ModuleBase_PrefMgr aMgr(myPreferences);
276 ModuleBase_Preferences::createEditContent(&aMgr, aPage);
279 void ModuleBase_PreferencesDlg::createViewerPage(int thePageId)
281 int viewTab = myPreferences->addItem(tr("Viewer"), thePageId);
283 QStringList gradList;
284 gradList << tr("Horizontal gradient") << tr("Vertical gradient") << tr("First diagonal gradient")
285 << tr("Second diagonal gradient") << tr("First corner gradient")
286 << tr("Second corner gradient") << tr("Third corner gradient")
287 << tr("Fourth corner gradient");
289 QList<QVariant> idList;
290 for (int i = 0; i < gradList.size(); i++)
293 int bgGroup = myPreferences->addItem(tr("Background"), viewTab);
295 QString aImgFiles("Image files (*.bmp *.gif *.pix *.xwd *.rgb *.rs)");
297 int bgId = myPreferences->addItem(tr("Viewer 3d"), bgGroup, SUIT_PreferenceMgr::Background,
298 ModuleBase_Preferences::VIEWER_SECTION, "background");
299 myPreferences->setItemProperty("gradient_names", gradList, bgId);
300 myPreferences->setItemProperty("gradient_ids", idList, bgId);
301 myPreferences->setItemProperty("texture_enabled", true, bgId);
302 myPreferences->setItemProperty("texture_center_enabled", true, bgId);
303 myPreferences->setItemProperty("texture_tile_enabled", true, bgId);
304 myPreferences->setItemProperty("texture_stretch_enabled", true, bgId);
305 myPreferences->setItemProperty("custom_enabled", false, bgId);
306 myPreferences->setItemProperty("image_formats", aImgFiles, bgId);
308 // Create other parameters group in viewer tab
309 int selectionGroup = myPreferences->addItem(tr("Default selection"), viewTab);
310 myPreferences->setItemProperty("columns", 3, selectionGroup);
311 myPreferences->addItem(tr("Faces"), selectionGroup,
312 SUIT_PreferenceMgr::Bool,
313 ModuleBase_Preferences::VIEWER_SECTION, "face-selection");
314 myPreferences->addItem(tr("Edges"), selectionGroup,
315 SUIT_PreferenceMgr::Bool,
316 ModuleBase_Preferences::VIEWER_SECTION, "edge-selection");
317 myPreferences->addItem(tr("Vertices"), selectionGroup,
318 SUIT_PreferenceMgr::Bool,
319 ModuleBase_Preferences::VIEWER_SECTION, "vertex-selection");
321 int sensitivityGroup = myPreferences->addItem(tr("Selection sensitivity"), viewTab);
322 myPreferences->setItemProperty("columns", 2, sensitivityGroup);
323 myPreferences->addItem(tr("Vertex"), sensitivityGroup, SUIT_PreferenceMgr::Double,
324 ModuleBase_Preferences::VIEWER_SECTION, "point-selection-sensitivity");
325 myPreferences->addItem(tr("Edge"), sensitivityGroup, SUIT_PreferenceMgr::Double,
326 ModuleBase_Preferences::VIEWER_SECTION, "edge-selection-sensitivity");
329 void ModuleBase_PreferencesDlg::createMenuPage(int thePageId)
331 int aMenuTab = myPreferences->addItem(tr("Main menu"), thePageId);
333 int aSizeGroup = myPreferences->addItem(tr("Size"), aMenuTab);
334 myPreferences->setItemProperty("columns", 1, aSizeGroup);
336 int aRowsNb = myPreferences->addItem(tr("Number of rows"), aSizeGroup,
337 SUIT_PreferenceMgr::IntSpin,
338 ModuleBase_Preferences::MENU_SECTION,
340 myPreferences->setItemProperty("min", 1, aRowsNb);
341 myPreferences->setItemProperty("max", 6, aRowsNb);
343 myPreferences->addItem(tr("Show Status Bar"), aSizeGroup,
344 SUIT_PreferenceMgr::Bool, ModuleBase_Preferences::MENU_SECTION,
348 void ModuleBase_PreferencesDlg::accept()
350 myPreferences->store();
353 // Save custom properties
354 ModuleBase_Preferences::updateConfigByResources();
358 void ModuleBase_PreferencesDlg::modified(ModuleBase_Prefs& theModified) const
360 theModified = myPreferences->modified();
363 void ModuleBase_PreferencesDlg::onDefault()
365 // reset main resources. It throwns all resource manager items to the
366 // initial/default state. If there is no a default state of the item,
367 // it will be filled with an empty value. It concernerned to plugin
368 // config items, like visualization color. The main xml do not contains
369 // default values for them. So, it is important to reset the config
370 // properties after reseting the resources preferences.
371 ModuleBase_Preferences::resetResourcePreferences(myPreferences);
372 // reset plugin's resources. It fills the config resources with the default
373 // values, stores result in the resource manager and retrieve the preferences
374 // items with these values.
375 ModuleBase_Preferences::resetConfigPropPreferences(myPreferences);
378 //**********************************************************
379 //**********************************************************
380 //**********************************************************
381 void ModuleBase_PreferencesMgr::changedResources(const ResourceMap& theMap)
384 ResourceMap::ConstIterator it;
386 for (it = theMap.begin(); it != theMap.end(); ++it) {
387 ModuleBase_Pref aPref;
388 it.key()->resource(aPref.first, aPref.second);
389 myModified.append(aPref);