コード例 #1
0
    def addImage(self, url, tags, path):
        '''
        Adds an image with url and tags that has already been downloaded
        to path to the model. path can be absolute or relative to self.troll_dir
        '''
        print "addImage url", url

        # set i to index of item in self.images with url, if it exists
        found = False
        for i in range(0, len(self.images)):
            if self.images[i][0] == url:
                print "%s exists at %d/%d" % (url, i, len(self.images))
                found = True
                break

        # if path is absolute, caculate path relative to troll_dir
        # and vice versa
        if os.path.isabs(path):
            abs_path = path
            rel_path = path[len(self.troll_dir) + 1:]
            print "abs_path", abs_path, "rel_path", rel_path
        else:
            abs_path = os.path.join(self.troll_dir, path)
            rel_path = path
        icon = QIcon(abs_path)
        expanded_tags = expand_tags(tags)

        if not found:
            # if this is a new image, append it
            i = self.rowCount()
            self.beginInsertRows(self.index(i), i, i)
            print "new image"
            self.images.append(
                (url, tags, rel_path, abs_path, icon, expanded_tags))
            self.save()
            self.endInsertRows()
        else:
            # otherwise replace the old image and delete its disk file
            print "replacing image at %d" % i
            if abs_path != self.images[i][3]:
                os.remove(self.images[i][3])
            self.images[i] = (url, tags, rel_path, abs_path, icon,
                              expanded_tags)
            self.save()
            qmi = self.index(i)
            self.dataChanged.emit(qmi, qmi)

        self.imageAdded.emit(i)
コード例 #2
0
 def setData(self, qmi, value, role=Qt.DisplayRole):
     """
     Sets the role data for image stored at index to value
     """
     index = qmi.row()
     url, tags, rel_path, abs_path, icon, expanded_tags = self.images[index]
     print "value", value
     if role == Qt.DisplayRole:
         url = value
     elif role == self.TagRole:
         tags = value
         expanded_tags = expand_tags(tags)
     self.images[index] = url, tags, rel_path, abs_path, icon, expanded_tags
     self.save()
     self.dataChanged.emit(qmi, qmi)
     return True
コード例 #3
0
ファイル: image_model.py プロジェクト: jfoote/trollbox
 def setData(self, qmi, value, role=Qt.DisplayRole):
     """
     Sets the role data for image stored at index to value
     """
     index = qmi.row()
     url, tags, rel_path, abs_path, icon, expanded_tags = self.images[index]
     print "value", value
     if role == Qt.DisplayRole:
         url = value
     elif role == self.TagRole:
         tags = value
         expanded_tags = expand_tags(tags)
     self.images[index] = url, tags, rel_path, abs_path, icon, expanded_tags
     self.save()
     self.dataChanged.emit(qmi, qmi)
     return True
コード例 #4
0
ファイル: image_model.py プロジェクト: jfoote/trollbox
    def addImage(self, url, tags, path):
        '''
        Adds an image with url and tags that has already been downloaded
        to path to the model. path can be absolute or relative to self.troll_dir
        '''
        print "addImage url", url 

        # set i to index of item in self.images with url, if it exists
        found = False
        for i in range(0, len(self.images)):
            if self.images[i][0] == url:
                print "%s exists at %d/%d" % (url, i, len(self.images))
                found = True
                break

        # if path is absolute, caculate path relative to troll_dir 
        # and vice versa
        if os.path.isabs(path):
            abs_path = path
            rel_path = path[len(self.troll_dir)+1:]
            print "abs_path", abs_path, "rel_path", rel_path
        else:
            abs_path = os.path.join(self.troll_dir, path)
            rel_path = path
        icon = QIcon(abs_path)
        expanded_tags = expand_tags(tags)

        if not found:
            # if this is a new image, append it
            i = self.rowCount()
            self.beginInsertRows(self.index(i), i, i)
            print "new image"
            self.images.append((url, tags, rel_path, abs_path, icon, expanded_tags))
            self.save()
            self.endInsertRows()
        else:
            # otherwise replace the old image and delete its disk file
            print "replacing image at %d" % i
            if abs_path != self.images[i][3]:
                os.remove(self.images[i][3])
            self.images[i] = (url, tags, rel_path, abs_path, icon, expanded_tags)
            self.save()
            qmi = self.index(i)
            self.dataChanged.emit(qmi, qmi)
        
        self.imageAdded.emit(i)