首页 > 代码库 > 共通函数 波形图 鼠标事件

共通函数 波形图 鼠标事件

# -*- coding: utf8 -*-

import sys

import datetime

import codecs

import os

from PyQt4 import QtGui, QtCore

import PyQt4.Qwt5 as Qwt

from PyQt4.Qwt5.anynumpy import *

from PlotGraph import *#波形图

from CommonFun import *#共通函数

from Spy import *#鼠标事件

import TypeConvert 

 

sqlDomestic = None

class BeiJingCosmic(QtGui.QWidget):

        def __init__(self, sqlDomesticArg, parent=None):

                QtGui.QWidget.__init__(self, parent)

                self.setWindowIcon(QtGui.QIcon(‘image/icon.ico‘))

                global sqlDomestic

                sqlDomestic = sqlDomesticArg

                self.titleLabel = QtGui.QLabel()

                self.titleLabel.setText(u"北京宇宙线超中子堆")

                self.titleLabel.setAlignment(Qt.Qt.AlignLeft)

                ##起始时间

                self.srartTimeLabel = QtGui.QLabel()

                self.srartTimeLabel.setAlignment(Qt.Qt.AlignRight)

                #四周显示的内容

                label2 = QtGui.QLabel()

                label2.setText(u"中国科学院空间环境研究预报中心")

                label2.setAlignment(Qt.Qt.AlignRight)

                self.updateTimeLabel = QtGui.QLabel()

                self.updateTimeLabel.setAlignment(Qt.Qt.AlignLeft)

         

                #北京宇宙线5分钟数据曲线

                self.serverPlotBJ5M = make1()

                self.serverPlotBJ5M.setYLeftLargeMag(5)

                self.serverPlotBJ5M.setYLeftScale(-10,10,4) 

                font = QtGui.QFont()

                font.setPointSize(9)

                txt=Qwt.QwtText(u"Counts/5 Minutes")

                txt.setFont(font)

                self.serverPlotBJ5M.setYLeft(txt)

 

                vBox = QtGui.QVBoxLayout()

                vBox.addWidget(self.serverPlotBJ5M)

                vBox.setSpacing(0)

                self.gBox = QtGui.QGroupBox()

                self.gBox.setLayout(vBox)

                self.gBox.setPalette(QtGui.QPalette(QtGui.QColor(255,255,225)))

                self.gBox.setAutoFillBackground(True)

 

                hBoxUp = QtGui.QHBoxLayout()

                hBoxUp.addWidget(self.titleLabel)

                hBoxUp.addWidget(self.srartTimeLabel)

                

                hBoxDown = QtGui.QHBoxLayout()

                hBoxDown.addWidget(self.updateTimeLabel)

                hBoxDown.addWidget(label2)

 

                mainLayout = QtGui.QVBoxLayout()

                mainLayout.addLayout(hBoxUp)

                mainLayout.addWidget(self.gBox)

                mainLayout.addLayout(hBoxDown)

                self.setLayout(mainLayout)

                #设置鼠标形状

                QtGui.QApplication.setOverrideCursor(QtCore.Qt.ArrowCursor)

                

                self.serverPlotBJ5M.setCanvasBackground(QtGui.QColor(255,255,225))

                self.createRightActions()

                self.registerContextMenuEvent()

                ###设置初始值

                curEndTime =QtCore.QDateTime.currentDateTime().addSecs(-8*3600)

                self.end = datetime.date(int(curEndTime.date().year()),

                                   int(curEndTime.date().month()),int(curEndTime.date().day()))

                self.start = self.end + datetime.timedelta(-2)

                self.resetBJPlot(self.start, self.end)

 

                #设置定时器,15MIN更新一次

                self.timer = QtCore.QTimer(self)

                self.connect(self.timer, QtCore.SIGNAL("timeout()"), self.updateEveryHalfHour)

                self.timer.start(1000*60*15)

 

        def createRightActions(self):

                #左侧

                self.nextPageAct = QtGui.QAction( u‘下一页‘, self)

                self.connect(self.nextPageAct, QtCore.SIGNAL("triggered()"), self.nextPageDis)

                self.prevPageAct = QtGui.QAction( u‘上一页‘, self)

                self.connect(self.prevPageAct, QtCore.SIGNAL("triggered()"), self.prevPageDis)

                self.closeAct = QtGui.QAction( u‘退出‘, self)

                self.connect(self.closeAct, QtCore.SIGNAL("triggered()"), self.close)

 

        def registerContextMenuEvent(self):

                #左侧

                self.setContextMenuPolicy(QtCore.Qt.ActionsContextMenu)

                self.addAction(self.prevPageAct)

                self.addAction(self.nextPageAct)

                self.addAction(self.closeAct)

 

 

        #从数据库获取北京5分钟数据

        def getBJ5MData(self, start, end):

                self.xDataBJ5M = []

                self.yDataBJ5M = []

                self.bootomDateBJ5M = []

                

                listDBX=[]#从数据库中取出的日期,得到的x

                listX = []#画图时用到的x

                listBootomDate=[]

                self.dataDate = []#存储从数据库中获得的年月日

                self.dateTime = []#存储从数据库获得的时分秒

                steps=(end-start).days+1

 

                for day in range(steps):

                        startStr=(start + datetime.timedelta(day)).__str__()

                        bootomDate=QtCore.QString(startStr)

                        listBootomDate.append(bootomDate)#要显示的日期

                self.bootomDateBJ5M = listBootomDate

                for fiveMinutes in range(steps*24*12):

                        listX.append(fiveMinutes)#x轴值

                        self.xDataBJ5M.append(fiveMinutes/(24*12*1.0))

 

                startStr=start.__str__( )

                endStr=end.__str__( ) 

                startStr=startStr.replace(‘-‘,‘‘)

                endStr=endStr.replace(‘-‘,‘‘)

                #获得查询起始时间 更新时间

                curTime = QtCore.QDateTime.currentDateTime().addSecs(-8*3600)

                updateTime = datetime.datetime(curTime.date().year(),curTime.date().month()

                                     ,curTime.date().day(), curTime.time().hour(),

                                         curTime.time().minute(), curTime.time().second())

                tc = TypeConvert.TypeConvert()

                ret, updateStr   = tc.ymdhmsTostr(updateTime.year, updateTime.month, updateTime.day,

                                                  updateTime.hour, updateTime.minute, updateTime.second)

                #设置起始 更新时间

                self.srartTimeLabel.setText(u"起始时间:%s-%s-%s 00:00:00 UTC"%(startStr[0:4], startStr[4:6],

                                                                           startStr[6:8]))

                self.updateTimeLabel.setText(u"更新时间:%s-%s-%s %s:%s:%s UTC"%(updateStr[0:4], updateStr[4:6],

                                                                           updateStr[6:8], updateStr[8:10],

                                                                           updateStr[10:12], updateStr[12:14]))

                

                #sql =ExecuteSQL(ConfigInfo.dbInfo[‘user‘],ConfigInfo.dbInfo[‘pwd‘],ConfigInfo.dbInfo[‘tns‘])

                #sqlDomestic =ExecuteSQL(‘YJDB‘,‘YJDB‘,‘isedb‘)

                sqlDomestic.getConnect()

                

                cuntSql = ‘select count(*) from t_bj_cr_5m_pre ‘

                itemSql = "select ((to_date(datadate, ‘YYYYMMDD‘)-to_date(‘%s‘, ‘YYYYMMDD‘))*24*12\

                          + (to_date(lpad(DATATIME,6,‘0‘), ‘hh24miss‘) - to_date(‘000000‘, ‘hh24miss‘))*24*12),\

                          neutron_count,datadate,lpad(datatime,6,‘0‘) from t_bj_cr_5m_pre"%startStr

                whereSql = "where DATADATE between %s and %s"%(startStr,endStr) + " order by DATADATE,DATATIME"

 

                state,count,cur = sqlDomestic.getQuery(cuntSql,itemSql,whereSql)

                if state ==1:#数据查询失败

                        return 1

                if count ==-99:#没有查询到数据

                        pass#弹出消息框,提示没有查询到数据

                else:

                        for row in cur:

                                listDBX.append( int(round(row[0])) )

                                self.yDataBJ5M.append(row[1])

                                self.dataDate.append(row[2])

                                self.dateTime.append(row[3])

                self.yDataBJ5M = handleVoidRecord(self.yDataBJ5M,listX,listDBX,True)

        

        def resetBJPlot(self,startTime,endTime):

                step = (endTime-startTime).days + 1

                self.getBJ5MData(startTime,endTime)

 

                #对数据进行处理并绘制曲线图

                self.handledXDataBJ5M,self.handledYDataBJ5M = handleData(self.xDataBJ5M,self.yDataBJ5M)

                self.serverPlotBJ5M.plotDraw(self.handledXDataBJ5M,self.handledYDataBJ5M,

                                             None,None,Qt.Qt.blue,None,False)

                minValue,maxValue,yStep=setYScaleCommon(self.yDataBJ5M,None ,

                                                        self.serverPlotBJ5M,10,False)

                #self.serverPlotBJ5M.disableY()

                self.serverPlotBJ5M.setXBottomScale(0,step,step/5+1)

                self.serverPlotBJ5M.setXBottom("",self.bootomDateBJ5M)

                #下面的接口在设置X坐标的时候可以将字体调小

                #self.serverPlotBJ5M.setXBottomFZDH("",self.bootomDateBJ5M)

 

 

        def nextPageDis(self):

                curEndTime =QtCore.QDateTime.currentDateTime().addSecs(-8*3600)

                curTime = datetime.date(int(curEndTime.date().year()),

                                   int(curEndTime.date().month()),int(curEndTime.date().day()))

                self.start =  self.start + datetime.timedelta(3)

                self.end =  self.end + datetime.timedelta(3)

                if self.end > curTime:

                        self.start =  self.start + datetime.timedelta(-3)

                        self.end =  self.end + datetime.timedelta(-3)

                        QtGui.QMessageBox.information(self,‘Message‘,

                        u"查询时间不能超过当前时间!", QtGui.QMessageBox.Yes)

                        return

                self.resetBJPlot(self.start, self.end)

 

        def prevPageDis(self):

                self.start =  self.start + datetime.timedelta(-3)

                self.end =  self.end + datetime.timedelta(-3)

                self.resetBJPlot(self.start, self.end)

 

        def updateEveryHalfHour(self):

                curEndTime =QtCore.QDateTime.currentDateTime().addSecs(-8*3600)

                self.end = datetime.date(int(curEndTime.date().year()),

                                   int(curEndTime.date().month()),int(curEndTime.date().day()))

                self.start = self.end + datetime.timedelta(-2)

                self.resetBJPlot(self.start, self.end)

 

        #键盘

        def keyPressEvent(self, event):

                if event.key() == QtCore.Qt.Key_Left:

                        self.prevPageDis()

                elif event.key() == QtCore.Qt.Key_Right:

                        self.nextPageDis()

                elif event.key() == QtCore.Qt.Key_Escape:

                        self.close()

 

 

 

‘‘‘

if __name__ == "__main__":

        app = QtGui.QApplication(sys.argv)

        login = BeiJingCosmic()

        login.show()

        app.exec_()

 

‘‘‘

 

 

共通函数 波形图 鼠标事件