# -*- coding: utf-8 -*- import json import os import sys from PyQt4 import QtGui from PyQt4 import QtCore import time TASK_LIST_PATH = u'/home/digit/tasklist.json' #Endodeスクリプトで指定したjsonファイルのパス # Progress bar styles PENDING_PASS_STYLE = """ QProgressBar { text-align: center; height: 15px; } QProgressBar::chunk { width: 10px; } """ FIRST_PASS_STYLE = """ QProgressBar { text-align: center; height: 15px; } QProgressBar::chunk { background-color: orange; width: 10px; margin: 1px; } """ SECOND_PASS_STYLE = """ QProgressBar { text-align: center; height: 15px; } QProgressBar::chunk { background-color: lime; width: 10px; margin: 1px; } """ class TaskList(): def __init__(self): self.path = TASK_LIST_PATH self.__load(self.path) def __load(self, src): try: f = open(src, 'r') self.tasklist = json.loads(f.read()) f.close except: print(u'(´・ω・`:∴..') self.tasklist = {} def update(self): self.__load(self.path) def getwait(self): waitlist = [] for element in self.tasklist.get('wait'): element['path'] = os.path.basename(element.get('path', u'')); waitlist.append(element) return waitlist def getprocess(self): processlist = [] for element in self.tasklist.get('process'): element['path'] = os.path.basename(element.get('path', u'')); processlist.append(element) return processlist def getcompleted(self): complist = [] for element in self.tasklist.get('completed'): element['path'] = os.path.basename(element.get('path', u'')); complist.append(element) return complist class CompletedTreeView(QtGui.QTreeView): def __init__(self): super(CompletedTreeView, self).__init__() self._datamodel = QtGui.QStandardItemModel(0, 2) self._datamodel.setHeaderData(0, QtCore.Qt.Horizontal, 'Title') self._datamodel.setHeaderData(1, QtCore.Qt.Horizontal, 'Date') self.setModel(self._datamodel) self.show() def _add_item(self, completed, n): titleitem = QtGui.QStandardItem(completed.get('path', u'')) self._datamodel.setItem(n, 0, titleitem) dateitem = QtGui.QStandardItem(completed.get('date', u'')) self._datamodel.setItem(n, 1, dateitem) def _delete_item(self): self._datamodel.removeRow(0) def append(self, completed): self._add_item(completed, self._index) self._index = self._index + 1 def clear(self): self._datamodel.removeRows(0, self._datamodel.rowCount()) self._index = 0 class ProcessTreeView(QtGui.QTreeView): def __init__(self): super(ProcessTreeView, self).__init__() self._datamodel = QtGui.QStandardItemModel(0, 2) self._datamodel.setHeaderData(0, QtCore.Qt.Horizontal, 'Title') self._datamodel.setHeaderData(1, QtCore.Qt.Horizontal, 'Progress') self.setModel(self._datamodel) self.show() def _add_item(self, process, n): titleitem = QtGui.QStandardItem(process.get('path', u'')) self._datamodel.setItem(n, 0, titleitem) pbar = QtGui.QProgressBar(self) pbar.setValue(process.get('progress', 0)) if process.get('pass', 0) == 1: pbar.setStyleSheet(FIRST_PASS_STYLE) pbar.setFormat('First Pass:%p%') pbar.setRange(0, 100) elif process.get('pass', 0) == 2: pbar.setStyleSheet(SECOND_PASS_STYLE) pbar.setFormat('Second Pass:%p%') pbar.setRange(0, 100) else: pbar.setStyleSheet(PENDING_PASS_STYLE) pbar.setFormat('') # maquee style pbar.setRange(0, 0) index = self._datamodel.index(n, 1, QtCore.QModelIndex()) self.setIndexWidget(index, pbar) def _delete_item(self): self._datamodel.removeRow(0) def append(self, process): self._add_item(process, self._index) self._index = self._index + 1 def clear(self): self._datamodel.removeRows(0, self._datamodel.rowCount()) self._index = 0 class WaitTreeView(QtGui.QTreeView): def __init__(self): super(WaitTreeView, self).__init__() self._datamodel = QtGui.QStandardItemModel(0, 2) self._datamodel.setHeaderData(0, QtCore.Qt.Horizontal, 'Title') self._datamodel.setHeaderData(1, QtCore.Qt.Horizontal, 'Date') self.setModel(self._datamodel) self.show() def _add_item(self, wait, n): titleitem = QtGui.QStandardItem(wait.get('path', u'')) self._datamodel.setItem(n, 0, titleitem) dateitem = QtGui.QStandardItem(wait.get('date', u'')) self._datamodel.setItem(n, 1, dateitem) def _delete_item(self): self._datamodel.removeRow(0) def append(self, wait): self._add_item(wait, self._index) self._index = self._index + 1 def clear(self): self._datamodel.removeRows(0, self._datamodel.rowCount()) self._index = 0 class MainWindow(QtGui.QMainWindow): def __init__(self): self.tasklist = None; super(MainWindow, self).__init__() self.initUI() def initUI(self): self.timer = QtCore.QBasicTimer() self.completed = CompletedTreeView() self.completed.setColumnWidth(0, 450) self.process = ProcessTreeView() self.process.setColumnWidth(0, 450) self.wait = WaitTreeView() self.wait.setColumnWidth(0, 450) self.update() splitter = QtGui.QSplitter(QtCore.Qt.Vertical) splitter.addWidget(self.completed) splitter.addWidget(self.process) splitter.addWidget(self.wait) self.setCentralWidget(splitter) self.setGeometry(300, 300, 600, 400) self.setWindowTitle('Endoding process viewer') self.show() self.timer.start(60000, self) def setTasklist(self, tasklist): self.tasklist = tasklist def timerEvent(self, event): if event.timerId() == self.timer.timerId(): self.update() else: QtGui.QFrame.timerEvent(self, event) def update(self): if self.tasklist is None: return self.tasklist.update() self.dispCompleted(self.tasklist) self.dispProcess(self.tasklist) self.dispWait(self.tasklist) def dispCompleted(self, tasklist): self.completed.clear() for element in tasklist.getcompleted(): self.completed.append(element) def dispProcess(self, tasklist): self.process.clear() for element in tasklist.getprocess(): self.process.append(element) def dispWait(self, tasklist): self.wait.clear() for element in tasklist.getwait(): self.wait.append(element) class WindowTypeQt(): def __init__(self): self.app = QtGui.QApplication(sys.argv) self.mainwindow = MainWindow() def show(self, tasklist): self.mainwindow.setTasklist(tasklist) self.mainwindow.update() sys.exit(self.app.exec_()) def main(): gui = WindowTypeQt() tasklist = TaskList() gui.show(tasklist) if __name__ == '__main__': main()
実行してみるとこんな感じです。
それぞれ以下を表示するようにしました。
・上段にエンコード済みのファイル
・中断にエンコード中のファイル
・下段にエンコード待ちのファイル
tasklist.jsonをネットワークフォルダに置けば、サーバ端末でのエンコード状況をクライアント端末から見ることが出来ます。
正直お試しなので、エンコードスクリプト、表示スクリプト共に課題は山盛りですが、私が使用する分にはそこまで困らないくらいのレベルで使えています。
改善点は少しずつ直していこうかと思っています。