class VMWidget(QWidget):
        
    def __init__(self,parent = None):
        super(VMWidget,self).__init__(parent)
        
        #self.setStyleSheet("QToolTip{background-color:white;color:black;font-size:12px;}")
        #self.setFont(QFont(u"微软雅黑",12))
        self.vmInfoList = []
        self.oldVmInfoList = [] 
        self.vmOffInfoList = []
        self.threadMap = {}
        self.processMap = {}
        self.parent = parent
        self.domainManager = DomainManager()
        self.progressMonitor = ProgressThread()
        self.vmstatusTimer = QTimer()
        #self.vmstatusTimer.start(7000)
        self.connect(self.vmstatusTimer, SIGNAL("timeout()"),self.postStatusToServer)
        self.startVmInfo = None
        if common.SCROLL_TYPE != "slider":
            self.scrollArea = QScrollArea(self)
            self.scrollArea.setStyleSheet("background-color:transparent;border:0px")
            self.vmButtonList = []
            self.vmTableWidget = QTableWidget()
            self.setTableWidgetStyle()
            
            self.mainLayout = QVBoxLayout()
            self.mainLayout.setMargin(0)
            self.mainLayout.setSpacing(0)
            self.mainLayout.addWidget(self.scrollArea)
            self.mainLayout.addSpacing(10)
        
            self.setLayout(self.mainLayout)
        
        
            self.downloadingList = []
        
            
            
        if common.SCROLL_TYPE == "slider":
            #三个自定义button,用于显示相应的虚拟机
            self.firstVM   = CustomVMButton(self)
            self.secondVM  = CustomVMButton(self)
            self.thirdVM   = CustomVMButton(self)
             
            self.vmButtonList = [self.firstVM, self.secondVM, self.thirdVM]
             
            for i in range(len(self.vmButtonList)):
                self.vmButtonList[i].setIcon(QIcon("images/windows.png"))
                self.vmButtonList[i].setIconSize(QSize(100,100))
                self.vmButtonList[i].setFlat(True)
                self.vmButtonList[i].setFont(QFont("Times", 15, QFont.Bold))
            
            #设置滑动条的样式
            self.slider = QSlider(Qt.Vertical,self)
            self.slider.setStyleSheet( "QSlider::groove:vertical {  "
                                        "border: 1px solid #4A708B;  "
                                        "background: #C0C0C0;  "
                                        "width: 5px;  "
                                        "border-radius: 1px;  "
                                        "padding-left:-1px;  "
                                        "padding-right:-1px;  "
                                        "padding-top:-1px;  "
                                        "padding-bottom:-1px;  "
                                        "}"
                                        "QSlider::handle:vertical {"
                                        "height: 100px;"
                                        "background: white;"
                                        "margin: 0 -4px;"
                                        "}" )
             
            self.btnLayout = QHBoxLayout(self)
            self.btnLayout.addStretch()
            self.btnLayout.addWidget(self.firstVM)
            self.btnLayout.addWidget(self.secondVM)
            self.btnLayout.addWidget(self.thirdVM)
            self.btnLayout.addStretch()
            self.btnLayout.addSpacing(10)
            self.btnLayout.addWidget(self.slider)
            self.btnLayout.setSpacing(10)
            self.btnLayout.setMargin(10)
             
            self.setLayout(self.btnLayout)
            
    def setTableWidgetStyle(self):
        
        #self.vmTableWidget.setColumnCount(3)
        #self.vmTableWidget.setRowCount(1)
        self.vmTableWidget.setFrameShape(QFrame.NoFrame)
        self.vmTableWidget.setEditTriggers(QTableWidget.NoEditTriggers)#不能编辑
        self.vmTableWidget.setSelectionMode(QAbstractItemView.NoSelection)
        self.vmTableWidget.verticalHeader().setVisible(False)#设置垂直头不可见
        self.vmTableWidget.horizontalHeader().setVisible(False)#设置垂直头不可见
        self.vmTableWidget.setShowGrid(False)
        self.vmTableWidget.setFocusPolicy(Qt.NoFocus)
        
        self.vmTableWidget.setStyleSheet("QTableWidget{background-color: rgb(235, 235, 235,0);}")    
    
    def getVmList(self):
        vmList = []
        vmInfo = {}
        vmInfo["name"] = "15615123021654541"
        vmInfo["status"] = "offline"
        vmInfo["name"] = "offline"
        vmInfo["os_distro"] = "windows_7"
        
        
        vmInfo1 = {}
        vmInfo1["name"] = "1561545421654541"
        vmInfo1["status"] = "offline"
        vmInfo1["name"] = "offline"
        
        vmInfo2 = {}
        vmInfo2["name"] = "1561afew21654541"
        vmInfo2["status"] = "offline"
        vmInfo2["name"] = "windows_7"
        
        vmInfo3 = {}
        vmInfo3["name"] = "afewfdsafewfa"
        vmInfo3["status"] = "offline"
        vmInfo3["name"] = "windows_7"
        
        vmList.append(vmInfo)
        vmList.append(vmInfo1)
        vmList.append(vmInfo2)
        vmList.append(vmInfo3)
        vmList.append(vmInfo1)
        vmList.append(vmInfo2)
        vmList.append(vmInfo3)
        
        return vmList
    def postStatusToServer(self):
        '''serverState = VMInfoManager.instance().getCurCloudServerState()
        if serverState != "enable":
            return'''
        postInfo = {}
        state = self.domainManager.getStatus()
        if self.vmOffInfoList == [] or len(self.downloadingList) != 0:
            #self.processMap.clear()
            #self.threadMap.clear()
            return
        if len(state) == 0:
            postInfo["state"] = "shutdown"
            postInfo["vmname"] = self.vmOffInfoList[0]["vmname"]
        else:
            for item in state:
                postInfo["state"] = "running"
                if state[item].has_key("disk_io"):
                    postInfo["io_throughput"] = state[item]["disk_io"]
                if state[item].has_key("max_disk_io"):
                    postInfo["io_throughput_peak"] = state[item]["max_disk_io"]
                if state[item].has_key("net_io"):
                    postInfo["net_throughput"] = state[item]["net_io"]
                if state[item].has_key("max_net_io"):
                    postInfo["net_throughput_peak"] = state[item]["max_net_io"]
                if state[item].has_key("memory_size"):
                    postInfo["memory"] = state[item]["memory_size"]*1024
                if state[item].has_key("vmname"):
                    postInfo["vmname"] = state[item]["vmname"]
                if state[item].has_key("name"):
                    postInfo["coursename"] = state[item]["name"]
                if state[item].has_key("cpu"):
                    postInfo["cpu_utilization"] = state[item]["cpu"]     
        self.emit(SIGNAL("vmstateinfo"),postInfo)
        
    def showVmList(self):
        subWidget = QWidget()
        if self.vmstatusTimer.isActive():
            pass
        else:
            #if globalvariable.CLASS_STATUS:
            self.vmstatusTimer.start(7000)
            #self.connect(self.vmstatusTimer, SIGNAL("timeout()"),self.postStatusToServer)
        
        if self.scrollArea.widget():
            self.scrollArea.takeWidget()
            
        self.vmTableWidget = QTableWidget()
        self.setTableWidgetStyle()
        #if self.vmTableWidget
        self.vmTableWidget.clear()
    
        #self.vmInfoList = self.getVmList()
        num = len(self.vmInfoList)
        columnCount = 1
        rowCount = 1
        if num == 0:
            return
        if num <=3:
            #self.scrollArea.verticalScrollBar().hide()
            rowCount = 1
            if num == 1:
                columnCount = 1
            elif num == 2:
                columnCount = 2
            elif num == 3:
                columnCount = 3
        else:
            rowCount = num/3
            rest = num%3
            if (rowCount == 2 and rest == 0)  or rowCount == 1 :
                pass
            if rest > 0:
                rowCount+=1
                
            columnCount = 3
        
        
        parentWidth = self.parent.width()
        resolutionValue = StoreInfoParser.instance().getResolutionValue()
        if resolutionValue:
            if len(resolutionValue.split("x")) >= 2:
                parentWidth = int(resolutionValue.split("x")[0])*5/9.0
        ratio = parentWidth/800.0
        tableWidth = (parentWidth - 20*2 -50*2*ratio)/3 + 5
        if len(self.vmInfoList) > 3:
            tableWidth = (parentWidth - 20*2 -50*2*ratio)/3 - 20*(1 - ratio) + 5
                
        self.vmTableWidget.setColumnCount(columnCount)
        self.vmTableWidget.setRowCount(rowCount)
        self.vmTableWidget.setFixedWidth(parentWidth-100)
        self.vmTableWidget.setFixedHeight((tableWidth+10)*rowCount)
        self.vmTableWidget.verticalHeader().setDefaultSectionSize(tableWidth+10)
        self.vmTableWidget.horizontalHeader().setDefaultSectionSize((parentWidth-100)/columnCount)
        #self.setWidget("status")
        if columnCount <= 3 and rowCount == 1:
            for i in range(columnCount):
                self.setRowColumnWidget(0,i,self.vmInfoList[i]["name"])
        else:
            for i in range(rowCount):
                for j in range(3):
                    if i*3 + j <= num-1:
                        self.setRowColumnWidget(i, j, self.vmInfoList[i*3+j]["name"])  
        
        mainLayout = QHBoxLayout()
        mainLayout.setMargin(0)
        mainLayout.addWidget(self.vmTableWidget)
        subWidget.setLayout(mainLayout)
        #if self.scrollArea.widget():
            #self.scrollArea.takeWidget()
        self.scrollArea.setWidget(subWidget) 
        self.scrollArea.setWidgetResizable(True) 
        #self.scrollArea.horizontalScrollBar().hide()   
    def setRowColumnWidget(self,row,column,vmid):
        status = "sfw"
        #self.vmTableWidget.clear()
        mainWidget = QWidget()
        #self.vmTableWidget.hideColumn()
        #vmRowButtonList = []
        parentWidth = self.parent.width()
        resolutionValue = StoreInfoParser.instance().getResolutionValue()
        if resolutionValue:
            if len(resolutionValue.split("x")) >= 2:
                parentWidth = int(resolutionValue.split("x")[0])*5/9.0
        ratio = parentWidth/800.0
        vmWidth = (parentWidth - 20*2 -50*2*ratio)/3
        if len(self.vmInfoList) > 3:
            vmWidth = (parentWidth - 20*2 -50*2*ratio)/3 - 20*(1 - ratio)
        
        vmButton = CustomVMButton()
        vmButton.setFixedSize(QSize(vmWidth, vmWidth + 5))
        vmButton.setIconSize(QSize(vmWidth - 60*ratio,vmWidth - 30*ratio))
        vmButton.setText(self.vmInfoList[row*3 + column]["name"])
        vmButton.setFlat(True)
        vmButton.setFont(QFont("Times", 15, QFont.Bold))
        vmInfo = self.vmInfoList[row*3 + column]
        vmButton.setVMInfo(vmInfo)
        imageName = "other.png"
        if self.vmInfoList[row*3 + column].has_key("os_distro"):
            if common.imageMap.has_key(self.vmInfoList[row*3 + column]["os_distro"]):
                imageName = common.imageMap[self.vmInfoList[row*3 + column]["os_distro"]]
            else:
                LogRecord.instance().logger.info(u'common.imageMap中未找到键值%s' % self.vmInfoList[row*3 + column]["os_distro"])
                        
        vmButton.setIcon(QIcon("images/systemImage/%s" % imageName))
                
#         if globalvariable.CLASS_STATUS and not globalvariable.TICHU_STATUS:
#             self.connect(vmButton, SIGNAL("clicked()"),self.slotOpenVMLesson)
#             self.connect(vmButton, SIGNAL("controlvm"),self.slotControlVM)
#         else:
#             self.connect(vmButton, SIGNAL("clicked()"),self.slotCreateVMLesson)
                    
        #vmRowButtonList.append(vmButton)
        self.vmButtonList.append(vmButton)
        #firMybutton.setStatus("undownload")
        vmButton.setObjectName(vmid + ":" + QString.number(row) + ":" + QString.number(column))
        self.connect(vmButton, SIGNAL("clicked()"),self.startDomain)
        
            
        progressBar = WidgetProgress()
        progressBar.setFixedSize(vmWidth, vmWidth + 5)
        #progressBar.progressBar.setValue(0)
        progressBar.setVmName(self.vmInfoList[row*3 + column]["name"])
        progressBar.setObjectName(vmid + ":" + QString.number(row) + ":" + QString.number(column))
        #progressBar.objectName()
        
        #self.firMybutton.setText(self.tr("确萨"))
        
        #progressBar = QProgressBar()
        
        myLayout = QHBoxLayout()
        myLayout.setMargin(0)
        myLayout.addStretch()
        myLayout.addWidget(vmButton)
        myLayout.addWidget(progressBar)
        myLayout.addStretch()
        
        if vmid in self.downloadingList:
            vmButton.hide()
        else:
            progressBar.hide()
             
        #firMybutton.hide()
        mainWidget.setLayout(myLayout)
        
        
        self.vmTableWidget.setCellWidget(row, column, mainWidget)
        #self.mainLayout.addWidget(mainWidget)  
    
    def setVMInfoList(self, vmInfoList, vmOffInfoList):
        
        self.oldVmInfoList = LocalImgManager.instance().getCompleteList()
        self.vmInfoList = vmInfoList
        self.vmOffInfoList = vmOffInfoList
        self.domainManager.deleteImgList(vmOffInfoList)
        
#         needDeleteList = self.compareDelete(self.oldVmInfoList, vmOffInfoList)
        if self.vmOffInfoList == []:
            #self.progressMonitor.stop()
            #self.threadMap.clear()
            for item in self.processMap:
                self.processMap[item].stop()
                if self.processMap[item].isRunning():
                    self.processMap[item].exit()
            for item in self.threadMap:
                if self.threadMap[item].isRunning():
                    self.threadMap[item].exit()
                
            self.processMap.clear()
            self.threadMap.clear()
            #self.killRsyncThread()
            
#         if len(needDeleteList) == 0:
#             return
#         else:
#             self.domainManager.deleteImgList(needDeleteList)
    def stopAllDownload(self):
        for item in self.processMap:
            self.processMap[item].stop()
            if self.processMap[item].isRunning():
                self.processMap[item].exit()
                
        self.killScpThread()
        for item in self.threadMap:
            #self.threadMap[item].stop()
            if self.threadMap[item].isRunning():
                self.threadMap[item].exit()
        
        self.downloadingList = []
        self.processMap.clear()
        self.threadMap.clear()
    def killScpThread(self):
        pidList = self.getScpsProcessId()
        #sshPidList = self.getSshProcessId()
        for item in pidList:
            self.killVmId(item)
        '''for mtem in sshPidList:
            self.killVmId(mtem)'''
        
    def killVmId(self,killid):
        cmd = "kill -9 " + killid
        os.system(str(cmd))
        
    def getScpsProcessId(self):#得到运行虚拟机的进程号序列,可用于关闭某个虚拟机
        
        idList = []
        cmd = "ps aux | grep scp"
        output = ""
        statusOutput = []
        statusOutput = commands.getstatusoutput(cmd)
        if statusOutput[0] == 0:
                output = statusOutput[1]
        else:
            output = ""
        result = output.split("\n")
        for i in range(len(result)):
            if QString(result[i]).contains("scp"):
                idList.append(QString(result[i]).simplified().split(" ")[1])
    
        return idList
    
    def getSshProcessId(self):#得到运行虚拟机的进程号序列,可用于关闭某个虚拟机
        
        idList = []
        cmd = "ps aux | grep /usr/bin/ssh"
        output = ""
        statusOutput = []
        statusOutput = commands.getstatusoutput(cmd)
        if statusOutput[0] == 0:
                output = statusOutput[1]
        else:
            output = ""
        result = output.split("\n")
        for i in range(len(result)):
            if QString(result[i]).contains("/usr/bin/ssh"):
                idList.append(QString(result[i]).simplified().split(" ")[1])
    
        return idList
    
    def compareDelete(self,oldList,newList):
        nameList = []
        deleteList = []
        for item in newList:
            nameList.append(item["name"])
            
        for name in oldList:
            if name not in nameList:
                deleteList.append(name)
        return deleteList
    
    def autoStartDownload(self):
        #return
        num = len(self.vmInfoList)
        if num == 0:
            return
        else:
            #self.progressMonitor.setVmInfoList(self.vmInfoList)
            #self.progressMonitor.start()
            #self.connect(self.progressMonitor, SIGNAL("downloadover"),self.slotDownloadOver)
            imgMap = LocalImgManager.instance().getCompliteListSize()
            for i in range(len(self.vmInfoList)):
                mark = 0
                for item in imgMap:
                    if item == self.vmInfoList[i]["name"] and imgMap[item] == self.vmInfoList[i]["img_file_size"]:
                        mark = 1
                if mark == 1:
                    continue
                row = i/3
                column = i%3
                self.downloadingList.append(self.vmInfoList[i]["name"])
#                 self.downloadThread = DownloadThread()
#                 self.progressThread = ProgressThread()
                threadid = self.vmInfoList[i]["name"] + ":" + QString.number(row) + ":" + QString.number(column)
#                 self.downloadThread.setProcessId(threadid + ":" + QString.number(self.vmInfoList[i]["img_file_size"]))
#                 self.progressThread.setProcessId(threadid + ":" + QString.number(self.vmInfoList[i]["img_file_size"]))
#                 self.connect(self.progressThread, SIGNAL("currentprogress"),self.updateProgressBar)
#                 self.connect(self.progressThread, SIGNAL("downloadover"),self.slotDownloadOver)
#                 self.downloadThread.start()
#                 self.progressThread.start()
                self.setRowColumnWidget(int(row), int(column), self.vmInfoList[i]["name"])
                self.createThread(threadid,self.vmInfoList[i])
                
                #time.sleep(1)
                #return
            self.startFirstThread()
    def startFirstThread(self):
        for item in self.threadMap:
            self.threadMap[item].start()
            self.processMap[item].start()
            self.connect(self.processMap[item], SIGNAL("currentprogress"),self.updateProgressBar)
            #self.connect(self.processMap[item], SIGNAL("downloadover"),self.slotDownloadOver)
            self.connect(self.threadMap[item], SIGNAL("downloadover"),self.slotDownloadOver)
            self.connect(self.threadMap[item], SIGNAL("downloaderror"),self.slotDownloadError)
            return
        
    def slotDownloadError(self,signalid):
        pass 
    
    def createThread(self,threadid,vmInfo):
        if self.threadMap.has_key(threadid):
            if self.threadMap[threadid].isRunning():
                self.threadMap[threadid].exit()
            self.threadMap.pop(threadid)
        if self.processMap.has_key(threadid):
            if self.processMap[threadid].isRunning():
                self.processMap[threadid].exit()
            self.processMap.pop(threadid)
        
        downloadThread = DownloadThread()
        progressThread = ProgressThread()
        self.threadMap[threadid] = downloadThread
        self.processMap[threadid] = progressThread
        self.threadMap[threadid].setProcessId(threadid + ":" + str(vmInfo["img_file_size"]))
        self.processMap[threadid].setProcessId(threadid + ":" + str(vmInfo["img_file_size"]))
        #self.threadMap[threadid].start()
        #self.processMap[threadid].start()
        #self.connect(self.processMap[threadid], SIGNAL("currentprogress"),self.updateProgressBar)
        #self.connect(self.processMap[threadid], SIGNAL("downloadover"),self.slotDownloadOver)
        
        
    def checkAddDownload(self):
        #return
        
        num = len(self.vmInfoList)
        #existImgList = LocalImgManager.instance().getCompleteList()
        existImgList = LocalImgManager.instance().getCompliteListSize()
        if num == 0:
            return
        else:
            count = 0
            for i in range(len(self.vmInfoList)):
                row = i/3
                column = i%3
                if existImgList.has_key(self.vmInfoList[i]["name"]) and existImgList.get(self.vmInfoList[i]["name"]) == self.vmInfoList[i]["img_file_size"]:
                    pass
                elif self.vmInfoList[i]["name"] in self.downloadingList:
                    pass
                elif existImgList.has_key(self.vmInfoList[i]["name"]) and self.domainManager.getDom() != None and self.startVmInfo != None and self.startVmInfo == self.domainManager.getDomainInfo():
                    pass
                else:
                    if self.vmInfoList[i]["name"] not in self.downloadingList:
                        self.downloadingList.append(self.vmInfoList[i]["name"])
                    threadid = self.vmInfoList[i]["name"] + ":" + QString.number(row) + ":" + QString.number(column)
                    self.createThread(threadid, self.vmInfoList[i])
                    self.setRowColumnWidget(int(row), int(column), self.vmInfoList[i]["name"])
                    count+= 1
                    
            if len(self.threadMap) == count:
                self.startFirstThread()
                if count > 0:
                    self.emit(SIGNAL("startadddownload"))
                
    def startDomain(self): 
        pbp = CustomVMButton().sender()
        buttonName = pbp.objectName()
        vmInfo = pbp.vmInfo
        for item in self.vmOffInfoList:
            if item["name"] == vmInfo["name"]:
                self.startVmInfo = item
        #return
    
        vmid = buttonName.split(":")[0]
        row = buttonName.split(":")[1]
        column = buttonName.split(":")[2]
        
        self.domainManager.setDomainInfo(self.startVmInfo)
        
        LogRecord.instance().logger.info(u"开始开启虚拟机-----------------------------------------------00")
        self.domainManager.startDomainByName()
        LogRecord.instance().logger.info(u"结束开启虚拟机-----------------------------------------------11")
        

    def updateProgressBar(self,count,objectname):
        #if self.threadMap[objectname].isRunning():
#         for i in range(len(self.downloadingList)):
#             if objectname == self.downloadingList[i]:
#                 progress=self.vmTableWidget.findChild((WidgetProgress, ),objectname)
#                 progress.progressBar.setValue(count) 
        progress=self.vmTableWidget.findChild((WidgetProgress, ),objectname)
        if not progress:
            return
        progress.progressBar.setValue(count) 
        
    def slotDownloadOver(self,count,name):
        serverState = VMInfoManager.instance().getCurCloudServerState()
        if serverState != "enable":
            return
        if self.processMap.has_key(name):
            self.processMap[name].stop()
        #self.processMap.pop(name)
        #self.threadMap.pop(name)
        vmid = name.split(":")[0]
        row = name.split(":")[1]
        column = name.split(":")[2]
        self.downloadingList = []
        self.setRowColumnWidget(int(row), int(column), vmid)
        time.sleep(4)
        if self.processMap.has_key(name) and self.processMap[name].isRunning:
            self.processMap[name].exit()
        if self.threadMap.has_key(name) and self.threadMap[name].isRunning:
            self.threadMap[name].exit()
        self.processMap.pop(name)
        self.threadMap.pop(name)
        self.emit(SIGNAL("avmdownloadover"))
