From 446f4b7364fb04cc19a4bb966d08aff74505c755 Mon Sep 17 00:00:00 2001 From: Jason Harris Date: Mon, 5 Sep 2005 07:36:55 +0000 Subject: [PATCH] 1. Make new tests directory compile. I have reverted Pino's "#if 0" comments, because the code in question does not cause a compile error here. Please send me your compile error instead of disabling the problem code. 2. The ExtDateEdit and ExtDateTimeEdit classes should fully work now. ExtDateEdit is a QSpinBox showing a date using the user's localized "short date" format. The up/down buttons of the spin box (or the Up/Down arrow keys) will modify the Day/Month/Year value, depending on which is highlighted. You can change which field is highlighted using the Tab/Shift-Tab or Right/Left arrow keys. TODO: allow the user to type numerical data into the date fields. add apidox to the new classes. 3. Added a new test program for ExtDate[Time]Edit: test_extdatetimeedit. Note that with the new "tests" subdirectory, you must specify a target. Does anyone know how to modify Makefile.am so that simply typing "unsermake" in 'tests' will make all test programs? Until then, you must actually type: unsermake test_extdate unsermake test_extdatepicker unsermake test_extdatetimeedit CCMAIL: kstars-devel@kde.org CCMAIL: toscano.pino@tiscali.it svn path=/trunk/KDE/kdeedu/libkdeedu/; revision=457243 --- extdate/extdatetimeedit.cpp | 174 ++++++++++++++++-- extdate/extdatetimeedit.h | 44 ++++- extdate/tests/Makefile.am | 15 +- extdate/tests/{main.cpp => edpicker_main.cpp} | 10 +- .../{testwidget.cpp => edpicker_widget.cpp} | 23 ++- .../tests/{testwidget.h => edpicker_widget.h} | 22 +-- extdate/tests/edtedit_main.cpp | 30 +++ extdate/tests/edtedit_widget.cpp | 47 +++++ extdate/tests/edtedit_widget.h | 42 +++++ extdate/tests/test_extdate.cc | 3 +- 10 files changed, 357 insertions(+), 53 deletions(-) rename extdate/tests/{main.cpp => edpicker_main.cpp} (80%) rename extdate/tests/{testwidget.cpp => edpicker_widget.cpp} (83%) rename extdate/tests/{testwidget.h => edpicker_widget.h} (83%) create mode 100644 extdate/tests/edtedit_main.cpp create mode 100644 extdate/tests/edtedit_widget.cpp create mode 100644 extdate/tests/edtedit_widget.h diff --git a/extdate/extdatetimeedit.cpp b/extdate/extdatetimeedit.cpp index 673a54c..61967c8 100644 --- a/extdate/extdatetimeedit.cpp +++ b/extdate/extdatetimeedit.cpp @@ -16,10 +16,14 @@ #include #include +#include #include +#include #include #include #include +#include + #include "extdatetime.h" #include "extdatetimeedit.h" @@ -28,21 +32,81 @@ ExtDateEdit::ExtDateEdit( const ExtDate &d, QWidget *parent ) : QSpinBox( parent ), ActiveField(0), m_Date(d) { setRange( -20000000, 20000000 ); //range of Julian Days - //Set the date format to be the Locale's short date format, except - //always use full years instead of two-digit years + //Set the date format to be the Locale's short date format, except: + //always use full years instead of trimming ti two digits + //and always use two-digit days and months m_DateFormat = KGlobal::locale()->dateFormatShort(); m_DateFormat.replace( "y", "Y" ); + m_DateFormat.replace( "n", "m" ); + m_DateFormat.replace( "e", "d" ); + + //Make sure highlight is persistent when value is changed + connect( this, SIGNAL( valueChanged( int ) ), this, SLOT( slotRefreshHighlight() ) ); + + edLineEdit *edle = new edLineEdit( this ); + setLineEdit(edle); + + setValue( m_Date.jd() ); + highlightActiveField(); } -#if 0 ExtDateEdit::ExtDateEdit( int jd, QWidget *parent ) { ExtDateEdit( ExtDate(jd), parent ); } -#endif ExtDateEdit::~ExtDateEdit() { } +QString ExtDateEdit::simpleDateFormat() { + //Convert the KDE date format string (e.g., "%Y-%m-%d") to one + //that accurately represents the number of digits in each date + //field (e.g., "YYYY-MM-DD"). Note that while the Months and + //Days fields will always have two digits, the number of digits + //in the Years field depends on the displayed year. + QString result = m_DateFormat; + result.replace( "%Y", "YYYY" ); + result.replace( "%m", "MM" ); + result.replace( "%d", "DD" ); + + int i=result.indexOf( "Y" ); + int dLength = result.length() - cleanText().length(); + if ( dLength > 0 ) { //the years field must have more than 4 digits + for ( uint j=0; jsetCursorPosition( iStart + iLength ); + lineEdit()->setSelection( iStart, iLength ); + +// //DEBUG +// kdDebug() << "selected text: " << lineEdit()->selectedText() << endl; + +} + +void ExtDateEdit::slotRefreshHighlight() { + highlightActiveField(); +} + void ExtDateEdit::stepBy( int steps ) { switch ( ActiveField ) { case 0: //days field @@ -84,31 +148,117 @@ int ExtDateEdit::valueFromText( const QString &text ) const { return INVALID_DAY; } -void ExtDateEdit::paintEvent( QPaintEvent *e ) { - QSpinBox::paintEvent( e ); +//Warning: this function assumes that the lineEdit() contains +//no prefix or suffix. I believe this assumption is always valid, +//but want to be explicit about this. +bool ExtDateEdit::focusNextPrevChild( bool next ) { + if ( !focusWidget() ) return false; + + int NewField = ActiveField; + int pos = lineEdit()->cursorPosition(); //assumes no prefix/suffix! + int step = ( next ? 1 : -1 ); + + QString sdf = simpleDateFormat(); + while ( NewField == ActiveField ) { + pos += step; + + if ( pos >= sdf.length() || pos < 0 ) + return QSpinBox::focusNextPrevChild( next ); + + QChar c = sdf.at(pos); + + if ( c == 'D' ) NewField = 0; + if ( c == 'M' ) NewField = 1; + if ( c == 'Y' ) NewField = 2; + +// //DEBUG +// kdDebug() << pos << " " << c << " " << NewField << endl; + + } + + ActiveField = NewField; + highlightActiveField(); + return true; } -void ExtDateEdit::keyPressEvent( QKeyEvent *e ) { - e->ignore(); +void ExtDateEdit::invokeKey( Qt::Key k ) { + QKeyEvent *e = new QKeyEvent( QEvent::KeyPress, k, 0, 0 ); + keyPressEvent( e ); + delete e; +} + +void ExtDateEdit::focusInEvent( QFocusEvent *e ) { +// //DEBUG +// kdDebug() << "focusInEvent()" << endl; + + QSpinBox::focusInEvent(e); + highlightActiveField(); } ExtDateTimeEdit::ExtDateTimeEdit( const ExtDateTime &dt, QWidget *parent ) : QWidget( parent ) { QHBoxLayout *hlay = new QHBoxLayout( this ); - m_DateEdit = new ExtDateEdit( dt.date(), parent ); - m_TimeEdit = new QTimeEdit( dt.time(), parent ); + m_DateEdit = new ExtDateEdit( dt.date(), this ); + m_TimeEdit = new QTimeEdit( dt.time(), this ); hlay->addWidget( m_DateEdit ); hlay->addWidget( m_TimeEdit ); } -#if 0 ExtDateTimeEdit::ExtDateTimeEdit( const ExtDate &d, const QTime &t, QWidget *parent ) { ExtDateTimeEdit( ExtDateTime( d, t ), parent ); } -#endif ExtDateTimeEdit::~ExtDateTimeEdit() { } +edLineEdit::edLineEdit( QWidget *parent ) : QLineEdit( parent ) { + edParent = (ExtDateEdit*)parent; +} + +void edLineEdit::mouseReleaseEvent( QMouseEvent * ) { +// //DEBUG +// kdDebug() << "mousePressEvent()" << endl; + + //assumes no prefix/suffix! + QString sdf = edParent->simpleDateFormat(); + int p = cursorPosition(); + if ( p >= sdf.length() ) p = sdf.length() - 1; + QChar c = sdf.at( p ); + while ( c != 0 && c != 'D' && c != 'M' && c != 'Y' ) + c = edParent->simpleDateFormat().at( --p ); + + if ( c == 'D' ) edParent->setActiveField( 0 ); + else if ( c == 'M' ) edParent->setActiveField( 1 ); + else if ( c == 'Y' ) edParent->setActiveField( 2 ); + +// //DEBUG +// kdDebug() << "ActiveField = " << edParent->activeField() << endl; + + edParent->highlightActiveField(); +} + +void edLineEdit::keyPressEvent( QKeyEvent *e ) { +// //DEBUG +// kdDebug() << "keyPressEvent()" << endl; + + switch ( e->key() ) { + case Qt::Key_Up: + edParent->stepBy( 1 ); + break; + case Qt::Key_Down: + edParent->stepBy( -1 ); + break; + case Qt::Key_Left: + edParent->invokeKey( Qt::Key_BackTab ); + break; + case Qt::Key_Right: + edParent->invokeKey( Qt::Key_Tab ); + break; + default: + e->ignore(); + break; + } +} + #include "extdatetimeedit.moc" diff --git a/extdate/extdatetimeedit.h b/extdate/extdatetimeedit.h index de94e7d..98b8d3a 100644 --- a/extdate/extdatetimeedit.h +++ b/extdate/extdatetimeedit.h @@ -48,7 +48,7 @@ #include #include - +#include class QTime; class QTimeEdit; class ExtDate; @@ -59,20 +59,32 @@ class ExtDateEdit : public QSpinBox { public: ExtDateEdit( const ExtDate &d = ExtDate::currentDate(), QWidget *parent = 0 ); -#if 0 ExtDateEdit( int jd, QWidget *parent = 0 ); -#endif ~ExtDateEdit(); void stepBy( int steps ); QValidator::State validate( QString &input, int &pos ); + ExtDate date() const { return m_Date; } + void setDate( const ExtDate &d ) { m_Date = d; } + + int activeField() const { return ActiveField; } + void setActiveField( int i ) { ActiveField = i; } + + void highlightActiveField(); + QString simpleDateFormat(); + void invokeKey( Qt::Key k ); + protected: QString textFromValue( int v ) const; int valueFromText( const QString &text ) const; - void paintEvent( QPaintEvent *e ); - void keyPressEvent( QKeyEvent *e ); + void focusInEvent( QFocusEvent *e ); + + bool focusNextPrevChild(bool next); + + private slots: + void slotRefreshHighlight(); private: uchar ActiveField; // 0==day; 1==month; 2==year @@ -92,14 +104,32 @@ class ExtDateTimeEdit : public QWidget { public: ExtDateTimeEdit( const ExtDateTime &dt = ExtDateTime::currentDateTime(), QWidget *p=0 ); -#if 0 ExtDateTimeEdit( const ExtDate &d, const QTime &t, QWidget *p=0 ); -#endif ~ExtDateTimeEdit(); + ExtDate date() const { return m_DateEdit->date(); } + void setDate( const ExtDate &d ) { m_DateEdit->setDate( d ); } + QTime time() const { return m_TimeEdit->time(); } + void setTime( const QTime &t ) { m_TimeEdit->setTime( t ); } + ExtDateTime dateTime() const { return ExtDateTime( date(), time() ); } + void setDateTime( const ExtDateTime &dt ) { setDate( dt.date() ); setTime( dt.time() ); } + private: QTimeEdit *m_TimeEdit; ExtDateEdit *m_DateEdit; }; +class edLineEdit : public QLineEdit { + public: + edLineEdit( QWidget *parent=0 ); + ~edLineEdit() {} + + protected: + void mouseReleaseEvent( QMouseEvent * ); + void keyPressEvent( QKeyEvent *e ); + + private: + ExtDateEdit *edParent; +}; + #endif //EXTDATETIMEEDIT_H diff --git a/extdate/tests/Makefile.am b/extdate/tests/Makefile.am index 7a5163d..9341bf1 100644 --- a/extdate/tests/Makefile.am +++ b/extdate/tests/Makefile.am @@ -1,12 +1,19 @@ INCLUDES = -I$(top_srcdir)/libkdeedu/extdate $(all_includes) -LDADD = ../libextdate.la - AM_LDFLAGS = $(QT_LDFLAGS) $(X_LDFLAGS) $(KDE_RPATH) -check_PROGRAMS = test_extdate test_extdatepicker +check_PROGRAMS = test_extdate test_extdatepicker test_extdatetimeedit test_extdate_SOURCES = test_extdate.cc -test_extdatepicker_SOURCES = testwidget.cpp main.cpp +test_extdate_LDFLAGS = $(all_libraries) +test_extdate_LDADD = ../libextdate.la + +test_extdatepicker_SOURCES = edpicker_widget.cpp edpicker_main.cpp +test_extdatepicker_LDFLAGS = $(all_libraries) +test_extdatepicker_LDADD = ../libextdate.la + +test_extdatetimeedit_SOURCES = edtedit_widget.cpp edtedit_main.cpp +test_extdatetimeedit_LDFLAGS = $(all_libraries) +test_extdatetimeedit_LDADD = ../libextdate.la METASOURCES = AUTO diff --git a/extdate/tests/main.cpp b/extdate/tests/edpicker_main.cpp similarity index 80% rename from extdate/tests/main.cpp rename to extdate/tests/edpicker_main.cpp index f71a6ac..213c2dd 100644 --- a/extdate/tests/main.cpp +++ b/extdate/tests/edpicker_main.cpp @@ -1,4 +1,4 @@ -#include "testwidget.h" +#include "edpicker_widget.h" #include #include #include @@ -16,15 +16,15 @@ int main( int argc, char *argv[] ) KAboutData aboutData( "test_extdatepicker", I18N_NOOP("Test ExtDatePicker"), "0.1", description, KAboutData::License_GPL, I18N_NOOP("(c) 2004, Jason Harris"), notice, - "http://30doradus.org"); + "http://edu.kde.org/kstars"); aboutData.addAuthor("Jason Harris", 0, - "jharris@30doradus.org", "http://www.30doradus.org"); + "kstars@30doradus.org", "http://edu.kde.org/kstars"); KCmdLineArgs::init( argc, argv, &aboutData ); KApplication a; - TestWidget *t = new TestWidget(0,0); - t->show(); + EDPicker *edp = new EDPicker(0,0); + edp->show(); QObject::connect(kapp, SIGNAL(lastWindowClosed()), kapp, SLOT(quit())); return a.exec(); } diff --git a/extdate/tests/testwidget.cpp b/extdate/tests/edpicker_widget.cpp similarity index 83% rename from extdate/tests/testwidget.cpp rename to extdate/tests/edpicker_widget.cpp index 18c2333..16692b5 100644 --- a/extdate/tests/testwidget.cpp +++ b/extdate/tests/edpicker_widget.cpp @@ -1,5 +1,5 @@ /*************************************************************************** - testwidget.h - description + edpicker_widget.cpp - description ------------------- begin : Sun Apr 11 2004 copyright : (C) 2004 by Jason Harris @@ -15,19 +15,18 @@ * * ***************************************************************************/ +#include +#include + #include #include #include -#include -#include -//Added by qt3to4: -#include -#include "extdatepicker.h" -#include "extdatewidget.h" -#include "testwidget.h" +#include "../extdatepicker.h" +#include "../extdatewidget.h" +#include "edpicker_widget.h" -TestWidget::TestWidget( QWidget *p=0, const char *name=0 ) : KMainWindow( p, name ) { +EDPicker::EDPicker( QWidget *p=0, const char *name=0 ) : KMainWindow( p, name ) { QWidget *w = new QWidget(this); glay = new QGridLayout(w, 3, 2); @@ -59,12 +58,12 @@ TestWidget::TestWidget( QWidget *p=0, const char *name=0 ) : KMainWindow( p, nam connect( edp, SIGNAL( dateChanged(const ExtDate&) ), this, SLOT( slotExtDateChanged(const ExtDate&) ) ); } -void TestWidget::slotKDateChanged(QDate d) { +void EDPicker::slotKDateChanged(QDate d) { kdpEdit->setText( d.toString() ); } -void TestWidget::slotExtDateChanged(const ExtDate &d) { +void EDPicker::slotExtDateChanged(const ExtDate &d) { edpEdit->setText( d.toString() ); } -#include "testwidget.moc" +#include "edpicker_widget.moc" diff --git a/extdate/tests/testwidget.h b/extdate/tests/edpicker_widget.h similarity index 83% rename from extdate/tests/testwidget.h rename to extdate/tests/edpicker_widget.h index 267645c..fae1560 100644 --- a/extdate/tests/testwidget.h +++ b/extdate/tests/edpicker_widget.h @@ -1,5 +1,5 @@ /*************************************************************************** - testwidget.cpp - description + edpicker_widget.h - description ------------------- begin : Sun Apr 11 2004 copyright : (C) 2004 by Jason Harris @@ -15,27 +15,25 @@ * * ***************************************************************************/ -#ifndef TESTWIDGET_H -#define TESTWIDGET_H +#ifndef EDPICKER_WIDGET_H +#define EDPICKER_WIDGET_H #include -//Added by qt3to4: -#include +class QDate; +class QGridLayout; class KDatePicker; class KLineEdit; class KDateWidget; +class ExtDate; class ExtDatePicker; class ExtDateWidget; -class QGridLayout; -class QDate; -class ExtDate; -class TestWidget : public KMainWindow { +class EDPicker : public KMainWindow { Q_OBJECT public: - TestWidget( QWidget *parent, const char *name ); - ~TestWidget() {} + EDPicker( QWidget *parent, const char *name ); + ~EDPicker() {} public slots: void slotKDateChanged(QDate); @@ -50,4 +48,4 @@ class TestWidget : public KMainWindow { KLineEdit *kdpEdit, *edpEdit; }; -#endif //ifndef TESTWIDGET_H +#endif //ifndef EDPICKER_WIDGET_H diff --git a/extdate/tests/edtedit_main.cpp b/extdate/tests/edtedit_main.cpp new file mode 100644 index 0000000..99da895 --- /dev/null +++ b/extdate/tests/edtedit_main.cpp @@ -0,0 +1,30 @@ +#include "edtedit_widget.h" +#include +#include +#include + +static const char description[] = I18N_NOOP("ExtDateTimeEdit test program"); +static const char notice[] = I18N_NOOP("Displays an ExtDateTimeEdit widget to test"); + +static KCmdLineOptions options[] = +{ + KCmdLineLastOption +}; + +int main( int argc, char *argv[] ) +{ + KAboutData aboutData( "test_extdatetimeedit", I18N_NOOP("Test ExtDateTimeEdit"), + "0.1", description, KAboutData::License_GPL, + I18N_NOOP("(c) 2005, Jason Harris"), notice, + "http://edu.kde.org/kstars"); + aboutData.addAuthor("Jason Harris", 0, + "kstars@30doradus.org", "http://edu.kde.org/kstars"); + + KCmdLineArgs::init( argc, argv, &aboutData ); + + KApplication a; + EDTEdit *edte = new EDTEdit(0,0); + edte->show(); + QObject::connect(kapp, SIGNAL(lastWindowClosed()), kapp, SLOT(quit())); + return a.exec(); +} diff --git a/extdate/tests/edtedit_widget.cpp b/extdate/tests/edtedit_widget.cpp new file mode 100644 index 0000000..d6bba18 --- /dev/null +++ b/extdate/tests/edtedit_widget.cpp @@ -0,0 +1,47 @@ +/*************************************************************************** + edtedit_widget.cpp - description + ------------------- + begin : Sun Sept 4 2005 + copyright : (C) 2005 by Jason Harris + email : kstars@30doradus.org + ***************************************************************************/ + +/*************************************************************************** + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + ***************************************************************************/ + +#include +#include +#include +#include + +#include "../extdatetime.h" +#include "../extdatetimeedit.h" +#include "edtedit_widget.h" + +EDTEdit::EDTEdit( QWidget *p=0, const char *name=0 ) : KMainWindow( p, name ) { + QWidget *w = new QWidget(this); + + vlay = new QVBoxLayout( w ); + + ed = new ExtDateEdit( ExtDate::currentDate(), w ); + edt = new ExtDateTimeEdit( ExtDateTime::currentDateTime(), w ); + + vlay->addWidget( ed ); + vlay->addWidget( edt ); + + setCentralWidget(w); +} + +EDTEdit::~EDTEdit() { + //Output current date setting on exit + kdDebug() << "ExDateEdit: " << ed->date().toString() << endl; + kdDebug() << "ExDateTimeEdit: " << edt->date().toString() << endl; +} + +#include "edtedit_widget.moc" diff --git a/extdate/tests/edtedit_widget.h b/extdate/tests/edtedit_widget.h new file mode 100644 index 0000000..5711b7d --- /dev/null +++ b/extdate/tests/edtedit_widget.h @@ -0,0 +1,42 @@ +/*************************************************************************** + edtedit_widget.cpp - description + ------------------- + begin : Sun Sept 4 2005 + copyright : (C) 2005 by Jason Harris + email : kstars@30doradus.org + ***************************************************************************/ + +/*************************************************************************** + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + ***************************************************************************/ + +#ifndef EDTEDIT_WIDGET_H +#define EDTEDIT_WIDGET_H + +#include + +class QVBoxLayout; +class ExtDate; +class ExtDateEdit; +class ExtDateTimeEdit; + +class EDTEdit : public KMainWindow { + Q_OBJECT + public: + EDTEdit( QWidget *parent, const char *name ); + ~EDTEdit(); + +// public slots: + + private: + QVBoxLayout *vlay; + ExtDateEdit *ed; + ExtDateTimeEdit *edt; +}; + +#endif //ifndef EDTEDIT_WIDGET_H diff --git a/extdate/tests/test_extdate.cc b/extdate/tests/test_extdate.cc index cce787c..e622268 100644 --- a/extdate/tests/test_extdate.cc +++ b/extdate/tests/test_extdate.cc @@ -1,6 +1,7 @@ #include #include -#include "extdatetime.h" + +#include "../extdatetime.h" void test1_unit(int a_year) -- 2.47.3