diff --git a/Modules/QtWidgets/include/QmitkSliceNavigationWidget.h b/Modules/QtWidgets/include/QmitkSliceNavigationWidget.h index 558f504354..c924fbdf01 100644 --- a/Modules/QtWidgets/include/QmitkSliceNavigationWidget.h +++ b/Modules/QtWidgets/include/QmitkSliceNavigationWidget.h @@ -1,122 +1,151 @@ /*============================================================================ The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center (DKFZ) All rights reserved. Use of this source code is governed by a 3-clause BSD license that can be found in the LICENSE file. ============================================================================*/ #ifndef QMITKSLICENAVIGATIONWIDGET_H #define QMITKSLICENAVIGATIONWIDGET_H #include "MitkQtWidgetsExports.h" #include "ui_QmitkSliceNavigationWidget.h" #include #include #include class MITKQTWIDGETS_EXPORT QmitkSliceNavigationWidget : public QWidget, public Ui::QmitkSliceNavigationWidget { Q_OBJECT public: - QmitkSliceNavigationWidget(QWidget *parent = nullptr, Qt::WindowFlags f = nullptr); - QString GetLabelUnit(); + QmitkSliceNavigationWidget(QWidget* parent = nullptr, Qt::WindowFlags f = nullptr); /** - * \brief Converts the passed value to a QString representation. + * \brief Convert the passed value to a QString representation. * * If the value exceeds a certain maximum, "INF" (for "infinity") is displayed * instead. */ QString ClippedValueToString(float value); - /** - * \brief Returns range-minimum (displayed as label left of slider if enabled) - */ + QString GetLabelUnit(); + QString GetMinValueLabel(); QString GetMaxValueLabel(); int GetPos(); bool GetInverseDirection() const; bool GetInvertedControls() const; public slots: /** - * \brief Updates the slider with the recent changes applied to the navigation widget. + * \brief Update the slider with the recent changes applied to the navigation widget. * * Intended to be called via event mechanism, e.g. if the connected * mitk::Stepper is modified. */ void Refetch(); - void SetStepper(mitk::Stepper *stepper); + /** + * \brief Set the stepper that should be represented and modified. + * + */ + void SetStepper(mitk::Stepper* stepper); + /** + * \brief Enable / disable displaying of the minimum and maximum labels + */ void ShowLabels(bool show); /** - * \brief En-/disables displaying of the unit label (range will be displayed + * \brief Enable / disable displaying of the unit label (range will be displayed * without unit if enabled). */ void ShowLabelUnit(bool show); void SetPos(int val); void SetInverseDirection(bool inverseDirection); void SetInvertedControls(bool invertedControls); protected slots: - void slider_valueChanged(double); + /** + * \brief React on changes of the slider. + * + * The position of the stepper (class member) is set according to the + * current slider value. + * This will also update the value labels. + */ + void SliderChanged(double); + + /** + * \brief React on changes of the spinbox. + * + * The position of the stepper (class member) is set according to the + * current spinbox value. + * This will also update the value labels. + */ + void SpinBoxChanged(double); /** - * \brief Set range minimum and maximum (displayed as labels left and right - * of slider if enabled) + * \brief Set label values for the range minimum and maximum. + * + * Displayed as labels to the left and the right of the slider, if enabled. */ void SetLabelValues(float min, float max); + /** + * \brief Enable / disable labels for the range minimum or maximum. + * + * Displayed as labels to the left and the right of the slider, if enabled. + */ void SetLabelValuesValid(bool minValid, bool maxValid); /** - * \brief Set range unit (e.g. mm or ms) which will be displayed below range - * labels if enabled. + * \brief Set the range unit (e.g. mm or ms). + * + * Displayed below the range labels, if enabled. */ - void SetLabelUnit(const char *unit); + void SetLabelUnit(const char* unit); /** - * \brief Configure slider with labels according to range and unit settings + * \brief Configure slider with labels according to range and unit settings. */ void SetLabels(); - void spinBox_valueChanged(double); +protected: + + mitk::Stepper::Pointer m_Stepper; + bool m_InRefetch; + QString m_LabelUnit; -protected: bool m_HasLabelUnit; bool m_MaxValueValid; bool m_MinValueValid; - QString m_LabelUnit; - mitk::Stepper::Pointer m_Stepper; - bool m_InRefetch; + bool m_HasLabels; float m_MinValue; float m_MaxValue; bool m_InverseDirection; bool m_InvertedControls; }; #endif // QMITKSLICENAVIGATIONWIDGET_H diff --git a/Modules/QtWidgets/src/QmitkSliceNavigationWidget.cpp b/Modules/QtWidgets/src/QmitkSliceNavigationWidget.cpp index 55cc102d91..73f4f106a4 100644 --- a/Modules/QtWidgets/src/QmitkSliceNavigationWidget.cpp +++ b/Modules/QtWidgets/src/QmitkSliceNavigationWidget.cpp @@ -1,307 +1,317 @@ /*============================================================================ The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center (DKFZ) All rights reserved. Use of this source code is governed by a 3-clause BSD license that can be found in the LICENSE file. ============================================================================*/ #include "QmitkSliceNavigationWidget.h" -QmitkSliceNavigationWidget::QmitkSliceNavigationWidget(QWidget *parent, Qt::WindowFlags f) : QWidget(parent, f) +QmitkSliceNavigationWidget::QmitkSliceNavigationWidget(QWidget* parent, Qt::WindowFlags f) + : QWidget(parent, f) { this->setupUi(this); m_Slider->setOrientation(Qt::Horizontal); m_Slider->setMinimum(0); m_Slider->setMaximum(0); m_Slider->setValue(0); m_Slider->setSingleStep(1); m_Slider->setPageStep(10); m_SpinBox->setMinimum( 0 ); m_SpinBox->setMaximum(0); m_SpinBox->setValue(0); m_SpinBox->setDecimals(0); m_SpinBox->setSingleStep(1); - this->connect(m_Slider, SIGNAL(valueChanged(double)), SLOT(slider_valueChanged(double))); - this->connect(m_SpinBox, SIGNAL(valueChanged(double)), SLOT(spinBox_valueChanged(double))); + this->connect(m_Slider, SIGNAL(valueChanged(double)), SLOT(SliderChanged(double))); + this->connect(m_SpinBox, SIGNAL(valueChanged(double)), SLOT(SpinBoxChanged(double))); // this avoids trying to use m_Stepper until it is set to something != nullptr // (additionally to the avoiding recursions during refetching) m_InRefetch = true; // Hide slider labeling -- until it is explicitly activated this->ShowLabels(false); // Set label values as invalid (N/A) this->SetLabelValuesValid(false, false); m_HasLabels = false; m_HasLabelUnit = true; m_InverseDirection = false; m_InvertedControls = false; } +QString QmitkSliceNavigationWidget::ClippedValueToString(float value) +{ + if (value < -10000000.0) + { + return "-INF"; + } + else if (value > 10000000.0) + { + return "+INF"; + } + else + { + return QString::number(value, 'f', 2); + } +} + +QString QmitkSliceNavigationWidget::GetLabelUnit() +{ + return m_LabelUnit; +} + +QString QmitkSliceNavigationWidget::GetMinValueLabel() +{ + if (m_MinValueValid) + { + return this->ClippedValueToString(m_MinValue); + } + else + { + return "N/A"; + } +} + +QString QmitkSliceNavigationWidget::GetMaxValueLabel() +{ + if (m_MaxValueValid) + { + return this->ClippedValueToString(m_MaxValue); + } + else + { + return "N/A"; + } +} + +int QmitkSliceNavigationWidget::GetPos() +{ + return m_Stepper->GetPos(); +} + +bool QmitkSliceNavigationWidget::GetInverseDirection() const +{ + return m_InverseDirection; +} + +bool QmitkSliceNavigationWidget::GetInvertedControls() const +{ + return m_InvertedControls; +} + void QmitkSliceNavigationWidget::Refetch() { if (!m_InRefetch) { m_InRefetch = true; if (m_Stepper->GetSteps() == 0) { m_Slider->setMaximum(0); m_Slider->setValue(0); m_SpinBox->setMaximum(0); m_SpinBox->setValue(0); } else { - m_Slider->setMaximum(m_Stepper->GetSteps() - 1); + unsigned int maximumSteps = m_Stepper->GetSteps() - 1; + unsigned int currentPosition = m_Stepper->GetPos(); + + // set slider value + m_Slider->setMaximum(maximumSteps); if (m_InverseDirection) { - m_Slider->setValue(m_Stepper->GetSteps() - 1 - m_Stepper->GetPos()); + m_Slider->setValue(maximumSteps - currentPosition); } else { - m_Slider->setValue(m_Stepper->GetPos()); + m_Slider->setValue(currentPosition); } - m_SpinBox->setMaximum(m_Stepper->GetSteps() - 1); + // set spinbox values + m_SpinBox->setMaximum(maximumSteps); if (m_InverseDirection) { - m_SpinBox->setValue(m_Stepper->GetSteps() - 1 - m_Stepper->GetPos()); + m_SpinBox->setValue(maximumSteps - currentPosition); } else { - m_SpinBox->setValue(m_Stepper->GetPos()); + m_SpinBox->setValue(currentPosition); } } if (m_Stepper->HasRange() && m_HasLabels) { - // Show slider with labels according to below settings + // Show slider with labels according to settings below m_SliderLabelLeft->setHidden(false); m_SliderLabelRight->setHidden(false); if (m_Stepper->HasValidRange()) { this->SetLabelValuesValid(true, true); this->SetLabelValues(m_Stepper->GetRangeMin(), m_Stepper->GetRangeMax()); } else { this->SetLabelValuesValid(false, false); } if (m_Stepper->HasUnitName()) { this->SetLabelUnit(m_Stepper->GetUnitName()); } } else { // Show slider without any labels m_SliderLabelLeft->setHidden(true); m_SliderLabelRight->setHidden(true); } - // Update GUI according to above settings + // Update GUI according to settings above this->SetLabels(); m_InRefetch = false; } } -void QmitkSliceNavigationWidget::SetStepper(mitk::Stepper *stepper) +void QmitkSliceNavigationWidget::SetStepper(mitk::Stepper* stepper) { m_Stepper = stepper; // this avoids trying to use m_Stepper until it is set to something != nullptr // (additionally to the avoiding recursions during refetching) m_InRefetch = (stepper == nullptr); } - -void QmitkSliceNavigationWidget::slider_valueChanged(double) -{ - if (!m_InRefetch) - { - if (m_InverseDirection) - { - m_Stepper->SetPos(m_Stepper->GetSteps() - 1 - m_Slider->value()); - } - else - { - m_Stepper->SetPos(m_Slider->value()); - } - this->Refetch(); - } -} - void QmitkSliceNavigationWidget::ShowLabels(bool show) { m_HasLabels = show; } void QmitkSliceNavigationWidget::ShowLabelUnit(bool show) { m_HasLabelUnit = show; } -void QmitkSliceNavigationWidget::SetLabelValues(float min, float max) -{ - m_MinValue = min; - m_MaxValue = max; -} - -void QmitkSliceNavigationWidget::SetLabelValuesValid(bool minValid, bool maxValid) +void QmitkSliceNavigationWidget::SetPos(int val) { - m_MinValueValid = minValid; - m_MaxValueValid = maxValid; + if (!m_InRefetch) + { + m_Stepper->SetPos(val); + } } -void QmitkSliceNavigationWidget::SetLabelUnit(const char *unit) +void QmitkSliceNavigationWidget::SetInverseDirection(bool inverseDirection) { - m_LabelUnit = unit; + if (inverseDirection != m_InverseDirection) + { + m_InverseDirection = inverseDirection; + this->Refetch(); + } } -QString QmitkSliceNavigationWidget::GetLabelUnit() +void QmitkSliceNavigationWidget::SetInvertedControls(bool invertedControls) { - return m_LabelUnit; + if (invertedControls != m_InvertedControls) + { + m_InvertedControls = invertedControls; + m_Slider->setInvertedAppearance(invertedControls); + m_Slider->setInvertedControls(invertedControls); + m_SpinBox->setInvertedControls(invertedControls); + } } -QString QmitkSliceNavigationWidget::ClippedValueToString(float value) +void QmitkSliceNavigationWidget::SliderChanged(double) { - if (value < -10000000.0) + if (m_InRefetch) { - return "-INF"; + return; } - else if (value > 10000000.0) + + if (m_InverseDirection) { - return "+INF"; + m_Stepper->SetPos(m_Stepper->GetSteps() - 1 - m_Slider->value()); } else { - return QString::number(value, 'f', 2); + m_Stepper->SetPos(m_Slider->value()); } + + this->Refetch(); } -QString QmitkSliceNavigationWidget::GetMinValueLabel() +void QmitkSliceNavigationWidget::SpinBoxChanged(double) { - if (m_MinValueValid) - { - return this->ClippedValueToString(m_MinValue); - } - else + if (m_InRefetch) { - return "N/A"; + return; } -} -QString QmitkSliceNavigationWidget::GetMaxValueLabel() -{ - if (m_MaxValueValid) + if (m_InverseDirection) { - return this->ClippedValueToString(m_MaxValue); + m_Stepper->SetPos(m_Stepper->GetSteps() - 1 - m_SpinBox->value()); } else { - return "N/A"; + m_Stepper->SetPos(m_SpinBox->value()); } + + this->Refetch(); +} + +void QmitkSliceNavigationWidget::SetLabelValues(float min, float max) +{ + m_MinValue = min; + m_MaxValue = max; +} + +void QmitkSliceNavigationWidget::SetLabelValuesValid(bool minValid, bool maxValid) +{ + m_MinValueValid = minValid; + m_MaxValueValid = maxValid; +} + +void QmitkSliceNavigationWidget::SetLabelUnit(const char* unit) +{ + m_LabelUnit = unit; } void QmitkSliceNavigationWidget::SetLabels() { QString minText = "

