MDateValidator
::MDateValidator ( QObject * parent
) : QRegExpValidator ( QRegExp ( "[0-9]{1,2}[/\\.]{0,1}[0-9]{1,2}[/\\.]{0,1}[0-9]{2,4}" ), parent
) {}
{
// Empty date => We accept it
if ( input.length() == 0 ) return Acceptable;
// Must follw minimal specifications...
return Invalid;
// 0 : We allow ".", but change it by "/"
input.replace ( '.', "/" );
// PreparacionInput prepare
// 1 : day must be 1..31. If first digit is between 4..9 => we change it to 04/09
if ( input.left ( 1 ) > "3" )
{
input = "0" + input;
pos++;
}
// If they introduce one-digit day and a bar we add the leading "0"
if ( ( input.length() == 2 ) && ( input.mid ( 1, 1 ) == "/" ) )
{
input.insert ( 0, "0" );
pos++;
}
// We do know the day, so we can make a first validation
int Dia = input.mid ( 0, 2 ).toInt();
if ( Dia > 31 ) return Invalid;
if ( ( Dia == 0 ) && ( input.length() < 2 ) ) return Intermediate;
// Append bar if they press the first month's day without pushing bar...
if ( ( input.length() > 2 ) && ( input.mid ( 2, 1 ) != "/" ) )
{
input.insert ( 2, "/" );
pos++;
}
// If there is no month, they are OK, by now...
if ( input.length() < 4 ) return Intermediate;
// If they enter a month before October without the leading zero, we put it
if ( ( input.length() == 5 ) && ( input.mid ( 4, 1 ) == "/" ) )
{
input.insert ( 3, "0" );
pos++;
}
// 2 : Month between 1 and 12, but considering the day...
int Mes = input.mid ( 3, 2 ).toInt();
if ( Mes > 12 ) return Invalid;
switch ( Mes )
{
// 30 day months
case 4 : // April
case 5 : // June
case 9 : // September
case 11 : // November
if ( Dia > 30 ) return Invalid;
break;
// Special case : February, we don't know the year, so 29 is possible...
case 2 :
if ( Dia > 29 ) return Invalid;
break;
default :
break;
}
// 3 : a month beginning by 2..9, We have to insert a leading 0
if ( input.mid ( 3, 1 ).toInt() > 1 )
{
input.insert ( 3, "0" );
pos++;
}
// We append month / year separation, if the put first year's digit without the bar
if ( ( input.length() > 5 ) && ( input.mid ( 5, 1 ) != "/" ) )
{
input.insert ( 5, "/" );
pos++;
}
// 4 : Year could begin by 1 ( 19xx ) or 2 ( 2xxx )
if ( input.mid ( 6, 1 ).toInt() > 2 )
{
input.insert ( 6, "20" );
pos += 2;
}
// Two / four last digits are for the year...
int Anyo = input.mid ( 6, 4 ).toInt();
if ( Anyo < 1900 ) return Intermediate;
if ( MDate::IsValid ( Anyo, Mes, Dia ) )
{
// Here must be your suggested call / signal
return Acceptable;
}
else
return Invalid;
}
MDateValidator::MDateValidator ( QObject * parent ) : QRegExpValidator ( QRegExp ( "[0-9]{1,2}[/\\.]{0,1}[0-9]{1,2}[/\\.]{0,1}[0-9]{2,4}" ), parent ) {}
QValidator::State MDateValidator::validate ( QString & input, int & pos ) const
{
// Empty date => We accept it
if ( input.length() == 0 ) return Acceptable;
// Must follw minimal specifications...
if ( QRegExpValidator::validate ( input, pos ) == Invalid )
return Invalid;
// 0 : We allow ".", but change it by "/"
input.replace ( '.', "/" );
// PreparacionInput prepare
// 1 : day must be 1..31. If first digit is between 4..9 => we change it to 04/09
if ( input.left ( 1 ) > "3" )
{
input = "0" + input;
pos++;
}
// If they introduce one-digit day and a bar we add the leading "0"
if ( ( input.length() == 2 ) && ( input.mid ( 1, 1 ) == "/" ) )
{
input.insert ( 0, "0" );
pos++;
}
// We do know the day, so we can make a first validation
int Dia = input.mid ( 0, 2 ).toInt();
if ( Dia > 31 ) return Invalid;
if ( ( Dia == 0 ) && ( input.length() < 2 ) ) return Intermediate;
// Append bar if they press the first month's day without pushing bar...
if ( ( input.length() > 2 ) && ( input.mid ( 2, 1 ) != "/" ) )
{
input.insert ( 2, "/" );
pos++;
}
// If there is no month, they are OK, by now...
if ( input.length() < 4 ) return Intermediate;
// If they enter a month before October without the leading zero, we put it
if ( ( input.length() == 5 ) && ( input.mid ( 4, 1 ) == "/" ) )
{
input.insert ( 3, "0" );
pos++;
}
// 2 : Month between 1 and 12, but considering the day...
int Mes = input.mid ( 3, 2 ).toInt();
if ( Mes > 12 ) return Invalid;
switch ( Mes )
{
// 30 day months
case 4 : // April
case 5 : // June
case 9 : // September
case 11 : // November
if ( Dia > 30 ) return Invalid;
break;
// Special case : February, we don't know the year, so 29 is possible...
case 2 :
if ( Dia > 29 ) return Invalid;
break;
default :
break;
}
// 3 : a month beginning by 2..9, We have to insert a leading 0
if ( input.mid ( 3, 1 ).toInt() > 1 )
{
input.insert ( 3, "0" );
pos++;
}
// We append month / year separation, if the put first year's digit without the bar
if ( ( input.length() > 5 ) && ( input.mid ( 5, 1 ) != "/" ) )
{
input.insert ( 5, "/" );
pos++;
}
// 4 : Year could begin by 1 ( 19xx ) or 2 ( 2xxx )
if ( input.mid ( 6, 1 ).toInt() > 2 )
{
input.insert ( 6, "20" );
pos += 2;
}
// Two / four last digits are for the year...
int Anyo = input.mid ( 6, 4 ).toInt();
if ( Anyo < 1900 ) return Intermediate;
if ( MDate::IsValid ( Anyo, Mes, Dia ) )
{
// Here must be your suggested call / signal
return Acceptable;
}
else
return Invalid;
}
To copy to clipboard, switch view to plain text mode
void MQEntryFormPrivate
::FieldExit ( QWidget * Sender
) {
// If is valid, we step to the next widget, else we remain
if ( BusinessObject()->ValidateField ( Sender ) )
pNext = this->GetNextFocus ( Sender );
// Some irrelevant stuff here...
pNext->setFocus();
void MQEntryFormPrivate::FieldExit ( QWidget * Sender )
{
QWidget * pNext = Sender;
// If is valid, we step to the next widget, else we remain
if ( BusinessObject()->ValidateField ( Sender ) )
pNext = this->GetNextFocus ( Sender );
// Some irrelevant stuff here...
pNext->setFocus();
To copy to clipboard, switch view to plain text mode
Bookmarks