class GtkProgress(Progress, gtk.Window): def __init__(self, hassub): Progress.__init__(self) gtk.Window.__init__(self) self.__gobject_init__() self.connect("delete-event", lambda x,y: True) self._hassub = hassub self._shorturl = ShortURL(50) self._ticking = False self._stopticking = False self._fetcher = None if hassub: self.set_size_request(500, 400) else: self.set_size_request(300, 80) self.set_icon(getPixbuf("smart")) self.set_title(_("Operation Progress")) self.set_modal(True) self.set_position(gtk.WIN_POS_CENTER) vbox = gtk.VBox() vbox.set_border_width(10) vbox.set_spacing(10) vbox.show() gtk.Window.add(self, vbox) self._topic = gtk.Label() self._topic.set_alignment(0, 0.5) self._topic.show() vbox.pack_start(self._topic, expand=False, fill=False) self._progress = gtk.ProgressBar() self._progress.set_size_request(-1, 25) self._progress.show() vbox.pack_start(self._progress, expand=False, fill=False) if hassub: self._scrollwin = gtk.ScrolledWindow() self._scrollwin.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC) self._scrollwin.set_shadow_type(gtk.SHADOW_IN) self._scrollwin.show() vbox.pack_start(self._scrollwin) self._treemodel = gtk.ListStore(gobject.TYPE_INT, gobject.TYPE_STRING, gobject.TYPE_STRING, gobject.TYPE_STRING, gobject.TYPE_STRING) self._treeview = gtk.TreeView(self._treemodel) self._treeview.show() self._scrollwin.add(self._treeview) renderer = ProgressCellRenderer() column = gtk.TreeViewColumn(_("Progress"), renderer, percent=0) column.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED) column.set_fixed_width(110) self._treeview.append_column(column) renderer = gtk.CellRendererText() renderer.set_fixed_height_from_font(True) column = gtk.TreeViewColumn(_("Current"), renderer, text=2) self._currentcolumn = column self._treeview.append_column(column) renderer = gtk.CellRendererText() renderer.set_fixed_height_from_font(True) column = gtk.TreeViewColumn(_("Total"), renderer, text=3) self._totalcolumn = column self._treeview.append_column(column) renderer = gtk.CellRendererText() renderer.set_fixed_height_from_font(True) column = gtk.TreeViewColumn(_("Speed"), renderer, text=4) self._speedcolumn = column self._treeview.append_column(column) renderer = gtk.CellRendererText() renderer.set_fixed_height_from_font(True) column = gtk.TreeViewColumn(_("Description"), renderer, text=1) column.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED) self._treeview.append_column(column) self._subiters = {} self._subindex = 0 self._lastpath = None self._bbox = gtk.HButtonBox() self._bbox.set_spacing(10) self._bbox.set_layout(gtk.BUTTONBOX_END) vbox.pack_start(self._bbox, expand=False) button = gtk.Button(stock="gtk-cancel") button.show() button.connect("clicked", self._cancel) self._bbox.pack_start(button) def setFetcher(self, fetcher): if fetcher: self._bbox.show() self._fetcher = fetcher else: self._bbox.hide() self._fetcher = None def _cancel(self, widget): if self._fetcher: self._fetcher.cancel() def tick(self): while not self._stopticking: self.lock() while gtk.events_pending(): gtk.main_iteration() self.unlock() time.sleep(INTERVAL) self._ticking = False def start(self): Progress.start(self) self.setHasSub(self._hassub) self._ticking = True self._stopticking = False if self._hassub: self._currentcolumn.set_visible(False) self._totalcolumn.set_visible(False) self._speedcolumn.set_visible(False) thread.start_new_thread(self.tick, ()) def stop(self): self._stopticking = True while self._ticking: pass Progress.stop(self) if self._hassub: self._treemodel.clear() self._subiters.clear() self._subindex = 0 self._lastpath = None self._shorturl.reset() gtk.Window.hide(self) def expose(self, topic, percent, subkey, subtopic, subpercent, data, done): gtk.Window.show(self) if self._hassub and subkey: if subkey in self._subiters: iter = self._subiters[subkey] else: iter = self._treemodel.append() self._subiters[subkey] = iter path = self._treemodel.get_path(iter) if self._lastpath: column = self._treeview.get_column(1) cellarea = self._treeview.get_cell_area(self._lastpath, column) cellarea.x, cellarea.y = self._treeview.\ widget_to_tree_coords(cellarea.x, cellarea.y) visiblearea = self._treeview.get_visible_rect() isvisible = visiblearea.intersect(cellarea).height if not self._lastpath or isvisible: self._treeview.scroll_to_cell(path, None, True, 0, 0) self._lastpath = path current = data.get("current", "") if current: self._currentcolumn.set_visible(True) total = data.get("total", "") if total: self._totalcolumn.set_visible(True) speed = data.get("speed", "") if speed: self._speedcolumn.set_visible(True) if current or total or speed: self._treemodel.set(iter, 2, current, 3, total, 4, speed) subtopic = self._shorturl.get(subtopic) self._treemodel.set(iter, 0, subpercent, 1, subtopic) else: self._topic.set_text(topic) self._progress.set_fraction(percent/100.) self._progress.set_text("%d%%" % percent) if self._hassub: self._treeview.queue_draw()
class TextProgress(Progress): def __init__(self): Progress.__init__(self) self._lasttopic = None self._lastsubkey = None self._lastsubkeystart = 0 self._fetchermode = False self._seentopics = {} self._addline = False self.setScreenWidth(getScreenWidth()) def setScreenWidth(self, width): self._screenwidth = width self._topicwidth = int(width * 0.4) self._hashwidth = int(width - self._topicwidth - 1) self._topicmask = "%%-%d.%ds" % (self._topicwidth, self._topicwidth) self._topicmaskn = "%%4d:%%-%d.%ds" % (self._topicwidth - 5, self._topicwidth - 5) self._shorturl = ShortURL(width - 4) def setFetcherMode(self, flag): self._fetchermode = flag def stop(self): Progress.stop(self) self._shorturl.reset() print def expose(self, topic, percent, subkey, subtopic, subpercent, data, done): out = sys.stdout if self.getHasSub(): if topic != self._lasttopic: self._lasttopic = topic out.write(" " * (self._screenwidth - 1) + "\r") if self._addline: print else: self._addline = True print topic if not subkey: return if not done: now = time.time() if subkey == self._lastsubkey: if (self._lastsubkeystart + 2 < now and self.getSubCount() > 1): return else: if (self._lastsubkeystart + 2 > now and self.getSubCount() > 1): return self._lastsubkey = subkey self._lastsubkeystart = now elif subkey == self._lastsubkey: self._lastsubkeystart = 0 current = subpercent topic = subtopic if self._fetchermode: if topic not in self._seentopics: self._seentopics[topic] = True out.write(" " * (self._screenwidth - 1) + "\r") print "->", self._shorturl.get(topic) topic = posixpath.basename(topic) else: current = percent n = data.get("item-number") if n: if len(topic) > self._topicwidth - 6: topic = topic[:self._topicwidth - 8] + ".." out.write(self._topicmaskn % (n, topic)) else: if len(topic) > self._topicwidth - 1: topic = topic[:self._topicwidth - 3] + ".." out.write(self._topicmask % topic) if not done: speed = data.get("speed") if speed: suffix = "(%s - %d%%)\r" % (speed, current) else: suffix = "(%3d%%)\r" % current elif subpercent is None: suffix = "[%3d%%]\n" % current else: suffix = "[%3d%%]\n" % percent hashwidth = self._hashwidth - len(suffix) hashes = int(hashwidth * current / 100) out.write("#" * hashes) out.write(" " * (hashwidth - hashes + 1)) out.write(suffix) out.flush()
class GtkProgress(Progress, gtk.Window): def __init__(self, hassub): Progress.__init__(self) gtk.Window.__init__(self) self.__gobject_init__() self.connect("delete-event", lambda x, y: True) self._hassub = hassub self._shorturl = ShortURL(50) self._ticking = False self._stopticking = False self._fetcher = None if hassub: self.set_size_request(500, 400) else: self.set_size_request(300, 80) self.set_icon(getPixbuf("smart")) self.set_title(_("Operation Progress")) self.set_modal(True) self.set_position(gtk.WIN_POS_CENTER) vbox = gtk.VBox() vbox.set_border_width(10) vbox.set_spacing(10) vbox.show() gtk.Window.add(self, vbox) self._topic = gtk.Label() self._topic.set_alignment(0, 0.5) self._topic.show() vbox.pack_start(self._topic, expand=False, fill=False) self._progress = gtk.ProgressBar() self._progress.set_size_request(-1, 25) self._progress.show() vbox.pack_start(self._progress, expand=False, fill=False) if hassub: self._scrollwin = gtk.ScrolledWindow() self._scrollwin.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC) self._scrollwin.set_shadow_type(gtk.SHADOW_IN) self._scrollwin.show() vbox.pack_start(self._scrollwin) self._treemodel = gtk.ListStore(gobject.TYPE_INT, gobject.TYPE_STRING, gobject.TYPE_STRING, gobject.TYPE_STRING, gobject.TYPE_STRING) self._treeview = gtk.TreeView(self._treemodel) self._treeview.show() self._scrollwin.add(self._treeview) renderer = ProgressCellRenderer() column = gtk.TreeViewColumn(_("Progress"), renderer, percent=0) column.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED) column.set_fixed_width(110) self._treeview.append_column(column) renderer = gtk.CellRendererText() renderer.set_fixed_height_from_font(True) column = gtk.TreeViewColumn(_("Current"), renderer, text=2) self._currentcolumn = column self._treeview.append_column(column) renderer = gtk.CellRendererText() renderer.set_fixed_height_from_font(True) column = gtk.TreeViewColumn(_("Total"), renderer, text=3) self._totalcolumn = column self._treeview.append_column(column) renderer = gtk.CellRendererText() renderer.set_fixed_height_from_font(True) column = gtk.TreeViewColumn(_("Speed"), renderer, text=4) self._speedcolumn = column self._treeview.append_column(column) renderer = gtk.CellRendererText() renderer.set_fixed_height_from_font(True) column = gtk.TreeViewColumn(_("Description"), renderer, text=1) column.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED) self._treeview.append_column(column) self._subiters = {} self._subindex = 0 self._lastpath = None self._bbox = gtk.HButtonBox() self._bbox.set_spacing(10) self._bbox.set_layout(gtk.BUTTONBOX_END) vbox.pack_start(self._bbox, expand=False) button = gtk.Button(stock="gtk-cancel") button.show() button.connect("clicked", self._cancel) self._bbox.pack_start(button) def setFetcher(self, fetcher): if fetcher: self._bbox.show() self._fetcher = fetcher else: self._bbox.hide() self._fetcher = None def _cancel(self, widget): if self._fetcher: self._fetcher.cancel() def tick(self): while not self._stopticking: self.lock() while gtk.events_pending(): gtk.main_iteration() self.unlock() time.sleep(INTERVAL) self._ticking = False def start(self): Progress.start(self) self.setHasSub(self._hassub) self._ticking = True self._stopticking = False if self._hassub: self._currentcolumn.set_visible(False) self._totalcolumn.set_visible(False) self._speedcolumn.set_visible(False) thread.start_new_thread(self.tick, ()) def stop(self): self._stopticking = True while self._ticking: pass Progress.stop(self) if self._hassub: self._treemodel.clear() self._subiters.clear() self._subindex = 0 self._lastpath = None self._shorturl.reset() gtk.Window.hide(self) def expose(self, topic, percent, subkey, subtopic, subpercent, data, done): gtk.Window.show(self) if self._hassub and subkey: if subkey in self._subiters: iter = self._subiters[subkey] else: iter = self._treemodel.append() self._subiters[subkey] = iter path = self._treemodel.get_path(iter) if self._lastpath: column = self._treeview.get_column(1) cellarea = self._treeview.get_cell_area( self._lastpath, column) cellarea.x, cellarea.y = self._treeview.\ widget_to_tree_coords(cellarea.x, cellarea.y) visiblearea = self._treeview.get_visible_rect() isvisible = visiblearea.intersect(cellarea).height if not self._lastpath or isvisible: self._treeview.scroll_to_cell(path, None, True, 0, 0) self._lastpath = path current = data.get("current", "") if current: self._currentcolumn.set_visible(True) total = data.get("total", "") if total: self._totalcolumn.set_visible(True) speed = data.get("speed", "") if speed: self._speedcolumn.set_visible(True) if current or total or speed: self._treemodel.set(iter, 2, current, 3, total, 4, speed) subtopic = self._shorturl.get(subtopic) self._treemodel.set(iter, 0, subpercent, 1, subtopic) else: self._topic.set_text(topic) self._progress.set_fraction(percent / 100.) self._progress.set_text("%d%%" % percent) if self._hassub: self._treeview.queue_draw()
class Up2dateProgress(Progress): def __init__(self): Progress.__init__(self) self._lasttopic = None self._lastsubkey = None self._lastsubkeystart = 0 self._fetchermode = False self._seentopics = {} self._addline = False self.setScreenWidth(getScreenWidth()) def setScreenWidth(self, width): self._screenwidth = width self._topicwidth = int(width*0.4) self._hashwidth = int(width-self._topicwidth-1) self._topicmask = "%%-%d.%ds" % (self._topicwidth, self._topicwidth) self._topicmaskn = "%%4d:%%-%d.%ds" % (self._topicwidth-5, self._topicwidth-5) self._shorturl = ShortURL(width-4) def setFetcherMode(self, flag): self._fetchermode = flag def stop(self): Progress.stop(self) self._shorturl.reset() print def expose(self, topic, percent, subkey, subtopic, subpercent, data, done): out = sys.stderr if self.getHasSub(): if topic != self._lasttopic: self._lasttopic = topic out.write(" "*(self._screenwidth-1)+"\r") if self._addline: print else: self._addline = True print topic if not subkey: return if not done: now = time.time() if subkey == self._lastsubkey: if (self._lastsubkeystart+2 < now and self.getSubCount() > 1): return else: if (self._lastsubkeystart+2 > now and self.getSubCount() > 1): return self._lastsubkey = subkey self._lastsubkeystart = now elif subkey == self._lastsubkey: self._lastsubkeystart = 0 current = subpercent topic = subtopic if self._fetchermode: if topic not in self._seentopics: self._seentopics[topic] = True out.write(" "*(self._screenwidth-1)+"\r") print "->", self._shorturl.get(topic) topic = posixpath.basename(topic) else: current = percent n = data.get("item-number") if n: if len(topic) > self._topicwidth-6: topic = topic[:self._topicwidth-8]+".." out.write(self._topicmaskn % (n, topic)) else: if len(topic) > self._topicwidth-1: topic = topic[:self._topicwidth-3]+".." out.write(self._topicmask % topic) if not done: speed = data.get("speed") if speed: suffix = "(%s - %d%%)\r" % (speed, current) else: suffix = "(%3d%%)\r" % current elif subpercent is None: suffix = "[%3d%%]\n" % current else: suffix = "[%3d%%]\n" % percent hashwidth = self._hashwidth-len(suffix) hashes = int(hashwidth*current/100) out.write("#"*hashes) out.write(" "*(hashwidth-hashes+1)) out.write(suffix) out.flush()
class QtProgress(Progress, qt.QDialog): def __init__(self, hassub, parent=None): Progress.__init__(self) qt.QDialog.__init__(self, parent) self._hassub = hassub self._shorturl = ShortURL(50) self._ticking = False self._stopticking = False self._fetcher = None self._beenshown = False self._mainthread = None if hassub: self.setMinimumSize(500, 400) else: self.setMinimumSize(300, 80) self.setIcon(getPixmap("smart")) self.setCaption(_("Operation Progress")) vbox = qt.QVBoxLayout(self, 10, 10) vbox.setResizeMode(qt.QLayout.FreeResize) vbox.setMargin(10) vbox.setSpacing(10) self._topic = qt.QLabel(self) vbox.addWidget(self._topic) self._progressbar = qt.QProgressBar(self) self._progressbar.setPercentageVisible(True) self._progressbar.show() vbox.addWidget(self._progressbar) if hassub: self._listview = qt.QListView(self) self._listview.setSorting(-1, False); self._listview.setSelectionMode(qt.QListView.NoSelection ) self._listview.show() vbox.addWidget(self._listview) column = self._listview.addColumn(_("Progress")) self._listview.setColumnWidthMode(column, qt.QListView.Manual) self._listview.setColumnWidth(column, 55) column = self._listview.addColumn(_("Current")) self._currentcolumn = column column = self._listview.addColumn(_("Total")) self._totalcolumn = column column = self._listview.addColumn(_("Speed")) self._speedcolumn = column column = self._listview.addColumn(_("ETA")) self._etacolumn = column column = self._listview.addColumn(_("Description")) self._listview.setColumnWidthMode(column, qt.QListView.Manual) self._listview.setColumnWidth(column, 165) self._desccolumn = column self._subiters = {} self._subindex = 0 self._bbox = qt.QHBox(self) self._bbox.setSpacing(10) self._bbox.layout().addStretch(1) vbox.addWidget(self._bbox) button = qt.QPushButton(_("Cancel"), self._bbox) button.show() qt.QObject.connect(button, qt.SIGNAL("clicked()"), self._cancel) def setFetcher(self, fetcher): if fetcher: self._bbox.show() self._fetcher = fetcher else: self._bbox.hide() self._fetcher = None def _cancel(self): if self._fetcher: self._fetcher.cancel() def setMainThread(self, main): self._mainthread = main def tick(self): while not self._stopticking: self.lock() ## Note: it's NOT safe to call processEvents from threads other than main #while qt.QApplication.eventLoop().hasPendingEvents(): # qt.QApplication.eventLoop().processEvents(qt.QEventLoop.AllEvents) self.unlock() time.sleep(INTERVAL) self._ticking = False def start(self): Progress.start(self) self.setHasSub(self._hassub) self._ticking = True self._stopticking = False if self._hassub: self._listview.hideColumn(self._currentcolumn) self._listview.hideColumn(self._totalcolumn) self._listview.hideColumn(self._speedcolumn) self._listview.hideColumn(self._etacolumn) thread.start_new_thread(self.tick, ()) def stop(self): self._stopticking = True while self._ticking: pass Progress.stop(self) if self._hassub: self._listview.clear() self._subiters.clear() self._subindex = 0 self._shorturl.reset() qt.QDialog.hide(self) def _currentThread(self): if hasattr(qt, 'QThread'): return qt.QThread.currentThread() else: return None def expose(self, topic, percent, subkey, subtopic, subpercent, data, done): if self._currentThread() != self._mainthread: # Note: it's NOT safe to use Qt from threads other than main return qt.QDialog.show(self) if not self._beenshown: centerWindow(self) self._beenshown = True self.raiseW() if self._hassub and subkey: if subkey in self._subiters: iter = self._subiters[subkey] else: iter = qt.QListViewItem(self._listview) self._subiters[subkey] = iter self._listview.ensureItemVisible(iter) current = data.get("current", "") if current: self._listview.setColumnWidth(self._currentcolumn, 110) total = data.get("total", "") if total: self._listview.setColumnWidth(self._totalcolumn, 110) if done: speed = "" eta = "" subpercent = 100 else: speed = data.get("speed", "") if speed: self._listview.setColumnWidth(self._speedcolumn, 110) eta = data.get("eta", "") if eta: self._listview.setColumnWidth(self._etacolumn, 110) if current or total or speed or eta: iter.setText(1, current) iter.setText(2, total) iter.setText(3, speed) iter.setText(4, eta) subtopic = self._shorturl.get(subtopic) iter.setText(0, str(subpercent) + "%") iter.setText(5, subtopic) iter.widthChanged(self._desccolumn) self._listview.insertItem(iter) else: self._topic.setText('<b>'+topic+'</b>') self._progressbar.setProgress(percent, 100) if self._hassub: self._listview.update() while qt.QApplication.eventLoop().hasPendingEvents(): qt.QApplication.eventLoop().processEvents(qt.QEventLoop.AllEvents)
class QtProgress(Progress, qt.QDialog): def __init__(self, hassub, parent=None): Progress.__init__(self) qt.QDialog.__init__(self, parent) self._hassub = hassub self._shorturl = ShortURL(50) self._ticking = False self._stopticking = False self._fetcher = None self._beenshown = False self._mainthread = None if hassub: self.setMinimumSize(500, 400) else: self.setMinimumSize(300, 80) self.setIcon(getPixmap("smart")) self.setCaption(_("Operation Progress")) vbox = qt.QVBoxLayout(self, 10, 10) vbox.setResizeMode(qt.QLayout.FreeResize) vbox.setMargin(10) vbox.setSpacing(10) self._topic = qt.QLabel(self) vbox.addWidget(self._topic) self._progressbar = qt.QProgressBar(self) self._progressbar.setPercentageVisible(True) self._progressbar.show() vbox.addWidget(self._progressbar) if hassub: self._listview = qt.QListView(self) self._listview.setSorting(-1, False) self._listview.setSelectionMode(qt.QListView.NoSelection) self._listview.show() vbox.addWidget(self._listview) column = self._listview.addColumn(_("Progress")) self._listview.setColumnWidthMode(column, qt.QListView.Manual) self._listview.setColumnWidth(column, 55) column = self._listview.addColumn(_("Current")) self._currentcolumn = column column = self._listview.addColumn(_("Total")) self._totalcolumn = column column = self._listview.addColumn(_("Speed")) self._speedcolumn = column column = self._listview.addColumn(_("ETA")) self._etacolumn = column column = self._listview.addColumn(_("Description")) self._listview.setColumnWidthMode(column, qt.QListView.Manual) self._listview.setColumnWidth(column, 165) self._desccolumn = column self._subiters = {} self._subindex = 0 self._bbox = qt.QHBox(self) self._bbox.setSpacing(10) self._bbox.layout().addStretch(1) vbox.addWidget(self._bbox) button = qt.QPushButton(_("Cancel"), self._bbox) button.show() qt.QObject.connect(button, qt.SIGNAL("clicked()"), self._cancel) def setFetcher(self, fetcher): if fetcher: self._bbox.show() self._fetcher = fetcher else: self._bbox.hide() self._fetcher = None def _cancel(self): if self._fetcher: self._fetcher.cancel() def setMainThread(self, main): self._mainthread = main def tick(self): while not self._stopticking: self.lock() ## Note: it's NOT safe to call processEvents from threads other than main #while qt.QApplication.eventLoop().hasPendingEvents(): # qt.QApplication.eventLoop().processEvents(qt.QEventLoop.AllEvents) self.unlock() time.sleep(INTERVAL) self._ticking = False def start(self): Progress.start(self) self.setHasSub(self._hassub) self._ticking = True self._stopticking = False if self._hassub: self._listview.hideColumn(self._currentcolumn) self._listview.hideColumn(self._totalcolumn) self._listview.hideColumn(self._speedcolumn) self._listview.hideColumn(self._etacolumn) thread.start_new_thread(self.tick, ()) def stop(self): self._stopticking = True while self._ticking: pass Progress.stop(self) if self._hassub: self._listview.clear() self._subiters.clear() self._subindex = 0 self._shorturl.reset() qt.QDialog.hide(self) def _currentThread(self): if hasattr(qt, 'QThread'): return qt.QThread.currentThread() else: return None def expose(self, topic, percent, subkey, subtopic, subpercent, data, done): if self._currentThread() != self._mainthread: # Note: it's NOT safe to use Qt from threads other than main return qt.QDialog.show(self) if not self._beenshown: centerWindow(self) self._beenshown = True self.raiseW() if self._hassub and subkey: if subkey in self._subiters: iter = self._subiters[subkey] else: iter = qt.QListViewItem(self._listview) self._subiters[subkey] = iter self._listview.ensureItemVisible(iter) current = data.get("current", "") if current: self._listview.setColumnWidth(self._currentcolumn, 110) total = data.get("total", "") if total: self._listview.setColumnWidth(self._totalcolumn, 110) if done: speed = "" eta = "" subpercent = 100 else: speed = data.get("speed", "") if speed: self._listview.setColumnWidth(self._speedcolumn, 110) eta = data.get("eta", "") if eta: self._listview.setColumnWidth(self._etacolumn, 110) if current or total or speed or eta: iter.setText(1, current) iter.setText(2, total) iter.setText(3, speed) iter.setText(4, eta) subtopic = self._shorturl.get(subtopic) iter.setText(0, str(subpercent) + "%") iter.setText(5, subtopic) iter.widthChanged(self._desccolumn) self._listview.insertItem(iter) else: self._topic.setText('<b>' + topic + '</b>') self._progressbar.setProgress(percent, 100) if self._hassub: self._listview.update() while qt.QApplication.eventLoop().hasPendingEvents(): qt.QApplication.eventLoop().processEvents(qt.QEventLoop.AllEvents)
class QtProgress(Progress, QtGui.QDialog): def __init__(self, hassub, parent=None): Progress.__init__(self) QtGui.QDialog.__init__(self, parent) self._hassub = hassub self._shorturl = ShortURL(50) self._ticking = False self._stopticking = False self._fetcher = None self._beenshown = False self._mainthread = None if hassub: self.setMinimumSize(500, 400) else: self.setMinimumSize(300, 80) self.setWindowIcon(QtGui.QIcon(getPixmap("smart"))) self.setWindowTitle(_("Operation Progress")) vbox = QtGui.QVBoxLayout(self) #vbox.setResizeMode(QtGui.QLayout.FreeResize) vbox.setMargin(10) vbox.setSpacing(10) self._topic = QtGui.QLabel(self) vbox.addWidget(self._topic) self._progressbar = QtGui.QProgressBar(self) #self._progressbar.setPercentageVisible(True) self._progressbar.show() vbox.addWidget(self._progressbar) if hassub: self._listview = QtGui.QTableWidget(self) #self._listview.setSorting(-1, False); self._listview.setSelectionMode(QtGui.QTableView.NoSelection ) self._listview.show() vbox.addWidget(self._listview) #column = self._listview.addColumn(_("Progress")) #self._listview.setColumnWidthMode(column, QtGui.QTableView.Manual) #self._listview.setColumnWidth(column, 55) #column = self._listview.addColumn(_("Current")) #self._currentcolumn = column #column = self._listview.addColumn(_("Total")) #self._totalcolumn = column #column = self._listview.addColumn(_("Speed")) #self._speedcolumn = column #column = self._listview.addColumn(_("ETA")) #self._etacolumn = column #column = self._listview.addColumn(_("Description")) #self._listview.setColumnWidthMode(column, QtGui.QTableView.Manual) #self._listview.setColumnWidth(column, 165) #self._desccolumn = column self._listview.setHorizontalHeaderLabels([_("Progress"), _("Current"), _("Total"), _("Speed"), _("ETA"), _("Description")]) self._subiters = {} self._subindex = 0 self._bbox = QtGui.QWidget(self) QtGui.QHBoxLayout(self._bbox) self._bbox.layout().setSpacing(10) self._bbox.layout().addStretch(1) vbox.addWidget(self._bbox) button = QtGui.QPushButton(_("Cancel"), self._bbox) button.show() QtCore.QObject.connect(button, QtCore.SIGNAL("clicked()"), self._cancel) self._bbox.layout().addWidget(button) def setFetcher(self, fetcher): if fetcher: self._bbox.show() self._fetcher = fetcher else: self._bbox.hide() self._fetcher = None def _cancel(self): if self._fetcher: self._fetcher.cancel() def setMainThread(self, main): self._mainthread = main def tick(self): while not self._stopticking: self.lock() ## Note: it's NOT safe to call processEvents from threads other than main #while QtGui.QCoreApplication.instance().hasPendingEvents(): # QtGui.QCoreApplication.instance().processEvents() self.unlock() time.sleep(INTERVAL) self._ticking = False def start(self): Progress.start(self) self.setHasSub(self._hassub) self._ticking = True self._stopticking = False if self._hassub: #self._listview.hideColumn(self._currentcolumn) #self._listview.hideColumn(self._totalcolumn) #self._listview.hideColumn(self._speedcolumn) #self._listview.hideColumn(self._etacolumn) pass thread.start_new_thread(self.tick, ()) def stop(self): self._stopticking = True while self._ticking: pass Progress.stop(self) if self._hassub: self._listview.clear() self._subiters.clear() self._subindex = 0 self._shorturl.reset() QtGui.QDialog.hide(self) def _currentThread(self): return QtCore.QThread.currentThread() def expose(self, topic, percent, subkey, subtopic, subpercent, data, done): if self._currentThread() != self._mainthread: # Note: it's NOT safe to use Qt from threads other than main return QtGui.QDialog.show(self) if not self._beenshown: centerWindow(self) self._beenshown = True self.raise_() if self._hassub and subkey: if subkey in self._subiters: iter = self._subiters[subkey] else: row = self._listview.rowCount() self._listview.insertRow(row) iter = [] iter.append(QtGui.QTableWidgetItem()) self._listview.setItem(row, 0, iter[0]) iter.append(QtGui.QTableWidgetItem()) self._listview.setItem(row, 1, iter[1]) iter.append(QtGui.QTableWidgetItem()) self._listview.setItem(row, 2, iter[2]) iter.append(QtGui.QTableWidgetItem()) self._listview.setItem(row, 3, iter[3]) iter.append(QtGui.QTableWidgetItem()) self._listview.setItem(row, 4, iter[4]) iter.append(QtGui.QTableWidgetItem()) self._listview.setItem(row, 5, iter[5]) self._subiters[subkey] = iter #self._listview.ensureItemVisible(iter) current = data.get("current", "") if current: self._listview.setColumnWidth(self._currentcolumn, 110) total = data.get("total", "") if total: self._listview.setColumnWidth(self._totalcolumn, 110) if done: speed = "" eta = "" subpercent = 100 else: speed = data.get("speed", "") if speed: self._listview.setColumnWidth(self._speedcolumn, 110) eta = data.get("eta", "") if eta: self._listview.setColumnWidth(self._etacolumn, 110) if current or total or speed or eta: iter[1].setText(current) iter[2].setText(total) iter[3].setText(speed) iter[4].setText(eta) subtopic = self._shorturl.get(subtopic) iter[0].setText(str(subpercent) + "%") iter[5].setText(subtopic) #iter.widthChanged(self._desccolumn) else: self._topic.setText('<b>'+topic+'</b>') self._progressbar.setValue(percent) if self._hassub: self._listview.update() while QtGui.QCoreApplication.instance().hasPendingEvents(): QtGui.QCoreApplication.instance().processEvents()
class QtProgress(Progress, QtGui.QDialog): def __init__(self, hassub, parent=None): Progress.__init__(self) QtGui.QDialog.__init__(self, parent) self._hassub = hassub self._shorturl = ShortURL(50) self._ticking = False self._stopticking = False self._fetcher = None self._beenshown = False self._mainthread = None if hassub: self.setMinimumSize(500, 400) else: self.setMinimumSize(300, 80) self.setWindowIcon(QtGui.QIcon(getPixmap("smart"))) self.setWindowTitle(_("Operation Progress")) vbox = QtGui.QVBoxLayout(self) #vbox.setResizeMode(QtGui.QLayout.FreeResize) vbox.setMargin(10) vbox.setSpacing(10) self._topic = QtGui.QLabel(self) vbox.addWidget(self._topic) self._progressbar = QtGui.QProgressBar(self) #self._progressbar.setPercentageVisible(True) self._progressbar.show() vbox.addWidget(self._progressbar) if hassub: self._listview = QtGui.QTableWidget(self) #self._listview.setSorting(-1, False); self._listview.setSelectionMode(QtGui.QTableView.NoSelection) self._listview.show() vbox.addWidget(self._listview) #column = self._listview.addColumn(_("Progress")) #self._listview.setColumnWidthMode(column, QtGui.QTableView.Manual) #self._listview.setColumnWidth(column, 55) #column = self._listview.addColumn(_("Current")) #self._currentcolumn = column #column = self._listview.addColumn(_("Total")) #self._totalcolumn = column #column = self._listview.addColumn(_("Speed")) #self._speedcolumn = column #column = self._listview.addColumn(_("ETA")) #self._etacolumn = column #column = self._listview.addColumn(_("Description")) #self._listview.setColumnWidthMode(column, QtGui.QTableView.Manual) #self._listview.setColumnWidth(column, 165) #self._desccolumn = column self._listview.setHorizontalHeaderLabels([ _("Progress"), _("Current"), _("Total"), _("Speed"), _("ETA"), _("Description") ]) self._subiters = {} self._subindex = 0 self._bbox = QtGui.QWidget(self) QtGui.QHBoxLayout(self._bbox) self._bbox.layout().setSpacing(10) self._bbox.layout().addStretch(1) vbox.addWidget(self._bbox) button = QtGui.QPushButton(_("Cancel"), self._bbox) button.show() QtCore.QObject.connect(button, QtCore.SIGNAL("clicked()"), self._cancel) self._bbox.layout().addWidget(button) def setFetcher(self, fetcher): if fetcher: self._bbox.show() self._fetcher = fetcher else: self._bbox.hide() self._fetcher = None def _cancel(self): if self._fetcher: self._fetcher.cancel() def setMainThread(self, main): self._mainthread = main def tick(self): while not self._stopticking: self.lock() ## Note: it's NOT safe to call processEvents from threads other than main #while QtGui.QApplication.instance().hasPendingEvents(): # QtGui.QApplication.instance().processEvents() self.unlock() time.sleep(INTERVAL) self._ticking = False def start(self): Progress.start(self) self.setHasSub(self._hassub) self._ticking = True self._stopticking = False if self._hassub: #self._listview.hideColumn(self._currentcolumn) #self._listview.hideColumn(self._totalcolumn) #self._listview.hideColumn(self._speedcolumn) #self._listview.hideColumn(self._etacolumn) pass thread.start_new_thread(self.tick, ()) def stop(self): self._stopticking = True while self._ticking: pass Progress.stop(self) if self._hassub: self._listview.clear() self._subiters.clear() self._subindex = 0 self._shorturl.reset() QtGui.QDialog.hide(self) def _currentThread(self): return QtCore.QThread.currentThread() def expose(self, topic, percent, subkey, subtopic, subpercent, data, done): if self._currentThread() != self._mainthread: # Note: it's NOT safe to use Qt from threads other than main return QtGui.QDialog.show(self) if not self._beenshown: centerWindow(self) self._beenshown = True self.raise_() if self._hassub and subkey: if subkey in self._subiters: iter = self._subiters[subkey] else: row = self._listview.rowCount() self._listview.insertRow(row) iter = [] iter.append(QtGui.QTableWidgetItem()) self._listview.setItem(row, 0, iter[0]) iter.append(QtGui.QTableWidgetItem()) self._listview.setItem(row, 1, iter[1]) iter.append(QtGui.QTableWidgetItem()) self._listview.setItem(row, 2, iter[2]) iter.append(QtGui.QTableWidgetItem()) self._listview.setItem(row, 3, iter[3]) iter.append(QtGui.QTableWidgetItem()) self._listview.setItem(row, 4, iter[4]) iter.append(QtGui.QTableWidgetItem()) self._listview.setItem(row, 5, iter[5]) self._subiters[subkey] = iter #self._listview.ensureItemVisible(iter) current = data.get("current", "") if current: self._listview.setColumnWidth(self._currentcolumn, 110) total = data.get("total", "") if total: self._listview.setColumnWidth(self._totalcolumn, 110) if done: speed = "" eta = "" subpercent = 100 else: speed = data.get("speed", "") if speed: self._listview.setColumnWidth(self._speedcolumn, 110) eta = data.get("eta", "") if eta: self._listview.setColumnWidth(self._etacolumn, 110) if current or total or speed or eta: iter[1].setText(current) iter[2].setText(total) iter[3].setText(speed) iter[4].setText(eta) subtopic = self._shorturl.get(subtopic) iter[0].setText(str(subpercent) + "%") iter[5].setText(subtopic) #iter.widthChanged(self._desccolumn) else: self._topic.setText('<b>' + topic + '</b>') self._progressbar.setValue(percent) if self._hassub: self._listview.update() while QtGui.QApplication.instance().hasPendingEvents(): QtGui.QApplication.instance().processEvents()