#coding:utf-8

import math

from PyQt4.QtCore import Qt, QRect, QPoint
from PyQt4.QtGui import QPainter, QBrush, QColor, QPen, QLinearGradient, QFontMetrics                     , \
    QDialog


class BaseDialog(QDialog):
        
    def __init__(self,parent = None):
        super(BaseDialog,self).__init__(parent)
        self.setWindowFlags(Qt.Dialog|Qt.FramelessWindowHint)
        self.setWindowModality(Qt.ApplicationModal)
        self.mousePressed = False
        self.title = ""
        self.hintInfo = ""
        
    def setTitle(self, title=None):
        self.title = title if title else ""
        
    # 添加鼠标移动窗口
    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):
        titleHeight = 30
        bottomHeight = 2
        
        #画标题背景
        painter = QPainter(self)
        painter.save()
        linearGradient = QLinearGradient(0, 0,0,titleHeight)
        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(), titleHeight)
        painter.fillRect(contenRect, QBrush(linearGradient))
        painter.restore()
        
        #画标题内容
        painter.save()
        painter.setPen(QPen(QColor(255, 255, 255),1))
        font = painter.font()
        font.setPointSize(12)
        painter.setFont(font)
        painter.drawText(QPoint(10,20), self.title)
        painter.restore()
        
        #画中间白色背景
        painter.save()
        painter.setPen(QPen(QColor(255, 255, 255),1))
        brush = QBrush(QColor(242,242,242))
        painter.setBrush(brush)
        contenRect = QRect(0, titleHeight, self.width()-1, self.height() - titleHeight - bottomHeight)

        painter.drawRect(contenRect)
        painter.restore()
        
        #画提示信息内容
        painter.save()
        painter.setPen(QPen(QColor(1, 1, 1),1))
        font = painter.font()
        font.setPointSize(10)
        painter.setFont(font)
        metrics = QFontMetrics(font)
        textTotalLength = metrics.width(self.hintInfo)
        lineCount = math.ceil(float(textTotalLength / self.width()))
        startx =  (self.width() - metrics.width(self.hintInfo)) / 2
        x = startx if startx > 0 else 0
        startY = (self.height() - metrics.ascent() * lineCount)/2
        y = startY

        lineIndex = 0
        curLineLength = 0
        for i, ch in enumerate(self.hintInfo):
            curLineLength += metrics.width(ch)
            if curLineLength > self.width():
                lineIndex += 1
                x= 0
                curLineLength = 0
            painter.drawText(x, y + lineIndex*metrics.ascent(), ch)
            x += metrics.width(ch)
        painter.restore()
        
        #画底层背景
        painter.save()
        brush = QBrush(QColor(219,234,255))
        painter.setBrush(brush)
        contenRect = QRect(0, self.height() - bottomHeight, self.width(), bottomHeight)
        painter.fillRect(contenRect, brush)
        painter.restore()
            