22 #include <QRadialGradient>
24 #include <QGraphicsSceneHoverEvent>
35 #include <KConfigDialog>
36 #include <KConfigGroup>
38 #include <KTimeZoneWidget>
39 #include <KSystemTimeZone>
42 #include <Plasma/Applet>
43 #include <Plasma/DataEngine>
59 : Plasma::Applet(parent, args),
62 KGlobal::locale()->insertCatalog(
"marble");
63 KGlobal::locale()->insertCatalog(
"marble_qt");
64 KGlobal::locale()->insertCatalog(
"timezones4");
65 setHasConfigurationInterface(
true);
66 setAcceptHoverEvents(
true);
69 resize(QSize(400, 200));
74 KConfigGroup cg = config();
78 m_map->
centerOn( cg.readEntry(
"rotation", -20), 0 );
105 m_customTz = cg.readEntry(
"customtz",
false );
106 m_locationkey = KSystemTimeZones::local().name();
108 QStringList tzlist = cg.readEntry(
"tzlist", QStringList());
109 m_locations = QMap<QString, KTimeZone>();
110 foreach(
const QString& tzname, tzlist ) {
111 m_locations.insert(tzname, KSystemTimeZones::zone(tzname));
113 if(!m_locations.contains(m_locationkey))
114 m_locationkey = m_locations.keys().first();
116 m_locations = KSystemTimeZones::zones();
117 QList<QString> zones = m_locations.keys();
118 for (
int i = 0; i < zones.size(); ++i ) {
119 KTimeZone curzone = m_locations.value( zones.at( i ) );
120 if ( curzone.latitude() == KTimeZone::UNKNOWN ||
121 curzone.longitude() == KTimeZone::UNKNOWN ) {
122 m_locations.remove( zones.at(i) );
128 m_timeFont = QFont(
"Helvetica", 12, QFont::Bold);
129 m_locationFont = QFont(
"Helvetica", 12, QFont::Bold);
130 m_points = QHash<QString, QPoint>();
131 m_lastRect = QRect(0,0,0,0);
132 m_showDate = cg.readEntry(
"showdate",
false);
136 Plasma::DataEngine *m_timeEngine = dataEngine(
"time");
137 m_timeEngine->connectSource(
"Local",
this, 6000, Plasma::AlignToMinute);
139 connect(m_map, SIGNAL(repaintNeeded(QRegion)),
this, SLOT(slotRepaint()));
147 void WorldClock::resizeMap(
bool changeAspect)
152 double ratio =
static_cast<double>(m_lastRect.width()) /
153 static_cast<double>(m_lastRect.height());
155 kDebug() <<
"equirectangular with rect" << m_lastRect;
156 kDebug() <<
"w/h ratio:" << ratio;
158 height = m_lastRect.height();
160 radius =
static_cast<int>(height/2);
162 width = m_lastRect.width();
163 height =
static_cast<int>(width/2);
164 radius =
static_cast<int>(width/4);
167 kDebug() <<
"mercator with rect" << m_lastRect;
168 kDebug() <<
"w/h ratio:" << ratio;
170 height = m_lastRect.height();
172 radius =
static_cast<int>(width/4);
174 width = m_lastRect.width();
176 radius =
static_cast<int>(width/4);
179 kDebug() <<
"width, height, radius:" << width << height << radius;
185 QRectF curGeo = geometry();
186 setGeometry( curGeo.x(), curGeo.y(),
static_cast<double>(width),
187 static_cast<double>(height) );
191 void WorldClock::slotRepaint()
197 const Plasma::DataEngine::Data &data)
201 m_localtime = QDateTime( QDate::currentDate(), data[
"Time"].toTime() );
202 m_time = KSystemTimeZones::local().convert(m_locations.value(m_locationkey),
211 Applet::hoverLeaveEvent(event);
217 m_hover =
event->pos() - m_t;
218 Applet::hoverEnterEvent(event);
224 m_hover =
event->pos() - m_t;
225 Applet::hoverMoveEvent(event);
230 QString WorldClock::getZone()
234 bool ok = !m_hover.isNull() &&
238 return KSystemTimeZones::local().name();
240 QList<QString> zones = m_locations.keys();
243 qreal mindist = 99999999999999999.9;
245 for (
int i = 0; i < zones.size(); ++i ) {
246 KTimeZone cz = m_locations.value( zones.at( i ) );
247 qreal dist = sqrt( pow(lat-cz.latitude(), 2) + pow(lon-cz.longitude(), 2) );
248 if ( dist < mindist ) {
250 closest = zones.at( i );
253 return m_locations.value( closest ).name();
256 void WorldClock::setTz( QString newtz )
258 if ( newtz == m_locationkey ) {
return; }
259 m_locationkey = newtz;
260 m_time = KSystemTimeZones::local().convert(m_locations.value(m_locationkey),
265 void WorldClock::recalculatePoints()
267 int x = m_map->
width();
269 m_points.insert(
"topright", QPoint( ( x*0.666 ), ( y*0.25 ) ) );
270 m_points.insert(
"topleft", QPoint( ( x*0.333 ), ( y*0.25 ) ) );
271 m_points.insert(
"middleright", QPoint( ( x*0.666 ), ( y*0.58333 ) ) );
272 m_points.insert(
"middleleft", QPoint( ( x*0.333 ), ( y*0.58333 ) ) );
273 m_points.insert(
"bottomright", QPoint( ( x*0.666 ), ( y*0.75 ) ) );
274 m_points.insert(
"bottomleft", QPoint( ( x*0.333 ), ( y*0.75 ) ) );
279 void WorldClock::recalculateFonts( )
283 timestr = KGlobal::locale()->formatDateTime( m_time );
285 timestr = KGlobal::locale()->formatTime( m_time.time() );
287 QString locstr = i18n( m_locationkey.toUtf8().data() );
288 locstr.remove( 0, locstr.lastIndexOf(
'/' ) + 1 ).replace(
'_',
' ' );
289 QRect timeRect( m_points.value(
"topleft" ), m_points.value(
"middleright" ) );
290 QRect locationRect( m_points.value(
"middleleft" ), m_points.value(
"bottomright" ) );
292 m_locationFont = calculateFont(locstr, locationRect);
293 m_timeFont = calculateFont(timestr, timeRect);
296 QFont WorldClock::calculateFont(
const QString &text,
const QRect &boundingBox)
const
298 QFont resultFont(
"Helvetica", 3, QFont::Bold);
304 for (
int curSize = resultFont.pointSize()+1; unscaled<100; ++curSize ) {
305 resultFont.setPointSize(curSize);
306 QFontMetrics metrics( resultFont );
307 QRect rect = metrics.boundingRect( text );
308 if ( rect.width() > boundingBox.width() ||
309 rect.height() > boundingBox.height() ) {
313 if ( rect.width() > lastBox.width() ||
314 rect.height() > lastBox.height() ) {
324 resultFont.setPointSize(resultFont.pointSize()-1);
328 void WorldClock::recalculateTranslation()
330 m_t = QPoint(static_cast<int>( (m_lastRect.width()/2) - (m_map->
width()/2) ),
331 static_cast<int>( (m_lastRect.height()/2) - (m_map->
height()/2) ));
332 m_t += m_lastRect.topLeft();
336 const QStyleOptionGraphicsItem *option,
337 const QRect &contentsRect)
340 if ( contentsRect != m_lastRect ) {
341 m_lastRect = contentsRect;
343 recalculateTranslation();
347 p->setRenderHint( QPainter::TextAntialiasing ,
true );
348 p->setRenderHint( QPainter::Antialiasing ,
true );
349 p->setPen( Qt::NoPen );
353 pixmap.fill( Qt::transparent );
356 QRect mapRect( 0, 0, m_map->
width(), m_map->
height() );
357 m_map->
paint(gp, mapRect );
358 p->drawPixmap( m_t, pixmap );
360 if ( !m_isHovered ) {
361 setTz( KSystemTimeZones::local().name() );
367 qreal lon = m_locations.value(m_locationkey).longitude() *
DEG2RAD;
368 qreal lat = m_locations.value(m_locationkey).latitude() *
DEG2RAD;
371 QPoint tz( tzx, tzy );
373 int radius = m_lastRect.width() / 40;
374 QRadialGradient grad( tz, radius );
375 grad.setColorAt( 0, QColor( 0xFF, 0xFF, 0x00, 0xFF ) );
376 grad.setColorAt( 0.33, QColor( 0xFF, 0xFF, 0x00, 0x46 ) );
377 grad.setColorAt( 0.66, QColor( 0xFF, 0xFF, 0x00, 0x14 ) );
378 grad.setColorAt( 1, QColor( 0xFF, 0xFF, 0x00, 0x00 ) );
379 p->setBrush( QBrush( grad ) );
380 p->drawEllipse( tz, radius, radius );
383 p->setPen( QColor( 0xFF, 0xFF, 0xFF ) );
385 QString locstr = i18n( m_locationkey.toUtf8().data() );
386 locstr.remove( 0, locstr.lastIndexOf(
'/' ) + 1 ).replace(
'_',
' ' );
390 timestr = KGlobal::locale()->formatDateTime( m_time );
392 timestr = KGlobal::locale()->formatTime( m_time.time() );
394 p->setFont( m_timeFont );
395 p->drawText( QRect( m_points.value(
"topleft" ) + m_t,
396 m_points.value(
"middleright" ) + m_t ),
397 Qt::AlignCenter, timestr );
399 p->setFont( m_locationFont );
400 p->drawText( QRect( m_points.value(
"middleleft" ) + m_t,
401 m_points.value(
"bottomright" ) + m_t ),
402 Qt::AlignCenter, locstr );
409 parent->setButtons(KDialog::Ok | KDialog::Apply | KDialog::Cancel);
411 KConfigGroup cg = config();
413 ui.longitudeEdit->setValue(cg.readEntry(
"rotation", -20));
416 ui.projection->setCurrentIndex(1);
418 ui.projection->setCurrentIndex(0);
420 if(cg.readEntry(
"daylight",
false ))
421 ui.daylightButton->setChecked(
true);
423 if(cg.readEntry(
"showdate",
false ))
424 ui.showdate->setChecked(
true);
426 if(cg.readEntry(
"customtz",
false ))
427 ui.customTz->setChecked(
true);
429 ui.tzWidget->setSelectionMode( QTreeView::MultiSelection );
430 foreach(
const QString& tz, cg.readEntry(
"tzlist")) {
431 ui.tzWidget->setSelected(tz,
true);
434 connect(parent, SIGNAL(okClicked()),
this, SLOT(
configAccepted()));
435 connect(parent, SIGNAL(applyClicked()),
this, SLOT(
configAccepted()));
436 parent->addPage(widget, parent->windowTitle(), icon());
441 KConfigGroup cg = config();
443 if( ui.daylightButton->isChecked() )
446 m_map->
centerOn(ui.longitudeEdit->value(), 0);
450 m_showDate = ui.showdate->isChecked();
451 m_customTz = ui.customTz->isChecked();
454 QStringList tzlist = ui.tzWidget->selection();
455 kDebug() <<
"\tSetting TZLIST";
457 QMap<QString, KTimeZone> selectedZones;
458 selectedZones.insert(KSystemTimeZones::local().name(),
459 KSystemTimeZones::local());
460 foreach(
const QString& tzname, tzlist ) {
461 selectedZones.insert(tzname, KSystemTimeZones::zone(tzname));
463 cg.writeEntry(
"tzlist",tzlist);
464 m_locations = selectedZones;
465 if(!m_locations.contains(m_locationkey))
466 m_locationkey = m_locations.keys().first();
470 if((ui.projection->currentIndex() + 1) != cg.readEntry(
"projection",
473 switch ( ui.projection->currentIndex() ) {
479 cg.writeEntry(
"projection", static_cast<int>(
Mercator));
492 cg.writeEntry(
"rotation", ui.longitudeEdit->value());
493 cg.writeEntry(
"centersun", ui.daylightButton->isChecked());
494 cg.writeEntry(
"showdate", ui.showdate->isChecked());
495 cg.writeEntry(
"customtz", ui.customTz->isChecked());
497 emit configNeedsSaving();
502 #include "worldclock.moc"
WorldClock(QObject *parent, const QVariantList &args)
void setLockToSubSolarPoint(bool visible)
Set the globe locked to the sub solar point.
bool geoCoordinates(const int x, const int y, qreal &lon, qreal &lat, GeoDataCoordinates::Unit unit=GeoDataCoordinates::Degree) const
Get the earth coordinates corresponding to a pixel in the map.
A painter that allows to draw geometric primitives on the map.
This file contains the headers for MarbleModel.
void setSubSolarPointIconVisible(bool visible)
Set whether the sun icon is shown in the sub solar point.
ViewportParams * viewport()
MarbleModel * model() const
Return the model that this view shows.
void setMapThemeId(const QString &maptheme)
Set a new map theme.
Flat projection ("plate carree")
This file contains the headers for MarbleMap.
void setSize(int width, int height)
void paintInterface(QPainter *painter, const QStyleOptionGraphicsItem *option, const QRect &contentsRect)
void hoverEnterEvent(QGraphicsSceneHoverEvent *event)
void setShowCompass(bool visible)
Set whether the compass overlay is visible.
void setShowPlaces(bool visible)
Set whether the place mark overlay is visible.
void setRadius(int radius)
Set the radius of the globe in pixels.
bool screenCoordinates(const qreal lon, const qreal lat, qreal &x, qreal &y) const
Get the screen coordinates corresponding to geographical coordinates in the map.
void setVisible(bool visible)
settting visible
void paint(GeoPainter &painter, const QRect &dirtyRect)
Paint the map using a give painter.
void setClockDateTime(const QDateTime &datetime)
void setShowSunShading(bool visible)
Set whether the night shadow is visible.
void hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
QList< RenderPlugin * > renderPlugins() const
Returns a list of all RenderPlugins in the model, this includes float items.
A class that can paint a view of the earth.
void setProjection(Projection projection)
Set the Projection used for the map.
void setShowTerrain(bool visible)
Set whether the terrain place mark overlay is visible.
void createConfigurationInterface(KConfigDialog *parent)
This file contains the headers for ViewportParams.
void setShowClouds(bool visible)
Set whether the cloud cover is visible.
void setShowGrid(bool visible)
Set whether the coordinate grid overlay is visible.
void hoverMoveEvent(QGraphicsSceneHoverEvent *event)
void setShowOtherPlaces(bool visible)
Set whether the other places overlay is visible.
void dataUpdated(const QString &source, const Plasma::DataEngine::Data &data)
Projection projection() const
Get the Projection used for the map.
void centerOn(const qreal lon, const qreal lat)
Center the view on a geographical point.
void setShowCityLights(bool visible)
Set whether city lights instead of night shadow are visible.
The abstract class that creates a renderable item.
void setShowCities(bool visible)
Set whether the city place mark overlay is visible.
void setShowScaleBar(bool visible)
Set whether the scale bar overlay is visible.