Skip to content

Instantly share code, notes, and snippets.

@rangalo
Created July 30, 2009 16:06
Show Gist options
  • Save rangalo/158755 to your computer and use it in GitHub Desktop.
Save rangalo/158755 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
#######################################################
#
# @Author: Hardik Mehta <hard.mehta@gmail.com>
#
# @version: 0.1 basic script
#
########################################################
from PyQt4.QtCore import QTimer, QString, Qt, SIGNAL, QRect
from PyQt4.QtGui import QPainter, QStyleOptionGraphicsItem, QBrush, QColor, QFont
from PyKDE4.plasma import Plasma
from PyKDE4 import plasmascript
from weatherInfo import WeatherInfo
from conditionMapper import ConditionMapper
from weather import Weather
import images_rc
class WeatherApplet(plasmascript.Applet):
def __init__(self,parent,args=None):
plasmascript.Applet.__init__(self,parent)
self._wi = WeatherInfo()
self._mapper = ConditionMapper()
self._weather = Weather()
self._unit = "SI"
self._degree_symbol = unichr(176)
self._image_prefix = ":/images/"
self._img_width = 16
self._img_height = 16
self._big_img_width = 48
self._big_img_height = 48
self._fc_column_width = 100
def init(self):
self.setHasConfigurationInterface(False)
self.setAspectRatioMode(Plasma.IgnoreAspectRatio)
self.resize(400,375)
self.checkWeather()
self.timer = QTimer()
self.connect(self.timer,SIGNAL("timeout()"),self.checkWeather)
self.timer.start(0.5*60000)
def paintInterface(self,painter,option,contentRect):
# define some parameters
padding = contentRect.height()/50
txtFieldWidth = contentRect.height()/2 - 2*padding
txtFieldHeight = contentRect.height()/20
current_img_width = contentRect.height()/3
current_img_height = contentRect.height()/3
whiteBrush10p = QBrush(QColor.fromCmyk(0,0,0,0,10))
whiteBrush20p = QBrush(QColor.fromCmyk(0,0,0,0,20))
textColor = Plasma.Theme.defaultTheme().color(Plasma.Theme.TextColor)
bgColor = Plasma.Theme.defaultTheme().color(Plasma.Theme.BackgroundColor)
textFont = Plasma.Theme.defaultTheme().font(Plasma.Theme.DefaultFont)
# create text rects
rect_text_location = QRect(contentRect.left() + padding ,contentRect.top() + 2*padding, txtFieldWidth, txtFieldHeight)
rect_text_temperature = QRect(contentRect.left() + padding ,contentRect.top() + 3* padding + 1 * txtFieldHeight , txtFieldWidth, txtFieldHeight)
rect_text_condition = QRect(contentRect.left() + padding ,contentRect.top() + 4* padding + 2 * txtFieldHeight , txtFieldWidth, txtFieldHeight)
rect_text_humidity = QRect(contentRect.left() + padding ,contentRect.top() + 5* padding + 3 * txtFieldHeight , txtFieldWidth, txtFieldHeight)
rect_text_wind = QRect(contentRect.left() + padding , contentRect.top() + 6* padding + 4 * txtFieldHeight , txtFieldWidth, txtFieldHeight)
painter.save()
painter.setPen(textColor)
painter.setFont(textFont)
painter.drawText(rect_text_location,Qt.Alignment(Qt.AlignLeft),"Location: " + self._weather.location)
painter.drawText(rect_text_temperature,Qt.Alignment(Qt.AlignLeft),"Temperature: " + self._weather.current_temperature)
painter.drawText(rect_text_condition,Qt.Alignment(Qt.AlignLeft),"Condition: " + self._weather.current_condition)
painter.drawText(rect_text_humidity,Qt.Alignment(Qt.AlignLeft),self._weather.current_humidity)
painter.drawText(rect_text_wind,Qt.Alignment(Qt.AlignLeft),self._weather.current_wind)
svg_current = Plasma.Svg()
svg_current.setImagePath(self._image_prefix + self._mapper.getMappedImageName(self._weather.current_condition))
svg_current.resize(current_img_width,current_img_height)
svg_current.paint(painter,contentRect.left() + contentRect.width()/2 + padding,
contentRect.top() + 2 * padding)
painter.restore()
def checkWeather(self):
self._wi.parse()
self._weather.extractData(self._wi,self._unit)
self.update()
def CreateApplet(parent):
return WeatherApplet(parent)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment