File indexing completed on 2024-05-12 15:53:54

0001 /**
0002  * Copyright (C) 2001-2015 Klaralvdalens Datakonsult AB.  All rights reserved.
0003  *
0004  * This file is part of the KD Chart library.
0005  *
0006  * This program is free software; you can redistribute it and/or
0007  * modify it under the terms of the GNU General Public License as
0008  * published by the Free Software Foundation; either version 2 of
0009  * the License, or (at your option) any later version.
0010  *
0011  * This program is distributed in the hope that it will be useful,
0012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0014  * GNU General Public License for more details.
0015  *
0016  * You should have received a copy of the GNU General Public License
0017  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
0018  */
0019 
0020 #include "mainwindow.h"
0021 
0022 #include "ModelParamVsParam.h"
0023 #include "ModelParamVsParamPlot.h"
0024 
0025 #include <QWidget>
0026 #include <QLabel>
0027 #include <QLineEdit>
0028 
0029 
0030 #include <KChartChart>
0031 #include <KChartDataValueAttributes>
0032 #include <KChartGridAttributes>
0033 #include <KChartLineDiagram>
0034 
0035 
0036 using namespace KChart;
0037 
0038 
0039 MainWindow::MainWindow(
0040     QWidget                 *p_parent)
0041 :
0042     QWidget(p_parent),
0043     m_sourceModel(new ModelParamVsParam),
0044     m_timeoutLineEdit(new QLineEdit("1")),
0045     m_nrOfParametersLineEdit(new QLineEdit("4")),
0046     m_nrOfSamplesLineEdit(new QLineEdit("4")),
0047     m_paramVsParamGridLayout(new QGridLayout)
0048 {
0049     connect(m_timeoutLineEdit, SIGNAL(editingFinished()), SLOT(timeoutEditingFinished()));
0050     connect(m_nrOfParametersLineEdit, SIGNAL(editingFinished()), SLOT(editingFinished()));
0051     connect(m_nrOfSamplesLineEdit, SIGNAL(editingFinished()), SLOT(editingFinished()));
0052 
0053     QVBoxLayout *vBoxLayout = new QVBoxLayout;
0054     setLayout(vBoxLayout);
0055 
0056     QGridLayout *gridLayout = new QGridLayout;
0057     vBoxLayout->addLayout(gridLayout);
0058     gridLayout->setColumnStretch(2, 1);
0059 
0060     int row(0);
0061 
0062     gridLayout->addWidget(new QLabel("Timeout [sec]:"), row, 0);
0063     gridLayout->addWidget(m_timeoutLineEdit, row++, 1);
0064 
0065     gridLayout->addWidget(new QLabel("Number of Parameters:"), row, 0);
0066     gridLayout->addWidget(m_nrOfParametersLineEdit, row++, 1);
0067 
0068     gridLayout->addWidget(new QLabel("Number of Samples:"), row, 0);
0069     gridLayout->addWidget(m_nrOfSamplesLineEdit, row++, 1);
0070 
0071     vBoxLayout->addLayout(m_paramVsParamGridLayout);
0072 
0073     m_sourceModel->setTimeout(m_timeoutLineEdit->text().toInt());
0074     m_sourceModel->populate(m_nrOfParametersLineEdit->text().toInt(), m_nrOfSamplesLineEdit->text().toInt());
0075     addPlots();
0076 } // MainWindow::MainWindow()
0077 
0078 
0079 MainWindow::~MainWindow()
0080 {
0081     delete m_sourceModel;
0082     m_sourceModel = nullptr;
0083 } // MainWindow::~MainWindow()
0084 
0085 
0086 void MainWindow::timeoutEditingFinished()
0087 {
0088     if (m_timeoutLineEdit->isModified())
0089     {
0090         m_timeoutLineEdit->setModified(false);
0091         m_sourceModel->setTimeout(m_timeoutLineEdit->text().toInt());
0092     }
0093 } // MainWindow::timeoutEditingFinished()
0094 
0095 
0096 void MainWindow::editingFinished()
0097 {
0098     if (m_nrOfParametersLineEdit->isModified() || m_nrOfSamplesLineEdit->isModified())
0099     {
0100         m_nrOfParametersLineEdit->setModified(false);
0101         m_nrOfSamplesLineEdit->setModified(false);
0102 
0103         m_sourceModel->populate(m_nrOfParametersLineEdit->text().toInt(), m_nrOfSamplesLineEdit->text().toInt());
0104         m_sourceModel->stopSampling();
0105         removePlots();
0106         addPlots();
0107         m_sourceModel->startSampling();
0108     }
0109 } // MainWindow::editingFinished()
0110 
0111 
0112 void MainWindow::addPlots()
0113 {
0114     for (int r = 0; r < m_sourceModel->columnCount(); r++)
0115     {
0116         for (int c = 0; c < m_sourceModel->columnCount(); c++)
0117         {
0118 
0119             Chart * chart = new Chart;
0120             m_paramVsParamGridLayout->addWidget(chart, r, c);
0121 
0122             CartesianCoordinatePlane * plane = static_cast<CartesianCoordinatePlane *>(chart->coordinatePlane());
0123 
0124             // Hide grid.
0125             GridAttributes ga = plane->globalGridAttributes();
0126             ga.setGridVisible(false);
0127             plane->setGlobalGridAttributes(ga);
0128 
0129             // Set axes fixed scale.
0130             qreal                       xoffset(c * 10);
0131             qreal                       yoffset(r * 10);
0132             QPair<qreal, qreal> horizontalRange(xoffset, xoffset + 10);
0133             QPair<qreal, qreal> verticalRange(yoffset, yoffset + 10);
0134             plane->setHorizontalRange(horizontalRange);
0135             plane->setVerticalRange(verticalRange);
0136 
0137             if (r == c)
0138             {
0139             }
0140             else
0141             {
0142                 ModelParamVsParamPlot   *modelParamVsParamPlot =
0143                     new ModelParamVsParamPlot(m_sourceModel, c, r);
0144 
0145                 LineDiagram *lineDiagram = new LineDiagram;
0146                 lineDiagram->setDatasetDimension(2);
0147                 lineDiagram->setModel(modelParamVsParamPlot);
0148                 lineDiagram->setPen(Qt::NoPen);
0149                 setMarkerAttributes(lineDiagram);
0150 
0151                 CartesianAxis   *xAxis = new CartesianAxis(lineDiagram);
0152                 CartesianAxis   *yAxis = new CartesianAxis(lineDiagram);
0153                 xAxis->setPosition(CartesianAxis::Bottom);
0154                 yAxis->setPosition(CartesianAxis::Left);
0155                 xAxis->setTitleText('P' + QString::number(c));
0156                 yAxis->setTitleText('P' + QString::number(r));
0157                 lineDiagram->addAxis(xAxis);
0158                 lineDiagram->addAxis(yAxis);
0159 
0160                 chart->coordinatePlane()->replaceDiagram(lineDiagram);
0161             }
0162         } // for all rows
0163     } // for all columns
0164 } // MainWindow::addPlots()
0165 
0166 
0167 void MainWindow::removePlots()
0168 {
0169     while (m_paramVsParamGridLayout->count())
0170     {
0171         Chart *chart = static_cast<Chart *>(m_paramVsParamGridLayout->itemAt(0)->widget());
0172         m_paramVsParamGridLayout->removeWidget(chart);
0173         delete chart;
0174     }
0175 } // MainWindow::removePlots()
0176 
0177 
0178 void MainWindow::setMarkerAttributes(
0179     KChart::LineDiagram     *p_lineDiagram)
0180 {
0181     QColor                              markerColor         = Qt::green;
0182     MarkerAttributes::MarkerStyle   markerStyle         = MarkerAttributes::Marker4Pixels;
0183     QColor                              firstMarkerColor    = Qt::red;
0184     MarkerAttributes::MarkerStyle   firstMarkerStyle    = MarkerAttributes::MarkerDiamond;
0185     DataValueAttributes             dva                 = p_lineDiagram->dataValueAttributes();
0186     MarkerAttributes                    ma                      = dva.markerAttributes();
0187     TextAttributes                      ta                      = dva.textAttributes();
0188 
0189     ma.setVisible(true);
0190     ma.setMarkerColor(markerColor);
0191     ma.setMarkerStyle(markerStyle);
0192     dva.setMarkerAttributes(ma);
0193 
0194     ta.setVisible(false);
0195     dva.setTextAttributes(ta);
0196 
0197     dva.setVisible(true);
0198     p_lineDiagram->setDataValueAttributes(0, dva);
0199     p_lineDiagram->setDataValueAttributes(1, dva);
0200 
0201     // Override for first row.
0202     ma.setMarkerColor(firstMarkerColor);
0203     ma.setMarkerStyle(firstMarkerStyle);
0204     dva.setMarkerAttributes(ma);
0205     QAbstractItemModel  *model = p_lineDiagram->model();
0206     p_lineDiagram->setDataValueAttributes(model->index(0, 0), dva);
0207     p_lineDiagram->setDataValueAttributes(model->index(0, 1), dva);
0208 } // MainWindow::setMarkerAttributes()