diff --git a/Plugins/org.mitk.gui.qt.chartExample/src/internal/ChartExample.cpp b/Plugins/org.mitk.gui.qt.chartExample/src/internal/ChartExample.cpp index 3b3a4c2ed7..b90bf3e33c 100644 --- a/Plugins/org.mitk.gui.qt.chartExample/src/internal/ChartExample.cpp +++ b/Plugins/org.mitk.gui.qt.chartExample/src/internal/ChartExample.cpp @@ -1,190 +1,249 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ // Blueberry #include #include #include #include // Qmitk #include "ChartExample.h" // Qt #include #include const std::string ChartExample::VIEW_ID = "org.mitk.views.chartexample"; void ChartExample::SetFocus() { m_Controls.m_buttonCreateChart->setFocus(); } void ChartExample::CreateQtPartControl(QWidget *parent) { // create GUI widgets from the Qt Designer's .ui file m_Controls.setupUi(parent); connect(m_Controls.m_buttonCreateChart, &QPushButton::clicked, this, &ChartExample::CreateChart); connect(m_Controls.m_buttonClearChart, &QPushButton::clicked, this, &ChartExample::ClearChart); connect(m_Controls.m_buttonAddData, &QPushButton::clicked, this, &ChartExample::AddData); + connect(m_Controls.m_checkBoxEnableErrors, &QCheckBox::toggled, this, &ChartExample::ShowErrorOptions); + connect(m_Controls.m_checkBoxEnableXErrors, &QCheckBox::toggled, this, &ChartExample::ShowXErrorOptions); + connect(m_Controls.m_checkBoxEnableYErrors, &QCheckBox::toggled, this, &ChartExample::ShowYErrorOptions); + + m_Controls.m_groupBoxErrors->setVisible(false); + m_Controls.m_groupBoxXErrors->setVisible(false); + m_Controls.m_groupBoxYErrors->setVisible(false); FillRandomDataValues(); auto chartStyle = GetColorTheme(); m_Controls.m_Chart->SetTheme(chartStyle); m_Controls.m_lineEditXAxisLabel->setText("xLabel"); m_Controls.m_lineEditYAxisLabel->setText("yLabel"); m_chartNameToChartType.emplace("bar", QmitkChartWidget::ChartType::bar); m_chartNameToChartType.emplace("line", QmitkChartWidget::ChartType::line); m_chartNameToChartType.emplace("spline", QmitkChartWidget::ChartType::spline); m_chartNameToChartType.emplace("pie", QmitkChartWidget::ChartType::pie); m_chartNameToChartType.emplace("area", QmitkChartWidget::ChartType::area); m_chartNameToChartType.emplace("area-spline", QmitkChartWidget::ChartType::area_spline); m_chartNameToChartType.emplace("scatter", QmitkChartWidget::ChartType::scatter); m_LineNameToLineType.emplace("solid", QmitkChartWidget::LineStyle::solid); m_LineNameToLineType.emplace("dashed", QmitkChartWidget::LineStyle::dashed); m_AxisScaleNameToAxisScaleType.emplace("linear", QmitkChartWidget::AxisScale::linear); m_AxisScaleNameToAxisScaleType.emplace("logarithmic", QmitkChartWidget::AxisScale::log); } void ChartExample::FillRandomDataValues() { std::vector numbers = generateRandomNumbers(10, 10.0); std::string text = convertToText(numbers, ";"); m_Controls.m_lineEditDataVector->setText(QString::fromStdString(text)); m_Controls.m_lineEditDataLabel->setText("test" + QString::number(countForUID)); + + numbers = generateRandomNumbers(10, 10.0); + text = convertToText(numbers, ";"); + m_Controls.m_lineEditXErrorPlus->setText(QString::fromStdString(text)); + numbers = generateRandomNumbers(10, 10.0); + text = convertToText(numbers, ";"); + m_Controls.m_lineEditXErrorMinus->setText(QString::fromStdString(text)); + numbers = generateRandomNumbers(10, 10.0); + text = convertToText(numbers, ";"); + m_Controls.m_lineEditYErrorPlus->setText(QString::fromStdString(text)); + numbers = generateRandomNumbers(10, 10.0); + text = convertToText(numbers, ";"); + m_Controls.m_lineEditYErrorMinus->setText(QString::fromStdString(text)); + countForUID++; } void ChartExample::CreateChart() { auto dataYAxisScaleType = m_AxisScaleNameToAxisScaleType.at(m_Controls.m_comboBoxYAxisScale->currentText().toStdString()); auto xAxisLabel = m_Controls.m_lineEditXAxisLabel->text().toStdString(); auto yAxisLabel = m_Controls.m_lineEditYAxisLabel->text().toStdString(); auto showLegend = m_Controls.m_checkBoxShowLegend->isChecked(); auto showDataPoints = m_Controls.m_checkBoxShowDataPoints->isChecked(); auto stackedData = m_Controls.m_checkBoxStackedData->isChecked(); auto showSubchart = m_Controls.m_checkBoxShowSubchart->isChecked(); m_Controls.m_Chart->SetYAxisScale(dataYAxisScaleType); m_Controls.m_Chart->SetXAxisLabel(xAxisLabel); m_Controls.m_Chart->SetYAxisLabel(yAxisLabel); m_Controls.m_Chart->SetShowLegend(showLegend); m_Controls.m_Chart->SetShowErrorBars(true); m_Controls.m_Chart->SetShowDataPoints(showDataPoints); m_Controls.m_Chart->SetStackedData(stackedData); m_Controls.m_Chart->Show(showSubchart); } void ChartExample::ClearChart() { m_Controls.m_Chart->Clear(); m_Controls.m_plainTextEditDataView->clear(); } -void ChartExample::AddData() +std::vector ChartExample::ConvertToVector(const QString& lineEditData) { - auto lineEditData = m_Controls.m_lineEditDataVector->text(); std::vector data; + if (lineEditData.isEmpty()) + { + return data; + } + for(const QString entry : lineEditData.split(';')) { data.push_back(entry.toDouble()); } + return data; +} + +void ChartExample::AddData() +{ + QString lineEditData = m_Controls.m_lineEditDataVector->text(); + auto data = ConvertToVector(lineEditData); auto chartType = m_chartNameToChartType.at(m_Controls.m_comboBoxChartType->currentText().toStdString()); std::string dataLabel = m_Controls.m_lineEditDataLabel->text().toStdString(); std::string dataColor = m_Controls.m_lineEditColor->text().toStdString(); auto dataLineStyleType = m_LineNameToLineType.at(m_Controls.m_comboBoxLineStyle->currentText().toStdString()); m_Controls.m_Chart->AddData1D(data, dataLabel, chartType); if (!dataColor.empty()) { m_Controls.m_Chart->SetColor(dataLabel, dataColor); } - auto errorValuesMinus = std::vector(data.size()); - std::transform(data.begin(), data.end(), errorValuesMinus.begin(), [](double d) { return d / 2; }); - m_Controls.m_Chart->SetXErrorBars(dataLabel, data, errorValuesMinus); - m_Controls.m_Chart->SetYErrorBars(dataLabel, data, errorValuesMinus); + if (m_Controls.m_checkBoxEnableErrors->isChecked()) + { + if (m_Controls.m_checkBoxEnableXErrors->isChecked()) + { + auto errorsPlus = ConvertToVector(m_Controls.m_lineEditXErrorPlus->text()); + auto errorsMinus = ConvertToVector(m_Controls.m_lineEditXErrorMinus->text()); + m_Controls.m_Chart->SetXErrorBars(m_Controls.m_lineEditDataLabel->text().toStdString(), errorsPlus, errorsMinus); + } + if (m_Controls.m_checkBoxEnableYErrors->isChecked()) + { + auto errorsPlus = ConvertToVector(m_Controls.m_lineEditYErrorPlus->text()); + auto errorsMinus = ConvertToVector(m_Controls.m_lineEditYErrorMinus->text()); + m_Controls.m_Chart->SetYErrorBars(m_Controls.m_lineEditDataLabel->text().toStdString(), errorsPlus, errorsMinus); + } + } + m_Controls.m_Chart->SetLineStyle(dataLabel, dataLineStyleType); QString dataOverview; dataOverview.append(m_Controls.m_lineEditDataLabel->text()); dataOverview.append("(").append(m_Controls.m_comboBoxChartType->currentText()); if (!dataColor.empty()) { dataOverview.append(", ").append(dataColor.c_str()); } dataOverview.append(", ").append(m_Controls.m_comboBoxLineStyle->currentText()); dataOverview.append(")"); dataOverview.append(":").append(lineEditData); m_Controls.m_plainTextEditDataView->appendPlainText(dataOverview); FillRandomDataValues(); } +void ChartExample::ShowErrorOptions(bool show) +{ + m_Controls.m_groupBoxErrors->setVisible(show); +} + +void ChartExample::ShowXErrorOptions(bool show) +{ + m_Controls.m_groupBoxXErrors->setVisible(show); +} + +void ChartExample::ShowYErrorOptions(bool show) +{ + m_Controls.m_groupBoxYErrors->setVisible(show); +} + std::vector ChartExample::generateRandomNumbers(unsigned int amount, double max) const { QRandomGenerator gen; gen.seed(time(NULL)); std::vector data; for (unsigned int i = 0; i < amount; i++) { data.push_back(gen.bounded(max)); } return data; } std::string ChartExample::convertToText(std::vector numbers, std::string delimiter) const { std::ostringstream oss; oss.precision(3); if (!numbers.empty()) { for (auto number : numbers) { oss << number << delimiter; } } auto aString = oss.str(); aString.pop_back(); return aString; } QmitkChartWidget::ChartStyle ChartExample::GetColorTheme() const { ctkPluginContext* context = berry::WorkbenchPlugin::GetDefault()->GetPluginContext(); ctkServiceReference styleManagerRef = context->getServiceReference(); if (styleManagerRef) { auto styleManager = context->getService(styleManagerRef); if (styleManager->GetStyle().name == "Dark") { return QmitkChartWidget::ChartStyle::darkstyle; } else { return QmitkChartWidget::ChartStyle::lightstyle; } } return QmitkChartWidget::ChartStyle::darkstyle; } diff --git a/Plugins/org.mitk.gui.qt.chartExample/src/internal/ChartExample.h b/Plugins/org.mitk.gui.qt.chartExample/src/internal/ChartExample.h index 14eacef16a..a6de806426 100644 --- a/Plugins/org.mitk.gui.qt.chartExample/src/internal/ChartExample.h +++ b/Plugins/org.mitk.gui.qt.chartExample/src/internal/ChartExample.h @@ -1,67 +1,72 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ - #ifndef ChartExample_h #define ChartExample_h #include #include #include "ui_ChartExampleControls.h" /** \brief ChartExample \warning This class is not yet documented. Use "git blame" and ask the author to provide basic documentation. \sa QmitkAbstractView \ingroup ${plugin_target}_internal */ class ChartExample : public QmitkAbstractView { // this is needed for all Qt objects that should have a Qt meta-object // (everything that derives from QObject and wants to have signal/slots) Q_OBJECT public: static const std::string VIEW_ID; protected: virtual void CreateQtPartControl(QWidget *parent) override; virtual void SetFocus() override; void CreateChart(); void ClearChart(); + std::vector ConvertToVector(const QString &lineEditData); void AddData(); + void ShowErrorOptions(bool show); + void ShowXErrorOptions(bool show); + void ShowYErrorOptions(bool show); + Ui::ChartExampleControls m_Controls; + private: void FillRandomDataValues(); std::vector generateRandomNumbers(unsigned int amount, double max) const; std::string convertToText(std::vector numbers, std::string delimiter) const; QmitkChartWidget::ChartStyle GetColorTheme() const; std::map m_chartNameToChartType; std::map m_LineNameToLineType; std::map m_AxisScaleNameToAxisScaleType; - unsigned int countForUID=0; + unsigned int countForUID = 0; }; #endif // ChartExample_h diff --git a/Plugins/org.mitk.gui.qt.chartExample/src/internal/ChartExampleControls.ui b/Plugins/org.mitk.gui.qt.chartExample/src/internal/ChartExampleControls.ui index c862708844..8563999a4c 100644 --- a/Plugins/org.mitk.gui.qt.chartExample/src/internal/ChartExampleControls.ui +++ b/Plugins/org.mitk.gui.qt.chartExample/src/internal/ChartExampleControls.ui @@ -1,341 +1,479 @@ ChartExampleControls 0 0 326 944 0 0 QmitkTemplate Data - - - - data label - - - - - - - - 0 - 0 - - - - data entry 0 0 - - + + - Color + error - - - - - 0 - 0 - - - - - - + + - Line style + data label - - + + - + 0 0 - - - solid - - - - - dashed - - - + Chart type - + 0 0 bar line area spline area-spline scatter + + + + Color + + + + + + + + 0 + 0 + + + + + + + + Line style + + + + + + + + 0 + 0 + + + + + solid + + + + + dashed + + + + + + + + + 0 + 0 + + + + Error values + + + + + + + + y error + + + + + + + x error + + + + + + + + 0 + 0 + + + + GroupBox + + + + + + + + plus error + + + + + + + minus error + + + + + + + + + + + + + + + + + + + 0 + 0 + + + + GroupBox + + + + + + + + plus error + + + + + + + + 0 + 0 + + + + + + + + + + + + 0 + 0 + + + + + + + + + + + minus error + + + + + + + + + + + + + Add data 0 0 Global options XAxis label YAxis label Y Axis scale linear logarithmic Show legend true Stacked data Show data points true Show Subchart Do image processing Create chart Clear chart Qt::Vertical 0 200 0 0 0 Qt::Vertical QSizePolicy::Expanding 20 0 QmitkChartWidget QWidget
QmitkChartWidget.h