#include <QTime>
#include <QDateTimeEdit>
+#include <QLineEdit>
#include <QHBoxLayout>
+#include <QMouseEvent>
#include <QKeyEvent>
#include <kglobal.h>
#include <klocale.h>
+#include <kdebug.h>
+
#include "extdatetime.h"
#include "extdatetimeedit.h"
: 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; j<dLength; j++ ) result.insert( i, "Y" );
+ } else if ( dLength < 0 ) { //the years field has less than 4 digits
+ result.remove( i, -1*dLength );
+ }
+
+ return result;
+}
+
+void ExtDateEdit::highlightActiveField() {
+ int iStart(0), iLength(0);
+ QString sdf = simpleDateFormat();
+
+ //Pick out the position and length of the currently-active field
+ if ( ActiveField == 0 ) { //Days field
+ iStart = sdf.indexOf( "D" );
+ iLength = 2; //The Days field should always be two digits
+ } else if ( ActiveField == 1 ) { //Months field
+ iStart = sdf.indexOf( "M" );
+ iLength = 2; //The Months field should always be two digits
+ } else { //Years field
+ iStart = sdf.indexOf( "Y" );
+ iLength = sdf.lastIndexOf( "Y" ) - iStart + 1;
+ }
+
+ lineEdit()->setCursorPosition( 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
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"
#include <QSpinBox>
#include <QWidget>
-
+#include <QLineEdit>
class QTime;
class QTimeEdit;
class ExtDate;
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
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
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
-#include "testwidget.h"
+#include "edpicker_widget.h"
#include <kapplication.h>
#include <kcmdlineargs.h>
#include <kaboutdata.h>
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();
}
/***************************************************************************
- testwidget.h - description
+ edpicker_widget.cpp - description
-------------------
begin : Sun Apr 11 2004
copyright : (C) 2004 by Jason Harris
* *
***************************************************************************/
+#include <QLabel>
+#include <QGridLayout>
+
#include <kdatepicker.h>
#include <kdatewidget.h>
#include <klineedit.h>
-#include <qlayout.h>
-#include <qlabel.h>
-//Added by qt3to4:
-#include <QGridLayout>
-#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);
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"
/***************************************************************************
- testwidget.cpp - description
+ edpicker_widget.h - description
-------------------
begin : Sun Apr 11 2004
copyright : (C) 2004 by Jason Harris
* *
***************************************************************************/
-#ifndef TESTWIDGET_H
-#define TESTWIDGET_H
+#ifndef EDPICKER_WIDGET_H
+#define EDPICKER_WIDGET_H
#include <kmainwindow.h>
-//Added by qt3to4:
-#include <QGridLayout>
+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);
KLineEdit *kdpEdit, *edpEdit;
};
-#endif //ifndef TESTWIDGET_H
+#endif //ifndef EDPICKER_WIDGET_H
--- /dev/null
+#include "edtedit_widget.h"
+#include <kapplication.h>
+#include <kcmdlineargs.h>
+#include <kaboutdata.h>
+
+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();
+}
--- /dev/null
+/***************************************************************************
+ 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 <QWidget>
+#include <QTimeEdit>
+#include <QVBoxLayout>
+#include <kdebug.h>
+
+#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"
--- /dev/null
+/***************************************************************************
+ 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 <kmainwindow.h>
+
+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
#include <stdlib.h>
#include <iostream>
-#include "extdatetime.h"
+
+#include "../extdatetime.h"
void test1_unit(int a_year)