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 <tdeglobal.h>
00022 #include <tdelocale.h>
00023 #include <tdemessagebox.h>
00024 
00025 #include "dateedit.moc"
00026 
00027 
00028 DateEdit::DateEdit(TQWidget* parent, const char* name)
00029     : KDateEdit(parent, name)
00030 {
00031     connect(this, TQT_SIGNAL(dateEntered(const TQDate&)), TQT_SLOT(newDateEntered(const TQDate&)));
00032 }
00033 
00034 void DateEdit::setMinDate(const TQDate& d, const TQString& 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 TQDate& d, const TQString& 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(TQDate());
00053 }
00054 
00055 // Check a new date against any minimum or maximum date.
00056 void DateEdit::newDateEntered(const TQDate& 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             setDate(mMinDate);
00065         }
00066         else if (mMaxDate.isValid()  &&  newDate > mMaxDate)
00067         {
00068             pastLimitMessage(mMaxDate, mMaxDateErrString,
00069                      i18n("Date cannot be later than %1"));
00070             setDate(mMaxDate);
00071         }
00072     }
00073 }
00074 
00075 void DateEdit::pastLimitMessage(const TQDate& limit, const TQString& error, const TQString& defaultError)
00076 {
00077     TQString errString = error;
00078     if (errString.isNull())
00079     {
00080         if (limit == TQDate::currentDate())
00081             errString = i18n("today");
00082         else
00083             errString = TDEGlobal::locale()->formatDate(limit, true);
00084         errString = defaultError.arg(errString);
00085     }
00086     KMessageBox::sorry(this, errString);
00087 }
00088 
00089 void DateEdit::mousePressEvent(TQMouseEvent *e)
00090 {
00091     if (isReadOnly())
00092     {
00093         // Swallow up the event if it's the left button
00094         if (e->button() == Qt::LeftButton)
00095             return;
00096     }
00097     KDateEdit::mousePressEvent(e);
00098 }
00099 
00100 void DateEdit::mouseReleaseEvent(TQMouseEvent* e)
00101 {
00102     if (!isReadOnly())
00103         KDateEdit::mouseReleaseEvent(e);
00104 }
00105 
00106 void DateEdit::mouseMoveEvent(TQMouseEvent* e)
00107 {
00108     if (!isReadOnly())
00109         KDateEdit::mouseMoveEvent(e);
00110 }
00111 
00112 void DateEdit::keyPressEvent(TQKeyEvent* e)
00113 {
00114     if (!isReadOnly())
00115         KDateEdit::keyPressEvent(e);
00116 }
00117 
00118 void DateEdit::keyReleaseEvent(TQKeyEvent* e)
00119 {
00120     if (!isReadOnly())
00121         KDateEdit::keyReleaseEvent(e);
00122 }