diff options
author | mkaay <mkaay@mkaay.de> | 2009-12-28 15:32:06 +0100 |
---|---|---|
committer | mkaay <mkaay@mkaay.de> | 2009-12-28 15:32:06 +0100 |
commit | ce2a8294b5aefe4497c88f24c817084868b8b1eb (patch) | |
tree | cf52569c6f52a12a4a2c5d5a6358aea797697ec5 /module/gui | |
parent | DLC Error Log (diff) | |
download | pyload-ce2a8294b5aefe4497c88f24c817084868b8b1eb.tar.xz |
gui now stable
Diffstat (limited to 'module/gui')
-rw-r--r-- | module/gui/Collector.py | 184 | ||||
-rw-r--r-- | module/gui/MainWindow.py | 6 | ||||
-rw-r--r-- | module/gui/Queue.py | 217 |
3 files changed, 210 insertions, 197 deletions
diff --git a/module/gui/Collector.py b/module/gui/Collector.py index bf0e50349..c2d167291 100644 --- a/module/gui/Collector.py +++ b/module/gui/Collector.py @@ -21,6 +21,8 @@ from PyQt4.QtGui import * from time import sleep +from module.gui.Queue import ItemIterator + class PackageCollector(QThread): def __init__(self, view, connector): QThread.__init__(self) @@ -52,7 +54,7 @@ class PackageCollector(QThread): pack = self.getPack(data["id"]) if not pack: pack = self.PackageCollectorPack(self) - pack.setData(data) + pack.setPackData(data) self.addPack(data["id"], pack) files = self.connector.getPackageFiles(data["id"]) pack.clear(files) @@ -61,125 +63,115 @@ class PackageCollector(QThread): child = pack.getChild(fid) if not child: child = self.PackageCollectorFile(self, pack) - child.setData(info) - pack.addChild(fid, child) + child.setFileData(info) + pack.addPackChild(fid, child) def addPack(self, pid, newPack): pos = None try: - for k, pack in enumerate(self.collector): - if pack.getData()["id"] == pid: - pos = k + for pack in ItemIterator(self.rootItem): + if pack.getPackData()["id"] == pid: + pos = self.rootItem.indexOfChild(pack) break if pos == None: raise Exception() - self.collector[pos] = newPack + item = self.rootItem.child(pos) + item.setPackData(newPack.getPackData()) except: - self.collector.append(newPack) - pos = self.collector.index(newPack) - item = self.rootItem.child(pos) - if not item: - item = QTreeWidgetItem() - self.rootItem.insertChild(pos, item) - item.setData(0, Qt.DisplayRole, QVariant(newPack.getData()["package_name"])) + self.rootItem.addChild(newPack) + item = newPack + item.setData(0, Qt.DisplayRole, QVariant(item.getPackData()["package_name"])) item.setData(0, Qt.UserRole, QVariant(pid)) def getPack(self, pid): - for k, pack in enumerate(self.collector): - if pack.getData()["id"] == pid: + for k, pack in enumerate(ItemIterator(self.rootItem)): + if pack.getPackData()["id"] == pid: return pack return None def clear(self, ids): clear = False - for pack in self.collector: - if not pack.getData()["id"] in ids: + for pack in ItemIterator(self.rootItem): + if not pack.getPackData()["id"] in ids: clear = True break if not clear: return - self.queue = [] self.rootItem.takeChildren() - class PackageCollectorPack(): + class PackageCollectorPack(QTreeWidgetItem): def __init__(self, collector): + QTreeWidgetItem.__init__(self) self.collector = collector - self.data = [] - self.children = [] + self._data = {} - def addChild(self, cid, newChild): + def addPackChild(self, cid, newChild): pos = None try: - for k, child in enumerate(self.getChildren()): + for child in ItemIterator(self): if child.getData()["id"] == cid: - pos = k + pos = self.indexOfChild(child) break if pos == None: raise Exception() - self.children[pos] = newChild + item = self.child(pos) + item.setFileData(newChild.getFileData()) except: - self.children.append(newChild) - pos = self.children.index(newChild) - ppos = self.collector.collector.index(self) - parent = self.collector.rootItem.child(ppos) - item = parent.child(pos) - if not item: - item = QTreeWidgetItem() - parent.insertChild(pos, item) - status = "%s (%s)" % (newChild.getData()["status_type"], newChild.getData()["plugin"]) - item.setData(0, Qt.DisplayRole, QVariant(newChild.getData()["filename"])) + self.addChild(newChild) + item = newChild + item.setData(0, Qt.DisplayRole, QVariant(item.getFileData()["filename"])) item.setData(0, Qt.UserRole, QVariant(cid)) - flags = Qt.ItemIsEnabled - item.setFlags(flags) - def getChildren(self): - return self.children + def setPackData(self, data): + self._data = data - def getChild(self, cid): - try: - return self.children[cid] - except: - return None - - def hasChildren(self, data): - return (len(self.children) > 0) + def getPackData(self): + return self._data - def setData(self, data): - self.data = data + def getChildren(self): + ret = [] + for item in ItemIterator(self): + ret.append(item) + return ret - def getData(self): - return self.data + def getChild(self, cid): + for item in ItemIterator(self): + if item.getFileData()["id"] == cid: + return item + return None def clear(self, ids): clear = False - children = {} - for file in self.getChildren(): - if not file.getData()["id"] in ids: - clear = True - break - try: - children[file.getData()["id"]] - clear = True - except: - children[file.getData()["id"]] = True - - if not clear: + remove = [] + children = [] + for k, file in enumerate(self.getChildren()): + if not file.getFileData()["id"] in ids: + remove.append(file.getFileData()["id"]) + if file.getFileData()["id"] in children and not file.getFileData()["id"] in remove: + remove.append(file.getFileData()["id"]) + continue + children.append(file.getFileData()["id"]) + if not remove: return - ppos = self.collector.collector.index(self) - parent = self.collector.rootItem.child(ppos) - parent.takeChildren() - self.children = [] + remove.sort() + remove.reverse() + parent = self + for k in remove: + parent.takeChild(k) - class PackageCollectorFile(): + class PackageCollectorFile(QTreeWidgetItem): def __init__(self, collector, pack): + QTreeWidgetItem.__init__(self) self.collector = collector self.pack = pack + self._data = {} + self.wait_since = None - def getData(self): - return self.data + def getFileData(self): + return self._data - def setData(self, data): - self.data = data + def setFileData(self, data): + self._data = data def getPack(self): return self.pack @@ -189,9 +181,9 @@ class LinkCollector(QThread): QThread.__init__(self) self.view = view self.connector = connector - self.collector = [] self.interval = 2 self.running = True + self.rootItem = self.view.invisibleRootItem() self.mutex = QMutex() def run(self): @@ -211,54 +203,52 @@ class LinkCollector(QThread): file = self.getFile(id) if not file: file = self.LinkCollectorFile(self) - file.setData(data) + file.setFileData(data) self.addFile(id, file) def addFile(self, pid, newFile): pos = None try: - for k, file in enumerate(self.collector): - if file.getData()["id"] == pid: - pos = k + for pack in ItemIterator(self.rootItem): + if file.getFileData()["id"] == pid: + pos = self.rootItem.indexOfChild(file) break if pos == None: raise Exception() - self.collector[pos] = newFile + item = self.rootItem.child(pos) + item.setFileData(newFile.getPackData()) except: - self.collector.append(newFile) - pos = self.collector.index(newFile) - item = self.view.topLevelItem(pos) - if not item: - item = QTreeWidgetItem() - self.view.insertTopLevelItem(pos, item) - item.setData(0, Qt.DisplayRole, QVariant(newFile.getData()["filename"])) + self.rootItem.addChild(newFile) + item = newFile + item.setData(0, Qt.DisplayRole, QVariant(newFile.getFileData()["filename"])) item.setData(0, Qt.UserRole, QVariant(pid)) flags = Qt.ItemIsSelectable | Qt.ItemIsDragEnabled | Qt.ItemIsEnabled item.setFlags(flags) def getFile(self, pid): - for k, file in enumerate(self.collector): - if file.getData()["id"] == pid: + for file in ItemIterator(self.rootItem): + if file.getFileData()["id"] == pid: return file return None def clear(self, ids): clear = False - for pack in self.collector: - if not pack.getData()["id"] in ids: + for file in ItemIterator(self.rootItem): + if not file.getFileData()["id"] in ids: clear = True break if not clear: return - self.collector = [] - self.view.emit(SIGNAL("clear")) + self.rootItem.takeChildren() - class LinkCollectorFile(): + class LinkCollectorFile(QTreeWidgetItem): def __init__(self, collector): + QTreeWidgetItem.__init__(self) self.collector = collector + self._data = {} - def getData(self): - return self.data + def getFileData(self): + return self._data - def setData(self, data): - self.data = data + def setFileData(self, data): + self._data = data diff --git a/module/gui/MainWindow.py b/module/gui/MainWindow.py index 1e769110b..76f60a5bd 100644 --- a/module/gui/MainWindow.py +++ b/module/gui/MainWindow.py @@ -281,7 +281,11 @@ class MainWindow(QMainWindow): """ items = self.tabs["collector"]["package_view"].selectedItems() for item in items: - id = item.data(0, Qt.UserRole).toPyObject() + try: + id = item.getFileData()["id"] + id = item.parent().getPackData()["id"] + except: + id = item.getPackData()["id"] self.emit(SIGNAL("pushPackageToQueue"), id) def saveWindow(self): diff --git a/module/gui/Queue.py b/module/gui/Queue.py index 0def31bf6..c0f43c740 100644 --- a/module/gui/Queue.py +++ b/module/gui/Queue.py @@ -28,15 +28,16 @@ class Queue(QThread): self.connector = connector self.statusMap = { "finished": 0, - "checking": 1, - "waiting": 2, - "reconnected": 3, - "downloading": 4, - "failed": 5, - "aborted": 6, + "queued": 1, + "checking": 2, + "waiting": 3, + "reconnected": 4, + "starting": 5, + "downloading": 6, + "failed": 7, + "aborted": 8, } self.statusMapReverse = dict((v,k) for k, v in self.statusMap.iteritems()) - self.queue = [] self.interval = 1 self.running = True self.wait_dict = {} @@ -70,10 +71,10 @@ class Queue(QThread): pack = self.getPack(data["id"]) if not pack: pack = self.QueuePack(self) - pack.setData(data) - self.addPack(data["id"], pack) + pack.setPackData(data) files = self.connector.getPackageFiles(data["id"]) pack.clear(files) + self.addPack(data["id"], pack) for fid in files: info = self.connector.getLinkInfo(fid) child = pack.getChild(fid) @@ -84,35 +85,36 @@ class Queue(QThread): info["downloading"] = downloading[info["id"]] except: pass - child.setData(info) - pack.addChild(fid, child) + if not info["status_type"]: + info["status_type"] = "queued" + child.setFileData(info) + pack.addPackChild(fid, child) + self.addPack(data["id"], pack) def addPack(self, pid, newPack): pos = None try: - for k, pack in enumerate(self.queue): - if pack.getData()["id"] == pid: - pos = k + for pack in QueueIterator(self.rootItem): + if pack.getPackData()["id"] == pid: + pos = self.rootItem.indexOfChild(pack) break if pos == None: raise Exception() - self.queue[pos] = newPack + item = self.rootItem.child(pos) + item.setPackData(newPack.getPackData()) except: - self.queue.append(newPack) - pos = self.queue.index(newPack) - item = self.rootItem.child(pos) - if not item: - item = QTreeWidgetItem() - self.rootItem.insertChild(pos, item) - item.setData(0, Qt.DisplayRole, QVariant(newPack.getData()["package_name"])) + self.rootItem.addChild(newPack) + item = newPack + item.setData(0, Qt.DisplayRole, QVariant(item.getPackData()["package_name"])) status = -1 - speed = self.getSpeed(newPack) + speed = self.getSpeed(item) plugins = [] - for child in newPack.getChildren(): - if self.statusMap.has_key(child.data["status_type"]) and self.statusMap[child.data["status_type"]] > status: - status = self.statusMap[child.data["status_type"]] - if not child.data["plugin"] in plugins: - plugins.append(child.data["plugin"]) + for child in item.getChildren(): + data = child.getFileData() + if self.statusMap.has_key(data["status_type"]) and self.statusMap[data["status_type"]] > status: + status = self.statusMap[data["status_type"]] + if not data["plugin"] in plugins: + plugins.append(data["plugin"]) if status >= 0: if speed == None: statustxt = self.statusMapReverse[status] @@ -121,29 +123,28 @@ class Queue(QThread): item.setData(2, Qt.DisplayRole, QVariant(statustxt)) item.setData(1, Qt.DisplayRole, QVariant(", ".join(plugins))) item.setData(0, Qt.UserRole, QVariant(pid)) - item.setData(3, Qt.UserRole, QVariant(newPack)) + item.setData(3, Qt.UserRole, QVariant(item)) def getPack(self, pid): - for k, pack in enumerate(self.queue): - if pack.getData()["id"] == pid: + for k, pack in enumerate(ItemIterator(self.rootItem)): + if pack.getPackData()["id"] == pid: return pack return None def clear(self, ids): clear = False - for pack in self.queue: - if not pack.getData()["id"] in ids: + for pack in ItemIterator(self.rootItem): + if not pack.getPackData()["id"] in ids: clear = True break if not clear: return - self.queue = [] self.rootItem.takeChildren() def getWaitingProgress(self, q): locker = QMutexLocker(self.mutex) if isinstance(q, self.QueueFile): - data = q.getData() + data = q.getFileData() if data["status_type"] == "waiting" and data["downloading"]: until = float(data["downloading"]["wait_until"]) try: @@ -164,7 +165,7 @@ class Queue(QThread): def getProgress(self, q): locker = QMutexLocker(self.mutex) if isinstance(q, self.QueueFile): - data = q.getData() + data = q.getFileData() if data["downloading"]: return int(data["downloading"]["percent"]) if data["status_type"] == "finished" or \ @@ -177,12 +178,12 @@ class Queue(QThread): perc_sum = 0 for child in children: val = 0 - data = child.getData() + data = child.getFileData() if data["downloading"]: val = int(data["downloading"]["percent"]) - elif child.data["status_type"] == "finished" or \ - child.data["status_type"] == "failed" or \ - child.data["status_type"] == "aborted": + elif data["status_type"] == "finished" or \ + data["status_type"] == "failed" or \ + data["status_type"] == "aborted": val = 100 perc_sum += val if count == 0: @@ -192,7 +193,7 @@ class Queue(QThread): def getSpeed(self, q): if isinstance(q, self.QueueFile): - data = q.getData() + data = q.getFileData() if data["downloading"]: return int(data["downloading"]["speed"]) elif isinstance(q, self.QueuePack): @@ -203,7 +204,7 @@ class Queue(QThread): running = False for child in children: val = 0 - data = child.getData() + data = child.getFileData() if data["downloading"]: if not data["status_type"] == "waiting": all_waiting = False @@ -215,91 +216,87 @@ class Queue(QThread): return speed_sum return None - class QueuePack(): + class QueuePack(QTreeWidgetItem): def __init__(self, queue): + QTreeWidgetItem.__init__(self) self.queue = queue - self.data = [] - self.children = [] + self._data = {} - def addChild(self, cid, newChild): + def addPackChild(self, cid, newChild): pos = None try: - for k, child in enumerate(self.getChildren()): + for child in ItemIterator(self): if child.getData()["id"] == cid: - pos = k + pos = self.indexOfChild(child) break if pos == None: raise Exception() - self.children[pos] = newChild + item = self.child(pos) + item.setFileData(newChild.getFileData()) except: - self.children.append(newChild) - pos = self.children.index(newChild) - ppos = self.queue.queue.index(self) - parent = self.queue.rootItem.child(ppos) - item = parent.child(pos) - if not item: - item = QTreeWidgetItem() - parent.insertChild(pos, item) - speed = self.queue.getSpeed(newChild) - if speed == None or newChild.getData()["status_type"] == "starting": - status = newChild.getData()["status_type"] + self.addChild(newChild) + item = newChild + speed = self.queue.getSpeed(item) + if speed == None or item.getFileData()["status_type"] == "starting": + status = item.getFileData()["status_type"] else: - status = "%s (%s KB/s)" % (newChild.getData()["status_type"], speed) - item.setData(0, Qt.DisplayRole, QVariant(newChild.getData()["filename"])) + status = "%s (%s KB/s)" % (item.getFileData()["status_type"], speed) + item.setData(0, Qt.DisplayRole, QVariant(item.getFileData()["filename"])) item.setData(2, Qt.DisplayRole, QVariant(status)) - item.setData(1, Qt.DisplayRole, QVariant(newChild.getData()["plugin"])) + item.setData(1, Qt.DisplayRole, QVariant(item.getFileData()["plugin"])) item.setData(0, Qt.UserRole, QVariant(cid)) - item.setData(3, Qt.UserRole, QVariant(newChild)) - - def getChildren(self): - return self.children + item.setData(3, Qt.UserRole, QVariant(item)) - def getChild(self, cid): - try: - return self.children[cid] - except: - return None + def setPackData(self, data): + self._data = data - def hasChildren(self, data): - return (len(self.children) > 0) + def getPackData(self): + return self._data - def setData(self, data): - self.data = data + def getChildren(self): + ret = [] + for item in ItemIterator(self): + ret.append(item) + return ret - def getData(self): - return self.data + def getChild(self, cid): + for item in ItemIterator(self): + if item.getFileData()["id"] == cid: + return item + return None def clear(self, ids): clear = False - children = {} - for file in self.getChildren(): - if not file.getData()["id"] in ids: - clear = True - break - try: - children[file.getData()["id"]] - clear = True - except: - children[file.getData()["id"]] = True - - if not clear: + remove = [] + children = [] + for k, file in enumerate(self.getChildren()): + if not file.getFileData()["id"] in ids: + remove.append(file.getFileData()["id"]) + if file.getFileData()["id"] in children and not file.getFileData()["id"] in remove: + remove.append(file.getFileData()["id"]) + continue + children.append(file.getFileData()["id"]) + if not remove: return - ppos = self.queue.queue.index(self) - parent = self.queue.rootItem.child(ppos) - parent.takeChildren() - self.children = [] + remove.sort() + remove.reverse() + parent = self + for k in remove: + parent.takeChild(k) - class QueueFile(): + class QueueFile(QTreeWidgetItem): def __init__(self, queue, pack): + QTreeWidgetItem.__init__(self) self.queue = queue self.pack = pack + self._data = {} self.wait_since = None - def getData(self): - return self.data + def getFileData(self): + return self._data - def setData(self, data): - self.data = data + def setFileData(self, data): + self._data = data def getPack(self): return self.pack @@ -335,3 +332,25 @@ class QueueProgressBarDelegate(QItemDelegate): QApplication.style().drawControl(QStyle.CE_ProgressBar, opts, painter) return QItemDelegate.paint(self, painter, option, index) + +class ItemIterator(): + def __init__(self, item): + self.item = item + self.current = -1 + + def __iadd__(self, val): + self.current += val + + def value(self): + return self.item.child(self.current) + + def next(self): + self.__iadd__(1) + value = self.value() + if value: + return self.value() + else: + raise StopIteration + + def __iter__(self): + return self |