Explorar o código

ENH: ctkWidgetAxes: Add setter for AxisLabels

Co-authored-by: Jean-Christophe Fillion-Robin <jchris.fillionr@kitware.com>
Davide Punzo %!s(int64=9) %!d(string=hai) anos
pai
achega
ca05a0fa68

+ 22 - 0
Libs/Widgets/Testing/Cpp/ctkAxesWidgetTest1.cpp

@@ -21,10 +21,12 @@
 // Qt includes
 #include <QApplication>
 #include <QSignalSpy>
+#include <QStringList>
 #include <QTimer>
 
 // CTK includes
 #include "ctkAxesWidget.h"
+#include "ctkCoreTestingMacros.h"
 
 // STD includes
 #include <cstdlib>
@@ -84,6 +86,26 @@ int ctkAxesWidgetTest1(int argc, char * argv [] )
             << spy[0].at(0).toInt() << " " << spy[1].at(0).toInt() << std::endl;
     return EXIT_FAILURE;
     }
+
+
+  // Test axesLabels/setAxesLabels
+  CHECK_BOOL(axes.setAxesLabels(QStringList()), false);
+
+  QStringList emptyAxesLabels =
+      QStringList() << "" << "" << "" << "" << "" << "";
+  CHECK_BOOL(axes.setAxesLabels(emptyAxesLabels), true);
+  CHECK_QSTRINGLIST(axes.axesLabels(), emptyAxesLabels);
+
+  QStringList singleLetterAxesLabels =
+      QStringList() << "W" << "E" << "S" << "N" << "Z" << "z";
+  CHECK_BOOL(axes.setAxesLabels(singleLetterAxesLabels), true);
+  CHECK_QSTRINGLIST(axes.axesLabels(), singleLetterAxesLabels);
+
+  QStringList additionalAxesLabels = singleLetterAxesLabels;
+  additionalAxesLabels.append("X");
+  CHECK_BOOL(axes.setAxesLabels(additionalAxesLabels), true);
+  CHECK_QSTRINGLIST(axes.axesLabels(), singleLetterAxesLabels);
+
   axes.setAutoReset(false);
   axes.setAutoReset(true);
   axes.setWindowTitle("AutoReset=On");

+ 27 - 2
Libs/Widgets/ctkAxesWidget.cpp

@@ -227,8 +227,6 @@ void ctkAxesWidget::paintEvent(QPaintEvent *)
   int length = qMin(this->width(), this->height());
   int diameter = length / goldenRatio;
   int radius = diameter / 2;
-
-  QStringList axesLabels;
   
   QList<QPoint> positions = d->extremities(center, radius);
   
@@ -293,6 +291,33 @@ void ctkAxesWidget::paintEvent(QPaintEvent *)
 }
 
 // ----------------------------------------------------------------------------------
+bool ctkAxesWidget::setAxesLabels(const QStringList& labels)
+{
+  Q_D(ctkAxesWidget);
+  if (labels.size() < 6)
+    {
+    qWarning("ctkAxesWidget::setAxesLabels() failed: At least 6 labels are expected.");
+    return false;
+    }
+
+  if (labels == d->AxesLabels)
+    {
+    return true;
+    }
+
+  d->AxesLabels = labels.mid(0, 6);
+  this->repaint();
+  return true;
+}
+
+// ----------------------------------------------------------------------------------
+QStringList ctkAxesWidget::axesLabels() const
+{
+  Q_D(const ctkAxesWidget);
+  return d->AxesLabels;
+}
+
+// ----------------------------------------------------------------------------------
 void ctkAxesWidget::mousePressEvent(QMouseEvent *mouseEvent)
 {
   Q_D(ctkAxesWidget);

+ 15 - 0
Libs/Widgets/ctkAxesWidget.h

@@ -37,6 +37,7 @@ class CTK_WIDGETS_EXPORT ctkAxesWidget : public QWidget
   Q_ENUMS(Axis)
   Q_PROPERTY(Axis currentAxis READ currentAxis WRITE setCurrentAxis NOTIFY currentAxisChanged)
   Q_PROPERTY(bool autoReset READ autoReset WRITE setAutoReset)
+  Q_PROPERTY(QStringList axesLabels READ axesLabels WRITE setAxesLabels)
 public : 
 
   enum Axis
@@ -81,6 +82,20 @@ public slots :
   /// Set the autoReset property to None anytime the currentAxis is changed.
   void setAutoReset(bool reset);
 
+  /// \brief Set the axes \a labels.
+  ///
+  /// At least 6 labels are required. If more than 6 labels are given, the
+  /// additional strings will be ignored.
+  ///
+  /// Returns True if the given \a labels are either successfully set or if the
+  /// current values match the provided ones.
+  ///
+  /// \sa axesLabels()
+  bool setAxesLabels(const QStringList& labels);
+
+  /// Get the axes labels
+  QStringList axesLabels() const;
+
   /// Size hints
   virtual QSize minimumSizeHint()const;
   virtual QSize sizeHint()const;