summaryrefslogtreecommitdiff
path: root/libopie/otimepicker.cpp
blob: 8e8a4e74faf9677b24943b44ff6361eee8a2779d (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include "otimepicker.h"

#include <qbuttongroup.h>
#include <qtoolbutton.h>
#include <qlayout.h>
#include <qstring.h>
#include <stdio.h>
#include <qlineedit.h>

OTimePicker::OTimePicker(QWidget* parent, const char* name, 
		       WFlags fl) :
  QWidget(parent,name,fl)
{
  QVBoxLayout *vbox=new QVBoxLayout(this);

  OClickableLabel *r;
  QString s;

  // Hour Row
  QWidget *row=new QWidget(this);
  QHBoxLayout *l=new QHBoxLayout(row);
  vbox->addWidget(row);
  
  
  for (int i=0; i<24; i++) {
    r=new OClickableLabel(row);
    hourLst.append(r);
    s.sprintf("%.2d",i);
    r->setText(s);
    r->setToggleButton(true);
    r->setAlignment(AlignHCenter | AlignVCenter);
    l->addWidget(r);
    connect(r, SIGNAL(toggled(bool)),
	    this, SLOT(slotHour(bool)));

    if (i==11) { // Second row
      row=new QWidget(this);
      l=new QHBoxLayout(row);
      vbox->addWidget(row);
    }
  }

  // Minute Row
  row=new QWidget(this);
  l=new QHBoxLayout(row);
  vbox->addWidget(row);
  
  for (int i=0; i<60; i+=5) {
    r=new OClickableLabel(row);
    minuteLst.append(r);
    s.sprintf("%.2d",i);
    r->setText(s);
    r->setToggleButton(true);
    r->setAlignment(AlignHCenter | AlignVCenter);
    l->addWidget(r);
    connect(r, SIGNAL(toggled(bool)),
	    this, SLOT(slotMinute(bool)));
  }
}

void OTimePicker::slotHour(bool b) {

  OClickableLabel *r = (OClickableLabel *) sender();

  if (b) {
    QValueListIterator<OClickableLabel *> it;
    for (it=hourLst.begin(); it!=hourLst.end(); it++) {
      if (*it != r) (*it)->setOn(false);
      else tm.setHMS((*it)->text().toInt(), tm.minute(), 0);
    }
    emit timeChanged(tm);
  } else {
    r->setOn(true);
  }

}

void OTimePicker::slotMinute(bool b) {

  OClickableLabel *r = (OClickableLabel *) sender();

  if (b) {
    QValueListIterator<OClickableLabel *> it;
    for (it=minuteLst.begin(); it!=minuteLst.end(); it++) {
      if (*it != r) (*it)->setOn(false);
      else tm.setHMS(tm.hour(),(*it)->text().toInt(), 0);
    }
    emit timeChanged(tm);
  } else {
    r->setOn(true);
  }

}

void OTimePicker::setMinute(int m) {

  QString minute;
  minute.sprintf("%.2d",m);

  QValueListIterator<OClickableLabel *> it;
  for (it=minuteLst.begin(); it!=minuteLst.end(); it++) {
    if ((*it)->text() == minute) (*it)->setOn(true);
    else (*it)->setOn(false);
  }  

  tm.setHMS(tm.hour(),m,0);
}

void OTimePicker::setHour(int h) {

  QString hour;
  hour.sprintf("%.2d",h);

  QValueListIterator<OClickableLabel *> it;
  for (it=hourLst.begin(); it!=hourLst.end(); it++) {
    if ((*it)->text() == hour) (*it)->setOn(true);
    else (*it)->setOn(false);
  }  
  tm.setHMS(h,tm.minute(),0);
}


OTimePickerDialog::OTimePickerDialog ( QWidget* parent, const char* name, WFlags fl )
	: OTimePickerDialogBase (parent , name, true , fl)
{

	connect ( m_timePicker, SIGNAL( timeChanged( const QTime& ) ),
		  this, SLOT( setTime ( const QTime& ) ) );
	connect ( minuteField, SIGNAL( textChanged ( const QString& ) ),
		  this, SLOT ( setMinute ( const QString& ) ) );
	connect ( hourField, SIGNAL( textChanged ( const QString& ) ),
		  this, SLOT ( setHour ( const QString& ) ) );

}

QTime& OTimePickerDialog::time()
{
	return m_time;
}
void OTimePickerDialog::setTime( const QTime& time )
{
	m_time = time;

	m_timePicker->setHour ( time.hour() );
	m_timePicker->setMinute( time.minute() );

	// Set Textfields
	if ( time.hour() < 10 )
		hourField->setText( "0" + QString::number( time.hour() ) );
	else
		hourField->setText( QString::number( time.hour() ) );

	if ( time.minute() < 10 )
		minuteField->setText( "0" + QString::number( time.minute() ) );
	else
		minuteField->setText( QString::number( time.minute() ) );
	
}

void OTimePickerDialog::setHour ( const QString& hour )
{
	if ( QTime::isValid ( hour.toInt(),  m_time.minute() , 00 ) ){
		m_time.setHMS ( hour.toInt(),  m_time.minute() , 00 );
		setTime ( m_time );
	}

}

void OTimePickerDialog::setMinute ( const QString& minute )
{
	if ( QTime::isValid ( m_time.hour(), minute.toInt(), 00 ) ){
		m_time.setHMS ( m_time.hour(), minute.toInt(), 00 );
		setTime ( m_time );
	}
}