# -*- coding: utf-8 -*-
# myPlasmoid/contents/code/main.py

#import kde and qt specific stuff
from PyQt4.QtCore import *
from PyQt4 import QtGui
from PyKDE4.plasma import Plasma
from PyKDE4 import plasmascript

#import commands for executing shell commands
from subprocess import Popen, PIPE

#Plasmoid gained by inheritance
class myPlasmoid(plasmascript.Applet):

    #constructor
    def __init__(self,parent,args=None):
        plasmascript.Applet.__init__(self,parent)

    #done once when initiating
    def init(self):

        #disable settings dialog
        self.setHasConfigurationInterface(False)
        #set size of Plasmoid
        self.resize(1086, 90)
        #set aspect ratio mode
        self.setAspectRatioMode(Plasma.IgnoreAspectRatio)
        #set timer interval in ms (1000=1s)
        self.startTimer(1000)

    #done when timer is resetted
    def timerEvent(self, event):

        #call draw method
        self.update()

    #draw method
    def paintInterface(self, painter, option, rect):

        #command to be executed in shell
        cmd = "stan-plasmoida.sh"
        stdout = Popen(cmd, shell=True, stdout=PIPE).stdout
        text = stdout.read().decode('utf8')

        #set pen color and draw text
        painter.save()
        painter.setPen(Qt.black)
        painter.setFont(QtGui.QFont("Inconsolata", 14))
        painter.drawText(rect, Qt.AlignLeft, text)
        painter.restore()

def CreateApplet(parent):
    return myPlasmoid(parent)
