kalarm/lib

dateedit.cpp

00001 /*
00002  *  dateedit.cpp  -  date entry widget
00003  *  Program:  kalarm
00004  *  Copyright (c) 2002 - 2005 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 }
00032 
00033 void DateEdit::setMinDate(const QDate& d, const QString& errorDate)
00034 {
00035     mMinDate = d;
00036     if (mMinDate.isValid()  &&  date().isValid()  &&  date() < mMinDate)
00037         setDate(mMinDate);
00038     mMinDateErrString = errorDate;
00039 }
00040 
00041 void DateEdit::setMaxDate(const QDate& d, const QString& errorDate)
00042 {
00043     mMaxDate = d;
00044     if (mMaxDate.isValid()  &&  date().isValid()  &&  date() > mMaxDate)
00045         setDate(mMaxDate);
00046     mMaxDateErrString = errorDate;
00047 }
00048 
00049 void DateEdit::setInvalid()
00050 {
00051     setDate(QDate());
00052 }
00053 
00054 // Check a new date against any minimum or maximum date.
00055 bool DateEdit::assignDate(const QDate& newDate)
00056 {
00057     if (newDate.isValid())
00058     {
00059         if (mMinDate.isValid()  &&  newDate < mMinDate)
00060         {
00061             pastLimitMessage(mMinDate, mMinDateErrString,
00062                      i18n("Date cannot be earlier than %1"));
00063             return false;
00064         }
00065         if (mMaxDate.isValid()  &&  newDate > mMaxDate)
00066         {
00067             pastLimitMessage(mMaxDate, mMaxDateErrString,
00068                      i18n("Date cannot be later than %1"));
00069             return false;
00070         }
00071     }
00072     return KDateEdit::assignDate(newDate);
00073 }
00074 
00075 void DateEdit::pastLimitMessage(const QDate& limit, const QString& error, const QString& defaultError)
00076 {
00077     QString errString = error;
00078     if (errString.isNull())
00079     {
00080         if (limit == QDate::currentDate())
00081             errString = i18n("today");
00082         else
00083             errString = KGlobal::locale()->formatDate(limit, true);
00084         errString = defaultError.arg(errString);
00085     }
00086     KMessageBox::sorry(this, errString);
00087 }
00088 
00089 void DateEdit::mousePressEvent(QMouseEvent *e)
00090 {
00091     if (isReadOnly())
00092     {
00093         // Swallow up the event if it's the left button
00094         if (e->button() == LeftButton)
00095             return;
00096     }
00097     KDateEdit::mousePressEvent(e);
00098 }
00099 
00100 void DateEdit::mouseReleaseEvent(QMouseEvent* e)
00101 {
00102     if (!isReadOnly())
00103         KDateEdit::mouseReleaseEvent(e);
00104 }
00105 
00106 void DateEdit::mouseMoveEvent(QMouseEvent* e)
00107 {
00108     if (!isReadOnly())
00109         KDateEdit::mouseMoveEvent(e);
00110 }
00111 
00112 void DateEdit::keyPressEvent(QKeyEvent* e)
00113 {
00114     if (!isReadOnly())
00115         KDateEdit::keyPressEvent(e);
00116 }
00117 
00118 void DateEdit::keyReleaseEvent(QKeyEvent* e)
00119 {
00120     if (!isReadOnly())
00121         KDateEdit::keyReleaseEvent(e);
00122 }
KDE Home | KDE Accessibility Home | Description of Access Keys