File indexing completed on 2024-05-19 11:21:28

0001 /*
0002     SPDX-License-Identifier: GPL-2.0-or-later
0003     SPDX-FileCopyrightText: 2011 Martin Kuettler <martinkuettler@gmail.com>
0004 */
0005 
0006 #include "qalculateplotassistant.h"
0007 
0008 #include <QAction>
0009 
0010 #include <KActionCollection>
0011 #include <KConfigGroup>
0012 #include "cantor_macros.h"
0013 
0014 QalculatePlotAssistant::QalculatePlotAssistant(QObject* parent, QList<QVariant> args) : Assistant(parent)
0015 {
0016     Q_UNUSED(args);
0017 
0018     m_dlg = nullptr;
0019 }
0020 
0021 void QalculatePlotAssistant::initActions()
0022 {
0023     setXMLFile(QLatin1String("cantor_qalculateplotassistant.rc"));
0024     QAction* plot  = new QAction(i18n("Plot"), actionCollection());
0025     actionCollection()->addAction(QLatin1String("qalculateplotassistant"), plot);
0026     connect(plot, SIGNAL(triggered()), this, SIGNAL(requested()));
0027 }
0028 
0029 void QalculatePlotAssistant::initDialog(QWidget* parent)
0030 {
0031     m_dlg = new QDialog(parent);
0032     QWidget *widget = new QWidget(m_dlg);
0033     m_base.setupUi(widget);
0034     QVBoxLayout *mainLayout = new QVBoxLayout;
0035     m_dlg->setLayout(mainLayout);
0036     mainLayout->addWidget(widget);
0037 
0038     m_base.buttonBox->button(QDialogButtonBox::Ok)->setIcon(QApplication::style()->standardIcon(QStyle::SP_DialogOkButton));
0039     m_base.buttonBox->button(QDialogButtonBox::Cancel)->setIcon(QApplication::style()->standardIcon(QStyle::SP_DialogCancelButton));
0040 
0041     connect(m_base.addButton, SIGNAL(clicked()), this, SLOT(addFunction()));
0042     connect(m_base.removeButton, SIGNAL(clicked()), this, SLOT(removeSelection()));
0043     connect(m_base.clearButton, SIGNAL(clicked()), this, SLOT(clearFunctions()));
0044     connect(m_base.functionTable, SIGNAL(currentCellChanged(int,int,int,int)), this, SLOT(currentItemChanged(int,int,int,int)));
0045     connect(m_base.stepsButton, SIGNAL(toggled(bool)), this, SLOT(toggleStep()));
0046     connect(m_base.stepButton, SIGNAL(toggled(bool)), this, SLOT(toggleSteps()));
0047     connect(m_base.buttonBox, SIGNAL(accepted()), m_dlg, SLOT(accept()));
0048     connect(m_base.buttonBox, SIGNAL(rejected()), m_dlg, SLOT(reject()));
0049     m_base.inlineCheckBox->setChecked(QalculateSettings::inlinePlot());
0050     m_base.colorCheckBox->setChecked(QalculateSettings::coloredPlot());
0051     m_base.gridCheckBox->setChecked(QalculateSettings::plotGrid());
0052     m_base.borderCheckBox->setChecked(QalculateSettings::plotBorder());
0053     m_base.smoothingBox->setCurrentIndex(QalculateSettings::plotSmoothing());
0054     m_base.styleBox->setCurrentIndex(QalculateSettings::plotStyle());
0055     m_base.legendBox->setCurrentIndex(QalculateSettings::plotLegend());
0056     m_base.stepsEdit->setText(QString::number(QalculateSettings::plotSteps()));
0057     m_base.stepEdit->setDisabled(true);
0058 }
0059 
0060 QStringList QalculatePlotAssistant::run(QWidget* parent)
0061 {
0062     if (!m_dlg)
0063     initDialog(parent);
0064 
0065     QStringList result;
0066     if (m_dlg->exec()) {
0067     if (m_base.functionTable->currentRow() >= 0)
0068         saveRowInformation(m_base.functionTable->currentRow());
0069     result << plotCommand();
0070     }
0071 
0072     return result;
0073 }
0074 
0075 
0076 void QalculatePlotAssistant::addFunction()
0077 {
0078     m_base.functionTable->insertRow(m_base.functionTable->rowCount());
0079     m_xVarList << QLatin1String("");
0080     m_styleList << QalculateSettings::STYLE_LINES;
0081     m_smoothingList << QalculateSettings::SMOOTHING_NONE;
0082     saveRowInformation(m_xVarList.size()-1);
0083 }
0084 
0085 void QalculatePlotAssistant::removeSelection()
0086 {
0087     int r = m_base.functionTable->currentRow();
0088     if (r < 0)
0089     return;
0090     m_base.functionTable->removeRow(r);
0091     if (m_xVarList.size() > r) {
0092     m_xVarList.removeAt(r);
0093     m_styleList.removeAt(r);
0094     m_smoothingList.removeAt(r);
0095     }
0096 }
0097 
0098 void QalculatePlotAssistant::clearFunctions()
0099 {
0100     m_xVarList.clear();
0101     m_styleList.clear();
0102     m_smoothingList.clear();
0103     while (m_base.functionTable->rowCount())
0104     m_base.functionTable->removeRow(0);
0105 }
0106 
0107 void QalculatePlotAssistant::toggleSteps()
0108 {
0109     m_base.stepsButton->setChecked(!m_base.stepButton->isChecked());
0110 }
0111 
0112 void QalculatePlotAssistant::toggleStep()
0113 {
0114     m_base.stepButton->setChecked(!m_base.stepsButton->isChecked());
0115 }
0116 
0117 void QalculatePlotAssistant::currentItemChanged(int newRow, int newColumn, int oldRow, int oldColumn)
0118 {
0119     Q_UNUSED(newColumn);
0120     Q_UNUSED(oldColumn);
0121 
0122     if (oldRow >= 0)
0123     saveRowInformation(oldRow);
0124     if (newRow >= 0)
0125     loadRowInformation(newRow);
0126 }
0127 
0128 void QalculatePlotAssistant::saveRowInformation(int row)
0129 {
0130     m_xVarList[row] = m_base.xVarEdit->text();
0131     switch(m_base.styleBox->currentIndex()) {
0132     case 0:
0133     m_styleList[row] = QalculateSettings::STYLE_LINES; break;
0134     case 1:
0135     m_styleList[row] = QalculateSettings::STYLE_POINTS; break;
0136     case 2:
0137     m_styleList[row] = QalculateSettings::STYLE_LINES_POINTS; break;
0138     case 3:
0139     m_styleList[row] = QalculateSettings::STYLE_BOXES; break;
0140     case 4:
0141     m_styleList[row] = QalculateSettings::STYLE_HISTOGRAM; break;
0142     case 5:
0143     m_styleList[row] = QalculateSettings::STYLE_STEPS; break;
0144     case 6:
0145     m_styleList[row] = QalculateSettings::STYLE_CANDLESTICKS; break;
0146     case 7:
0147     m_styleList[row] = QalculateSettings::STYLE_DOTS; break;
0148     }
0149     switch(m_base.smoothingBox->currentIndex()) {
0150     case 0:
0151     m_smoothingList[row] = QalculateSettings::SMOOTHING_NONE; break;
0152     case 1:
0153     m_smoothingList[row] = QalculateSettings::SMOOTHING_UNIQUE; break;
0154     case 2:
0155     m_smoothingList[row] = QalculateSettings::SMOOTHING_CSPLINES; break;
0156     case 3:
0157     m_smoothingList[row] = QalculateSettings::SMOOTHING_BEZIER; break;
0158     case 4:
0159     m_smoothingList[row] = QalculateSettings::SMOOTHING_SBEZIER; break;
0160     }
0161 }
0162 
0163 void QalculatePlotAssistant::loadRowInformation(int row)
0164 {
0165     m_base.xVarEdit->setText(m_xVarList[row]);
0166     m_base.styleBox->setCurrentIndex(m_styleList[row]);
0167     m_base.smoothingBox->setCurrentIndex(m_smoothingList[row]);
0168 }
0169 
0170 QString QalculatePlotAssistant::plotCommand()
0171 {
0172     QStringList boolList;
0173     boolList << QLatin1String("false") << QLatin1String("true");
0174     QString command = QLatin1String("plot");
0175     if (!m_base.plotTitleEdit->text().isEmpty())
0176     command += QString::fromLatin1(" plottitle='%1'").arg(m_base.plotTitleEdit->text());
0177     if (!m_base.xLabelEdit->text().isEmpty())
0178     command += QString::fromLatin1(" xlabel='%1'").arg(m_base.xLabelEdit->text());
0179     if (!m_base.yLabelEdit->text().isEmpty())
0180     command += QString::fromLatin1(" ylabel='%1'").arg(m_base.yLabelEdit->text());
0181     if (m_base.legendBox->currentIndex() != QalculateSettings::plotLegend()) {
0182     QString legend;
0183     switch(m_base.legendBox->currentIndex()) {
0184     case QalculateSettings::LEGEND_NONE:
0185         legend=QLatin1String("none"); break;
0186     case QalculateSettings::LEGEND_TOP_LEFT:
0187         legend=QLatin1String("top_left"); break;
0188     case QalculateSettings::LEGEND_TOP_RIGHT:
0189         legend=QLatin1String("top_right"); break;
0190     case QalculateSettings::LEGEND_BOTTOM_LEFT:
0191         legend=QLatin1String("bottom_left"); break;
0192     case QalculateSettings::LEGEND_BOTTOM_RIGHT:
0193         legend=QLatin1String("bottom_right"); break;
0194     case QalculateSettings::LEGEND_BELOW:
0195         legend=QLatin1String("below"); break;
0196     case QalculateSettings::LEGEND_OUTSIDE:
0197         legend=QLatin1String("outside"); break;
0198     }
0199     command += QString::fromLatin1(" legend=%1").arg(legend);
0200     }
0201     if (m_base.gridCheckBox->isChecked() != QalculateSettings::plotGrid())
0202     command += QString::fromLatin1(" grid=%1").arg(boolList[m_base.gridCheckBox->isChecked()]);
0203     if (m_base.borderCheckBox->isChecked() != QalculateSettings::plotBorder())
0204     command += QString::fromLatin1(" border=%1").arg(boolList[m_base.borderCheckBox->isChecked()]);
0205     if (m_base.colorCheckBox->isChecked() != QalculateSettings::coloredPlot())
0206     command += QString::fromLatin1(" color=%1").arg(boolList[m_base.colorCheckBox->isChecked()]);
0207     if (m_base.inlineCheckBox->isChecked() != QalculateSettings::inlinePlot())
0208     command += QString::fromLatin1(" inline=%1").arg(boolList[m_base.inlineCheckBox->isChecked()]);
0209     if (m_base.xLogCheckBox->isChecked())
0210     command += QString::fromLatin1(" xlog=true xlogbase='%1'").arg(m_base.xLogEdit->text());
0211     if (m_base.yLogCheckBox->isChecked())
0212     command += QString::fromLatin1(" ylog=true ylogbase='%1'").arg(m_base.yLogEdit->text());
0213     if (m_base.saveCheckBox->isChecked()) {
0214     QString filetype;
0215     switch (m_base.saveFileBox->currentIndex()) {
0216     case 0:
0217         filetype = QLatin1String("auto"); break;
0218     case 1:
0219         filetype = QLatin1String("png"); break;
0220     case 2:
0221         filetype = QLatin1String("ps"); break;
0222     case 3:
0223         filetype = QLatin1String("eps"); break;
0224     case 4:
0225         filetype = QLatin1String("latex"); break;
0226     case 5:
0227         filetype = QLatin1String("svg"); break;
0228     case 6:
0229         filetype = QLatin1String("fig"); break;
0230     }
0231     command += QString::fromLatin1(" filename='%1' filetype=%2").arg
0232         (m_base.saveFileEdit->text(), filetype);
0233     }
0234     command += QString::fromLatin1(" xmin='%1' xmax='%2'").arg
0235     (m_base.xMinEdit->text(), m_base.xMaxEdit->text());
0236     if (m_base.stepsButton->isChecked())
0237     command += QString::fromLatin1(" steps='%1'").arg(m_base.stepsEdit->text());
0238     else
0239     command += QString::fromLatin1(" step='%1'").arg(m_base.stepEdit->text());
0240     for (int i = 0; i < m_xVarList.size(); ++i) {
0241     if (i>0)
0242         command += QLatin1Char(',');
0243     command += QString::fromLatin1(" title='%1' '%2' xvar='%3'").arg
0244         (m_base.functionTable->item(i,0)->text(),
0245          m_base.functionTable->item(i,1)->text(),
0246          m_xVarList[i]);
0247     if (m_styleList[i] != QalculateSettings::plotStyle()) {
0248         QString style;
0249         switch(m_styleList[i]) {
0250         case QalculateSettings::STYLE_LINES:
0251         style=QLatin1String("lines"); break;
0252         case QalculateSettings::STYLE_POINTS:
0253         style=QLatin1String("points"); break;
0254         case QalculateSettings::STYLE_LINES_POINTS:
0255         style=QLatin1String("points_lines"); break;
0256         case QalculateSettings::STYLE_BOXES:
0257         style=QLatin1String("boxes"); break;
0258         case QalculateSettings::STYLE_HISTOGRAM:
0259         style=QLatin1String("histogram"); break;
0260         case QalculateSettings::STYLE_STEPS:
0261         style=QLatin1String("steps"); break;
0262         case QalculateSettings::STYLE_CANDLESTICKS:
0263         style=QLatin1String("candlesticks"); break;
0264         case QalculateSettings::STYLE_DOTS:
0265         style=QLatin1String("dots"); break;
0266         }
0267         command += QString::fromLatin1(" style=%1").arg(style);
0268     }
0269     if (m_smoothingList[i] != QalculateSettings::plotSmoothing()) {
0270         QString smoothing;
0271         switch(m_smoothingList[i]) {
0272         case QalculateSettings::SMOOTHING_NONE:
0273         smoothing=QLatin1String("none"); break;
0274         case QalculateSettings::SMOOTHING_UNIQUE:
0275         smoothing=QLatin1String("monotonic"); break;
0276         case QalculateSettings::SMOOTHING_CSPLINES:
0277         smoothing=QLatin1String("csplines"); break;
0278         case QalculateSettings::SMOOTHING_BEZIER:
0279         smoothing=QLatin1String("bezier"); break;
0280         case QalculateSettings::SMOOTHING_SBEZIER:
0281         smoothing=QLatin1String("sbezier"); break;
0282         }
0283         command += QString::fromLatin1(" smoothing=%1").arg(smoothing);
0284     }
0285     }
0286     return command;
0287 }
0288 
0289 K_PLUGIN_FACTORY_WITH_JSON(qalculateplotassistant, "qalculateplotassistant.json", registerPlugin<QalculatePlotAssistant>();)
0290 #include "qalculateplotassistant.moc"