/* EditVariableDialog.cpp * * Copyright (C) 2019 Jim Evins * * This file is part of gLabels-qt. * * gLabels-qt is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * gLabels-qt is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with gLabels-qt. If not, see . */ #include "EditVariableDialog.h" #include "model/Settings.h" #include namespace { // All variable types. (must be in sorted order) const QVector allTypes = { glabels::model::Variable::Type::NUMERIC, glabels::model::Variable::Type::STRING }; // All variable increments. (must be in sorted order) const QVector allIncrements = { glabels::model::Variable::Increment::NEVER, glabels::model::Variable::Increment::PER_COPY, glabels::model::Variable::Increment::PER_MERGE_RECORD, glabels::model::Variable::Increment::PER_PAGE }; } namespace glabels { /// /// Constructor /// EditVariableDialog::EditVariableDialog( QWidget *parent ) : QDialog(parent) { setupUi( this ); QRegularExpression reIdentifier( "[a-zA-Z_][a-zA-Z_0-9]*" ); nameEdit->setValidator( new QRegularExpressionValidator( reIdentifier ) ); for ( auto type : allTypes ) { typeCombo->addItem( model::Variable::typeToI18nString( type ) ); } for ( auto type : allIncrements ) { incrementCombo->addItem( model::Variable::incrementToI18nString( type ) ); } } /// /// Set variable /// void EditVariableDialog::setVariable( const model::Variable& variable ) { typeCombo->setCurrentIndex( static_cast(variable.type()) ); nameEdit->setText( variable.name() ); valueEdit->setText( variable.value() ); incrementCombo->setCurrentIndex( static_cast(variable.increment()) ); stepSizeEdit->setText( variable.stepSize() ); updateControls(); } /// /// Get variable /// model::Variable EditVariableDialog::variable() const { return model::Variable( static_cast(typeCombo->currentIndex()), nameEdit->text(), valueEdit->text(), static_cast(incrementCombo->currentIndex()), stepSizeEdit->text() ); } /// /// nameEdit Changed /// void EditVariableDialog::onNameEditChanged() { validateCurrentInputs(); } /// /// typeCombo Changed /// void EditVariableDialog::onTypeComboChanged() { updateControls(); } /// /// valueEdit Changed /// void EditVariableDialog::onValueEditChanged() { validateCurrentInputs(); } /// /// incrementCombo Changed /// void EditVariableDialog::onIncrementComboChanged() { updateControls(); } /// /// stepSizeEdit Changed /// void EditVariableDialog::onStepSizeEditChanged() { validateCurrentInputs(); } /// /// update controls /// void EditVariableDialog::updateControls() { auto type = static_cast(typeCombo->currentIndex()); auto increment = static_cast(incrementCombo->currentIndex()); if ( type == model::Variable::Type::NUMERIC ) { valueEdit->setValidator( new QDoubleValidator() ); stepSizeEdit->setValidator( new QDoubleValidator() ); if ( increment == model::Variable::Increment::NEVER ) { stepSizeEdit->setText( "0" ); } } else { valueEdit->setValidator( nullptr ); stepSizeEdit->setValidator( nullptr ); incrementCombo->setCurrentIndex( static_cast(model::Variable::Increment::NEVER) ); stepSizeEdit->setText( "" ); } incrementLabel->setEnabled( type == model::Variable::Type::NUMERIC ); incrementCombo->setEnabled( type == model::Variable::Type::NUMERIC ); stepSizeLabel->setEnabled( (type == model::Variable::Type::NUMERIC) && (increment != model::Variable::Increment::NEVER) ); stepSizeEdit->setEnabled( (type == model::Variable::Type::NUMERIC) && (increment != model::Variable::Increment::NEVER) ); validateCurrentInputs(); } /// /// validate current inputs /// void EditVariableDialog::validateCurrentInputs() { bool hasValidIdentifier = nameEdit->hasAcceptableInput(); bool hasValidValue = valueEdit->hasAcceptableInput(); bool hasValidStepSize = stepSizeEdit->hasAcceptableInput(); bool isValid = hasValidIdentifier && hasValidValue && hasValidStepSize; buttonBox->button(QDialogButtonBox::Ok)->setEnabled( isValid ); } } // namespace glabels