lfpport_qte_dateeditor.cpp

来自「This is a resource based on j2me embedde」· C++ 代码 · 共 1,029 行 · 第 1/2 页

CPP
1,029
字号
/* * * * Copyright  1990-2007 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER *  * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License version * 2 only, as published by the Free Software Foundation. *  * 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 version 2 for more details (a copy is * included at /legal/license.txt). *  * You should have received a copy of the GNU General Public License * version 2 along with this work; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA *  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa * Clara, CA 95054 or visit www.sun.com if you need additional * information or have any questions. *  * This source file is specific for Qt-based configurations. */#include <qpainter.h>#include <qevent.h>#include <qlabel.h>#include <qlayout.h>#include <qspinbox.h>#include <qcombobox.h>#include <sys/time.h>#include <stdlib.h>#include "lfpport_qte_util.h"#include "lfpport_qte_patched.h"#include <lfpport_form.h>#include "lfpport_qte_dateeditor.h"#include <moc_lfpport_qte_dateeditor.cpp>#include <qteapp_export.h>#include <midpStorage.h>#include <midp_logging.h>#define TIME_WIDGET_HEIGHT 25#define DATE_WIDGET_HEIGHT 28#define DATE_FORMAT_HINT   "www mm dd, yyyy"static const int ValueAM = 0;static const int ValuePM = 1;/** * Constructs DateEditor widget * * @param longDate when TRUE selects long format for *   date string, otherwise short format will be used * @param parent reference to the parent container */DateEditor::DateEditor(bool longDate, DateTimeEditor *parent) :    QPushButton(parent) {    TRACE_DE(  DateEditor::DateEditor..);    QString qiconPath;    pcsl_string* iconPath = (pcsl_string*)        storage_get_config_root(INTERNAL_STORAGE_ID);    pcsl_string2QString(*iconPath, qiconPath);    QPixmap qicon(qiconPath + "calendar.png");    setPixmap(qicon);    setFixedWidth(qicon.width() + 6);    setFixedHeight(qicon.height() + 6);    dte = parent;    setFocusPolicy(QWidget::StrongFocus);    longFormat = longDate;    currDate = QDate::currentDate();    picker = NULL;    connect(this,SIGNAL(clicked()),this,SLOT(pickDate()));    TRACE_DE(..DateEditor::DateEditor);}/** * Makes this item have focus, enabling any item-specific commands; in * addition, if the given event was caused by a user action, notifies the * Java platform of the change in focus. * * @param event device event corresponding to the change in focus. */void DateEditor::focusInEvent(QFocusEvent *event) {    TRACE_DE(DateEditor::focusInEvent);    // Notify Java if this is caused by user action    if (event->reason() != QFocusEvent::Other) {        MidpFormFocusChanged(parentWidget()->parentWidget());    }    QPushButton::focusInEvent(event);}/** * If in undefined state and min or hour got a new value, * than switch from undefined state to a defined default state */void DateEditor::setToDefault() {   dte->dateLabel->setText(longFormat ?                           TimeUtils::longDateString(currDate) :                           TimeUtils::shortDateString(currDate) );}/** The user has picked a date */void DateEditor::pickDate() {    TRACE_DE(  DateEditor::pickDate..);    if (picker == NULL) {        picker = new DatePicker(currDate, QPoint(), this);    }    picker->show();    TRACE_DE(..DateEditor::pickDate);}/** Service slot for hiding the date editor */void DateEditor::gotHide() {    TRACE_DE(  DateEditor::gotHide..);    // we have to redo the button...    setDown( false );    TRACE_DE(..DateEditor::gotHide);}/** * Slot used to set date as picked by the user from the editor * * @param y year value to be set * @param m month value to be set * @param d day value to be set */void DateEditor::setDate(int y, int m, int d) {    TRACE_DE(  DateEditor::setDate..);    setDate( QDate( y,m,d) );    TRACE_DE(..DateEditor::setDate);}/** * Slot used to set date as picked by the user from the editor * * @param d date value to be set */void DateEditor::setDate(QDate d) {    TRACE_DE(  DateEditor::setDate..);    dte->time->stackCounter++;    currDate = d;    dte->dateLabel->setText(longFormat ?        TimeUtils::longDateString(d) :        TimeUtils::shortDateString(d) );    // if was undefined state - set initial values to hour and minute    if (dte->time->undefinedState) {        dte->time->setUndefinedState(FALSE);    }    if (dte->time->needPeerNotification && dte->time->stackCounter == 1) {        MidpFormItemPeerStateChanged(dte->parentWidget(), 1);    }    dte->time->stackCounter--;    TRACE_DE(..DateEditor::setDate);}/** * Constructor of time editor widget * * @param parent parent widget * @param initialTime time to show on widget creation */SetTime::SetTime(DateTimeEditor *parent, long initialTime ) :    QFrame( parent ) {    TRACE_DE(  SetTime::SetTime..);    // initial state of peer notification is TRUE (on).    // when value is set programmatically the flaf is set to false temporarily    needPeerNotification = TRUE;    stackCounter = 0;    dte = parent;    setFocusPolicy(QWidget::StrongFocus);    use12hourTime = FALSE;    QHBoxLayout *hb2 = new QHBoxLayout( this );    hb2->setSpacing( 3 );    QLabel *l = new QLabel( tr("(HH:MM)"), this );    hb2->addWidget(l);    sbHour = new QSpinBox( this );    sbHour->setFocusPolicy(QWidget::StrongFocus);    sbHour->setFixedWidth(40);    if(use12hourTime) {        sbHour->setMinValue(1);        sbHour->setMaxValue(12);        int show_hour = hour;        if (hour > 12) {            show_hour -= 12;        }        if (show_hour == 0) {            show_hour = 12;        }        sbHour->setValue(show_hour);    } else {        sbHour->setMinValue(0);        sbHour->setMaxValue(23);    }    sbHour->setWrapping(TRUE);    connect( sbHour, SIGNAL(valueChanged(int)),	    this, SLOT(hourChanged(int)) );    hb2->addWidget(sbHour);    // minutes    sbMin = new QSpinBox( this );    sbMin->setFocusPolicy(QWidget::StrongFocus);    // sbMin->setBackgroundColor(Qt::white);    sbMin->setMinValue(0);    sbMin->setMaxValue(59);    sbMin->setWrapping(TRUE);    sbMin->setFixedWidth(40);    connect(sbMin, SIGNAL(valueChanged(int)),       this, SLOT(minuteChanged(int)));    hb2->addWidget(sbMin);    if (use12hourTime) {        ampm = new QComboBox( this );        // ampm->setBackgroundColor(Qt::white);        ampm->insertItem(tr("AM"), ValueAM);        ampm->insertItem(tr("PM"), ValuePM);        ampm->setFixedWidth(40);        connect(ampm, SIGNAL(activated(int)),	        this, SLOT(checkedPM(int)));        hb2->addWidget(ampm);    }    if (initialTime == 0) {        undefinedState = FALSE;        setUndefinedState(TRUE);    } else {        undefinedState = FALSE;        QDateTime *init = new QDateTime();        init->setTime_t((uint)(initialTime));        hour = init->time().hour();        minute = init->time().minute();        sbHour->setValue(hour);        sbMin->setValue(minute);    }    TRACE_DE(..SetTime::SetTime);}/** * Makes this item have focus, enabling any item-specific commands; in * addition, if the given event was caused by a user action, notifies * the Java platform of the change in focus. * * @param event device event corresponding to the change in focus. */void SetTime::focusInEvent(QFocusEvent *event) {    TRACE_DE(SetTime::focusInEvent);    // show cursor if no cursor is shown    if (!sbHour->hasFocus() && !sbMin->hasFocus()) {        sbHour->setFocus();    }    // Notify Java if this is caused by user action    MidpFormFocusChanged(parentWidget()->parentWidget());    QWidget::focusInEvent(event);}/** * Undefined state appears as "HH:MM" and no date selected. * This method allows to switch from defined to undefined appearance. * * @param turnOn true if the state should be set to undefined mode */void SetTime::setUndefinedState(bool turnOn) {    // if already at the same state - return    if (! (turnOn ^ undefinedState)) {        return;    }    TRACE_DE(  SetTime::setUndefinedState..);    if (turnOn && (undefinedState == FALSE)) {        undefinedState = TRUE;        sbHour->setSpecialValueText("HH");        sbMin->setSpecialValueText("MM");        sbHour->setMinValue(sbHour->minValue() - 1);        sbMin->setMinValue(sbMin->minValue() - 1);        sbHour->setValue(sbHour->minValue());        sbMin->setValue(sbMin->minValue());    } else if (!turnOn && (undefinedState == TRUE)) {        undefinedState = FALSE;        sbHour->setSpecialValueText("");        sbMin->setSpecialValueText("");        sbHour->setMinValue(sbHour->minValue() + 1);        sbMin->setMinValue(sbMin->minValue() + 1);        sbHour->setValue(sbHour->minValue());        sbMin->setValue(sbMin->minValue());    }    TRACE_DE(..SetTime::setUndefinedState);}/** * Gets time set in time editor * * @return currently set QTime (hour and minute) */QTime SetTime::time() const {    return QTime(hour, minute);}/** * Uses this method to set value of this date widget. * * @param hour hour to be set * @param min minute to be set */void SetTime::setTime(int ihour, int imin) {    TRACE_DE(  SetTime::setTime..);    hour = ihour;    minute = imin;    sbHour->setValue(hour);    sbMin->setValue(minute);    TRACE_DE(..SetTime::setTime);}/** * This method receives hour changed event. * * @param value new hour */void SetTime::hourChanged(int value) {    TRACE_DE(  SetTime::hourChanged..);    stackCounter++;    // sets parent's focus    QWidget::setFocus();    if (undefinedState) {        if (value != sbHour->minValue()) {            setUndefinedState(FALSE);            sbHour->setValue(value);            dte->date->setToDefault();        } else {            stackCounter--;            return;        }    }    if(use12hourTime) {        int realhour = value;        if (realhour == 12)            realhour = 0;        if (ampm->currentItem() == ValuePM )            realhour += 12;        hour = realhour;    } else {        hour = value;    }    sbHour->setFocus();    if (needPeerNotification && stackCounter == 1) {        // 2 = hint that hour has changed        MidpFormItemPeerStateChanged(parentWidget()->parentWidget(), 2);    }    stackCounter--;    TRACE_DE(..SetTime::hourChanged);}/** * This method receives minute changed event * * @param value new minute */void SetTime::minuteChanged(int value) {    TRACE_DE(  SetTime::minuteChanged..);    stackCounter++;    QWidget::setFocus();    if (undefinedState) {        if (value != sbMin->minValue()) {            setUndefinedState(FALSE);            sbMin->setValue(value);            dte->date->setToDefault();        } else {            stackCounter--;            return;        }    }    minute = value;    sbMin->setFocus();    if (needPeerNotification && stackCounter == 1) {        // 3 = hint that minutes has changed        MidpFormItemPeerStateChanged(parentWidget()->parentWidget(), 3);    }    stackCounter--;    TRACE_DE(..SetTime::minuteChanged);}/** * Changes display and edit mode to 12 or 24 hour. * * @param on 0 if 24 hour mode, otherwise 12 hour mode */void SetTime::show12hourTime(int on) {    TRACE_DE(  SetTime::show12hourTime..);    use12hourTime = on;    ampm->setEnabled(on);    int show_hour = hour;    if ( on ) {	    /* this might change the value of hour */	    sbHour->setMinValue(1);	    sbHour->setMaxValue(12);	    /* so use one we saved earlier */	    if (show_hour >= 12) {	        show_hour -= 12;	        ampm->setCurrentItem( ValuePM );	    } else {	        ampm->setCurrentItem( ValueAM );	    }	    if (show_hour == 0)	        show_hour = 12;    } else {	    sbHour->setMinValue( 0 );	    sbHour->setMaxValue( 23 );    }    sbHour->setValue( show_hour );    TRACE_DE(..SetTime::show12hourTime);}/** * This method receives pm/am changed event * * @param index the position of the selected item */void SetTime::checkedPM(int c) {    TRACE_DE(  SetTime::checkedPM..);    int show_hour = sbHour->value();    if (show_hour == 12)	    show_hour = 0;    if (c == ValuePM )	    show_hour += 12;    hour = show_hour;    TRACE_DE(..SetTime::checkedPM);}/** * Constructs DateTimeEditor widget. * * @param parent a pointer to the parent * @param dmode date and/or time input mode selection. *   1 or 3 (binary mask 01) = date editor is on *   2 or 3 (binary mask 10) = time editor is on * @param initialTime UTC time to initialize widget instance with */DateTimeEditor::DateTimeEditor(QWidget *parent, int dmode, long initialTime) :    QWidget( parent) {

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?