Switched to QProgressDialog + added batched rotations
authorDmitriy Morozov <morozov@cs.duke.edu>
Sat, 29 Mar 2008 11:20:24 -0400
changeset 4 3e59316763c8
parent 3 f8daad0248d8
child 5 cf32356b1020
Switched to QProgressDialog + added batched rotations
prefetch.ui
pyqflickr.py
--- a/prefetch.ui	Sat Mar 29 09:11:51 2008 -0400
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,46 +0,0 @@
-<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 09:11:51 2008 -0400
+++ b/pyqflickr.py	Sat Mar 29 11:20:24 2008 -0400
@@ -4,7 +4,6 @@
 from PyQt4 import Qt, QtCore, QtGui
 
 from ui_mainwin import Ui_MainWindow
-from ui_prefetch import Ui_prefetchDialog
 import flickr, os
 
 
@@ -22,33 +21,16 @@
         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)
+class ProgressDialog(QtGui.QProgressDialog):
+    def __init__(self, text, max, cancelButtonText = 'Cancel'):
+        QtGui.QProgressDialog.__init__(self, text, cancelButtonText, 0, max)
+        self.canceled = False
+        self.setValue(0)
+        self.setMinimumDuration(0)
+        self.connect(self, QtCore.SIGNAL("canceled()"), self.cancel)
 
     def cancel(self):
-        self.cancelled = True
-
-    def fetch(self):
-        self.show()
-        i = 0
-        for p in self.photos:
-            self.dialog.progressBar.setValue(i)
-            p.get(working_path, photo_size)
-            p.getInfo()
-            i = i + 1
-            QtGui.QApplication.processEvents()
-            if self.cancelled: break
-        self.hide()
+        self.canceled = True
 
 # Main window
 class PyQFlickr(QtGui.QMainWindow):
@@ -58,6 +40,16 @@
         # In set mode?
         if self.photo == None:
             if key == QtCore.Qt.Key_Up: 
+                progress = ProgressDialog("Rotating photos", len(self.torotate))
+                i = 0
+                for index,degree in self.torotate:
+                    progress.setValue(i)
+                    self.photos[index].rotate(degree)
+                    i += 1
+                    QtGui.QApplication.processEvents()
+                    if progress.canceled: break
+
+                self.torotate = []
                 self.photos = None
                 self.showSets()
             return
@@ -73,11 +65,9 @@
             self.photo = None
             self.showSet()
         elif key == QtCore.Qt.Key_R:
-            self.photos[self.photo].rotate(90)
-            self.showPhoto(force = True)
+            self.torotate.append((self.photo, 90))
         elif key == QtCore.Qt.Key_W:
-            self.photos[self.photo].rotate(270)
-            self.showPhoto(force = True)
+            self.torotate.append((self.photo, 270))
         elif key == QtCore.Qt.Key_P:
             self.photos[self.photo].toggle_public()
             self.showPhoto()
@@ -86,21 +76,28 @@
         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>' % \
+            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>'
+            if i % 6 == 0: txt += '</tr><tr>'
             i += 1
-        txt = txt + '</tr></table>'
+        txt += '</tr></table>'
         self.ui.interfaceBrowser.setHtml(txt)
 
     def showSet(self):
+        progress = ProgressDialog("Loading thumbnails", len(self.photos))
         txt = '<a href="fetchall">Pre-fetch all photos (with info)</a><br>'
         i = 1
         for p in self.photos:
-            txt = txt + '<td align="center"><a href="%i"><img src="%s"></a></td>' % \
+            txt += '<td align="center"><a href="%i"><img src="%s"></a></td>' % \
                          (i-1, p.get(working_path, 's'))
+            progress.setValue(i)
+            QtGui.QApplication.processEvents()
             if i % 10 == 0: txt += '</tr><tr>'
             i += 1
+            if progress.canceled:
+                self.photos = None
+                self.showSets()
+                return
         txt += '</tr></table>'
         self.ui.interfaceBrowser.setHtml(txt)
 
@@ -113,7 +110,7 @@
             if id:
                 info_txt += ('<a href="%s">X</a>' % id)
             info_txt += '<br>'
-        info_txt = info_txt + '<br>'
+        info_txt += '<br>'
         if photo.is_public():   info_txt += "Public"
         else:                   info_txt += "Private"
 
@@ -135,8 +132,15 @@
         self.ui.interfaceBrowser.setHtml(txt)
 
     def prefetchAll(self):
-        prefetcher = Prefetcher(self.photos)
-        prefetcher.fetch()
+        progress = ProgressDialog("Prefetching photos", len(self.photos))
+        i = 0
+        for p in self.photos:
+            progress.setValue(i)
+            p.get(working_path, photo_size)
+            p.getInfo()
+            i += 1
+            QtGui.QApplication.processEvents()
+            if progress.canceled: break
 
     def anchorClicked(self, url):
         if self.photos == None:         # In sets mode
@@ -151,6 +155,7 @@
                 self.prefetchAll()
         else:                           # In photo mode
             self.photos[self.photo].removeTag(url.toString())
+            self.showPhoto()
 
     def message(self, msg):
         QtGui.QMessageBox.warning(None, "Warning", msg)
@@ -190,6 +195,7 @@
         self.tags = flickr.tags().values()
         self.sets = flickr.photosets()
         self.showSets()
+        self.torotate = []
         
         # Tag completer
         self.completions = QtGui.QStringListModel(QtCore.QStringList(self.tags))