⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qextdatetimespinbox.cpp

📁 Linux/Windows 环境下的跨平台开发程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
    QStringList days;
    QStringList months;
    days+="Monday";
    days+="Tuesday";
    days+="Wednesday";
    days+="Thursday";
    days+="Friday";
    days+="Saturday";
    days+="Sunday";
    months+="January";
    months+="February";
    months+="March";
    months+="April";
    months+="May";
    months+="June";
    months+="July";
    months+="August";
    months+="September";
    months+="October";
    months+="November";
    months+="December";
    setStrings(days, months);
}

/*!
\fn int QextDateTimeSpinBox::setFormat(QString& format)
Sets the format of the displayed date and/or time, using the format specifiers described in the
documentation for 
QextDateTimeSpinBox::QextDateTimeSpinBox(const QString*, const QDate*, const QTime*, 
                                         const QStringList*, const QStringList*).
*/
void QextDateTimeSpinBox::setFormat(const QString& format) {
    Validator->setFormat(format);
    refresh();
}

/*!
\fn void QextDateTimeSpinBox::setStrings(const QStringList* days, const QStringList* months)
Sets the strings used to represent the daysof the week and the months of the year.  By 
default these strings are English.
*/
void QextDateTimeSpinBox::setStrings(const QStringList& days, const QStringList& months) {
    setLongMonthNames(months);
    setLongDayNames(days);
}

/*!
\fn void QextDateTimeSpinBox::setTime(const QTime& time)
Sets the time used by the control to the one specified by the time argument.
*/
void QextDateTimeSpinBox::setTime(const QTime& time) {
    Time=time;
}

/*!
\fn void QextDateTimeSpinBox::stepDown()
[public slot]
responds to the down arrow of the spin box control
*/
void QextDateTimeSpinBox::stepDown() {
    QDate TempDate;
    int hour=Time.hour();
    int minute=Time.minute();
    int second=Time.second();
    int year=Date.year();
    int month=Date.month();
    int day=Date.day();
    int cursor=editor()->cursorPosition();
    CursorPos=cursor;

    //find which field the cursor is currently in
    QextDateTimeValidator::Token const *Field=fieldFromIndex(cursor);
    
    //if cursor is at end of string, do nothing
    if (!Field) {
        return;
    }
    switch(Field->FType) {
        case QextDateTimeValidator::FIELD_LITERAL:
            break;

        case QextDateTimeValidator::FIELD_AMPM:
            hour+=12;
            hour%=24;
            Time.setHMS(hour, minute, second);
            break;

        case QextDateTimeValidator::FIELD_DAY:
        case QextDateTimeValidator::FIELD_DAY_STRING:
            day--;

            //edge case
            if (day<1) {
                TempDate.setYMD(year, month, 1);
                day=TempDate.daysInMonth();
#ifdef QTVER_PRE_30
                editor()->cursorRight(FALSE);
#else
                editor()->cursorForward(FALSE);
#endif
            }
            Date.setYMD(year, month, day);

            //cursor tracks beginning of field
            if (day==9) {
#ifdef QTVER_PRE_30
                editor()->cursorLeft(FALSE);
#else
                editor()->cursorBackward(FALSE);
#endif
            }
            break;
        
        case QextDateTimeValidator::FIELD_HOUR_12:
        case QextDateTimeValidator::FIELD_HOUR_24:
            hour--;
            if (hour<0) {
                hour=23;
            }
            Time.setHMS(hour, minute, second);
            break;

        case QextDateTimeValidator::FIELD_MINUTE:
            minute--;
            if (minute<0) {
                minute=59;
            }
            Time.setHMS(hour, minute, second);
            break;

        case QextDateTimeValidator::FIELD_MONTH:
        case QextDateTimeValidator::FIELD_MONTH_STRING:
            month--;
            if (month<1) {
                month=12;
            }
            TempDate.setYMD(year, month, 1);
            if (day>TempDate.daysInMonth()) {
                day=TempDate.daysInMonth();
            }
            Date.setYMD(year, month, day);
            break;

        case QextDateTimeValidator::FIELD_SECOND:
            second--;
            if (second<0) {
                second=59;
            }
            Time.setHMS(hour, minute, second);
            break;

        case QextDateTimeValidator::FIELD_YEAR_2:
        case QextDateTimeValidator::FIELD_YEAR_4:
            year--;

            //QDate edge case
            if (year<1753) {
                year=1753;
            }

            //special case for February in leap-years
            if (month==2 && day==29) {
                day=28;
            }
            Date.setYMD(year, month, day);
            break;
    }

    //update display in the editor
    refresh();

    //move cursor to beginning of current field for use with variable-sized fields
    CursorPos=indexFromField(Field);
    editor()->setCursorPosition(CursorPos);
}

