Browse Source

invertedAppearance and invertedControls properties for ctkDoubleSlider, ctkDoubleSpinBox and ctkSliderWidget

Miklos Espak 12 years ago
parent
commit
07a5028079

+ 3 - 0
Libs/Widgets/CMakeLists.txt

@@ -67,6 +67,8 @@ set(KIT_SRCS
   ctkDoubleRangeSlider.h
   ctkDoubleSlider.cpp
   ctkDoubleSlider.h
+  ctkDoubleSpinBox.cpp
+  ctkDoubleSpinBox.h
   ctkDynamicSpacer.cpp
   ctkDynamicSpacer.h
   ctkErrorLogStatusMessageHandler.cpp
@@ -218,6 +220,7 @@ set(KIT_MOC_SRCS
   ctkDirectoryButton.h
   ctkDoubleRangeSlider.h
   ctkDoubleSlider.h
+  ctkDoubleSpinBox.h
   ctkDynamicSpacer.h
   ctkErrorLogWidget.h
   ctkErrorLogStatusMessageHandler.h

+ 3 - 3
Libs/Widgets/Resources/UI/ctkSliderWidget.ui

@@ -31,7 +31,7 @@
     </widget>
    </item>
    <item>
-    <widget class="ctkSpinBox" name="SpinBox"/>
+    <widget class="ctkDoubleSpinBox" name="SpinBox"/>
    </item>
   </layout>
  </widget>
@@ -42,9 +42,9 @@
    <header>ctkDoubleSlider.h</header>
   </customwidget>
   <customwidget>
-   <class>ctkSpinBox</class>
+   <class>ctkDoubleSpinBox</class>
    <extends>QWidget</extends>
-   <header>ctkSpinBox.h</header>
+   <header>ctkDoubleSpinBox.h</header>
   </customwidget>
  </customwidgets>
  <resources/>

+ 28 - 0
Libs/Widgets/ctkDoubleSlider.cpp

@@ -356,6 +356,34 @@ void ctkDoubleSlider::setTracking(bool enable)
 }
 
 // --------------------------------------------------------------------------
+bool ctkDoubleSlider::invertedAppearance()const
+{
+  Q_D(const ctkDoubleSlider);
+  return d->Slider->invertedAppearance();
+}
+
+// --------------------------------------------------------------------------
+void ctkDoubleSlider::setInvertedAppearance(bool invertedAppearance)
+{
+  Q_D(ctkDoubleSlider);
+  d->Slider->setInvertedAppearance(invertedAppearance);
+}
+
+// --------------------------------------------------------------------------
+bool ctkDoubleSlider::invertedControls()const
+{
+  Q_D(const ctkDoubleSlider);
+  return d->Slider->invertedControls();
+}
+
+// --------------------------------------------------------------------------
+void ctkDoubleSlider::setInvertedControls(bool invertedControls)
+{
+  Q_D(ctkDoubleSlider);
+  d->Slider->setInvertedControls(invertedControls);
+}
+
+// --------------------------------------------------------------------------
 void ctkDoubleSlider::triggerAction( QAbstractSlider::SliderAction action)
 {
   Q_D(ctkDoubleSlider);

+ 24 - 0
Libs/Widgets/ctkDoubleSlider.h

@@ -54,6 +54,8 @@ class CTK_WIDGETS_EXPORT ctkDoubleSlider : public QWidget
   Q_PROPERTY(bool tracking READ hasTracking WRITE setTracking)
   Q_PROPERTY(Qt::Orientation orientation READ orientation WRITE setOrientation)
   Q_PROPERTY(QString handleToolTip READ handleToolTip WRITE setHandleToolTip)
+  Q_PROPERTY(bool invertedAppearance READ invertedAppearance WRITE setInvertedAppearance)
+  Q_PROPERTY(bool invertedControls READ invertedControls WRITE setInvertedControls)
 
 public:
   /// Superclass typedef
@@ -153,6 +155,28 @@ public:
   /// The orientation must be Qt::Vertical (the default) or Qt::Horizontal.
   Qt::Orientation orientation()const;
 
+  /// 
+  /// This property holds whether or not a slider shows its values inverted.
+  /// If this property is false (the default), the minimum and maximum will
+  /// be shown in its classic position for the inherited widget. If the value
+  /// is true, the minimum and maximum appear at their opposite location.
+  /// Note: This property makes most sense for sliders and dials. For scroll
+  /// bars, the visual effect of the scroll bar subcontrols depends on whether
+  /// or not the styles understand inverted appearance; most styles ignore this
+  /// property for scroll bars.
+  /// \sa invertedControls
+  void setInvertedAppearance(bool invertedAppearance);
+  bool invertedAppearance()const;
+
+  /// This property holds whether or not the slider inverts its wheel and key
+  /// events.
+  /// If this property is false, scrolling the mouse wheel "up" and using keys
+  /// like page up will increase the slider's value towards its maximum.
+  /// Otherwise pressing page up will move value towards the slider's minimum.
+  /// \sa invertedAppearance
+  void setInvertedControls(bool invertedControls);
+  bool invertedControls()const;
+
   ///
   /// Controls the text to display for the handle tooltip. It is in addition
   /// to the widget tooltip.

+ 115 - 0
Libs/Widgets/ctkDoubleSpinBox.cpp

@@ -0,0 +1,115 @@
+/*=========================================================================
+
+  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 <QApplication>
+
+// CTK includes
+#include "ctkDoubleSpinBox.h"
+
+// ----------------------------------------------------------------------------
+class ctkDoubleSpinBoxPrivate
+{
+public:
+  ctkDoubleSpinBoxPrivate();
+
+  bool InvertedControls;
+};
+
+// ----------------------------------------------------------------------------
+//  Methods ctkDoubleSpinBoxPrivate
+
+// ----------------------------------------------------------------------------
+ctkDoubleSpinBoxPrivate::ctkDoubleSpinBoxPrivate()
+{
+  this->InvertedControls = false;
+}
+
+// ----------------------------------------------------------------------------
+//  Methods ctkDoubleSpinBox
+
+// ----------------------------------------------------------------------------
+ctkDoubleSpinBox::ctkDoubleSpinBox(QWidget *_parent)
+  :Superclass(_parent)
+  , d_ptr(new ctkDoubleSpinBoxPrivate())
+{
+}
+
+// ----------------------------------------------------------------------------
+ctkDoubleSpinBox::~ctkDoubleSpinBox()
+{
+}
+
+// ----------------------------------------------------------------------------
+void ctkDoubleSpinBox::setInvertedControls(bool invertedControls)
+{
+  Q_D(ctkDoubleSpinBox);
+  d->InvertedControls = invertedControls;
+}
+
+// ----------------------------------------------------------------------------
+bool ctkDoubleSpinBox::invertedControls() const
+{
+  Q_D(const ctkDoubleSpinBox);
+  return d->InvertedControls;
+}
+
+// ----------------------------------------------------------------------------
+void ctkDoubleSpinBox::stepBy(int steps)
+{
+  Q_D(const ctkDoubleSpinBox);
+  if (d->InvertedControls)
+    {
+    steps = -steps;
+    }
+  Superclass::stepBy(steps);
+}
+
+// ----------------------------------------------------------------------------
+QAbstractSpinBox::StepEnabled ctkDoubleSpinBox::stepEnabled() const
+{
+  Q_D(const ctkDoubleSpinBox);
+  
+  if (!d->InvertedControls)
+    {
+    return Superclass::stepEnabled();
+    }
+
+  if (this->isReadOnly())
+    {
+    return StepNone;
+    }
+
+  if (this->wrapping())
+    {
+    return StepEnabled(StepUpEnabled | StepDownEnabled);
+    }
+    
+  StepEnabled ret = StepNone;
+  double value = this->value();
+  if (value < this->maximum())
+    {
+    ret |= StepDownEnabled;
+    }
+  if (value > this->minimum())
+    {
+    ret |= StepUpEnabled;
+    }
+  return ret;
+}

+ 75 - 0
Libs/Widgets/ctkDoubleSpinBox.h

@@ -0,0 +1,75 @@
+/*=========================================================================
+
+  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.
+
+=========================================================================*/
+
+#ifndef __ctkDoubleSpinBox_h
+#define __ctkDoubleSpinBox_h
+
+// QT includes
+#include <QDoubleSpinBox>
+
+// CTK includes
+#include "ctkWidgetsExport.h"
+
+class ctkDoubleSpinBoxPrivate;
+
+/// \ingroup Widgets
+/// ctkDoubleSpinBox is an advanced QDoubleSpinBox with the invertedControls
+/// property. If the property is set, the decreasing the value by the mouse
+/// button or mouse wheel increase the value of the widget, and inverts the
+/// control similarly in the other way round. The property is switched off by
+/// default.
+class CTK_WIDGETS_EXPORT ctkDoubleSpinBox : public QDoubleSpinBox
+{
+  Q_OBJECT
+  Q_PROPERTY(bool invertedControls READ invertedControls WRITE setInvertedControls)
+
+public:
+  typedef QDoubleSpinBox Superclass;
+
+  ctkDoubleSpinBox(QWidget *_parent = 0);
+  virtual ~ctkDoubleSpinBox();
+
+  /// This property holds whether or not the spin box inverts its wheel and key
+  /// events.
+  /// If this property is false, scrolling the mouse wheel "up" and using keys
+  /// like page up will increase the spinbox's value towards its maximum.
+  /// Otherwise pressing page up will move value towards the slider's minimum.
+  void setInvertedControls(bool invertedControls);
+  bool invertedControls() const;
+
+  /// Overrides QDoubleSpinBox::stepBy(int) and negates the step number if the
+  /// invertedControls property is true.
+  virtual void stepBy(int steps);
+
+protected:
+  /// If the invertedControls property is false (by default) then this function
+  /// behavesLike QDoubleSpinBox::stepEnabled(). If the property is true then
+  /// stepping down is allowed if the value is less then the maximum, and
+  /// stepping up is allowed if the value is greater then the minimum.
+  virtual StepEnabled stepEnabled () const;
+
+  QScopedPointer<ctkDoubleSpinBoxPrivate> d_ptr;
+
+private:
+  Q_DECLARE_PRIVATE(ctkDoubleSpinBox);
+  Q_DISABLE_COPY(ctkDoubleSpinBox);
+};
+
+#endif

+ 30 - 1
Libs/Widgets/ctkSliderWidget.cpp

@@ -517,6 +517,35 @@ bool ctkSliderWidget::hasTracking()const
   return d->Tracking;
 }
 
+// --------------------------------------------------------------------------
+bool ctkSliderWidget::invertedAppearance()const
+{
+  Q_D(const ctkSliderWidget);
+  return d->Slider->invertedAppearance();
+}
+
+// --------------------------------------------------------------------------
+void ctkSliderWidget::setInvertedAppearance(bool invertedAppearance)
+{
+  Q_D(ctkSliderWidget);
+  d->Slider->setInvertedAppearance(invertedAppearance);
+}
+
+// --------------------------------------------------------------------------
+bool ctkSliderWidget::invertedControls()const
+{
+  Q_D(const ctkSliderWidget);
+  return d->Slider->invertedControls() && d->SpinBox->invertedControls();
+}
+
+// --------------------------------------------------------------------------
+void ctkSliderWidget::setInvertedControls(bool invertedControls)
+{
+  Q_D(ctkSliderWidget);
+  d->Slider->setInvertedControls(invertedControls);
+  d->SpinBox->setInvertedControls(invertedControls);
+}
+
 // -------------------------------------------------------------------------
 ctkSliderWidget::SynchronizeSiblings
 ctkSliderWidget::synchronizeSiblings() const
@@ -595,7 +624,7 @@ ctkPopupWidget* ctkSliderWidget::popup()const
 }
 
 // --------------------------------------------------------------------------
