Guest User

DptMain

a guest
Mar 12th, 2019
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 12.03 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2.  
  3. __author__     = "Roy Wang ([email protected])"
  4. __version__    = "2.4"
  5. __date__       = "$Date: 2013/11/26 14:00:00 $"
  6. __copyright__  = "Copyright (c) 2013 Dicovi"
  7. __license__    = "Dicovi"
  8.  
  9.  
  10. from PyQt4.QtGui import *  
  11. from PyQt4.QtCore import *
  12. import sys, os
  13. from dpt_client import *
  14. from ui_dpt import *
  15. import serial
  16. import sip, decimal
  17. import dpt_qrc
  18.  
  19. class DptClientCfg():
  20.     def __init__(self):
  21.         self.port = 0
  22.         self.key = ''
  23.         self.api = ''
  24.         self.lan = "english"
  25.         self.cfgfilename = './production.cfg'
  26.        
  27.     def readcfg(self):
  28.         if not os.path.exists(self.cfgfilename):
  29.             return False
  30.         try:
  31.             file_hander = open(self.cfgfilename, 'r')
  32.         except (IOError, OSError):
  33.             return False
  34.        
  35.         lines  = file_hander.readlines()
  36.         file_hander.close()
  37.    
  38.         for line in lines:
  39.             line = str(line).strip()
  40.             if line.startswith('#'):
  41.                 continue
  42.             value = str(line.split('=')[1]).strip()
  43.             if line.startswith('port') or line.startswith('PORT'):
  44.                 value = value.lstrip('COM')
  45.                 value = value.lstrip('com')
  46.                 if not value.isdigit():
  47.                     return False
  48.                 self.port = int(value)
  49.             if line.startswith('productionkey') or line.startswith('PRODUCTIONKEY'):
  50.                 self.key = value
  51.             if line.startswith('LANGUAGE') or line.startswith('language'):
  52.                 self.lan = value
  53.             if line.startswith('API') or line.startswith('api'):
  54.                 self.api = value
  55.         if self.port == 0:
  56.             return False
  57.         if self.key == '':      
  58.             return False
  59.         return True
  60.    
  61.     def writecfg(self):        
  62.         try:
  63.             file_hander = open(self.cfgfilename, 'w')
  64.         except (IOError, OSError):
  65.             return False
  66.        
  67.         item = "language=" + self.lan + "\n"
  68.         file_hander.write(item)
  69.         item = "port=COM"+str(self.port) + "\n"
  70.         file_hander.write(item)
  71.         item = "productionkey=" + self.key + "\n"
  72.         file_hander.write(item)
  73.         item = "api=" + self.api + "\n"
  74.         file_hander.write(item)
  75.         file_hander.close()
  76.         return True
  77.  
  78. '''
  79. class MovieSplashScreen(QSplashScreen):
  80.    def __init__(self, movie, parent = None):
  81.        movie.jumpToFrame(0)
  82.        pixmap = QPixmap(movie.frameRect().size())
  83.        QSplashScreen.__init__(self, pixmap)
  84.        self.movie = movie
  85.        self.movie.frameChanged.connect(self.repaint)
  86.        
  87.    def showEvent(self, event):
  88.        self.movie.start()
  89.        
  90.    def hideEvent(self, event):
  91.        self.movie.stop()
  92.        
  93.    def paintEvent(self, event):
  94.        painter = QPainter(self)
  95.        pixmap = self.movie.currentPixmap()
  96.        self.setMask(pixmap.mask())
  97.        painter.drawPixmap(0, 0, pixmap)
  98.        
  99.    def sizeHint(self):
  100.        return self.movie.scaledSize()
  101. '''
  102.    
  103. class waitDlg(QDialog):
  104.     def __init__(self, parent=None,  win_x=0, win_y=0, win_w=0, win_h=0):
  105.         super(waitDlg, self).__init__(parent)
  106.         w = h = 128
  107.         x = win_x + (win_w - w)/2
  108.         y = win_y + (win_h - h)/2
  109.         self.setGeometry(x,y,w,h)
  110.         self.resize(128,128)
  111.         #self.gif = QGraphicsView("./icon/loading.gif")
  112.        
  113.         self.label = QLabel(self)
  114.         #self.label.setText("Waitting...")
  115.         self.label.resize(128,128)
  116.         self.movie = QMovie(":/icon/loading.gif")
  117.         #print self.movie.currentImage()
  118.         #print self.movie.currentPixmap()
  119.    
  120.         self.label.setMovie(self.movie)
  121.         self.setWindowFlags(Qt.FramelessWindowHint)
  122.         self.setAutoFillBackground(True)
  123.         self.movie.start()
  124.     def __del__(self):
  125.         self.movie.stop()
  126.  
  127.  
  128.  
  129. class DptMainWin(QMainWindow):
  130.     def scan_serial(self):
  131.         """scan for available ports. return a list of tuples (num, name)"""
  132.         available = []
  133.         for i in range(16):
  134.             try:
  135.                 s = serial.Serial(i)
  136.                 available.append(s.portstr)
  137.                 s.close()   # explicit close 'cause of delayed GC in java
  138.             except serial.SerialException:
  139.                 pass
  140.         return available
  141.    
  142.     def update_translate(self):
  143.         self.setWindowTitle(self.tr("Production Tools"))
  144.         self.ui.menuHelp.setTitle(self.tr("Help"))
  145.         self.ui.actionAbout.setText(self.tr("About"))
  146.         self.ui.label_lan.setText(self.tr("Language"))
  147.         self.ui.label_port.setText(self.tr("Serial Port"))
  148.         self.ui.label_api.setText(self.tr("API"))
  149.         self.ui.label_key.setText(self.tr("Production Key"))
  150.         self.ui.label_info.setText(self.tr("Information"))
  151.         self.ui.label_model.setText(self.tr("Model"))
  152.         self.ui.label_order.setText(self.tr("Order"))
  153.         self.ui.label_total.setText(self.tr("Total"))
  154.         self.ui.label_producted.setText(self.tr("Producted"))
  155.         self.ui.start.setText(self.tr("Start"))
  156.    
  157.     def __init__(self, app, parent=None):
  158.         super(DptMainWin, self).__init__(parent)
  159.         self.app = app
  160.        
  161.         self.cfg = DptClientCfg()
  162.         self.cfg.readcfg()
  163.         self.trans = None
  164.         if self.cfg.lan == "chinese":
  165.             trans = QTranslator()
  166.             trans.load(":/zh_CN.qm")
  167.             self.app.installTranslator(trans)
  168.             self.trans = trans
  169.        
  170.         ui = Ui_mainWindow()
  171.         ui.setupUi(self)
  172.         self.ui = ui
  173.         self.update_translate()
  174.        
  175.         ui.lan.addItems((u"English", u"简体中文"))
  176.         if self.cfg.lan == "chinese":
  177.             ui.lan.setCurrentIndex(1)
  178.         else:
  179.             ui.lan.setCurrentIndex(0)
  180.        
  181.         self.connect(ui.lan, SIGNAL("currentIndexChanged(int)"), self.change_lan)
  182.         available_port = self.scan_serial()
  183.         ui.port.addItems(available_port)
  184.        
  185.        
  186.         if self.cfg.port > 0:
  187.             port = "COM" + str(self.cfg.port)
  188.             idx = ui.port.findText(port)
  189.             if idx >= 0:
  190.                 ui.port.setCurrentIndex(idx)
  191.        
  192.         if len(self.cfg.api) > 0:
  193.             ui.api.setText(self.cfg.api)
  194.         if len(self.cfg.key) > 0:
  195.             ui.production_key.setText(self.cfg.key)
  196.        
  197.         ui.total.setProperty("intValue", 0)
  198.         ui.producted.setProperty("intValue", 0)
  199.         ui.result.setProperty("intValue", 0)
  200.         #ui.info.setDisabled(True)
  201.         #ui.model.setDisabled(True)
  202.         #ui.order.setDisabled(True)
  203.        
  204.         self.connect(ui.start,SIGNAL("clicked()"),self.tweak_production)
  205.         #self.connect(ui.actionAbout, SIGNAL("clicked()"), self.showAbout)
  206.         ui.actionAbout.triggered.connect(self.showAbout)
  207.        
  208.         self.worker = DptClientWorker()  
  209.         self.connect(self.worker, SIGNAL("msg_out"), self.update_msg)
  210.         self.connect(self.worker, SIGNAL("update_num"), self.update_num)
  211.         self.connect(self.worker, SIGNAL("update_info"), self.update_info)
  212.         self.connect(self.worker, SIGNAL("finished()"), self.stop_update_ui)
  213.         self.connect(self.worker, SIGNAL("terminated()"), self.stop_update_ui)
  214.         self.connect(self.worker, SIGNAL("update_result"), self.update_result_led)
  215.        
  216.        
  217.         self.productioning = False      
  218.         self.wait = None
  219.    
  220.     def closeEvent(self, event):
  221.         if self.productioning:
  222.             self.worker.stop()
  223.             if None == self.wait:
  224.                 self.wait = waitDlg(win_x=self.x(), win_y=self.y(), win_w=self.width(), win_h=self.height()) #waitDlg()
  225.                 self.wait.exec_()
  226.         while self.productioning:
  227.             time.sleep(0.1)
  228.            
  229.     def change_lan(self, idx):
  230.         if idx == 1:
  231.             self.cfg.lan = "chinese"
  232.         else:
  233.             self.cfg.lan = "english"
  234.         self.cfg.writecfg()
  235.        
  236.         if self.trans is not None:
  237.             self.app.removeTranslator(self.trans)
  238.             self.trans = None
  239.            
  240.         if self.cfg.lan == "chinese":
  241.             trans = QTranslator()
  242.             trans.load(":/zh_CN.qm")
  243.             self.app.installTranslator(trans)
  244.             self.trans = trans
  245.            
  246.         self.update_translate()
  247.            
  248.  
  249.     def tweak_production(self):
  250.         if not self.productioning:
  251.             key = self.ui.production_key.text()
  252.             api = self.ui.api.text()
  253.             key = str(key).strip()
  254.             api = str(api).strip()
  255.             if len(api)==0 or len(key) == 0:
  256.                 return
  257.             if not str(api).startswith("https://"):
  258.                 return
  259.            
  260.             portstr = str(self.ui.port.currentText())
  261.             if len(portstr) == 0:
  262.                 return
  263.            
  264.             #print "start production"
  265.             port = int(portstr.lstrip("COM").lstrip("com"))
  266.             self.ui.api.setDisabled(True)
  267.             self.ui.lan.setDisabled(True)
  268.             self.ui.port.setDisabled(True)
  269.             self.ui.production_key.setDisabled(True)
  270.             self.ui.start.setText(self.tr("Stop"))
  271.  
  272.             self.ui.info.clear()
  273.             self.worker.set_param(unicode(api), port, unicode(key))
  274.             self.cfg.port = port
  275.             self.cfg.key = key
  276.             self.cfg.api = api
  277.             self.cfg.writecfg()
  278.             self.worker.start()
  279.             self.productioning = True
  280.         else:
  281.             #print "stop production"
  282.             self.worker.stop()
  283.             if None == self.wait:
  284.                 self.wait = waitDlg(win_x=self.x(), win_y=self.y(), win_w=self.width(), win_h=self.height())
  285.                 self.wait.exec_()            
  286.             #if not self.wait.isActiveWindow():
  287.             #    self.wait.exec_()
  288.  
  289.    
  290.     def stop_update_ui(self):
  291.         #print "stop_update_ui"
  292.         ui = self.ui
  293.         ui.lan.setDisabled(False)
  294.         ui.port.setDisabled(False)
  295.         ui.api.setDisabled(False)
  296.         ui.production_key.setDisabled(False)
  297.         ui.model.clear()
  298.         ui.order.clear()
  299.         ui.info.clear()
  300.         ui.total.setProperty("intValue", 0)
  301.         ui.producted.setProperty("intValue", 0)
  302.         ui.result.setProperty("intValue", 0)
  303.        
  304.         #ui.info.clear()
  305.         ui.start.setText(self.tr("Start"))
  306.         ui.start.setDisabled(False)
  307.        
  308.         #self.splash.finish(self)
  309.         if None != self.wait:
  310.             self.wait.close()
  311.             self.wait.destroy()
  312.             self.wait = None
  313.         #if self.wait.isActiveWindow():
  314.         #    self.wait.close()
  315.         self.productioning = False
  316.        
  317.    
  318.     def update_msg(self, clean=0, msg=""):
  319.         ui = self.ui
  320.         if clean == 1:
  321.             ui.info.clear()
  322.         if msg != "":
  323.             ui.info.append(msg)
  324.        
  325.        
  326.     def update_num(self, total=0, producted=0):
  327.         ui = self.ui
  328.         ui.total.setProperty("intValue", total)
  329.         ui.producted.setProperty("intValue", producted)
  330.        
  331.     def update_result_led(self, result = 0):
  332.         ui = self.ui
  333.         ui.result.setProperty("intValue", result)
  334.        
  335.        
  336.     def update_info(self, info):
  337.         if not isinstance(info, dict):
  338.             return
  339.         ui = self.ui
  340.         if dict(info).has_key('model_description'):
  341.             ui.model.setText(info['model_description'])
  342.         if dict(info).has_key('order_description'):
  343.             ui.order.setText(info['order_description'])
  344.         if dict(info).has_key('number'):
  345.             ui.total.setProperty("intValue", info['number'])
  346.         if dict(info).has_key('producted'):
  347.             ui.producted.setProperty("intValue", info['producted'])
  348.    
  349.     def showAbout(self):
  350.         QMessageBox.about(self, self.tr("About"), self.tr("DTP Version: ") + __version__)
  351.  
  352.  
  353. #trans = QTranslator()
  354. #trans.load("zh_CN")
  355. app=QApplication(sys.argv)
  356. #app.installTranslator(trans)
  357. #app.removeTranslator(trans)
  358. win=DptMainWin(app=app)
  359. win.show()
  360. app.exec_()
Add Comment
Please, Sign In to add comment