Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- __author__ = "Roy Wang ([email protected])"
- __version__ = "2.4"
- __date__ = "$Date: 2013/11/26 14:00:00 $"
- __copyright__ = "Copyright (c) 2013 Dicovi"
- __license__ = "Dicovi"
- from PyQt4.QtGui import *
- from PyQt4.QtCore import *
- import sys, os
- from dpt_client import *
- from ui_dpt import *
- import serial
- import sip, decimal
- import dpt_qrc
- class DptClientCfg():
- def __init__(self):
- self.port = 0
- self.key = ''
- self.api = ''
- self.lan = "english"
- self.cfgfilename = './production.cfg'
- def readcfg(self):
- if not os.path.exists(self.cfgfilename):
- return False
- try:
- file_hander = open(self.cfgfilename, 'r')
- except (IOError, OSError):
- return False
- lines = file_hander.readlines()
- file_hander.close()
- for line in lines:
- line = str(line).strip()
- if line.startswith('#'):
- continue
- value = str(line.split('=')[1]).strip()
- if line.startswith('port') or line.startswith('PORT'):
- value = value.lstrip('COM')
- value = value.lstrip('com')
- if not value.isdigit():
- return False
- self.port = int(value)
- if line.startswith('productionkey') or line.startswith('PRODUCTIONKEY'):
- self.key = value
- if line.startswith('LANGUAGE') or line.startswith('language'):
- self.lan = value
- if line.startswith('API') or line.startswith('api'):
- self.api = value
- if self.port == 0:
- return False
- if self.key == '':
- return False
- return True
- def writecfg(self):
- try:
- file_hander = open(self.cfgfilename, 'w')
- except (IOError, OSError):
- return False
- item = "language=" + self.lan + "\n"
- file_hander.write(item)
- item = "port=COM"+str(self.port) + "\n"
- file_hander.write(item)
- item = "productionkey=" + self.key + "\n"
- file_hander.write(item)
- item = "api=" + self.api + "\n"
- file_hander.write(item)
- file_hander.close()
- return True
- '''
- class MovieSplashScreen(QSplashScreen):
- def __init__(self, movie, parent = None):
- movie.jumpToFrame(0)
- pixmap = QPixmap(movie.frameRect().size())
- QSplashScreen.__init__(self, pixmap)
- self.movie = movie
- self.movie.frameChanged.connect(self.repaint)
- def showEvent(self, event):
- self.movie.start()
- def hideEvent(self, event):
- self.movie.stop()
- def paintEvent(self, event):
- painter = QPainter(self)
- pixmap = self.movie.currentPixmap()
- self.setMask(pixmap.mask())
- painter.drawPixmap(0, 0, pixmap)
- def sizeHint(self):
- return self.movie.scaledSize()
- '''
- class waitDlg(QDialog):
- def __init__(self, parent=None, win_x=0, win_y=0, win_w=0, win_h=0):
- super(waitDlg, self).__init__(parent)
- w = h = 128
- x = win_x + (win_w - w)/2
- y = win_y + (win_h - h)/2
- self.setGeometry(x,y,w,h)
- self.resize(128,128)
- #self.gif = QGraphicsView("./icon/loading.gif")
- self.label = QLabel(self)
- #self.label.setText("Waitting...")
- self.label.resize(128,128)
- self.movie = QMovie(":/icon/loading.gif")
- #print self.movie.currentImage()
- #print self.movie.currentPixmap()
- self.label.setMovie(self.movie)
- self.setWindowFlags(Qt.FramelessWindowHint)
- self.setAutoFillBackground(True)
- self.movie.start()
- def __del__(self):
- self.movie.stop()
- class DptMainWin(QMainWindow):
- def scan_serial(self):
- """scan for available ports. return a list of tuples (num, name)"""
- available = []
- for i in range(16):
- try:
- s = serial.Serial(i)
- available.append(s.portstr)
- s.close() # explicit close 'cause of delayed GC in java
- except serial.SerialException:
- pass
- return available
- def update_translate(self):
- self.setWindowTitle(self.tr("Production Tools"))
- self.ui.menuHelp.setTitle(self.tr("Help"))
- self.ui.actionAbout.setText(self.tr("About"))
- self.ui.label_lan.setText(self.tr("Language"))
- self.ui.label_port.setText(self.tr("Serial Port"))
- self.ui.label_api.setText(self.tr("API"))
- self.ui.label_key.setText(self.tr("Production Key"))
- self.ui.label_info.setText(self.tr("Information"))
- self.ui.label_model.setText(self.tr("Model"))
- self.ui.label_order.setText(self.tr("Order"))
- self.ui.label_total.setText(self.tr("Total"))
- self.ui.label_producted.setText(self.tr("Producted"))
- self.ui.start.setText(self.tr("Start"))
- def __init__(self, app, parent=None):
- super(DptMainWin, self).__init__(parent)
- self.app = app
- self.cfg = DptClientCfg()
- self.cfg.readcfg()
- self.trans = None
- if self.cfg.lan == "chinese":
- trans = QTranslator()
- trans.load(":/zh_CN.qm")
- self.app.installTranslator(trans)
- self.trans = trans
- ui = Ui_mainWindow()
- ui.setupUi(self)
- self.ui = ui
- self.update_translate()
- ui.lan.addItems((u"English", u"简体中文"))
- if self.cfg.lan == "chinese":
- ui.lan.setCurrentIndex(1)
- else:
- ui.lan.setCurrentIndex(0)
- self.connect(ui.lan, SIGNAL("currentIndexChanged(int)"), self.change_lan)
- available_port = self.scan_serial()
- ui.port.addItems(available_port)
- if self.cfg.port > 0:
- port = "COM" + str(self.cfg.port)
- idx = ui.port.findText(port)
- if idx >= 0:
- ui.port.setCurrentIndex(idx)
- if len(self.cfg.api) > 0:
- ui.api.setText(self.cfg.api)
- if len(self.cfg.key) > 0:
- ui.production_key.setText(self.cfg.key)
- ui.total.setProperty("intValue", 0)
- ui.producted.setProperty("intValue", 0)
- ui.result.setProperty("intValue", 0)
- #ui.info.setDisabled(True)
- #ui.model.setDisabled(True)
- #ui.order.setDisabled(True)
- self.connect(ui.start,SIGNAL("clicked()"),self.tweak_production)
- #self.connect(ui.actionAbout, SIGNAL("clicked()"), self.showAbout)
- ui.actionAbout.triggered.connect(self.showAbout)
- self.worker = DptClientWorker()
- self.connect(self.worker, SIGNAL("msg_out"), self.update_msg)
- self.connect(self.worker, SIGNAL("update_num"), self.update_num)
- self.connect(self.worker, SIGNAL("update_info"), self.update_info)
- self.connect(self.worker, SIGNAL("finished()"), self.stop_update_ui)
- self.connect(self.worker, SIGNAL("terminated()"), self.stop_update_ui)
- self.connect(self.worker, SIGNAL("update_result"), self.update_result_led)
- self.productioning = False
- self.wait = None
- def closeEvent(self, event):
- if self.productioning:
- self.worker.stop()
- if None == self.wait:
- self.wait = waitDlg(win_x=self.x(), win_y=self.y(), win_w=self.width(), win_h=self.height()) #waitDlg()
- self.wait.exec_()
- while self.productioning:
- time.sleep(0.1)
- def change_lan(self, idx):
- if idx == 1:
- self.cfg.lan = "chinese"
- else:
- self.cfg.lan = "english"
- self.cfg.writecfg()
- if self.trans is not None:
- self.app.removeTranslator(self.trans)
- self.trans = None
- if self.cfg.lan == "chinese":
- trans = QTranslator()
- trans.load(":/zh_CN.qm")
- self.app.installTranslator(trans)
- self.trans = trans
- self.update_translate()
- def tweak_production(self):
- if not self.productioning:
- key = self.ui.production_key.text()
- api = self.ui.api.text()
- key = str(key).strip()
- api = str(api).strip()
- if len(api)==0 or len(key) == 0:
- return
- if not str(api).startswith("https://"):
- return
- portstr = str(self.ui.port.currentText())
- if len(portstr) == 0:
- return
- #print "start production"
- port = int(portstr.lstrip("COM").lstrip("com"))
- self.ui.api.setDisabled(True)
- self.ui.lan.setDisabled(True)
- self.ui.port.setDisabled(True)
- self.ui.production_key.setDisabled(True)
- self.ui.start.setText(self.tr("Stop"))
- self.ui.info.clear()
- self.worker.set_param(unicode(api), port, unicode(key))
- self.cfg.port = port
- self.cfg.key = key
- self.cfg.api = api
- self.cfg.writecfg()
- self.worker.start()
- self.productioning = True
- else:
- #print "stop production"
- self.worker.stop()
- if None == self.wait:
- self.wait = waitDlg(win_x=self.x(), win_y=self.y(), win_w=self.width(), win_h=self.height())
- self.wait.exec_()
- #if not self.wait.isActiveWindow():
- # self.wait.exec_()
- def stop_update_ui(self):
- #print "stop_update_ui"
- ui = self.ui
- ui.lan.setDisabled(False)
- ui.port.setDisabled(False)
- ui.api.setDisabled(False)
- ui.production_key.setDisabled(False)
- ui.model.clear()
- ui.order.clear()
- ui.info.clear()
- ui.total.setProperty("intValue", 0)
- ui.producted.setProperty("intValue", 0)
- ui.result.setProperty("intValue", 0)
- #ui.info.clear()
- ui.start.setText(self.tr("Start"))
- ui.start.setDisabled(False)
- #self.splash.finish(self)
- if None != self.wait:
- self.wait.close()
- self.wait.destroy()
- self.wait = None
- #if self.wait.isActiveWindow():
- # self.wait.close()
- self.productioning = False
- def update_msg(self, clean=0, msg=""):
- ui = self.ui
- if clean == 1:
- ui.info.clear()
- if msg != "":
- ui.info.append(msg)
- def update_num(self, total=0, producted=0):
- ui = self.ui
- ui.total.setProperty("intValue", total)
- ui.producted.setProperty("intValue", producted)
- def update_result_led(self, result = 0):
- ui = self.ui
- ui.result.setProperty("intValue", result)
- def update_info(self, info):
- if not isinstance(info, dict):
- return
- ui = self.ui
- if dict(info).has_key('model_description'):
- ui.model.setText(info['model_description'])
- if dict(info).has_key('order_description'):
- ui.order.setText(info['order_description'])
- if dict(info).has_key('number'):
- ui.total.setProperty("intValue", info['number'])
- if dict(info).has_key('producted'):
- ui.producted.setProperty("intValue", info['producted'])
- def showAbout(self):
- QMessageBox.about(self, self.tr("About"), self.tr("DTP Version: ") + __version__)
- #trans = QTranslator()
- #trans.load("zh_CN")
- app=QApplication(sys.argv)
- #app.installTranslator(trans)
- #app.removeTranslator(trans)
- win=DptMainWin(app=app)
- win.show()
- app.exec_()
Add Comment
Please, Sign In to add comment