File indexing completed on 2024-05-12 16:35:03

0001 /* This file is part of the KDE project
0002  * Copyright (C) 2011 Sebastian Sauer <mail@dipe.org>
0003  *
0004  * This library is free software; you can redistribute it and/or
0005  * modify it under the terms of the GNU Library General Public
0006  * License as published by the Free Software Foundation; either
0007  * version 2 of the License, or (at your option) any later version.
0008  *
0009  * This library 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 GNU
0012  * Library General Public License for more details.
0013  *
0014  * You should have received a copy of the GNU Library General Public License
0015  * along with this library; see the file COPYING.LIB.  If not, write to
0016  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017  * Boston, MA 02110-1301, USA.
0018  */
0019 
0020 #include "UserVariableOptionsWidget.h"
0021 
0022 #include "UserVariable.h"
0023 #include "VariablesDebug.h"
0024 
0025 #include <KoVariableManager.h>
0026 
0027 #include <QGridLayout>
0028 #include <QHBoxLayout>
0029 #include <QLabel>
0030 #include <QComboBox>
0031 #include <QLineEdit>
0032 #include <QPushButton>
0033 #include <QValidator>
0034 #include <QInputDialog>
0035 
0036 #include <klocalizedstring.h>
0037 #include <kmessagebox.h>
0038 
0039 UserVariableOptionsWidget::UserVariableOptionsWidget(UserVariable* userVariable, QWidget *parent)
0040     : QWidget(parent)
0041     , userVariable(userVariable)
0042 {
0043     QGridLayout *layout = new QGridLayout(this);
0044     layout->setColumnStretch(1, 1);
0045     setLayout(layout);
0046 
0047     QLabel *nameLabel = new QLabel(i18n("Name:"), this);
0048     nameLabel->setAlignment(Qt::AlignRight);
0049     layout->addWidget(nameLabel, 0, 0);
0050     QHBoxLayout *nameLayout = new QHBoxLayout();
0051     nameEdit = new QComboBox(this);
0052     nameEdit->setObjectName(QLatin1String("nameEdit"));
0053     nameEdit->setMinimumContentsLength(10);
0054     nameLabel->setBuddy(nameEdit);
0055     connect(nameEdit, SIGNAL(currentIndexChanged(QString)), this, SLOT(nameChanged()));
0056     nameLayout->addWidget(nameEdit);
0057 
0058     newButton = new QPushButton(i18n("New"), this);
0059     connect(newButton, SIGNAL(clicked()), this, SLOT(newClicked()));
0060     nameLayout->addWidget(newButton);
0061 
0062     deleteButton = new QPushButton(i18n("Delete"), this);
0063     deleteButton->setObjectName("DeleteButton");
0064     connect(deleteButton, SIGNAL(clicked()), this, SLOT(deleteClicked()));
0065     nameLayout->addWidget(deleteButton);
0066 
0067     layout->addLayout(nameLayout, 0, 1);
0068 
0069     QLabel *typeLabel = new QLabel(i18n("Format:"), this);
0070     typeLabel->setAlignment(Qt::AlignRight);
0071     layout->addWidget(typeLabel, 1, 0);
0072     typeEdit = new QComboBox(this);
0073     typeEdit->setObjectName(QLatin1String("typeEdit"));
0074     typeLabel->setBuddy(typeEdit);
0075     typeEdit->addItem(i18n("String"), QLatin1String("string"));
0076     typeEdit->addItem(i18n("Boolean"), QLatin1String("boolean"));
0077     typeEdit->addItem(i18n("Float"), QLatin1String("float"));
0078     typeEdit->addItem(i18n("Percentage"), QLatin1String("percentage"));
0079     typeEdit->addItem(i18n("Currency"), QLatin1String("currency"));
0080     typeEdit->addItem(i18n("Date"), QLatin1String("date"));
0081     typeEdit->addItem(i18n("Time"), QLatin1String("time"));
0082     typeEdit->addItem(i18n("Formula"), QLatin1String("formula"));
0083     typeEdit->addItem(i18n("Void"), QLatin1String("void"));
0084     typeEdit->setCurrentIndex(qMax(0, typeEdit->findData(variableManager()->userType(userVariable->name()))));
0085     connect(typeEdit, SIGNAL(currentIndexChanged(QString)), this, SLOT(typeChanged()));
0086     layout->addWidget(typeEdit, 1, 1);
0087 
0088     QLabel *valueLabel = new QLabel(i18n("Value:"), this);
0089     valueLabel->setAlignment(Qt::AlignRight);
0090     layout->addWidget(valueLabel, 2, 0);
0091     valueEdit = new QLineEdit(this);
0092     valueEdit->setObjectName(QLatin1String("valueEdit"));
0093     valueLabel->setBuddy(valueEdit);
0094     valueEdit->setText(variableManager()->value(userVariable->name()));
0095     connect(valueEdit, SIGNAL(textChanged(QString)), this, SLOT(valueChanged()));
0096     layout->addWidget(valueEdit, 2, 1);
0097 
0098     updateNameEdit();
0099 }
0100 
0101 UserVariableOptionsWidget::~UserVariableOptionsWidget()
0102 {
0103 }
0104 
0105 KoVariableManager *UserVariableOptionsWidget::variableManager()
0106 {
0107     return userVariable->variableManager();
0108 }
0109 
0110 void UserVariableOptionsWidget::nameChanged()
0111 {
0112     bool enabled = !variableManager()->userVariables().isEmpty();
0113 
0114     nameEdit->setEnabled(enabled);
0115     userVariable->setName(nameEdit->currentText());
0116 
0117     bool wasBlocked = typeEdit->blockSignals(true);
0118     typeEdit->setCurrentIndex(qMax(0, typeEdit->findData(variableManager()->userType(userVariable->name()))));
0119     typeEdit->blockSignals(wasBlocked);
0120     typeEdit->setEnabled(enabled);
0121 
0122     wasBlocked = valueEdit->blockSignals(true);
0123     valueEdit->setText(variableManager()->value(userVariable->name()));
0124     valueEdit->blockSignals(wasBlocked);
0125     valueEdit->setEnabled(enabled);
0126 
0127     deleteButton->setEnabled(enabled);
0128 }
0129 
0130 void UserVariableOptionsWidget::typeChanged()
0131 {
0132     QString value = variableManager()->value(userVariable->name());
0133     QString type = typeEdit->itemData(typeEdit->currentIndex()).toString();
0134     variableManager()->setValue(userVariable->name(), value, type);
0135     //userVariable->valueChanged();
0136 }
0137 
0138 void UserVariableOptionsWidget::valueChanged()
0139 {
0140     QString value = valueEdit->text();
0141     QString type = variableManager()->userType(userVariable->name());
0142     variableManager()->setValue(userVariable->name(), value, type);
0143     //userVariable->valueChanged();
0144 }
0145 
0146 void UserVariableOptionsWidget::newClicked()
0147 {
0148     class Validator : public QValidator
0149     {
0150     public:
0151         Validator(KoVariableManager *variableManager) : m_variableManager(variableManager) {}
0152         State validate(QString &input, int &) const override
0153         {
0154             QString s = input.trimmed();
0155             return s.isEmpty() || m_variableManager->userVariables().contains(s) ? Intermediate : Acceptable;
0156         }
0157     private:
0158         KoVariableManager *m_variableManager;
0159     };
0160     Validator validator(variableManager());
0161     QString name = QInputDialog::getText(this, i18n("New Variable"), i18n("Name for new variable:")/*QT5TODO:, &validator*/).trimmed();
0162     if (name.isEmpty()) {
0163         return;
0164     }
0165     userVariable->setName(name);
0166     variableManager()->setValue(userVariable->name(), QString(), QLatin1String("string"));
0167     updateNameEdit();
0168     valueEdit->setFocus();
0169 }
0170 
0171 void UserVariableOptionsWidget::deleteClicked()
0172 {
0173     if (!variableManager()->userVariables().contains(userVariable->name())) {
0174         return;
0175     }
0176     if (KMessageBox::questionYesNo(this,
0177             i18n("Delete variable <b>%1</b>?", userVariable->name()),
0178             i18n("Delete Variable"),
0179             KStandardGuiItem::yes(),
0180             KStandardGuiItem::cancel(),
0181             QString(),
0182             KMessageBox::Dangerous | KMessageBox::Notify) != KMessageBox::Yes) {
0183         return;
0184     }
0185     variableManager()->remove(userVariable->name());
0186     userVariable->setName(QString());
0187     updateNameEdit();
0188 }
0189 
0190 void UserVariableOptionsWidget::updateNameEdit()
0191 {
0192     QStringList names = variableManager()->userVariables();
0193     bool wasBlocked = nameEdit->blockSignals(true);
0194     nameEdit->clear();
0195     nameEdit->addItems(names);
0196     nameEdit->blockSignals(wasBlocked);
0197     if (userVariable->name().isNull() && !names.isEmpty()) {
0198         userVariable->setName(names.first());
0199     }
0200     nameEdit->setCurrentIndex(qMax(0, names.indexOf(userVariable->name())));
0201     nameChanged();
0202 }
0203