kalarm/lib

dateedit.cpp
1 /*
2  * dateedit.cpp - date entry widget
3  * Program: kalarm
4  * Copyright © 2002-2007 by David Jarvie <software@astrojar.org.uk>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  */
20 
21 #include <tdeglobal.h>
22 #include <tdelocale.h>
23 #include <tdemessagebox.h>
24 
25 #include "dateedit.moc"
26 
27 
28 DateEdit::DateEdit(TQWidget* parent, const char* name)
29  : KDateEdit(parent, name)
30 {
31  connect(this, TQ_SIGNAL(dateEntered(const TQDate&)), TQ_SLOT(newDateEntered(const TQDate&)));
32 }
33 
34 void DateEdit::setMinDate(const TQDate& d, const TQString& errorDate)
35 {
36  mMinDate = d;
37  if (mMinDate.isValid() && date().isValid() && date() < mMinDate)
38  setDate(mMinDate);
39  mMinDateErrString = errorDate;
40 }
41 
42 void DateEdit::setMaxDate(const TQDate& d, const TQString& errorDate)
43 {
44  mMaxDate = d;
45  if (mMaxDate.isValid() && date().isValid() && date() > mMaxDate)
46  setDate(mMaxDate);
47  mMaxDateErrString = errorDate;
48 }
49 
51 {
52  setDate(TQDate());
53 }
54 
55 // Check a new date against any minimum or maximum date.
56 void DateEdit::newDateEntered(const TQDate& newDate)
57 {
58  if (newDate.isValid())
59  {
60  if (mMinDate.isValid() && newDate < mMinDate)
61  {
62  pastLimitMessage(mMinDate, mMinDateErrString,
63  i18n("Date cannot be earlier than %1"));
64  setDate(mMinDate);
65  }
66  else if (mMaxDate.isValid() && newDate > mMaxDate)
67  {
68  pastLimitMessage(mMaxDate, mMaxDateErrString,
69  i18n("Date cannot be later than %1"));
70  setDate(mMaxDate);
71  }
72  }
73 }
74 
75 void DateEdit::pastLimitMessage(const TQDate& limit, const TQString& error, const TQString& defaultError)
76 {
77  TQString errString = error;
78  if (errString.isNull())
79  {
80  if (limit == TQDate::currentDate())
81  errString = i18n("today");
82  else
83  errString = TDEGlobal::locale()->formatDate(limit, true);
84  errString = defaultError.arg(errString);
85  }
86  KMessageBox::sorry(this, errString);
87 }
88 
89 void DateEdit::mousePressEvent(TQMouseEvent *e)
90 {
91  if (isReadOnly())
92  {
93  // Swallow up the event if it's the left button
94  if (e->button() == TQt::LeftButton)
95  return;
96  }
97  KDateEdit::mousePressEvent(e);
98 }
99 
100 void DateEdit::mouseReleaseEvent(TQMouseEvent* e)
101 {
102  if (!isReadOnly())
103  KDateEdit::mouseReleaseEvent(e);
104 }
105 
106 void DateEdit::mouseMoveEvent(TQMouseEvent* e)
107 {
108  if (!isReadOnly())
109  KDateEdit::mouseMoveEvent(e);
110 }
111 
112 void DateEdit::keyPressEvent(TQKeyEvent* e)
113 {
114  if (!isReadOnly())
115  KDateEdit::keyPressEvent(e);
116 }
117 
118 void DateEdit::keyReleaseEvent(TQKeyEvent* e)
119 {
120  if (!isReadOnly())
121  KDateEdit::keyReleaseEvent(e);
122 }
bool isValid() const
Returns true if the widget contains a valid date.
Definition: dateedit.h:48
void setMaxDate(const TQDate &date, const TQString &errorDate=TQString())
Sets the latest date which can be entered.
Definition: dateedit.cpp:42
void setInvalid()
Sets the date held in the widget to an invalid date.
Definition: dateedit.cpp:50
DateEdit(TQWidget *parent=0, const char *name=0)
Constructor.
Definition: dateedit.cpp:28
void setMinDate(const TQDate &date, const TQString &errorDate=TQString())
Sets the earliest date which can be entered.
Definition: dateedit.cpp:34
bool isReadOnly() const
void setDate(const TQDate &date)
TQDate date() const
void dateEntered(const TQDate &date)