/*!
\fn void QextDateTimeSpinBox::stepUp()
[public slot]
responds to the up arrow of the spin box control
*/
void QextDateTimeSpinBox::stepUp() {
    QDate TempDate;
    int hour=Time.hour();
    int minute=Time.minute();
    int second=Time.second();
    int year=Date.year();
    int month=Date.month();
    int day=Date.day();
    int cursor=editor()->cursorPosition();
    CursorPos=cursor;

    //find which field the cursor is currently in
    QextDateTimeValidator::Token const *Field=fieldFromIndex(cursor);
    
    //if cursor is at end of string, do nothing
     if (!Field) {
        return;
    }
    
    //update the field if necessary
    switch(Field->FType) {
        case QextDateTimeValidator::FIELD_LITERAL:
            break;

        case QextDateTimeValidator::FIELD_AMPM:
            hour+=12;
            hour%=24;
            Time.setHMS(hour, minute, second);
            break;

        case QextDateTimeValidator::FIELD_DAY:
        case QextDateTimeValidator::FIELD_DAY_STRING:
            day++;

            //edge case
            if (day>Date.daysInMonth()) {
                day=1;
#ifdef QTVER_PRE_30
                editor()->cursorLeft(FALSE);
#else
                editor()->cursorBackward(FALSE);
#endif
            }
            Date.setYMD(year, month, day);
            if (day==10) {
#ifdef QTVER_PRE_30
                editor()->cursorRight(FALSE);
#else
                editor()->cursorForward(FALSE);
#endif
            }
            break;
        
        case QextDateTimeValidator::FIELD_HOUR_12:
        case QextDateTimeValidator::FIELD_HOUR_24:
            hour++;
            hour%=24;
            Time.setHMS(hour, minute, second);
            break;

        case QextDateTimeValidator::FIELD_MINUTE:
            minute++;
            if (minute>59) {
                minute=0;
            }
            Time.setHMS(hour, minute, second);
            break;

        case QextDateTimeValidator::FIELD_MONTH:
        case QextDateTimeValidator::FIELD_MONTH_STRING:
            month++;
            if (month>12) {
                month=1;
            }
            TempDate.setYMD(year, month, 1);
            if (day>TempDate.daysInMonth()) {
                day=TempDate.daysInMonth();
            }
            Date.setYMD(year, month, day);
            break;

        case QextDateTimeValidator::FIELD_SECOND:
            second++;
            if (second>59) {
                second=0;
            }
            Time.setHMS(hour, minute, second);
            break;

        case QextDateTimeValidator::FIELD_YEAR_2:
        case QextDateTimeValidator::FIELD_YEAR_4:
            year++;

            //QDate edge case
            if (year>8000) {
                year=8000;
            }

            //special case for February in leap-years
            if (month==2 && day==29) {
                day=28;
            }
            Date.setYMD(year, month, day);
            break;
    }

    //update display in the editor
    refresh();

    //move cursor to beginning of current field for use with variable-sized fields
    CursorPos=indexFromField(Field);
    editor()->setCursorPosition(CursorPos);
}

/*!
\fn QTime QextDateTimeSpinBox::time(void) const
Returns the most recent valid time entered into the control.  
*/
QTime QextDateTimeSpinBox::time(void) const {
    return Time;
}

/*!
\fn void QextDateTimeSpinBox::updateDisplay()
override QSpinBox::updateDisplay() to keep the cursor from moving all around the edit box
on mouseover events
*/
void QextDateTimeSpinBox::updateDisplay() {
    CursorPos=editor()->cursorPosition();
    QSpinBox::updateDisplay();
    editor()->setCursorPosition(CursorPos);
}

/*!
\fn QextDateTimeValidator* QextDateTimeSpinBox::validator(void) const
returns date validator pointer
*/
QextDateTimeValidator* QextDateTimeSpinBox::validator(void) const {
    return Validator;
}


/*!
\fn QString QextDateTimeSpinBox::longMonthName(int monthNum) const
returns the month name for the month specified.  By default, longMonthName(1) will return 
"January", etc.
*/
QString QextDateTimeSpinBox::longMonthName(int monthNum) const {
    return monthNames[monthNum-1];    
}

/*!
\fn QString QextDateTimeSpinBox::longDayName(int dayNum) const
returns the day name for the day of the week specified.  By default longDayName(1) will return 
"Monday", etc.  Note that this is different from the American week which starts with Sunday.  This
is to maintain consistency with the QT 3.0 function QDate::longDayName().
*/
QString QextDateTimeSpinBox::longDayName(int dayNum) const {
    return dayNames[dayNum-1];    
}

/*!
\fn void QextDateTimeSpinBox::setLongMonthNames(const QStringList&)
Sets the names of the months.  The QStringList reference passed in is assumed to have at least 12
QStrings in it; any after the first 12 will be ignored.
*/
void QextDateTimeSpinBox::setLongMonthNames(const QStringList& names) {
    QStringList::ConstIterator i=names.begin();
    int j;
    for (j=0; j<12; j++) {
        monthNames[j]=*i;
        i++;
    }
}

/*!
\fn void QextDateTimeSpinBox::setLongDayNames(const QStringList&)
Sets the names of days of the week.  Note that Monday is assumed to map to the first element, and 
Sunday to the last.  This is for consistency with the behavior of QDate.  The QStringList passed 
in is assumed to have at least 7 elements.  Any elements beyond the first 7 are ignored.
*/
void QextDateTimeSpinBox::setLongDayNames(const QStringList& names) {
    QStringList::ConstIterator i=names.begin();
    int j;
    for (j=0; j<7; j++) {
        dayNames[j]=*i;
        i++;
    }
}
    

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -