📄 qextdatetimevalidator.cpp
字号:
#include <ctype.h>
#include <qdatetime.h>
#include <qvalidator.h>
#include <qstring.h>
#include "qextdatetimevalidator.h"
/*!
\fn QextDateTimeValidator::QextDateTimeValidator(QWidget* parent, const char* name)
Constructs a validator
*/
QextDateTimeValidator::QextDateTimeValidator(QWidget* parent, const char* name)
#ifdef QTVER_PRE_30
:QValidator(parent, name) {
#else
:QValidator((QObject*)parent, name) {
#endif
//initialize default names for days and months
QStringList defDays;
QStringList defMonths;
defDays+="Monday";
defDays+="Tuesday";
defDays+="Wednesday";
defDays+="Thursday";
defDays+="Friday";
defDays+="Saturday";
defDays+="Sunday";
defMonths+="January";
defMonths+="February";
defMonths+="March";
defMonths+="April";
defMonths+="May";
defMonths+="June";
defMonths+="July";
defMonths+="August";
defMonths+="September";
defMonths+="October";
defMonths+="November";
defMonths+="December";
setStrings(defDays, defMonths);
findStringLimits();
//set up for string field length variables
MaxDayString=0;
MinDayString=INT_MAX;
MaxMonthString=0;
MinMonthString=INT_MAX;
//initialize display format structure to NULL
DisplayFormat=NULL;
}
/*!
\fn QextDateTimeValidator::~QextDateTimeValidator()
Default destructor
*/
QextDateTimeValidator::~QextDateTimeValidator() {
}
/*!
\fn QextDateTimeValidator::Token* const QextDateTimeValidator::displayFormat()
Parses the display format string, and returns a pointer to a linked list of tokens in it.
*/
QextDateTimeValidator::Token* const QextDateTimeValidator::displayFormat() {
parseFormat();
return DisplayFormat;
}
/*!
\fn void QextDateTimeValidator::findStringLimits(void)
Finds the minimum and maximum lengths of strings representing the names of days and months.
This information is used in determining which field the edit box cursor is currently in.
*/
void QextDateTimeValidator::findStringLimits(void) {
int i;
for (i=1; i<=7; i++) {
if (longDayName(i).length()<MinDayString) {
MinDayString=longDayName(i).length();
}
if (longDayName(i).length()>MaxDayString) {
MaxDayString=longDayName(i).length();
}
}
for (i=1; i<=12; i++) {
if (longMonthName(i).length()<MinMonthString) {
MinMonthString=longMonthName(i).length();
}
if (longMonthName(i).length()>MaxMonthString) {
MaxMonthString=longMonthName(i).length();
}
}
}
/*!
\fn void QextDateTimeValidator::makeString(QString& str, QDate& Date, QTime& Time) const
Makes a display string from the current format and the date and time information provided.
Strings representing the names of days and months are also passed in.
*/
void QextDateTimeValidator::makeString(QString& str, QDate& Date, QTime& Time) const {
int month=Date.month();
int day=Date.day();
int year=Date.year();
int hour=Time.hour();
int minute=Time.minute();
int second=Time.second();
int weekDay=Date.dayOfWeek();
int i;
unsigned int j;
Token* CurToken=DisplayFormat;
QString TokenString;
while (CurToken) {
/*literal string - append as is*/
if (CurToken->Type==TYPE_LITERAL) {
str.append(CurToken->SpecString);
}
/*format specifier - translate to string*/
else {
TokenString="";
const char* temp=(const char*)(CurToken->SpecString);
for (i=0; !isalpha(temp[i]); i++) {}
switch(temp[i]) {
case 'A':
if (hour<12) {
str.append("AM");
}
else {
str.append("PM");
}
break;
case 'a':
if (hour<12) {
str.append("am");
}
else {
str.append("pm");
}
break;
case 'd':
if (CurToken->IsRightJustified && day<10) {
str.append("0");
}
str.append(QString::number(day));
break;
case 'H':
if (CurToken->IsRightJustified && hour<10) {
str.append("0");
}
str.append(QString::number(hour));
break;
case 'h':
if (CurToken->IsRightJustified && (((hour+11)%12)+1)<10) {
str.append("0");
}
str.append(QString::number(((hour+11)%12)+1));
break;
case 'M':
if (CurToken->IsRightJustified && month<10) {
str.append("0");
}
str.append(QString::number(month));
break;
case 'm':
if (CurToken->IsRightJustified) {
/*add leading spaces if necessary*/
if (longMonthName(month).length()<CurToken->MinSize) {
for (j=0; j<CurToken->MaxSize-longMonthName(month).length(); j++) {
str.append(" ");
}
}
}
if (CurToken->TruncateLength==0) {
CurToken->TruncateLength=longMonthName(month).length();
}
str.append(longMonthName(month).left(CurToken->TruncateLength));
break;
case 'n':
if (minute<10) {
str.append('0');
}
str.append(QString::number(minute));
break;
case 's':
if (second<10) {
str.append('0');
}
str.append(QString::number(second));
break;
case 'w':
if (CurToken->IsRightJustified) {
/*add leading spaces if necessary*/
if (longDayName(weekDay).length()<CurToken->MinSize) {
for (j=0; j<CurToken->MinSize-longDayName(weekDay).length(); j++) {
str.append(" ");
}
}
}
if (CurToken->TruncateLength==0) {
CurToken->TruncateLength=longDayName(weekDay).length();
}
str.append(longDayName(weekDay).left(CurToken->TruncateLength));
break;
case 'Y':
str.append(QString::number(year));
break;
case 'y':
if ((year%100)<10) {
str.append('0');
}
str.append(QString::number(year%100));
break;
}
}
CurToken=CurToken->Next;
}
}
/*!
\fn unsigned int QextDateTimeValidator::maxDayStringSize(void) const
Gets the maximum length of the full name of a day of the week.
*/
unsigned int QextDateTimeValidator::maxDayStringSize(void) const {
return MaxDayString;
}
/*!
\fn unsigned int QextDateTimeValidator::maxMonthStringSize(void) const
Gets the maximum length of the full name of a month of the year.
*/
unsigned int QextDateTimeValidator::maxMonthStringSize(void) const {
return MaxMonthString;
}
/*!
\fn unsigned int QextDateTimeValidator::minDayStringSize(void) const
Gets the minimum length of the full name of a day of the week.
*/
unsigned int QextDateTimeValidator::minDayStringSize(void) const {
return MinDayString;
}
/*!
\fn unsigned int QextDateTimeValidator::minMonthStringSize(void) const
Gets the minimum length of the full name of a month of the year.
*/
unsigned int QextDateTimeValidator::minMonthStringSize(void) const {
return MinMonthString;
}
/*!
\fn void QextDateTimeValidator::parseFormat()
Parses the date and time display format into a series of tokens and literal strings. Used
by the update mechanism when the user clicks the up or down button.
*/
void QextDateTimeValidator::parseFormat() {
Token* CurToken=DisplayFormat;
Token* temp;
const char* FormatString=(const char*)Format;
bool CompleteToken;
int startIndex;
int i, j=0;
/*remove current display format list*/
while (CurToken) {
temp=CurToken;
CurToken=CurToken->Next;
delete temp;
}
DisplayFormat=NULL;
/*parse through format string*/
for (i=0; FormatString[i]; ) {
/*allocate new token structure*/
CurToken=new Token;
CurToken->Next=NULL;
CurToken->IsRightJustified=FALSE;
CurToken->TruncateLength=0;
CurToken->SpecString="";
if (!DisplayFormat) {
DisplayFormat=CurToken;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -