Sfoglia il codice sorgente

Merge branch 'popup-in-slider-widget'

* popup-in-slider-widget:
  Add ctkSliderWidget::popupSlider
Julien Finet 13 anni fa
parent
commit
2e9f6c0c0f

+ 3 - 0
Libs/Widgets/Testing/Cpp/ctkSliderWidgetTest2.cpp

@@ -58,6 +58,9 @@ int ctkSliderWidgetTest2(int argc, char * argv [] )
     return EXIT_FAILURE;
     }
 
+  sliderSpinBox.setPopupSlider(true);
+  sliderSpinBox.setPopupSlider(false);
+
   sliderSpinBox.show();
   if (argc < 2 || QString(argv[1]) != "-I" )
     {

+ 50 - 0
Libs/Widgets/ctkSliderWidget.cpp

@@ -23,6 +23,7 @@
 #include <QMouseEvent>
 
 // CTK includes
+#include "ctkPopupWidget.h"
 #include "ctkSliderWidget.h"
 #include "ui_ctkSliderWidget.h"
 
@@ -38,6 +39,8 @@ protected:
 
 public:
   ctkSliderWidgetPrivate(ctkSliderWidget& object);
+  virtual ~ctkSliderWidgetPrivate();
+
   void updateSpinBoxWidth();
   int synchronizedSpinBoxWidth()const;
   void synchronizeSiblingSpinBox(int newWidth);
@@ -50,6 +53,7 @@ public:
   bool   Changing;
   double ValueBeforeChange;
   bool   AutoSpinBoxWidth;
+  ctkPopupWidget* SliderPopup;
 };
 
 // --------------------------------------------------------------------------
@@ -60,6 +64,15 @@ ctkSliderWidgetPrivate::ctkSliderWidgetPrivate(ctkSliderWidget& object)
   this->Changing = false;
   this->ValueBeforeChange = 0.;
   this->AutoSpinBoxWidth = true;
+  this->SliderPopup = 0;
+}
+
+
+// --------------------------------------------------------------------------
+ctkSliderWidgetPrivate::~ctkSliderWidgetPrivate()
+{
+  delete this->SliderPopup;
+  this->SliderPopup = 0;
 }
 
 // --------------------------------------------------------------------------
@@ -496,6 +509,43 @@ void ctkSliderWidget::setSpinBoxVisible(bool visible)
 }
 
 // --------------------------------------------------------------------------
+bool ctkSliderWidget::hasPopupSlider()const
+{
+  Q_D(const ctkSliderWidget);
+  return d->SliderPopup != 0;
+}
+
+// --------------------------------------------------------------------------
+void ctkSliderWidget::setPopupSlider(bool popup)
+{
+  Q_D(ctkSliderWidget);
+  if (this->hasPopupSlider() == popup)
+    {
+    return;
+    }
+  if (popup)
+    {
+    d->SliderPopup = new ctkPopupWidget(0);
+    //d->SliderPopup->setParent(this);
+
+    QHBoxLayout* layout = new QHBoxLayout(d->SliderPopup);
+    layout->setContentsMargins(0,0,0,0);
+    layout->addWidget(d->Slider);
+
+    d->SliderPopup->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
+    d->SliderPopup->setOrientation(Qt::Horizontal);
+    d->SliderPopup->setHorizontalDirection(Qt::RightToLeft);
+    d->SliderPopup->setBaseWidget(d->SpinBox);
+    }
+  else
+    {
+    qobject_cast<QHBoxLayout*>(this->layout())->insertWidget(0,d->Slider);
+    d->SliderPopup->deleteLater();
+    d->SliderPopup = 0;
+    }
+}
+
+// --------------------------------------------------------------------------
 QDoubleSpinBox* ctkSliderWidget::spinBox()
 {
   Q_D(ctkSliderWidget);

+ 15 - 0
Libs/Widgets/ctkSliderWidget.h

@@ -30,6 +30,7 @@ class QDoubleSpinBox;
 
 #include "ctkWidgetsExport.h"
 
+class ctkPopupWidget;
 class ctkSliderWidgetPrivate;
 
 ///
@@ -52,6 +53,7 @@ class CTK_WIDGETS_EXPORT ctkSliderWidget : public QWidget
   Q_PROPERTY(Qt::Alignment spinBoxAlignment READ spinBoxAlignment WRITE setSpinBoxAlignment)
   Q_PROPERTY(bool tracking READ hasTracking WRITE setTracking)
   Q_PROPERTY(bool spinBoxVisible READ isSpinBoxVisible WRITE setSpinBoxVisible);
+  Q_PROPERTY(bool popupSlider READ hasPopupSlider WRITE setPopupSlider);
 
 public:
   /// Superclass typedef
@@ -167,6 +169,19 @@ public:
   bool isSpinBoxVisible()const;
 
   ///
+  /// The slider can be handled as a popup for the spinbox. The location where
+  /// the popup appears is controlled by \sa alignement.
+  /// False by default.
+  bool hasPopupSlider()const;
+  void setPopupSlider(bool popup);
+
+  ///
+  /// Return the popup if ctkSliderWidget hasPopupSlider() is true, 0 otherwise.
+  /// It can be useful to control where the popup shows up relative to the
+  /// spinbox the popup \sa ctkPopupWidget::baseWidget.
+  ctkPopupWidget* popup()const;
+
+  ///
   /// Sometimes it can be useful to have a direct control to the spinbox
   /// To place it somewhere else that ctkDoubleSlider can't. Be careful
   /// with what you do with the spinbox as ctkDoubleSlider might change