📄 datetimeedit.cpp
字号:
/************************************************************************ Copyright (C) 2000-2005 Trolltech AS. All rights reserved.**** This file is part of the Qtopia Environment.** ** This program is free software; you can redistribute it and/or modify it** under the terms of the GNU General Public License as published by the** Free Software Foundation; either version 2 of the License, or (at your** option) any later version.** ** A copy of the GNU GPL license version 2 is included in this package as ** LICENSE.GPL.**** This program is distributed in the hope that it will be useful, but** WITHOUT ANY WARRANTY; without even the implied warranty of** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. ** See the GNU General Public License for more details.**** In addition, as a special exception Trolltech gives permission to link** the code of this program with Qtopia applications copyrighted, developed** and distributed by Trolltech under the terms of the Qtopia Personal Use** License Agreement. You must comply with the GNU General Public License** in all respects for all of the code used other than the applications** licensed under the Qtopia Personal Use License Agreement. If you modify** this file, you may extend this exception to your version of the file,** but you are not obligated to do so. If you do not wish to do so, delete** this exception statement from your version.** ** See http://www.trolltech.com/gpl/ for GPL licensing information.**** Contact info@trolltech.com if any conditions of this licensing are** not clear to you.************************************************************************/#include "datetimeedit.h"#include <qtopia/qpeapplication.h>#include <qtopia/timestring.h>#include <qtopia/config.h>#ifdef QTOPIA_PHONE#include <qtopia/contextbar.h>#include <qtopia/global.h>#endif#include <qvalidator.h>#include <qapplication.h>#include <qpopupmenu.h>#include <qlayout.h>#ifdef QTOPIA_PHONEextern bool mousePreferred; // can't call Global::mousePreferred in libqtopia2 from libqtopia#endif#ifdef Q_WS_QWSextern Q_EXPORT QRect qt_maxWindowRect;#endifclass QPEDatePickerPopup : public QFrame{ Q_OBJECTpublic: QPEDatePickerPopup( bool showNullButton, QWidget *parent, const char *name = 0, int wflags = 0); QDate selectedDate() const;signals: void dateClicked(const QDate &);public slots: void setDate(const QDate &); void setWeekStartsMonday(bool); void popup(); // populates widget. protected: void closeEvent( QCloseEvent *e ); void keyPressEvent( QKeyEvent *e );private slots: void passOnNullDate();private: void populate(); bool mShowNull; QPEDatePicker *monthView; QPushButton *nullButton; bool wsm; QDate mdt;};class QPEDateEditPrivate{public: QPEDateEditPrivate() : popup(0) {} QPEDatePickerPopup *popup;};static bool fromString(const QString& input, QTime& result){ int h,m,s; h=m=s=0; int i = 0; int st = 0; // hour, minute, second, end. int count = 0; int acc = 0; while (i < (int)input.length()) { if (input[i].isSpace()) { i++; continue; } // expect digits, if (input[i].isDigit()) { if (st > 2 || count > 1) return FALSE; if (acc) acc *= 10; acc += input[i].digitValue(); count++; switch(st) { case 0: if (acc > 23) return FALSE; h = acc; break; case 1: if (acc > 59) return FALSE; m = acc; break; case 2: if (acc > 59) return FALSE; s = acc; break; } } else if (input[i] == QChar(':')) { if (st > 1 || count == 0) return FALSE; acc = 0; st++; count = 0; } else if (input[i] == QChar('A') || input[i] == QChar('a') || input[i] == QChar('P') || input[i] == QChar('p')) { if (st == 0 && count == 0) return FALSE; acc = 0; count = 0; st = 3; if (input[i] == QChar('p') || input[i] == QChar('P')) { if (h == 12) ; else if (h > 11) return FALSE; else h += 12; } else { if (h == 12) h = 0; else if (h > 11) return FALSE; } if (i + 1 == (int)input.length()) break; if (i + 2 != (int)input.length()) return FALSE; if (input[i+1] == QChar('m') || input[i+1] == QChar('M')) break; else return FALSE; } else return FALSE; i++; } result.setHMS(h,m,s); return TRUE;}class TimeValidator : public QValidator{public: TimeValidator(QWidget *p, const char *n = 0) : QValidator(p,n) { } State validate(QString &input, int &curs) const { QTime t; // <00->23>:<00->59>[AM|PM] QString k; int digs=0; int ncurs=0; for (int i=0; i<(int)input.length(); i++) { char c=input[i].latin1(); if ( c>='0' && c<='9' ) { QString s; s+=c; if ( i > 3 ) {#ifdef QTOPIA_PHONE if ( digs == 2 ) { digs = 0; if ( c == '2' ) s = " AM"; else if ( c == '7' ) s = " PM"; else s = QString::null; }#endif } else if ( digs == 2 ) { digs = 0; k += ':'; if ( i<curs ) ncurs++; } k += s; digs+=s.length(); if ( i<curs ) ncurs+=s.length(); } else if ( c == ' ' ) { digs = 0; k += c; if ( i<curs ) ncurs++; } else if ( c == 'a' || c == 'A' || c == 'p' || c == 'P' || c == 'm' || c == 'M' ) { digs = 0; k += c; if ( i<curs ) ncurs++; } else if ( c == ':' || digs==2 ) { digs = 0; k += ':'; if ( i<curs ) ncurs++; } } input = k; curs = ncurs; return Acceptable; }};/*! \class QPETimeEdit datetimeedit.h \brief The QPETimeEdit class provides a compact widget for selecting a time. QPETimeEdit extends QSpinBox to validate times as they are entered. It handles 12 hour and 24 hour time formats. First availability: Qtopia 1.6 \ingroup qtopiaemb*//*! \fn QPETimeEdit::valueChanged( const QTime &t ); This signal is emitted when the time is changed to \a t.*//*! Constructs a QPETimeEdit with the time set to 00:00. The \a parent and \a name parameters are the standard Qt parent parameters.*/QPETimeEdit::QPETimeEdit( QWidget *parent, const char *name) : QSpinBox(parent, name){ setWrapping(TRUE); setMinValue(0); setMaxValue(23*60 + 59); setLineStep(15); connect(this, SIGNAL(valueChanged(int)), this, SLOT(changeTimeUsingValue(int))); tv = new TimeValidator(this); QPEApplication::setInputMethodHint(this,QPEApplication::Number); setValidator(tv); TimeString::connectChange(this,SLOT(clockChanged()) );}/*! Constructs a QPETimeEdit with the time set to \a dt. The \a parent and \a name parameters are the standard Qt parent parameters.*/QPETimeEdit::QPETimeEdit( const QTime &dt, QWidget *parent, const char *name ) : QSpinBox(parent, name){ setWrapping(TRUE); setMinValue(0); setMaxValue(23*60 + 59); setLineStep(15); setTime(dt); connect(this, SIGNAL(valueChanged(int)), this, SLOT(changeTimeUsingValue(int))); tv = new TimeValidator(this); QPEApplication::setInputMethodHint(this,QPEApplication::Number); setValidator(tv);}/*! \internal*/void QPETimeEdit::clockChanged(){ updateDisplay();}/*! Destructs QPETimeEdit.*/QPETimeEdit::~QPETimeEdit(){}/*! \reimp Reimplemented to validate the time entered.*/// minutes only;QString QPETimeEdit::mapValueToText(int v){ if (v >= 24*60) return QString::null; QTime t(v / 60, v % 60); return TimeString::localHM(t);}/*! \reimp Reimplemented to validate the time entered.*/int QPETimeEdit::mapTextToValue(bool *ok){ QTime res; bool test = fromString(text(),res); if (ok) *ok = test; int h = res.hour(); int m = res.minute(); int v = h * 60 + m; return v;}/*! \internal*/QTime QPETimeEdit::mapValueToTime(int v) const{ QTime tm(v / 60, v % 60,0); return tm;}/*! \internal*/void QPETimeEdit::changeTimeUsingValue(int v){ QTime tm = mapValueToTime(v); emit valueChanged(tm);}/*! Sets displayed time to \a tm. \sa time()*/void QPETimeEdit::setTime( const QTime &tm ){ if (tm == time()) return; setValue(tm.hour() * 60 + tm.minute());}/*! Returns the time selected. \sa setTime()*/QTime QPETimeEdit::time( ) const{ QTime tm = mapValueToTime(value()); return tm;}/*! Increases the time by 15 minutes.*/void QPETimeEdit::stepUp(){ int tmp = value(); tmp = tmp - (tmp % 15); tmp += 15; if (tmp > maxValue()) { if (parent() && qstrcmp(parent()->className(), "QPEDateTimeEdit") == 0) { QPEDateTimeEdit *dte = (QPEDateTimeEdit *)parent(); dte->blockSignals(TRUE); dte->setDate(dte->date().addDays(1)); dte->blockSignals(FALSE); } tmp = 0; } setValue(tmp);}/*! Decreases the time by 15 minutes.*/void QPETimeEdit::stepDown(){ int tmp = value(); if (tmp % 15) tmp = tmp - (tmp % 15); else tmp -=15; if (tmp < minValue()) { if (parent() && qstrcmp(parent()->className(), "QPEDateTimeEdit") == 0) { QPEDateTimeEdit *dte = (QPEDateTimeEdit *)parent(); dte->blockSignals(TRUE); dte->setDate(dte->date().addDays(-1)); dte->blockSignals(FALSE); } tmp = 23*60+45; } setValue(tmp);}/*=====================================*//*! \class QPEDateEdit datetimeedit.h \brief The QPEDateEdit class provides a compact widget for selecting a date. QPEDateEdit displays a QPushButton with the selected date displayed. When clicked, a QPEDatePicker is popped up and a new date can be selected. The button can display the date in either long or short format. It is also possible to allow no to date selected. \ingroup qtopiaemb*//*! \fn QPEDateEdit::valueChanged( const QDate &d ); This signal is emitted when the date is changed to \a d.*//*!
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -