#!/usr/bin/python

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

class LoadMeter(QProgressBar):
    """docstring for LoadMeter"""
    def __init__(self, parent=None):
        super(LoadMeter, self).__init__(parent)

    def paintEvent(self, event):

        val = self.value()
        pos = QStyle.sliderPositionFromValue(self.minimum(), self.maximum(), val, self.width())
        pos60 = QStyle.sliderPositionFromValue(self.minimum(), self.maximum(), 100, self.width())
        pos80 = QStyle.sliderPositionFromValue(self.minimum(), self.maximum(), 120, self.width())

        p = QPainter(self)
        p.setPen(QGraphicsLinearLayout("spread:reflect, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(85, 117, 197, 255), stop:0.65641 rgba(130, 159, 210, 255), stop:0.774359 rgba(150, 177, 215, 255), stop:0.846154 rgba(169, 195, 220, 255), stop:0.933333 rgba(207, 224, 202, 255), stop:1 rgba(249, 255, 180, 255)"))
        p.setBrush(QBrush(QLinearGradient(
            """spread:reflect, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(85, 117, 197, 255), stop:0.65641 rgba(130, 159, 210, 255), stop:0.774359 rgba(150, 177, 215, 255), stop:0.846154 rgba(169, 195, 220, 255), stop:0.933333 rgba(207, 224, 202, 255), stop:1 rgba(249, 255, 180, 255)
            """)))
        if 0 <= val <= 110:
            p.drawRect(0, 0, pos, self.height())

        elif 110 < val <= 130:

            p.drawRect(0,0,pos60, self.height())
            p.setPen(QLinearGradient(
                """
                spread:reflect, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(249, 255, 180, 255), stop:1 rgba(254, 162, 55, 255)
                """))
            p.setBrush(QBrush(QLinearGradient(
                """
                spread:reflect, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(249, 255, 180, 255), stop:1 rgba(254, 162, 55, 255)
                """)))
            p.drawRect(pos60, 0, pos - pos60, self.height())

        else:
            p.drawRect(0,0,pos60, self.height())
            p.setPen(QLinearGradient(
                """
                spread:reflect, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(249, 255, 180, 255), stop:1 rgba(254, 162, 55, 255)
                """))
            p.setBrush(QBrush(QLinearGradient(
                """
                spread:reflect, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(249, 255, 180, 255), stop:1 rgba(254, 162, 55, 255)
                """)))
            p.drawRect(pos60, 0, pos80 - pos60, self.height())
            p.setPen(Qt.red)
            p.setBrush(QBrush(Qt.red))
            p.drawRect(pos80, 0, pos - pos80, self.height())

        p.setPen(QLinearGradient("""
            spread:reflect, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(254, 162, 55, 255), stop:0.225641 rgba(231, 87, 29, 255), stop:0.451282 rgba(217, 40, 13, 255), stop:1 rgba(204, 0, 0, 255)
            """
                                 ))
        p.setBrush(QBrush(QLinearGradient("""
        spread:reflect, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(254, 162, 55, 255), stop:0.225641 rgba(231, 87, 29, 255), stop:0.451282 rgba(217, 40, 13, 255), stop:1 rgba(204, 0, 0, 255)"""
                                          )))
        p.drawRect(pos, 0, self.width(), self.height())


        p.setPen(Qt.black)
        p.setBrush(QBrush(Qt.black))
        p.drawText(0,0, self.width(), self.height(), Qt.AlignCenter, str(val) + "%")


if __name__ == "__main__":

    import sys

    app = QApplication(sys.argv)
    w = LoadMeter()
    w.show()
    w.setValue(100)
    sys.exit(app.exec_())
    