mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
When picking a random book ensure recently chosen books are not re-selected. Fixes #1948889 ["Pick a random book" function not random](https://bugs.launchpad.net/calibre/+bug/1948889)
This commit is contained in:
parent
2f6ad90635
commit
9e1e87371f
@ -20,15 +20,29 @@ class PickRandomAction(InterfaceAction):
|
||||
|
||||
def genesis(self):
|
||||
self.qaction.triggered.connect(self.pick_random)
|
||||
self.recently_picked = {}
|
||||
|
||||
def location_selected(self, loc):
|
||||
enabled = loc == 'library'
|
||||
self.qaction.setEnabled(enabled)
|
||||
self.menuless_qaction.setEnabled(enabled)
|
||||
|
||||
def library_changed(self, db):
|
||||
self.recently_picked = {}
|
||||
|
||||
def pick_random(self):
|
||||
pick = random.randint(0, self.gui.library_view.model().rowCount(None))
|
||||
self.gui.library_view.set_current_row(pick)
|
||||
self.gui.library_view.scroll_to_row(pick)
|
||||
|
||||
|
||||
lv = self.gui.library_view
|
||||
count = lv.model().rowCount(None)
|
||||
rp = self.recently_picked
|
||||
while len(rp) > count // 2:
|
||||
n = next(iter(rp))
|
||||
del rp[n]
|
||||
while True:
|
||||
pick = random.randint(0, count)
|
||||
if pick in rp:
|
||||
continue
|
||||
rp.pop(pick, None)
|
||||
rp[pick] = True
|
||||
break
|
||||
lv.set_current_row(pick)
|
||||
lv.scroll_to_row(pick)
|
||||
|
Loading…
x
Reference in New Issue
Block a user