Added prefetcher and set view
authorDmitriy Morozov <morozov@cs.duke.edu>
Sat, 29 Mar 2008 08:28:24 -0400
changeset 2 b53ed7e259e5
parent 1 0683f22a97d5
child 3 f8daad0248d8
Added prefetcher and set view
mainwin.ui
prefetch.ui
pyqflickr.py
--- a/mainwin.ui	Sat Mar 29 07:06:33 2008 -0400
+++ b/mainwin.ui	Sat Mar 29 08:28:24 2008 -0400
@@ -10,12 +10,16 @@
    </rect>
   </property>
   <property name="windowTitle" >
-   <string>MainWindow</string>
+   <string>PyQFlickr</string>
   </property>
   <widget class="QWidget" name="centralwidget" >
    <layout class="QVBoxLayout" >
     <item>
-     <widget class="QTextBrowser" name="interfaceBrowser" />
+     <widget class="QTextBrowser" name="interfaceBrowser" >
+      <property name="focusPolicy" >
+       <enum>Qt::NoFocus</enum>
+      </property>
+     </widget>
     </item>
    </layout>
   </widget>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/prefetch.ui	Sat Mar 29 08:28:24 2008 -0400
@@ -0,0 +1,46 @@
+<ui version="4.0" >
+ <class>prefetchDialog</class>
+ <widget class="QDialog" name="prefetchDialog" >
+  <property name="geometry" >
+   <rect>
+    <x>0</x>
+    <y>0</y>
+    <width>361</width>
+    <height>79</height>
+   </rect>
+  </property>
+  <property name="sizePolicy" >
+   <sizepolicy vsizetype="Minimum" hsizetype="Minimum" >
+    <horstretch>0</horstretch>
+    <verstretch>0</verstretch>
+   </sizepolicy>
+  </property>
+  <property name="windowTitle" >
+   <string>Pre-fetch progress</string>
+  </property>
+  <layout class="QVBoxLayout" >
+   <item>
+    <widget class="QProgressBar" name="progressBar" >
+     <property name="sizePolicy" >
+      <sizepolicy vsizetype="Minimum" hsizetype="Minimum" >
+       <horstretch>0</horstretch>
+       <verstretch>0</verstretch>
+      </sizepolicy>
+     </property>
+     <property name="value" >
+      <number>24</number>
+     </property>
+    </widget>
+   </item>
+   <item>
+    <widget class="QPushButton" name="cancelButton" >
+     <property name="text" >
+      <string>Cancel</string>
+     </property>
+    </widget>
+   </item>
+  </layout>
+ </widget>
+ <resources/>
+ <connections/>
+</ui>
--- a/pyqflickr.py	Sat Mar 29 07:06:33 2008 -0400
+++ b/pyqflickr.py	Sat Mar 29 08:28:24 2008 -0400
@@ -4,6 +4,7 @@
 from PyQt4 import Qt, QtCore, QtGui
 
 from ui_mainwin import Ui_MainWindow
+from ui_prefetch import Ui_prefetchDialog
 import flickr, os
 
 
@@ -20,10 +21,49 @@
 
         return QtGui.QLineEdit.event(self, event)
 
+# Prefetch window
+class Prefetcher(QtGui.QWidget):
+    def __init__(self, photos, parent = None):
+        QtGui.QWidget.__init__(self, parent)
+        self.photos = photos
+        self.cancelled = False
+
+        self.dialog = Ui_prefetchDialog()
+        self.dialog.setupUi(self)
+        self.dialog.progressBar.setMaximum(len(self.photos))
+        self.dialog.progressBar.setValue(0)
+
+        self.connect(self.dialog.cancelButton, QtCore.SIGNAL("clicked()"), self.cancel)
+
+    def cancel(self):
+        self.cancelled = True
+
+    def fetch(self):
+        self.show()
+        i = 0
+        for p in self.photos:
+            print i
+            self.dialog.progressBar.setValue(i)
+            p.get(working_path, photo_size)
+            p.get_info()
+            i = i + 1
+            QtGui.QApplication.processEvents()
+            if self.cancelled: break
+        self.hide()
+
 # Main window
 class PyQFlickr(QtGui.QMainWindow):
     def shortcutPressed(self, key):
         if self.photos == None: return
