?? qextdatetimespinbox.cpp
字號:
#include <qlineedit.h>
#include "qextdatetimespinbox.h"
/*!
\fn QextDateTimeSpinBox::QextDateTimeSpinBox(QWidget* parent=NULL, const char* name=NULL,
const QString* format=NULL, const QDate* date=NULL, const QTime* time=NULL,
const QStringList* days=NULL, const QStringList* months=NULL)
Constructs a QextDateTimeSpinBox with date given by the date argument and time given by the
time argument. The control will use the strings provided in the days and months QString arrays.
The date and time will be formatted as specified in the format argument, using the following
format specifiers:
\verbatim
%A uppercase AM/PM indicator ("AM" or "PM")
%a lowercase AM/PM indicator ("am" or "pm")
%d Day (1-31, depending on the current month)
%H Hour, on a 24-hour clock (0-23)
%h Hour, on a 12-hour clock (1-12)
%M Month (1-12)
%m Month String ("January", "February", etc.)
%n Minute (0-59)
%s Second (0-59)
%w Day of the week ("Sunday", "Monday", etc.)
%Y 4-digit year (1752-8000)
%y 2-digit year (0-99)
\endverbatim
The specifiers can also be modified with integers - for example %3w will print the first 3 letters
of the day of the week ("Sun", "Mon", etc.).
A 0 in a specifier indicates that leading zeros will be added where applicable. For example %0M
will represent January as "01". A zero in a non-integer specifier indicates right-alignment.
For example %0m will represent May as " May". Note the leading spaces. The length of the
field will be specified by the longest string in the Months member array.
If for example we want the format to be 17:52:07 Tue 27 Feb 2010, the widget would be created
like this:
\code
QString format("%0H:%n:%s %3w %d %3m %Y");
QextDateTimeSpinBox* DateTime=new QextDateTimeSpinBox(&format);
\endcode
If the date or time arguments are NULL or unspecified, the widget will use the current date or
time as returned by QDate::currentDate() or QTime::currentTime(). If the daysor months
structures are NULL or unspecified, the default day and/or month names will be used.
*/
QextDateTimeSpinBox::QextDateTimeSpinBox(QWidget* parent, const char* name,
const QString* format, const QDate* date,
const QTime* time, const QStringList* days,
const QStringList* months):QSpinBox(parent, name) {
//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";
//set up display strings
if (days) {
if (months) {
setStrings(*days, *months);
}
else {
setStrings(*days, defMonths);
}
}
else {
if (months) {
setStrings(defDays, *months);
}
else {
setStrings(defDays, defMonths);
}
}
//set up validator class
Validator=new QextDateTimeValidator(this, "Date/Time Validator");
//set up date
if (date) {
setDate(*date);
}
else {
setDate(QDate::currentDate());
}
//set up time
if (time) {
setTime(*time);
}
else {
setTime(QTime::currentTime());
}
//enable the down button when the control is first displayed
setValue(1);
//connect to slots
QObject::connect(editor(), SIGNAL(textChanged(const QString&)),
this, SLOT(onEditTextChanged(const QString&)));
//set up display format
if (format) {
setFormat(*format);
}
//default format: e.g. "12:00:00 Mon 1 Jan 2001"
else {
setFormat(QString("%0H:%n:%s %3w %d %3m %Y"));
}
//initialize display
refresh();
}
/*!
\fn QextDateTimeSpinBox::~QextDateTimeSpinBox()
Standard destructor.
*/
QextDateTimeSpinBox::~QextDateTimeSpinBox() {
delete Validator;
}
/*!
\fn QDate QextDateTimeSpinBox::date(void) const
Returns the most recent valid date entered into the control.
*/
QDate QextDateTimeSpinBox::date(void) const {
return Date;
}
/*!
\fn QextDateTimeValidator::Token const* QextDateTimeSpinBox::fieldFromIndex(int index) const
returns a pointer to a QextDateTimeValidator::Token data structure pertaining to the field in the
editor in which the cursor lies. Used internally. If the value of index goes beyond the length
of the string contained in the editor, NULL will be returned.
*/
QextDateTimeValidator::Token const* QextDateTimeSpinBox::fieldFromIndex(int index) {
int CurIndex=0;
//find which field cursor is in
QextDateTimeValidator::Token const* curToken=Validator->displayFormat();
QextDateTimeValidator::Token const* retVal=curToken;
while (CurIndex<=index && curToken) {
CurIndex+=fieldSize(curToken);
retVal=curToken;
curToken=curToken->Next;
}
//return NULL if we've passed the end of the string
if (CurIndex<=index) {
return NULL;
}
return retVal;
}
/*!
\fn int QextDateTimeSpinBox::fieldSize(QextDateTimeValidator::Token const* Field) const
returns the length of the token represented by Field. Used internally.
*/
int QextDateTimeSpinBox::fieldSize(QextDateTimeValidator::Token const* Field) const {
int FieldSize=0;
int hour, day, month;
//fixed-size fields
if (Field->IsFixedSize) {
FieldSize=Field->MaxSize;
}
//variable-sized fields
else {
switch (Field->FType) {
case QextDateTimeValidator::FIELD_DAY:
day=Date.day();
if (day<10) {
FieldSize=1;
}
else {
FieldSize=2;
}
break;
case QextDateTimeValidator::FIELD_HOUR_12:
case QextDateTimeValidator::FIELD_HOUR_24:
hour=Time.hour();
if (hour<10) {
FieldSize=1;
}
else {
FieldSize=2;
}
break;
case QextDateTimeValidator::FIELD_MONTH:
month=Date.month();
if (month<10) {
FieldSize=1;
}
else {
FieldSize=2;
}
break;
case QextDateTimeValidator::FIELD_DAY_STRING:
FieldSize=longDayName(Date.dayOfWeek()).length();
break;
case QextDateTimeValidator::FIELD_MONTH_STRING:
FieldSize=longMonthName(Date.month()).length();
break;
}
}
return FieldSize;
}
/*!
\fn QString QextDateTimeSpinBox::format(void)
Returns the current format string used by the control. The string returned will use the format
specifiers described in the documentation for
QextDateTimeSpinBox::QextDateTimeSpinBox(const QString*, const QDate*, const QTime*,
const QString*, const QString*).
*/
QString QextDateTimeSpinBox::format(void) const {
return Format;
}
/*!
\fn int QextDateTimeSpinBox::indexFromField(QextDateTimeValidator::Token const* field)
returns the starting offset of the field represented by the QextDateTimeValidator::Token
pointer in the editor.
*/
int QextDateTimeSpinBox::indexFromField(QextDateTimeValidator::Token const* field) {
int CurIndex=0;
//find which field the cursor is in
QextDateTimeValidator::Token const* curToken=Validator->displayFormat();
while (curToken!=field) {
CurIndex+=fieldSize(curToken);
curToken=curToken->Next;
}
//if in a right-justified month string, adjust index for leading spaces
if (field->FType==QextDateTimeValidator::FIELD_MONTH_STRING && field->IsRightJustified) {
CurIndex+=Validator->maxMonthStringSize()-longMonthName(Date.month()).length();
}
//if in a right-justified day string, adjust index for leading spaces
if (field->FType==QextDateTimeValidator::FIELD_DAY_STRING && field->IsRightJustified) {
CurIndex+=Validator->maxDayStringSize()-longDayName(Date.dayOfWeek()).length();
}
return CurIndex;
}
/*!
\fn QString QextDateTimeSpinBox::mapValueToText(int)
Trivial override of the default QSpinBox behavior - text does not map directly to a value in this
case.
*/
int QextDateTimeSpinBox::mapTextToValue(bool* ok) {
*ok=TRUE;
return 1;
}
/*!
\fn QString QextDateTimeSpinBox::mapValueToText(int)
overrides default string interpreter - integer values do not map to anything in this case
*/
QString QextDateTimeSpinBox::mapValueToText(int) {
QString str(tr(""));
Validator->makeString(str, Date, Time);
return str;
}
/*!
\fn void QextDateTimeSpinBox::onEditTextChanged(const QString& text)
[public slot]
called when editor text changes
*/
void QextDateTimeSpinBox::onEditTextChanged(const QString& text) {
QDate date;
QString string=text;
int i=0;
//acceptable string in editor - update internal info, and update display
if(Validator->validate(string, i) == QValidator::Acceptable) {
Date.setYMD(Date.year(), Date.month(), Date.day());
refresh();
}
//intermediate string - do nothing
else if (Validator->validate(string, i) == QValidator::Intermediate) {
}
//unacceptable string in editor - update display with last valid date and time
else {
refresh();
}
}
/*!
\fn void QextDateTimeSpinBox::onFormatChanged(const QString& text)
[public slot]
called when format specifier string changes.
*/
void QextDateTimeSpinBox::onFormatChanged(const QString& text) {
QDate date;
QString string=text;
int i=0;
//acceptable string in editor - update internal info, and update display
if (Validator->validate(string, i) == QValidator::Acceptable) {
Date.setYMD(Date.year(), Date.month(), Date.day());
setFormat(text);
}
//intermediate string - do nothing
else if (Validator->validate(string, i) == QValidator::Intermediate) {
}
//unacceptable string in editor - update display with last valid date and time
else {
refresh();
}
}
/*!
\fn void QextDateTimeSpinBox::refresh()
refreshes the text in the editor
*/
void QextDateTimeSpinBox::refresh() {
//validator creates the display string
QString str("");
Validator->makeString(str, Date, Time);
//display string and place cursor at appropriate location
editor()->setText(str);
editor()->setCursorPosition(CursorPos);
}
/*!
\fn void QextDateTimeSpinBox::setDate(const QDate& date)
Sets the date used by the control to the one specified by the date argument.
*/
void QextDateTimeSpinBox::setDate(const QDate& date) {
Date=date;
}
/*!
\fn void QextDateTimeSpinBox::setDefaultStrings(void)
Sets up default day and month strings.
*/
void QextDateTimeSpinBox::setDefaultStrings(void) {
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -