panel clock (kclockapplet) patch
Helge Deller
deller at gmx.de
Wed Nov 6 23:39:13 GMT 2002
On Wednesday 06 November 2002 22:50, Ryan Cumming wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> On November 6, 2002 13:34, Helge Deller wrote:
> > This patch seems to work somewhat more correct and includes changes as
> > suggested in feedback by Melchior.
> >
> > Comments ?
>
> Could you explain this code:
Sure.
> int ClockSettings::calc_TZ_offset(const QString& zone)
> {
> static int lock = 0; /* a very simple lock algoritm */
>
> while (lock)
> /* wait */;
>
> lock++;
>
> <do stuff>
>
> lock--;
>
> return diff;
> }
>
> ?
> If you're trying to protect from multiple entrances from a single thread,
> the second time you enter the function, the program will just get stuck in
> an infinite loop.
Yes.
> If you're trying to protect from multiple entrances from
> multiple threads, that code is -not- thread safe.
Yes. Just ignore that piece of code. Such a locking doesn't seem to be necessary.
Attached is the latest (and hopefully last) patch, with the following additional fixes:
- visual fix by melchior regarding the bounding box/ sunken display of digital clock,
- wrong locking functions removed,
- Australia/Sydney instead of Australias/Sydney
Helge
-------------- next part --------------
Index: clock.cpp
===================================================================
RCS file: /home/kde/kdebase/kicker/applets/clock/clock.cpp,v
retrieving revision 1.97
diff -u -p -r1.97 clock.cpp
--- clock.cpp 2002/11/05 11:06:57 1.97
+++ clock.cpp 2002/11/06 23:36:11
@@ -118,7 +118,7 @@ PlainClock::~PlainClock()
int PlainClock::preferedWidthForHeight(int ) const
{
- return kMax(sizeHint().width()-4, 0);
+ return kMax(sizeHint().width()+4, 0);
}
@@ -131,7 +131,7 @@ int PlainClock::preferedHeightForWidth(i
void PlainClock::updateClock()
{
QString newStr =
- KGlobal::locale()->formatTime(QTime::currentTime(),
+ KGlobal::locale()->formatTime(_applet->clockGetTime(),
_settings->showSeconds());
if (newStr != _timeStr) {
@@ -186,7 +186,7 @@ int DigitalClock::preferedWidthForHeight
int DigitalClock::preferedHeightForWidth(int w) const
{
if (w < 0) w = 0;
- return((w / numDigits() * 2) + 6);
+ return((w / numDigits() * 2) + 6);
}
@@ -194,7 +194,7 @@ void DigitalClock::updateClock()
{
static bool colon = true;
QString newStr;
- QTime t(QTime::currentTime());
+ QTime t(_applet->clockGetTime());
int h = t.hour();
int m = t.minute();
@@ -273,6 +273,7 @@ void DigitalClock::drawContents( QPainte
p->translate( -2, -2 );
setUpdatesEnabled( TRUE );
QLCDNumber::drawContents( p );
+ p->translate( +1, +1 );
}
@@ -309,7 +310,7 @@ AnalogClock::AnalogClock(ClockApplet *ap
setBackgroundMode(NoBackground); //prevent flicker
}
- _time = QTime::currentTime();
+ _time = _applet->clockGetTime();
_spPx = new QPixmap(size().width() * settings->antialiasFactor(),
size().height() * settings->antialiasFactor());
@@ -345,10 +346,10 @@ void AnalogClock::initBackgroundPixmap()
void AnalogClock::updateClock()
{
if (!_settings->showSeconds())
- if (_time.minute() == QTime::currentTime().minute())
+ if (_time.minute() == _applet->clockGetTime().minute())
return;
- _time = QTime::currentTime();
+ _time = _applet->clockGetTime();
repaint( false ); //don't erase on redraw
}
@@ -542,7 +543,7 @@ FuzzyClock::FuzzyClock(ClockApplet *appl
<< i18n("Noon") << i18n("Afternoon") << i18n("Evening")
<< i18n("Late evening");
- _time = QTime::currentTime();
+ _time = _applet->clockGetTime();
repaint();
}
@@ -568,11 +569,11 @@ int FuzzyClock::preferedHeightForWidth(i
void FuzzyClock::updateClock()
{
- if (_time.hour() == QTime::currentTime().hour() &&
- _time.minute() == QTime::currentTime().minute())
+ if (_time.hour() == _applet->clockGetTime().hour() &&
+ _time.minute() == _applet->clockGetTime().minute())
return;
- _time = QTime::currentTime();
+ _time = _applet->clockGetTime();
repaint();
}
@@ -627,7 +628,7 @@ void FuzzyClock::drawContents(QPainter *
} else if (_settings->fuzzyness() == 3) {
newTimeStr = dayTime[_time.hour() / 3];
} else {
- int dow = QDate::currentDate().dayOfWeek();
+ int dow = _applet->clockGetDate().dayOfWeek();
if (dow == 1)
newTimeStr = i18n("Start of week");
@@ -686,6 +687,7 @@ ClockApplet::~ClockApplet()
{
if (_calendar)
_calendar->close();
+ config()->sync();
delete _settings;
}
@@ -769,7 +771,7 @@ void ClockApplet::slotApplySettings()
if (!_settings->showDate())
_settings->resetZone();
- setCurrentTimeZone(_settings->zone());
+ TZoffset = _settings->calc_TZ_offset( _settings->zone() );
switch (_settings->type()) {
case ClockSettings::Plain:
@@ -809,7 +811,7 @@ void ClockApplet::slotApplySettings()
void ClockApplet::slotUpdate()
{
- if (_lastDate != QDate::currentDate())
+ if (_lastDate != clockGetDate())
updateDateLabel();
_clock->updateClock();
@@ -871,6 +873,7 @@ void ClockApplet::openContextMenu()
KLocale *loc = KGlobal::locale();
QDateTime dt = QDateTime::currentDateTime();
+ dt = dt.addSecs(TZoffset);
KPopupMenu *copyMenu = new KPopupMenu( menu );
copyMenu->insertItem(loc->formatDateTime(dt), 201);
@@ -953,13 +956,24 @@ void ClockApplet::slotCopyMenuActivated(
QApplication::clipboard()->setText(s);
}
+QTime ClockApplet::clockGetTime()
+{
+ return QTime::currentTime().addSecs(TZoffset);
+}
+
+QDate ClockApplet::clockGetDate()
+{
+ return QDate::currentDate().addDays(TZoffset/(24*60*60));
+}
+
void ClockApplet::showZone(int z)
{
_settings->resetZone(z);
- setCurrentTimeZone(_settings->zone());
+ TZoffset = _settings->calc_TZ_offset( _settings->zone() );
updateDateLabel();
_clock->forceUpdate(); /* force repaint */
slotUpdate();
+ _settings->writeSettings();
}
void ClockApplet::nextZone()
@@ -1027,7 +1041,7 @@ void ClockApplet::slotUpdateToolTip()
void ClockApplet::updateDateLabel()
{
- _lastDate = QDate::currentDate();
+ _lastDate = clockGetDate();
if (_settings->zoneIndex())
{
Index: clock.h
===================================================================
RCS file: /home/kde/kdebase/kicker/applets/clock/clock.h,v
retrieving revision 1.31
diff -u -p -r1.31 clock.h
--- clock.h 2002/05/23 14:58:17 1.31
+++ clock.h 2002/11/06 23:36:11
@@ -168,7 +168,6 @@ protected:
class ClockApplet : public KPanelApplet
{
Q_OBJECT
-// friend class ClockSettings;
public:
ClockApplet(const QString& configFile, Type t = Normal, int actions = 0,
@@ -180,6 +179,10 @@ public:
void preferences();
Orientation getOrientation() { return orientation(); }
void resizeRequest() { emit(updateLayout()); }
+
+ int TZoffset;
+ QTime clockGetTime();
+ QDate clockGetDate();
protected slots:
void slotApplySettings();
Index: settings.cpp
===================================================================
RCS file: /home/kde/kdebase/kicker/applets/clock/settings.cpp,v
retrieving revision 1.6
diff -u -p -r1.6 settings.cpp
--- settings.cpp 2002/09/21 11:20:04 1.6
+++ settings.cpp 2002/11/06 23:36:12
@@ -52,12 +52,13 @@ CONNECTION WITH THE SOFTWARE OR THE USE
#include <qstringlist.h>
#include <qtabwidget.h>
#include <qtooltip.h>
+#include <qdatetime.h>
#include <stdlib.h>
#include <time.h>
+#include <unistd.h>
-void
-setCurrentTimeZone( const QString & zone )
+static void setCurrentTimeZone(const QString& zone)
{
// set TZ variable for this program
// Ever occuring error:
@@ -73,14 +74,10 @@ setCurrentTimeZone( const QString & zone
}
ClockSettings::ClockSettings(QWidget* app, KConfig* conf)
- : applet(app), config(conf), confDlg(0), _zoneIndex(0)
+ : applet(app), config(conf), confDlg(0)
{
+ _defaultTZ = ::getenv("TZ");
tzset();
- QFile f( "/etc/timezone" );
- if ( f.open( IO_ReadOnly ) ) {
- QTextStream ts( &f );
- ts >> _defaultTZ;
- }
config->setGroup("General");
@@ -97,9 +94,10 @@ ClockSettings::ClockSettings(QWidget* ap
/* default displayable timezones */
QString tzList = config->readEntry("RemoteZones",
"America/Los_Angeles,America/New_York,"
- "Australias/Sydney,Asia/Tokyo,"
+ "Australia/Sydney,Asia/Tokyo,"
"Europe/Berlin,Europe/London,Europe/Moscow");
_remotezonelist = QStringList::split(QRegExp(","), tzList);
+ _zoneIndex = config->readNumEntry("Initial_Zone_Index", 0);
config->setGroup("Date");
_useColDate = config->readBoolEntry("Use_Custom_Colors",false);
@@ -156,7 +154,6 @@ ClockSettings::ClockSettings(QWidget* ap
ClockSettings::~ClockSettings()
{
- setCurrentTimeZone(_defaultTZ);
delete confDlg;
}
@@ -165,6 +162,14 @@ QString ClockSettings::zone(int z) const
return (z==0 ? _defaultTZ : _remotezonelist[z-1]);
}
+int ClockSettings::calc_TZ_offset(const QString& zone)
+{
+ setCurrentTimeZone(zone);
+ QDateTime t1( QDateTime::currentDateTime() );
+ setCurrentTimeZone(_defaultTZ);
+ return QDateTime::currentDateTime().secsTo(t1);
+}
+
void ClockSettings::readZoneList( const QStringList & tzDefaults )
{
QFile f("/usr/share/zoneinfo/zone.tab");
@@ -285,6 +290,7 @@ void ClockSettings::writeSettings()
}
config->writeEntry("RemoteZones", _remotezonelist.join(","));
+ config->writeEntry("Initial_Zone_Index",_zoneIndex);
config->setGroup("Date");
config->writeEntry("Use_Custom_Colors",_useColDate);
Index: settings.h
===================================================================
RCS file: /home/kde/kdebase/kicker/applets/clock/settings.h,v
retrieving revision 1.2
diff -u -p -r1.2 settings.h
--- settings.h 2002/05/25 09:32:01 1.2
+++ settings.h 2002/11/06 23:36:12
@@ -32,9 +32,6 @@ CONNECTION WITH THE SOFTWARE OR THE USE
class KConfig;
class ClockConfDialog;
-// needed by clock.cpp and settings.cpp (Werner)
-void setCurrentTimeZone( const QString & zone );
-
class ClockSettings : public QObject
{
Q_OBJECT
@@ -75,6 +72,8 @@ public:
void prevZone();
unsigned int zoneIndex() { return _zoneIndex; }
void resetZone(int z=0) { _zoneIndex = z; }
+
+ int calc_TZ_offset(const QString& zone);
signals:
void newSettings();
More information about the kde-core-devel
mailing list