Auto-select the longest title
authorJames Bunton <jamesbunton@delx.net.au>
Sat, 16 Feb 2013 23:33:28 +0000 (10:33 +1100)
committerJames Bunton <jamesbunton@delx.net.au>
Sat, 16 Feb 2013 23:33:28 +0000 (10:33 +1100)
bluplayer.py

index 9259b83..15f6c97 100755 (executable)
@@ -126,6 +126,7 @@ class MakeMkvCon(object):
 
 class MakeMkv(QObject):
        title_loaded = pyqtSignal(Title)
+       title_load_complete = pyqtSignal()
        status = pyqtSignal(str)
        fatal_error = pyqtSignal(str)
 
@@ -170,6 +171,7 @@ class MakeMkv(QObject):
                        title_page = grab_dict(title_list_page["title%d" % i])
                        title = Title(title_page)
                        self.title_loaded.emit(title)
+               self.title_load_complete.emit()
 
        def run(self):
                logging.info("MakeMKV thread started")
@@ -222,13 +224,22 @@ class PlayerWindow(QWidget):
                self.setWindowTitle("BluPlayer")
 
        def add_title(self, title):
-               name = "Title %s (%s)" % (title.id, title.duration)
+               name = "Title %s (%s)" % (int(title.id)+1, title.duration)
                self.list_widget.addItem(name)
-               if not self.title_map:
-                       # select the first item
-                       self.list_widget.setCurrentItem(self.list_widget.item(0))
                self.title_map[name] = title
 
+       def select_longest_title(self):
+               longest_title = None
+               longest_item = None
+               for i in xrange(self.list_widget.count()):
+                       item = self.list_widget.item(i)
+                       name = str(item.text())
+                       title = self.title_map[name]
+                       if longest_title is None or title.duration > longest_title.duration:
+                               longest_title = title
+                               longest_item = item
+               self.list_widget.setCurrentItem(longest_item)
+
        def add_log_entry(self, text):
                self.log.append(text)
 
@@ -276,6 +287,7 @@ def main():
 
        worker_thread.started.connect(makemkv.run)
        makemkv.title_loaded.connect(player_window.add_title)
+       makemkv.title_load_complete.connect(player_window.select_longest_title)
        makemkv.status.connect(player_window.add_log_entry)
        makemkv.fatal_error.connect(player_window.popup_fatal_error)