#         for item in self.threadMap:
#             self.threadMap[item].start()
#             self.processMap[item].start()
#             self.connect(self.processMap[item], SIGNAL("currentprogress"),self.updateProgressBar)
#             self.connect(self.processMap[item], SIGNAL("downloadover"),self.slotDownloadOver)
#             return
    def getDownloadingList(self):
        return self.downloadingList
            
    def updateVMList(self):
        language = StoreInfoParser.instance().getLanguage()
        m_pTranslator = QTranslator()
        exePath = "./"
        if language == "chinese":
            QmName = "zh_CN.qm"
            StoreInfoParser.instance().setLanguage("chinese")
        else:
            QmName = "en_US.qm"
            
        if(m_pTranslator.load(QmName, exePath)):
            QCoreApplication.instance().installTranslator(m_pTranslator)
        
        if self.vmstatusTimer.isActive():
            self.vmstatusTimer.stop()
        else:
            pass
        subWidget = QWidget()
        vmNum = len(self.vmInfoList)
        rowNum = 0
        subMainLayout = QVBoxLayout()
        subMainLayout.setMargin(0)
        #subMainLayout.addStretch()
        #subMainLayout.addSpacing(10)
        #subMainLayout.setSpacing(10)
        
        self.vmButtonList = []
        
        if vmNum % 3 != 0:
            rowNum = vmNum / 3 + 1
        else:
            rowNum = vmNum / 3
             
        for i in range(rowNum):
            indexEnd = 3
            if i == rowNum - 1:
                indexEnd = vmNum - (i*3)
                 
            parentWidth = self.parent.width()
            resolutionValue = StoreInfoParser.instance().getResolutionValue()
            if resolutionValue:
                if len(resolutionValue.split("x")) >= 2:
                    parentWidth = int(resolutionValue.split("x")[0])*5/9.0
            
            ratio = parentWidth/800.0
            vmWidth = (parentWidth - 20*2 -50*2*ratio)/3
            if vmNum > 3:
                vmWidth = (parentWidth - 20*2 -50*2*ratio)/3 - 20*(1 - ratio)
            vmRowButtonList = []
            for j in range(indexEnd):
                vmButton = CustomVMButton(self)
                vmButton.setFixedSize(QSize(vmWidth, vmWidth + 5))
                vmButton.setIconSize(QSize(vmWidth - 60*ratio,vmWidth - 30*ratio))
                vmButton.setText(self.vmInfoList[i*3 + j]["name"])
                vmButton.setToolTip(self.tr("course: ") + self.vmInfoList[i*3 + j]["name"])
                vmButton.setStyleSheet(u"QToolTip{border-radius:5px;background-color:white;color:black;font-size:20px;font-family:微软雅黑;}")
                vmButton.setFlat(True)
                vmButton.setFont(QFont("Times", 15, QFont.Bold))
                vmInfo = self.vmInfoList[i*3 + j]
                vmButton.setVMInfo(vmInfo)
                imageName = "other.png"
                if self.vmInfoList[i*3 + j].has_key("os_distro"):
                    if common.imageMap.has_key(self.vmInfoList[i*3 + j]["os_distro"]):
                        imageName = common.imageMap[self.vmInfoList[i*3 + j]["os_distro"]]
                    else:
                        LogRecord.instance().logger.info(u'common.imageMap中未找到键值%s' % self.vmInfoList[i*3 + j]["os_distro"])
                        
                vmButton.setIcon(QIcon("images/systemImage/%s" % imageName))
                if globalvariable.CLASS_STATUS and not globalvariable.TICHU_STATUS:
                    self.connect(vmButton, SIGNAL("clicked()"),self.slotOpenVMLesson)
                    self.connect(vmButton, SIGNAL("controlvm"),self.slotControlVM)
                else:
                    self.connect(vmButton, SIGNAL("clicked()"),self.slotCreateVMLesson)
                    
                vmRowButtonList.append(vmButton)
                self.vmButtonList.append(vmButton)
              
            btnLayout = QHBoxLayout()
            if vmNum > 3:
                btnLayout.setSpacing(10)
                #btnLayout.setMargin(10)
                btnLayout.addSpacing(30)
            for vmbtn in vmRowButtonList:
                btnLayout.addWidget(vmbtn)
            btnLayout.addStretch()
            subMainLayout.addLayout(btnLayout) 
             
        #subMainLayout.addStretch()
        subWidget.setLayout(subMainLayout)
        if self.scrollArea.widget():
            self.scrollArea.takeWidget()
        self.scrollArea.setWidgetResizable(False)
        self.scrollArea.setWidget(subWidget)
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
    def startVM(self):
        return  
        
    def autoOpenVMLesson(self, vmName, allflag = False):
        LogRecord.instance().logger.info(u'自动打开相应的虚拟机')
        if self.vmButtonList:
            vmInfo = self.vmButtonList[0].getVMInfo()
            VMOperation.instance().setCurrentVMInfo(vmInfo)
            #thread.start_new_thread(VMOperation.instance().openVM,())
            if allflag == True:                      
                if VMOperation.instance().openVM():
                    LogRecord.instance().logger.info(u'start the lesson')  
            else:
                if VMOperation.instance().openVM():
                    LogRecord.instance().logger.info(u'start the lesson')  
        else:
            LogRecord.instance().logger.info(u'未监测到虚拟机%s的信息' % vmName)  
    
    def autoOpenOffVMLesson(self):
        LogRecord.instance().logger.info(u'自动打开相应的虚拟机')
        
        if len(self.vmInfoList) == 1:
            if self.vmInfoList[0]["name"] in self.downloadingList:
                return
            else:
                self.startVmInfo = self.vmInfoList[0]
                self.domainManager.setDomainInfo(self.startVmInfo)
                self.domainManager.startDomainByName()
        
        else:
            return
    def checkDownloadOver(self):
        if self.autoCourse in self.downloadingList:
            pass    
        else:
            self.courseTimer.stop()
            #self.autoCourse = ""
            for item in self.vmOffInfoList:
                if self.autoCourse == item["name"]:
                    vmInfo = item
                    self.domainManager.setDomainInfo(vmInfo)
                    self.domainManager.startDomainByName()
                    return
            
    def autoOpenCourse(self,name,classid = None):
        LogRecord.instance().logger.info(u'自动打开相应的虚拟机')
        self.autoCourse = name
        if name in self.downloadingList:
            self.courseTimer = QTimer()
            self.courseTimer.start(1000)
            self.connect(self.courseTimer, SIGNAL("timeout()"),self.checkDownloadOver)
        
        else:
            for item in self.vmOffInfoList:
                if name == item["name"]:
                    vmInfo = item
                    if classid != None:
                        vmInfo["classid"] = classid
                
                    self.domainManager.setDomainInfo(vmInfo)
                    self.domainManager.startDomainByName()
                    
                    return
                
    def slotCreateVMLesson(self):
        vmBtn = self.sender()
        if vmBtn:
            LogRecord.instance().logger.info(u'开始创建虚拟机')
            if globalvariable.VM_IS_CREATE_STATUS:
                LogRecord.instance().logger.info(u'重复点击!')
                return
            else:
                globalvariable.VM_IS_CREATE_STATUS = True

            LogRecord.instance().logger.info(u'准备获取虚拟机信息!')
            paramInfo = vmBtn.getVMInfo()
            LogRecord.instance().logger.info(u'准备获取虚拟机信息完成!')
            try:
                vmName = VMOperation.instance().createVMLesson(paramInfo)
            except:
                LogRecord.instance().logger.info(u'创建虚拟机异常.........!')
            LogRecord.instance().logger.info(u'得到创建的虚拟机信息!')
            if vmName:
                LogRecord.instance().logger.info(u'创建虚拟机%s成功' % vmName)
                vmInfo = NetworkManager.instance().getVMInfo(vmName)
                if vmInfo == None:
                    globalvariable.VM_IS_CREATE_STATUS = False
                    return
                if len(vmInfo) == 0:
                    globalvariable.VM_IS_CREATE_STATUS = False
                    return
                vmInfo[0]["vmname"] = vmInfo[0]["name"]
                if vmInfo:
                    VMOperation.instance().setCurrentVMInfo(vmInfo[0])
                    if VMOperation.instance().openVM(True):
                        #保存开启虚拟机的名称
                        #globalvariable.VM_IS_CREATE_STATUS = False
                        WindowMonitor.instance().insertVmId(vmName)
                    else:
                        globalvariable.VM_IS_CREATE_STATUS = False
                        #删除没有成功运行的虚拟机
                        if VMOperation.instance().removeVMLesson(vmName):
                            LogRecord.instance().logger.info(u"删除后台相应的虚拟机成功")
                else:
                    LogRecord.instance().logger.info(u'未查询到相应的虚拟机:%s' % vmName)
                    globalvariable.VM_IS_CREATE_STATUS = False
            else:
                LogRecord.instance().logger.info(u'创建虚拟机失败')
                globalvariable.VM_IS_CREATE_STATUS = False


            #刷新虚拟机界面
            self.emit(SIGNAL("refreshVMS"))


    def slotControlVM(self, action):
        vmBtn = self.sender()
        if vmBtn:
            vmInfo = vmBtn.getVMInfo()
            VMOperation.instance().setCurrentVMInfo(vmInfo)
            VMOperation.instance().controlVM(action)
            
            vmInfos = globalvariable.VM_INFO
            self.setVMInfoList(vmInfos,[])
            self.updateVMList()

        
    def slotOpenVMLesson(self):
        #vmInfo = []
        vmBtn = self.sender()
        if vmBtn:
            btnVMInfo = vmBtn.getVMInfo()
            VMOperation.instance().setCurrentVMInfo(btnVMInfo)
            VMOperation.instance().openVM(True)
            
