#coding:utf-8
'''
Created on 2014-8-19

@author: root
'''
from PyQt4.QtGui import QWidget,QTextBrowser,QTextCursor,QPushButton,QIcon,QPainter,QLinearGradient, \
                        QBrush,QColor,QLabel,QFont
from PyQt4.QtCore import SIGNAL,Qt,QRect 
from model.thread.readlogfilethread import ReadLogFileThread
import sys

class LogWidget(QWidget):
    def __init__(self,parent=None):
        super(LogWidget,self).__init__(parent)
        self.setFixedSize(900,400)
        self.setWindowFlags(Qt.FramelessWindowHint|Qt.Dialog)
        
        self.initVariable()
        self.initSubWidget()
        
        
    def initVariable(self):
        self.topLine = 50
        self.mousePressed = False
        
    def initSubWidget(self):
        self.logReadThread = ReadLogFileThread()
        
        self.textBrowser = QTextBrowser(self)
        self.textBrowser.setFixedSize(self.geometry().width()-20,self.geometry().height()-self.topLine-45)
        self.textBrowser.move(10,self.topLine)
        
        self.pauseButton = QPushButton(self)
        self.pauseButton.setFixedSize(880,30)
        self.pauseButton.setText(u"暂停")
        self.pauseButton.move(10,self.height()-40)
        self.pauseButton.setObjectName("pauseBtn")
        self.connect(self.pauseButton, SIGNAL("clicked()"),self.slotChangeThreadState)
        
        self.closeButton = QPushButton(self)
        self.closeButton.setFixedSize(30,30)
        self.closeButton.setObjectName("closeBtn")
        self.closeButton.setIcon(QIcon("../images/close.png"))
        self.closeButton.move(self.geometry().width()-35,5)
        self.connect(self.closeButton,SIGNAL("clicked()"),self.cancel)
        self.connect(self.logReadThread, SIGNAL("getoutput"),self.output)
        
        self.tipText = QLabel(u'日志信息',self)
        self.tipText.setFont(QFont("times",15,QFont.Bold))
        self.tipText.move(5,5)

        
    def slotChangeThreadState(self):
        if self.pauseButton.text() == u'暂停':
            self.pauseThread()
        elif self.pauseButton.text() == u'重新开始':
            self.startThread()
            
    def startThread(self):
        self.logReadThread.start()
        self.pauseButton.setText(u"暂停")
        
    def pauseThread(self):
        self.logReadThread.stop()
        self.pauseButton.setText(u"重新开始")
        
    def cancel(self):
        self.logReadThread.stop()
        self.close()
        
    def output(self,text):
        self.textBrowser.clear()
        self.textBrowser.setText(self.trUtf8(text))
        cursor = self.textBrowser.textCursor()
        cursor.movePosition(QTextCursor.End)
        self.textBrowser.setTextCursor(cursor)
        
    def mouseMoveEvent(self,event):
        if self.mousePressed:
            self.move(self.pos() + event.pos() - self.currentPos)   
        
    def mousePressEvent(self,event):
        if event.buttons() == Qt.LeftButton:
            self.currentPos = event.pos()
            self.mousePressed = True
            
    def mouseReleaseEvent(self,event):
        if event.buttons() == Qt.LeftButton:
            self.mousePressed = False
            
    def paintEvent(self,event):
        painter = QPainter(self)
        painter.save()
        linearGradient = QLinearGradient(0,0,0,self.geometry().width())
        linearGradient.setColorAt(0, QColor(60,150,255))
        linearGradient.setColorAt(0.1, QColor(6,88,200))
        linearGradient.setColorAt(1, QColor(80,150,255))
        painter.setBrush(QBrush(linearGradient))
        contenRect = QRect(0, 0, self.width(), self.topLine-10)
        painter.fillRect(contenRect, QBrush(linearGradient))
        painter.restore()
        
        painterBack = QPainter(self)
        backBrush = QBrush(QColor(200,200,250))
        painterBack.setBrush(backBrush)
        backRect = QRect(0,self.topLine-10,self.width(),self.height())
        painterBack.fillRect(backRect, backBrush)

        
        
