| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397 | /*=========================================================================  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;  QString  Label;  QSettings* Settings;  ctkSettingsPanel::SettingOptions Options;  QVariant value()const;  bool setValue(const QVariant& value);  QMetaProperty metaProperty();};// --------------------------------------------------------------------------PropertyType::PropertyType()  : Object(0), Settings(0)  , Options(ctkSettingsPanel::OptionNone){}// --------------------------------------------------------------------------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 = true;  if (this->Object->property(this->Property.toLatin1()) != value)    {    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();  /// Return QSettings associated with a given settingKey or the general \a Settings.   /// If \a settingKey is not found, it will return 0.  /// \sa ctkSettingsPanel::registerProperty  QSettings* settings(const QString& settingKey)const;  QSettings*                  Settings;  QMap<QString, PropertyType> Properties;  bool                        SaveToSettingsWhenRegister;};// --------------------------------------------------------------------------ctkSettingsPanelPrivate::ctkSettingsPanelPrivate(ctkSettingsPanel& object)  :q_ptr(&object){  qRegisterMetaType<ctkSettingsPanel::SettingOption>("ctkSettingsPanel::SettingOption");  qRegisterMetaType<ctkSettingsPanel::SettingOptions>("ctkSettingsPanel::SettingOptions");  this->Settings = 0;  this->SaveToSettingsWhenRegister = true;}// --------------------------------------------------------------------------void ctkSettingsPanelPrivate::init(){}// --------------------------------------------------------------------------QSettings* ctkSettingsPanelPrivate::settings(const QString& settingKey)const{  if (!this->Properties.contains(settingKey))    {    return 0;    }  const PropertyType& prop = this->Properties[settingKey];  if (prop.Settings != 0)    {    return prop.Settings;    }  return this->Settings;}// --------------------------------------------------------------------------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);  foreach(const QString& key, d->Properties.keys())    {    QSettings* settings = d->settings(key);    if (!settings)      {      continue;      }    if (settings->contains(key))      {      QVariant value = 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(key))    {    return;    }  this->setSetting(key, d->Properties[key].value());}// --------------------------------------------------------------------------void ctkSettingsPanel::setSetting(const QString& key, const QVariant& newVal){  Q_D(ctkSettingsPanel);  QSettings* settings = d->settings(key);  if (!settings)    {    return;    }  QVariant oldVal = settings->value(key);  settings->setValue(key, newVal);  d->Properties[key].setValue(newVal);  if (settings->status() != QSettings::NoError)    {    logger.warn( QString("Error #%1 while writing setting \"%2\"")      .arg(static_cast<int>(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,                                        const QString& label,                                        ctkSettingsPanel::SettingOptions options,                                        QSettings* settings){  Q_D(ctkSettingsPanel);  PropertyType prop;  prop.Object = object;  prop.Property = property;  prop.DefaultValue = prop.PreviousValue = prop.value();  prop.Label = label;  prop.Options = options;  if (d->Settings != settings)    {    prop.Settings = settings;    }    QSettings* propSettings = settings ? settings : d->Settings;  if (propSettings && propSettings->contains(key))    {    QVariant val = propSettings->value(key);    prop.setValue(val);    prop.PreviousValue = val;    }      d->Properties[key] = prop;  // Create a signal mapper per property to be able to support  // multiple signals from the same sender.  QSignalMapper* signalMapper = new QSignalMapper(this);  QObject::connect(signalMapper, SIGNAL(mapped(QString)),                   this, SLOT(updateSetting(QString)));  signalMapper->setMapping(object, key);  this->connect(object, signal, 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();}// --------------------------------------------------------------------------QStringList ctkSettingsPanel::changedSettings()const{  Q_D(const ctkSettingsPanel);  QStringList settingsKeys;  foreach(const QString& key, d->Properties.keys())    {    const PropertyType& prop = d->Properties[key];    if (prop.PreviousValue != prop.value())      {      settingsKeys << key;      }    }  return settingsKeys;}// --------------------------------------------------------------------------QString ctkSettingsPanel::settingLabel(const QString& settingKey)const{  Q_D(const ctkSettingsPanel);  return d->Properties[settingKey].Label;}// --------------------------------------------------------------------------ctkSettingsPanel::SettingOptions ctkSettingsPanel::settingOptions(const QString& settingKey)const{  Q_D(const ctkSettingsPanel);  return d->Properties[settingKey].Options;}// --------------------------------------------------------------------------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);    }}
 |