123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321 |
- /*=========================================================================
- Library: CTK
- Copyright (c) Kitware Inc.
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0.txt
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- =========================================================================*/
- // Qt includes
- #include <QDebug>
- #include <QMetaProperty>
- #include <QSettings>
- #include <QSignalMapper>
- // CTK includes
- #include "ctkSettingsPanel.h"
- #include "ctkLogger.h"
- static ctkLogger logger("org.commontk.libs.widgets.ctkSettingsPanel");
- namespace
- {
- // --------------------------------------------------------------------------
- struct PropertyType
- {
- PropertyType();
- QObject* Object;
- QString Property;
- QVariant PreviousValue;
- QVariant DefaultValue;
- QVariant value()const;
- bool setValue(const QVariant& value);
- QMetaProperty metaProperty();
- };
- // --------------------------------------------------------------------------
- PropertyType::PropertyType()
- : Object(0)
- {
- }
- // --------------------------------------------------------------------------
- QVariant PropertyType::value()const
- {
- if (this->Object == 0 ||
- this->Property.isEmpty())
- {
- return QVariant();
- }
- return this->Object->property(this->Property.toLatin1());
- }
- // --------------------------------------------------------------------------
- bool PropertyType::setValue(const QVariant& val)
- {
- if (this->Object == 0 || this->Property.isEmpty())
- {
- Q_ASSERT(this->Object && !this->Property.isEmpty());
- return false;
- }
- QVariant value(val);
- // HACK - See http://bugreports.qt.nokia.com/browse/QTBUG-19823
- if (qstrcmp(this->metaProperty().typeName(), "QStringList") == 0 && !value.isValid())
- {
- value = QVariant(QStringList());
- }
- bool success = this->Object->setProperty(this->Property.toLatin1(), value);
- Q_ASSERT(success);
- return success;
- }
- // --------------------------------------------------------------------------
- QMetaProperty PropertyType::metaProperty()
- {
- Q_ASSERT(this->Object);
- for(int i=0; i < this->Object->metaObject()->propertyCount(); ++i)
- {
- this->Object->metaObject()->property(i);
- if (this->Object->metaObject()->property(i).name() == this->Property)
- {
- return this->Object->metaObject()->property(i);
- }
- }
- return QMetaProperty();
- }
- } // end of anonymous namespace
- //-----------------------------------------------------------------------------
- class ctkSettingsPanelPrivate
- {
- Q_DECLARE_PUBLIC(ctkSettingsPanel);
- protected:
- ctkSettingsPanel* const q_ptr;
- public:
- ctkSettingsPanelPrivate(ctkSettingsPanel& object);
- void init();
- QSettings* Settings;
- QMap<QString, PropertyType> Properties;
- QSignalMapper* SignalMapper;
- bool SaveToSettingsWhenRegister;
- };
- // --------------------------------------------------------------------------
- ctkSettingsPanelPrivate::ctkSettingsPanelPrivate(ctkSettingsPanel& object)
- :q_ptr(&object)
- {
- this->Settings = 0;
- this->SignalMapper = 0;
- this->SaveToSettingsWhenRegister = true;
- }
- // --------------------------------------------------------------------------
- void ctkSettingsPanelPrivate::init()
- {
- Q_Q(ctkSettingsPanel);
-
- this->SignalMapper = new QSignalMapper(q);
- QObject::connect(this->SignalMapper, SIGNAL(mapped(QString)),
- q, SLOT(updateSetting(QString)));
- }
- // --------------------------------------------------------------------------
- ctkSettingsPanel::ctkSettingsPanel(QWidget* _parent)
- : Superclass(_parent)
- , d_ptr(new ctkSettingsPanelPrivate(*this))
- {
- Q_D(ctkSettingsPanel);
- d->init();
- }
- // --------------------------------------------------------------------------
- ctkSettingsPanel::~ctkSettingsPanel()
- {
- this->applySettings();
- }
- // --------------------------------------------------------------------------
- QSettings* ctkSettingsPanel::settings()const
- {
- Q_D(const ctkSettingsPanel);
- return d->Settings;
- }
- // --------------------------------------------------------------------------
- void ctkSettingsPanel::setSettings(QSettings* settings)
- {
- Q_D(ctkSettingsPanel);
- if (d->Settings == settings)
- {
- return;
- }
- d->Settings = settings;
- this->updateProperties();
- }
- // --------------------------------------------------------------------------
- void ctkSettingsPanel::updateProperties()
- {
- Q_D(ctkSettingsPanel);
- if (!d->Settings)
- {
- return;
- }
- foreach(const QString& key, d->Properties.keys())
- {
- if (d->Settings->contains(key))
- {
- QVariant value = d->Settings->value(key);
- PropertyType& prop = d->Properties[key];
- // Update object registered using registerProperty()
- prop.setValue(value);
- prop.PreviousValue = value;
- }
- else
- {
- this->updateSetting(key);
- }
- }
- }
- // --------------------------------------------------------------------------
- void ctkSettingsPanel::updateSetting(const QString& key)
- {
- Q_D(ctkSettingsPanel);
- if (!d->Settings)
- {
- return;
- }
- this->setSetting(key, d->Properties[key].value());
- }
- // --------------------------------------------------------------------------
- void ctkSettingsPanel::setSetting(const QString& key, const QVariant& newVal)
- {
- Q_D(ctkSettingsPanel);
- QVariant oldVal = d->Settings->value(key);
- d->Settings->setValue(key, newVal);
- d->Properties[key].setValue(newVal);
- if (d->Settings->status() != QSettings::NoError)
- {
- logger.warn( QString("Error %1 while writing setting %1")
- .arg(static_cast<int>(d->Settings->status()))
- .arg(key));
- }
- if (oldVal != newVal)
- {
- emit settingChanged(key, newVal);
- }
- }
- // --------------------------------------------------------------------------
- void ctkSettingsPanel::registerProperty(const QString& key,
- QObject* object,
- const QString& property,
- const char* signal)
- {
- Q_D(ctkSettingsPanel);
- PropertyType prop;
- prop.Object = object;
- prop.Property = property;
- prop.DefaultValue = prop.PreviousValue = prop.value();
- if (d->Settings && d->Settings->contains(key))
- {
- QVariant val = d->Settings->value(key);
- prop.setValue(val);
- prop.PreviousValue = val;
- }
- d->Properties[key] = prop;
- d->SignalMapper->setMapping(object, key);
- this->connect(object, signal, d->SignalMapper, SLOT(map()));
-
- if (d->SaveToSettingsWhenRegister)
- {
- this->updateSetting(key);
- }
- }
- // --------------------------------------------------------------------------
- QVariant ctkSettingsPanel::defaultPropertyValue(const QString& key) const
- {
- Q_D(const ctkSettingsPanel);
- if (!d->Properties.contains(key))
- {
- return QVariant();
- }
- return d->Properties.value(key).DefaultValue;
- }
- // --------------------------------------------------------------------------
- QVariant ctkSettingsPanel::previousPropertyValue(const QString& key) const
- {
- Q_D(const ctkSettingsPanel);
- if (!d->Properties.contains(key))
- {
- return QVariant();
- }
- return d->Properties.value(key).PreviousValue;
- }
- // --------------------------------------------------------------------------
- QVariant ctkSettingsPanel::propertyValue(const QString& key) const
- {
- Q_D(const ctkSettingsPanel);
- if (!d->Properties.contains(key))
- {
- return QVariant();
- }
- return d->Properties.value(key).value();
- }
- // --------------------------------------------------------------------------
- void ctkSettingsPanel::applySettings()
- {
- Q_D(ctkSettingsPanel);
- foreach(const QString& key, d->Properties.keys())
- {
- PropertyType& prop = d->Properties[key];
- prop.PreviousValue = prop.value();
- }
- }
- // --------------------------------------------------------------------------
- void ctkSettingsPanel::resetSettings()
- {
- Q_D(ctkSettingsPanel);
- foreach(const QString& key, d->Properties.keys())
- {
- this->setSetting(key, d->Properties[key].PreviousValue);
- }
- }
- // --------------------------------------------------------------------------
- void ctkSettingsPanel::restoreDefaultSettings()
- {
- Q_D(ctkSettingsPanel);
- foreach(const QString& key, d->Properties.keys())
- {
- this->setSetting(key, d->Properties[key].DefaultValue);
- }
- }
|