File indexing completed on 2024-04-21 03:41:34

0001 /*************************************************************************************
0002  *  Copyright (C) 2007 by Aleix Pol <aleixpol@kde.org>                               *
0003  *                                                                                   *
0004  *  This program is free software; you can redistribute it and/or                    *
0005  *  modify it under the terms of the GNU General Public License                      *
0006  *  as published by the Free Software Foundation; either version 2                   *
0007  *  of the License, or (at your option) any later version.                           *
0008  *                                                                                   *
0009  *  This program is distributed in the hope that it will be useful,                  *
0010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of                   *
0011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                    *
0012  *  GNU General Public License for more details.                                     *
0013  *                                                                                   *
0014  *  You should have received a copy of the GNU General Public License                *
0015  *  along with this program; if not, write to the Free Software                      *
0016  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA   *
0017  *************************************************************************************/
0018 
0019 #include "varedit.h"
0020 #include <analitza/analyzer.h>
0021 #include <analitza/expression.h>
0022 #include <analitza/variables.h>
0023 #include <analitzagui/expressionedit.h>
0024 
0025 #include <QDialogButtonBox>
0026 #include <QPushButton>
0027 #include <QVBoxLayout>
0028 #include <klocalizedstring.h>
0029 
0030 VarEdit::VarEdit(QWidget *parent, bool modal)
0031     : QDialog(parent)
0032     , vars(nullptr)
0033     , m_correct(false)
0034 {
0035     setWindowTitle(i18n("Add/Edit a variable"));
0036     setModal(modal);
0037 
0038     m_buttonBox = new QDialogButtonBox(this);
0039     m_buttonBox->setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
0040     m_removeBtn = m_buttonBox->addButton(i18n("Remove Variable"), QDialogButtonBox::DestructiveRole);
0041     m_removeBtn->setIcon(QIcon::fromTheme(QStringLiteral("edit-table-delete-row")));
0042 
0043     connect(m_removeBtn, &QAbstractButton::clicked, this, &VarEdit::removeVariable);
0044 
0045     connect(m_buttonBox, &QDialogButtonBox::accepted, this, &VarEdit::accept);
0046     connect(m_buttonBox, &QDialogButtonBox::rejected, this, &VarEdit::reject);
0047 
0048     m_exp = new Analitza::ExpressionEdit(this);
0049     connect(m_exp, &QPlainTextEdit::textChanged, this, &VarEdit::edit);
0050     connect(m_exp, &Analitza::ExpressionEdit::returnPressed, this, &VarEdit::ok);
0051 
0052     m_valid = new QLabel(this);
0053 
0054     QVBoxLayout *topLayout = new QVBoxLayout(this);
0055     topLayout->addWidget(m_exp);
0056     topLayout->addWidget(m_valid);
0057     topLayout->addWidget(m_buttonBox);
0058 
0059     m_exp->setFocus();
0060 }
0061 
0062 void VarEdit::setName(const QString &newVar)
0063 {
0064     m_var = newVar;
0065     setWindowTitle(i18n("Edit '%1' value", newVar));
0066     if (!vars)
0067         m_exp->setText(i18n("not available"));
0068     else {
0069         m_exp->setExpression(Analitza::Expression(vars->value(newVar)->copy()));
0070     }
0071     m_removeBtn->setEnabled(vars && canRemove(newVar));
0072 }
0073 
0074 bool VarEdit::canRemove(const QString &name) const
0075 {
0076     QHash<QString, Analitza::Object *>::const_iterator it = vars->constBegin(), itEnd = vars->constEnd();
0077     for (; it != itEnd; ++it) {
0078         QStringList deps = AnalitzaUtils::dependencies(*it, QStringList());
0079         if (deps.contains(name)) {
0080             return false;
0081         }
0082     }
0083     return true;
0084 }
0085 
0086 Analitza::Expression VarEdit::val()
0087 {
0088     Analitza::Expression val;
0089     Analitza::Analyzer a(vars);
0090 
0091     a.setExpression(m_exp->expression());
0092 
0093     if (a.isCorrect()) {
0094         a.simplify();
0095         val = a.expression();
0096     }
0097 
0098     m_correct = a.isCorrect();
0099     if (m_correct) {
0100         m_valid->setText(i18n("<b style='color:#090'>%1 := %2</b>", m_var, val.toString()));
0101         m_valid->setToolTip(QString());
0102     } else {
0103         m_valid->setText(i18n("<b style='color:red'>WRONG</b>"));
0104         m_valid->setToolTip(a.errors().join(QStringLiteral("\n")));
0105     }
0106     m_buttonBox->button(QDialogButtonBox::Ok)->setEnabled(m_correct);
0107     m_exp->setCorrect(m_correct);
0108 
0109     return val;
0110 }
0111 
0112 void VarEdit::edit()
0113 {
0114     val();
0115 }
0116 
0117 void VarEdit::ok()
0118 {
0119     if (m_correct)
0120         accept();
0121 }
0122 
0123 void VarEdit::setAnalitza(Analitza::Analyzer *na)
0124 {
0125     vars = na->variables();
0126     m_exp->setAnalitza(na);
0127 }
0128 
0129 void VarEdit::removeVariable()
0130 {
0131     vars->remove(m_var);
0132     close();
0133 }