+        
+        # In set mode?
+        if self.photo == None:
+            if key == QtCore.Qt.Key_Up: 
+                self.photos = None
+                self.showSets()
+            return
+
+        # In photo mode
         if   key == QtCore.Qt.Key_Right:
             self.photo = (self.photo + 1) % len(self.photos)
             self.showPhoto()
@@ -31,9 +71,8 @@
             self.photo = (self.photo - 1) % len(self.photos)
             self.showPhoto()
         elif key == QtCore.Qt.Key_Up:
-            self.photos = None
             self.photo = None
-            self.showSets()
+            self.showSet()
         elif key == QtCore.Qt.Key_R:
             self.photos[self.photo].rotate()
             self.showPhoto(force = True)
@@ -41,6 +80,28 @@
             self.photos[self.photo].toggle_public()
             self.showPhoto()
 
+    def showSets(self):
+        txt = '<table><tr>'
+        i = 1
+        for s in self.sets:
+            txt = txt + '<td width="120" align="center"><a href="%s"><img src="%s"><br>%s</a></td>' % \
+                         (s.id(), s.get_photo(working_path), s.name())
+            if i % 6 == 0: txt = txt + '</tr><tr>'
+            i += 1
+        txt = txt + '</tr></table>'
+        self.ui.interfaceBrowser.setHtml(txt)
+
+    def showSet(self):
+        txt = '<a href="fetchall">Pre-fetch all photos</a><br>'
+        i = 1
+        for p in self.photos:
+            txt = txt + '<td align="center"><a href="%i"><img src="%s"></a></td>' % \
+                         (i-1, p.get(working_path, 's'))
+            if i % 10 == 0: txt = txt + '</tr><tr>'
+            i += 1
+        txt = txt + '</tr></table>'
+        self.ui.interfaceBrowser.setHtml(txt)
+
     def showPhoto(self, force = False):
         photo = self.photos[self.photo]
         tags = photo.getTags()
@@ -56,33 +117,36 @@
                         <td margin="5" align="center">
                             <img src="%s">
                         </td>
-                        <td bgcolor="#CCCCCC">%s</td>
+                        <td bgcolor="#DDDDDD">%s</td>
+                    </tr>
+                    <tr>
+                    <td colspan="2">%i out of %i</td>
                     </tr>
                  </table>
                  </center>''' % \
-               (photo.get(working_path, photo_size, force), info_txt)
+               (photo.get(working_path, photo_size, force), info_txt,
+                self.photo + 1, len(self.photos))
         self.ui.interfaceBrowser.setHtml(txt)
 
+    def prefetchAll(self):
+        prefetcher = Prefetcher(self.photos)
+        prefetcher.fetch()
+
     def anchorClicked(self, url):
-        if self.photos == None:          # In set mode
+        if self.photos == None:         # In sets mode
             self.photos = flickr.photos(url.toString())     # url must be the setid
-            self.photo = 0
-            self.showPhoto()
+            self.photo = None
+            self.showSet()
+        elif self.photo == None:        # In photos mode
+            if url.toString() != 'fetchall':
+                self.photo = int(url.toString())
+                self.showPhoto()
+            else:
+                self.prefetchAll()
 
     def message(self, msg):
         QtGui.QMessageBox.warning(None, "Warning", msg)
 
-    def showSets(self):
-        txt = '<table><tr>'
-        i = 1
-        for s in self.sets:
-            txt = txt + '<td width="120" align="center"><a href="%s"><img src="%s"><br>%s</a></td>' % \
-                         (s.id(), s.get_photo(working_path), s.name())
-            if i % 8 == 0: txt = txt + '</tr><tr>'
-            i += 1
-        txt = txt + '</tr></table>'
-        self.ui.interfaceBrowser.setHtml(txt)
-
     def addTag(self):
         if self.photo == None: return       # Need to be in the photo editing mode
         tag = self.ui.tagEdit.text()