summaryrefslogtreecommitdiffabout
path: root/libkdepim/kdateedit.cpp
authorzautrix <zautrix>2004-09-13 21:35:12 (UTC)
committer zautrix <zautrix>2004-09-13 21:35:12 (UTC)
commitbd6b4acb39498ffea3beea7d9b5f7620b60c14db (patch) (side-by-side diff)
treee2d7425b7bb49fbcb7acb5ced497ac1fed06dfaf /libkdepim/kdateedit.cpp
parent06eabf6e82c0390699d11fd12580d91261829431 (diff)
downloadkdepimpi-bd6b4acb39498ffea3beea7d9b5f7620b60c14db.zip
kdepimpi-bd6b4acb39498ffea3beea7d9b5f7620b60c14db.tar.gz
kdepimpi-bd6b4acb39498ffea3beea7d9b5f7620b60c14db.tar.bz2
Many bugfixes
Diffstat (limited to 'libkdepim/kdateedit.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--libkdepim/kdateedit.cpp17
1 files changed, 14 insertions, 3 deletions
diff --git a/libkdepim/kdateedit.cpp b/libkdepim/kdateedit.cpp
index 5fb948a..c4c0081 100644
--- a/libkdepim/kdateedit.cpp
+++ b/libkdepim/kdateedit.cpp
@@ -31,193 +31,199 @@
#include <kdatepicker.h>
#include <kdebug.h>
#include <kglobal.h>
#include <kiconloader.h>
#include <klocale.h>
#include <kmessagebox.h>
#include <knotifyclient.h>
#include <qpalette.h>
#include "kdateedit.h"
//#include "kdateedit.moc"
KDateEdit::KDateEdit(QWidget *parent, const char *name, bool withoutDP )
: QHBox(parent, name)
{
dateFormShort = true;
withoutDp = withoutDP;
mDateEdit = new QLineEdit(this);
mDateEdit->setText(KGlobal::locale()->formatDate(QDate::currentDate(),dateFormShort));
setFocusProxy(mDateEdit);
mDateEdit->installEventFilter(this);
// Highlight Background and Textcolor
QPalette palette = QWidget::palette();
unsigned char red, green, blue;
red = palette.color( QPalette::Normal , QColorGroup::Background ).red() - 10;
green = palette.color( QPalette::Normal , QColorGroup::Background ).green() - 10;
blue = palette.color( QPalette::Normal , QColorGroup::Background ).blue() - 10;
palette.setColor( QColorGroup::Highlight, QColor(red,green,blue) );
palette.setColor( QColorGroup::HighlightedText, palette.color( QPalette::Normal , QColorGroup::Foreground ) );
mDateEdit->setPalette( palette );
if ( withoutDP ) {
mDateFrame = 0;
mDateButton = 0;
mDatePicker = 0;
} else {
QPixmap pixmap = SmallIcon("smallcal");
mDateButton = new QPushButton(this);
mDateButton->setPixmap(pixmap);
mDateFrame = new QVBox(0,0,WType_Popup);
// mDateFrame->setFrameStyle(QFrame::PopupPanel | QFrame::Raised);
mDateFrame->setFrameStyle( QFrame::WinPanel |QFrame::Raised );
mDateFrame->setLineWidth(3);
mDateFrame->hide();
mDatePicker = new KDatePicker(mDateFrame,QDate::currentDate());
connect(mDatePicker,SIGNAL(dateEntered(QDate)),SLOT(setDate(QDate)));
connect(mDatePicker,SIGNAL(dateEntered(QDate)),SIGNAL(dateChanged(QDate)));
connect(mDatePicker,SIGNAL(dateSelected(QDate)),SLOT(setDate(QDate)));
connect(mDatePicker,SIGNAL(dateSelected(QDate)),SIGNAL(dateChanged(QDate)));
connect(mDatePicker,SIGNAL(dateSelected(QDate)),mDateFrame,SLOT(hide()));
connect(mDateButton,SIGNAL(clicked()),SLOT(toggleDatePicker()));
//mDateFrame->resize( 400, 300 );
}
connect(mDateEdit,SIGNAL(returnPressed()),SLOT(lineEnterPressed()));
connect(mDateEdit,SIGNAL(textChanged(const QString &)),
SLOT(textChanged(const QString &)));
// Create the keyword list. This will be used to match against when the user
// enters information.
mKeywordMap[i18n("tomorrow")] = 1;
mKeywordMap[i18n("today")] = 0;
mKeywordMap[i18n("yesterday")] = -1;
/*
* This loop uses some math tricks to figure out the offset in days
* to the next date the given day of the week occurs. There
* are two cases, that the new day is >= the current day, which means
* the new day has not occured yet or that the new day < the current day,
* which means the new day is already passed (so we need to find the
* day in the next week).
*/
QString dayName;
int currentDay = QDate::currentDate().dayOfWeek();
for (int i = 1; i <= 7; ++i)
{
dayName = KGlobal::locale()->weekDayName(i).lower();
if (i >= currentDay)
mKeywordMap[dayName] = i - currentDay;
else
mKeywordMap[dayName] = 7 - currentDay + i;
}
mTextChanged = false;
mHandleInvalid = false;
QWidget::setTabOrder( mDateEdit, mDateButton );
}
KDateEdit::~KDateEdit()
{
delete mDateFrame;
}
-
+void KDateEdit::clear()
+{
+ bool b = mDateEdit->signalsBlocked();
+ mDateEdit->blockSignals(true);
+ mDateEdit->setText("");
+ mDateEdit->blockSignals(b);
+}
void KDateEdit::setDate(QDate newDate)
{
if (!newDate.isValid() && !mHandleInvalid)
return;
if ( readDate() == newDate )
return;
QString dateString = "";
if(newDate.isValid())
dateString = KGlobal::locale()->formatDate( newDate, dateFormShort );
mTextChanged = false;
// We do not want to generate a signal here, since we explicity setting
// the date
bool b = mDateEdit->signalsBlocked();
mDateEdit->blockSignals(true);
mDateEdit->setText(dateString);
mDateEdit->blockSignals(b);
}
void KDateEdit::setDate( QDate date,int *cpos,const int key ,const bool dateFormShort)
{
QString dateForm = dateFormShort ?
KGlobal::locale()->dateFormatShort() :
KGlobal::locale()->dateFormat();
int begin = dateForm.find("%");
int space = 0;
int allStrLength = 0;
int strLength = 0;
int repeat = 0;
// witch? Day, Month or Year switch?
while(1){
switch ( dateForm.at(begin + 1).latin1() )
{
case 'd':// 16 (month day)
strLength = 2; //Ok
break;
case 'm':// 01 (month)
strLength = 2; //Ok
break;
case 'a':// Mon (Weekday)
strLength = KGlobal::locale()->weekDayName(date.dayOfWeek(), true).length();
break;
case 'A':// Monday (Weekday)
strLength = KGlobal::locale()->weekDayName(date.dayOfWeek(), false).length();
break;
case 'b':// Jan (monthName)
strLength = KGlobal::locale()->monthName(date.month(), true).length();
break;
case 'B':// January (monthName)
strLength = KGlobal::locale()->monthName(date.month(), false).length();
break;
case 'y':// 04 (year short)
strLength = 2; //Ok
break;
case 'Y':// 2004 (year)
strLength = 4; //Ok
break;
default:
break;
}
space = begin - (repeat++ * 2);
// all select? then dayswitch
if( (mDateEdit->text().length() == mDateEdit->markedText().length() ) &&
( (dateForm.at(begin + 1).latin1() == 'd') ||
(dateForm.at(begin + 1).latin1() == 'a') ||
(dateForm.at(begin + 1).latin1() == 'A') ) ) {
break;
}
// mDateEdit-StringPos == CursorPosition(cpos) then break and set date
if( ( (space + allStrLength) <= *cpos && *cpos <= (space + allStrLength + strLength) ) || *cpos < begin ) {
break;
}
allStrLength += strLength;
begin = dateForm.find("%", begin +1);
}
// set date
switch ( dateForm.at(begin + 1).latin1() ) {
case 'd':
case 'a':
case 'A':
if(key == Key_Up) {
setDate( date.addDays( 1 ) );
}
else if(key == Key_Down) {
setDate( date.addDays( -1 ) );
}
maxDay = readDate().day();
break;
case 'm':
case 'b':
case 'B':
if(key == Key_Up) {
@@ -370,117 +376,122 @@ void KDateEdit::setSelect( int from, int to )
{
// return;
mDateEdit->setSelection( from , to );
}
void KDateEdit::toggleDatePicker()
{
if( mDateFrame->isVisible() ) {
mDateFrame->hide();
} else {
QPoint tmpPoint = mapToGlobal(mDateButton->geometry().bottomRight());
QSize datepickersize = mDatePicker->sizeHint();
if ( tmpPoint.x() < 7+datepickersize.width() ) tmpPoint.setX( 7+datepickersize.width() );
int h = QApplication::desktop()->height();
if ( tmpPoint.y() + datepickersize.height() > h ) tmpPoint.setY( h - datepickersize.height() );
mDateFrame->setGeometry(tmpPoint.x()-datepickersize.width()-7, tmpPoint.y(),
datepickersize.width()+2*mDateFrame->lineWidth(), datepickersize.height()+2*mDateFrame->lineWidth());
QDate date = readDate();
if(date.isValid()) {
mDatePicker->setDate(date);
} else {
mDatePicker->setDate(QDate::currentDate());
}
mDateFrame->show();
}
}
void KDateEdit::lineEnterPressed()
{
QDate date = readDate();
if(date.isValid())
{
// Update the edit. This is needed if the user has entered a
// word rather than the actual date.
setDate(date);
emit(dateChanged(date));
emit returnPressed();
}
else
{
if ( withoutDp ) {
KNotifyClient::beep();
} else {
if ( !mDateEdit->text().isEmpty() ) {
mTextChanged = false;
QString text = i18n( "You entered an invalid date!\n Will use current date instead." );
if ( KMessageBox::warningContinueCancel( 0, text ) == KMessageBox::Continue ) {
setDate( QDate::currentDate() );
emit dateChanged( QDate::currentDate() );
}
}
}
}
}
bool KDateEdit::inputIsValid()
{
return readDate().isValid();
}
QDate KDateEdit::readDate() const
{
QString text = mDateEdit->text();
QDate date;
if (mKeywordMap.contains(text.lower()))
{
date = QDate::currentDate().addDays(mKeywordMap[text.lower()]);
}
else
{
date = KGlobal::locale()->readDate(text);
}
return date;
}
bool KDateEdit::eventFilter(QObject *, QEvent *e)
{
// We only process the focus out event if the text has changed
// since we got focus
if ((e->type() == QEvent::FocusOut) && mTextChanged)
{
lineEnterPressed();
mTextChanged = false;
}
// switch dateFormShort by double klick with mouse
else if (e->type() == QEvent::MouseButtonDblClick)
{
- dateFormShort = dateFormShort?false:true;
- mDateEdit->setText(KGlobal::locale()->formatDate(readDate(),dateFormShort));
+ toggleDateFormat();
}
else if (e->type() == QEvent::FocusIn)
{
maxDay = readDate().day();
}
return false;
}
+void KDateEdit::toggleDateFormat()
+{
+ dateFormShort = ! dateFormShort;
+ mDateEdit->setText(KGlobal::locale()->formatDate(readDate(),dateFormShort));
+
+}
void KDateEdit::textChanged(const QString &)
{
if(mHandleInvalid && mDateEdit->text().stripWhiteSpace().isEmpty()) {
QDate date; //invalid date
emit(dateChanged(date));
} else {
mTextChanged = true;
}
maxDay = readDate().day();
}