6#include <qabstractitemmodel.h>
7#include <qstyleditemdelegate.h>
12#include <qtextformat.h>
13#include <qheaderview.h>
14#include <private/qwidget_p.h>
15#include <qpushbutton.h>
16#include <qtoolbutton.h>
20#include <qapplication.h>
21#include <private/qapplication_p.h>
22#include <qbasictimer.h>
23#include <qstylepainter.h>
30using namespace Qt::StringLiterals;
42 return QString::number(number).rightJustified(fieldWidth, u'0');
60 virtual QDate
applyToDate(QDate date, QCalendar cal = QCalendar())
const = 0;
61 virtual void setDate(QDate date, QCalendar cal = QCalendar()) = 0;
62 virtual QString
text()
const = 0;
63 virtual QString
text(QDate date, QCalendar cal,
int repeat)
const = 0;
74 return "<b>"_L1 + str +
"</b>"_L1;
75 int startPos = str.size() - pos;
76 return QStringView{str}.mid(0, startPos) +
"<b>"_L1 + QStringView{str}.mid(startPos, pos) +
"</b>"_L1;
86 virtual QDate
applyToDate(QDate date, QCalendar cal)
const override;
87 virtual void setDate(QDate date, QCalendar cal)
override;
88 virtual QString
text()
const override;
89 virtual QString
text(QDate date, QCalendar cal,
int repeat)
const override;
103 if (key == Qt::Key_Right || key == Qt::Key_Left) {
106 }
else if (key == Qt::Key_Up) {
112 }
else if (key == Qt::Key_Down) {
118 }
else if (key == Qt::Key_Back || key == Qt::Key_Backspace) {
133 if (key < Qt::Key_0 || key > Qt::Key_9)
135 int pressedKey = key - Qt::Key_0;
139 m_day = m_day % 10 * 10 + pressedKey;
152 auto parts = cal.partsFromDate(date);
153 if (!parts.isValid())
155 parts.day = qMin(qMax(1, m_day), cal.daysInMonth(parts.month, parts.year));
156 return cal.dateFromParts(parts);
161 m_day = m_oldDay = date.day(cal);
167 return highlightString(formatNumber(m_day, 2), m_pos);
173 return QString::number(date.day(cal));
174 }
else if (repeat == 2) {
175 return formatNumber(date.day(cal), 2);
176 }
else if (repeat == 3) {
177 return m_locale.dayName(date.dayOfWeek(cal), QLocale::ShortFormat);
179 return m_locale.dayName(date.dayOfWeek(cal), QLocale::LongFormat);
191 virtual QDate
applyToDate(QDate date, QCalendar cal)
const override;
192 virtual void setDate(QDate date, QCalendar cal)
override;
193 virtual QString
text()
const override;
194 virtual QString
text(QDate date, QCalendar cal,
int repeat)
const override;
208 if (key == Qt::Key_Right || key == Qt::Key_Left) {
211 }
else if (key == Qt::Key_Up) {
217 }
else if (key == Qt::Key_Down) {
223 }
else if (key == Qt::Key_Back || key == Qt::Key_Backspace) {
229 m_month = m_oldMonth;
231 m_month = m_month / 10;
238 if (key < Qt::Key_0 || key > Qt::Key_9)
240 int pressedKey = key - Qt::Key_0;
242 m_month = pressedKey;
244 m_month = m_month % 10 * 10 + pressedKey;
257 auto parts = cal.partsFromDate(date);
258 if (!parts.isValid())
260 parts.month = qMin(qMax(1, m_month), cal.monthsInYear(parts.year));
261 parts.day = qMin(parts.day, cal.daysInMonth(m_month, parts.year));
262 return cal.dateFromParts(parts);
267 m_month = m_oldMonth = date.month(cal);
273 return highlightString(formatNumber(m_month, 2), m_pos);
278 const auto parts = cal.partsFromDate(date);
281 return QString::number(parts.month);
283 return formatNumber(parts.month, 2);
286 return cal.standaloneMonthName(m_locale, parts.month, parts.year, QLocale::ShortFormat);
288 return cal.standaloneMonthName(m_locale, parts.month, parts.year, QLocale::LongFormat);
299 virtual QDate
applyToDate(QDate date, QCalendar cal)
const override;
300 virtual void setDate(QDate date, QCalendar cal)
override;
301 virtual QString
text()
const override;
302 virtual QString
text(QDate date, QCalendar cal,
int repeat)
const override;
320 for (
int i = 0; i < n; i++)
327 if (key == Qt::Key_Right || key == Qt::Key_Left) {
330 }
else if (key == Qt::Key_Up) {
334 }
else if (key == Qt::Key_Down) {
338 }
else if (key == Qt::Key_Back || key == Qt::Key_Backspace) {
343 int pow = pow10(m_pos);
344 m_year = m_oldYear / pow * pow + m_year % (pow * 10) / 10;
350 if (key < Qt::Key_0 || key > Qt::Key_9)
352 int pressedKey = key - Qt::Key_0;
353 int pow = pow10(m_pos);
354 m_year = m_year / (pow * 10) * (pow * 10) + m_year % pow * 10 + pressedKey;
365 auto parts = cal.partsFromDate(date);
366 if (!parts.isValid())
369 parts.year = qMax(1, m_year);
370 parts.day = qMin(parts.day, cal.daysInMonth(parts.month, parts.year));
371 return cal.dateFromParts(parts);
376 m_year = m_oldYear = date.year(cal);
382 return highlightString(formatNumber(m_year, 4), m_pos);
388 return formatNumber(date.year(cal) % 100, 2);
389 return QString::number(date.year(cal));
423 void toPreviousToken();
424 void applyToDate(QCalendar cal);
426 int countRepeat(
const QString &str,
int index)
const;
429 QStringList m_separators;
430 std::vector<SectionToken> m_tokens;
444 : m_currentToken(-1),
453 m_yearValidator.m_locale = locale;
454 m_monthValidator.m_locale = locale;
455 m_dayValidator.m_locale = locale;
466 Q_ASSERT(index >= 0 && index < str.size());
468 const QChar ch = str.at(index);
469 while (index + count < str.size() && str.at(index + count) == ch)
476 m_yearValidator.setDate(date, cal);
477 m_monthValidator.setDate(date, cal);
478 m_dayValidator.setDate(date, cal);
479 m_initialDate = date;
480 m_currentDate = date;
487 const int numSeps = m_separators.size();
488 const int numTokens =
int(m_tokens.size());
489 for (
int i = 0; i < numSeps; ++i) {
490 str += m_separators.at(i);
493 if (i == m_currentToken)
496 str += token.validator->text(m_currentDate, cal, token.repeat);
505 m_separators.clear();
515 const auto quote = u'\'';
516 bool quoting =
false;
518 while (pos < format.size()) {
519 const QStringView mid = QStringView{format}.mid(pos);
522 if (mid.startsWith(quote)) {
525 const QChar nextChar = format.at(pos);
527 separator += nextChar;
531 if (nextChar == u'd') {
532 offset = qMin(4, countRepeat(format, pos));
533 validator = &m_dayValidator;
534 }
else if (nextChar == u'M') {
535 offset = qMin(4, countRepeat(format, pos));
536 validator = &m_monthValidator;
537 }
else if (nextChar == u'y') {
538 offset = qMin(4, countRepeat(format, pos));
539 validator = &m_yearValidator;
541 separator += nextChar;
544 m_tokens.push_back(SectionToken(validator, offset));
545 m_separators.append(separator);
546 separator = QString();
547 if (m_currentToken < 0)
548 m_currentToken =
int(m_tokens.size()) - 1;
555 m_separators += separator;
560 m_currentDate = m_yearValidator.applyToDate(m_currentDate, cal);
561 m_currentDate = m_monthValidator.applyToDate(m_currentDate, cal);
562 m_currentDate = m_dayValidator.applyToDate(m_currentDate, cal);
567 if (m_currentToken < 0)
570 m_currentToken %= m_tokens.size();
575 if (m_currentToken < 0)
578 m_currentToken %= m_tokens.size();
632 void updateDateLabel();
633 void createDateLabel();
634 void removeDateLabel();
638 QBasicTimer m_acceptTimer;
644 const QCalendar m_calendar;
667 m_acceptTimer.start(m_editDelay,
this);
669 m_dateText->setText(m_dateValidator->currentText(m_calendar));
671 QSize s = m_dateFrame->sizeHint();
672 QRect r = m_widget->geometry();
673 QRect newRect((r.width() - s.width()) / 2, (r.height() - s.height()) / 2, s.width(), s.height());
674 m_dateFrame->setGeometry(newRect);
677 QPalette p = m_dateFrame->palette();
678 p.setBrush(QPalette::Window, m_dateFrame->window()->palette().brush(QPalette::Window));
679 m_dateFrame->setPalette(p);
681 m_dateFrame->raise();
687 QDate date = m_dateValidator->currentDate();
692 emit dateChanged(date);
699 m_dateFrame =
new QFrame(m_widget);
700 QVBoxLayout *vl =
new QVBoxLayout;
701 m_dateText =
new QLabel;
702 vl->addWidget(m_dateText);
703 m_dateFrame->setLayout(vl);
704 m_dateFrame->setFrameShadow(QFrame::Plain);
705 m_dateFrame->setFrameShape(QFrame::Box);
707 m_dateValidator->setLocale(m_widget->locale());
708 m_dateValidator->setFormat(m_widget->locale().dateFormat(QLocale::ShortFormat));
709 m_dateValidator->setInitialDate(m_date, m_calendar);
711 m_dateFrame->setAutoFillBackground(
true);
712 m_dateFrame->setBackgroundRole(QPalette::Window);
719 m_acceptTimer.stop();
721 m_dateFrame->deleteLater();
722 delete m_dateValidator;
723 m_dateFrame =
nullptr;
724 m_dateText =
nullptr;
725 m_dateValidator =
nullptr;
731 if (e->type() == QEvent::KeyPress || e->type() == QEvent::KeyRelease) {
732 QKeyEvent *ke =
static_cast<QKeyEvent *>(e);
733 if ((ke->text().size() > 0 && ke->text().at(0).isPrint()) || m_dateFrame) {
734 if (ke->key() == Qt::Key_Return || ke->key() == Qt::Key_Enter || ke->key() == Qt::Key_Select) {
736 emit editingFinished();
738#if QT_CONFIG(shortcut)
739 }
else if (ke->matches(QKeySequence::Cancel)) {
742 }
else if (e->type() == QEvent::KeyPress) {
744 m_dateValidator->handleKeyEvent(ke, m_calendar);
753
754
755
756
757
758
759
762 return QObject::eventFilter(o,e);
767 if (e->timerId() == m_acceptTimer.timerId()) {
790QT_WARNING_DISABLE_MSVC(4351)
829 if (parent.isValid())
836 if (parent.isValid())
879 return m_calendar.standaloneMonthName(locale, month, m_shownYear, QLocale::LongFormat);
920#if QT_CONFIG(wheelevent)
930 bool validDateClicked;
931#ifdef QT_KEYPAD_NAVIGATION
937 : QAbstractTableModel(parent),
940 m_date(QDate::currentDate()),
941 m_minimumDate(QDate::fromJulianDay(1)),
942 m_maximumDate(9999, 12, 31),
943 m_shownYear(m_date.year(m_calendar)),
944 m_shownMonth(m_date.month(m_calendar)),
945 m_firstDay(QLocale().firstDayOfWeek()),
946 m_horizontalHeaderFormat(QCalendarWidget::ShortDayNames),
955 if (col < 0 || col > 6)
957 int day = m_firstDay + col;
960 return Qt::DayOfWeek(day);
965 if (day < 1 ||
unsigned(day) >
unsigned(7))
967 int column = (
int)day - (
int)m_firstDay;
974
975
976
977
978
979
980
981
982
983
984
989 while (refDay <= 31) {
990 QDate refDate(m_shownYear, m_shownMonth, refDay, m_calendar);
991 if (refDate.isValid())
1000 return (columnForDayOfWeek(
static_cast<Qt::DayOfWeek>(m_calendar.dayOfWeek(date)))
1001 - (date.day(m_calendar) % 7) + 8) % 7;
1009 const QDate refDate = referenceDate();
1010 if (!refDate.isValid())
1017 const int requestedDay =
1018 7 * (row - m_firstRow) + column - columnForFirstOfShownMonth - refDate.day(m_calendar) + 1;
1019 return refDate.addDays(requestedDay);
1024 if (!row && !column)
1032 const QDate refDate = referenceDate();
1033 if (!refDate.isValid())
1037 const int requestedPosition = (refDate.daysTo(date) - m_firstColumn +
1038 columnForFirstOfShownMonth + refDate.day(m_calendar) - 1);
1040 int c = requestedPosition % 7;
1041 int r = requestedPosition / 7;
1061 switch (m_horizontalHeaderFormat) {
1062 case QCalendarWidget::SingleLetterDayNames: {
1063 QString standaloneDayName = m_view->locale().standaloneDayName(day, QLocale::NarrowFormat);
1064 if (standaloneDayName == m_view->locale().dayName(day, QLocale::NarrowFormat))
1065 return standaloneDayName.left(1);
1066 return standaloneDayName;
1068 case QCalendarWidget::ShortDayNames:
1069 return m_view->locale().dayName(day, QLocale::ShortFormat);
1070 case QCalendarWidget::LongDayNames:
1071 return m_view->locale().dayName(day, QLocale::LongFormat);
1081 QPalette::ColorGroup cg = QPalette::Active;
1082 QTextCharFormat format;
1086 if (!m_view->isEnabled())
1087 cg = QPalette::Disabled;
1088 else if (!m_view->isActiveWindow())
1089 cg = QPalette::Inactive;
1090 format.setFont(
m_view->font());
1093 bool header = (m_weekNumbersShown && col == HeaderColumn)
1094 || (m_horizontalHeaderFormat != QCalendarWidget::NoHorizontalHeader && row == HeaderRow);
1095 format.setBackground(pal.brush(cg, header ? QPalette::AlternateBase : QPalette::Base));
1096 format.setForeground(pal.brush(cg, QPalette::Text));
1098 format.merge(m_headerFormat);
1102 Qt::DayOfWeek dayOfWeek = dayOfWeekForColumn(col);
1103 if (m_dayFormats.contains(dayOfWeek))
1104 format.merge(m_dayFormats.value(dayOfWeek));
1108 QDate date = dateForCell(row, col);
1109 format.merge(m_dateFormats.value(date));
1110 if (date < m_minimumDate || date > m_maximumDate)
1111 format.setBackground(pal.brush(cg, QPalette::Window));
1112 if (m_shownMonth != date.month(m_calendar))
1113 format.setForeground(pal.brush(QPalette::Disabled, QPalette::Text));
1120 if (role == Qt::TextAlignmentRole)
1121 return (
int) Qt::AlignCenter;
1123 int row = index.row();
1124 int column = index.column();
1126 if (role == Qt::DisplayRole) {
1129 QDate date = dateForCell(row, columnForDayOfWeek(Qt::Monday));
1131 return date.weekNumber();
1133 if (m_horizontalHeaderFormat != QCalendarWidget::NoHorizontalHeader && row == HeaderRow
1134 && column >= m_firstColumn && column < m_firstColumn + ColumnCount)
1135 return dayName(dayOfWeekForColumn(column));
1136 QDate date = dateForCell(row, column);
1138 return date.day(m_calendar);
1142 QTextCharFormat fmt = formatForCell(row, column);
1143 if (role == Qt::BackgroundRole)
1144 return fmt.background().color();
1145 if (role == Qt::ForegroundRole)
1146 return fmt.foreground().color();
1147 if (role == Qt::FontRole)
1149 if (role == Qt::ToolTipRole)
1150 return fmt.toolTip();
1156 QDate date = dateForCell(index.row(), index.column());
1157 if (!date.isValid())
1158 return QAbstractTableModel::flags(index);
1159 if (date < m_minimumDate)
1161 if (date > m_maximumDate)
1163 return QAbstractTableModel::flags(index);
1169 if (m_date < m_minimumDate)
1170 m_date = m_minimumDate;
1171 else if (m_date > m_maximumDate)
1172 m_date = m_maximumDate;
1178 m_shownYear = m_date.year(c);
1179 m_shownMonth = m_date.month(c);
1202 if (!d.isValid() || d == m_minimumDate)
1206 if (m_maximumDate < m_minimumDate)
1207 m_maximumDate = m_minimumDate;
1208 if (m_date < m_minimumDate)
1209 m_date = m_minimumDate;
1215 if (!d.isValid() || d == m_maximumDate)
1219 if (m_minimumDate > m_maximumDate)
1220 m_minimumDate = m_maximumDate;
1221 if (m_date > m_maximumDate)
1222 m_date = m_maximumDate;
1228 m_minimumDate = min;
1229 m_maximumDate = max;
1230 if (m_minimumDate > m_maximumDate)
1231 qSwap(m_minimumDate, m_maximumDate);
1232 if (m_date < m_minimumDate)
1233 m_date = m_minimumDate;
1234 if (m_date > m_maximumDate)
1235 m_date = m_maximumDate;
1243 emit dataChanged(begin, end);
1244 emit headerDataChanged(Qt::Vertical, 0, m_firstRow + RowCount - 1);
1245 emit headerDataChanged(Qt::Horizontal, 0, m_firstColumn + ColumnCount - 1);
1250 if (m_horizontalHeaderFormat == format)
1253 int oldFormat = m_horizontalHeaderFormat;
1254 m_horizontalHeaderFormat = format;
1255 if (oldFormat == QCalendarWidget::NoHorizontalHeader) {
1259 }
else if (m_horizontalHeaderFormat == QCalendarWidget::NoHorizontalHeader) {
1269 if (m_firstDay == dayOfWeek)
1272 m_firstDay = dayOfWeek;
1305 : QTableView(parent),
1307 validDateClicked(
false)
1309 setTabKeyNavigation(
false);
1311 verticalHeader()->setVisible(
false);
1312 horizontalHeader()->setVisible(
false);
1313 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
1314 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
1321 return QTableView::moveCursor(cursorAction, modifiers);
1323 QCalendar cal = calendarModel->calendar();
1326 return currentIndex();
1329 QDate currentDate =
static_cast<QCalendarModel*>(model())->dateForCell(index.row(), index.column());
1330 switch (cursorAction) {
1331 case QAbstractItemView::MoveUp:
1332 currentDate = currentDate.addDays(-7);
1334 case QAbstractItemView::MoveDown:
1335 currentDate = currentDate.addDays(7);
1337 case QAbstractItemView::MoveLeft:
1338 currentDate = currentDate.addDays(isRightToLeft() ? 1 : -1);
1340 case QAbstractItemView::MoveRight:
1341 currentDate = currentDate.addDays(isRightToLeft() ? -1 : 1);
1343 case QAbstractItemView::MoveHome: {
1344 auto parts = cal.partsFromDate(currentDate);
1345 if (parts.isValid()) {
1347 currentDate = cal.dateFromParts(parts);
1351 case QAbstractItemView::MoveEnd: {
1352 auto parts = cal.partsFromDate(currentDate);
1353 if (parts.isValid()) {
1354 parts.day = cal.daysInMonth(parts.month, parts.year);
1355 currentDate = cal.dateFromParts(parts);
1359 case QAbstractItemView::MovePageUp:
1360 currentDate = currentDate.addMonths(-1, cal);
1362 case QAbstractItemView::MovePageDown:
1363 currentDate = currentDate.addMonths(1, cal);
1365 case QAbstractItemView::MoveNext:
1366 case QAbstractItemView::MovePrevious:
1367 return currentIndex();
1371 emit changeDate(currentDate,
true);
1372 return currentIndex();
1377#ifdef QT_KEYPAD_NAVIGATION
1410#if QT_CONFIG(wheelevent)
1425#ifdef QT_KEYPAD_NAVIGATION
1426 if (event->type() == QEvent::FocusIn) {
1427 if (QCalendarModel *calendarModel = qobject_cast<QCalendarModel *>(model())) {
1428 origDate = calendarModel->m_date;
1433 return QTableView::event(event);
1442 QPoint pos = event->position().toPoint();
1444 QDate date = calendarModel->dateForCell(index.row(), index.column());
1445 if (date.isValid() && date >= calendarModel->m_minimumDate
1446 && date <= calendarModel->m_maximumDate) {
1455 if (!calendarModel) {
1456 QTableView::mouseDoubleClickEvent(event);
1463 QDate date = handleMouseEvent(event);
1464 validDateClicked =
false;
1465 if (date == calendarModel->m_date && !style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick)) {
1466 emit editingFinished();
1473 if (!calendarModel) {
1474 QTableView::mousePressEvent(event);
1481 if (event->button() != Qt::LeftButton)
1484 QDate date = handleMouseEvent(event);
1485 if (date.isValid()) {
1486 validDateClicked =
true;
1487 int row = -1, col = -1;
1489 if (row != -1 && col != -1) {
1490 selectionModel()->setCurrentIndex(model()->index(row, col), QItemSelectionModel::NoUpdate);
1493 validDateClicked =
false;
1501 if (!calendarModel) {
1502 QTableView::mouseMoveEvent(event);
1509 if (validDateClicked) {
1510 QDate date = handleMouseEvent(event);
1511 if (date.isValid()) {
1512 int row = -1, col = -1;
1514 if (row != -1 && col != -1) {
1515 selectionModel()->setCurrentIndex(model()->index(row, col), QItemSelectionModel::NoUpdate);
1526 if (!calendarModel) {
1527 QTableView::mouseReleaseEvent(event);
1531 if (event->button() != Qt::LeftButton)
1537 if (validDateClicked) {
1538 QDate date = handleMouseEvent(event);
1539 if (date.isValid()) {
1540 emit changeDate(date,
true);
1542 if (style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick))
1543 emit editingFinished();
1545 validDateClicked =
false;
1560 void paintCell(QPainter *painter,
const QRect &rect, QDate date)
const;
1564 mutable QStyleOptionViewItem storedOption;
1579 QStyleOptionToolButton opt;
1580 initStyleOption(&opt);
1582 if (opt.state & QStyle::State_MouseOver || isDown()) {
1584 setPalette(QPalette());
1587 QPalette toolPalette = palette();
1588 toolPalette.setColor(QPalette::ButtonText, toolPalette.color(QPalette::HighlightedText));
1589 setPalette(toolPalette);
1592 QToolButton::paintEvent(e);
1628 Q_DECLARE_PUBLIC(QCalendarWidget)
1634 void paintCell(QPainter *painter,
const QRect &rect, QDate date)
const;
1677void QCalendarDelegate::paint(QPainter *painter,
const QStyleOptionViewItem &option,
1680 QDate date = calendarWidgetPrivate->m_model->dateForCell(index.row(), index.column());
1681 if (date.isValid()) {
1682 storedOption = option;
1683 QRect rect = option.rect;
1684 calendarWidgetPrivate->paintCell(painter, rect, date);
1686 QStyledItemDelegate::paint(painter, option, index);
1690void QCalendarDelegate::
paintCell(QPainter *painter,
const QRect &rect, QDate date)
const
1692 storedOption.rect = rect;
1697 QStyledItemDelegate::paint(painter, storedOption, idx);
1706 m_selection =
nullptr;
1710 oldFocusPolicy = Qt::StrongFocus;
1715 Q_Q(QCalendarWidget);
1717 bool navigatorEnabled = (
m_navigator->widget() !=
nullptr);
1718 if (enable == navigatorEnabled)
1722 m_navigator->setWidget(q);
1723 q->connect(m_navigator, SIGNAL(dateChanged(QDate)),
1724 q, SLOT(_q_slotChangeDate(QDate)));
1725 q->connect(m_navigator, SIGNAL(editingFinished()),
1726 q, SLOT(_q_editingFinished()));
1730 q->disconnect(m_navigator, SIGNAL(dateChanged(QDate)),
1731 q, SLOT(_q_slotChangeDate(QDate)));
1732 q->disconnect(m_navigator, SIGNAL(editingFinished()),
1733 q, SLOT(_q_editingFinished()));
1740 Q_Q(QCalendarWidget);
1741 navBarBackground =
new QWidget(widget);
1742 navBarBackground->setObjectName(
"qt_calendar_navigationbar"_L1);
1743 navBarBackground->setAutoFillBackground(
true);
1744 navBarBackground->setBackgroundRole(QPalette::Highlight);
1746 prevMonth =
new QPrevNextCalButton(navBarBackground);
1747 nextMonth =
new QPrevNextCalButton(navBarBackground);
1748 prevMonth->setAutoRaise(
true);
1749 nextMonth->setAutoRaise(
true);
1750 prevMonth->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Minimum);
1751 nextMonth->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Minimum);
1752 nextMonth->setAutoRaise(
true);
1754 prevMonth->setAutoRepeat(
true);
1755 nextMonth->setAutoRepeat(
true);
1757 monthButton =
new QCalToolButton(navBarBackground);
1758 monthButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Minimum);
1760 monthButton->setPopupMode(QToolButton::InstantPopup);
1761 monthMenu =
new QMenu(monthButton);
1762 for (
int i = 1, e =
m_model->m_calendar.maximumMonthsInYear(); i <= e; i++) {
1763 QString monthName(m_model->monthName(q->locale(), i));
1764 QAction *act =
monthMenu->addAction(monthName);
1766 monthToAction[i] = act;
1769 yearButton =
new QCalToolButton(navBarBackground);
1770 yearButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Minimum);
1772 yearEdit =
new QSpinBox(navBarBackground);
1774 QFont font = q->font();
1778 yearEdit->setFrame(
false);
1779 yearEdit->setMinimum(m_model->m_minimumDate.year(m_model->m_calendar));
1780 yearEdit->setMaximum(m_model->m_maximumDate.year(m_model->m_calendar));
1782 spaceHolder =
new QSpacerItem(0,0);
1784 QHBoxLayout *headerLayout =
new QHBoxLayout;
1785 headerLayout->setContentsMargins(QMargins());
1786 headerLayout->setSpacing(0);
1787 headerLayout->addWidget(prevMonth);
1788 headerLayout->insertStretch(headerLayout->count());
1790 headerLayout->addItem(spaceHolder);
1792 headerLayout->insertStretch(headerLayout->count());
1793 headerLayout->addWidget(nextMonth);
1794 navBarBackground->setLayout(headerLayout);
1796 yearEdit->setFocusPolicy(Qt::StrongFocus);
1797 prevMonth->setFocusPolicy(Qt::NoFocus);
1798 nextMonth->setFocusPolicy(Qt::NoFocus);
1799 yearButton->setFocusPolicy(Qt::NoFocus);
1800 monthButton->setFocusPolicy(Qt::NoFocus);
1803 prevMonth->setObjectName(
"qt_calendar_prevmonth"_L1);
1804 nextMonth->setObjectName(
"qt_calendar_nextmonth"_L1);
1805 monthButton->setObjectName(
"qt_calendar_monthbutton"_L1);
1806 yearButton->setObjectName(
"qt_calendar_yearbutton"_L1);
1807 yearEdit->setObjectName(
"qt_calendar_yearedit"_L1);
1815 Q_Q(QCalendarWidget);
1816 prevMonth->setIcon(q->style()->standardIcon(q->isRightToLeft() ? QStyle::SP_ArrowRight : QStyle::SP_ArrowLeft,
nullptr, q));
1817 nextMonth->setIcon(q->style()->standardIcon(q->isRightToLeft() ? QStyle::SP_ArrowLeft : QStyle::SP_ArrowRight,
nullptr, q));
1823 int beg = 1, end = maxMonths;
1824 bool prevEnabled =
true;
1825 bool nextEnabled =
true;
1827 if (m_model->m_shownYear == m_model->m_minimumDate.year(cal)) {
1828 beg = m_model->m_minimumDate.month(cal);
1829 if (m_model->m_shownMonth == m_model->m_minimumDate.month(cal))
1830 prevEnabled =
false;
1832 if (m_model->m_shownYear == m_model->m_maximumDate.year(cal)) {
1833 end = m_model->m_maximumDate.month(cal);
1834 if (m_model->m_shownMonth == m_model->m_maximumDate.month(cal))
1835 nextEnabled =
false;
1837 prevMonth->setEnabled(prevEnabled);
1838 nextMonth->setEnabled(nextEnabled);
1839 for (
int i = 1; i <= maxMonths; i++) {
1840 bool monthEnabled =
true;
1841 if (i < beg || i > end)
1842 monthEnabled =
false;
1843 monthToAction[i]->setEnabled(monthEnabled);
1849 Q_Q(QCalendarWidget);
1851 for (
int i = 1; i <= 12; i++) {
1852 QString monthName(m_model->monthName(q->locale(), i));
1853 monthToAction[i]->setText(monthName);
1859 Q_Q(QCalendarWidget);
1862 QDate newDate = date;
1863 QDate minDate = q->minimumDate();
1864 QDate maxDate = q->maximumDate();
1865 if (minDate.isValid()&& minDate.daysTo(newDate) < 0)
1867 if (maxDate.isValid()&& maxDate.daysTo(newDate) > 0)
1869 showMonth(newDate.year(cal), newDate.month(cal));
1870 int row = -1, col = -1;
1872 if (row != -1 && col != -1)
1874 m_view->selectionModel()->setCurrentIndex(m_model->index(row, col),
1875 QItemSelectionModel::NoUpdate);
1882 QDate currentDate = getCurrentDate();
1883 QDate newDate = currentDate.addMonths(act->data().toInt() - currentDate.month(m_model->m_calendar), m_model->m_calendar);
1890 return m_model->dateForCell(index.row(), index.column());
1895 QDate currentDate = getCurrentDate().addMonths(-1, m_model->m_calendar);
1901 QDate currentDate = getCurrentDate().addMonths(1, m_model->m_calendar);
1907 Q_Q(QCalendarWidget);
1909 q->setFocusPolicy(oldFocusPolicy);
1910 qApp->removeEventFilter(q);
1911 spaceHolder->changeSize(0, 0);
1913 QDate currentDate = getCurrentDate();
1914 int newYear = q->locale().toInt(yearEdit->text());
1915 currentDate = currentDate.addYears(newYear - currentDate.year(
m_model->m_calendar),
m_model->m_calendar);
1916 yearButton->setText(q->locale().toString(currentDate, u"yyyy", m_model->m_calendar));
1922 Q_Q(QCalendarWidget);
1924 yearEdit->setGeometry(yearButton->x(), yearButton->y(),
1925 yearEdit->sizeHint().width(), yearButton->height());
1926 spaceHolder->changeSize(yearButton->width(), 0);
1928 oldFocusPolicy = q->focusPolicy();
1929 q->setFocusPolicy(Qt::NoFocus);
1931 qApp->installEventFilter(q);
1933 yearEdit->selectAll();
1934 yearEdit->setFocus(Qt::MouseFocusReason);
1941 Q_Q(QCalendarWidget);
1944 emit q->currentPageChanged(year, month);
1946 cachedSizeHint = QSize();
1953 Q_Q(QCalendarWidget);
1955 QString monthName = m_model->monthName(q->locale(), m_model->m_shownMonth);
1958 yearEdit->setValue(m_model->m_shownYear);
1959 yearButton->setText(yearEdit->text());
1964 QDate currentDate = m_model->m_date;
1968 m_selection->clear();
1969 if (row != -1 && column != -1) {
1970 idx =
m_model->index(row, column);
1971 m_selection->setCurrentIndex(idx, QItemSelectionModel::SelectCurrent);
1977 Q_Q(
const QCalendarWidget);
1978 q->paintCell(painter, rect, date);
1988 _q_slotChangeDate(date,
true);
1993 QDate oldDate = m_model->m_date;
1995 QDate newDate = m_model->m_date;
1998 if (oldDate != newDate) {
2000 Q_Q(QCalendarWidget);
2002 emit q->selectionChanged();
2008 Q_Q(QCalendarWidget);
2009 emit q->activated(m_model->m_date);
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2096
2097
2098
2099
2100
2101
2102
2103QCalendarWidget::QCalendarWidget(QWidget *parent)
2104 : QWidget(*
new QCalendarWidgetPrivate, parent, { })
2106 Q_D(QCalendarWidget);
2108 setAutoFillBackground(
true);
2109 setBackgroundRole(QPalette::Window);
2111 QVBoxLayout *layoutV =
new QVBoxLayout(
this);
2112 layoutV->setContentsMargins(QMargins());
2113 d->m_model =
new QCalendarModel(
this);
2114 QTextCharFormat fmt;
2115 fmt.setForeground(QBrush(Qt::red));
2116 d->m_model->m_dayFormats.insert(Qt::Saturday, fmt);
2117 d->m_model->m_dayFormats.insert(Qt::Sunday, fmt);
2118 d->m_view =
new QCalendarView(
this);
2119 d->m_view->setObjectName(
"qt_calendar_calendarview"_L1);
2120 d->m_view->setModel(d->m_model);
2121 d->m_model->setView(d->m_view);
2122 d->m_view->setSelectionBehavior(QAbstractItemView::SelectItems);
2123 d->m_view->setSelectionMode(QAbstractItemView::SingleSelection);
2124 d->m_view->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
2125 d->m_view->horizontalHeader()->setSectionsClickable(
false);
2126 d->m_view->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch);
2127 d->m_view->verticalHeader()->setSectionsClickable(
false);
2128 d->m_selection = d->m_view->selectionModel();
2129 d->createNavigationBar(
this);
2130 d->m_view->setFrameStyle(QFrame::NoFrame);
2131 d->m_delegate =
new QCalendarDelegate(d,
this);
2132 d->m_view->setItemDelegate(d->m_delegate);
2134 d->updateNavigationBar();
2135 setFocusPolicy(Qt::StrongFocus);
2136 setFocusProxy(d->m_view);
2137 setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
2139 connect(d->m_view, SIGNAL(showDate(QDate)),
2140 this, SLOT(_q_slotShowDate(QDate)));
2141 connect(d->m_view, SIGNAL(changeDate(QDate,
bool)),
2142 this, SLOT(_q_slotChangeDate(QDate,
bool)));
2143 connect(d->m_view, SIGNAL(clicked(QDate)),
2144 this, SIGNAL(clicked(QDate)));
2145 connect(d->m_view, SIGNAL(editingFinished()),
2146 this, SLOT(_q_editingFinished()));
2148 connect(d->prevMonth, SIGNAL(clicked(
bool)),
2149 this, SLOT(_q_prevMonthClicked()));
2150 connect(d->nextMonth, SIGNAL(clicked(
bool)),
2151 this, SLOT(_q_nextMonthClicked()));
2152 connect(d->yearButton, SIGNAL(clicked(
bool)),
2153 this, SLOT(_q_yearClicked()));
2154 connect(d->monthMenu, SIGNAL(triggered(QAction*)),
2155 this, SLOT(_q_monthChanged(QAction*)));
2156 connect(d->yearEdit, SIGNAL(editingFinished()),
2157 this, SLOT(_q_yearEditingFinished()));
2159 layoutV->setContentsMargins(QMargins());
2160 layoutV->setSpacing(0);
2161 layoutV->addWidget(d->navBarBackground);
2162 layoutV->addWidget(d->m_view);
2164 d->m_navigator =
new QCalendarTextNavigator(
this);
2165 setDateEditEnabled(
true);
2169
2170
2171QCalendarWidget::~QCalendarWidget()
2176
2177
2178QSize QCalendarWidget::sizeHint()
const
2180 return minimumSizeHint();
2184
2185
2186QSize QCalendarWidget::minimumSizeHint()
const
2188 Q_D(
const QCalendarWidget);
2189 if (d->cachedSizeHint.isValid())
2190 return d->cachedSizeHint;
2201 QStyleOption option;
2202 option.initFrom(
this);
2203 const int marginH = (style()->pixelMetric(QStyle::PM_FocusFrameHMargin, &option,
this) + 1) * 2;
2205 if (horizontalHeaderFormat() == QCalendarWidget::NoHorizontalHeader) {
2208 for (
int i = 1; i <= 7; i++) {
2209 QFontMetrics fm(d->m_model->formatForCell(0, i).font());
2210 w = qMax(w, fm.horizontalAdvance(d->m_model->dayName(d->m_model->dayOfWeekForColumn(i))) + marginH);
2211 h = qMax(h, fm.height());
2215 if (verticalHeaderFormat() == QCalendarWidget::NoVerticalHeader) {
2218 for (
int i = 1; i <= 6; i++) {
2219 QFontMetrics fm(d->m_model->formatForCell(i, 0).font());
2220 for (
int j = 1; j < end; j++)
2221 w = qMax(w, fm.horizontalAdvance(QString::number(j)) + marginH);
2222 h = qMax(h, fm.height());
2226 QFontMetrics fm(d->m_model->formatForCell(1, 1).font());
2227 for (
int i = 1; i <= end; i++) {
2228 w = qMax(w, fm.horizontalAdvance(QString::number(i)) + marginH);
2229 h = qMax(h, fm.height());
2232 if (d->m_view->showGrid()) {
2240 h = qMax(h, d->m_view->verticalHeader()->minimumSectionSize());
2241 w = qMax(w, d->m_view->horizontalHeader()->minimumSectionSize());
2244 QSize headerSize(0, 0);
2245 if (d->navBarVisible) {
2246 int headerH = d->navBarBackground->sizeHint().height();
2249 headerW += d->prevMonth->sizeHint().width();
2250 headerW += d->nextMonth->sizeHint().width();
2252 QFontMetrics fm = d->monthButton->fontMetrics();
2254 for (
int i = 1; i < 12; i++) {
2255 QString monthName = d->m_model->monthName(locale(), i);
2256 monthW = qMax(monthW, fm.boundingRect(monthName).width());
2258 const int buttonDecoMargin = d->monthButton->sizeHint().width() - fm.boundingRect(d->monthButton->text()).width();
2259 headerW += monthW + buttonDecoMargin;
2261 fm = d->yearButton->fontMetrics();
2262 headerW += fm.boundingRect(
"5555"_L1).width() + buttonDecoMargin;
2264 headerSize = QSize(headerW, headerH);
2267 w = qMax(headerSize.width(), w);
2268 h = (h * rows) + headerSize.height();
2269 QMargins cm = contentsMargins();
2270 w += cm.left() + cm.right();
2271 h += cm.top() + cm.bottom();
2272 d->cachedSizeHint = QSize(w, h);
2273 return d->cachedSizeHint;
2277
2278
2280void QCalendarWidget::paintCell(QPainter *painter,
const QRect &rect, QDate date)
const
2282 Q_D(
const QCalendarWidget);
2283 d->m_delegate->paintCell(painter, rect, date);
2287
2288
2289
2290
2291
2292
2293
2294
2295
2297QDate QCalendarWidget::selectedDate()
const
2299 Q_D(
const QCalendarWidget);
2300 return d->m_model->m_date;
2303void QCalendarWidget::setSelectedDate(QDate date)
2305 Q_D(QCalendarWidget);
2306 if (d->m_model->m_date == date && date == d->getCurrentDate())
2309 if (!date.isValid())
2312 d->m_model->setDate(date);
2314 QDate newDate = d->m_model->m_date;
2315 QCalendar cal = d->m_model->m_calendar;
2316 d->showMonth(newDate.year(cal), newDate.month(cal));
2317 emit selectionChanged();
2321
2322
2323
2324
2325
2327int QCalendarWidget::yearShown()
const
2329 Q_D(
const QCalendarWidget);
2330 return d->m_model->m_shownYear;
2334
2335
2336
2337
2338
2340int QCalendarWidget::monthShown()
const
2342 Q_D(
const QCalendarWidget);
2343 return d->m_model->m_shownMonth;
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2358void QCalendarWidget::setCurrentPage(
int year,
int month)
2360 Q_D(QCalendarWidget);
2361 QDate currentDate = d->getCurrentDate();
2362 QCalendar cal = d->m_model->m_calendar;
2363 int day = currentDate.day(cal);
2364 int daysInMonths = cal.daysInMonth(month, year);
2365 if (day > daysInMonths)
2368 d->showMonth(year, month);
2370 QDate newDate(year, month, day, d->m_model->m_calendar);
2371 int row = -1, col = -1;
2372 d->m_model->cellForDate(newDate, &row, &col);
2373 if (row != -1 && col != -1) {
2374 d->m_view->selectionModel()->setCurrentIndex(d->m_model->index(row, col),
2375 QItemSelectionModel::NoUpdate);
2380
2381
2382
2383
2384
2386void QCalendarWidget::showNextMonth()
2388 Q_D(
const QCalendarWidget);
2389 int year = yearShown();
2390 int month = monthShown();
2391 if (month == d->m_model->m_calendar.maximumMonthsInYear()) {
2397 setCurrentPage(year, month);
2401
2402
2403
2404
2405
2407void QCalendarWidget::showPreviousMonth()
2409 Q_D(
const QCalendarWidget);
2411 int year = yearShown();
2412 int month = monthShown();
2415 month = d->m_model->m_calendar.maximumMonthsInYear();
2419 setCurrentPage(year, month);
2423
2424
2425
2426
2427
2428
2430void QCalendarWidget::showNextYear()
2432 int year = yearShown();
2433 int month = monthShown();
2435 setCurrentPage(year, month);
2439
2440
2441
2442
2443
2444
2446void QCalendarWidget::showPreviousYear()
2448 int year = yearShown();
2449 int month = monthShown();
2451 setCurrentPage(year, month);
2455
2456
2457
2458
2459void QCalendarWidget::showSelectedDate()
2461 Q_D(
const QCalendarWidget);
2463 QDate currentDate = selectedDate();
2464 setCurrentPage(currentDate.year(d->m_model->m_calendar), currentDate.month(d->m_model->m_calendar));
2468
2469
2470
2471
2472void QCalendarWidget::showToday()
2474 Q_D(
const QCalendarWidget);
2476 QDate currentDate = QDate::currentDate();
2477 setCurrentPage(currentDate.year(d->m_model->m_calendar), currentDate.month(d->m_model->m_calendar));
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2506QDate QCalendarWidget::minimumDate()
const
2508 Q_D(
const QCalendarWidget);
2509 return d->m_model->m_minimumDate;
2512void QCalendarWidget::setMinimumDate(QDate date)
2514 Q_D(QCalendarWidget);
2515 if (!date.isValid() || d->m_model->m_minimumDate == date)
2518 QDate oldDate = d->m_model->m_date;
2519 d->m_model->setMinimumDate(date);
2520 d->yearEdit->setMinimum(d->m_model->m_minimumDate.year(d->m_model->m_calendar));
2521 d->updateMonthMenu();
2522 QDate newDate = d->m_model->m_date;
2523 if (oldDate != newDate) {
2525 d->showMonth(newDate.year(d->m_model->m_calendar), newDate.month(d->m_model->m_calendar));
2526 d->m_navigator->setDate(newDate);
2527 emit selectionChanged();
2531void QCalendarWidget::clearMinimumDate()
2533 setMinimumDate(QDate::fromJulianDay(1));
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2562QDate QCalendarWidget::maximumDate()
const
2564 Q_D(
const QCalendarWidget);
2565 return d->m_model->m_maximumDate;
2568void QCalendarWidget::setMaximumDate(QDate date)
2570 Q_D(QCalendarWidget);
2571 if (!date.isValid() || d->m_model->m_maximumDate == date)
2574 QDate oldDate = d->m_model->m_date;
2575 d->m_model->setMaximumDate(date);
2576 d->yearEdit->setMaximum(d->m_model->m_maximumDate.year(d->m_model->m_calendar));
2577 d->updateMonthMenu();
2578 QDate newDate = d->m_model->m_date;
2579 if (oldDate != newDate) {
2581 d->showMonth(newDate.year(d->m_model->m_calendar), newDate.month(d->m_model->m_calendar));
2582 d->m_navigator->setDate(newDate);
2583 emit selectionChanged();
2587void QCalendarWidget::clearMaximumDate()
2589 setMaximumDate(QDate(9999, 12, 31));
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2611void QCalendarWidget::setDateRange(QDate min, QDate max)
2613 Q_D(QCalendarWidget);
2614 if (d->m_model->m_minimumDate == min && d->m_model->m_maximumDate == max)
2616 if (!min.isValid() || !max.isValid())
2619 QDate oldDate = d->m_model->m_date;
2620 d->m_model->setRange(min, max);
2621 d->yearEdit->setMinimum(d->m_model->m_minimumDate.year(d->m_model->m_calendar));
2622 d->yearEdit->setMaximum(d->m_model->m_maximumDate.year(d->m_model->m_calendar));
2623 d->updateMonthMenu();
2624 QDate newDate = d->m_model->m_date;
2625 if (oldDate != newDate) {
2627 d->showMonth(newDate.year(d->m_model->m_calendar), newDate.month(d->m_model->m_calendar));
2628 d->m_navigator->setDate(newDate);
2629 emit selectionChanged();
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2647
2648
2649
2650
2651
2653void QCalendarWidget::setHorizontalHeaderFormat(QCalendarWidget::HorizontalHeaderFormat format)
2655 Q_D(QCalendarWidget);
2656 if (d->m_model->m_horizontalHeaderFormat == format)
2659 d->m_model->setHorizontalHeaderFormat(format);
2660 d->cachedSizeHint = QSize();
2661 d->m_view->viewport()->update();
2662 d->m_view->updateGeometry();
2665QCalendarWidget::HorizontalHeaderFormat QCalendarWidget::horizontalHeaderFormat()
const
2667 Q_D(
const QCalendarWidget);
2668 return d->m_model->m_horizontalHeaderFormat;
2673
2674
2675
2676
2677
2678
2679
2680
2681
2684
2685
2686
2687
2688
2690QCalendarWidget::VerticalHeaderFormat QCalendarWidget::verticalHeaderFormat()
const
2692 Q_D(
const QCalendarWidget);
2693 bool shown = d->m_model->weekNumbersShown();
2695 return QCalendarWidget::ISOWeekNumbers;
2696 return QCalendarWidget::NoVerticalHeader;
2699void QCalendarWidget::setVerticalHeaderFormat(QCalendarWidget::VerticalHeaderFormat format)
2701 Q_D(QCalendarWidget);
2703 if (format == QCalendarWidget::ISOWeekNumbers)
2705 if (d->m_model->weekNumbersShown() == show)
2707 d->m_model->setWeekNumbersShown(show);
2708 d->cachedSizeHint = QSize();
2709 d->m_view->viewport()->update();
2710 d->m_view->updateGeometry();
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2728bool QCalendarWidget::isGridVisible()
const
2730 Q_D(
const QCalendarWidget);
2731 return d->m_view->showGrid();
2735
2736
2737
2738
2739
2741QCalendar QCalendarWidget::calendar()
const
2743 Q_D(
const QCalendarWidget);
2744 return d->m_model->m_calendar;
2748
2749
2750
2751
2752
2753
2754
2755
2757void QCalendarWidget::setCalendar(QCalendar c)
2759 Q_D(QCalendarWidget);
2760 d->m_model->setCalendar(c);
2761 d->updateMonthMenuNames();
2762 d->yearEdit->setMinimum(d->m_model->m_minimumDate.year(d->m_model->m_calendar));
2763 d->yearEdit->setMaximum(d->m_model->m_maximumDate.year(d->m_model->m_calendar));
2764 d->updateNavigationBar();
2767void QCalendarWidget::setGridVisible(
bool show)
2769 Q_D(QCalendarWidget);
2770 d->m_view->setShowGrid(show);
2771 d->cachedSizeHint = QSize();
2772 d->m_view->viewport()->update();
2773 d->m_view->updateGeometry();
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2792QCalendarWidget::SelectionMode QCalendarWidget::selectionMode()
const
2794 Q_D(
const QCalendarWidget);
2795 return d->m_view->readOnly ? QCalendarWidget::NoSelection : QCalendarWidget::SingleSelection;
2798void QCalendarWidget::setSelectionMode(SelectionMode mode)
2800 Q_D(QCalendarWidget);
2801 d->m_view->readOnly = (mode == QCalendarWidget::NoSelection);
2802 d->setNavigatorEnabled(isDateEditEnabled() && (selectionMode() != QCalendarWidget::NoSelection));
2807
2808
2809
2810
2811
2812
2814void QCalendarWidget::setFirstDayOfWeek(Qt::DayOfWeek dayOfWeek)
2816 Q_D(QCalendarWidget);
2817 if ((Qt::DayOfWeek)d->m_model->firstColumnDay() == dayOfWeek)
2820 d->m_model->setFirstColumnDay(dayOfWeek);
2824Qt::DayOfWeek QCalendarWidget::firstDayOfWeek()
const
2826 Q_D(
const QCalendarWidget);
2827 return (Qt::DayOfWeek)d->m_model->firstColumnDay();
2831
2832
2833QTextCharFormat QCalendarWidget::headerTextFormat()
const
2835 Q_D(
const QCalendarWidget);
2836 return d->m_model->m_headerFormat;
2840
2841
2842
2843
2844
2845
2846void QCalendarWidget::setHeaderTextFormat(
const QTextCharFormat &format)
2848 Q_D(QCalendarWidget);
2849 d->m_model->m_headerFormat = format;
2850 d->cachedSizeHint = QSize();
2851 d->m_view->viewport()->update();
2852 d->m_view->updateGeometry();
2856
2857
2858
2859
2860QTextCharFormat QCalendarWidget::weekdayTextFormat(Qt::DayOfWeek dayOfWeek)
const
2862 Q_D(
const QCalendarWidget);
2863 return d->m_model->m_dayFormats.value(dayOfWeek);
2867
2868
2869
2870
2871
2872
2873void QCalendarWidget::setWeekdayTextFormat(Qt::DayOfWeek dayOfWeek,
const QTextCharFormat &format)
2875 Q_D(QCalendarWidget);
2876 d->m_model->m_dayFormats[dayOfWeek] = format;
2877 d->cachedSizeHint = QSize();
2878 d->m_view->viewport()->update();
2879 d->m_view->updateGeometry();
2883
2884
2885
2886QMap<QDate, QTextCharFormat> QCalendarWidget::dateTextFormat()
const
2888 Q_D(
const QCalendarWidget);
2889 return d->m_model->m_dateFormats;
2893
2894
2895
2896QTextCharFormat QCalendarWidget::dateTextFormat(QDate date)
const
2898 Q_D(
const QCalendarWidget);
2899 return d->m_model->m_dateFormats.value(date);
2903
2904
2905
2906
2907void QCalendarWidget::setDateTextFormat(QDate date,
const QTextCharFormat &format)
2909 Q_D(QCalendarWidget);
2911 d->m_model->m_dateFormats.clear();
2913 d->m_model->m_dateFormats[date] = format;
2914 d->m_view->viewport()->update();
2915 d->m_view->updateGeometry();
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936bool QCalendarWidget::isDateEditEnabled()
const
2938 Q_D(
const QCalendarWidget);
2939 return d->m_dateEditEnabled;
2942void QCalendarWidget::setDateEditEnabled(
bool enable)
2944 Q_D(QCalendarWidget);
2945 if (isDateEditEnabled() == enable)
2948 d->m_dateEditEnabled = enable;
2950 d->setNavigatorEnabled(enable && (selectionMode() != QCalendarWidget::NoSelection));
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965int QCalendarWidget::dateEditAcceptDelay()
const
2967 Q_D(
const QCalendarWidget);
2968 return d->m_navigator->dateEditAcceptDelay();
2971void QCalendarWidget::setDateEditAcceptDelay(
int delay)
2973 Q_D(QCalendarWidget);
2974 d->m_navigator->setDateEditAcceptDelay(delay);
2978
2979
2980
2981
2982
2983
2984
2985void QCalendarWidget::updateCell(QDate date)
2987 if (Q_UNLIKELY(!date.isValid())) {
2988 qWarning(
"QCalendarWidget::updateCell: Invalid date");
2995 Q_D(QCalendarWidget);
2997 d->m_model->cellForDate(date, &row, &column);
2998 if (row == -1 || column == -1)
3001 QModelIndex modelIndex = d->m_model->index(row, column);
3002 if (!modelIndex.isValid())
3005 d->m_view->viewport()->update(d->m_view->visualRect(modelIndex));
3009
3010
3011
3012
3013
3014
3015void QCalendarWidget::updateCells()
3017 Q_D(QCalendarWidget);
3019 d->m_view->viewport()->update();
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3035
3036
3037
3038
3039
3040
3041
3044
3045
3046
3047
3048
3049
3052
3053
3054
3055
3056
3057
3058
3059
3060
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3075bool QCalendarWidget::isNavigationBarVisible()
const
3077 Q_D(
const QCalendarWidget);
3078 return d->navBarVisible;
3082void QCalendarWidget::setNavigationBarVisible(
bool visible)
3084 Q_D(QCalendarWidget);
3085 d->navBarVisible = visible;
3086 d->cachedSizeHint = QSize();
3087 d->navBarBackground->setVisible(visible);
3092
3093
3094bool QCalendarWidget::event(QEvent *event)
3096 Q_D(QCalendarWidget);
3097 switch (event->type()) {
3098 case QEvent::LayoutDirectionChange:
3099 d->updateButtonIcons();
3101 case QEvent::LocaleChange:
3102 d->m_model->setFirstColumnDay(locale().firstDayOfWeek());
3103 d->cachedSizeHint = QSize();
3104 d->updateMonthMenuNames();
3105 d->updateNavigationBar();
3106 d->m_view->updateGeometry();
3111 case QEvent::FontChange:
3112 case QEvent::ApplicationFontChange:
3113 d->cachedSizeHint = QSize();
3114 d->m_view->updateGeometry();
3116 case QEvent::StyleChange:
3117 d->cachedSizeHint = QSize();
3118 d->m_view->updateGeometry();
3123 return QWidget::event(event);
3127
3128
3129bool QCalendarWidget::eventFilter(QObject *watched, QEvent *event)
3131 Q_D(QCalendarWidget);
3132 if (event->type() == QEvent::MouseButtonPress && d->yearEdit->hasFocus()) {
3137 QWidget *tlw = window();
3138 QWidget *widget = qobject_cast<QWidget *>(watched);
3139 if (!widget || widget->window() != tlw)
3140 return QWidget::eventFilter(watched, event);
3142 QPoint mousePos = widget->mapTo(tlw,
static_cast<QMouseEvent *>(event)->position().toPoint());
3143 QRect geom = QRect(d->yearEdit->mapTo(tlw, QPoint(0, 0)), d->yearEdit->size());
3144 if (!geom.contains(mousePos)) {
3146 d->_q_yearEditingFinished();
3151 return QWidget::eventFilter(watched, event);
3155
3156
3157void QCalendarWidget::mousePressEvent(QMouseEvent *event)
3159 setAttribute(Qt::WA_NoMouseReplay);
3160 QWidget::mousePressEvent(event);
3165
3166
3167void QCalendarWidget::resizeEvent(QResizeEvent * event)
3169 Q_D(QCalendarWidget);
3174 if (d->yearEdit->isVisible() && event->size().width() != event->oldSize().width())
3175 d->_q_yearEditingFinished();
3177 QWidget::resizeEvent(event);
3181
3182
3183void QCalendarWidget::keyPressEvent(QKeyEvent * event)
3185#if QT_CONFIG(shortcut)
3186 Q_D(QCalendarWidget);
3187 if (d->yearEdit->isVisible()&& event->matches(QKeySequence::Cancel)) {
3188 d->yearEdit->setValue(yearShown());
3189 d->_q_yearEditingFinished();
3193 QWidget::keyPressEvent(event);
3198#include "qcalendarwidget.moc"
3199#include "moc_qcalendarwidget.cpp"
The QCalendar class describes calendar systems.
constexpr QModelIndex() noexcept
Creates a new empty model index.
virtual Section handleKey(int key)=0
virtual void setDate(QDate date, QCalendar cal=QCalendar())=0
virtual QDate applyToDate(QDate date, QCalendar cal=QCalendar()) const =0
QCalendarDateSectionValidator()
virtual QString text(QDate date, QCalendar cal, int repeat) const =0
static QString highlightString(const QString &str, int pos)
virtual QString text() const =0
virtual ~QCalendarDateSectionValidator()
void setLocale(const QLocale &locale)
void setInitialDate(QDate date, QCalendar cal)
~QCalendarDateValidator()
void handleKeyEvent(QKeyEvent *keyEvent, QCalendar cal)
void setFormat(const QString &format)
QString currentText(QCalendar cal) const
QDate currentDate() const
virtual QDate applyToDate(QDate date, QCalendar cal) const override
virtual Section handleKey(int key) override
virtual void setDate(QDate date, QCalendar cal) override
virtual QString text() const override
virtual QString text(QDate date, QCalendar cal, int repeat) const override
void paintCell(QPainter *painter, const QRect &rect, QDate date) const
QString dayName(Qt::DayOfWeek day) const
void showMonth(int year, int month)
QCalendar calendar() const
QTextCharFormat formatForCell(int row, int col) const
int columnCount(const QModelIndex &parent) const override
Returns the number of columns for the children of the given parent.
QTextCharFormat m_headerFormat
QString monthName(const QLocale &locale, int month)
void setFirstColumnDay(Qt::DayOfWeek dayOfWeek)
int columnForDayOfWeek(Qt::DayOfWeek day) const
void setCalendar(QCalendar c)
Qt::ItemFlags flags(const QModelIndex &index) const override
\reimp
StaticDayOfWeekAssociativeArray< QTextCharFormat > m_dayFormats
int columnForFirstOfMonth(QDate date) const
void setMinimumDate(QDate date)
void cellForDate(QDate date, int *row, int *column) const
bool weekNumbersShown() const
QVariant data(const QModelIndex &index, int role) const override
Returns the data stored under the given role for the item referred to by the index.
void setMaximumDate(QDate date)
Qt::DayOfWeek firstColumnDay() const
void setHorizontalHeaderFormat(QCalendarWidget::HorizontalHeaderFormat format)
Qt::DayOfWeek dayOfWeekForColumn(int section) const
QDate referenceDate() const
void setView(QCalendarView *view)
int rowCount(const QModelIndex &parent) const override
Returns the number of rows under the given parent.
void setRange(QDate min, QDate max)
QMap< QDate, QTextCharFormat > m_dateFormats
QDate dateForCell(int row, int column) const
void setWeekNumbersShown(bool show)
virtual QDate applyToDate(QDate date, QCalendar cal) const override
QCalendarMonthValidator()
virtual Section handleKey(int key) override
virtual QString text(QDate date, QCalendar cal, int repeat) const override
virtual void setDate(QDate date, QCalendar cal) override
virtual QString text() const override
int dateEditAcceptDelay() const
void setDateEditAcceptDelay(int delay)
void timerEvent(QTimerEvent *e) override
This event handler can be reimplemented in a subclass to receive timer events for the object.
void setWidget(QWidget *widget)
bool eventFilter(QObject *o, QEvent *e) override
Filters events if this object has been installed as an event filter for the watched object.
virtual void keyboardSearch(const QString &) override
Moves to and selects the item best matching the string search.
void mouseReleaseEvent(QMouseEvent *event) override
void mousePressEvent(QMouseEvent *event) override
bool event(QEvent *event) override
void keyPressEvent(QKeyEvent *event) override
void changeDate(QDate date, bool changeMonth)
void setReadOnly(bool enable)
QDate handleMouseEvent(QMouseEvent *event)
QModelIndex moveCursor(CursorAction cursorAction, Qt::KeyboardModifiers modifiers) override
Returns a QModelIndex object pointing to the next object in the view, based on the given cursorAction...
void mouseDoubleClickEvent(QMouseEvent *event) override
void mouseMoveEvent(QMouseEvent *event) override
virtual QDate applyToDate(QDate date, QCalendar cal) const override
virtual QString text(QDate date, QCalendar cal, int repeat) const override
virtual void setDate(QDate date, QCalendar cal) override
virtual Section handleKey(int key) override
virtual QString text() const override
constexpr SectionToken(QCalendarDateSectionValidator *v, int rep)
QCalendarDateSectionValidator * validator