📄 datepicker.cpp
字号:
#endif}#ifdef QTOPIA_PHONEvoid DatePickerTable::paintFocus(QPainter *p, const QRect &cr){ QRect focusRect( 0, 0, cr.width(), cr.height() ); if ( !mousePreferred && parentWidget() && parentWidget()->isModalEditing()) { p->setPen( QPen( palette().color(QPalette::Active, QColorGroup::Highlight), 1 ) ); } else { p->setPen( QPen( black, 1) ); } p->setBrush( NoBrush ); p->drawRect( focusRect.x(), focusRect.y(), focusRect.width(), focusRect.height() ); p->drawRect( focusRect.x() + 1, focusRect.y() + 1, focusRect.width() - 2, focusRect.height() - 2 );}#endifvoid DatePickerTable::keyPressEvent( QKeyEvent *e ){ e->ignore();}void DatePickerTable::setupLabels(){ TimeString::Length len = TimeString::Short; int approxSize = QFontMetrics(QFont()).width(" Wed ") * 7; if ( QApplication::desktop()->width() > approxSize ) len = TimeString::Medium; for ( int i = 0; i < 7; i++ ) { if ( onMonday ) horizontalHeader()->setLabel( i, TimeString::localDayOfWeek(i + 1, len) ); else horizontalHeader()->setLabel( i, TimeString::localDayOfWeek(((i + 6) % 7) + 1, len) ); }}//---------------------------------------------------------------------------/*! \class QPEDatePicker datepicker.h \brief The QPEDatePicker class allows a date to be selected from a calendar view. QPEDatePicker comprises a header to select month and year and a calendar view to select the date. First availability: Qtopia 1.6 \ingroup qtopiaemb*//*! \fn void QPEDatePicker::dateClicked( const QDate &date ); This signal is emitted when a date in the calendar is clicked. \a date contains the date that was clicked*//*! Constructs a QPEDatePicker. The \a parent and \a name parameters are the standard Qt parent parameters.*/QPEDatePicker::QPEDatePicker( QWidget *parent, const char *name) : QVBox( parent, name ){ year = 1970; // Default to epoch. month = 1;#ifdef QTOPIA_PHONE setMargin(2);#endif header = new DatePickerHeader( this, "DatePickerHeader" ); table = new DatePickerTable( this, "DatePickerTable" ); table->setFrameStyle( QFrame::NoFrame ); header->setDate( year, month ); table->updateContents(); connect( header, SIGNAL( dateChanged(int,int) ), this, SLOT( setDate(int,int) ) ); connect( table, SIGNAL( clickedPos(int,int) ), this, SLOT( calendarClicked(int,int) ) ); connect( table, SIGNAL( currentChanged(int,int) ), this, SLOT( calendarChanged(int,int) ) ); connect( qApp, SIGNAL(weekChanged(bool)), this, SLOT(setWeekStartsMonday(bool)) ); TimeString::connectChange(table, SLOT(timeStringChanged())); setDate(QDate::currentDate()); setFocusPolicy(StrongFocus); setFocus(); table->setFocusPolicy(NoFocus); header->setFocusPolicy(NoFocus);}/*! Destructs QPEDatePicker.*/QPEDatePicker::~QPEDatePicker(){}void QPEDatePicker::paintCell(int row, int col, QPainter * p, const QRect & cr, bool, const QColorGroup & cg){ QDate cDay = Calendar::dateAtCoord( year, month, row, col, table->weekStartsMonday()); paintDay(cDay, p, cr, cg);}/*! Paints a single day \a cDay in the calendar using painter \a p and color group \a cg. The cell geometry is \a cr. The default implementation draws the day of the month in the top left corner of the cell.*/void QPEDatePicker::paintDay(const QDate &cDay, QPainter *p, const QRect &cr, const QColorGroup &cg){ p->save(); QColorGroup mygroup = cg; if (cDay.month() == month) mygroup.setBrush( QColorGroup::Base, white ); else mygroup.setBrush( QColorGroup::Base, QColor( 224, 224, 224 ) ); mygroup.setColor( QColorGroup::Text, black ); paintDayBackground(cDay, p, cr, mygroup); QPen tPen = p->pen(); p->setPen( cg.mid() ); p->drawLine( 0, cr.height() - 1, cr.width() - 1, cr.height() - 1 ); p->drawLine( cr.width() - 1, 0, cr.width() - 1, cr.height() - 1 ); p->setPen(tPen); // base font size of geometry of first cell; QSize cSize = table->cellGeometry(0,0).size(); p->setPen( mygroup.text() ); QFont f = p->font(); QFontMetrics fmtest( f ); if (fmtest.height() > QMAX(10, cSize.height() / 3)) { f.setPointSize( QMAX(( f.pointSize() / 3 ) * 2, 8 ) ); p->setFont( f ); } QFontMetrics fm( f ); if (QDate::currentDate() == cDay) { p->setPen(QColor(255,255,255)); p->fillRect(1, 1, fm.width(QString::number( cDay.day() )) + 1, fm.height(), QColor(0,0,0)); } p->drawText( 2, 1 + fm.ascent(), QString::number( cDay.day() ) ); p->restore();}/*! Paints the background of a single day in the calendar using painter \a p and color group \a cg. The cell geometry is \a cr. The default implementation fills with the base color.*/void QPEDatePicker::paintDayBackground(const QDate &, QPainter *p, const QRect &cr, const QColorGroup &cg){ p->fillRect( 0, 0, cr.width(), cr.height(), cg.brush( QColorGroup::Base ) );}/*! Returns TRUE if the beginning of the week is Monday. \sa setWeekStartsMonday()*/bool QPEDatePicker::weekStartsMonday() const{ return table->weekStartsMonday();}/*! \internal*/void QPEDatePicker::updateContents(){ table->updateContents();}/*! Sets the selected date to year \a y and month \a m. The current day of the month is retained unless it falls outside the number of days in the selected date.*/void QPEDatePicker::setDate( int y, int m ){ /* only change the date if this is a different date, * other wise we may mistakenly overide the day */ if ( y != year || m != month ) { QDate nd( y, m, 1 ); if ( nd.daysInMonth() < day ) setDate(y, m, nd.daysInMonth()); else setDate(y, m, day); }}/*! Sets the current date to year \a y, month \a m and day \a d.*/void QPEDatePicker::setDate( int y, int m, int d ){ setDate(QDate(y, m, d));}/* called when we wish to close or pass back the date */void QPEDatePicker::calendarClicked(int r, int c){ calendarChanged(r, c);#ifdef QTOPIA_PHONE if( !mousePreferred ) setModalEditing(FALSE); table->updateCell(table->currentRow(), table->currentColumn());#endif emit dateClicked(QDate(year, month, day));}void QPEDatePicker::calendarChanged(int r, int c){ setDate( Calendar::dateAtCoord(year, month, r, c, table->weekStartsMonday()) );}/*! Sets the selected date to \a d.*/void QPEDatePicker::setDate( const QDate &d ){ if (year != d.year() || month != d.month()) { year = d.year(); month = d.month(); header->setDate( d.year(), d.month()); table->updateContents(); } else if (day == d.day()) return; day = d.day(); int r, c; Calendar::coordForDate(d.year(), d.month(), d, r, c, table->weekStartsMonday()); table->setCurrentCell(r,c);}/*! Returns the selected date.*/QDate QPEDatePicker::selectedDate() const{ if ( !table ) return QDate::currentDate(); return QDate( year, month, day );}/*! Display the calendar with weeks starting on Monday if \a startMonday is TRUE, otherwise weeks start with Sunday. \sa weekStartsMonday()*/void QPEDatePicker::setWeekStartsMonday( bool startMonday ){ table->setWeekStartsMonday( startMonday ); int r, c; Calendar::coordForDate(year, month, QDate(year, month, day), r, c, table->weekStartsMonday()); table->setCurrentCell(r,c);}/*! \internal*/void QPEDatePicker::keyPressEvent( QKeyEvent *e ){#ifdef QTOPIA_PHONE if( !mousePreferred ) { if (!isModalEditing()) { if (e->key() == Key_Select) { setModalEditing(TRUE); table->updateCell(table->currentRow(), table->currentColumn()); } else { e->ignore(); } return; } }#endif switch(e->key()) { case Key_Up: setDate(QDate(year, month, day).addDays(-7)); break; case Key_Down: setDate(QDate(year, month, day).addDays(7)); break; case Key_Left: setDate(QDate(year, month, day).addDays(-1)); break; case Key_Right: setDate(QDate(year, month, day).addDays(1)); break;#ifdef QTOPIA_PHONE case Key_1: setDate(QDate(year, month, day).addDays(-7)); break; case Key_3: setDate(QDate(year, month, day).addDays(7)); break; case Key_4: if (month == 1) setDate(year-1, 12); else setDate(year, month-1); break; case Key_6: if (month == 12) setDate(year+1, 1); else setDate(year, month+1); break; case Key_7: setDate(year-1, month); break; case Key_9: setDate(year+1, month); break; case Key_5: setDate(QDate::currentDate()); break; case Key_Back: case Key_No: if( !mousePreferred ) { setModalEditing(FALSE); table->updateCell(table->currentRow(), table->currentColumn()); } e->ignore(); // let the parent get this too break;#endif case Key_Enter: case Key_Return: case Key_Space:#ifdef QTOPIA_PHONE case Key_Select: if( !mousePreferred ) { setModalEditing(FALSE); table->updateCell(table->currentRow(), table->currentColumn()); }#endif emit dateClicked(QDate(year, month, day)); break; default: e->ignore(); break; }}#include "datepicker.moc"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -