#!/usr/bin/python
#coding=utf8
from PyQt4 import QtGui
from PyQt4.QtGui import QWidget
from PyQt4.QtCore import QString,Qt
from os import getenv
from ui.ui_screenResolutionSet import Ui_Form
from common import common_run

class ScreenResolutionSetQss (QWidget, Ui_Form):
    def __init__(self, parent=None):
        super(ScreenResolutionSetQss, self).__init__(parent)

        self.setupUi(self)
        self.__borderImagesPath = getenv('WIN_UI_IMAGES_PATH') # 读取环境变量 图片路径
        self.__setitstyle()
        self.setAttribute(Qt.WA_DeleteOnClose) # 当窗口关闭时释放内存
        self.mousePressed = False
        self.label_curResolution_mid.hide()  # 暂时隐藏　中间分辨率标签,　分辨率自适应，排版有问题．

    def update_screen_show(self):
        self.__setitstyle()

    # 设置样式
    def __setitstyle(self):
        self.widget.setGeometry(common_run.get_width(30.0),
                                common_run.get_height(20.0),
                                common_run.get_width(300.0),
                                common_run.get_height(30.0))

        self.label_l.setGeometry(common_run.get_width(40), common_run.get_height(270), common_run.get_width(30), common_run.get_height(30))
        self.label_h.setGeometry(common_run.get_width(40), common_run.get_height(50), common_run.get_width(30), common_run.get_height(30))
        self.verticalSlider.setGeometry(common_run.get_width(40), common_run.get_height(85), common_run.get_width(40), common_run.get_height(180))

        self.label_curResolution_mid.setGeometry(common_run.get_width(90), common_run.get_height(152), common_run.get_width(151), common_run.get_height(16))
        self.label_curResolution_min.setGeometry(common_run.get_width(90), common_run.get_height(250), common_run.get_width(131), common_run.get_height(16))
        self.label_curResolution_max.setGeometry(common_run.get_width(90), common_run.get_height(85), common_run.get_width(161), common_run.get_height(16))

        __font_size = str(int(common_run.get_width(13.0)))
        self.setStyleSheet(QString("QLabel{font: 63 "+__font_size+"pt}"))
        self.verticalSlider.setFocusPolicy(Qt.NoFocus)

        self.verticalSlider.setStyleSheet(QString("QSlider::sub-page:horizontal {background: qlineargradient(x1: 0, y1: 0,    x2: 0, y2: 1,  stop: 0 )}"
                                                  "QSlider{border-image:url(%1/settingWindow/slider_back.png);}"#设置背景图片（倒三角形）
                                                # "QSlider:groove:vertical{background:transparent;}"
                                                "QSlider:handle:vertical{border-image:url(%1/settingWindow/slider_handle.png);background:transparent;}"#设置滑块图片
                                     ).arg( str(self.__borderImagesPath)))
        self.label_ticks.setHidden(True)
        self.setTicks()


    def setTicks(self,numbers=21):
        """
        设置刻度显示
        :param numbers: 刻度线数量
        :return:
        """

        # list = common_run.get_screen_resolutionlist()
        # ys = len(list) % 2
        # if ys == 0:
        #     mid_label_stip = common_run.get_height(85.0) + common_run.get_height(180)*float((len(list) / 2)-1) / float(len(list))
        # else:
        mid_label_stip = common_run.get_height(85) + \
                         common_run.get_height(90) -\
                         common_run.get_height(2)

        self.label_curResolution_mid.setGeometry(
            common_run.get_width(90),
            common_run.get_height(mid_label_stip),
            common_run.get_width(151),
            common_run.get_height(16))

        # self.label_ticks.setMinimumHeight(common_run.get_height(190.0))
        # self.label_ticks.setMaximumHeight(common_run.get_height(190.0))
        # self.verticalSlider.setMinimumHeight(common_run.get_height(190.0))
        # self.verticalSlider.setMaximumHeight(common_run.get_height(190.0))
        # totalHeight = self.verticalSlider.height()
        # strip=totalHeight//(numbers-1)
        # for i in range(numbers+1):
        #     label_tick=QtGui.QLabel(self.label_ticks)
        #     label_tick.setGeometry(0,i*strip+5,5,1)
        #     label_tick.setStyleSheet(QString("QLabel{border-image:url(%1/settingWindow/slider_tick.png);}").arg( str(self.__borderImagesPath)))


###############################
#   单元测试
##############################
# if __name__ == "__main__":
    # from PyQt4 import QtGui
    # from os import environ
    # environ["WIN_UI_IMAGES_PATH"]="../../../images"
    # app = QtGui.QApplication([])
    # win = ScreenResolutionSetQss()
    # win.show()
    # app.exec_()