" + this->GetMinValueLabel(); QString maxText = "

" + this->GetMaxValueLabel(); if (m_HasLabelUnit) { minText += " " + this->GetLabelUnit(); maxText += " " + this->GetLabelUnit(); } if (m_MinValueValid) { minText += "
(pos 0)"; } if (m_MaxValueValid) { maxText += "
(pos " + QString::number(m_Stepper->GetSteps() - 1) + ")"; } minText += "

"; maxText += "

"; m_SliderLabelLeft->setText(minText); m_SliderLabelRight->setText(maxText); } - -void QmitkSliceNavigationWidget::spinBox_valueChanged(double) -{ - if (!m_InRefetch) - { - if (m_InverseDirection) - { - m_Stepper->SetPos(m_Stepper->GetSteps() - 1 - m_SpinBox->value()); - } - else - { - m_Stepper->SetPos(m_SpinBox->value()); - } - this->Refetch(); - } -} - -int QmitkSliceNavigationWidget::GetPos() -{ - return m_Stepper->GetPos(); -} - -void QmitkSliceNavigationWidget::SetPos(int val) -{ - if (!m_InRefetch) - { - m_Stepper->SetPos(val); - } -} - -bool QmitkSliceNavigationWidget::GetInverseDirection() const -{ - return m_InverseDirection; -} - -void QmitkSliceNavigationWidget::SetInverseDirection(bool inverseDirection) -{ - if (inverseDirection != m_InverseDirection) - { - m_InverseDirection = inverseDirection; - this->Refetch(); - } -} - -bool QmitkSliceNavigationWidget::GetInvertedControls() const -{ - return m_InvertedControls; -} - -void QmitkSliceNavigationWidget::SetInvertedControls(bool invertedControls) -{ - if (invertedControls != m_InvertedControls) - { - m_InvertedControls = invertedControls; - m_Slider->setInvertedAppearance(invertedControls); - m_Slider->setInvertedControls(invertedControls); - m_SpinBox->setInvertedControls(invertedControls); - } -} -