📄 qcalendarwidget.cpp
字号:
yearButton->setFocusPolicy(Qt::NoFocus); monthButton->setFocusPolicy(Qt::NoFocus); //set names for the header controls. prevMonth->setObjectName(QLatin1String("qt_calendar_prevmonth")); nextMonth->setObjectName(QLatin1String("qt_calendar_nextmonth")); monthButton->setObjectName(QLatin1String("qt_calendar_monthbutton")); yearButton->setObjectName(QLatin1String("qt_calendar_yearbutton")); yearEdit->setObjectName(QLatin1String("qt_calendar_yearedit")); updateMonthMenu(); showMonth(m_model->date.year(),m_model->date.month());}void QCalendarWidgetPrivate::updateMonthMenu(){ int beg = 1, end = 12; bool prevEnabled = true; bool nextEnabled = true; if (m_model->shownYear == m_model->minimumDate.year()) { beg = m_model->minimumDate.month(); if (m_model->shownMonth == m_model->minimumDate.month()) prevEnabled = false; } if (m_model->shownYear == m_model->maximumDate.year()) { end = m_model->maximumDate.month(); if (m_model->shownMonth == m_model->maximumDate.month()) nextEnabled = false; } prevMonth->setEnabled(prevEnabled); nextMonth->setEnabled(nextEnabled); for (int i = 1; i <= 12; i++) { bool monthEnabled = true; if (i < beg || i > end) monthEnabled = false; monthToAction[i]->setEnabled(monthEnabled); }}void QCalendarWidgetPrivate::updateMonthMenuNames(){ Q_Q(QCalendarWidget); for (int i = 1; i <= 12; i++) { QString monthName(q->locale().monthName(i, QLocale::LongFormat)); monthToAction[i]->setText(monthName); }}void QCalendarWidgetPrivate::updateCurrentPage(QDate &newDate){ Q_Q(QCalendarWidget); QDate minDate = q->minimumDate(); QDate maxDate = q->maximumDate(); if (minDate.isValid()&& minDate.daysTo(newDate) < 0) newDate = minDate; if (maxDate.isValid()&& maxDate.daysTo(newDate) > 0) newDate = maxDate; showMonth(newDate.year(), newDate.month()); int row = -1, col = -1; m_model->cellForDate(newDate, &row, &col); if (row != -1 && col != -1) { m_view->selectionModel()->setCurrentIndex(m_model->index(row, col), QItemSelectionModel::NoUpdate); }}void QCalendarWidgetPrivate::_q_monthChanged(QAction *act){ monthButton->setText(act->text()); QDate currentDate = getCurrentDate(); QDate newDate = currentDate.addMonths(act->data().toInt()-currentDate.month()); updateCurrentPage(newDate);}QDate QCalendarWidgetPrivate::getCurrentDate(){ QModelIndex index = m_view->currentIndex(); return m_model->dateForCell(index.row(), index.column());}void QCalendarWidgetPrivate::_q_prevMonthClicked(){ QDate currentDate = getCurrentDate().addMonths(-1); updateCurrentPage(currentDate);}void QCalendarWidgetPrivate::_q_nextMonthClicked(){ QDate currentDate = getCurrentDate().addMonths(1); updateCurrentPage(currentDate);}void QCalendarWidgetPrivate::_q_yearEditingFinished(){ yearButton->setText(yearEdit->text()); yearEdit->hide(); spaceHolder->changeSize(0, 0); yearButton->show(); QDate currentDate(yearEdit->text().toInt(), getCurrentDate().month(), getCurrentDate().day()); updateCurrentPage(currentDate);}void QCalendarWidgetPrivate::_q_yearClicked(){ //show the spinbox on top of the button yearEdit->setGeometry(yearButton->x(), yearButton->y(), yearEdit->sizeHint().width(), yearButton->height()); spaceHolder->changeSize(yearButton->width(), 0); yearButton->hide(); yearEdit->show(); yearEdit->raise(); yearEdit->selectAll(); yearEdit->setFocus(Qt::MouseFocusReason);}void QCalendarWidgetPrivate::showMonth(int year, int month){ if (m_model->shownYear == year && m_model->shownMonth == month) return; Q_Q(QCalendarWidget); m_model->showMonth(year, month); updateNavigationBar(); emit q->currentPageChanged(year, month); m_view->internalUpdate(); cachedSizeHint = QSize(); update(); updateMonthMenu();}void QCalendarWidgetPrivate::updateNavigationBar(){ Q_Q(QCalendarWidget); QString monthName = q->locale().monthName(m_model->shownMonth, QLocale::LongFormat); monthButton->setText(monthName); yearButton->setText(QString::number(m_model->shownYear)); yearEdit->setValue(m_model->shownYear);}void QCalendarWidgetPrivate::update(){ QDate currentDate = m_model->date; int row, column; m_model->cellForDate(currentDate, &row, &column); QModelIndex idx; m_selection->clear(); if (row != -1 && column != -1) { idx = m_model->index(row, column); m_selection->setCurrentIndex(idx, QItemSelectionModel::SelectCurrent); }}void QCalendarWidgetPrivate::paintCell(QPainter *painter, const QRect &rect, const QDate &date) const{ Q_Q(const QCalendarWidget); q->paintCell(painter, rect, date);}void QCalendarWidgetPrivate::_q_slotChangeDate(const QDate &date, bool changeMonth){ QDate oldDate = m_model->date; m_model->setDate(date); QDate newDate = m_model->date; if (changeMonth) showMonth(newDate.year(), newDate.month()); if (oldDate != newDate) { update(); Q_Q(QCalendarWidget); emit q->selectionChanged(); }}void QCalendarWidgetPrivate::_q_editingFinished(){ Q_Q(QCalendarWidget); emit q->activated(m_model->date);}/*! \class QCalendarWidget \brief The QCalendarWidget class provides a monthly based calendar widget allowing the user to select a date. \since 4.2 \mainclass \image cleanlooks-calendarwidget.png The widget is initialized with the current month and year, but QCalendarWidget provides several public slots to change the year and month that is shown. The currently displayed month and year can be retrieved using the currentPageMonth() and currentPageYear() functions, respectively. By default, today's date is selected, and the user can select a date using both mouse and keyboard. The currently selected date can be retrieved using the selectedDate() function. It is possible to constrain the user selection to a given date range by setting the minimumDate and maximumDate properties. Alternatively, both properties can be set in one go using the setDateRange() convenience slot. Set the \l selectionMode property to NoSelection to prohibit the user from selecting at all. Note that a date also can be selected programmatically using the setSelectedDate() slot. A newly created calendar widget uses abbreviated day names, and both Saturdays and Sundays are marked in red. The calendar grid is not visible. The week numbers are displayed, and the first column day is Sunday. The notation of the days can be altered to a single letter abbreviations ("M" for "Monday") by setting the horizontalHeaderFormat property to QCalendarWidget::SingleLetterDayNames. Setting the same property to QCalendarWidget::LongDayNames makes the header display the complete day names. The week numbers can be removed by setting the verticalHeaderFormat property to QCalendarWidget::NoVerticalHeader. The calendar grid can be turned on by setting the gridVisible property to true using the setGridVisible() function: \table \row \o \image qcalendarwidget-grid.png \row \o \code QCalendarWidget *calendar; calendar->setGridVisible(true); \endcode \endtable Finally, the day in the first column can be altered using the setFirstDayOfWeek() function. The QCalendarWidget class also provides three signals, selectionChanged(), activated() and currentPageChanged() making it possible to respond to user interaction. The rendering of the headers, weekdays or single days can be largely customized by setting QTextCharFormat's for some special weekday, a special date or for the rendering of the headers. Only a subset of the properties in QTextCharFormat are used by the calendar widget. Currently, the foreground, background and font properties are used to determine the rendering of individual cells in the widget. \sa QDate, QDateEdit, QTextCharFormat*//*! \enum QCalendarWidget::SelectionMode This enum describes the types of selection offered to the user for selecting dates in the calendar. \value NoSelection Dates cannot be selected. \value SingleSelection Single dates can be selected. \sa selectionMode*//*! Constructs a calendar widget with the given \a parent. The widget is initialized with the current month and year, and the currently selected date is today. \sa setCurrentPage()*/QCalendarWidget::QCalendarWidget(QWidget *parent) : QWidget(*new QCalendarWidgetPrivate, parent, 0){ Q_D(QCalendarWidget); setAutoFillBackground(true); setBackgroundRole(QPalette::Window); QVBoxLayout *layoutV = new QVBoxLayout(this); layoutV->setMargin(0); d->m_model = new QCalendarModel(this); QTextCharFormat fmt; fmt.setForeground(QBrush(Qt::red)); d->m_model->m_dayFormats.insert(Qt::Saturday, fmt); d->m_model->m_dayFormats.insert(Qt::Sunday, fmt); d->m_view = new QCalendarView(this); d->m_view->setObjectName(QLatin1String("qt_calendar_calendarview")); d->m_view->setModel(d->m_model); d->m_model->setView(d->m_view); d->m_view->setSelectionBehavior(QAbstractItemView::SelectItems); d->m_view->setSelectionMode(QAbstractItemView::SingleSelection); d->m_view->horizontalHeader()->setResizeMode(QHeaderView::Stretch); d->m_view->horizontalHeader()->setClickable(false); d->m_view->verticalHeader()->setResizeMode(QHeaderView::Stretch); d->m_view->verticalHeader()->setClickable(false); d->m_selection = d->m_view->selectionModel(); d->createNavigationBar(this); d->m_view->setFrameStyle(QFrame::NoFrame); d->m_delegate = new QCalendarDelegate(d, this); d->m_view->setItemDelegate(d->m_delegate); d->update(); d->updateNavigationBar(); setFocusPolicy(Qt::StrongFocus); setFocusProxy(d->m_view); setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred); connect(d->m_view, SIGNAL(changeDate(QDate,bool)), this, SLOT(_q_slotChangeDate(QDate,bool))); connect(d->m_view, SIGNAL(clicked(QDate)), this, SIGNAL(clicked(QDate))); connect(d->m_view, SIGNAL(editingFinished()), this, SLOT(_q_editingFinished())); connect(d->prevMonth, SIGNAL(clicked(bool)), this, SLOT(_q_prevMonthClicked())); connect(d->nextMonth, SIGNAL(clicked(bool)), this, SLOT(_q_nextMonthClicked())); connect(d->yearButton, SIGNAL(clicked(bool)), this, SLOT(_q_yearClicked())); connect(d->monthMenu, SIGNAL(triggered(QAction*)), this, SLOT(_q_monthChanged(QAction*))); connect(d->yearEdit, SIGNAL(editingFinished()), this, SLOT(_q_yearEditingFinished())); layoutV->setMargin(0); layoutV->setSpacing(0); layoutV->addWidget(d->navBarBackground); layoutV->addWidget(d->m_view); d->m_navigator = new QCalendarTextNavigator(this); d->m_navigator->setLocale(locale()); setDateEditEnabled(true);}/*! Destroys the calendar widget.*/QCalendarWidget::~QCalendarWidget(){}/*! \reimp*/QSize QCalendarWidget::sizeHint() const{ return minimumSizeHint();}/*! \reimp*/QSize QCalendarWidget::minimumSizeHint() const{ Q_D(const QCalendarWidget); if (d->cachedSizeHint.isValid()) return d->cachedSizeHint; ensurePolished(); int w = 0; int h = 0; int end = 53; int rows = 7; int cols = 8; int startRow = 0; int startCol = 0; const int marginH = (style()->pixelMetric(QStyle::PM_FocusFrameHMargin) + 1) * 2; if (horizontalHeaderFormat() == QCalendarWidget::NoHorizontalHeader) { rows = 6; startRow = 1; } else { for (int i = 1; i <= 7; i++) { QFontMetrics fm(d->m_model->formatForCell(0, i).font()); w = qMax(w, fm.width(d->m_model->dayName(d->m_model->dayOfWeekForColumn(i))) + marginH); h = qMax(h, fm.height()); } } if (verticalHeaderFormat() == QCalendarWidget::NoVerticalHeader) { cols = 7; startCol = 1; } QFontMetrics fm(d->m_model->formatForCell(1, 1).font()); for (int i = 1; i <= end; i++) { w = qMax(w, fm.width(QString::number(i)) + marginH); h = qMax(h, fm.height()); } if (d->m_view->showGrid()) { // hardcoded in tableview w += 1; h += 1; } w += 1; // default column span h = qMax(h, d->m_view->verticalHeader()->minimumSectionSize()); w = qMax(w, d->m_view->horizontalHeader()->minimumSectionSize()); //add the size of the header. QSize headerSize(0, 0); if (d->navBarVisible) { int headerH = d->navBarBackground->sizeHint().height(); int headerW = 0; headerW += d->prevMonth->sizeHint().width(); headerW += d->nextMonth->sizeHint().width(); QFontMetrics fm = d->monthButton->fontMetrics(); int monthW = 0; for (int i = 1; i < 12; i++) { QString monthName = locale().monthName(i, QLocale::LongFormat); monthW = qMax(monthW, fm.boundingRect(monthName).width()); } const int buttonDecoMargin = d->monthButton->sizeHint().width() - fm.boundingRect(d->monthButton->text()).width(); headerW += monthW + buttonDecoMargin; fm = d->yearButton->fontMetrics(); headerW += fm.boundingRect(QLatin1String("5555")).width() + buttonDecoMargin; headerSize = QSize(headerW, head
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -