#coding:utf-8
from PyQt4.QtCore import Qt, SIGNAL, QRegExp
from PyQt4.QtGui import QVBoxLayout, QLabel,QGridLayout, QRegExpValidator, QLineEdit, QPushButton, QHBoxLayout

from common.infohintdialog import InfoHintDialog

class CenterEditDialog(InfoHintDialog):
    def __init__(self, hintInfo="", parent = None):
        InfoHintDialog.__init__(self, hintInfo, parent)
        self.setStyleSheet("QPushButton{background:url(../images/button.png);border:2px;}")
        self.setWindowFlags(Qt.Dialog|Qt.WindowStaysOnTopHint|Qt.FramelessWindowHint)
        self.setWindowModality(Qt.ApplicationModal)
        self.setFixedSize(400,200)
        self.labelName = QLabel(u"中心:")
        self.lineEditName = QLineEdit()
        
        self.labelIp = QLabel(u"IP地址：")
        self.lineEditIp = QLineEdit()
        
        rx = QRegExp("^([1]?/d/d?|2[0-4]/d|25[0-5])/.([1]?/d/d?|2[0-4]/d|25[0-5])/.([1]?/d/d?|2[0-4]/d|25[0-5])/.([1]?/d/d?|2[0-4]/d|25[0-5])$")
        validator = QRegExpValidator(rx,self)
        self.lineEditIp = QLineEdit()
        self.lineEditIp.setValidator(validator)
        self.lineEditIp.setEnabled(False)
        
        self.saveBtn = QPushButton(u'保存')
        self.saveBtn.setFixedSize(75,27)
        self.saveBtn.setFlat(True)
        self.cancelBtn = QPushButton(u'取消')
        self.cancelBtn.setFixedSize(75,27)
        self.cancelBtn.setFlat(True)
        
        miniLayout = QGridLayout()
        miniLayout.addWidget(self.labelName,0,0)
        miniLayout.addWidget(self.lineEditName,0,1)
        miniLayout.addWidget(self.labelIp,1,0)
        miniLayout.addWidget(self.lineEditIp,1,1)
        
        btnLayout = QHBoxLayout()
        btnLayout.addWidget(self.saveBtn)
        btnLayout.addSpacing(50)
        btnLayout.addWidget(self.cancelBtn)
        
        vLayout = QVBoxLayout()
        vLayout.addSpacing(60)
        vLayout.addLayout(miniLayout)
        vLayout.addSpacing(30)
        vLayout.addLayout(btnLayout)
        
        self.mainlayout = QHBoxLayout(self)
        self.mainlayout.addSpacing(70)
        self.mainlayout.addLayout(vLayout)
        self.mainlayout.addSpacing(70)

        self.okBtn.hide()
        
        self.connect(self.saveBtn, SIGNAL("clicked()"),self.slotSave)
        self.connect(self.cancelBtn, SIGNAL("clicked()"),self.slotCancel)

    def slotCancel(self):
        self.close()

    def slotSave(self):
        self.emit(SIGNAL("changeCenterDate"),self.lineEditName.text(),self.lineEditIp.text())