Fleshed out input validation for EditVariableDialog.
This commit is contained in:
@@ -22,6 +22,26 @@
|
||||
|
||||
#include "model/Settings.h"
|
||||
|
||||
#include <QPushButton>
|
||||
|
||||
|
||||
namespace
|
||||
{
|
||||
// All variable types. (must be in sorted order)
|
||||
const QVector<glabels::model::Variable::Type> allTypes = {
|
||||
glabels::model::Variable::Type::NUMERIC,
|
||||
glabels::model::Variable::Type::STRING
|
||||
};
|
||||
|
||||
// All variable increments. (must be in sorted order)
|
||||
const QVector<glabels::model::Variable::Increment> 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,17 +147,25 @@ namespace glabels
|
||||
///
|
||||
void EditVariableDialog::updateControls()
|
||||
{
|
||||
model::Variable::Type type = static_cast<model::Variable::Type>(typeCombo->currentIndex());
|
||||
model::Variable::Increment increment = static_cast<model::Variable::Increment>(incrementCombo->currentIndex());
|
||||
auto type = static_cast<model::Variable::Type>(typeCombo->currentIndex());
|
||||
auto increment = static_cast<model::Variable::Increment>(incrementCombo->currentIndex());
|
||||
|
||||
if ( type != model::Variable::Type::NUMERIC )
|
||||
if ( type == model::Variable::Type::NUMERIC )
|
||||
{
|
||||
incrementCombo->setCurrentIndex( static_cast<int>(model::Variable::Increment::NEVER) );
|
||||
valueEdit->setValidator( new QDoubleValidator() );
|
||||
stepSizeEdit->setValidator( new QDoubleValidator() );
|
||||
|
||||
if ( increment == model::Variable::Increment::NEVER )
|
||||
{
|
||||
stepSizeEdit->setText( "0" );
|
||||
}
|
||||
}
|
||||
|
||||
if ( increment == model::Variable::Increment::NEVER )
|
||||
else
|
||||
{
|
||||
stepSizeEdit->setText( "0" );
|
||||
valueEdit->setValidator( nullptr );
|
||||
stepSizeEdit->setValidator( nullptr );
|
||||
incrementCombo->setCurrentIndex( static_cast<int>(model::Variable::Increment::NEVER) );
|
||||
stepSizeEdit->setText( "" );
|
||||
}
|
||||
|
||||
incrementLabel->setEnabled( type == model::Variable::Type::NUMERIC );
|
||||
@@ -122,6 +174,22 @@ namespace glabels
|
||||
(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 );
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -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();
|
||||
|
||||
};
|
||||
|
||||
|
||||
@@ -210,7 +210,6 @@ namespace glabels
|
||||
{
|
||||
if ( v.name() == name )
|
||||
{
|
||||
qDebug() << "Selecting row " << iRow;
|
||||
table->setCurrentCell( iRow, 0,
|
||||
(QItemSelectionModel::Select|QItemSelectionModel::Rows) );
|
||||
break;
|
||||
|
||||
@@ -24,18 +24,7 @@
|
||||
</widget>
|
||||
</item>
|
||||
<item row="0" column="1">
|
||||
<widget class="QComboBox" name="typeCombo">
|
||||
<item>
|
||||
<property name="text">
|
||||
<string>Numeric</string>
|
||||
</property>
|
||||
</item>
|
||||
<item>
|
||||
<property name="text">
|
||||
<string>String</string>
|
||||
</property>
|
||||
</item>
|
||||
</widget>
|
||||
<widget class="QComboBox" name="typeCombo"/>
|
||||
</item>
|
||||
<item row="1" column="0">
|
||||
<widget class="QLabel" name="label">
|
||||
@@ -67,28 +56,7 @@
|
||||
<item row="3" column="1">
|
||||
<layout class="QHBoxLayout" name="horizontalLayout_2">
|
||||
<item>
|
||||
<widget class="QComboBox" name="incrementCombo">
|
||||
<item>
|
||||
<property name="text">
|
||||
<string>Never</string>
|
||||
</property>
|
||||
</item>
|
||||
<item>
|
||||
<property name="text">
|
||||
<string>Per Copy</string>
|
||||
</property>
|
||||
</item>
|
||||
<item>
|
||||
<property name="text">
|
||||
<string>Per Merge Record</string>
|
||||
</property>
|
||||
</item>
|
||||
<item>
|
||||
<property name="text">
|
||||
<string>Per Page</string>
|
||||
</property>
|
||||
</item>
|
||||
</widget>
|
||||
<widget class="QComboBox" name="incrementCombo"/>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QLabel" name="stepSizeLabel">
|
||||
@@ -227,11 +195,28 @@
|
||||
</hint>
|
||||
</hints>
|
||||
</connection>
|
||||
<connection>
|
||||
<sender>nameEdit</sender>
|
||||
<signal>textChanged(QString)</signal>
|
||||
<receiver>EditVariableDialog</receiver>
|
||||
<slot>onNameEditChanged()</slot>
|
||||
<hints>
|
||||
<hint type="sourcelabel">
|
||||
<x>371</x>
|
||||
<y>62</y>
|
||||
</hint>
|
||||
<hint type="destinationlabel">
|
||||
<x>393</x>
|
||||
<y>165</y>
|
||||
</hint>
|
||||
</hints>
|
||||
</connection>
|
||||
</connections>
|
||||
<slots>
|
||||
<slot>onTypeComboChanged()</slot>
|
||||
<slot>onValueEditChanged()</slot>
|
||||
<slot>onIncrementComboChanged()</slot>
|
||||
<slot>onStepSizeEditChanged()</slot>
|
||||
<slot>onNameEditChanged()</slot>
|
||||
</slots>
|
||||
</ui>
|
||||
|
||||
@@ -188,14 +188,6 @@
|
||||
<source>Variable Type:</source>
|
||||
<translation type="unfinished"></translation>
|
||||
</message>
|
||||
<message>
|
||||
<source>Numeric</source>
|
||||
<translation type="unfinished"></translation>
|
||||
</message>
|
||||
<message>
|
||||
<source>String</source>
|
||||
<translation type="unfinished"></translation>
|
||||
</message>
|
||||
<message>
|
||||
<source>Name:</source>
|
||||
<translation type="unfinished"></translation>
|
||||
@@ -208,22 +200,6 @@
|
||||
<source>Increment:</source>
|
||||
<translation type="unfinished"></translation>
|
||||
</message>
|
||||
<message>
|
||||
<source>Never</source>
|
||||
<translation type="unfinished"></translation>
|
||||
</message>
|
||||
<message>
|
||||
<source>Per Copy</source>
|
||||
<translation type="unfinished"></translation>
|
||||
</message>
|
||||
<message>
|
||||
<source>Per Merge Record</source>
|
||||
<translation type="unfinished"></translation>
|
||||
</message>
|
||||
<message>
|
||||
<source>Per Page</source>
|
||||
<translation type="unfinished"></translation>
|
||||
</message>
|
||||
<message>
|
||||
<source>Step Size:</source>
|
||||
<translation type="unfinished"></translation>
|
||||
|
||||
Reference in New Issue
Block a user