-ctkSpinBox* ctkSliderWidget::spinBox()
+ctkDoubleSpinBox* ctkSliderWidget::spinBox()
 {
   Q_D(ctkSliderWidget);
   return d->SpinBox;

+ 29 - 5
Libs/Widgets/ctkSliderWidget.h

@@ -32,13 +32,13 @@
 class ctkDoubleSlider;
 class ctkPopupWidget;
 class ctkSliderWidgetPrivate;
-class ctkSpinBox;
+class ctkDoubleSpinBox;
 
 /// \ingroup Widgets
 ///
-/// ctkSliderWidget is a wrapper around a ctkDoubleSlider and a QDoubleSpinBox
+/// ctkSliderWidget is a wrapper around a ctkDoubleSlider and a ctkDoubleSpinBox
 /// where the slider value and the spinbox value are synchronized.
-/// \sa ctkRangeWidget, ctkDoubleRangeSlider, QSpinBox
+/// \sa ctkRangeWidget, ctkDoubleRangeSlider, QDoubleSpinBox
 class CTK_WIDGETS_EXPORT ctkSliderWidget : public QWidget
 {
   Q_OBJECT
@@ -58,6 +58,8 @@ class CTK_WIDGETS_EXPORT ctkSliderWidget : public QWidget
   Q_PROPERTY(bool tracking READ hasTracking WRITE setTracking)
   Q_PROPERTY(bool spinBoxVisible READ isSpinBoxVisible WRITE setSpinBoxVisible);
   Q_PROPERTY(bool popupSlider READ hasPopupSlider WRITE setPopupSlider);
+  Q_PROPERTY(bool invertedAppearance READ invertedAppearance WRITE setInvertedAppearance)
+  Q_PROPERTY(bool invertedControls READ invertedControls WRITE setInvertedControls)
 
 public:
 
@@ -66,7 +68,7 @@ public:
   /// The slider widget siblings aren't updated and this widget does not update
   /// from its siblings.
   /// SynchronizeWidth:
-  /// The width of the SpinBox is set to the same width of the largest QSpinBox
+  /// The width of the SpinBox is set to the same width of the largest QDoubleSpinBox
   /// of its ctkSliderWidget siblings.
   /// SynchronizeDecimals:
   /// Whenever one of the siblings changes its number of decimals, all its
@@ -194,6 +196,28 @@ public:
   ctkSliderWidget::SynchronizeSiblings synchronizeSiblings() const;
   void setSynchronizeSiblings(ctkSliderWidget::SynchronizeSiblings options);
 
+  /// This property holds whether or not a slider shows its values inverted.
+  /// If this property is false (the default), the minimum and maximum will
+  /// be shown in its classic position for the inherited widget. If the value
+  /// is true, the minimum and maximum appear at their opposite location.
+  /// Note: This property makes most sense for sliders and dials. For scroll
+  /// bars, the visual effect of the scroll bar subcontrols depends on whether
+  /// or not the styles understand inverted appearance; most styles ignore this
+  /// property for scroll bars.
+  /// \sa invertedControls
+  void setInvertedAppearance(bool invertedAppearance);
+  bool invertedAppearance()const;
+
+  /// This property holds whether or not the slider and the spinbox invert
+  /// their wheel and key events.
+  /// If this property is false, scrolling the mouse wheel "up" and using keys
+  /// like page up will increase the value of the slider widget towards its
+  /// maximum. Otherwise, pressing page up will move value towards the minimum.
+  /// The default value of the property is false.
+  /// \sa invertedAppearance
+  void setInvertedControls(bool invertedControls);
+  bool invertedControls()const;
+
   ///
   /// The Spinbox visibility can be controlled using setSpinBoxVisible() and
   /// isSpinBoxVisible().
@@ -219,7 +243,7 @@ public:
   /// Returns the spinbox synchronized with the slider. Be careful
   /// with what you do with the spinbox as the slider might change
   /// properties automatically.
-  ctkSpinBox* spinBox();
+  ctkDoubleSpinBox* spinBox();
 
   ///
   /// Returns the slider synchronized with the spinbox. Be careful