/*
** Originally based on the KWorldClock program
** written by Matthias Hölzer-Klüpfel
** Copyright (C) 1998-2001 by Matthias Hölzer-Klüpfel <hoelzer@kde.org>
**
** Copyright (C) 2008 by Henry de Valence <hdevalence@gmail.com>
**
*/
/*
** 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 3 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program in a file called COPYING; if not, write to
** the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
** MA 02110-1301, USA.
*/
#include <time.h>
#include <assert.h>
#include <QPainter>
#include <QFontMetrics>
#include <QSizeF>
#include <plasma/theme.h>
#include <plasma/dataengine.h>
#include <kglobal.h>
#include <qimageblitz/qimageblitz.h>
#include <kstandarddirs.h>
#include <kworldclock/maploader.h>
#include "plasmamap.h"
//#include "astro.h"
//#include "sunclock.h"
using namespace KWorldClock;
WorldClock::WorldClock(QObject *parent, const QVariantList &args)
: Plasma::Applet(parent, args)
{
setDrawStandardBackground(true);
setSize(400, 200);
}
WorldClock::~WorldClock()
{
}
void WorldClock::connectToEngine()
{
Plasma::DataEngine* timeEngine = dataEngine("time");
timeEngine->connectSource("Local", this, 6000, Plasma::AlignToMinute);
}
void WorldClock::init()
{
connectToEngine();
m_nextRenderTime = QTime(0,0,0,1);
//m_nextRenderTime = m_nextRenderTime.addSecs( 60 * 10 ); //move it forwards 10 min
}
void WorldClock::dataUpdated(const QString &name, const Plasma::DataEngine::Data &data)
{
m_time = data["Time"].toTime();
update();
}
//much of this is based on the mapwidget code
void WorldClock::calculatePixmap(const QRect& contentsRect, QPixmap& final)
{
time_t local = time(NULL);
struct tm *curtime = gmtime(&local);
int sec = curtime->tm_hour*60*60 + curtime->tm_min*60 + curtime->tm_sec;
int gmt_position = contentsRect.width() * sec / 86400;
if( m_nextRenderTime > m_time ) {
qDebug() << "less than x mins since last calculatePixmap()";
if( m_lastContentsRect == contentsRect ) {
qDebug() << "same size, returning last result";
final = m_lastRender;
return;
} else {
qDebug() << "scaling old image to new size";
final = final.scaled(contentsRect.size());
//final = m_lastRender;
return;
}
} else {
qDebug() << "more than x mins since last calculatePixmap()";
}
qDebug() << "loading theme 'daynight' width=" << contentsRect.width()
<< " height=" << contentsRect.height();
load( static_cast<unsigned int>(contentsRect.width()),
"daynight", static_cast<unsigned int>(contentsRect.height()));
QPixmap map = darkMap();
QPixmap clean = lightMap();
qDebug() << "contentsRect = " << contentsRect.size();
qDebug() << "QPixmap map = " << map.size();
qDebug() << "QPixmap clean = " << clean.size();
QPainter mp(&map);
clean.setAlphaChannel(darkMask(map.width(), map.height()));
mp.drawPixmap(0,0, clean);
QPainter p(&final);
int greenwich = map.width()/2;
if (gmt_position >= greenwich) {
p.drawPixmap(gmt_position-greenwich, 0, map);
p.drawPixmap(0, 0, map, map.width()-gmt_position+greenwich, 0, gmt_position-greenwich, -1);
} else {
p.drawPixmap(0,0, map, greenwich-gmt_position, 0, map.width()+gmt_position-greenwich, -1);
p.drawPixmap(map.width()+gmt_position-greenwich, 0, map, 0, 0, greenwich-gmt_position, -1);
}
QTime time = QTime::currentTime();
p.setPen(Qt::black);
p.drawText( 0, (final.height() - 10 ), "last rendered at " + time.toString("hh:mm:ss.zzz") );
m_lastRender = final;
m_nextRenderTime = time;
m_nextRenderTime = m_nextRenderTime.addSecs( 60 * 2 ); //move it forwards
return;
}
void WorldClock::paintInterface(QPainter *p, const QStyleOptionGraphicsItem *option,
const QRect &contentsRect)
{
qDebug() << "updating..";
p->setRenderHint(QPainter::SmoothPixmapTransform);
p->setRenderHint(QPainter::Antialiasing);
QPixmap world(contentsRect.size());
calculatePixmap(contentsRect, world);
p->drawPixmap(0,0, world );
m_lastRender = world;
QTime time = QTime::currentTime();
p->setPen(Qt::white);
p->drawText( 0,0, "last painted at " + time.toString("hh:mm:ss.zzz") );
}
#include "plasmamap.moc"