import threading
class AADownloadThread (threading.Thread):
- def __init__ (self, url, counter):
+ def __init__ (self, url, artist, album, counter):
threading.Thread.__init__ (self, target=self.grab_image, args=(url,))
self.thumbnailer = LocalThumbnailer ()
self.counter = counter
+ self.artistName = artist.replace (" ", "_")
+ self.albumName = album.replace (" ", "_")
self.image_path = None
self.thumb_path = None
self.urllib_wrapper = UrllibWrapper ()
print "Working", self.counter
image = self.urllib_wrapper.get_url (image_url)
if (image):
- self.image_path = os.path.join (CACHE_LOCATION, "alternative-" + str(self.counter))
- self.thumb_path = os.path.join (CACHE_LOCATION, "alternative-" + str(self.counter) + "thumb")
+ self.image_path = os.path.join (CACHE_LOCATION, self.artistName + self.albumName + str(self.counter))
+ self.thumb_path = os.path.join (CACHE_LOCATION, self.artistName + self.albumName + str(self.counter) + "thumb")
self.urllib_wrapper.save_content_into_file (image, self.image_path)
self.thumbnailer.create (self.image_path, self.thumb_path)
return a list of paths of possible album arts
"""
results_page = self.__msn_images (artist, album)
- return self.__process_results_page (results_page, max_alternatives)
+ return self.__process_results_page (results_page, artist, album, max_alternatives)
def get_alternatives_free_text (self, search_text, max_alternatives=4):
results_page = self.__msn_images_free_text (search_text)
return self.__process_results_page (results_page, max_alternatives)
- def __process_results_page (self, results_page, max_alternatives):
+ def __process_results_page (self, results_page, artist, album, max_alternatives):
counter = 0
threads = []
for image_url in self.__get_url_from_msn_results_page (results_page):
if (counter >= max_alternatives):
break
- t = AADownloadThread (image_url, counter)
+ t = AADownloadThread (image_url, artist, album, counter)
t.start ()
threads.append (t)
counter += 1
def save_alternative (self, artist, album, img_path, thumb_path):
+ """
+ This is done now in the controller
+ """
if not os.path.exists (img_path) or not os.path.exists (thumb_path):
print "**** CRITICAL **** image in path", path, "doesn't exist!"
return (None, None)
def _artist (self):
return self._artist
- def _album_art (self):
+ def _albumArt (self):
return self._album_art
- @QtCore.Signal
- def changed (self): pass
-
- title = QtCore.Property (unicode, _title, notify=changed)
- artist = QtCore.Property (unicode, _artist, notify=changed)
- album_art = QtCore.Property (unicode, _album_art, notify=changed)
+ def _setAlbumArt (self, path):
+ print "Setting the new album art to", path
+ self._album_art = path
+ self.album_art_changed.emit ()
+
+ prop_changed = QtCore.Signal ()
+ album_art_changed = QtCore.Signal ()
+
+ title = QtCore.Property (unicode, _title, notify=prop_changed)
+ artist = QtCore.Property (unicode, _artist, notify=prop_changed)
+ album_art = QtCore.Property (unicode,
+ _albumArt,
+ _setAlbumArt,
+ notify=album_art_changed)
if index.isValid () and role == AlbumModel.COLUMNS.index ('album'):
return self._albums[index.row ()]
return None
+
+ def updateThumb (self, row, url):
+ assert row >= 0 and row < len (self._albums)
+ self._albums[row].album_art = url
from tracker_backend_gi import TrackerBackendGI
from albumItem import AlbumItem
-from aa_spec import getCoverArtThumbFileName
+from aa_spec import getCoverArtThumbFileName, getCoverArtFileName
class DownloadThread (QtCore.QThread):
if counter >= MAX_OPTIONS:
break
- print counter, "Setting url", img
- self.model.updateData (counter, img)
+ self.model.updateData (counter, img, thumb)
counter += 1
self.download = DownloadThread (model, album)
self.download.start ()
+ @QtCore.Slot (QtCore.QObject, QtCore.QObject)
+ def albumSelected (self, model, album):
+ """
+ Starts a thread to look for possible images online.
+ The thread will update the model (and the changes are visible in the UI)
+ """
+ print "clicked on", album.title
+ self.download = DownloadThread (model, album)
+ self.download.start ()
+
+ @QtCore.Slot (str, QtCore.QObject)
+ def coverSelected (self, coverObject, albumObject):
+ """
+ The user has clicked in one cover!
+ """
+ print "Selected cover"
+ filename = getCoverArtFileName (albumObject.title)
+ thumbnail = getCoverArtThumbFileName (albumObject.title)
+
+ os.rename (cover.url, filename)
+ os.rename (cover.thumb, thumbnail)
+
+ albumObject.album_art = thumbnail
+ albumObject.album_art_changed.emit ()
+
+ @QtCore.Slot (QtCore.QObject)
+ def resetAlternatives (self, coversModel):
+ print "Reseting alternatives", coversModel
+ QtGui.QPixmapCache.clear ()
+ coversModel.resetAlternatives ()
+
def get_all_albums (self):
"""
class CoverItem (QtCore.QObject):
- def __init__ (self, url):
+ def __init__ (self, url, thumb):
QtCore.QObject.__init__(self)
self._url = url
-
+ self._thumb = thumb
+
def _url (self):
return self._url
self._url = url
self.url_changed.emit ()
+ def _thumb (self):
+ return self._thumb
+
+ def _setThumb (self, thumb):
+ self._thumb = thumb
+
url_changed = QtCore.Signal ()
url = QtCore.Property (unicode, _url, _setUrl, notify=url_changed)
+ thumb = QtCore.Property (unicode, _thumb, _setThumb)
def __init__ (self):
QtCore.QAbstractListModel.__init__ (self)
self._alternatives = [
- CoverItem ("images/button-red.png"),
- CoverItem ("images/button-blue.png"),
- CoverItem ("images/button-red.png"),
- CoverItem ("images/button-blue.png")
+ CoverItem ("images/button-red.png", None),
+ CoverItem ("images/button-blue.png", None),
+ CoverItem ("images/button-red.png", None),
+ CoverItem ("images/button-blue.png", None)
]
self.setRoleNames (dict(enumerate(CoversModel.COLUMNS)))
return self._alternatives[index.row ()]
return None
- def updateData (self, row, url):
+ def updateData (self, row, url, thumb):
assert row >= 0 and row < len (self._alternatives)
+ print "Replacing", self._alternatives[row].url, "with", url, "in row", row
self._alternatives[row].url = url
+ self._alternatives[row].thumb = thumb
+
+ def resetAlternatives (self):
+ for row in range (0, len (self._alternatives)):
+ print "Removing cached images:"
+ print " ", self._alternatives[row].url
+ print " ", self._alternatives[row].thumb
+ os.remove (self._alternatives[row].url)
+ os.remove (self._alternatives[row].thumb)
+ if (row % 2) == 0:
+ self.updateData(row, "images/button-red.png", None)
+ else:
+ self.updateData(row, "images/button-blue.png", None)
+ print "Ok"
Row {
id: coversAlternatives
- property alias model: imagesLoop.model
+ property alias altmodel: imagesLoop.model
+ property variant selectedAlbum
+ property variant mc
+
+ signal done
anchors.horizontalCenter : parent.horizontalCenter
anchors.verticalCenter: parent.verticalCenter
Repeater {
id: imagesLoop
- model: 5
delegate: Component {
ClickeableImage {
source: model.cover.url
width: 120
height: 120
- onClicked: { console.log (model.cover.url) }
+ onClicked: {
+ console.log (model.cover.url)
+ mc.coverSelected (model.cover, selectedAlbum)
+ mc.resetAlternatives (altmodel)
+ coversAlternatives.done ()
+ }
}
}
}
onRowSelected: {
screen.state = "in_alternativesPage"
missionControl.albumSelected (coversModel, album)
+ alternativesPage.selectedAlbum = album
}
}
Alternatives {
id:alternativesPage
- model: coversModel
+ altmodel: coversModel
+ mc: missionControl
+ onDone: {
+ screen.state = "in_albumsPage"
+ }
}
states: [