summaryrefslogtreecommitdiff
path: root/core/applets/clockapplet/clock.cpp
blob: 9fead03b6c3066d7cc6ca03319b05b41a2a8c611 (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
/**********************************************************************
** Copyright (C) 2000 Trolltech AS.  All rights reserved.
**
** This file is part of Qtopia Environment.
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** See http://www.trolltech.com/gpl/ for GPL licensing information.
**
** Contact info@trolltech.com if any conditions of this licensing are
** not clear to you.
**
**********************************************************************/

#include "clock.h"

#include <opie2/otaskbarapplet.h>
#include <qpe/qpeapplication.h>
#include <qpe/qcopenvelope_qws.h>
#include <qpe/config.h>

LauncherClock::LauncherClock( QWidget *parent ) : QLabel( parent )
{
    // If you want a sunken border around the clock do this:
    // setFrameStyle( QFrame::Panel | QFrame::Sunken );
    //setFont( QFont( "Helvetica", , QFont::Normal ) );
    connect( qApp, SIGNAL( timeChanged() ), this, SLOT( updateTime( ) ) );
    connect( qApp, SIGNAL( clockChanged( bool ) ),
       this, SLOT( slotClockChanged( bool ) ) );
    readConfig();
    timerId = 0;
    timerEvent( 0 );
    show();
}

int LauncherClock::position()
{
    return 10;
}

void LauncherClock::readConfig() {
    Config config( "qpe" );
    config.setGroup( "Time" );
    ampmFormat = config.readBoolEntry( "AMPM", TRUE );
    config.setGroup( "Date" );
    format = config.readNumEntry("ClockApplet",0);
}

void LauncherClock::mouseReleaseEvent( QMouseEvent * )
{
	QCString setTimeApp;
	setTimeApp="systemtime";
	QCopEnvelope e("QPE/Application/"+setTimeApp, "raise()");
}


void LauncherClock::timerEvent( QTimerEvent *e )
{
    if ( !e || e->timerId() == timerId ) {
  killTimer( timerId );
  changeTime();
  QTime t = QTime::currentTime();
  int ms = (60 - t.second())*1000 - t.msec();
  timerId = startTimer( ms );
    } else {
  QLabel::timerEvent( e );
    }
}

void LauncherClock::updateTime( void )
{
    changeTime();
}

void LauncherClock::changeTime( void )
{
    QTime tm = QDateTime::currentDateTime().time();
    QString s;
    if( ampmFormat ) {
  int hour = tm.hour();
  if (hour == 0)
      hour = 12;
  if (hour > 12)
      hour -= 12;
  s.sprintf( "%2d:%02d %s", hour, tm.minute(),
       (tm.hour() >= 12) ? "PM" : "AM" );
    } else
  s.sprintf( "%2d:%02d", tm.hour(), tm.minute() );

    if (format==1) {
  QDate dm = QDate::currentDate();
  QString d;
  d.sprintf("%d/%d ", dm.day(), dm.month());
  setText( d+s );
    } else if (format==2) {
  QDate dm = QDate::currentDate();
  QString d;
  d.sprintf("%d/%d ", dm.month(), dm.day());
  setText( d+s );
    } else {
  setText( s );
    }
}

void LauncherClock::slotClockChanged( bool pm )
{
    readConfig();
    updateTime();
}

Q_EXPORT_INTERFACE()
{
    Q_CREATE_INSTANCE( OTaskbarAppletWrapper<LauncherClock> );
}