#         if globalvariable.CLASS_STATUS:
#             vmInfo = globalvariable.VM_INFO
#         else:
#             vmInfo = VMInfoManager.instance().getLessonListInfo()
#             
        #self.setVMInfoList(vmInfo,[])
        #self.updateVMList()
    #slider        
    def setSliderMaxValue(self, value):
        """设在滑动条的最大值"""
        maxValue = 1
        if value % 3 == 0:
            maxValue = (value / 3)
        else:
            maxValue = (value / 3 + 1)
         
        maxValue = maxValue - 1
         
        self.slider.setMinimum(1)
        self.slider.setMaximum(maxValue*3)
        self.slider.setValue(self.slider.maximum())
        self.currentIndex = maxValue
        self.maxValue = maxValue
        self.updateVMList()
        
    def paintEvent(self,event):
        if common.SCROLL_TYPE == "slider":
            self.setNodeSize(QSize(self.geometry().width()*4/15,self.geometry().height()*4/9))
        elif len(self.vmInfoList) <= 6:
            subWidget = self.scrollArea.widget()
            if subWidget != None:
                if len(self.vmInfoList) <= 3:
                    subWidget.move((self.geometry().width() - subWidget.width())/2, (self.geometry().height() - subWidget.height())/2 - 20)
                else:
                    subWidget.move((self.geometry().width() - subWidget.width())/2 - 15, (self.geometry().height() - subWidget.height())/2)
        QWidget.paintEvent(self, event)
    
    #slider               
    def setNodeSize(self,size):
        self.btnLayout.setSpacing(size.width()/20)
        self.btnLayout.setMargin(size.width()*3/20)
        
        self.firstVM.setIconSize(size)
        self.secondVM.setIconSize(size)
        self.thirdVM.setIconSize(size)
        
        self.slider.resize(QSize(self.slider.frameGeometry().width(),self.height()-100))
        
    def wheelEvent(self,event):
        """鼠标滚轮滚动事件,显示对应的虚拟机"""
        if common.SCROLL_TYPE == "slider":
            if event.delta() < 0 and self.currentIndex > 0:
                self.currentIndex -= 1
                self.updateVMListSlider()
            elif event.delta() > 0 and self.currentIndex < self.maxValue:
                self.currentIndex += 1
                self.updateVMListSlider()
   
            self.slider.wheelEvent(event)
        else:
            maxValue = self.scrollArea.verticalScrollBar().maximum()
            minValue = self.scrollArea.verticalScrollBar().minimum()
            currentValue = self.scrollArea.verticalScrollBar().value()
            if event.delta() < 0 and currentValue < maxValue:
                self.scrollArea.wheelEvent(event)
            elif event.delta() > 0 and currentValue > minValue:
                self.scrollArea.wheelEvent(event)
        
    #slider     
    def updateVMListSlider(self):
        """更新虚拟机列表"""
        for i in range(len(self.vmButtonList)):
            self.vmButtonList[i].hide()
 
        vmsInfoList = VMInfoManager.instance().getVMSInfo()
        if vmsInfoList:
            self.slider.show()
            startIndex = (self.maxValue - self.currentIndex) * 3
             
            if startIndex/3 == (self.maxValue):
                endIndex = len(vmsInfoList)
            else:
                endIndex = startIndex + 3
                 
            for i in range(startIndex, endIndex):
                self.vmButtonList[i - startIndex].setText(vmsInfoList[i])
                self.vmButtonList[i - startIndex].show()  
        else:
            self.slider.hide()
         
            
            
    """      echo <graphics type='sdl display='$DISPLAY' xauth='$XAUTHORITY'/>
Beispiel #2
0
 def clear(self):
     """ Clear the table and set the header label.
     """
     QTableWidget.clear(self)
     self.setRowCount(0)
     self.setColumnCount(2)
     self.setHorizontalHeaderLabels(['Property', 'Value'])
Beispiel #3
0
 def clear(self):
     """ Clear the table and set the header label.
     """
     QTableWidget.clear(self)
     self.setRowCount(0)
     self.setColumnCount(2)
     self.setHorizontalHeaderLabels(['Property', 'Value'])
Beispiel #4
0
    def setSnapshotTabWindow(self, eventNames, eventTs, eventIds):
        tabWidget = None
        isNew = True
        
        for i in range(len(eventIds)):
            if self.tabWindowDict.has_key(eventIds[i]):
                tabWidget = self.tabWindowDict[eventIds[i]]
            else:
                tabWidget = QTableWidget()
                self.tabWindowDict[eventIds[i]] = tabWidget
                QObject.connect(tabWidget, SIGNAL(_fromUtf8("cellDoubleClicked (int,int)")), self.__showArrayData)
            
            data = self.retrieveMasarData(eventid=eventIds[i])
            if data:
                if isNew:
                    for j in range(self.snapshotTabWidget.count(), 0, -1):
                        self.snapshotTabWidget.removeTab(j)
            
                    self.pv4cDict.clear()
                    self.data4eid.clear()
                    self.arrayData.clear()
                    isNew = False

                tabWidget.clear()
                self.setSnapshotTable(data, tabWidget, eventIds[i])
                tabWidget.resizeColumnsToContents()
                ts = eventTs[i].split('.')[0]
                
                label = QString.fromUtf8((eventNames[i]+': ' + ts))
                self.snapshotTabWidget.addTab(tabWidget, label)
                self.snapshotTabWidget.setTabText(i+1, label)
                self.pv4cDict[str(eventIds[i])] = data['PV Name']
                self.data4eid[str(eventIds[i])] = data
            
        self.snapshotTabWidget.setCurrentIndex(1)
Beispiel #5
0
class ImportDataDlg(QDialog):
    __isJy = False  # 数据校验成功的标志
    __mlist = []  # 定义一个列表用于保存从excel表中取出的数据

    def __init__(self, iface, parent=None, impType=ImpDateType.SITEANDCELL):
        super(ImportDataDlg, self).__init__()
        self.iface = iface
        self.parent = parent
        self.impType = impType
        self.initView()

    def initView(self):
        if self.impType == ImpDateType.SERVINGCELL:
            self.setWindowTitle(u'相邻小区数据导入')
        else:
            self.setWindowTitle(u'基站和小区数据导入')
        self.setWindowIcon(QIcon('images/logo.png'))
        self.resize(620, 480)

        # 数据表格
        self.tableWidget = QTableWidget(self)
        self.tableWidget.setAlternatingRowColors(True)
        self.tableWidget.setRowCount(7)
        # 设置当前Table不能编辑
        self.tableWidget.setEditTriggers(QAbstractItemView.NoEditTriggers)
        # 初始化表格上下文菜单
        self.initTableContextMenu()
        # 初始化表头
        self.initTableHeader()
        # 导入出错列表
        self.listWidget = QListWidget(self)
        # 按钮组
        impBtn = QPushButton(u"导入EXCEL表", self)
        yzBtn = QPushButton(u"数据检验", self)
        impdateBtn = QPushButton(u"导入数据", self)
        btnVBox = QVBoxLayout()
        btnVBox.addWidget(impBtn)
        btnVBox.addWidget(yzBtn)
        btnVBox.addWidget(impdateBtn)
        # 错误列表与按钮组
        hBox = QHBoxLayout()
        hBox.setMargin(20)
        hBox.addWidget(self.listWidget)
        hBox.addLayout(btnVBox)

        self.mbar = QStatusBar(self)
        self.mbar.showMessage(u'准备就绪...')

        self.maction = QToolBar(self)
        self.editAction = QAction(u'编辑', self.maction)
        self.editAction.setCheckable(True)

        self.combox = QComboBox(self)
        self.combox.addItems(HeadsConfig.ImpExcelName)

        self.maction.addWidget(self.combox)
        self.maction.addAction(self.editAction)

        vBox = QVBoxLayout()
        vBox.addWidget(self.maction)
        vBox.addWidget(self.tableWidget)
        vBox.addLayout(hBox)
        vBox.addWidget(self.mbar)

        vBox.setStretchFactor(self.tableWidget, 9)
        vBox.setStretchFactor(hBox, 5)
        vBox.setStretchFactor(self.mbar, 1)

        self.setLayout(vBox)

        QObject.connect(impBtn, SIGNAL('clicked()'), self.impClick)
        QObject.connect(yzBtn, SIGNAL('clicked()'), self.yzClick)
        QObject.connect(impdateBtn, SIGNAL('clicked()'), self.impdateClick)
        QObject.connect(self.editAction, SIGNAL('triggered()'), self.editClick)
        QObject.connect(self.combox, SIGNAL('currentIndexChanged(int)'),
                        self.comboxChange)

        self.listWidget.doubleClicked.connect(self.mlistClicked)
        # self.connect(self.listWidget, SIGNAL("itemDoubleClicked (QListWidgetItem)"), self.mlistClicked)

    def initTableContextMenu(self):
        self.tableWidget.setContextMenuPolicy(Qt.CustomContextMenu)
        self.popMenu = QMenu(self.tableWidget)
        delAction = QAction(u'删除', self)  # 删除
        self.popMenu.addAction(delAction)

    # 设置表格可以双击修改数据
    def setEditTriggers(self, isTrigger):
        if isTrigger:
            self.tableWidget.setEditTriggers(QAbstractItemView.DoubleClicked)
        else:
            self.tableWidget.setEditTriggers(QAbstractItemView.NoEditTriggers)

    # 提供给外部修改状态栏消息
    def setStatusBarMsg(self, msg):
        self.mbar.showMessage(msg)

    # 选框改变时,清空当前表格的全部内容,包括表格头
    def updateType(self, mtype):
        self.impType = mtype
        self.tableWidget.clear()  # 清空表格所有内容
        self.initTableHeader()

    # 初始化表格的每个Item
    def initTable(self, mlist):
        self.tableWidget.setRowCount(len(mlist))
        for (i, v) in enumerate(mlist):
            for (j, item) in enumerate(v):
                if type(item) != str:
                    item = unicode(item)
                if item == None:
                    item = u""
                tabItem = QTableWidgetItem(item)
                tabItem.setTextAlignment(Qt.AlignCenter)
                self.tableWidget.setItem(i, j, tabItem)

    # 初始化错误信息列表
    def initListView(self, mlist):

        for iv in mlist:
            lisItm = QListWidgetItem(self.listWidget)
            lisItm.setTextColor(Qt.red)
            lisItm.setData(Qt.UserRole, iv)
            if isinstance(iv, basestring):
                # 如果错误信息是一行字符串
                lisItm.setText(iv)
            else:
                lisItm.setText(u'第' + unicode(str(iv['row'] + 1)) + u'行,第' +
                               unicode(str(iv['col'] + 1)) + u'列:' + iv['msg'])

    # 初始化Table的头
    def initTableHeader(self):
        self.heads = []
        if self.impType == ImpDateType.SITEANDCELL:
            # 获取当前项目图层的字段名
            cell_layer = getLayerByName(u"小区", self.iface)
            for head in HeadsConfig.SiteANDCellHead:
                self.heads.append(head)
            if len(cell_layer.pendingFields()) > 55:
                for (index, field) in enumerate(cell_layer.pendingFields()):
                    if index > 54:
                        field_name = field.name().strip()
                        self.heads.append(field_name)

        else:
            self.heads = HeadsConfig.ServingCellHead

        self.tableWidget.setColumnCount(len(self.heads))  # 设置表格的列数
        for (i, h) in enumerate(self.heads):
            tabItem = QTableWidgetItem(h)
            self.tableWidget.setHorizontalHeaderItem(i, tabItem)

    # 自定义为Table添加Item
    def addTableItem(self, row, col, content):
        tabItem = QTableWidgetItem(content)
        self.tableWidget.setItem(row, col, tabItem)

    # 修改Item的内容
    def editTableItem(self, row, col, content):
        tabItem = self.tableWidget.item(row, col)
        tabItem.setText(content)
        self.tableWidget.setItem(row, col, tabItem)

    # 从Excel表读取数据(导入Excel表)
    def impClick(self):
        fileName = QFileDialog.getOpenFileName(self, u'基站小区数据导入', '/',
                                               'Excel Files (*.xls *.xlsx)')
        if fileName.strip() != "":
            self.setStatusBarMsg(u'选择完毕:' + fileName)
            importData = GetDataFromExcel(fileName, self.impType, self.heads)
            self.__mlist = []
            self.__mlist.extend(importData.getData())
            self.tableWidget.clearContents()
            self.listWidget.clear()
            self.initTable(self.__mlist)
            self.setStatusBarMsg(u'数据导入完成...')
            self.__isJy = False  # 导入完数据后,说明需要重新验证数据的正确性

        else:
            QMessageBox.information(self.parent, u"错误", u"请选中文件")

    # 数据验证按钮点击事件处理
    def yzClick(self):
        if len(self.__mlist) > 0:
            self.erlist = []  # 定义一个列表用于保存数据验证错误的数据
            # 清楚全部的Item
            if self.listWidget.count() > 0:
                self.listWidget.clear()

            # 根据tableWidget更新self.__mlist
            for (r, items) in enumerate(self.__mlist):
                for (v, item) in enumerate(self.__mlist[r]):
                    if self.tableWidget.item(r, v).text() == u"":
                        continue
                    else:
                        # 跟据self.__mlist[r][v]数据类型进行比对
                        if type(self.__mlist[r][v]) == int:
                            if unicode(self.__mlist[r][v]) != (
                                    self.tableWidget.item(r, v).text()):
                                self.__mlist[r][v] = int(
                                    self.tableWidget.item(r, v).text())
                        elif type(self.__mlist[r][v]) == float:
                            if unicode(self.__mlist[r][v]) != (
                                    self.tableWidget.item(r, v).text()):
                                self.__mlist[r][v] = float(
                                    self.tableWidget.item(r, v).text())
                        elif type(self.__mlist[r][v]) == str:
                            if unicode(self.__mlist[r][v]
                                       ) != self.tableWidget.item(r, v).text():
                                self.__mlist[r][v] = str(
                                    self.tableWidget.item(r, v).text())
                        elif type(self.__mlist[r][v]) == unicode:
                            if (self.__mlist[r][v]) != self.tableWidget.item(
                                    r, v).text():
                                self.__mlist[r][v] = self.tableWidget.item(
                                    r, v).text()
                        else:
                            print type(self.__mlist[r][v])
            # 执行数据校验函数
            self.erlist = checkDataByDataType(self.__mlist, self.impType)
            if len(self.erlist) > 0:
                self.initListView(self.erlist)
                QMessageBox.information(self.parent, u'数据校验',
                                        u'数据校验失败,请检查数据正确性后,再导入到地图中')
                self.__isJy = False
            else:
                QMessageBox.information(self.parent, u'数据校验', u'数据校验成功,没有错误数据')
                self.__isJy = True
        else:
            QMessageBox.warning(self.parent, u'数据校验', u'请先导入Excel数据后再操作!')

    # 导入数据到地图中
    def impdateClick(self):
        if self.__isJy:  # 如果数据校验成功
            if self.impType == ImpDateType.SITEANDCELL:
                # 导入基站小区
                importDataToLayer = ImportDataToLayer(self.iface, self.__mlist,
                                                      self.parent)
                if importDataToLayer.importSiteAndCellData():
                    QMessageBox.information(self.parent, u"导入数据", u"导入数据成功!")
                else:
                    QMessageBox.critical(self.parent, u"导入数据", u"导入数据失败!")

            else:
                # 导入相邻小区
                importDataToLayer = ImportDataToLayer(self.iface, self.__mlist,
                                                      self.parent)
                if importDataToLayer.importSCellData():
                    QMessageBox.information(self.parent, u"导入数据", u"导入数据成功!")
                else:
                    QMessageBox.critical(self.parent, u"导入数据", u"导入数据失败!")
        else:
            QMessageBox.warning(self.parent, u'数据导入', u'请确保校验数据成功后,再导入到地图中')

    # 编辑Action点击事件
    def editClick(self):
        self.setEditTriggers(self.editAction.isChecked())

    # 错误列表双击事件处理
    def mlistClicked(self, listItem):
        itemData = listItem.data(Qt.UserRole)
        self.tableWidget.setFocus()
        self.tableWidget.setCurrentCell(itemData['row'], itemData['col'])

    # 选框改变事件
    def comboxChange(self, index):
        self.updateType(index)

    # 字段验证是否为空
    def __validNull(self, name, col, row, itm, rowitm):
        if itm is None or itm == '':
            tmpMap = {}
            tmpMap['col'] = col
            tmpMap['row'] = row
            tmpMap['msg'] = unicode(name) + u'不能为空'
            tmpMap['item'] = rowitm
            return tmpMap
        else:
            return None

    # 导入数据线程开始信号  绑定函数
    def impStart(self):
        self.setStatusBarMsg(u'准备导入...')

    # 导入数据线程发生异常信号 绑定函数
    def impError(self, e, exception_string):
        self.setStatusBarMsg(u'发生错误:' + unicode(e))
        QMessageBox.warning(self.parent, u'Excel数据导入', u'发生错误:' + unicode(e))

    # 导入数据线程完成信号 绑定函数
    def impFinish(self, mylist):
        self.__mlist = []
        self.__mlist.extend(mylist)
        self.mthread.quit()
        self.mthread.wait()
        self.mthread.deleteLater()
        self.impDateThread.deleteLater()

        self.tableWidget.clearContents()
        self.listWidget.clear()
        self.initTable(self.__mlist)
        self.setStatusBarMsg(u'数据导入完成...')
        self.__isJy = False  # 导入完数据后,说明需要重新验证数据的正确性

    # 导入数据到地图线程发生异常信号 绑定函数
    def impError1(self, e, exception_string):
        self.setStatusBarMsg(u"导入数据发生错误")
        QMessageBox.critical(self, u'数据导入', u"发生错误:" + unicode(e))

    # 导入数据到地图线程完成信号 绑定函数
    def impFinish1(self, mylist):
        self.threadImp.quit()
        self.threadImp.wait()
        self.threadImp.deleteLater()
        self.impFeatureThread.deleteLater()
        remsg = u'数据导入完成!'
        layer = None
        if self.impType == LayerType.SITE:
            # remsg = u'基站' + remsg
            layer = getLayerByName(u'基站', self.iface)
        elif self.impType == LayerType.CELL:
            # remsg = u'小区' + remsg
            layer = getLayerByName(u'小区', self.iface)
        else:
            remsg = u'相邻小区' + remsg
            layer = getLayerByName(u'相邻小区', self.iface)
        self.setStatusBarMsg(remsg)
        layer.updateExtents()  # 更新地图数据
        self.iface.actionDraw().trigger()
        QMessageBox.information(self, u'数据导入', remsg)

        merlist = []
        for eritm in self.erlist:
            merlist.append(eritm['item'])
        self.tableWidget.clearContents()  # 先清楚表格的内容,再将错误的行显示到表格中
        self.initTable(merlist)
class ViewTableForm(QWidget):
    def __init__(self, visualization, parent_widget=None):
        QWidget.__init__(self, parent_widget)
        self.inGui = False

        self.widgetLayout = QVBoxLayout(self)

        self.tableWidget = QTableWidget(self)
        self.tableWidget.setObjectName("tableWidget")
        size = QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
        self.tableWidget.setSizePolicy(size)
        self.widgetLayout.addWidget(self.tableWidget)

        self.tabIcon = QIcon(":/Images/Images/map.png")

        self.tabLabel = visualization.table_name

        self.load_table(visualization=visualization)

    def load_table(self, visualization, limit=10000):

        storage = StorageFactory().get_storage(
            type='%s_storage' % visualization.output_type,
            storage_location=visualization.storage_location)
        table_data = storage.load_table(table_name=visualization.table_name)

        try:
            primary_keys = visualization.indicators[0].primary_keys
        except:
            primary_keys = []

        keys = primary_keys + [
            key for key in table_data.keys() if key not in primary_keys
        ]
        num_rows = min(len(table_data[keys[0]]), limit)
        num_cols = len(keys)

        self.tableWidget.clear()
        self.tableWidget.setColumnCount(num_cols)
        self.tableWidget.setRowCount(num_rows)

        j = 0
        for key in keys:
            col = QTableWidgetItem()
            col.setText(QString(key))
            self.tableWidget.setHorizontalHeaderItem(j, col)
            j += 1

        self.tableWidget.resizeColumnsToContents()

        order = sorted(enumerate(table_data[keys[0]]), lambda (i, v),
                       (j, v2): int(v * 100) - int(v2 * 100))

        for i, (idx, v) in enumerate(order):
            row = QTableWidgetItem()
            self.tableWidget.setVerticalHeaderItem(i, row)
            j = 0
            for key in keys:
                item = QTableWidgetItem()
                item.setText(QString(str(table_data[key][idx])))
                self.tableWidget.setItem(i, j, item)
                j += 1
            if i > limit:
                msg = 'The table %s has been truncated to %i rows because of memory limitations.' % (
                    visualization.table_name, limit)
                detailed_msg = '<qt>To view the full results, open the following file:<br><br><small>%s</small></qt>' % visualization.get_file_path(
                )
                MessageBox.warning(mainwindow=self,
                                   text=msg,
                                   detailed_text=detailed_msg)
                break

        #self.tableWidget.resizeRowsToContents()
    def removeElement(self):
        return True
Beispiel #7
0
class OWxsh_waviness(widget.OWWidget):
    name = "xsh_waviness"
    id = "orange.widgets.preprocessor.xsh_waviness"
    description = "xoppy application to compute..."
    icon = "icons/waviness.png"
    author = "Luca Rebuffi"
    maintainer_email = "[email protected]; [email protected]"
    priority = 10
    category = ""
    keywords = ["xoppy", "xsh_waviness"]

    outputs = [{"name": "PreProcessor_Data",
                "type": ShadowPreProcessorData,
                "doc": "PreProcessor Data",
                "id": "PreProcessor_Data"}]

    want_main_area = 1
    want_control_area = 1

    WIDGET_WIDTH = 1100
    WIDGET_HEIGHT = 650

    xx = None
    yy = None
    zz = None

    number_of_points_x = Setting(10)
    number_of_points_y = Setting(100)

    dimension_x = Setting(20.1)
    dimension_y = Setting(113.1)

    estimated_slope_error = Setting(0.9)
    montecarlo_seed = Setting(2387427)

    waviness_file_name = Setting('waviness.dat')

    harmonic_maximum_index = Setting(60)

    data = Setting({'c': ['0.3',
                          '0.1',
                          '0.1',
                          '0.0',
                          '0.0',
                          '0.0',
                          '0.3',
                          '0.0',
                          '0.0',
                          '0.3',
                          '0.0',
                          '0.0',
                          '0.5',
                          '0.0',
                          '0.0',
                          '0.2',
                          '0.2',
                          '0.2',
                          '0.9',
                          '0.0',
                          '0.0',
                          '0.0',
                          '0.0',
                          '0.0',
                          '0.4',
                          '0.0',
                          '0.0',
                          '0.4',
                          '0.0',
                          '0.0',
                          '0.0',
                          '0.6',
                          '0.6',
                          '0.0',
                          '0.4',
                          '0.4',
                          '0.0',
                          '0.4',
                          '0.4',
                          '0.1',
                          '0.4',
                          '0.4',
                          '0.1',
                          '0.2',
                          '0.2',
                          '0.0',
                          '0.2',
                          '0.2',
                          '0.0',
                          '0.3',
                          '0.3',
                          '0.0',
                          '0.0',
                          '0.0',
                          '0.0',
                          '0.0',
                          '0.0',
                          '0.0',
                          '0.0',
                          '0.0',
                          '0.0'],
                    'y': ['0.0',
                          '-0.1',
                          '-0.1',
                          '0.0',
                          '0.0',
                          '0.0',
                          '0.03',
                          '0.0',
                          '0.0',
                          '0.2',
                          '0.0',
                          '0.0',
                          '0.2',
                          '0.0',
                          '0.0',
                          '0.1',
                          '0.1',
                          '0.1',
                          '0.1',
                          '0.0',
                          '0.0',
                          '0.0',
                          '0.0',
                          '0.0',
                          '0.01',
                          '0.0',
                          '0.0',
                          '0.03',
                          '0.0',
                          '0.0',
                          '0.0',
                          '0.02',
                          '0.02',
                          '0.0',
                          '0.1',
                          '0.1',
                          '0.0',
                          '0.1',
                          '0.1',
                          '0.0',
                          '0.1',
                          '0.1',
                          '0.0',
                          '0.0',
                          '0.0',
                          '0.0',
                          '0.3',
                          '0.3',
                          '0.0',
                          '0.2',
                          '0.2',
                          '0.0',
                          '0.2',
                          '0.2',
                          '0.0',
                          '0.2',
                          '0.2',
                          '0.0',
                          '0.0',
                          '0.0',
                          '0.0'],
                    'g': ['0.0',
                          '0.3',
                          '0.3',
                          '0.0',
                          '0.0',
                          '0.0',
                          '0.05',
                          '0.0',
                          '0.0',
                          '0.05',
                          '0.0',
                          '0.0',
                          '0.1',
                          '0.0',
                          '0.0',
                          '0.05',
                          '0.05',
                          '0.05',
                          '0.2',
                          '0.0',
                          '0.0',
                          '0.0',
                          '0.0',
                          '0.0',
                          '0.1',
                          '0.0',
                          '0.0',
                          '0.1',
                          '0.0',
                          '0.0',
                          '0.0',
                          '0.2',
                          '0.2',
                          '0.0',
                          '0.1',
                          '0.1',
                          '0.0',
                          '0.1',
                          '0.1',
                          '0.0',
                          '0.1',
                          '0.1',
                          '0.0',
                          '0.0',
                          '0.0',
                          '0.0',
                          '0.1',
                          '0.1',
                          '0.0',
                          '0.2',
                          '0.2',
                          '0.0',
                          '0.1',
                          '0.1',
                          '0.0',
                          '0.1',
                          '0.1',
                          '0.0',
                          '0.0',
                          '0.0',
                          '0.0']})

    def __init__(self):
        super().__init__()

        geom = QApplication.desktop().availableGeometry()
        self.setGeometry(QRect(round(geom.width() * 0.05),
                               round(geom.height() * 0.05),
                               round(min(geom.width() * 0.98, self.WIDGET_WIDTH)),
                               round(min(geom.height() * 0.95, self.WIDGET_HEIGHT))))

        gen_box = ShadowGui.widgetBox(self.controlArea, "Waviness Parameters", addSpace=True, orientation="horizontal",
                                      width=500)

        tabs_setting = gui.tabWidget(gen_box)

        tab_input = ShadowGui.createTabPage(tabs_setting, "Input Parameter")
        tab_harmonics = ShadowGui.createTabPage(tabs_setting, "Harmonics")
        tab_out = ShadowGui.createTabPage(tabs_setting, "Output")

        self.input_box = ShadowGui.widgetBox(tab_input, "Inputs", addSpace=True, orientation="vertical", width=470)

        gui.button(self.input_box, self, "Load xsh_waviness input file ...", callback=self.load_inp_file)

        gui.separator(self.input_box)

        ShadowGui.lineEdit(self.input_box, self, "number_of_points_x", "Number of Points (<201)           X (width)",
                           labelWidth=300, valueType=int, orientation="horizontal")
        ShadowGui.lineEdit(self.input_box, self, "number_of_points_y",
                           "                                                 Y (length)", labelWidth=300, valueType=int,
                           orientation="horizontal")

        gui.separator(self.input_box)

        ShadowGui.lineEdit(self.input_box, self, "dimension_x", "Dimensions [cm]                        X (width)",
                           labelWidth=300, valueType=float, orientation="horizontal")
        ShadowGui.lineEdit(self.input_box, self, "dimension_y",
                           "                                                 Y (length)", labelWidth=300,
                           valueType=float, orientation="horizontal")

        gui.separator(self.input_box)

        ShadowGui.lineEdit(self.input_box, self, "estimated_slope_error", "Estimated slope error [arcsec]",
                           labelWidth=300, valueType=float, orientation="horizontal")
        ShadowGui.lineEdit(self.input_box, self, "montecarlo_seed", "Monte Carlo initial seed", labelWidth=300,
                           valueType=int, orientation="horizontal")

        self.output_box = ShadowGui.widgetBox(tab_input, "Outputs", addSpace=True, orientation="vertical", width=470)

        self.select_file_box = ShadowGui.widgetBox(self.output_box, "", addSpace=True, orientation="horizontal")

        gui.separator(self.output_box)

        gui.button(self.output_box, self, "Write xsh_waviness input file (optional) ...", callback=self.write_inp_file)

        ShadowGui.lineEdit(self.select_file_box, self, "waviness_file_name", "Output File Name", labelWidth=120,
                           valueType=str, orientation="horizontal")

        self.harmonics_box = ShadowGui.widgetBox(tab_harmonics, "Harmonics", addSpace=True, orientation="vertical",
                                                 width=470, height=690)

        ShadowGui.lineEdit(self.harmonics_box, self, "harmonic_maximum_index", "Harmonic Maximum Index", labelWidth=300,
                           valueType=int, orientation="horizontal", callback=self.set_harmonics)

        gui.separator(self.harmonics_box)

        self.scrollarea = QScrollArea()
        self.scrollarea.setMaximumWidth(400)

        self.harmonics_box.layout().addWidget(self.scrollarea, alignment=Qt.AlignHCenter)

        self.shadow_output = QTextEdit()
        self.shadow_output.setReadOnly(True)

        out_box = ShadowGui.widgetBox(tab_out, "System Output", addSpace=True, orientation="horizontal", height=600)
        out_box.layout().addWidget(self.shadow_output)

        button_box = ShadowGui.widgetBox(self.controlArea, "", addSpace=False, orientation="horizontal")

        button = gui.button(button_box, self, "Calculate Waviness", callback=self.calculate_waviness)
        button.setFixedHeight(45)
        button.setFixedWidth(170)

        button = gui.button(button_box, self, "Generate Waviness File", callback=self.generate_waviness_file)
        font = QFont(button.font())
        font.setBold(True)
        button.setFont(font)
        palette = QPalette(button.palette())  # make a copy of the palette
        palette.setColor(QPalette.ButtonText, QColor('Dark Blue'))
        button.setPalette(palette)  # assign new palette
        button.setFixedHeight(45)
        button.setFixedWidth(200)

        button = gui.button(button_box, self, "Reset Fields", callback=self.call_reset_settings)
        font = QFont(button.font())
        font.setItalic(True)
        button.setFont(font)
        palette = QPalette(button.palette())  # make a copy of the palette
        palette.setColor(QPalette.ButtonText, QColor('Dark Red'))
        button.setPalette(palette)  # assign new palette
        button.setFixedHeight(45)
        button.setFixedWidth(120)

        gui.rubber(self.controlArea)

        self.figure = Figure(figsize=(600, 600))
        self.figure.patch.set_facecolor('white')

        self.axis = self.figure.add_subplot(111, projection='3d')

        self.axis.set_xlabel("X (cm)")
        self.axis.set_ylabel("Y (cm)")
        self.axis.set_zlabel("Z (µm)")

        self.figure_canvas = FigureCanvasQTAgg(self.figure)
        self.mainArea.layout().addWidget(self.figure_canvas)

        gui.rubber(self.mainArea)

    def restoreWidgetPosition(self):
        super().restoreWidgetPosition()

        self.table = QTableWidget(self.harmonic_maximum_index + 1, 3)
        self.table.setAlternatingRowColors(True)
        self.table.horizontalHeader().setResizeMode(QHeaderView.Fixed)

        for i in range(0, 3):
            self.table.setColumnWidth(i, 70)

        horHeaders = []
        verHeaders = []

        for n, key in enumerate(sorted(self.data.keys())):
            horHeaders.append(key)

            for m, item in enumerate(self.data[key]):
                table_item = QTableWidgetItem(str(item))
                table_item.setTextAlignment(Qt.AlignRight)
                self.table.setItem(m, n, table_item)
                verHeaders.append(str(m))

        self.table.setHorizontalHeaderLabels(horHeaders)
        self.table.setVerticalHeaderLabels(verHeaders)
        self.table.resizeRowsToContents()

        self.table.itemChanged.connect(self.table_item_changed)

        self.scrollarea.setWidget(self.table)
        self.scrollarea.setWidgetResizable(1)

        gui.rubber(self.controlArea)

    def reload_harmonics_table(self):
        horHeaders = []
        verHeaders = []

        self.table.itemChanged.disconnect(self.table_item_changed)

        self.table.clear()

        row_count = self.table.rowCount()

        for n in range(0, row_count):
            self.table.removeRow(0)

        for index in range(0, self.harmonic_maximum_index + 1):
            self.table.insertRow(0)

        for n, key in enumerate(sorted(self.data.keys())):
            horHeaders.append(key)

            for m, item in enumerate(self.data[key]):
                table_item = QTableWidgetItem(str(item))
                table_item.setTextAlignment(Qt.AlignRight)
                self.table.setItem(m, n, table_item)
                verHeaders.append(str(m))

        self.table.setHorizontalHeaderLabels(horHeaders)
        self.table.setVerticalHeaderLabels(verHeaders)

        self.table.resizeRowsToContents()

        for i in range(0, 3):
            self.table.setColumnWidth(i, 70)

        self.table.itemChanged.connect(self.table_item_changed)

    def table_item_changed(self):
        dict = {}
        message = ""
        error_row_index = -1
        error_column_index = -1
        previous_value = ""

        try:
            row_count = self.harmonic_maximum_index + 1

            for column_index in range(0, self.table.columnCount()):
                column_name = self.table.horizontalHeaderItem(column_index).data(0)

                row_content = []

                for row_index in range(0, row_count):
                    if not self.table.item(row_index, column_index) is None:
                        message = "Value at row " + str(
                            row_index) + " and column \'" + column_name + "\' is not numeric"
                        error_row_index = row_index
                        error_column_index = column_index
                        previous_value = self.data[column_name][row_index]

                        value = float(self.table.item(row_index, column_index).data(0))  # to raise exception

                        row_content.append(str(value))

                dict[column_name] = row_content

            self.data = dict
        except ValueError:
            QMessageBox.critical(self, "QMessageBox.critical()",
                                 message + "\nValue is reset to previous value",
                                 QMessageBox.Ok)

            table_item = QTableWidgetItem(previous_value)
            table_item.setTextAlignment(Qt.AlignRight)
            self.table.setItem(error_row_index, error_column_index, table_item)
            self.table.setCurrentCell(error_row_index, error_column_index)

        except Exception as exception:
            QMessageBox.critical(self, "QMessageBox.critical()",
                                 exception.args[0],
                                 QMessageBox.Ok)

    def set_harmonics(self):
        if self.harmonic_maximum_index < 0:
            QMessageBox.critical(self, "QMessageBox.critical()",
                                 "Harmonic Maximum Index should be a positive integer number",
                                 QMessageBox.Ok)
        else:
            row_count = len(self.data["c"])

            if self.harmonic_maximum_index + 1 > row_count:
                for n, key in enumerate(sorted(self.data.keys())):
                    for m in range(row_count, self.harmonic_maximum_index + 1):
                        self.data[key].append('0.0')
            else:
                for n, key in enumerate(sorted(self.data.keys())):
                    self.data[key] = copy.deepcopy(self.data[key][0: self.harmonic_maximum_index + 1])

            self.reload_harmonics_table()

    def load_inp_file(self):
        file_name = QFileDialog.getOpenFileName(self, "Select a input file for XSH_WAVINESS", ".", "*.inp")

        if not file_name is None:
            sys.stdout = EmittingStream(textWritten=self.writeStdOut)

            if not file_name.strip() == "":
                dict = ST.waviness_read(file=file_name)

                self.number_of_points_x = dict["npointx"]
                self.number_of_points_y = dict["npointy"]
                self.dimension_y = dict["xlength"]
                self.dimension_x = dict["width"]
                self.estimated_slope_error = dict["slp"]
                self.montecarlo_seed = dict["iseed"]
                self.waviness_file_name = dict["file"].strip('\n\r').strip()
                self.harmonic_maximum_index = dict["nharmonics"]

                self.data["c"] = self.to_str_array(dict["c"])
                self.data["y"] = self.to_str_array(dict["y"])
                self.data["g"] = self.to_str_array(dict["g"])

                self.reload_harmonics_table()

    def write_inp_file(self):
        try:
            sys.stdout = EmittingStream(textWritten=self.writeStdOut)

            self.check_fields()

            file_name = self.waviness_file_name.strip().split(sep=".dat")[0] + ".inp"

            dict = {}

            dict["npointx"] = self.number_of_points_x
            dict["npointy"] = self.number_of_points_y
            dict["xlength"] = self.dimension_y
            dict["width"] = self.dimension_x
            dict["slp"] = self.estimated_slope_error
            dict["iseed"] = self.montecarlo_seed
            dict["file"] = self.waviness_file_name.strip('\n\r')
            dict["nharmonics"] = self.harmonic_maximum_index

            dict["c"] = self.to_float_array(self.data["c"])
            dict["y"] = self.to_float_array(self.data["y"])
            dict["g"] = self.to_float_array(self.data["g"])

            ST.waviness_write(dict, file=file_name)

            QMessageBox.information(self, "QMessageBox.information()",
                                    "File \'" + file_name + "\' written to disk",
                                    QMessageBox.Ok)

        except Exception as exception:
            QMessageBox.critical(self, "QMessageBox.critical()",
                                 exception.args[0],
                                 QMessageBox.Ok)

    def calculate_waviness(self):
        try:
            sys.stdout = EmittingStream(textWritten=self.writeStdOut)

            self.check_fields()

            xx, yy, zz = ST.waviness_calc(npointx=self.number_of_points_x,
                                          npointy=self.number_of_points_y,
                                          width=self.dimension_x,
                                          xlength=self.dimension_y,
                                          slp=self.estimated_slope_error,
                                          nharmonics=self.harmonic_maximum_index,
                                          iseed=self.montecarlo_seed,
                                          c=self.to_float_array(self.data["c"]),
                                          y=self.to_float_array(self.data["y"]),
                                          g=self.to_float_array(self.data["g"]))
            self.xx = xx
            self.yy = yy
            self.zz = zz

            self.axis.clear()

            x_to_plot, y_to_plot = numpy.meshgrid(xx, yy)
            z_to_plot = []

            for y_index in range(0, len(yy)):
                z_array = []
                for x_index in range(0, len(xx)):
                    z_array.append(1e4 * float(zz[x_index][y_index]))  # to micron
                z_to_plot.append(z_array)

            z_to_plot = numpy.array(z_to_plot)

            self.axis.plot_surface(x_to_plot, y_to_plot, z_to_plot,
                                   rstride=1, cstride=1, cmap=cm.autumn, linewidth=0.5, antialiased=True)

            slope, sloperms = ST.slopes(zz, xx, yy)

            title = ' Slope error rms in X direction: %f arcsec' % (sloperms[0]) + '\n' + \
                    '                                            : %f urad' % (sloperms[2]) + '\n' + \
                    ' Slope error rms in Y direction: %f arcsec' % (sloperms[1]) + '\n' + \
                    '                                            : %f urad' % (sloperms[3])

            self.axis.set_xlabel("X (cm)")
            self.axis.set_ylabel("Y (cm)")
            self.axis.set_zlabel("Z (µm)")
            self.axis.set_title(title)
            self.axis.mouse_init()

            self.figure_canvas.draw()

            QMessageBox.information(self, "QMessageBox.information()",
                                    "Waviness calculated: if the result is satisfactory,\nclick \'Generate Waviness File\' to complete the operation ",
                                    QMessageBox.Ok)
        except Exception as exception:
            QMessageBox.critical(self, "QMessageBox.critical()",
                                 exception.args[0],
                                 QMessageBox.Ok)


    def generate_waviness_file(self):
        if not self.zz is None and not self.yy is None and not self.xx is None:
            if not self.waviness_file_name is None:
                self.waviness_file_name = self.waviness_file_name.strip()

                if self.waviness_file_name == "": raise Exception("Output File Name missing")
            else:
                raise Exception("Output File Name missing")

            sys.stdout = EmittingStream(textWritten=self.writeStdOut)

            ST.write_shadow_surface(self.zz.T, self.xx, self.yy, outFile=self.waviness_file_name)
            QMessageBox.information(self, "QMessageBox.information()",
                                    "Waviness file " + self.waviness_file_name + " written on disk",
                                    QMessageBox.Ok)

            self.send("PreProcessor_Data", ShadowPreProcessorData(waviness_data_file=self.waviness_file_name))

    def call_reset_settings(self):
        if ConfirmDialog.confirmed(parent=self, message="Confirm Reset of the Fields?"):
            try:
                self.resetSettings()
                self.reload_harmonics_table()
            except:
                pass

    def check_fields(self):
        self.number_of_points_x = ShadowGui.checkStrictlyPositiveNumber(self.number_of_points_x, "Number of Points X")
        self.number_of_points_y = ShadowGui.checkStrictlyPositiveNumber(self.number_of_points_y, "Number of Points Y")

        self.dimension_x = ShadowGui.checkStrictlyPositiveNumber(self.dimension_x, "Dimension X")
        self.dimension_y = ShadowGui.checkStrictlyPositiveNumber(self.dimension_y, "Dimension Y")

        self.estimated_slope_error = ShadowGui.checkPositiveNumber(self.estimated_slope_error, "Estimated slope error")
        self.montecarlo_seed = ShadowGui.checkPositiveNumber(self.montecarlo_seed, "Monte Carlo initial seed")

        self.harmonic_maximum_index = ShadowGui.checkPositiveNumber(self.harmonic_maximum_index,
                                                                    "Harmonic Maximum Index")

        if not self.waviness_file_name is None:
            self.waviness_file_name = self.waviness_file_name.strip()

            if self.waviness_file_name == "": raise Exception("Output File Name missing")
        else:
            raise Exception("Output File Name missing")


    def to_float_array(self, string_array):
        float_array = []

        for index in range(len(string_array)):
            float_array.append(float(string_array[index]))

        return float_array

    def to_str_array(self, float_array):
        string_array = []

        for index in range(len(float_array)):
            string_array.append(str(float_array[index]))

        return string_array

    def writeStdOut(self, text):
        cursor = self.shadow_output.textCursor()
        cursor.movePosition(QTextCursor.End)
        cursor.insertText(text)
        self.shadow_output.setTextCursor(cursor)
        self.shadow_output.ensureCursorVisible()
Beispiel #8
0
class MainForm(QDialog):
    def __init__(self, parent=None):
        super(MainForm, self).__init__(parent)

        self.words_to_be_learned_llist = []
        self.words2sentence_list = []

        self.files_table = QTableWidget()
        self.files_paths_list = []
        self.refresh_files_table()

        self.files_Button = QPushButton(u"添加文章")

        self.zhong_kao_CheckBox = QCheckBox(u"中考词汇")
        self.gao_kao_CheckBox = QCheckBox(u"高考词汇")
        self.analysis_Button = QPushButton(u"分析")

        self.files_layout =  QHBoxLayout()
        self.files_layout.addWidget(self.files_table)
        self.files_layout.addWidget(self.files_Button)

        self.a_g_layout = QHBoxLayout()
        self.a_g_layout.addWidget(self.zhong_kao_CheckBox)
        self.a_g_layout.addWidget(self.gao_kao_CheckBox)
        self.a_g_layout.addWidget(self.analysis_Button)
        # self.a_g_layout.addWidget(self.generate_Button)

        self.layout = QVBoxLayout()
        self.layout.addLayout(self.files_layout)
        self.layout.addLayout(self.a_g_layout)

        self.setLayout(self.layout)
        self.setWindowTitle(u"外文阅读工具")

        self.connect(self.files_Button, SIGNAL("clicked()"), self.addFiles)
        self.connect(self.analysis_Button, SIGNAL("clicked()"), self.analysis)

    def addFiles(self):
        print("addFiles")
        files = QFileDialog.getOpenFileNames(self, "Select Music Files",
            QDesktopServices.storageLocation(QDesktopServices.DocumentsLocation))
        if not files:
            return
        self.files_paths_list = files
        print "len_list = {0}".format(len(self.files_paths_list))
        self.refresh_files_table()

    def refresh_files_table(self):
        print("refresh_files_table")
        self.files_table.clear()

        self.Y_MAX = len(self.files_paths_list)
        self.X_MAX = 1
        self.files_table.setColumnCount(self.X_MAX)
        self.files_table.setRowCount(self.Y_MAX)
        self.files_table.setHorizontalHeaderLabels([u'文章列表'])#tips:这一句必须在setHeaderLabels之后出现
        for y in range(self.Y_MAX):
            text = re.split("[^a-zA-Z0-9\_\.\(\)]",#这里用正则表达式,因为windows和linux下的斜杠不同
                            self.files_paths_list[y])[-1]
            item = QTableWidgetItem(u"{0}".format(text))
            self.files_table.setItem(y, 0, item)

    def analysis(self):
        print("in analysis")
        time1 = time.time()
        try:#每次读取known时建立一个.bak备份吧
            known_words = A_a.load_known_words()
            print "known_words",len(known_words)
        except:
            QMessageBox.warning(self, u"Error", u"known.txt not found!")
            return

        if len(self.files_paths_list) == 0:
            QMessageBox.warning(self, u"Error", u"文章列表不能为空!")
            return

        self.words_to_be_learned_llist = []#!!复原这个llist,别掉了self了
        self.words2sentence_list = []
        for _file in self.files_paths_list:
            words_to_be_learned, words_sentence = A_a.analysis_article(_file, known_words)
            self.words_to_be_learned_llist.append(words_to_be_learned)
            self.words2sentence_list.append(words_sentence)

        time_used = time.time() - time1
        Form_analysis = F_a.analysis_Form(time_used ,self.files_paths_list,
                                    self.words_to_be_learned_llist, self.words2sentence_list)
        if Form_analysis.exec_():
            pass
Beispiel #9
0
class MainForm(QDialog):
    def __init__(self, parent=None):
        super(MainForm, self).__init__(parent)

        self.words_to_be_learned_llist = []
        self.words2sentence_list = []

        self.files_table = QTableWidget()
        self.files_paths_list = []
        self.refresh_files_table()

        self.files_Button = QPushButton(u"添加文章")

        self.zhong_kao_CheckBox = QCheckBox(u"中考词汇")
        self.gao_kao_CheckBox = QCheckBox(u"高考词汇")
        self.analysis_Button = QPushButton(u"分析")

        self.files_layout = QHBoxLayout()
        self.files_layout.addWidget(self.files_table)
        self.files_layout.addWidget(self.files_Button)

        self.a_g_layout = QHBoxLayout()
        self.a_g_layout.addWidget(self.zhong_kao_CheckBox)
        self.a_g_layout.addWidget(self.gao_kao_CheckBox)
        self.a_g_layout.addWidget(self.analysis_Button)
        # self.a_g_layout.addWidget(self.generate_Button)

        self.layout = QVBoxLayout()
        self.layout.addLayout(self.files_layout)
        self.layout.addLayout(self.a_g_layout)

        self.setLayout(self.layout)
        self.setWindowTitle(u"外文阅读工具")

        self.connect(self.files_Button, SIGNAL("clicked()"), self.addFiles)
        self.connect(self.analysis_Button, SIGNAL("clicked()"), self.analysis)

    def addFiles(self):
        print("addFiles")
        files = QFileDialog.getOpenFileNames(
            self, "Select Music Files",
            QDesktopServices.storageLocation(
                QDesktopServices.DocumentsLocation))
        if not files:
            return
        self.files_paths_list = files
        print "len_list = {0}".format(len(self.files_paths_list))
        self.refresh_files_table()

    def refresh_files_table(self):
        print("refresh_files_table")
        self.files_table.clear()

        self.Y_MAX = len(self.files_paths_list)
        self.X_MAX = 1
        self.files_table.setColumnCount(self.X_MAX)
        self.files_table.setRowCount(self.Y_MAX)
        self.files_table.setHorizontalHeaderLabels(
            [u'文章列表'])  #tips:这一句必须在setHeaderLabels之后出现
        for y in range(self.Y_MAX):
            text = re.split(
                "[^a-zA-Z0-9\_\.\(\)]",  #这里用正则表达式,因为windows和linux下的斜杠不同
                self.files_paths_list[y])[-1]
            item = QTableWidgetItem(u"{0}".format(text))
            self.files_table.setItem(y, 0, item)

    def analysis(self):
        print("in analysis")
        time1 = time.time()
        try:  #每次读取known时建立一个.bak备份吧
            known_words = A_a.load_known_words()
            print "known_words", len(known_words)
        except:
            QMessageBox.warning(self, u"Error", u"known.txt not found!")
            return

        if len(self.files_paths_list) == 0:
            QMessageBox.warning(self, u"Error", u"文章列表不能为空!")
            return

        self.words_to_be_learned_llist = []  #!!复原这个llist,别掉了self了
        self.words2sentence_list = []
        for _file in self.files_paths_list:
            words_to_be_learned, words_sentence = A_a.analysis_article(
                _file, known_words)
            self.words_to_be_learned_llist.append(words_to_be_learned)
            self.words2sentence_list.append(words_sentence)

        time_used = time.time() - time1
        Form_analysis = F_a.analysis_Form(time_used, self.files_paths_list,
                                          self.words_to_be_learned_llist,
                                          self.words2sentence_list)
        if Form_analysis.exec_():
            pass
Beispiel #10
0
class VerifySetpoint(QDialog):
    def __init__(self, configFile, rowCount, verifyWindowDict, parent=None):
        super(VerifySetpoint,
              self).__init__(parent,
                             Qt.CustomizeWindowHint | Qt.WindowTitleHint)
        self.configFile = configFile
        self.rowCount = rowCount
        self.verifyWindowDict = verifyWindowDict
        self.setWindowTitle('%s: setpoint v.s. readback' %
                            self.configFile.split('/')[-1])
        resolution = QDesktopWidget().screenGeometry()
        #self.setGeometry(resolution.width(), resolution.height() ,250, 150)
        self.setGeometry(resolution.width(), 0, 250, 150)
        self.startUpdate = 1
        self.keys = []

        fd = open(self.configFile)
        lines = fd.readlines()
        #print(lines)
        setpointPVList = []
        readbackPVList = []
        self.allPVList = []
        #thresholdList = []
        rampRatePVList = []
        for line in lines:
            #print(line.split())
            setpointPVList.append(str(line.split()[0]))
            readbackPVList.append(str(line.split()[1]))
            if len(line.split()) > 2:
                #thresholdList.append(str(line.split()[2]))
                rampRatePVList.append(str(line.split()[2]))
        self.allPVList = setpointPVList + readbackPVList + rampRatePVList
        self.pvListColumn = 3
        #print(setpointPVList)
        #print(readbackPVList)
        #print(self.allPVList)
        #print(thresholdList)
        #print(rampRatePVList)

        layout = QGridLayout(self)
        self.label = QLabel()
        if self.rowCount > len(readbackPVList):
            self.label.setText(
                "%d PVs in the original snapshot, but only %d pairs of setpoint &\
readback PVs in this table because some setpoint PVs don't have readbacks\n\nPlease click the \
button below to update data\n" % (self.rowCount, len(readbackPVList)))
        else:
            self.label.setText(
                "%d pairs of setpoint & readback PVs in this table\n\n \
Please click the button below to update data\n" % (len(readbackPVList)))
        layout.addWidget(self.label, 0, 0, 1, 2)

        self.table = QTableWidget()
        sizePolicy = QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(
            self.table.sizePolicy().hasHeightForWidth())
        self.table.setSizePolicy(sizePolicy)
        self.table.setMinimumSize(QSize(700, 500))
        self.table.resize(self.table.sizeHint())

        self.table.setRowCount(len(setpointPVList))
        #if len(thresholdList):
        if len(rampRatePVList):
            #self.keys = ['setpoint PV','readback PV','SP value','RB value','diff', 'threshold']
            self.keys = [
                'setpoint PV', 'readback PV', 'SP value (Amp)',
                'RB value (Amp)', 'diff', 'ramp-rate (Amp/Sec)'
            ]
        else:
            self.keys = [
                'setpoint PV', 'readback PV', 'SP value', 'RB value', 'diff'
            ]

        self.table.setColumnCount(len(self.keys))
        self.table.setHorizontalHeaderLabels(self.keys)
        layout.addWidget(self.table, 1, 0, 1, 2)

        #=======================================================================
        # toggleButton = QPushButton('Updating started', self)
        # toggleButton.clicked.connect(self.toggle)
        # layout.addWidget(toggleButton, 2, 0, 1, 1)
        #=======================================================================

        updateButton = QPushButton('Update Table', self)
        updateButton.clicked.connect(self.updateTable)
        layout.addWidget(updateButton, 2, 0, 1, 1)

        self.quit = QPushButton('Close Widget', self)
        #self.connect(self.quit, SIGNAL('clicked()'), self.close)
        #self.destroyed.connect(self.cleanup())
        #self.deleteLater.connect(self.cleanup())
        self.connect(self.quit, SIGNAL('clicked()'), self.cleanup)
        layout.addWidget(self.quit, 2, 1, 1, 1)
        self.setLayout(layout)
        #self.timer = cothread.Timer(2, self.updateTable, retrigger=True, reuse=True)
        self.updateTable()
        #camonitor(self.allPVList, self.callback)
        #t = threading.Timer(2, self.updateTable)
        #t.start()

    #===========================================================================
    # def toggle(self):
    #    source = self.sender()
    #    if self.startUpdate:
    #        source.setText("Updating stopped")
    #        self.startUpdate = 0
    #    else:
    #        source.setText("Updating started")
    #        self.startUpdate = 1
    #    print(self.startUpdate)
    #===========================================================================

    def __setTableItem(self, table, row, col, text):
        item = table.item(row, col)
        if item:
            item.setText(text)
        else:
            newitem = QTableWidgetItem(text)
            newitem.setFlags(Qt.ItemIsEnabled | Qt.ItemIsSelectable)
            table.setItem(row, col, newitem)

    #def updateTable(self):
    #camonitor(self.allPVList, self.callback)
    #cothread.WaitForQuit()

    def updateTable(self):
        #def callback(self, value, index):
        #while(True):
        #if self.startUpdate:
        disConnectedPVs = []
        connectedPVs = []
        self.table.clear()
        self.table.setHorizontalHeaderLabels(self.keys)
        self.table.setSortingEnabled(False)
        #print("update table:")
        #print(self.allPVList)
        #cothread.Sleep(2)
        connnectionStatus = connect(self.allPVList,
                                    cainfo=True,
                                    wait=False,
                                    timeout=2,
                                    throw=False)
        for status in connnectionStatus:
            if status.ok != True:
                disConnectedPVs.append(status.name)
            else:
                connectedPVs.append(status.name)

        if len(disConnectedPVs) > 0:
            print("%d PVs seem disconnected: \n" % len(disConnectedPVs))
            print(disConnectedPVs)
            self.label.setText(
                "%d PVs in the original snapshot, but only %d pairs of setpoint &\
readback in this table because some PVs don't have readbacks or they are disconnected\n\nPlease click the \
button below to update data\n" % (self.rowCount, len(connectedPVs)))
            self.allPVList = connectedPVs

        try:
            pvValues = caget(self.allPVList)
        except:
            print("Oops: can't get PV values to verify setpoint and readback")
            self.label.setText(
                "Oops: can't get PV values to verify setpoint and readback\n\n"
            )
            traceback.print_exc()
            return
        #print(pvValues)

        for i in range(int(len(self.allPVList) / self.pvListColumn)):
            self.__setTableItem(self.table, i, 0,
                                str(self.allPVList[i]))  #setpoint PV name
            self.__setTableItem(
                self.table, i, 1,
                str(self.allPVList[
                    i + int(len(self.allPVList) / self.pvListColumn)]))
            self.__setTableItem(self.table, i, 2, str(pvValues[i]))
            self.__setTableItem(
                self.table, i, 3,
                str(pvValues[i +
                             int(len(self.allPVList) / self.pvListColumn)]))
            diff_ = abs(pvValues[i] -
                        pvValues[i +
                                 int(len(self.allPVList) / self.pvListColumn)])
            diff = diff_.__format__('.9f')
            self.__setTableItem(self.table, i, 4, str(diff))
            self.__setTableItem(
                self.table, i, 5,
                str(pvValues[i +
                             int((self.pvListColumn - 1) *
                                 len(self.allPVList) / self.pvListColumn)]))

        #self.table.resize(self.table.sizeHint())
        self.table.setSortingEnabled(True)
        self.table.sortItems(4, 1)
        self.table.resizeColumnsToContents()
        #print("end of update table:")
        #return 2
        #self.timer.reset(2, retrigger=True)
        #time.sleep(2)
    def cleanup(self):
        del self.verifyWindowDict[
            self.configFile]  #closed verifyWindow can be opened again
        self.close()
class VerifySetpoint(QDialog):
    def __init__(self, configFile, rowCount, verifyWindowDict, parent=None):
        super(VerifySetpoint, self).__init__(parent, Qt.CustomizeWindowHint|Qt.WindowTitleHint)
        self.configFile = configFile
        self.rowCount = rowCount
        self.verifyWindowDict = verifyWindowDict
        self.setWindowTitle('%s: setpoint v.s. readback'%self.configFile.split('/')[-1])  
        resolution = QDesktopWidget().screenGeometry()
        #self.setGeometry(resolution.width(), resolution.height() ,250, 150)
        self.setGeometry(resolution.width(),0, 250, 150)
        self.startUpdate = 1   
        self.keys = []       
        
        fd = open(self.configFile)
        lines = fd.readlines()
        #print(lines)
        setpointPVList = []
        readbackPVList = []
        self.allPVList = []
        #thresholdList = []
        rampRatePVList = []
        for line in lines:
            #print(line.split())
            setpointPVList.append(str(line.split()[0]))
            readbackPVList.append(str(line.split()[1]))
            if len(line.split())>2:
                #thresholdList.append(str(line.split()[2]))
                rampRatePVList.append(str(line.split()[2]))
        self.allPVList = setpointPVList + readbackPVList + rampRatePVList
        self.pvListColumn = 3
        #print(setpointPVList)
        #print(readbackPVList)
        #print(self.allPVList)
        #print(thresholdList) 
        #print(rampRatePVList)      
        
        layout = QGridLayout(self)  
        self.label = QLabel()
        if self.rowCount > len(readbackPVList):
            self.label.setText("%d PVs in the original snapshot, but only %d pairs of setpoint &\
readback PVs in this table because some setpoint PVs don't have readbacks\n\nPlease click the \
button below to update data\n"%(self.rowCount,len(readbackPVList)))   
        else:
            self.label.setText("%d pairs of setpoint & readback PVs in this table\n\n \
Please click the button below to update data\n"%(len(readbackPVList)))             
        layout.addWidget(self.label, 0, 0, 1, 2)
         
        self.table = QTableWidget()
        sizePolicy = QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.table.sizePolicy().hasHeightForWidth())
        self.table.setSizePolicy(sizePolicy)
        self.table.setMinimumSize(QSize(700, 500))
        self.table.resize(self.table.sizeHint())
        
        self.table.setRowCount(len(setpointPVList))
        #if len(thresholdList):
        if len(rampRatePVList):
            #self.keys = ['setpoint PV','readback PV','SP value','RB value','diff', 'threshold']
            self.keys = ['setpoint PV','readback PV','SP value (Amp)','RB value (Amp)','diff', 'ramp-rate (Amp/Sec)']
        else:
            self.keys = ['setpoint PV','readback PV','SP value','RB value','diff']
        
        self.table.setColumnCount(len(self.keys))
        self.table.setHorizontalHeaderLabels(self.keys)             
        layout.addWidget(self.table, 1, 0, 1, 2)    

        #=======================================================================
        # toggleButton = QPushButton('Updating started', self)
        # toggleButton.clicked.connect(self.toggle)
        # layout.addWidget(toggleButton, 2, 0, 1, 1)
        #=======================================================================
        
        updateButton = QPushButton('Update Table', self)
        updateButton.clicked.connect(self.updateTable)
        layout.addWidget(updateButton, 2, 0, 1, 1)
        
        self.quit = QPushButton('Close Widget', self)
        #self.connect(self.quit, SIGNAL('clicked()'), self.close)
        #self.destroyed.connect(self.cleanup())
        #self.deleteLater.connect(self.cleanup())
        self.connect(self.quit, SIGNAL('clicked()'), self.cleanup)
        layout.addWidget(self.quit, 2, 1, 1, 1)
        self.setLayout(layout)    
        #self.timer = cothread.Timer(2, self.updateTable, retrigger=True, reuse=True)
        self.updateTable()
        #camonitor(self.allPVList, self.callback)
        #t = threading.Timer(2, self.updateTable)
        #t.start()  
    
    #===========================================================================
    # def toggle(self):
    #    source = self.sender()
    #    if self.startUpdate:
    #        source.setText("Updating stopped")
    #        self.startUpdate = 0
    #    else:
    #        source.setText("Updating started")
    #        self.startUpdate = 1
    #    print(self.startUpdate)    
    #===========================================================================
            
 
    def __setTableItem(self, table, row, col, text):
        item = table.item(row, col)
        if item:
            item.setText(text)
        else:
            newitem = QTableWidgetItem(text)
            newitem.setFlags(Qt.ItemIsEnabled|Qt.ItemIsSelectable)
            table.setItem(row, col, newitem)    
        
    #def updateTable(self):
        #camonitor(self.allPVList, self.callback)
        #cothread.WaitForQuit()
       
    def updateTable(self):
    #def callback(self, value, index):
        #while(True):
            #if self.startUpdate: 
        disConnectedPVs = []
        connectedPVs = []
        self.table.clear()
        self.table.setHorizontalHeaderLabels(self.keys) 
        self.table.setSortingEnabled(False)
        #print("update table:")
        #print(self.allPVList)
        #cothread.Sleep(2)
        connnectionStatus = connect(self.allPVList, cainfo=True, wait=False, timeout=2, throw=False)
        for status in connnectionStatus:
            if status.ok != True:
                disConnectedPVs.append(status.name)
            else:
                connectedPVs.append(status.name)
        
        if len(disConnectedPVs) > 0:
            print("%d PVs seem disconnected: \n"%len(disConnectedPVs))
            print(disConnectedPVs)
            self.label.setText("%d PVs in the original snapshot, but only %d pairs of setpoint &\
readback in this table because some PVs don't have readbacks or they are disconnected\n\nPlease click the \
button below to update data\n"%(self.rowCount,len(connectedPVs)))
            self.allPVList = connectedPVs
        
        try:
            pvValues = caget(self.allPVList)
        except:
            print("Oops: can't get PV values to verify setpoint and readback")
            self.label.setText("Oops: can't get PV values to verify setpoint and readback\n\n")
            traceback.print_exc()
            return
        #print(pvValues)
        
        for i in range(int(len(self.allPVList)/self.pvListColumn)):
            self.__setTableItem(self.table, i, 0, str(self.allPVList[i]))#setpoint PV name
            self.__setTableItem(self.table, i, 1, str(self.allPVList[i+int(len(self.allPVList)/self.pvListColumn)]))
            self.__setTableItem(self.table, i, 2, str(pvValues[i]))
            self.__setTableItem(self.table, i, 3, str(pvValues[i+int(len(self.allPVList)/self.pvListColumn)]))
            diff_ = abs(pvValues[i] - pvValues[i+int(len(self.allPVList)/self.pvListColumn)])
            diff = diff_.__format__('.9f')
            self.__setTableItem(self.table, i, 4, str(diff))
            self.__setTableItem(self.table, i, 5, str(pvValues[i+int((self.pvListColumn-1)*len(self.allPVList)/self.pvListColumn)]))
    
        #self.table.resize(self.table.sizeHint())
        self.table.setSortingEnabled(True)
        self.table.sortItems(4,1)
        self.table.resizeColumnsToContents()
        #print("end of update table:")
        #return 2
        #self.timer.reset(2, retrigger=True)
                #time.sleep(2)
    def cleanup(self):
        del self.verifyWindowDict[self.configFile]#closed verifyWindow can be opened again
        self.close()
        
Beispiel #12
0
class edycja_proby(QDialog):
    def __init__(self, sample, defpol={}, defpol_order=[], parent=None):
        ''' defpol and defpol_order should contains the same values, it will
        not be checked in this class is it valid, pay attention to it'''
        super(edycja_proby, self).__init__(parent)

        # self.dane = globals()["daneW"][0]
        # self.defpol = globals()["daneW"][1]
        # globals()["daneW"] = []
        self.sample = copy.deepcopy(sample)
        self.sample._edytowany = 'E'
        self.org_sample = sample
        self.newName = self.sample.KeyCode()

        self.setWindowTitle("Sample: " + self.sample.KeyCode())
        self.resize(691, 749)
        self.setMinimumSize(QSize(691, 749))
        # self.setMaximumSize(QSize(691, 749))

        # Prepare headers
        self.defpol = defpol
        self.defpol_order = defpol_order
        self.prepareDefPol()
        self.selected = []  # list with index of selected ring in sample

        # ustawienie pol z danymi
        self.p_naglowek = QTableWidget()
        self.p_naglowek.setObjectName("p_naglowek")
        self.p_naglowek.setColumnCount(1)
        self.p_naglowek.setRowCount(len(self.headers))
        self.p_naglowek.setHorizontalHeaderLabels(["Value"])
        self.p_naglowek.setVerticalHeaderLabels(self.headers)
        self.p_naglowek.setAlternatingRowColors(True)
        self.p_naglowek.setSortingEnabled(False)

        self.p_dane = QTableWidget()
        self.p_dane.setObjectName("p_dane")
        self.p_dane.setColumnCount(10)
        self.p_dane.setHorizontalHeaderLabels(
            ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"])

        self.dodaj = QPushButton("Add")
        self.usun = QPushButton("Delete")
        self.przerysuj = QPushButton("Redraw")
        self.podziel = QPushButton("Divide")
        self.polacz = QPushButton("Join")
        self.anuluj = QPushButton("Cancel")
        self.wykonaj = QPushButton("Update")

        self.vbl = QVBoxLayout()
        self.qmc = Qt4MplCanvas(self)
        self.ntb = NavigationToolbar(self.qmc, self)
        self.vbl.addWidget(self.qmc)
        self.vbl.addWidget(self.ntb)

        self.updateHeaderTable()
        self.wpisz_pomiary()
        self.przerysuj_wykres()

        # ustawiamy wyglad okna
        layout = QGridLayout()
        layout.addWidget(self.p_naglowek, 0, 0)

        layout1 = QGridLayout()
        layout1.addWidget(self.p_dane, 0, 0, 1, 5)
        layout1.addWidget(self.dodaj, 1, 0)
        layout1.addWidget(self.usun, 1, 1)
        layout1.addWidget(self.podziel, 1, 2)
        layout1.addWidget(self.polacz, 1, 3)
        layout1.addWidget(self.przerysuj, 1, 4)
        layout1.setRowMinimumHeight(1, 25)
        layout1.setRowStretch(0, 1)

        layout.addLayout(layout1, 0, 1)
        layout.addLayout(self.vbl, 1, 0, 1, 2)
        layout.addWidget(self.anuluj, 2, 0)
        layout.addWidget(self.wykonaj, 2, 1)
        layout.setColumnMinimumWidth(0, 290)
        layout.setColumnStretch(1, 1)
        layout.setRowStretch(0, 1)
        layout.setRowMinimumHeight(0, 350)
        self.setLayout(layout)

        # Sygnaly
        self.connect(self.anuluj, SIGNAL("clicked()"), self.schowaj)
        self.connect(self.wykonaj, SIGNAL("clicked()"), self.wykonaj_odczytanie)
        self.connect(self.dodaj, SIGNAL("clicked()"), self.dodaj_wartosc)
        self.connect(self.przerysuj, SIGNAL("clicked()"), self.przerysuj_wykres)
        self.connect(self.usun, SIGNAL("clicked()"), self.usun_wartosc)
        self.connect(self.podziel, SIGNAL("clicked()"), self.podziel_wartosc)
        self.connect(self.polacz, SIGNAL("clicked()"), self.polacz_wartosc)
        self.p_naglowek.cellChanged.connect(self.edytowana_kom_nagl)
        self.p_dane.itemSelectionChanged.connect(self.przerysuj_wykres)
        self.p_dane.cellChanged.connect(self.przerysuj_wykres)

    def prepareDefPol(self):
        """Prepare Headers for metadata,
        BEAWARE! measurements are delete on the end"""
        self.headers = [
                        'KeyCode',
                        'DateBegin',
                        'DateEnd',
                        'Length',
                        'Gat',
                        'SapWoodRings',
                        'measurements',
                        ]
        add_table = []
        if len(self.defpol_order):
            add_table = self.defpol_order[:]
        elif len(self.defpol.keys()):
            add_table = sorted(list(self.defpol.keys()))
        else:
            add_table = sorted(self.sample.unikalneNaglowki())

        for val in add_table:
            if val not in self.headers:
                self.headers.append(val)
        self.headers.remove('measurements')

    def updateHeaderTable(self):
        # Dodaj wartosci wierszow dla tabeli naglowka proby
        self.p_naglowek.blockSignals(True)
        for i, val in enumerate(self.headers):
            if self.sample.wypiszMetadana(val):
                komorka = QTableWidgetItem(str(self.sample.wypiszMetadana(val)))
            else:
                komorka = QTableWidgetItem('---')

            if val == 'Length':
                komorka.setFlags(Qt.ItemIsSelectable | Qt.ItemIsEnabled)

            self.p_naglowek.setItem(i, 0, komorka)
        self.p_naglowek.blockSignals(False)

    def wykonaj_odczytanie(self):
        # spisz cala probe z edytowanych tabel i zapisz ja do zmiennej globalnej
        # daneW
        self.odczytaj_dane(origin='org')
        self.newName = self.org_sample.KeyCode()
        self.hide()

    def przerysuj_wykres(self):
        self.odczytaj_dane()
        self.updateHeaderTable()
        self.chartData = [
                     [],  # X axis - years
                     [],  # Y axis - measurements
                    ]

        self.chartData[1] = self.sample.wypiszPomiary()
        self.chartData[0] = range(1, self.sample.Length() + 1)

        self.qmc.axes.clear()
        # draw sample curve
        self.qmc.axes.plot(self.chartData[0], self.chartData[1])

        # draw selected years
        Xpoints = []
        Ypoints = []
        self.selected = []
        j = 0
        for i in range(self.sample.Length()):
            if i == 11:
                j += 1
            item = self.p_dane.item(j, i-(j*10))
            if item.isSelected():
                Xpoints.append(i+1)
                self.selected.append(i)
                Ypoints.append(int(item.text()))
        self.qmc.axes.plot(Xpoints, Ypoints, 'ro')

        # formatting of support lines
        self.qmc.axes.xaxis.set_major_locator(MultipleLocator(10))
        self.qmc.axes.xaxis.set_minor_locator(MultipleLocator(2))

        # Prepare axis dimensions
        Xmax = 40
        if self.sample.Length() > 40:
            Xmax = self.sample.Length() + 2
        Ymax = max(self.sample.wypiszPomiary()) + 30
        self.qmc.axes.axis([0, Xmax, 0, Ymax])

        self.qmc.axes.xaxis.grid(
            True,
            'minor',
            linewidth=0.4,
            ls='-',
            color='0.20')
        self.qmc.axes.xaxis.grid(
            True,
            'major',
            linewidth=1,
            ls='-',
            color='0.80')
        self.qmc.axes.tick_params(axis='both', which='major', labelsize=10)
        self.qmc.axes.set_axisbelow(True)
        # ustawienie obszaru wykresu
        self.qmc.axes.set_position([0.03, 0.05, 0.96, 0.94])
        self.qmc.draw()

    def odczytaj_dane(self, origin='copy'):
        """update sample - read all metadata and measurements which could be
            altered by user. If origin of sample has to be modified use any
            string in origin
        """
        if origin == 'copy':
            target = self.sample
        else:
            target = self.org_sample

        for i, head in enumerate(self.headers):
            ins = unicode(self.p_naglowek.item(i, 0).text())
            if head not in ['Length'] and ins not in ['---', 0]:
                target.wpiszMetadana(
                    head,
                    ins
                    )

        rowNum = self.p_dane.rowCount()
        colNum = self.p_dane.columnCount()
        measurements = []
        check = 'ok'
        redraw = 0
        for w in range(rowNum):
            for k in range(colNum):
                item = self.p_dane.item(w, k)
                try:
                    if int(item.text()) != 0 and str(item.text()).isdigit:
                        measurements.append(int(item.text()))
                        # if after notOk we find number there is something wrong
                        # and we neef to reread measuremnts to tabel
                        if check == 'notOk':
                            redraw = 1
                except:
                    check = 'notOk'

        target.uaktualnijPom(measurements)
        if redraw == 1:
            self.wpisz_pomiary()

    def dodaj_wartosc(self):
        kk = 0
        self.ile_wierszy = self.p_dane.rowCount()
        self.ile_kol = self.p_dane.columnCount()
        for wiersz in range(self.ile_wierszy):
            for kolumna in range(self.ile_kol):
                item = self.p_dane.item(wiersz, kolumna)
                if self.p_dane.isItemSelected(item) == True and kk == 0:
                    kk = 1
                    text, ok = QInputDialog.getText(
                        self, 'Value', 'inser value: ')
                    if ok:
                        measurements = self.sample.wypiszPomiary()
                        measurements.insert((wiersz*10)+kolumna, int(text))
                        self.sample.uaktualnijPom(measurements)
        self.wpisz_pomiary()
        self.przerysuj_wykres()

    def usun_wartosc(self):
        sel = self.selected[:]
        sel.reverse()
        for s in sel:
            self.sample.usunOstatniPomiar(position=int(s))
        self.wpisz_pomiary()
        self.przerysuj_wykres()

    def wpisz_pomiary(self):
        # przygotowanie labelek poziomych dla tablicy z pomiarami
        self.w = (len(self.sample.wypiszPomiary()))/10
        if (len(self.sample.wypiszPomiary())) % 10 > 0:
            self.w += 1
        i = 0
        self.ww = []
        while i < self.w:
            self.ww.append(str(i*10))
            i += 1
        self.p_dane.blockSignals(True)
        self.p_dane.clear()
        self.p_dane.setRowCount(self.w)
        self.p_dane.setVerticalHeaderLabels(self.ww)
        self.p_dane.setAlternatingRowColors(True)
        self.p_dane.setSortingEnabled(False)

        # Dodajemy wartosci pomiarow
        i = 0
        j = 0
        k = 0
        measurements = self.sample.wypiszPomiary()
        while k < len(measurements):
            if j == 10:
                j = 0
                i += 1
            komorka = QTableWidgetItem(str(measurements[k]))
            self.p_dane.setItem(i, j, komorka)
            j += 1
            k += 1
        # uzupelniamy pozostale pola pustymi wartosciami
        while j % 10 != 0:
            komorka = QTableWidgetItem("")
            self.p_dane.setItem(i, j, komorka)
            j += 1
        self.p_dane.resizeColumnsToContents()
        self.p_dane.blockSignals(False)

    def podziel_wartosc(self):
        if len(self.selected) != 1:
            pass
        else:
            measurements = self.sample.wypiszPomiary()
            val = measurements[self.selected[0]]
            self.divideWindow = okno_podzialu(val)
            self.divideWindow.exec_()
            measurements.pop(self.selected[0])
            self.sample.uaktualnijPom(measurements)
            self.sample.dodajPomiar(self.divideWindow.val1,
                                    position=self.selected[0])
            self.sample.dodajPomiar(self.divideWindow.val0,
                                    position=self.selected[0])

            self.wpisz_pomiary()
            self.przerysuj_wykres()

    def polacz_wartosc(self):
        measurements = self.sample.wypiszPomiary()
        i = len(measurements) - 1
        selectionRange = []
        newMeasurements = []
        while i > -1:
            if i in self.selected:
                selectionRange.append(measurements[i])
            elif i not in self.selected:
                if len(selectionRange) > 0:
                    newMeasurements.append(sum(selectionRange))
                    selectionRange = []
                newMeasurements.append(measurements[i])
            i -= 1
        if len(selectionRange) > 0:
            newMeasurements.append(selectionRange)
        newMeasurements.reverse()
        self.sample.uaktualnijPom(newMeasurements)

        self.wpisz_pomiary()
        self.przerysuj_wykres()

    def schowaj(self):
        self.odczytaj_dane()
        self.hide()

    def edytowana_kom_nagl(self):
        # neccessary to maintain user specific date, otherwise it will be
        # shuflled
        row = self.p_naglowek.currentRow()
        self.p_naglowek.blockSignals(True)
        if self.headers[row] in ['DateBegin', 'DateEnd']:
            head_temp = self.headers[row]
            if head_temp == "DateBegin":
                self.sample.ustawDateBegin(
                    int(self.p_naglowek.item(row, 0).text()))
                it = QTableWidgetItem(str(self.sample.DateEnd()))
                self.p_naglowek.setItem(self.headers.index("DateEnd"), 0, it)
            if head_temp == "DateEnd":
                self.sample.ustawDateEnd(
                    int(self.p_naglowek.item(row, 0).text()))
                it = QTableWidgetItem(str(self.sample.DateBegin()))
                self.p_naglowek.setItem(self.headers.index("DateBegin"), 0, it)
        self.p_naglowek.blockSignals(False)
        self.przerysuj_wykres()
Beispiel #13
0
class WordsWidget(QWidget):

    def __init__(self):
        super(WordsWidget, self).__init__()

        self.page_number = 1
        self.row = 20

        self.create_layout()
        self.create_connections()
        self.load_words()

    def create_layout(self):
        self.words_table = QTableWidget(0, 8)

        self.dictionaries = Dictionary.objects.all()
        self.dict_combo = QComboBox()
        self.dict_combo.addItems([d.name for d in self.dictionaries])

        self.refresh_button = QPushButton("Refresh")
        self.next_button = QPushButton("Next")
        self.previous_button = QPushButton("Previous")

        hbox = QHBoxLayout()
        hbox.addWidget(QLabel('Dictionary'))
        hbox.addWidget(self.dict_combo)
        hbox.addWidget(self.refresh_button)
        hbox.addStretch()
        hbox.addWidget(self.previous_button)
        hbox.addWidget(self.next_button)

        vbox = QVBoxLayout()
        vbox.addWidget(self.words_table)
        vbox.addLayout(hbox)

        self.setLayout(vbox)

    def create_connections(self):
        self.connect(self.dict_combo, SIGNAL("currentIndexChanged(int)"),
                     self.change_dictionary)
        self.connect(self.refresh_button, SIGNAL("clicked()"), self.load_words)
        self.connect(self.next_button, SIGNAL("clicked()"), self.next_words)
        self.connect(self.previous_button, SIGNAL("clicked()"),
                     self.previous_words)
        self.connect(self.words_table,
                     SIGNAL("itemDoubleClicked(QTableWidgetItem*)"),
                     self.edit_word)

    def next_words(self):
        self.page_number += 1
        self.load_words()

    def previous_words(self):
        self.page_number -= 1
        self.load_words()

    def change_dictionary(self):
        self.page_number = 1
        self.load_words()

    def load_words(self):
        dictionary = self.dictionaries[self.dict_combo.currentIndex()]
        words = Word.objects.filter(dictionary=dictionary.abbrev) \
                    .order_by('original')
        paginator = Paginator(words, self.row, allow_empty_first_page=True)
        try:
            page_obj = paginator.page(self.page_number)
        except InvalidPage:
            return

        self.words_table.clear()
        self.words_table.setRowCount(len(page_obj.object_list))

        self.words_table.setHorizontalHeaderLabels(['Original',
                                                    'Translation',
                                                    'Parts of Speech',
                                                    'Phoneme',
                                                    'Synonyms',
                                                    'Antonyms',
                                                    'Added at',
                                                    'Status'
                                                   ])

        for i, word in enumerate(page_obj.object_list):
            for j, cell in enumerate([word.original, word.translation, word.pos,
                                     word.phoneme, word.synonyms, word.antonyms,
                                     word.added_at.strftime("%Y-%m-%d %H:%M"),
                                      "exported" if word.exported else "new"]):
                item = QTableWidgetItem(cell)
                item.setFlags(Qt.ItemIsSelectable | Qt.ItemIsEnabled)
                self.words_table.setItem(i, j, item)

        self.next_button.setEnabled(page_obj.has_next())
        self.previous_button.setEnabled(page_obj.has_previous())

    def edit_word(self):
        index = self.words_table.currentRow() + self.row * (self.page_number - 1)
        dictionary = self.dictionaries[self.dict_combo.currentIndex()]
        word = Word.objects.filter(dictionary=dictionary.abbrev) \
                    .order_by('original')[index]
        edit_word = AddWordWidget(self)
        edit_word.word = word
        edit_word.words_widget = self
        edit_word.show()
Beispiel #14
0
class Search(QDialog):

    def __init__(self, parent=None):
        super(Search, self).__init__(parent=parent)

        self.create_layout()
        self.create_connections()

    def create_layout(self):
        label = QLabel("Your query:")
        self.query_edit = QLineEdit()
        self.find_button = QPushButton("Search")
        self.get_button = QPushButton("Get selected books")

        self.table = QTableWidget()
        self.table.setColumnCount(3)
        self.table.setHorizontalHeaderLabels(['Select', 'Name', 'URL'])

        hbox = QHBoxLayout()
        hbox.addWidget(label)
        hbox.addWidget(self.query_edit)
        hbox.addWidget(self.find_button)

        vbox = QVBoxLayout()
        vbox.addLayout(hbox)
        vbox.addWidget(self.table)
        vbox.addWidget(self.get_button)

        self.setLayout(vbox)

    def create_connections(self):
        self.connect(self.find_button, SIGNAL('clicked()'), self.render_result)
        self.connect(self.get_button, SIGNAL('clicked()'), self.download)

    def render_result(self):
        query = self.query_edit.text()
        if not query:
            return

        self.feed = FeedBooks()
        response = self.feed.search(str(query))

        if not response:
            QMessageBox.critical(self, 'Error', 'Could not get any result')
            return

        self.table.clear()
        self.table.setHorizontalHeaderLabels(['Select', 'Title', 'URL'])
        self.table.setRowCount(len(response[1]))

        for i, name in enumerate(zip(response[1], response[3])):
            item = QTableWidgetItem(1)
            item.data(Qt.CheckStateRole)
            item.setCheckState(Qt.Checked)
            self.table.setItem(i, 0, item)

            for j in range(2):
                item = QTableWidgetItem(name[j])
                item.setFlags(Qt.ItemIsEnabled | Qt.ItemIsSelectable)
                self.table.setItem(i, j+1, item)

        self.table.resizeColumnsToContents()

    def download(self):
        rows = self.table.rowCount()

        progress = QProgressDialog("Downloading books...", "Abort download", 0,
                                   rows, self)
        progress.setWindowModality(Qt.WindowModal)
        progress.show()

        to_download = [self.table.item(row, 2).text() for row in range(rows) if
                       self.table.item(row, 0).checkState() == Qt.Checked]

        for i, book in enumerate(to_download):
            progress.setValue(i)
            book_id = self.feed.download(str(book))
            if not book_id:
                QMessageBox.critical(self, 'Error', 'Could not download the '
                                     'book')
            elif book_id != -1:
                book = Book(book_id)
                insert_library(book)

            if progress.wasCanceled():
                break

        progress.setValue(rows)
        progress.close()
        self.parent().library.refresh()
Beispiel #15
0
class AccessFrontend(ScrollArea):
    COMPONENT = 'access'
    LABEL = tr('Access to services')
    REQUIREMENTS = ('access',)
    STYLE_ALLOW = "color: white; background-color: white; border: none;"
    STYLE_DISALLOW = "color: lightGray; background-color: lightGray; border: none;"
    ICON = ':/icons/Acl.png'

    def __init__(self, client, parent):
        ScrollArea.__init__(self)

        self.client = client
        self.mainwindow = parent
        self._modified = False
        self.__disabled = False
        self.net_object = QNetObject.getInitializedInstance(self.client)
        if EDENWALL:
            self.vpn_object = QOpenVpnObject.getInstance()

        self.setupWidgets()
        self.getConfigs()
        self.getNetworks()
        # vpn_config is used to check if the VPN config changed or not
        self.vpn_config = self.getVPNConfig()
        self.fillTable()
        self.net_object.registerCallbacks(self.validateNetCfg, self.updateWithNetCfg)
        if EDENWALL:
            self.vpn_object.registerCallbacks(self.validateVpnCfg, self.updateWithVpnCfg)


        self.mainwindow.addToInfoArea(tr("Access interface enabled"))

    @staticmethod
    def get_calls():
        """
        services called at startup (self.mainwindow.init_call)
        """
        return (('access', 'getConfig'),)

    def getVPNConfig(self):
        if not EDENWALL:
            return None
        vpn = self.vpn_object.getCfg()
        if not vpn:
            return None
        return (vpn.enabled, vpn.protocol, vpn.port, vpn.client_network)

    def setupWidgets(self):
        layout = QVBoxLayout(self)
        self.setLayout(layout)

        self.icon = QIcon()
        ALLOW = QPixmap(":/icons-20/status_on.png")
        self.icon.addPixmap(ALLOW, QIcon.Normal, QIcon.On)
        DISALLOW = QPixmap(":/icons-20/status_off.png")
        self.icon.addPixmap(DISALLOW, QIcon.Normal, QIcon.Off)

        title = QLabel(u"<H1>%s</H1>" % tr("Access to services"))
        layout.addWidget(title)

        self.table = QTableWidget()
        self.table.setSelectionMode(QAbstractItemView.NoSelection)

        layout.addWidget(self.table)

    def isModified(self):
        return self._modified

    def isValid(self):
        if self.__disabled:
            return True
        valid, errmsg = self.access_cfg.isValidWithMsg()
        if not valid:
            self.error_message = errmsg
        return valid

    def setModified(self, modif=True):
        if self.__disabled:
            self._modified = False
            return
        self._modified = modif
        if modif:
            self.mainwindow.setModified(self, True)

    def resetConf(self):
        if self.__disabled:
            return
        self.getConfigs()
        self.getNetworks()
        self.fillTable()

    def __disable(self, reason):
        if self.__disabled:
            return
        self.__disabled = True
        self.mainwindow.addToInfoArea(
            tr("The Access to services interface is disabled."),
            COLOR_ERROR)
        self.mainwindow.addToInfoArea(reason, COLOR_ERROR)
        self.close()
        raise NuConfModuleDisabled(reason)

    def getConfigs(self):
        if self.__disabled:
            return
        try:
            data = self.mainwindow.init_call('access', 'getConfig')
        except RpcdError:
            self.__disable(tr("Could not get Access to services configuration."))
            return
        if data is None:
            self.access_cfg = AccessConf.defaultConf()
        else:
            self.access_cfg = AccessConf.deserialize(data)

    def _getNetworks(self, netcfg):
        return [(interface.system_name, network)
            for interface, network in netcfg.iterKnownNetworks()]

    def getNetworks(self):
        if self.__disabled:
            return
        netcfg = QNetObject.getInstance().netcfg
        if netcfg is None:
            self.networks = ()
            self.mainwindow.addToInfoArea(
                tr("The access interface could not load the network configuration"),
                COLOR_ERROR
                )
            return
        # list of (interface, network) where interface (str) is the system
        # name, and network (IPy.IP object) is the network address
        # (eg. IP('192.168.0.0/24')
        self.networks = self._getNetworks(netcfg)
        self.networks += list(self.access_cfg.custom_networks)
        self.networks.sort()

    def fillTable(self):
        if self.__disabled:
            self.table.clear()
            return
        services = list(self.access_cfg.permissions)
        self.table.clear()

        # (interface (str), network (IPy), ip version (int)) => row (int)
        # Don't use (interface, network) because of a bug in IPy < 0.70:
        # IP('0.0.0.0/0') and IP('::/0') are considered as equal
        self.net_to_row = {}

        component_to_name = ComponentToName()
        self.table.setSortingEnabled(False)
        self.table.setRowCount(len(self.networks))
        self.setVerticalHeaders()
        self.table.setColumnCount(len(services))
        self.table.setHorizontalHeaderLabels([component_to_name.display_name(service) for service in services])
        self.table.horizontalHeader().setResizeMode(QHeaderView.ResizeToContents)

        for irow, interface_network in enumerate(self.networks):
            self.setRow(interface_network, irow)
            for icol, service in enumerate(self.access_cfg.permissions):
                allow = interface_network in self.access_cfg.permissions[service]
                self.createService(irow, icol, service, interface_network, allow)

    def getRow(self, interface_network, pop=False):
        interface, network = interface_network
        key = (interface, network, network.version())
        if pop:
            return self.net_to_row.pop(key)
        else:
            return self.net_to_row[key]

    def setRow(self, interface_network, row):
        interface, network = interface_network
        key = (interface, network, network.version())
        self.net_to_row[key] = row

    def setVerticalHeaders(self):
        labels = [self.netLabel(interface, network)
            for interface, network in self.networks]
        self.table.setVerticalHeaderLabels(labels)

    def saveConf(self, message):
        data = self.access_cfg.serialize(downgrade=True)
        self.client.call("access", 'setConfig', message, data)
        self.setModified(False)

    def changeService(self, col, service, interface_network):
        row = self.getRow(interface_network)
        self.setModified(True)
        button = self.table.cellWidget(row, col)
        if button.isChecked():
            self.access_cfg.permissions[service].add(interface_network)
            button.setStyleSheet(self.STYLE_ALLOW)
        else:
            try:
                self.access_cfg.permissions[service].remove(interface_network)
            except KeyError:
                pass
            button.setStyleSheet(self.STYLE_DISALLOW)

    def appendNetwork(self, interface_network, close_all_ports=False):
        row = self.table.rowCount()
        self.table.insertRow(row)
        self.networks.append(interface_network)
        self.setRow(interface_network, row)
        interface, network = interface_network
        for icol, service in enumerate(self.access_cfg.permissions):
            if (not close_all_ports) \
            and (service in OPEN_BY_DEFAULT) \
            and (network not in CLOSED_NETWORKS):
                allow = True
                self.access_cfg.permissions[service].add(interface_network)
            else:
                allow = interface_network in self.access_cfg.permissions[service]
            self.createService(row, icol, service, interface_network, allow)

    def removeNetwork(self, interface_network):
        index = self.getRow(interface_network, pop=True)
        self.table.removeRow(index)
        network = self.networks[index]
        for service, networks in self.access_cfg.permissions.iteritems():
            try:
                networks.remove(network)
            except KeyError:
                pass
        del self.networks[index]
        for key, row in self.net_to_row.iteritems():
            if row > index:
                self.net_to_row[key] -= 1

    # callback used to update the table on network modification
    def updateWithNetCfg(self, deleted_nets, added_nets):
        if not(deleted_nets or added_nets):
            return

        # delete
        for key in deleted_nets:
            self.removeNetwork(key)

        # add new
        for interface_network in added_nets:
            self.appendNetwork(interface_network, close_all_ports=True)

        # modify and update
        self.setVerticalHeaders()
        self.setModified(True)

    def createService(self, row, col, service, interface_network, allow):
        button = QPushButton(self.icon, u'')
        button.setCheckable(True)
        button.setFlat(True)
        button.setAutoFillBackground(True)
        if allow:
            style = self.STYLE_ALLOW
        else:
            style = self.STYLE_DISALLOW
        button.setStyleSheet(style)
        button.setFocusPolicy(Qt.NoFocus)

        self.mainwindow.writeAccessNeeded(button)
        self.connect(button, SIGNAL('clicked()'), partial(self.changeService, col, service, interface_network))
        self.table.setCellWidget(row, col, button)

        # with PyQt 4.4.2, table.setCellWidget(button) changes
        # the button's state (bug fixed in PyQt 4.4.4)
        button.setChecked(allow)

    # callback used when the networks are modified
    def validateNetCfg(self):
        netcfg = QNetObject.getInstance().netcfg

        previous_nets = set(self.networks) - set(self.access_cfg.custom_networks)
        new_networks = self._getNetworks(netcfg)
        new_networks = set(new_networks)

        not_in_both = previous_nets ^ new_networks
        deleted_nets = not_in_both & previous_nets
        added_nets = not_in_both & new_networks
        return True, deleted_nets, added_nets

    def validateVpnCfg(self):
        """
        always accept modifications
        """
        return True, self.getVPNConfig()

    def updateWithVpnCfg(self, new_config):
        if self.vpn_config == new_config:
            return

        # anything changed, access has to reapply the new config
        self.setModified()
        self.vpn_config = new_config

        # get old/new key
        config = self.vpn_object.getCfg()
        if self.access_cfg.custom_networks:
            old_key = self.access_cfg.custom_networks[0]
        else:
            old_key = None
        if config.enabled:
            network = config.client_network
            try:
                network = IP(network)
            except ValueError:
                # ignore invalid network: vpn check will raise an error
                return
            new_key = (OPENVPN_INTERFACE, network)
        else:
            new_key = None

        # no change? exit
        if old_key == new_key:
            return

        # create/delete vpn custom network
        if old_key:
            self.removeNetwork(old_key)
        if new_key:
            self.access_cfg.custom_networks = [new_key]
            self.appendNetwork(new_key)
        else:
            self.access_cfg.custom_networks = []
        self.setVerticalHeaders()

    def netLabel(self, interface, network):
        """
        return label for network
        """
        interface_label = interface
        network_label = str(network)
        network = IP(network)
        netcfg = QNetObject.getInstance().netcfg
        try:
            interface = netcfg.getInterfaceBySystemName(interface)
            interface_label = interface.user_label

            network = netcfg.getNet(network)
            network_label = network.displayName()
        except NoMatch:
            pass
        return tr("%s: %s") % (interface_label, network_label)
class OWdabam_height_profile(OWWidget):
    name = "DABAM Height Profile"
    id = "dabam_height_profile"
    description = "Calculation of mirror surface error profile"
    icon = "icons/dabam.png"
    author = "Luca Rebuffi"
    maintainer_email = "[email protected]; [email protected]"
    priority = 6
    category = ""
    keywords = ["dabam_height_profile"]

    outputs = [{"name": "PreProcessor_Data",
                "type": ShadowPreProcessorData,
                "doc": "PreProcessor Data",
                "id": "PreProcessor_Data"}]

    want_main_area = 1
    want_control_area = 1

    MAX_WIDTH = 1320
    MAX_HEIGHT = 700

    IMAGE_WIDTH = 860
    IMAGE_HEIGHT = 645

    CONTROL_AREA_WIDTH = 405
    TABS_AREA_HEIGHT = 618

    xx = None
    yy = None
    zz = None

    entry_number = Setting(1)

    shape=Setting(0)
    slope_error_from = Setting(0.0)
    slope_error_to = Setting(1.5)
    dimension_y_from = Setting(0.0)
    dimension_y_to = Setting(200.0)

    use_undetrended = Setting(0)

    step_x = Setting(1.0)
    dimension_x = Setting(10.0)

    center_y = Setting(1)
    modify_y = Setting(0)
    new_length = Setting(200.0)
    filler_value = Setting(0.0)

    scale_factor_y = Setting(1.0)
    renormalize_y = Setting(1)
    error_type_y = Setting(0)
    rms_y = Setting(0.9)

    dabam_profile_index = Setting(1)

    heigth_profile_file_name = Setting('mirror.dat')

    tab=[]

    def __init__(self):
        super().__init__()

        self.runaction = widget.OWAction("Calculate Height Profile", self)
        self.runaction.triggered.connect(self.calculate_heigth_profile_ni)
        self.addAction(self.runaction)

        self.runaction = widget.OWAction("Generate Height Profile File", self)
        self.runaction.triggered.connect(self.generate_heigth_profile_file_ni)
        self.addAction(self.runaction)

        geom = QApplication.desktop().availableGeometry()
        self.setGeometry(QRect(round(geom.width() * 0.05),
                               round(geom.height() * 0.05),
                               round(min(geom.width() * 0.98, self.MAX_WIDTH)),
                               round(min(geom.height() * 0.95, self.MAX_HEIGHT))))

        self.setMaximumHeight(self.geometry().height())
        self.setMaximumWidth(self.geometry().width())

        # DABAM INITIALIZATION
        self.server = dabam.dabam()
        self.server.set_input_silent(True)

        gui.separator(self.controlArea)

        button_box = oasysgui.widgetBox(self.controlArea, "", addSpace=False, orientation="horizontal")

        button = gui.button(button_box, self, "Calculate Height\nProfile", callback=self.calculate_heigth_profile)
        button.setFixedHeight(45)

        button = gui.button(button_box, self, "Generate Height\nProfile File", callback=self.generate_heigth_profile_file)
        font = QFont(button.font())
        font.setBold(True)
        button.setFont(font)
        palette = QPalette(button.palette())  # make a copy of the palette
        palette.setColor(QPalette.ButtonText, QColor('Dark Blue'))
        button.setPalette(palette)  # assign new palette
        button.setFixedHeight(45)
        button.setFixedWidth(150)

        button = gui.button(button_box, self, "Reset Fields", callback=self.call_reset_settings)
        font = QFont(button.font())
        font.setItalic(True)
        button.setFont(font)
        palette = QPalette(button.palette())  # make a copy of the palette
        palette.setColor(QPalette.ButtonText, QColor('Dark Red'))
        button.setPalette(palette)  # assign new palette
        button.setFixedHeight(45)

        gui.separator(self.controlArea)

        tabs_setting = gui.tabWidget(self.controlArea)
        tabs_setting.setFixedHeight(self.TABS_AREA_HEIGHT)
        tabs_setting.setFixedWidth(self.CONTROL_AREA_WIDTH-5)

        tab_input = oasysgui.createTabPage(tabs_setting, "DABAM Search Setting")
        tab_gener = oasysgui.createTabPage(tabs_setting, "DABAM Generation Setting")
        tab_out = oasysgui.createTabPage(tabs_setting, "Output")

        manual_box = oasysgui.widgetBox(tab_input, "Manual Entry", addSpace=True, orientation="vertical")

        oasysgui.lineEdit(manual_box, self, "entry_number", "Entry Number",
                           labelWidth=300, valueType=int, orientation="horizontal")

        gui.separator(manual_box)

        button = gui.button(manual_box, self, "Retrieve Profile", callback=self.retrieve_profile)
        button.setFixedHeight(35)
        button.setFixedWidth(self.CONTROL_AREA_WIDTH-35)

        input_box = oasysgui.widgetBox(tab_input, "Search Parameters", addSpace=True, orientation="vertical")

        gui.comboBox(input_box, self, "shape", label="Mirror Shape", labelWidth=300,
                     items=["All", "Plane", "Cylindrical", "Elliptical", "Toroidal", "Spherical"],
                     sendSelectedValue=False, orientation="horizontal")

        gui.separator(input_box)
        
        input_box_1 = oasysgui.widgetBox(input_box, "", addSpace=True, orientation="horizontal")

        oasysgui.lineEdit(input_box_1, self, "slope_error_from", "Slope Error From (" + u"\u03BC" + "rad)",
                           labelWidth=150, valueType=float, orientation="horizontal")
        oasysgui.lineEdit(input_box_1, self, "slope_error_to", "To (" + u"\u03BC" + "rad)",
                           labelWidth=60, valueType=float, orientation="horizontal")

        input_box_2 = oasysgui.widgetBox(input_box, "", addSpace=True, orientation="horizontal")

        self.le_dimension_y_from = oasysgui.lineEdit(input_box_2, self, "dimension_y_from", "Mirror Length From",
                           labelWidth=150, valueType=float, orientation="horizontal")
        self.le_dimension_y_to = oasysgui.lineEdit(input_box_2, self, "dimension_y_to", "To",
                           labelWidth=60, valueType=float, orientation="horizontal")

        table_box = oasysgui.widgetBox(tab_input, "Search Results", addSpace=True, orientation="vertical", height=290)

        self.overlay_search = Overlay(table_box, self.search_profiles)
        self.overlay_search.hide()

        button = gui.button(input_box, self, "Search", callback=self.overlay_search.show)
        button.setFixedHeight(35)
        button.setFixedWidth(self.CONTROL_AREA_WIDTH-35)

        gui.comboBox(table_box, self, "use_undetrended", label="Use Undetrended Profile", labelWidth=300,
                     items=["No", "Yes"], callback=self.table_item_clicked, sendSelectedValue=False, orientation="horizontal")

        gui.separator(table_box)

        self.scrollarea = QScrollArea()
        self.scrollarea.setMinimumWidth(self.CONTROL_AREA_WIDTH-35)

        table_box.layout().addWidget(self.scrollarea, alignment=Qt.AlignHCenter)

        self.table = QTableWidget(1, 5)
        self.table.setAlternatingRowColors(True)
        self.table.horizontalHeader().setResizeMode(QHeaderView.Fixed)
        self.table.verticalHeader().setVisible(False)

        self.table.setColumnWidth(0, 40)
        self.table.setColumnWidth(1, 70)
        self.table.setColumnWidth(2, 70)
        self.table.setColumnWidth(3, 85)
        self.table.setColumnWidth(4, 80)

        self.table.resizeRowsToContents()
        self.table.setSelectionBehavior(QAbstractItemView.SelectRows)
        self.table.itemClicked.connect(self.table_item_clicked)

        self.scrollarea.setWidget(self.table)
        self.scrollarea.setWidgetResizable(1)

        output_profile_box = oasysgui.widgetBox(tab_gener, "Surface Generation Parameters", addSpace=True, orientation="vertical", height=270)

        self.le_dimension_x = oasysgui.lineEdit(output_profile_box, self, "dimension_x", "Width",
                           labelWidth=300, valueType=float, orientation="horizontal")
        self.le_step_x = oasysgui.lineEdit(output_profile_box, self, "step_x", "Step Width",
                           labelWidth=300, valueType=float, orientation="horizontal")

        gui.comboBox(output_profile_box, self, "center_y", label="Center Profile in the middle of O.E.", labelWidth=300,
                     items=["No", "Yes"], sendSelectedValue=False, orientation="horizontal")

        gui.comboBox(output_profile_box, self, "modify_y", label="Modify Length?", labelWidth=240,
                     items=["No", "Rescale to new length", "Fit to new length (fill or cut)"], callback=self.set_ModifyY, sendSelectedValue=False, orientation="horizontal")

        self.modify_box_1 = oasysgui.widgetBox(output_profile_box, "", addSpace=False, orientation="vertical", height=50)

        self.modify_box_2 = oasysgui.widgetBox(output_profile_box, "", addSpace=False, orientation="vertical", height=50)
        oasysgui.lineEdit(self.modify_box_2, self, "scale_factor_y", "Scale Factor", labelWidth=300, valueType=float, orientation="horizontal")

        self.modify_box_3 = oasysgui.widgetBox(output_profile_box, "", addSpace=False, orientation="vertical", height=50)
        self.le_new_length = oasysgui.lineEdit(self.modify_box_3, self, "new_length", "New Length", labelWidth=300, valueType=float, orientation="horizontal")
        oasysgui.lineEdit(self.modify_box_3, self, "filler_value", "Filler Value (if new length > profile length) [nm]", labelWidth=300, valueType=float, orientation="horizontal")

        self.set_ModifyY()

        gui.comboBox(output_profile_box, self, "renormalize_y", label="Renormalize Length Profile to different RMS", labelWidth=300,
                     items=["No", "Yes"], callback=self.set_RenormalizeY, sendSelectedValue=False, orientation="horizontal")

        self.output_profile_box_1 = oasysgui.widgetBox(output_profile_box, "", addSpace=True, orientation="vertical")

        gui.comboBox(self.output_profile_box_1, self, "error_type_y", label="Normalization to", labelWidth=270,
                     items=["Figure Error (nm)", "Slope Error (" + u"\u03BC" + "rad)"],
                     sendSelectedValue=False, orientation="horizontal")

        oasysgui.lineEdit(self.output_profile_box_1, self, "rms_y", "Rms Value",
                           labelWidth=300, valueType=float, orientation="horizontal")

        self.set_RenormalizeY()

        output_box = oasysgui.widgetBox(tab_gener, "Outputs", addSpace=True, orientation="vertical")

        select_file_box = oasysgui.widgetBox(output_box, "", addSpace=True, orientation="horizontal")

        self.le_heigth_profile_file_name = oasysgui.lineEdit(select_file_box, self, "heigth_profile_file_name", "Output File Name",
                                                        labelWidth=120, valueType=str, orientation="horizontal")

        gui.button(select_file_box, self, "...", callback=self.selectFile)

        self.shadow_output = QTextEdit()
        self.shadow_output.setReadOnly(True)

        out_box = oasysgui.widgetBox(tab_out, "System Output", addSpace=True, orientation="horizontal", height=500)
        out_box.layout().addWidget(self.shadow_output)

        gui.rubber(self.controlArea)

        self.initializeTabs()

        gui.rubber(self.mainArea)

        self.overlay_search.raise_()

    def resizeEvent(self, event):
        self.overlay_search.resize(self.CONTROL_AREA_WIDTH - 15, 290)
        event.accept()

    def after_change_workspace_units(self):
        self.si_to_user_units = 1e2 / self.workspace_units_to_cm

        self.horHeaders = ["Entry", "Shape", "Length\n[" + self.workspace_units_label + "]", "Heights St.Dev.\n[nm]",  "Slopes St.Dev.\n[" + u"\u03BC" + "rad]"]
        self.table.setHorizontalHeaderLabels(self.horHeaders)
        self.plot_canvas[0].setGraphXLabel("Y [" + self.workspace_units_label + "]")
        self.plot_canvas[1].setGraphXLabel("Y [" + self.workspace_units_label + "]")
        self.axis.set_xlabel("X [" + self.workspace_units_label + "]")
        self.axis.set_ylabel("Y [" + self.workspace_units_label + "]")

        label = self.le_dimension_y_from.parent().layout().itemAt(0).widget()
        label.setText(label.text() + " [" + self.workspace_units_label + "]")
        label = self.le_dimension_y_to.parent().layout().itemAt(0).widget()
        label.setText(label.text() + " [" + self.workspace_units_label + "]")
        label = self.le_dimension_x.parent().layout().itemAt(0).widget()
        label.setText(label.text() + " [" + self.workspace_units_label + "]")
        label = self.le_step_x.parent().layout().itemAt(0).widget()
        label.setText(label.text() + " [" + self.workspace_units_label + "]")
        label = self.le_new_length.parent().layout().itemAt(0).widget()
        label.setText(label.text() + " [" + self.workspace_units_label + "]")

    def initializeTabs(self):
        self.tabs = gui.tabWidget(self.mainArea)

        self.tab = [gui.createTabPage(self.tabs, "Info"),
                    gui.createTabPage(self.tabs, "Heights Profile"),
                    gui.createTabPage(self.tabs, "Slopes Profile"),
                    gui.createTabPage(self.tabs, "PSD Heights"),
                    gui.createTabPage(self.tabs, "CSD Heights"),
                    gui.createTabPage(self.tabs, "ACF"),
                    gui.createTabPage(self.tabs, "Generated 2D Profile"),
        ]

        for tab in self.tab:
            tab.setFixedHeight(self.IMAGE_HEIGHT)
            tab.setFixedWidth(self.IMAGE_WIDTH)

        self.plot_canvas = [None, None, None, None, None, None]

        self.plot_canvas[0] = PlotWindow(roi=False, control=False, position=True, plugins=False)
        self.plot_canvas[0].setDefaultPlotLines(True)
        self.plot_canvas[0].setActiveCurveColor(color='darkblue')
        self.plot_canvas[0].setGraphYLabel("Z [nm]")
        self.plot_canvas[0].setGraphTitle("Heights Profile")
        self.plot_canvas[0].setDrawModeEnabled(True, 'rectangle')
        self.plot_canvas[0].setZoomModeEnabled(True)

        self.plot_canvas[1] = PlotWindow(roi=False, control=False, position=True, plugins=False)
        self.plot_canvas[1].setDefaultPlotLines(True)
        self.plot_canvas[1].setActiveCurveColor(color='darkblue')
        self.plot_canvas[1].setGraphYLabel("Zp [$\mu$rad]")
        self.plot_canvas[1].setGraphTitle("Slopes Profile")
        self.plot_canvas[1].setDrawModeEnabled(True, 'rectangle')
        self.plot_canvas[1].setZoomModeEnabled(True)

        self.plot_canvas[2] = PlotWindow(roi=False, control=False, position=True, plugins=False)
        self.plot_canvas[2].setDefaultPlotLines(True)
        self.plot_canvas[2].setActiveCurveColor(color='darkblue')
        self.plot_canvas[2].setGraphXLabel("f [m^-1]")
        self.plot_canvas[2].setGraphYLabel("PSD [m^3]")
        self.plot_canvas[2].setGraphTitle("Power Spectral Density of Heights Profile")
        self.plot_canvas[2].setDrawModeEnabled(True, 'rectangle')
        self.plot_canvas[2].setZoomModeEnabled(True)
        self.plot_canvas[2].setXAxisLogarithmic(True)
        self.plot_canvas[2].setYAxisLogarithmic(True)

        self.plot_canvas[3] = PlotWindow(roi=False, control=False, position=True, plugins=False)
        self.plot_canvas[3].setDefaultPlotLines(True)
        self.plot_canvas[3].setActiveCurveColor(color='darkblue')
        self.plot_canvas[3].setGraphXLabel("f [m^-1]")
        self.plot_canvas[3].setGraphYLabel("CSD [m^3]")
        self.plot_canvas[3].setGraphTitle("Cumulative Spectral Density of Heights Profile")
        self.plot_canvas[3].setDrawModeEnabled(True, 'rectangle')
        self.plot_canvas[3].setZoomModeEnabled(True)
        self.plot_canvas[3].setXAxisLogarithmic(True)

        self.plot_canvas[4] = PlotWindow(roi=False, control=False, position=True, plugins=False)
        self.plot_canvas[4].setDefaultPlotLines(True)
        self.plot_canvas[4].setActiveCurveColor(color='darkblue')
        self.plot_canvas[4].setGraphXLabel("Length [m]")
        self.plot_canvas[4].setGraphYLabel("ACF")
        self.plot_canvas[4].setGraphTitle("Autocovariance Function of Heights Profile")
        self.plot_canvas[4].setDrawModeEnabled(True, 'rectangle')
        self.plot_canvas[4].setZoomModeEnabled(True)

        self.figure = Figure(figsize=(self.IMAGE_HEIGHT, self.IMAGE_HEIGHT)) # QUADRATA!
        self.figure.patch.set_facecolor('white')

        self.axis = self.figure.add_subplot(111, projection='3d')

        self.axis.set_zlabel("Z [nm]")

        self.plot_canvas[5] = FigureCanvasQTAgg(self.figure)

        self.profileInfo = QTextEdit()
        self.profileInfo.setReadOnly(True)
        self.profileInfo.setMinimumHeight(self.IMAGE_HEIGHT-5)
        self.profileInfo.setMaximumHeight(self.IMAGE_HEIGHT-5)
        self.profileInfo.setMinimumWidth(310)
        self.profileInfo.setMaximumWidth(310)

        profile_box = oasysgui.widgetBox(self.tab[0], "", addSpace=True, orientation="horizontal", height = self.IMAGE_HEIGHT, width=320)
        profile_box.layout().addWidget(self.profileInfo)

        for index in range(0, 6):
            self.tab[index+1].layout().addWidget(self.plot_canvas[index])

        self.tabs.setCurrentIndex(1)

    def plot_dabam_graph(self, plot_canvas_index, curve_name, x_values, y_values, xtitle, ytitle, color='blue', replace=True):
        self.plot_canvas[plot_canvas_index].addCurve(x_values, y_values, curve_name, symbol='', color=color, replace=replace) #'+', '^', ','
        self.plot_canvas[plot_canvas_index].setGraphXLabel(xtitle)
        self.plot_canvas[plot_canvas_index].setGraphYLabel(ytitle)
        self.plot_canvas[plot_canvas_index].replot()

    def set_ModifyY(self):
        self.modify_box_1.setVisible(self.modify_y == 0)
        self.modify_box_2.setVisible(self.modify_y == 1)
        self.modify_box_3.setVisible(self.modify_y == 2)

    def set_RenormalizeY(self):
        self.output_profile_box_1.setVisible(self.renormalize_y==1)

    def table_item_clicked(self):
        if self.table.selectionModel().hasSelection():
            if not self.table.rowCount() == 0:
                if not self.table.item(0, 0) is None:
                    row = self.table.selectionModel().selectedRows()[0].row()
                    self.entry_number = int(self.table.item(row, 0).text())

                    self.retrieve_profile()

    def retrieve_profile(self):
        try:
            if self.entry_number is None or self.entry_number <= 0:
                raise Exception("Entry number should be a strictly positive integer number")

            self.server.load(self.entry_number)
            self.profileInfo.setText(self.server.info_profiles())
            self.plot_canvas[0].setGraphTitle(
                "Heights Profile. St.Dev.=%.3f nm" % (self.server.stdev_profile_heights() * 1e9))
            self.plot_canvas[1].setGraphTitle(
                "Slopes Profile. St.Dev.=%.3f $\mu$rad" % (self.server.stdev_profile_slopes() * 1e6))
            if self.use_undetrended == 0:
                self.plot_dabam_graph(0, "heights_profile", self.si_to_user_units * self.server.y,
                                      1e9 * self.server.zHeights, "Y [" + self.workspace_units_label + "]", "Z [nm]")
                self.plot_dabam_graph(1, "slopes_profile", self.si_to_user_units * self.server.y, 1e6 * self.server.zSlopes,
                                      "Y [" + self.workspace_units_label + "]", "Zp [$\mu$rad]")
            else:
                self.plot_dabam_graph(0, "heights_profile", self.si_to_user_units * self.server.y,
                                      1e9 * self.server.zHeightsUndetrended, "Y [" + self.workspace_units_label + "]",
                                      "Z [nm]")
                self.plot_dabam_graph(1, "slopes_profile", self.si_to_user_units * self.server.y,
                                      1e6 * self.server.zSlopesUndetrended, "Y [" + self.workspace_units_label + "]",
                                      "Zp [$\mu$rad]")
            y = self.server.f ** (self.server.powerlaw["hgt_pendent"]) * 10 ** self.server.powerlaw["hgt_shift"]
            i0 = self.server.powerlaw["index_from"]
            i1 = self.server.powerlaw["index_to"]
            beta = -self.server.powerlaw["hgt_pendent"]
            self.plot_canvas[2].setGraphTitle(
                "Power Spectral Density of Heights Profile (beta=%.2f,Df=%.2f)" % (beta, (5 - beta) / 2))
            self.plot_dabam_graph(2, "psd_heights_2", self.server.f, self.server.psdHeights, "f [m^-1]", "PSD [m^3]")
            self.plot_dabam_graph(2, "psd_heights_1", self.server.f, y, "f [m^-1]", "PSD [m^3]", color='green',
                                  replace=False)
            self.plot_dabam_graph(2, "psd_heights_3", self.server.f[i0:i1], y[i0:i1], "f [m^-1]", "PSD [m^3]", color='red',
                                  replace=False)
            self.plot_dabam_graph(3, "csd", self.server.f, self.server.csd_heights(), "f [m^-1]", "CSD [m^3]")
            c1, c2, c3 = dabam.autocorrelationfunction(self.server.y, self.server.zHeights)
            self.plot_canvas[4].setGraphTitle(
                "Autocovariance Function of Heights Profile.\nAutocorrelation Length (ACF=0.5)=%.3f m" % (c3))
            self.plot_dabam_graph(4, "acf", c1[0:-1], c2, "Length [m]", "Heights Autocovariance")
            # surface error removal
            if not self.zz is None and not self.yy is None and not self.xx is None:
                self.xx = None
                self.yy = None
                self.zz = None
                self.axis.set_title("")
                self.axis.clear()
                self.plot_canvas[5].draw()

            if (self.tabs.currentIndex()==6): self.tabs.setCurrentIndex(1)

        except Exception as exception:
            QMessageBox.critical(self, "Error",
                                 exception.args[0],
                                 QMessageBox.Ok)

            #raise exception


    def search_profiles(self):
        try:
            self.table.itemClicked.disconnect(self.table_item_clicked)
            self.table.clear()

            row_count = self.table.rowCount()
            for n in range(0, row_count):
                self.table.removeRow(0)

            self.table.setHorizontalHeaderLabels(self.horHeaders)

            profiles = dabam.dabam_summary_dictionary(surface=self.get_dabam_shape(),
                                                      slp_err_from=self.slope_error_from*1e-6,
                                                      slp_err_to=self.slope_error_to*1e-6,
                                                      length_from=self.dimension_y_from / self.si_to_user_units,
                                                      length_to=self.dimension_y_to / self.si_to_user_units)

            for index in range(0, len(profiles)):
                self.table.insertRow(0)

            for index in range(0, len(profiles)):
                table_item = QTableWidgetItem(str(profiles[index]["entry"]))
                table_item.setTextAlignment(Qt.AlignCenter)
                self.table.setItem(index, 0, table_item)
                table_item = QTableWidgetItem(str(profiles[index]["surface"]))
                table_item.setTextAlignment(Qt.AlignLeft)
                self.table.setItem(index, 1, table_item)
                table_item = QTableWidgetItem(str(numpy.round(profiles[index]["length"]*self.si_to_user_units, 3)))
                table_item.setTextAlignment(Qt.AlignRight)
                self.table.setItem(index, 2, table_item)
                table_item = QTableWidgetItem(str(numpy.round(profiles[index]["hgt_err"]*1e9, 3)))
                table_item.setTextAlignment(Qt.AlignRight)
                self.table.setItem(index, 3, table_item)
                table_item = QTableWidgetItem(str(numpy.round(profiles[index]["slp_err"]*1e6, 3)))
                table_item.setTextAlignment(Qt.AlignRight)
                self.table.setItem(index, 4, table_item)

            self.table.setHorizontalHeaderLabels(self.horHeaders)
            self.table.resizeRowsToContents()
            self.table.setSelectionBehavior(QAbstractItemView.SelectRows)

            self.table.itemClicked.connect(self.table_item_clicked)

            self.overlay_search.hide()

        except Exception as exception:
            self.overlay_search.hide()

            QMessageBox.critical(self, "Error",
                                 exception.args[0],
                                 QMessageBox.Ok)

    def get_dabam_shape(self):
        if self.shape == 0: return None
        elif self.shape == 1: return "plane"
        elif self.shape == 2: return "cylindrical"
        elif self.shape == 3: return "elliptical"
        elif self.shape == 4: return "toroidal"
        elif self.shape == 5: return "spherical"

    def calculate_heigth_profile_ni(self):
        self.calculate_heigth_profile(not_interactive_mode=True)

    def calculate_heigth_profile(self, not_interactive_mode=False):
        try:
            if self.server.y is None: raise Exception("No Profile Selected")

            sys.stdout = EmittingStream(textWritten=self.writeStdOut)

            self.check_fields()

            combination = "EF"

            if self.modify_y == 2:
                profile_1D_y_x_temp = self.si_to_user_units * self.server.y
                if self.use_undetrended == 0: profile_1D_y_y_temp = self.si_to_user_units * self.server.zHeights
                else: profile_1D_y_y_temp = self.si_to_user_units * self.server.zHeightsUndetrended

                first_coord = profile_1D_y_x_temp[0]
                second_coord  = profile_1D_y_x_temp[1]
                last_coord = profile_1D_y_x_temp[-1]
                step = numpy.abs(second_coord - first_coord)
                length = numpy.abs(last_coord - first_coord)
                n_points_old = len(profile_1D_y_x_temp)

                if self.new_length > length:
                    difference = self.new_length - length

                    n_added_points = int(difference/step)
                    if difference % step == 0:
                        n_added_points += 1
                    if n_added_points % 2 != 0:
                        n_added_points += 1


                    profile_1D_y_x = numpy.arange(n_added_points + n_points_old) * step
                    profile_1D_y_y = numpy.ones(n_added_points + n_points_old) * self.filler_value * 1e-9 * self.si_to_user_units
                    profile_1D_y_y[int(n_added_points/2) : n_points_old + int(n_added_points/2)] = profile_1D_y_y_temp
                elif self.new_length < length:
                    difference = length - self.new_length

                    n_removed_points = int(difference/step)
                    if difference % step == 0:
                        n_removed_points -= 1
                    if n_removed_points % 2 != 0:
                        n_removed_points -= 1

                    if n_removed_points >= 2:
                        profile_1D_y_x = profile_1D_y_x_temp[0 : (n_points_old - n_removed_points)]
                        profile_1D_y_y = profile_1D_y_y_temp[(int(n_removed_points/2) - 1) : (n_points_old - int(n_removed_points/2) - 1)]

                    else:
                        profile_1D_y_x = profile_1D_y_x_temp
                        profile_1D_y_y = profile_1D_y_y_temp
                else:
                    profile_1D_y_x = profile_1D_y_x_temp
                    profile_1D_y_y = profile_1D_y_y_temp

            else:
                if self.modify_y == 0:
                    profile_1D_y_x = self.si_to_user_units * self.server.y
                elif self.modify_y == 1:
                    profile_1D_y_x = self.si_to_user_units * self.server.y * self.scale_factor_y

                if self.use_undetrended == 0: profile_1D_y_y = self.si_to_user_units * self.server.zHeights
                else: profile_1D_y_y = self.si_to_user_units * self.server.zHeightsUndetrended


            if self.center_y:
                first_coord = profile_1D_y_x[0]
                last_coord = profile_1D_y_x[-1]
                length = numpy.abs(last_coord - first_coord)

                profile_1D_y_x_temp = numpy.linspace(-length/2, length/2, len(profile_1D_y_x))

                profile_1D_y_x = profile_1D_y_x_temp

            if self.renormalize_y == 0:
                rms_y = None
            else:
                if self.error_type_y == profiles_simulation.FIGURE_ERROR:
                    rms_y = self.si_to_user_units * self.rms_y * 1e-9   # from nm to user units
                else:
                    rms_y = self.rms_y * 1e-6 # from urad to rad

            xx, yy, zz = profiles_simulation.simulate_profile_2D(combination = combination,
                                                                 error_type_l = self.error_type_y,
                                                                 rms_l = rms_y,
                                                                 x_l = profile_1D_y_x,
                                                                 y_l = profile_1D_y_y,
                                                                 mirror_width = self.dimension_x,
                                                                 step_w = self.step_x,
                                                                 rms_w = 0.0)

            self.xx = xx
            self.yy = yy
            self.zz = zz # in user units

            self.axis.clear()

            x_to_plot, y_to_plot = numpy.meshgrid(xx, yy)
            z_to_plot = zz * 1e9 / self.si_to_user_units #nm

            self.axis.plot_surface(x_to_plot, y_to_plot, z_to_plot,
                                   rstride=1, cstride=1, cmap=cm.autumn, linewidth=0.5, antialiased=True)

            sloperms = profiles_simulation.slopes(zz.T, xx, yy, return_only_rms=1)

            title = ' Slope error rms in X direction: %f $\mu$rad' % (sloperms[0]*1e6) + '\n' + \
                    ' Slope error rms in Y direction: %f $\mu$rad' % (sloperms[1]*1e6)

            self.axis.set_xlabel("X [" + self.workspace_units_label + "]")
            self.axis.set_ylabel("Y [" + self.workspace_units_label + "]")
            self.axis.set_zlabel("Z [nm]")

            self.axis.set_title(title)
            self.axis.mouse_init()

            if not not_interactive_mode:
                try:
                    self.plot_canvas[5].draw()
                except:
                    pass

                self.tabs.setCurrentIndex(6)

                QMessageBox.information(self, "QMessageBox.information()",
                                        "Height Profile calculated: if the result is satisfactory,\nclick \'Generate Height Profile File\' to complete the operation ",
                                        QMessageBox.Ok)
        except Exception as exception:
            QMessageBox.critical(self, "Error",
                                 exception.args[0],
                                 QMessageBox.Ok)

            #raise exception

    def generate_heigth_profile_file_ni(self):
        self.generate_heigth_profile_file(not_interactive_mode=True)

    def generate_heigth_profile_file(self, not_interactive_mode=False):
        if not self.zz is None and not self.yy is None and not self.xx is None:
            try:
                congruence.checkDir(self.heigth_profile_file_name)

                sys.stdout = EmittingStream(textWritten=self.writeStdOut)

                ST.write_shadow_surface(self.zz, self.xx, self.yy, outFile=congruence.checkFileName(self.heigth_profile_file_name))
                if not not_interactive_mode:
                    QMessageBox.information(self, "QMessageBox.information()",
                                            "Height Profile file " + self.heigth_profile_file_name + " written on disk",
                                            QMessageBox.Ok)
                if self.modify_y == 0:
                    dimension_y = self.si_to_user_units * (self.server.y[-1] - self.server.y[0])
                if self.modify_y == 1:
                    dimension_y = self.si_to_user_units * (self.server.y[-1] - self.server.y[0]) * self.scale_factor_y
                elif self.modify_y == 2:
                    dimension_y = self.new_length

                self.send("PreProcessor_Data", ShadowPreProcessorData(error_profile_data_file=self.heigth_profile_file_name,
                                                                      error_profile_x_dim=self.dimension_x,
                                                                      error_profile_y_dim=dimension_y))
            except Exception as exception:
                QMessageBox.critical(self, "Error",
                                     exception.args[0],
                                     QMessageBox.Ok)

    def call_reset_settings(self):
        if ConfirmDialog.confirmed(parent=self, message="Confirm Reset of the Fields?"):
            try:
                self.resetSettings()
            except:
                pass

    def check_fields(self):
        self.dimension_x = congruence.checkStrictlyPositiveNumber(self.dimension_x, "Dimension X")
        self.step_x = congruence.checkStrictlyPositiveNumber(self.step_x, "Step X")
        if self.step_x > self.dimension_x/2:
            raise Exception("Step Width should be smaller than or equal to Width/2")
        if self.modify_y == 1:
            self.scale_factor_y = congruence.checkStrictlyPositiveNumber(self.scale_factor_y, "Scale Factor")
        elif self.modify_y == 2:
            self.new_length = congruence.checkStrictlyPositiveNumber(self.new_length, "New Length")

        if self.renormalize_y == 1:
            self.rms_y = congruence.checkPositiveNumber(self.rms_y, "Rms Y")

        congruence.checkDir(self.heigth_profile_file_name)

    def writeStdOut(self, text):
        cursor = self.shadow_output.textCursor()
        cursor.movePosition(QTextCursor.End)
        cursor.insertText(text)
        self.shadow_output.setTextCursor(cursor)
        self.shadow_output.ensureCursorVisible()

    def selectFile(self):
        self.le_heigth_profile_file_name.setText(oasysgui.selectFileFromDialog(self, self.heigth_profile_file_name, "Select Output File", file_extension_filter="Data Files (*.dat)"))
class ViewTableForm(QWidget):
    def __init__(self, visualization, parent_widget = None):
        QWidget.__init__(self, parent_widget)
        self.inGui = False

        self.widgetLayout = QVBoxLayout(self)

        self.tableWidget = QTableWidget(self)
        self.tableWidget.setObjectName("tableWidget")
        size = QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
        self.tableWidget.setSizePolicy(size)
        self.widgetLayout.addWidget(self.tableWidget)

        self.tabIcon = QIcon(":/Images/Images/map.png")

        self.tabLabel = visualization.table_name

        self.load_table(visualization = visualization)

    def load_table(self, visualization, limit = 10000):

        storage = StorageFactory().get_storage(
                       type = '%s_storage'%visualization.output_type,
                       storage_location = visualization.storage_location)
        table_data = storage.load_table(
                                table_name = visualization.table_name)

        try:
            primary_keys = visualization.indicators[0].primary_keys
        except:
            primary_keys = []

        keys = primary_keys + [key for key in table_data.keys()
                                   if key not in primary_keys]
        num_rows = min(len(table_data[keys[0]]), limit)
        num_cols = len(keys)

        self.tableWidget.clear()
        self.tableWidget.setColumnCount(num_cols)
        self.tableWidget.setRowCount(num_rows)

        j = 0
        for key in keys:
            col = QTableWidgetItem()
            col.setText(QString(key))
            self.tableWidget.setHorizontalHeaderItem(j,col)
            j += 1

        self.tableWidget.resizeColumnsToContents()

        order = sorted(enumerate(table_data[keys[0]]), lambda (i,v),(j,v2): int(v*100)-int(v2*100))

        for i, (idx,v) in enumerate(order):
            row = QTableWidgetItem()
            self.tableWidget.setVerticalHeaderItem(i,row)
            j = 0
            for key in keys:
                item = QTableWidgetItem()
                item.setText(QString(str(table_data[key][idx])))
                self.tableWidget.setItem(i,j,item)
                j += 1
            if i > limit:
                msg = 'The table %s has been truncated to %i rows because of memory limitations.'%(visualization.table_name,limit)
                detailed_msg = '<qt>To view the full results, open the following file:<br><br><small>%s</small></qt>'%visualization.get_file_path()
                MessageBox.warning(mainwindow = self,
                                  text = msg,
                                  detailed_text = detailed_msg)
                break


        #self.tableWidget.resizeRowsToContents()
    def removeElement(self):
        return True
Beispiel #18
0
class typicalWindow(QtGui.QMainWindow):
	def __init__(self, parent=None):
		super(typicalWindow, self).__init__(parent)
		# QtGui.QMainWindow.__init__(self)
		
		#window
		self.setWindowTitle(winTitle)
		self.central_widget=QWidget(self)
		self.setCentralWidget(self.central_widget)
		self.masterLayout=QGridLayout(self.central_widget)
		self.masterLayout.setAlignment(QtCore.Qt.AlignTop)
		
		#mainlayout
		self.vertical_order_layout=QtGui.QBoxLayout(2)
		self.vertical_order_layout.setAlignment(QtCore.Qt.AlignTop | QtCore.Qt.AlignVCenter)
		self.masterLayout.addLayout(self.vertical_order_layout, 0,0,1,1)
		
		self.topDivideLayout=QGridLayout()
		self.botDivideLayout=QGridLayout()
		self.upper_layout=QGridLayout()
		
		
		self.topDivideLayout.addLayout(self.upper_layout, 0,0,1,1)
		
		self.lower_layout=QGridLayout()
		self.lower_layout.setAlignment(QtCore.Qt.AlignTop)
		self.botDivideLayout.addLayout(self.lower_layout, 0,0,1,1)
		
		self.midLayout=QGridLayout()
		self.midLayout.setAlignment(QtCore.Qt.AlignTop)
		
		self.base_layout=QGridLayout()
		self.base_layout.setAlignment(QtCore.Qt.AlignTop)
		self.botDivideLayout.addLayout(self.base_layout, 4,0,1,1)
		
		#sshFile=open(os.path.join(__location__, styleSheetFile+".stylesheet"), 'r')
		#self.styleData=sshFile.read()
		#sshFile.close
		
		#self.setStyleSheet(self.styleData)
		self.top=QtGui.QFrame(self)
		self.top.setFrameShape(QtGui.QFrame.StyledPanel)
		self.top.setLayout(self.topDivideLayout)
		
		self.bottom=QtGui.QFrame(self)
		self.bottom.setFrameShape(QtGui.QFrame.StyledPanel)
		self.bottom.setLayout(self.botDivideLayout)
		
		self.splitPlane=QtGui.QSplitter(QtCore.Qt.Vertical)
		self.splitPlane.addWidget(self.top)
		self.splitPlane.addWidget(self.bottom)
		self.splitPlane.setSizes([650, 650])
		self.vertical_order_layout.addWidget(self.splitPlane)
		
		#layouts
		self.window_layer_00=QGridLayout()
		self.upper_layout.addLayout(self.window_layer_00, 0,0,1,1)
		
		self.window_layer_01=QGridLayout()
		self.upper_layout.addLayout(self.window_layer_01,1,0,1,1)
		
		self.window_layer_02=QGridLayout()
		self.upper_layout.addLayout(self.window_layer_02, 2,0,1,1)
		
		self.window_layer_03=QGridLayout()
		self.upper_layout.addLayout(self.window_layer_03,3,0,1,1)
		
		self.window_layer_04=QGridLayout()
		self.upper_layout.addLayout(self.window_layer_04, 4,0,1,1)
		
		self.window_layer_05=QGridLayout()
		self.upper_layout.addLayout(self.window_layer_05,5,0,1,1)
		
		self.window_layer_06=QGridLayout()
		self.midLayout.addLayout(self.window_layer_06, 6,0,1,1)
		
		self.frame_layout=QGridLayout()
		self.frame_layout.setAlignment(QtCore.Qt.AlignTop)
		self.lower_layout.addLayout(self.frame_layout, 0,0,1,1)
		
		
		self.frameWidget=QtGui.QGridLayout()
		self.frameWidget.setContentsMargins(5,10,5,10)
		self.frameOverride=QtGui.QFrame()
		self.frameOverride.setStyleSheet("background-color: #434343; border-style: solid; border-width: 2px; border-color:#434343;border-radius:8px;")
		self.frameOverride.setFixedHeight(100)
		self.frame_layout.addLayout(self.frameWidget, 0,0,1,1)
		self.frame_layout.addWidget(self.frameOverride, 0,0,1,1)
		
		
		self.frame_title_layout=QGridLayout()
		self.frameWidget.addLayout(self.frame_title_layout, 0,0,1,1)
		self.frame_radio_layout=QGridLayout()
		self.frameWidget.addLayout(self.frame_radio_layout, 1,0,1,1)
		self.frame_btn_layout=QGridLayout()
		self.frameWidget.addLayout(self.frame_btn_layout, 2,0,1,1)
	
		self.btm_btn_layout=QtGui.QGridLayout()
		self.btm_btn_layout.setAlignment(QtCore.Qt.AlignTop)
		self.btm_btn_layout.setContentsMargins(5,10,5,10)	
		self.wbFrame=QtGui.QFrame()
		self.wbFrame.setStyleSheet("background-color: #434343; border-style: solid; border-width: 2px; border-color:#434343;border-radius:8px;")
		self.btm_over_layout=QtGui.QGridLayout()
		self.btm_over_layout.setAlignment(QtCore.Qt.AlignTop)
		self.btm_over_layout.addLayout(self.btm_btn_layout, 0,0,1,1)
		self.btm_over_layout.addWidget(self.wbFrame, 0,0,1,1)
		
		self.pkt_layout= QGridLayout()
		self.pkt_layout.setAlignment(QtCore.Qt.AlignTop)
		self.pkt_widget=QGridLayout()
		self.pkt_widget.setContentsMargins(5,5,5,5)	
		self.pkt_frame=QFrame()
		self.pkt_frame.setMinimumWidth(650)
		self.pkt_frame.setStyleSheet("background-color: #434343; border-style: solid; border-width: 2px; border-color:#434343;border-radius:8px;")
		self.base_layout.addLayout(self.pkt_layout, 0,0,1,1)
		
		self.wndw_layer_pkt=QtGui.QGridLayout()
		self.wndw_layer_pkt.setAlignment(QtCore.Qt.AlignTop)
		self.pkt_widget.addLayout(self.wndw_layer_pkt, 0,0,1,1)
		
		self.park_btn_pkt=QtGui.QBoxLayout(2)
		self.park_btn_pkt.setAlignment(QtCore.Qt.AlignTop)
		self.park_btn_pkt.setContentsMargins(5,2,5,8)
		self.wndw_layer_pkt.addLayout(self.park_btn_pkt, 0,0,1,1)
		self.park_frame=QtGui.QFrame()
		self.park_frame.setStyleSheet("background-color: #434343; border-style: solid; border-width: 2px; border-color:#434343;border-radius:8px;")
		
		
		#widgets
		self.drop_lbl_01=QLabel()
		self.drop_lbl_01.setAlignment(QtCore.Qt.AlignLeft | QtCore.Qt.AlignVCenter)
		self.drop_lbl_01.setStyleSheet("color: #b1b1b1; background-color: rgba(255,255,255,0);")
		self.window_layer_01.addWidget(self.drop_lbl_01, 0,0,1,1)
		
		self.drop_01=QComboBox()
		self.window_layer_01.addWidget(self.drop_01, 0,1,1,1)
		self.drop_01.addItems(prjFileName)
		self.drop_01.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
		self.drop_01.customContextMenuRequested.connect(self.onRightClick)

		# self.drop_01.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
		# self.connect(self.drop_lbl_01, SIGNAL("customContextMenuRequested(QPoint)"), self.onRightClick)
		
		self.drop_lbl_02=QLabel()
		self.drop_lbl_02.setAlignment(QtCore.Qt.AlignLeft | QtCore.Qt.AlignVCenter)
		self.drop_lbl_02.setStyleSheet("color: #b1b1b1; background-color: rgba(255,255,255,0);")
		self.window_layer_01.addWidget(self.drop_lbl_02, 0,2,1,1)
		
		self.drop_02=QComboBox()
		self.window_layer_01.addWidget(self.drop_02, 0,3,1,1)
		# QtCore.QObject.connect(self.drop_02, SIGNAL("currentIndexChanged(QString)"),
		# 						self.on_drop_01_changed)
		self.drop_02.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
		self.connect(self.drop_lbl_01, SIGNAL("customContextMenuRequested(QPoint)"), self.onRightClick)
		
		QtCore.QObject.connect(self.drop_01, SIGNAL("currentIndexChanged(QString)"),
								self.on_drop_01_changed)

		self.button_01=QPushButton("Set")
		self.button_01.setToolTip("set")
		self.connect(self.button_01, SIGNAL('clicked()'), self.listCreate)
		self.window_layer_01.addWidget(self.button_01, 0,4,1,1)
		
		self.button_02=QPushButton("Set2")
		self.button_02.setToolTip("set2")
		self.connect(self.button_02, SIGNAL('clicked()'), self.connectButton01)
		self.window_layer_01.addWidget(self.button_02, 0,5,1,1)

		self.drop_lbl_03=QLabel()
		self.drop_lbl_03.setAlignment(QtCore.Qt.AlignLeft | QtCore.Qt.AlignVCenter)
		self.drop_lbl_03.setStyleSheet("color: #b1b1b1; background-color: rgba(255,255,255,0);")
		self.window_layer_02.addWidget(self.drop_lbl_03, 0,0,1,1)
		
		self.drop_03=QComboBox()
		self.window_layer_02.addWidget(self.drop_03, 0,1,1,1)
		# QtCore.QObject.connect(self.drop_03, SIGNAL("currentIndexChanged(QString)"),
		# 						self.on_drop_01_changed)
		self.drop_03.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
		self.connect(self.drop_03, SIGNAL("customContextMenuRequested(QPoint)"), self.onRightClick)

		self.button_03=QPushButton("button_03")
		self.button_03.setToolTip("button_03")
		self.connect(self.button_03, SIGNAL('clicked()'), self.connectButton01)
		self.window_layer_02.addWidget(self.button_03, 0,2,1,1)
		
		self.button_04=QPushButton("button_04")
		self.button_04.setToolTip("button_04")
		self.connect(self.button_04, SIGNAL('clicked()'), self.connectButton01)
		self.window_layer_02.addWidget(self.button_04, 0,3,1,1)
		
		self.button_05=QPushButton("button_05")
		self.button_05.setToolTip("button_05")
		self.connect(self.button_05, SIGNAL('clicked()'), self.connectButton01)
		self.window_layer_02.addWidget(self.button_05, 0,4,1,1)

		self.drop_04=QComboBox()
		self.window_layer_04.addWidget(self.drop_04, 0,2,1,1)
		# QtCore.QObject.connect(self.drop_04, SIGNAL("currentIndexChanged(QString)"),
		# 						self.on_drop_01_changed)
		self.drop_04.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
		self.connect(self.drop_04, SIGNAL("customContextMenuRequested(QPoint)"), self.onRightClick)

		self.list_frame=QFrame()
		self.list_frame.setStyleSheet("color: rgb"+str(buttonColoursDict.get("red")))
		self.list_layout=QHBoxLayout()
		self.list_frame.setLayout(self.list_layout)
		
		self.drop_list_builder_05=QComboBox()
		self.drop_list_builder_05.addItems(get_a_play_list)
		QtCore.QObject.connect(self.drop_list_builder_05, SIGNAL("currentIndexChanged(QString)"),
								self.build)
		self.drop_list_builder_05.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
		self.connect(self.drop_list_builder_05, SIGNAL("customContextMenuRequested(QPoint)"), self.onRightClick)
		self.list_layout.addWidget(self.drop_list_builder_05)
		self.window_layer_04.addWidget(self.list_frame, 0,3,1,1)

		self.drop_list_06=QComboBox()
		# QtCore.QObject.connect(self.drop_list_06, SIGNAL("currentIndexChanged(QString)"),self.load)
		self.drop_list_06.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
		self.connect(self.drop_list_06, SIGNAL("customContextMenuRequested(QPoint)"), self.onRightClick)
		#if len(pres)<1:
			#self.drop_list_06.setEnabled(0)
		#else:
			#self.drop_list_06.setEnabled(1)
		self.drop_list_06.addItems(alist2)
		self.list_layout.addWidget(self.drop_list_06)
		
		self.type_list_drop=QComboBox()
		self.type_list_drop.addItems(typesOfStuffInList)
		QtCore.QObject.connect(self.type_list_drop, SIGNAL("currentIndexChanged(QString)"),
								self.on_drop_01_changed)
		self.window_layer_04.addWidget(self.type_list_drop, 0,5,1,1)
		
		self.button_06=QPushButton("button_06")
		self.button_06.setToolTip("button_06")
		self.connect(self.button_06, SIGNAL('clicked()'), self.connectButton01)	
		self.window_layer_04.addWidget(self.button_06, 0,6,0,1)



		headers = ('Name', 'Date', 'Path')

		self.listWidg = QTableWidget(1, 3)
		# self.listWidg.setSelectionMode(QtGui.QAbstractItemView.SingleSelection)
		# self.listWidg.setSelectionBehavior(QtGui.QAbstractItemView.SelectRows)
		# self.listWidg.cellPressed.connect(self.clicked)
		
		# self.listWidg=QTableWidget(0, 3)
		self.listWidg.setHorizontalHeaderLabels(headers)
		# tableWidget=self.listWidg
		self.listWidg.setEditTriggers(QAbstractItemView.DoubleClicked | QAbstractItemView.SelectedClicked)		
		col1, col2, col3= 240, 160, 500
		self.listWidg.setColumnWidth(0, col1)
		self.listWidg.setColumnWidth(1, col2)
		self.listWidg.setColumnWidth(2, col3)
		self.listWidg.setSelectionBehavior(QAbstractItemView.SelectRows)
		self.listWidg.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
		self.listWidg.customContextMenuRequested.connect(self.RightClick)
		# self.listWidg.setSelectionMode(QtGui.QAbstractItemView.MultiSelection)		
		self.connect(self.listWidg, SIGNAL("itemClicked(QTableWidgetItem *)"), self.clicked)
		self.connect(self.listWidg, SIGNAL("itemDoubleClicked(QTableWidgetItem *)"), self.dclicked)
		self.window_layer_05.addWidget(self.listWidg, 0,2,1,1)


		self.status_lbl=QLabel()
		self.status_lbl.setStyleSheet('background-color:transparent')
		self.status_lbl.setAlignment(QtCore.Qt.AlignCenter|QtCore.Qt.AlignVCenter)
		self.frame_title_layout.addWidget(self.status_lbl, 0,2,1,1)

		self.spaceHold=QLabel()
		self.spaceHold.setStyleSheet("color: #b1b1b1; background-color: rgba(255,255,255,0);")
		self.spaceHold.setAlignment(QtCore.Qt.AlignCenter|QtCore.Qt.AlignVCenter)
		self.frame_title_layout.addWidget(self.spaceHold, 0,0,1,1)


		self.checkbox=QCheckBox("add")
		self.checkbox.setStyleSheet("color: #b1b1b1; background-color: rgba(255,255,255,0);")
		# self.checkbox.setContentsMargins(5,0,0,0)
		self.checkbox.setChecked(1)
		self.frame_title_layout.addWidget(self.checkbox, 0,1,1,1)
		
		self.radiobox=QGridLayout()

		self.radio=QRadioButton("radio")
		self.radio.setStyleSheet("color: #b1b1b1; background-color: rgba(255,255,255,0);")
		self.radio.setChecked(1)
		self.radiobox.addWidget(self.radio, 0,0,1,1)
		
		
		self.newradio=QRadioButton("newradio")
		self.newradio.setStyleSheet("color: #b1b1b1; background-color: rgba(255,255,255,0);")
		self.radiobox.addWidget(self.newradio, 0,2,1,1)
		
		self.frame_len_layout=QGridLayout()
		self.frame_len_layout.setAlignment(QtCore.Qt.AlignCenter|QtCore.Qt.AlignVCenter)
		#self.frame_title_layout.addWidget(self.frame_len_layout, 1,3,1,1)
		
		self.spaceHold=QLabel()
		self.spaceHold.setStyleSheet("color: #b1b1b1; background-color: rgba(255,255,255,0);")
		self.spaceHold.setAlignment(QtCore.Qt.AlignCenter|QtCore.Qt.AlignVCenter)
		self.frame_title_layout.addWidget(self.spaceHold, 0,3,1,1)
		
		self.over=QRadioButton("over")
		self.over.setStyleSheet("color: #b1b1b1; background-color: rgba(255,255,255,0);")
		self.radiobox.addWidget(self.over, 1,1,1,1)
		
		self.head_lbl=QLabel("from")
		self.head_lbl.setStyleSheet("color: #b1b1b1; background-color: rgba(255,255,255,0);")
		self.radiobox.addWidget(self.head_lbl, 1, 4,1,1)
		
		self.head_field=QTextEdit("")
		self.head_field.setStyleSheet("color: #b1b1b1; background-color: rgba(255,255,255,0);")
		self.radiobox.addWidget(self.head_field, 1, 5,1,1)
		
		self.toe_lbl=QLabel("til")
		self.toe_lbl.setStyleSheet("color: #b1b1b1; background-color: rgba(255,255,255,0);")
		self.radiobox.addWidget(self.toe_lbl, 1, 6,1,1)
		
		self.toe_field=QTextEdit("")
		self.toe_field.setStyleSheet("color: #b1b1b1; background-color: rgba(255,255,255,0);")
		self.radiobox.addWidget(self.toe_field, 1, 7,1,1)
		
		self.fieldBox=QLineEdit()
		self.fieldBox.setVisible(0)
		self.fieldBox.setText(defaultText)
		
		self.play_in_rv_button=QPushButton("play in RV")
		self.connect(self.play_in_rv_button, SIGNAL('clicked()'), self.play_in_rv)
		self.frame_btn_layout.addWidget(self.play_in_rv_button, 0,0,0,1)
		
		self.look_btn=QPushButton("compare")
		self.connect(self.look_btn, SIGNAL('clicked()'), self.compare_in_rv)
		self.frame_btn_layout.addWidget(self.look_btn, 0,1, 0,1)


		# self.link_btn=QPushButton("link")
		# self.connect(self.link_btn, SIGNAL('clicked()'), self.play_in_rv)
		# self.frame_btn_layout.addWidget(self.link_btn, 0,2,1,1)
		
		self.create_btn=QPushButton("Publish")
		self.connect(self.create_btn, SIGNAL('clicked()'), self.pub_to_shotgun)
		self.frame_btn_layout.addWidget(self.create_btn, 0,3, 1,1)
		
		self.pocketTitle=QPushButton("title")
		self.pocketTitle.setObjectName('label')
		#self.pocketTitle.setStyleSheet("QPushButton#label{font-weight:500; color: rgb"str(buttonColorDict).get("yello"))+"; button-color: rgba(255,255,255,0); font-size: 10pt; border-width: 0px; font-style: bold;}")
		self.connect(self.pocketTitle, SIGNAL('clicked()'), self.send)
		self.connect(self.pocketTitle, SIGNAL('customContextMenuRequested(QPoint)'), lambda: self.send())
		self.park_btn_pkt.addWidget(self.pocketTitle)

		self.a_btn=QPushButton("a_btn")
		#self.a_btn.setStyleSheet("background-color: rgb"str(buttonColorDict).get("yello")))
		self.connect(self.a_btn, SIGNAL('clicked()'), self.play_in_rv)
		self.park_btn_pkt.addWidget(self.a_btn)
		
		self.card_menu=QMenu("card")
		self.card_menuBar=self.menuBar()
		self.card_menuBar.addMenu(self.card_menu)
		self.park_btn_pkt.addWidget(self.card_menuBar)
		buttonGrp.append(self.card_menuBar)
		
		self.card_btn=QToolButton()
		self.card_btn.setPopupMode(QToolButton.MenuButtonPopup)
		self.card_btn.setMenu(self.card_menu)
		self.card_special_btn=QPushButton("card special")
		self.connect(self.card_special_btn, SIGNAL('clicked()'), self.card_special_callup)
		action=QtGui.QWidgetAction(self.card_btn)
		action.setDefaultWidget(self.card_special_btn)
		self.card_btn.menu().addAction(action)

		self.B_card_btn=QToolButton()
		self.B_card_btn.setPopupMode(QToolButton.MenuButtonPopup)
		self.B_card_btn.setMenu(self.card_menu)
		self.B_card_special_btn=QPushButton("card special")
		self.connect(self.B_card_special_btn, SIGNAL('clicked()'),self.B_card_special_callup)
		action=QtGui.QWidgetAction(self.B_card_btn)
		action.setDefaultWidget(self.B_card_special_btn)
		self.B_card_btn.menu().addAction(action)

		self.start_window()

	def start_window(self):
		# self.connectButton01
		print PROJECT		
		print SCENE
		print SHOT
		index = self.drop_01.findText(SCENE, QtCore.Qt.MatchFixedString)
		self.drop_01.setCurrentIndex(index)
		self.get_scene()
		index2 = self.drop_02.findText(SHOT, QtCore.Qt.MatchFixedString)
		self.drop_02.setCurrentIndex(index2)		
		self.listCreate()

	def buttonToggle(self):
		get_a_layout=self.park_btn_pkt
		get_size=get_a_layout.getContentsMargine()
		if get_size==(0,0,0,0):
			self.setvisible()
		else:
			self.setinvisible()
			
	def setinvisible(self):
		for each in buttonGrp:
			each.setVisible(0)
		self.park_btn_pkt.setContentsMargine(0,0,0,0)
		
	def setvisible(self):
		for each in buttonGrp:
			each.setVisible(1)
		self.park_btn_pkt.setContentsMargine(5,8,5,8)


	def get_scene(self):
		scene=self.drop_01
		scene=scene.currentText()		
		getPath='/jobs/'+PROJECT+'/'+scene
		get_items=os.listdir(getPath)
		get_items=sorted(get_items)
		# get_items=set(get_items)
		# get_items=sorted(get_items)
		self.get_shot(get_items)

	def get_shot(self, get_items):
		getDropScene=self.drop_02
		getDropScene.clear()
		getDropScene.addItems(get_items)		

	def onRightClick(self):
		scene=self.drop_01
		scene=scene.currentText()		
		path='/jobs/'+PROJECT+'/'+scene+"/"
		self.launch_folder(path)


	def launch_folder(self, path):
		# command="xdg-open '%s'"%path
		command="dolphin '%s'"%path
		subprocess.Popen(command, stdout=subprocess.PIPE, shell=True)
		
	def on_drop_01_changed(self):
		self.get_scene()
		newcol1=self.listWidg.columnWidth(0)
		newcol2=self.listWidg.columnWidth(1)
		newcol3=self.listWidg.columnWidth(2)
		if newcol1==0:
			col1, col2, col3= 240, 160, 500
		else:
			col1, col2, col3= newcol1, newcol2, newcol3
		# findproject=self.drop_01
		self.listWidg.clear()
		self.status_lbl.clear()
		# self.listCreate()
		# self.makeList(listpath, newUser, self.listWidg, model, stat_lab, listtype)
		# self.drop_03.addItems(get_items)
		# model, countdata, listArray	=self.get_listStuff()	
		# if self.on_drop_01=="item1":
		# 	buildListPath=pathList.get("listpathtype").replace(getUser, newUser)
		# 	self.makeList(listpath, newUser, self.listWidg, model, stat_lab, listtype)
		# elif self.on_drop_01=="item2":
		# 	buildListPath=pathList.get("listpathtype2").replace(getUser, newUser)
		# 	self.makeList(listpath, newUser, self.listWidg, model, stat_lab, listtype)
			
	def clicked(self):
		print "hi"
		
	def dclicked(self):
		print "hello"
		
	def get_listStuff(self):
		listArray=self.listWidg
		countdata=listArray.rowCount()
		model=listArray.model()
		return model, countdata, listArray
		
	def getListWidgetData(self):
		model, countdata, listArray	=self.get_listStuff()	
		dataInListWidget=[]
		for row in range(model.rowCount()):
			dataInListWidget.append([])
			for column in range(model.columnCount()):
				index = model.index(row, column)
				dataInListWidget[row].append(str(model.data(index).toString()))
		return dataInListWidget, countdata
		
	def grab_folder_items(self):
		directory=rvFolder
		getstuff=os.listdir(directory)
		getUser=M_USER
		(dataInListWidget, countdata)=self.getListWidgetData()
		# self.listWidg.setRowCount(0)
		# self.listWidg.setColumnCount(0)
		try:
			getFiles=[os.path.join(directory, o) for o in os.listdir(directory) if os.path.isdir(os.path.join(directory, o))]
			pass
		except:
			print "nothing found"
			return
		getFile=[(each) for each in getFiles if getpwuid(stat(each).st_uid).pw_name==getUser]
		getFiles.sort(key=lambda x: os.path.getmtime(x))
		fileDict=[]
		for each in getFiles:
			statbuf=os.stat(each)
			timeFormat=time.strftime('%m/%d/%Y', time.gmtime(os.path.getctime(each)))
			getAccTime=time.ctime(os.path.getmtime(each))
			if "  " in str(getAccTime):
				getAccTime=getAccTime.split("  ")
				getAccTime=getAccTime[1].split(" ")[1]
			else:
				getAccTime=getAccTime.split("  ")[3]
			timeFormat=timeFormat+"  "+getAccTime
			makeDict=(each, timeFormat)
			fileDict.append(makeDict)
		count=len(fileDict)
		fileDict=reversed(fileDict)
		dictItems=fileDict
		return fileDict, count


	def listCreate(self):
		self.listWidg.setColumnCount(3)
		fileDict, count=self.grab_folder_items()		
		self.listWidg.setRowCount(count)
		for row, item in enumerate(fileDict):
			key=item[0].split('/')[-1]
			path=item[0]
			value=item[1]
			self.listWidg.setItem(row, 0, QTableWidgetItem(key))
			self.listWidg.setItem(row, 1, QTableWidgetItem(value))
			self.listWidg.setItem(row, 2, QTableWidgetItem(path))



	def is_listWid_item_selected(self):
		listW=self.listWidg
		(dataInListWidget, countdata)=self.getListWidgetData()
		get_string_id=[]
		for index in xrange(countdata):
			get=listW.item(index, 0).isSelected()
			if get==True:
				getObj=listW.item(index, 2).text()
				getObj=str(getObj)
				get_string_id.append(getObj)
			else:
				get=listW.item(index, 1).isSelected()
				if get==True:
					getObj=listW.item(index, 2).text()
					getObj=str(getObj)
					get_string_id.append(getObj)
				else:
					get=listW.item(index, 2).isSelected()
					if get==True:
						getObj=listW.item(index, 2).text()
						getObj=str(getObj)
						get_string_id.append(getObj)
		return get_string_id
		
	def build(self):
		list_build=self.drop_list_builder_05
		list_build_function=list_build.currentText()
		selected_in_list=self.is_listWid_item_selected()
		allthePaths=('//', '//')
		allthePathsDic={"firstPath":'//', "secondPath":'//'}
		#drop_list_builder_05
		getlisttype=self.type_list_drop
		listtype=getlisttype.currentText()
		if selected_in_list>1:
			getItems=[(each) for each in selected_in_list]
			nameToSave=' '.join(getItems)
			if listtype=="firstPath":
				suffixAppend="first"
				path=allthePathsDic.get("firstPath")
			if listtype=="secondPath":
				suffixAppend="second"
				path=allthePathsDic.get("secondPath")
		compareBucket=[]
		getitems=[(suffixAppend+":"+each.split("/")[-1]) for each in selected_in_list]
		name_to_save=' '.join(getitems)
		if list_build_function==get_a_play_list[1]:
			prompt="name of list:"
			getcomment=self.makeBody(prompt)
			if getComment==None:
				print "needs name"
				return
			else:
				pass
			getComment=getComment.replace(' ', '_')
			#shotList=suffixAppend+"_"+getComment+"storedText.txt"
			fileBuild=path+shotList
			copyfilemessage="creating in "+fileBuild
			replay = QtGui.QMessageBox.question(None, 'Message' ,copyfilemessage, QtGui.QMessageBox.Yes, QtGui.QMessageBox.No)
			if reply == QtGui.QMessageBox.Yes:
				if os.path.isfile(fileBuild)==True:
					cmessage="create over "+fileBuild
					replay = QtGui.QMessageBox.question(None, 'Message' ,cmessage, QtGui.QMessageBox.Yes, QtGui.QMessageBox.No)
					if reply == QtGui.QMessageBox.Yes:
						inp=open(fileBuild, "w+")
						inp.write(name_to_save)
						inp.close()
						print "created "+fileBuild
					else:
						print "cancelled"
						return
				else:
					inp=open(fileBuild, "w+")
					inp.write(name_to_save)
					inp.close()
					print "created "+fileBuild
			else:
				print "cancelled"
				return
		elif list_build_function==get_a_play_list[2]:
			fileDict, list=self.getAllLists(allthePaths)
					
	def getAllLists(self, stuff):
		fileDict={}
		for each in stuff:
			getList, getnamesdic=self.obtain_presets(each)
			getnames=getnamesdic.keys()
			for eachp in eachn in map(None, getList, getnames):
				dictlist={eachn:eachp}
				fileDict.update(dictlist)
		return fileDict, getList
			
	def obtain_presets(self, morestuff):
		preset=False
		format=".txt"
		getpreset=[os.path.join(dirpath, name) for dirpath, dirnames, files in os.walk(morestuff) for name in files if name.lower().endswith(format)]
		preset=[(each) for each in getpreset if "storedText" in each]
		getlistnames={}
		for each in preset:
			getName=each.split("/")[-1]
			nam=getName.split("_")
			getpletename='_'.join(nam[:-1])
			diction={getpletename:nam[0]}
			getlistnames.update(diction)
		return preset, getlistnames
		
		
	def makeBody(self, prompt):
		text, ok=QtGui.QInputDialog.getText(None, 'Intput Dialog', prompt)
		if ok:
			project=(str(text))
		else:
			return
		return project
		
	def makeBodyFilled(self, prompt, message):
		text, ok=QtGui.QInputDialog.getText(None, 'Intput Dialog', prompt, QtGui.QLineEdit.Normal, message)
		if ok and text:
			project=(str(text))
		else:
			return
		return project

	def load(self):
		list_load=self.drop_list_06
		list_load_function=list_load.currentText()
		allthePaths=('//', '//')
		allthePathsDic={"firstPath":'//', "secondPath":'//'}
		if list_load_function==presetlist[0]:
			prompt="name of list:"
			getcomment=self.makeBody(prompt)
			if getComment==None:
				print "needs name"
				return
			else:
				pass
			getComment=getComment.replace(' ', '_')
			shotList=suffixAppend+"_"+getComment+"storedText.txt"
			fileBuild=path+shotList
			copyfilemessage="creating in "+fileBuild
			replay = QtGui.QMessageBox.question(None, 'Message', copyfilemessage, QtGui.QMessageBox.Yes, QtGui.QMessageBox.No)
			if reply == QtGui.QMessageBox.Yes:
				if os.path.isfile(fileBuild)==True:
					cmessage="create over "+fileBuild
					replay = QtGui.QMessageBox.question(None, 'Message' ,cmessage, QtGui.QMessageBox.Yes, QtGui.QMessageBox.No)
					if reply == QtGui.QMessageBox.Yes:
						inp=open(fileBuild, "w+")
						inp.write(name_to_save)
						inp.close()
						print "created "+fileBuild
					else:
						print "cancelled"
						return
				else:
					inp=open(fileBuild, "w+")
					inp.write(name_to_save)
					inp.close()
					print "created "+fileBuild
			else:
				print "cancelled"
				return
		elif list_build_function==list_build[2]:
			fileDict, list=self.getAllLists(allthePaths)
			
	def reset_callup(self):
		allthePaths=('//', '//')
		allthePathsDic={"firstPath":'//', "secondPath":'//'}
		getlisttype=self.type_list_drop
		listtype=getlisttype.currentText()
		if listtype=="firstPath":
			directory=allthePathsDic.get("firstPath")
		getUser=getUser
		self.directory_for_taking(getUser, directory)
		
	def directory_for_taking(self, getUser, directory):
		model, countdata, listArray	=self.get_listStuff()
		# self.status_lbl

	def connectButton01(self):
		print "hi"
		self.listCreate()


	def RightClick(self):
		selected_in_list=self.is_listWid_item_selected()
		path=str(selected_in_list[0])+"/"
		# command="xdg-open '%s'"%path
		self.launch_folder(path)


	def play_in_rv(self):
		selected_in_list=self.is_listWid_item_selected()
		command="rv "+str(selected_in_list[0])+"/*"
		print "you are running command: "+command
		subprocess.Popen(command, stdout=subprocess.PIPE, shell=True)


	def compare_in_rv(self):
		selected_in_list=self.is_listWid_item_selected()
		if len(selected_in_list)<2:
			print "must select more than one object in list"
		else:
			command="rv -wipe "+str(selected_in_list[0])+"/* "+str(selected_in_list[1])+"/*"
			print "you are running command: "+command
			subprocess.Popen(command, stdout=subprocess.PIPE, shell=True)


	def pub_to_shotgun(self):
		selected_in_list=self.is_listWid_item_selected()
		getPath=selected_in_list[0]+'/'+foldertypeplay+"/"
		get_items=os.listdir(getPath)
		getFileName=get_items[0].split(".")[0]
		print getFileName
		# getFileName=str(getFileName)+"-"+str(typeplay)
		# print getFileName
		format="_jpg"
		# command='msubmitCmd -p "'+str(selected_in_list[0])+getFileName+'.%04d'+formatEXT+'"'+' -s '+str(startFR)+' -e '+str(endFR)+' -t review -n='+comment+' --task techanim -audio '+audioFile+' -audioOffset 0'
		command='msubmitCmd -p "'+str(selected_in_list[0])+'/'+typeplay+format+'/'+getFileName+'.%04d'+formatEXT+'"'+' -s '+str(startFR)+' -e '+str(endFR)+' -t review -n="'+comment+'" --task '+DEPT
		# command="rv "+str(selected_in_list[0])+"/*"
		print "you are running command: "+command
		subprocess.Popen(command, stdout=subprocess.PIPE, shell=True)


	def send(self):
		print "end"

	def card_special_callup(self):
		print "card_special_callup"
		
	def Bcard_special_callup(self):
		print "B_card_special_callup"
		
		
	def B_card_special_callup(self):
		print "B_card_special_callup"