浏览代码

Merge pull request #743 from cpinter/settings-related-improvements

Settings related improvements
Jean-Christophe Fillion-Robin 7 年之前
父节点
当前提交
2e899b6385

+ 2 - 3
Libs/Core/ctkBooleanMapper.cpp

@@ -45,7 +45,7 @@ ctkBooleanMapperPrivate::ctkBooleanMapperPrivate()
 
 // --------------------------------------------------------------------------
 ctkBooleanMapper::ctkBooleanMapper(
-  QObject* targetObject, const QByteArray& property, const char* signal)
+  QObject* targetObject, const QByteArray& property, const QByteArray& signal)
   : QObject(targetObject)
   , d_ptr(new ctkBooleanMapperPrivate)
 {
@@ -53,7 +53,7 @@ ctkBooleanMapper::ctkBooleanMapper(
   Q_ASSERT(targetObject != 0);
   Q_D(ctkBooleanMapper);
   d->PropertyName = property;
-  if (signal)
+  if (!signal.isEmpty())
     {
     connect(targetObject, signal, this, SLOT(emitValueChanged()));
     }
@@ -187,4 +187,3 @@ void ctkBooleanMapper::emitValueAsChanged()
   emit valueAsIntChanged(this->valueAsInt());
   emit valueAsStringChanged(this->valueAsString());
 }
-

+ 18 - 8
Libs/Core/ctkBooleanMapper.h

@@ -31,13 +31,23 @@ class ctkBooleanMapperPrivate;
 
 //---------------------------------------------------------------------------
 /// \ingroup Core
-/// QCheckBox* checkBox = new QCheckBox;
-/// ctkBooleanMapper* inverter =
-///   new ctkBooleanMapper("checked", SIGNAL("toggled(bool)"), checkBox);
-/// inverter->setComplementValue(true);
-/// // -> checkBox->checked() == false
-/// inverter->setValue(false);
-/// // -> checkBox->checked() == false
+///
+/// Example:
+///   QCheckBox* checkBox = new QCheckBox;
+///   ctkBooleanMapper* inverter =
+///     new ctkBooleanMapper("checked", SIGNAL("toggled(bool)"), checkBox);
+///   inverter->setComplementValue(true);
+///   // -> checkBox->checked() == false
+///   inverter->setValue(false);
+///   // -> checkBox->checked() == false
+///
+/// Python example:
+///   boolMapper = ctk.ctkBooleanMapper(checkBox, "checked", "toggled(bool)")
+///   boolMapper.trueValue = qt.QMessageBox.Yes
+///   boolMapper.falseValue = qt.QMessageBox.InvalidRole
+///   parent.registerProperty(
+///     "settingsPropertyName", boolMapper, "valueAsInt", qt.SIGNAL("valueAsIntChanged(int)"))
+///
 class CTK_CORE_EXPORT ctkBooleanMapper : public QObject
 {
   Q_OBJECT
@@ -66,7 +76,7 @@ public:
   /// \a object is destructed.
   /// property and object must be valid and non empty. If signal is 0,
   /// \a valueChanged(bool) and \a complementChanged(bool) won't be fired.
-  ctkBooleanMapper(QObject* targetObject, const QByteArray& propertyName, const char* signal);
+  ctkBooleanMapper(QObject* targetObject, const QByteArray& propertyName, const QByteArray& signal);
   virtual ~ctkBooleanMapper();
 
   QByteArray propertyName()const;

+ 11 - 0
Libs/Core/ctkCorePythonQtDecorators.h

@@ -25,6 +25,7 @@
 #include <PythonQt.h>
 
 // CTK includes
+#include <ctkBooleanMapper.h>
 #include <ctkErrorLogContext.h>
 #include <ctkWorkflowStep.h>
 #include <ctkWorkflowTransitions.h>
@@ -44,6 +45,7 @@ public:
 
   ctkCorePythonQtDecorators()
     {
+    PythonQt::self()->registerClass(&ctkBooleanMapper::staticMetaObject, "CTKCore");
     PythonQt::self()->registerCPPClass("ctkErrorLogContext", 0, "CTKCore");
     PythonQt::self()->registerCPPClass("ctkWorkflowStep", 0, "CTKCore");
     PythonQt::self()->registerClass(&ctkWorkflowInterstepTransition::staticMetaObject, "CTKCore");
@@ -52,6 +54,15 @@ public:
 public Q_SLOTS:
 
   //
+  // ctkBooleanMapper
+  //
+
+  ctkBooleanMapper* new_ctkBooleanMapper(QObject* targetObject, const QByteArray& propertyName, const QByteArray& signal)
+    {
+    return new ctkBooleanMapper(targetObject, propertyName, signal);
+    }
+
+  //
   // ctkWorkflowStep
   //
 

+ 22 - 0
Libs/Widgets/ctkComboBox.cpp

@@ -366,3 +366,25 @@ void ctkComboBox::wheelEvent(QWheelEvent* event)
     event->ignore();
     }
 }
+
+// -------------------------------------------------------------------------
+QString ctkComboBox::currentUserDataAsString()const
+{
+  return this->itemData(this->currentIndex()).toString();
+}
+
+// -------------------------------------------------------------------------
+void ctkComboBox::setCurrentUserDataAsString(QString userData)
+{
+  for (int index=0; index<this->count(); ++index)
+    {
+    QString currentItemUserData = this->itemData(index).toString();
+    if (!userData.compare(currentItemUserData))
+      {
+      this->setCurrentIndex(index);
+      return;
+      }
+    }
+
+  qWarning() << Q_FUNC_INFO << ": No item found with user data string " << userData;
+}

+ 11 - 2
Libs/Widgets/ctkComboBox.h

@@ -52,10 +52,12 @@ class CTK_WIDGETS_EXPORT ctkComboBox : public QComboBox
   /// ScrollOn by default.
   /// /sa scrollWheelEffect, setScrollWheelEffect
   Q_PROPERTY(ScrollEffect scrollWheelEffect READ scrollWheelEffect WRITE setScrollWheelEffect)
+  /// Current item's user data as string (Qt::UserRole role)
+  Q_PROPERTY(QString currentUserDataAsString READ currentUserDataAsString WRITE setCurrentUserDataAsString)
 
   Q_ENUMS(ScrollEffect);
 public:
-  /// Constructor, build a ctkComboBox that behave like QComboBox.
+  /// Constructor, build a ctkComboBox that behaves like QComboBox.
   explicit ctkComboBox(QWidget* parent = 0);
   virtual ~ctkComboBox();
 
@@ -87,7 +89,7 @@ public:
     /// Scrolling is only possible if the combobox has the focus.
     /// The focus policy is automatically set to Qt::StrongFocus
     ScrollWithFocus,
-    /// Scrolling is not possible when the combobox is inside a scrollarea with
+    /// Scrolling is not possible when the combobox is inside a scroll area with
     /// a visible vertical scrollbar.
     ScrollWithNoVScrollBar
   };
@@ -103,6 +105,13 @@ public:
   /// Reimplemented for internal reasons
   virtual QSize sizeHint()const;
 
+  /// Get current item's user data as string
+  QString currentUserDataAsString()const;
+
+public slots:
+  /// Set current item based on user data
+  void setCurrentUserDataAsString(QString userData);
+
 protected:
   /// Reimplemented for internal reasons
   virtual void paintEvent(QPaintEvent* event);