kalarm/lib

dateedit.cpp

00001 /*
00002  *  dateedit.cpp  -  date entry widget
00003  *  Program:  kalarm
00004  *  Copyright © 2002-2007 by David Jarvie <software@astrojar.org.uk>
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License along
00017  *  with this program; if not, write to the Free Software Foundation, Inc.,
00018  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019  */
00020 
00021 #include <kglobal.h>
00022 #include <klocale.h>
00023 #include <kmessagebox.h>
00024 
00025 #include "dateedit.moc"
00026 
00027 
00028 DateEdit::DateEdit(QWidget* parent, const char* name)
00029     : KDateEdit(parent, name)
00030 {
00031     connect(this, SIGNAL(dateEntered(const QDate&)), SLOT(newDateEntered(const QDate&)));
00032 }
00033 
00034 void DateEdit::setMinDate(const QDate& d, const QString& errorDate)
00035 {
00036     mMinDate = d;
00037     if (mMinDate.isValid()  &&  date().isValid()  &&  date() < mMinDate)
00038         setDate(mMinDate);
00039     mMinDateErrString = errorDate;
00040 }
00041 
00042 void DateEdit::setMaxDate(const QDate& d, const QString& errorDate)
00043 {
00044     mMaxDate = d;
00045     if (mMaxDate.isValid()  &&  date().isValid()  &&  date() > mMaxDate)
00046         setDate(mMaxDate);
00047     mMaxDateErrString = errorDate;
00048 }
00049 
00050 void DateEdit::setInvalid()
00051 {
00052     setDate(QDate());
00053 }
00054 
00055 // Check a new date against any minimum or maximum date.
00056 void DateEdit::newDateEntered(const QDate& newDate)
00057 {
00058     if (newDate.isValid())
00059     {
00060         if (mMinDate.isValid()  &&  newDate < mMinDate)
00061         {
00062             pastLimitMessage(mMinDate, mMinDateErrString,
00063                      i18n("Date cannot be earlier than %1"));
00064         }
00065         else if (mMaxDate.isValid()  &&  newDate > mMaxDate)
00066         {
00067             pastLimitMessage(mMaxDate, mMaxDateErrString,
00068                      i18n("Date cannot be later than %1"));
00069         }
00070     }
00071 }
00072 
00073 void DateEdit::pastLimitMessage(const QDate& limit, const QString& error, const QString& defaultError)
00074 {
00075     QString errString = error;
00076     if (errString.isNull())
00077     {
00078         if (limit == QDate::currentDate())
00079             errString = i18n("today");
00080         else
00081             errString = KGlobal::locale()->formatDate(limit, true);
00082         errString = defaultError.arg(errString);
00083     }
00084     KMessageBox::sorry(this, errString);
00085 }
00086 
00087 void DateEdit::mousePressEvent(QMouseEvent *e)
00088 {
00089     if (isReadOnly())
00090     {
00091         // Swallow up the event if it's the left button
00092         if (e->button() == Qt::LeftButton)
00093             return;
00094     }
00095     KDateEdit::mousePressEvent(e);
00096 }
00097 
00098 void DateEdit::mouseReleaseEvent(QMouseEvent* e)
00099 {
00100     if (!isReadOnly())
00101         KDateEdit::mouseReleaseEvent(e);
00102 }
00103 
00104 void DateEdit::mouseMoveEvent(QMouseEvent* e)
00105 {
00106     if (!isReadOnly())
00107         KDateEdit::mouseMoveEvent(e);
00108 }
00109 
00110 void DateEdit::keyPressEvent(QKeyEvent* e)
00111 {
00112     if (!isReadOnly())
00113         KDateEdit::keyPressEvent(e);
00114 }
00115 
00116 void DateEdit::keyReleaseEvent(QKeyEvent* e)
00117 {
00118     if (!isReadOnly())
00119         KDateEdit::keyReleaseEvent(e);
00120 }
KDE Home | KDE Accessibility Home | Description of Access Keys