Advertisement
Guest User

Untitled

a guest
Dec 5th, 2014
58
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import sys
  2. import time
  3. from numpy import arange, sin, pi
  4. from PyQt4 import QtGui
  5. from PyQt4 import QtCore
  6. from Matplot import Ui_MainWindow
  7. import pypyodbc
  8. import matplotlib.pyplot as plt
  9. from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
  10. from matplotlib.figure import Figure
  11. import random
  12.  
  13. class cursorConnect():
  14.     def __init__(self, ip, instance):
  15.         self.cnxn = pypyodbc.connect('DRIVER={SQL SERVER};SERVER=' + ip + ";DATABASE=" + instance + ";Trusted_Connection=yes;")
  16.         self.cursor = self.cnxn.cursor()
  17.  
  18. class Main(QtGui.QMainWindow):  #inherit QMainWindow class from the ui created with Qt designer.  This class
  19.     def __init__(self):
  20.         QtGui.QMainWindow.__init__(self)  #instantiate the inherited class
  21.         self.ui = Ui_MainWindow()   #set self.ui to the Ui_MainWindow object
  22.         self.ui.setupUi(self)       #Run setupUi function
  23.         self.main_widget = QtGui.QWidget(self)
  24.         self.thePlot = MyStaticMplCanvas(self.main_widget)
  25.         self.thePlot.__init__(self)
  26.         self.ui.gridLayout.addWidget(self.thePlot)
  27.         #data = [random.random() for i in range(10)]
  28.  
  29.         self.getMezzdata()
  30.         self.setupSignals()
  31.  
  32.     def getMezzdata(self):
  33.         mezzNames = GetData('select Name from Mezzanines')
  34.         mezzString = []
  35.         for mezz in mezzNames:
  36.             mezzString.append(mezz[0])
  37.         self.ui.listWidget_Mezz.addItems(mezzString)
  38.     def setupSignals(self):
  39.         self.ui.listWidget_Mezz.itemClicked.connect(self.mezzSelected)
  40.         self.ui.listWidget_Furnace.itemClicked.connect(self.furSelected)
  41.         self.ui.listWidget_BouleID.itemClicked.connect(self.bouleIDSelected)
  42.         self.ui.btn_Plot.clicked.connect(self.plotIT)
  43.         #self.ui.btn_Plot.click
  44.     def mezzSelected(self):
  45.         mezzitemSelected = self.ui.listWidget_Mezz.selectedItems()
  46.         mezzitemSelected = mezzitemSelected[0].text()
  47.         sqlStatement="select Number from Furnaces F inner join Mezzanines M on F.MezzanineFK=M.PK where name='" + mezzitemSelected + "' order by F.Number"
  48.         furnaceNames = GetData(sqlStatement)
  49.         #x = np.shape(furnaceNames)
  50.  
  51.         furnaceInt = []
  52.         for furnace in furnaceNames:
  53.             furnaceInt.append(furnace[0])
  54.         furnaceInt.sort()
  55.         a = (str(furnace) for furnace in furnaceInt)
  56.         furnaceStr = []
  57.         for furnace in furnaceInt:
  58.             furnaceStr.append(str(furnace))
  59.  
  60.         self.ui.listWidget_Furnace.clear()
  61.         self.ui.listWidget_Furnace.addItems(furnaceStr)
  62.     def furSelected(self):
  63.         mezzitemSelected = self.ui.listWidget_Mezz.selectedItems()
  64.         print(mezzitemSelected)
  65.         mezzitemSelected = mezzitemSelected[0].text()
  66.         furitemselected = self.ui.listWidget_Furnace.selectedItems()
  67.         furitemselected = furitemselected[0].text()
  68.         sqlStatement=("SELECT BIR.BouleID from BouleIDRecords BIR inner join Furnaces F"
  69.         " on BIR.FurnaceFK=F.PK "
  70.         "inner join Mezzanines M "
  71.         "on F.MezzanineFK=M.PK where M.Name='" + mezzitemSelected + "' and F.Number='" + furitemselected + "'")
  72.         print(sqlStatement)
  73.         bouleIDData = GetData(sqlStatement)
  74.         print(bouleIDData)
  75.         bouleIDInt = []
  76.         for bouleID in bouleIDData:
  77.             bouleIDInt.append(bouleID[0])
  78.         bouleIDInt.sort()
  79.         #for boule in bouleIDInt:
  80.         #    print(boule)
  81.         bouleIDStr = []
  82.         for bouleID in bouleIDInt:
  83.             bouleIDStr.append(str(bouleID))
  84.  
  85.         self.ui.listWidget_BouleID.clear()
  86.         self.ui.listWidget_BouleID.addItems(bouleIDStr)
  87.     def bouleIDSelected(self):
  88.         print('hello')
  89.         cursorObject = cursorConnect("GTASFDM", "GTAT_ASFData_PROD")
  90.         #cursorObject.cursor.execute("select * top 1 from DataProcessLogs")
  91.         #cursorObject.cursor.fetchall()
  92.         cursorObject.cursor.execute('select top 1 * from dataprocesslogs')
  93.         #thedescription = cursorObject.cursor.description
  94.         columnnames = []
  95.         for thevalue in cursorObject.cursor.description:
  96.             columnnames.append(thevalue[0])
  97.         print(columnnames[2])
  98.         printType(columnnames[0])
  99.         self.ui.listWidget_Columns.clear()
  100.         self.ui.listWidget_Columns.addItems(columnnames)
  101.  
  102.     def plotIT(self):
  103.         mezzitemSelected = self.ui.listWidget_Mezz.selectedItems()
  104.         mezzitemSelected = mezzitemSelected[0].text()
  105.         furitemselected = self.ui.listWidget_Furnace.selectedItems()
  106.         furitemselected = furitemselected[0].text()
  107.         bouleIDSelected = self.ui.listWidget_BouleID.selectedItems()
  108.         bouleIDSelected = bouleIDSelected[0].text()
  109.         columnSelected = self.ui.listWidget_Columns.selectedItems()
  110.         columnSelected = columnSelected[0].text()
  111.         sqlStatement = ("select time, " + columnSelected + " from vw_RunData "
  112.         "where mezzaninename = '" + mezzitemSelected + "' and furnacenumber = "
  113.         "'" + furitemselected + "' and bouleid = '" + bouleIDSelected + "' order by time")
  114.         print(sqlStatement)
  115.         rawData = GetData(sqlStatement)
  116.  
  117.  
  118.         self.thePlot.axes = self.thePlot.fig.add_subplot(111)
  119.         print('Before plot attempt')
  120.  
  121.  
  122.         self.thePlot.axes.plot([1, 2])
  123.  
  124.         print('After plot attempt')
  125.  
  126. class MyMplCanvas(FigureCanvas):
  127.     def __init__(self, parent=None):
  128.         self.fig = Figure() #(figsize=(width, height, dpi=dpi))
  129.         #self.axes = self.fig.add_subplot(111)
  130.         #self.axes.hold(False)
  131.  
  132.         #self.compute_initial_figure()
  133.  
  134.         FigureCanvas.__init__(self, self.fig)
  135.         self.setParent(parent)
  136.  
  137.         #FigureCanvas.setSizePolicy(self,QtGui.QSizePolicy.Expanding,QtGui.QSizePolicy.Expanding)
  138.         #FigureCanvas.updateGeometry(self)
  139.  
  140.     def compute_initial_figure(self):
  141.         pass
  142. class MyStaticMplCanvas(MyMplCanvas):
  143.     """Simple canvas with a sine plot."""
  144.     def compute_initial_figure(self):
  145.         t = arange(0.0, 3.0, 0.01)
  146.         s = sin(2*pi*t)
  147.  
  148.         #self.axes.plot([1,2])
  149.         #self.axes.plot(t, s)
  150.  
  151. def GetData(SQLString):
  152.     cnxn = pypyodbc.connect('DRIVER={SQL SERVER};SERVER=GTASFDM;DATABASE=GTAT_ASFData_PROD;Trusted_Connection=yes;')
  153.     cursor = cnxn.cursor()
  154.     cursor.execute(SQLString)
  155.  
  156.     results = cursor.fetchall()
  157.  
  158.  
  159.     """
  160.    data = cursor.fetchall()
  161.    theData = []
  162.    if column == 1:
  163.        for row in data:
  164.            theData.append(row[0])
  165.        return theData
  166.    elif column == '*':
  167.        return data
  168.    """
  169.     return results
  170. def printType(object):
  171.     return print(type(object))
  172. def convert():
  173.     pass
  174.  
  175.  
  176.  
  177.  
  178.  
  179. if __name__ == '__main__':
  180.     app = QtGui.QApplication(sys.argv)
  181.     window = Main()
  182.     window.show()
  183.     sys.exit(app.exec_())
Advertisement
RAW Paste Data Copied
Advertisement