📄 qcalendarwidget.cpp
字号:
/******************************************************************************** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.**** This file is part of the QtGui module of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file. Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://trolltech.com/products/qt/licenses/licensing/opensource/**** If you are unsure which license is appropriate for your use, please** review the following information:** http://trolltech.com/products/qt/licenses/licensing/licensingoverview** or contact the sales department at sales@trolltech.com.**** In addition, as a special exception, Trolltech gives you certain** additional rights. These rights are described in the Trolltech GPL** Exception version 1.0, which can be found at** http://www.trolltech.com/products/qt/gplexception/ and in the file** GPL_EXCEPTION.txt in this package.**** In addition, as a special exception, Trolltech, as the sole copyright** holder for Qt Designer, grants users of the Qt/Eclipse Integration** plug-in the right for the Qt/Eclipse Integration to link to** functionality provided by Qt Designer and its related libraries.**** Trolltech reserves all rights not expressly granted herein.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include "qcalendarwidget.h"#ifndef QT_NO_CALENDARWIDGET#include <qabstractitemmodel.h>#include <qitemdelegate.h>#include <qdatetime.h>#include <qtableview.h>#include <qlayout.h>#include <qevent.h>#include <qtextformat.h>#include <qheaderview.h>#include <private/qwidget_p.h>#include <qpushbutton.h>#include <qtoolbutton.h>#include <qlabel.h>#include <qspinbox.h>#include <qmenu.h>#include <qapplication.h>#include <qbasictimer.h>#include <qstylepainter.h>enum { RowCount = 6, ColumnCount = 7, HeaderColumn = 0, HeaderRow = 0, MinimumDayOffset = 1};class QCalendarDateSectionValidator{public: enum Section { NextSection, ThisSection, PrevSection }; QCalendarDateSectionValidator() {} virtual ~QCalendarDateSectionValidator() {} virtual Section handleKey(int key) = 0; virtual QDate applyToDate(const QDate &date) const = 0; virtual void setDate(const QDate &date) = 0; virtual QString text() const = 0; virtual QString text(const QDate &date, int repeat) const = 0; QLocale m_locale;protected: QString highlightString(const QString &str, int pos) const;private:};QString QCalendarDateSectionValidator::highlightString(const QString &str, int pos) const{ if (pos == 0) return QLatin1String("<b>") + str + QLatin1String("</b>"); int startPos = str.length() - pos; return str.mid(0, startPos) + QLatin1String("<b>") + str.mid(startPos, pos) + QLatin1String("</b>");}class QCalendarDayValidator : public QCalendarDateSectionValidator{public: QCalendarDayValidator(); virtual Section handleKey(int key); virtual QDate applyToDate(const QDate &date) const; virtual void setDate(const QDate &date); virtual QString text() const; virtual QString text(const QDate &date, int repeat) const;private: int m_pos; int m_day; int m_oldDay;};QCalendarDayValidator::QCalendarDayValidator() : QCalendarDateSectionValidator(), m_pos(0), m_day(1), m_oldDay(1){}QCalendarDateSectionValidator::Section QCalendarDayValidator::handleKey(int key){ if (key == Qt::Key_Right || key == Qt::Key_Left) { m_pos = 0; return QCalendarDateSectionValidator::ThisSection; } else if (key == Qt::Key_Up) { m_pos = 0; ++m_day; if (m_day > 31) m_day = 1; return QCalendarDateSectionValidator::ThisSection; } else if (key == Qt::Key_Down) { m_pos = 0; --m_day; if (m_day < 1) m_day = 31; return QCalendarDateSectionValidator::ThisSection; } else if (key == Qt::Key_Back || key == Qt::Key_Backspace) { --m_pos; if (m_pos < 0) m_pos = 1; if (m_pos == 0) m_day = m_oldDay; else m_day = m_day / 10; //m_day = m_oldDay / 10 * 10 + m_day / 10; if (m_pos == 0) return QCalendarDateSectionValidator::PrevSection; return QCalendarDateSectionValidator::ThisSection; } if (key < Qt::Key_0 || key > Qt::Key_9) return QCalendarDateSectionValidator::ThisSection; int pressedKey = key - Qt::Key_0; if (m_pos == 0) m_day = pressedKey; else m_day = m_day % 10 * 10 + pressedKey; if (m_day > 31) m_day = 31; ++m_pos; if (m_pos > 1) { m_pos = 0; return QCalendarDateSectionValidator::NextSection; } return QCalendarDateSectionValidator::ThisSection;}QDate QCalendarDayValidator::applyToDate(const QDate &date) const{ int day = m_day; if (day < 1) day = 1; else if (day > 31) day = 31; if (day > date.daysInMonth()) day = date.daysInMonth(); return QDate(date.year(), date.month(), day);}void QCalendarDayValidator::setDate(const QDate &date){ m_day = m_oldDay = date.day(); m_pos = 0;}QString QCalendarDayValidator::text() const{ QString str; if (m_day / 10 == 0) str += QLatin1String("0"); str += QString::number(m_day); return highlightString(str, m_pos);}QString QCalendarDayValidator::text(const QDate &date, int repeat) const{ if (repeat <= 1) { return QString::number(date.day()); } else if (repeat == 2) { QString str; if (date.day() / 10 == 0) str += QLatin1String("0"); return str + QString::number(date.day()); } else if (repeat == 3) { return m_locale.dayName(date.dayOfWeek(), QLocale::ShortFormat); } else if (repeat >= 4) { return m_locale.dayName(date.dayOfWeek(), QLocale::LongFormat); } return QString();}//////////////////////////////////class QCalendarMonthValidator : public QCalendarDateSectionValidator{public: QCalendarMonthValidator(); virtual Section handleKey(int key); virtual QDate applyToDate(const QDate &date) const; virtual void setDate(const QDate &date); virtual QString text() const; virtual QString text(const QDate &date, int repeat) const;private: int m_pos; int m_month; int m_oldMonth;};QCalendarMonthValidator::QCalendarMonthValidator() : QCalendarDateSectionValidator(), m_pos(0), m_month(1), m_oldMonth(1){}QCalendarDateSectionValidator::Section QCalendarMonthValidator::handleKey(int key){ if (key == Qt::Key_Right || key == Qt::Key_Left) { m_pos = 0; return QCalendarDateSectionValidator::ThisSection; } else if (key == Qt::Key_Up) { m_pos = 0; ++m_month; if (m_month > 12) m_month = 1; return QCalendarDateSectionValidator::ThisSection; } else if (key == Qt::Key_Down) { m_pos = 0; --m_month; if (m_month < 1) m_month = 12; return QCalendarDateSectionValidator::ThisSection; } else if (key == Qt::Key_Back || key == Qt::Key_Backspace) { --m_pos; if (m_pos < 0) m_pos = 1; if (m_pos == 0) m_month = m_oldMonth; else m_month = m_month / 10; //m_month = m_oldMonth / 10 * 10 + m_month / 10; if (m_pos == 0) return QCalendarDateSectionValidator::PrevSection; return QCalendarDateSectionValidator::ThisSection; } if (key < Qt::Key_0 || key > Qt::Key_9) return QCalendarDateSectionValidator::ThisSection; int pressedKey = key - Qt::Key_0; if (m_pos == 0) m_month = pressedKey; else m_month = m_month % 10 * 10 + pressedKey; if (m_month > 12) m_month = 12; ++m_pos; if (m_pos > 1) { m_pos = 0; return QCalendarDateSectionValidator::NextSection; } return QCalendarDateSectionValidator::ThisSection;}QDate QCalendarMonthValidator::applyToDate(const QDate &date) const{ int month = m_month; if (month < 1) month = 1; else if (month > 12) month = 12; QDate newDate(date.year(), m_month, 1); int day = date.day(); if (day > newDate.daysInMonth()) day = newDate.daysInMonth(); return QDate(date.year(), month, day);}void QCalendarMonthValidator::setDate(const QDate &date){ m_month = m_oldMonth = date.month(); m_pos = 0;}QString QCalendarMonthValidator::text() const{ QString str; if (m_month / 10 == 0) str += QLatin1String("0"); str += QString::number(m_month); return highlightString(str, m_pos);}QString QCalendarMonthValidator::text(const QDate &date, int repeat) const{ if (repeat <= 1) { return QString::number(date.month()); } else if (repeat == 2) { QString str; if (date.month() / 10 == 0) str += QLatin1String("0"); return str + QString::number(date.month()); } else if (repeat == 3) { return m_locale.monthName(date.month(), QLocale::ShortFormat); } else if (repeat >= 4) { return m_locale.monthName(date.month(), QLocale::LongFormat); } return QString();}//////////////////////////////////class QCalendarYearValidator : public QCalendarDateSectionValidator{public: QCalendarYearValidator(); virtual Section handleKey(int key); virtual QDate applyToDate(const QDate &date) const; virtual void setDate(const QDate &date); virtual QString text() const; virtual QString text(const QDate &date, int repeat) const;private: int pow10(int n); int m_pos; int m_year; int m_oldYear;};QCalendarYearValidator::QCalendarYearValidator() : QCalendarDateSectionValidator(), m_pos(0), m_year(2000), m_oldYear(2000){}int QCalendarYearValidator::pow10(int n){ int power = 1; for (int i = 0; i < n; i++) power *= 10; return power;}QCalendarDateSectionValidator::Section QCalendarYearValidator::handleKey(int key){ if (key == Qt::Key_Right || key == Qt::Key_Left) { m_pos = 0; return QCalendarDateSectionValidator::ThisSection; } else if (key == Qt::Key_Up) { m_pos = 0; ++m_year; return QCalendarDateSectionValidator::ThisSection; } else if (key == Qt::Key_Down) { m_pos = 0; --m_year; return QCalendarDateSectionValidator::ThisSection; } else if (key == Qt::Key_Back || key == Qt::Key_Backspace) { --m_pos; if (m_pos < 0) m_pos = 3; int pow = pow10(m_pos); m_year = m_oldYear / pow * pow + m_year % (pow * 10) / 10; if (m_pos == 0) return QCalendarDateSectionValidator::PrevSection; return QCalendarDateSectionValidator::ThisSection; } if (key < Qt::Key_0 || key > Qt::Key_9) return QCalendarDateSectionValidator::ThisSection; int pressedKey = key - Qt::Key_0; int pow = pow10(m_pos); m_year = m_year / (pow * 10) * (pow * 10) + m_year % pow * 10 + pressedKey; ++m_pos; if (m_pos > 3) { m_pos = 0; return QCalendarDateSectionValidator::NextSection; } return QCalendarDateSectionValidator::ThisSection;}QDate QCalendarYearValidator::applyToDate(const QDate &date) const{ int year = m_year; if (year < 1) year = 1; QDate newDate(year, date.month(), 1); int day = date.day(); if (day > newDate.daysInMonth()) day = newDate.daysInMonth(); return QDate(year, date.month(), day);}void QCalendarYearValidator::setDate(const QDate &date){ m_year = m_oldYear = date.year(); m_pos = 0;}QString QCalendarYearValidator::text() const{ QString str; int pow = 10; for (int i = 0; i < 3; i++) { if (m_year / pow == 0) str += QLatin1String("0"); pow *= 10; } str += QString::number(m_year); return highlightString(str, m_pos);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -