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

📄 q3datetimeedit.cpp

📁 奇趣公司比较新的qt/emd版本
💻 CPP
📖 第 1 页 / 共 5 页
字号:
    return QWidget::event(e);}/*! \reimp*/void Q3DateTimeEditor::resizeEvent(QResizeEvent *e){    d->resize(e->size());    QWidget::resizeEvent(e);}/*! \reimp*/void Q3DateTimeEditor::paintEvent(QPaintEvent *){    QString txt;    for (uint i = 0; i < d->sectionCount(); ++i) {        txt += cw->sectionFormattedText(i);        if (i < d->sectionCount()-1) {            if (d->section(i+1).separator())                txt += d->separator();            else                txt += QLatin1Char(' ');        }    }    QPainter p(this);    const QBrush &bg = palette().brush(isEnabled() ? QPalette::Base : QPalette::Window);    p.fillRect(0, 0, width(), height(), bg);    d->paint(txt, hasFocus(), p, palette(), rect(), style());}/*!    Returns the section index at point \a p.*/int Q3DateTimeEditor::sectionAt(const QPoint &p){    return d->section(p);}int Q3DateTimeEditor::mapSection(int sec){    return d->mapSection(sec);}/*! \reimp*/void Q3DateTimeEditor::mousePressEvent(QMouseEvent *e){    QPoint p(e->pos().x(), 0);    int sec = sectionAt(p);    if (sec != -1) {        cw->setFocusSection(sec);        repaint(rect());    }}/*! \reimp*/bool Q3DateTimeEditor::eventFilter(QObject *o, QEvent *e){    if (o == this) {        if (e->type() == QEvent::KeyPress) {            QKeyEvent *ke = (QKeyEvent*)e;            switch (ke->key()) {            case Qt::Key_Right:                if (d->focusSection() < (int)d->sectionCount()-1) {                    if (cw->setFocusSection(focusSection()+1))                        repaint(rect());                }                return true;            case Qt::Key_Left:                if (d->focusSection() > 0) {                    if (cw->setFocusSection(focusSection()-1))                        repaint(rect());                }                return true;            case Qt::Key_Up:                cw->stepUp();                return true;            case Qt::Key_Down:                cw->stepDown();                return true;            case Qt::Key_Backspace:                if (qobject_cast<Q3DateEdit*>(cw))                    ((Q3DateEdit*)cw)->removeFirstNumber(d->focusSection());                else if (qobject_cast<Q3TimeEdit*>(cw))                    ((Q3TimeEdit*)cw)->removeFirstNumber(d->focusSection());                return true;            case Qt::Key_Delete:                cw->removeLastNumber(d->focusSection());                return true;            case Qt::Key_Tab:            case Qt::Key_BackTab: {                if (ke->state() == Qt::ControlButton)                    return false;                QWidget *w = this;                bool hadDateEdit = false;                while (w) {                    if (qobject_cast<QDateTimeSpinWidget*>(w) || qobject_cast<Q3DateTimeEdit*>(w))                        break;                    hadDateEdit = hadDateEdit || qobject_cast<Q3DateEdit*>(w);                    w = w->parentWidget();                }                if (w) {                    if (!qobject_cast<Q3DateTimeEdit*>(w)) {                        w = w->parentWidget();                    } else {                        Q3DateTimeEdit *ed = (Q3DateTimeEdit*)w;                        if (hadDateEdit && ke->key() == Qt::Key_Tab) {                            ed->timeEdit()->setFocus();                            return true;                        } else if (!hadDateEdit && ke->key() == Qt::Key_BackTab) {                            ed->dateEdit()->setFocus();                            return true;                        } else {                            while (w && !qobject_cast<Q3DateTimeEdit*>(w))                                w = w->parentWidget();                        }                    }                    qApp->sendEvent(w, e);                    return true;                }            } break;            default:                QString txt = ke->text().toLower();                if (!txt.isEmpty() && !separator().isEmpty() && txt[0] == separator()[0]) {                    // do the same thing as KEY_RIGHT when the user presses the separator key                    if (d->focusSection() < 2) {                        if (cw->setFocusSection(focusSection()+1))                            repaint(rect());                    }                    return true;                } else if (!txt.isEmpty() && qobject_cast<Q3TimeEdit*>(cw) && focusSection() == (int) d->sectionCount()-1) {                    // the first character of the AM/PM indicator toggles if the section has focus                    Q3TimeEdit *te = (Q3TimeEdit*)cw;                    QTime time = te->time();                    if (lAMPM && lAM && lPM && (te->display()&Q3TimeEdit::AMPM)) {                        if (txt[0] == (*lAM).toLower()[0] && time.hour() >= 12) {                            time.setHMS(time.hour()-12, time.minute(), time.second(), time.msec());                            te->setTime(time);                        } else if (txt[0] == (*lPM).toLower()[0] && time.hour() < 12) {                            time.setHMS(time.hour()+12, time.minute(), time.second(), time.msec());                            te->setTime(time);                        }                    }                }                int num = txt[0].digitValue();                if (num != -1) {                    cw->addNumber(d->focusSection(), num);                    return true;                }            }        }    }    return false;}/*!    Appends the number section \a sec to the editor.*/void Q3DateTimeEditor::appendSection(const QNumberSection& sec){    d->appendSection(sec);}/*!    Removes all sections from the editor.*/void Q3DateTimeEditor::clearSections(){    d->clearSections();}/*!    Sets the selection of \a sec to start at \a selstart and end at \a    selend.*/void Q3DateTimeEditor::setSectionSelection(int sec, int selstart, int selend){    d->setSectionSelection(sec, selstart, selend);}/*!    Sets the separator for all numbered sections to \a s. Note that    currently, only the first character of \a s is used.*/void Q3DateTimeEditor::setSeparator(const QString& s){    d->setSeparator(s);    update();}/*!    Returns the editor's separator.*/QString Q3DateTimeEditor::separator() const{    return d->separator();}/*!    Returns the number of the section that has focus.*/int Q3DateTimeEditor::focusSection() const{    return d->focusSection();}/*!    Sets the focus to section \a sec. If \a sec does not exist,    nothing happens.*/bool Q3DateTimeEditor::setFocusSection(int sec){    return d->setFocusSection(sec);}/*!    \class Q3DateTimeEditBase    \brief The Q3DateTimeEditBase class provides an abstraction for date and edit editors.    \compat    Small abstract class that provides some functions that are common    for both Q3DateEdit and Q3TimeEdit. It is used internally by    Q3DateTimeEditor.*//*!    \fn Q3DateTimeEditBase::Q3DateTimeEditBase(QWidget *, const char*)    \internal*//*!    \fn Q3DateTimeEditBase::setFocusSection(int)    \internal*//*! \fn QString Q3DateTimeEditBase::sectionFormattedText(int sec)    \internal  Pure virtual function which returns the formatted text of section \a  sec.*//*! \fn void Q3DateTimeEditBase::stepUp()    \internal  Pure virtual slot which is called whenever the user increases the  number in a section by pressing the widget's arrow buttons or the  keyboard's arrow keys.*//*! \fn void Q3DateTimeEditBase::stepDown()    \internal  Pure virtual slot which is called whenever the user decreases the  number in a section by pressing the widget's arrow buttons or the  keyboard's arrow keys.*//*! \fn void Q3DateTimeEditBase::addNumber(int sec, int num)    \internal  Pure virtual function which is called whenever the user types a number.  \a sec indicates the section where the number should be added. \a  num is the number that was pressed.*//*! \fn void Q3DateTimeEditBase::removeLastNumber(int sec)    \internal  Pure virtual function which is called whenever the user tries to  remove the last number from \a sec by pressing the delete key.*/////////////////class Q3DateEditPrivate{public:    int y;    int m;    int d;    // remembers the last entry for the day.    // if the day is 31 and you cycle through the months,    // the day will be 31 again if you reach a month with 31 days    // otherwise it will be the highest day in the month    int dayCache;    int yearSection;    int monthSection;    int daySection;    Q3DateEdit::Order ord;    bool overwrite;    bool adv;    int timerId;    bool typing;    QDate min;    QDate max;    bool changed;    Q3DateTimeEditor *ed;    Q3SpinWidget *controls;};/*!    \class Q3DateEdit q3datetimeedit.h    \brief The Q3DateEdit class provides a date editor.    \compat    Q3DateEdit allows the user to edit dates by using the keyboard or    the arrow keys to increase/decrease date values. The arrow keys    can be used to move from section to section within the Q3DateEdit    box. Dates appear in accordance with the local date/time settings    or in year, month, day order if the system doesn't provide this    information. It is recommended that the Q3DateEdit be initialised    with a date, e.g.    \code    Q3DateEdit *dateEdit = new Q3DateEdit(QDate::currentDate(), this);    dateEdit->setRange(QDate::currentDate().addDays(-365),                        QDate::currentDate().addDays( 365));    dateEdit->setOrder(Q3DateEdit::MDY);    dateEdit->setAutoAdvance(true);    \endcode    Here we've created a new Q3DateEdit object initialised with today's    date and restricted the valid date range to today plus or minus    365 days. We've set the order to month, day, year. If the auto    advance property is true (as we've set it here) when the user    completes a section of the date, e.g. enters two digits for the    month, they are automatically taken to the next section.    The maximum and minimum values for a date value in the date editor    default to the maximum and minimum values for a QDate. You can    change this by calling setMinValue(), setMaxValue() or setRange().    Terminology: A Q3DateEdit widget comprises three 'sections', one    each for the year, month and day. You can change the separator    character using Q3DateTimeEditor::setSeparator(), by default the    separator will be taken from the systems settings. If that is    not possible, it defaults to "-".    \img datetimewidgets.png Date Time Widgets    \sa QDate Q3TimeEdit Q3DateTimeEdit*//*!    \enum Q3DateEdit::Order    This enum defines the order in which the sections that comprise a    date appear.    \value MDY month-day-year    \value DMY day-month-year    \value YMD year-month-day (the default)    \omitvalue YDM*//*!    \enum Q3TimeEdit::Display    This enum defines the sections that comprise a time    \value Hours The hours section    \value Minutes The minutes section    \value Seconds The seconds section    \value AMPM The AM/PM section    The values can be or'ed together to show any combination.*//*!    Constructs an empty date editor which is a child of \a parent and    called name \a name.*/Q3DateEdit::Q3DateEdit(QWidget * parent, const char * name)    : Q3DateTimeEditBase(parent, name){    init();    updateButtons();}/*!    \overload    Constructs a date editor with the initial value \a date, parent \a    parent and called \a name.    The date editor is initialized with \a date.*/Q3DateEdit::Q3DateEdit(const QDate& date, QWidget * parent, const char * name)    : Q3DateTimeEditBase(parent, name){    init();    setDate(date);}/*! \internal*/void Q3DateEdit::init(){    d = new Q3DateEditPrivate();    d->controls = new QDateTimeSpinWidget(this, 0);    d->ed = new Q3DateTimeEditor(this, d->controls);    d->controls->setEditWidget(d->ed);    setFocusProxy(d->ed);    connect(d->controls, SIGNAL(stepUpPressed()), SLOT(stepUp()));    connect(d->controls, SIGNAL(stepDownPressed()), SLOT(stepDown()));    connect(this, SIGNAL(valueChanged(QDate)), SLOT(updateButtons()));    d->ed->appendSection(QNumberSection(0,4));    d->ed->appendSection(QNumberSection(5,7));    d->ed->appendSection(QNumberSection(8,10));    d->yearSection = -1;    d->monthSection = -1;    d->daySection = -1;    d->y = 0;    d->m = 0;    d->d = 0;    d->dayCache = 0;    setOrder(localOrder());    setFocusSection(0);    d->overwrite = true;    d->adv = false;    d->timerId = 0;    d->typing = false;    d->min = QDate(1752, 9, 14);    d->max = QDate(8000, 12, 31);    d->changed = false;    setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);    refcount++;}/*!    Destroys the object and frees any allocated resources.*/Q3DateEdit::~Q3DateEdit(){    delete d;    if (!--refcount)        cleanup();}/*!    \property Q3DateEdit::minValue    \brief the editor's minimum value    Setting the minimum date value is equivalent to calling    Q3DateEdit::setRange(\e d, maxValue()), where \e d is the minimum    date. The default minimum date is 1752-09-14.    \sa maxValue setRange()*/QDate Q3DateEdit::minValue() const{    return d->min;}/*!    \property Q3DateEdit::maxValue    \brief the editor's maximum value    Setting the maximum date value for the editor is equivalent to    calling Q3DateEdit::setRange(minValue(), \e d), where \e d is the    maximum date. The default maximum date is 8000-12-31.    \sa minValue setRange()*/QDate Q3DateEdit::maxValue() const{    return d->max;}/*!    Sets the valid input range for the editor to be from \a min to \a    max inclusive. If \a min is invalid no minimum date will be set.    Similarly, if \a max is invalid no maximum date will be set.*/void Q3DateEdit::setRange(const QDate& min, const QDate& max){    if (min.isValid())        d->min = min;

⌨️ 快捷键说明

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