diff --git a/glabels/EditVariableDialog.cpp b/glabels/EditVariableDialog.cpp index 62de4db..2abcb12 100644 --- a/glabels/EditVariableDialog.cpp +++ b/glabels/EditVariableDialog.cpp @@ -22,6 +22,26 @@ #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 { @@ -33,6 +53,19 @@ namespace glabels : 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 ) ); + } } @@ -64,6 +97,15 @@ namespace glabels } + /// + /// nameEdit Changed + /// + void EditVariableDialog::onNameEditChanged() + { + validateCurrentInputs(); + } + + /// /// typeCombo Changed /// @@ -78,6 +120,7 @@ namespace glabels /// void EditVariableDialog::onValueEditChanged() { + validateCurrentInputs(); } @@ -95,6 +138,7 @@ namespace glabels /// void EditVariableDialog::onStepSizeEditChanged() { + validateCurrentInputs(); } @@ -103,25 +147,49 @@ namespace glabels /// void EditVariableDialog::updateControls() { - model::Variable::Type type = static_cast(typeCombo->currentIndex()); - model::Variable::Increment increment = static_cast(incrementCombo->currentIndex()); + auto type = static_cast(typeCombo->currentIndex()); + auto increment = static_cast(incrementCombo->currentIndex()); - if ( type != model::Variable::Type::NUMERIC ) + 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( "" ); } - if ( increment == model::Variable::Increment::NEVER ) - { - stepSizeEdit->setText( "0" ); - } - 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 ); } diff --git a/glabels/EditVariableDialog.h b/glabels/EditVariableDialog.h index 1419f2c..8c57a20 100644 --- a/glabels/EditVariableDialog.h +++ b/glabels/EditVariableDialog.h @@ -54,6 +54,7 @@ namespace glabels // Slots ///////////////////////////////// private slots: + void onNameEditChanged(); void onTypeComboChanged(); void onValueEditChanged(); void onIncrementComboChanged(); @@ -64,6 +65,7 @@ namespace glabels // Private methods ///////////////////////////////// void updateControls(); + void validateCurrentInputs(); }; diff --git a/glabels/VariablesView.cpp b/glabels/VariablesView.cpp index c737b71..088fe70 100644 --- a/glabels/VariablesView.cpp +++ b/glabels/VariablesView.cpp @@ -210,7 +210,6 @@ namespace glabels { if ( v.name() == name ) { - qDebug() << "Selecting row " << iRow; table->setCurrentCell( iRow, 0, (QItemSelectionModel::Select|QItemSelectionModel::Rows) ); break; diff --git a/glabels/ui/EditVariableDialog.ui b/glabels/ui/EditVariableDialog.ui index 8cd925e..11399a7 100644 --- a/glabels/ui/EditVariableDialog.ui +++ b/glabels/ui/EditVariableDialog.ui @@ -24,18 +24,7 @@ - - - - Numeric - - - - - String - - - + @@ -67,28 +56,7 @@ - - - - Never - - - - - Per Copy - - - - - Per Merge Record - - - - - Per Page - - - + @@ -227,11 +195,28 @@ + + nameEdit + textChanged(QString) + EditVariableDialog + onNameEditChanged() + + + 371 + 62 + + + 393 + 165 + + + onTypeComboChanged() onValueEditChanged() onIncrementComboChanged() onStepSizeEditChanged() + onNameEditChanged() diff --git a/translations/glabels_C.ts b/translations/glabels_C.ts index 59305a8..29dc951 100644 --- a/translations/glabels_C.ts +++ b/translations/glabels_C.ts @@ -188,14 +188,6 @@ Variable Type: - - Numeric - - - - String - - Name: @@ -208,22 +200,6 @@ Increment: - - Never - - - - Per Copy - - - - Per Merge Record - - - - Per Page - - Step Size: