summaryrefslogtreecommitdiffstats
path: root/kfind/kdatecombo.cpp
blob: 5ac494f86b263bfdda6f2fca63f87ebcfaf19e67 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/***********************************************************************
 *
 *  kdatecombo.cpp
 *
 ***********************************************************************/

#include <tqtimer.h>

#include <kglobal.h>
#include <klocale.h>
#include <kdatepicker.h>
#include <kdatetbl.h>
#include <kdebug.h>

#include "kdatecombo.h"

#include "kdatecombo.moc"

KDateCombo::KDateCombo(TQWidget *parent, const char *name ) : TQComboBox(FALSE, parent,name)
{
  TQDate date = TQDate::currentDate();
  initObject(date, parent, name);
}

KDateCombo::KDateCombo(const TQDate & date, TQWidget *parent, const char *name) : TQComboBox(FALSE, parent,name)
{
  initObject(date, parent, name);
}

void KDateCombo::initObject(const TQDate & date, TQWidget *, const char *)
{
  clearValidator();
  popupFrame = new KPopupFrame(this, "popupFrame");
  popupFrame->installEventFilter(this);
  datePicker = new KDatePicker(popupFrame, date, "datePicker");
  datePicker->setMinimumSize(datePicker->sizeHint());
  datePicker->installEventFilter(this);
  popupFrame->setMainWidget(datePicker);
  setDate(date);

  connect(datePicker, TQT_SIGNAL(dateSelected(TQDate)), this, TQT_SLOT(dateEnteredEvent(TQDate)));
}

KDateCombo::~KDateCombo()
{
  delete datePicker;
  delete popupFrame;
}

TQString KDateCombo::date2String(const TQDate & date)
{
  return(KGlobal::locale()->formatDate(date, true));
}

TQDate & KDateCombo::string2Date(const TQString & str, TQDate *qd)
{
  return *qd = KGlobal::locale()->readDate(str);
}

TQDate & KDateCombo::getDate(TQDate *currentDate)
{
  return string2Date(currentText(), currentDate);
}

bool KDateCombo::setDate(const TQDate & newDate)
{
  if (newDate.isValid())
  {
    if (count())
      clear();
    insertItem(date2String(newDate));
    return TRUE;
  }
  return FALSE;
}

void KDateCombo::dateEnteredEvent(TQDate newDate)
{
  if (!newDate.isValid())
     newDate = datePicker->date();
  popupFrame->hide();
  setDate(newDate);
}

void KDateCombo::mousePressEvent (TQMouseEvent * e)
{
  if (e->button() & Qt::LeftButton)
  {
    if  (TQT_TQRECT_OBJECT(rect()).contains( e->pos()))
    {
      TQDate tempDate;
      getDate(& tempDate);
      datePicker->setDate(tempDate);
      popupFrame->popup(mapToGlobal(TQPoint(0, height())));
      //datePicker->setFocus();
    }
  }
}

bool KDateCombo::eventFilter (TQObject*, TQEvent* e)
{
  if ( e->type() == TQEvent::MouseButtonPress )
  {
      TQMouseEvent *me = (TQMouseEvent *)e;
      TQPoint p = mapFromGlobal( me->globalPos() );
      if (TQT_TQRECT_OBJECT(rect()).contains( p ) )
      {
        TQTimer::singleShot(10, this, TQT_SLOT(dateEnteredEvent()));
        return true;
      }
  }
  else if ( e->type() == TQEvent::KeyRelease )
  {
      TQKeyEvent *k = (TQKeyEvent *)e;
      //Press return == pick selected date and close the combo
      if((k->key()==Qt::Key_Return)||(k->key()==Qt::Key_Enter))
      {
        dateEnteredEvent(datePicker->date());
        return true;
      }
      else if (k->key()==Qt::Key_Escape)
      {
        popupFrame->hide();
        return true;
      }
      else
        return false;
  }

  return false;
}