def initModel(self):
     fid = self.__obj.fid
     dataProvider = self.__obj.qgsMapLayer.dataProvider()
     if dataProvider.name() == u'WFS':
         if hasattr(dataProvider, 'idFromFid') and callable(getattr(dataProvider, 'idFromFid')):
             fid = dataProvider.idFromFid(fid)
             if type(fid) != 'long':
                 fid = long(fid)
     
     self.__ngw_feature = NGWFeature(fid, self.__ngw_resource)
     self.__images_urls = []
     
     attachments = self.__ngw_feature.get_attachments()
     for attachment in attachments:
         if attachment[u'is_image'] == True:
             self.insertRow(NGWAttachment(attachment[u'id'], self.__ngw_feature))
     
     self.initEnded.emit()
class NGWImagesModel(QtCore.QAbstractListModel):
    initEnded = QtCore.pyqtSignal()    
    def __init__(self, obj, ngw_resource, parent = None):
        super(NGWImagesModel, self).__init__(parent)
        
        self.__obj = obj
        self.__ngw_resource = ngw_resource
        self.__images = []
        
        self.__thread = QtCore.QThread(self)
        self.moveToThread(self.__thread)
        self.__thread.started.connect(self.initModel)
        self.initEnded.connect(self.__thread.quit)
        self.__thread.start()
    
    def initModel(self):
        fid = self.__obj.fid
        dataProvider = self.__obj.qgsMapLayer.dataProvider()
        if dataProvider.name() == u'WFS':
            if hasattr(dataProvider, 'idFromFid') and callable(getattr(dataProvider, 'idFromFid')):
                fid = dataProvider.idFromFid(fid)
                if type(fid) != 'long':
                    fid = long(fid)
        
        self.__ngw_feature = NGWFeature(fid, self.__ngw_resource)
        self.__images_urls = []
        
        attachments = self.__ngw_feature.get_attachments()
        for attachment in attachments:
            if attachment[u'is_image'] == True:
                self.insertRow(NGWAttachment(attachment[u'id'], self.__ngw_feature))
        
        self.initEnded.emit()
          
    def rowCount(self, parent=QtCore.QModelIndex()):        
        return len( self.__images )
    
    def removeRows(self, row, count, parent=QtCore.QModelIndex()):
        self.beginRemoveRows(parent, row, row + count)
        
        for i in range(0, count):
            #self.__ngw_feature.unlink_attachment( self.__images_urls[row][1] )
            #self.__images_urls.remove(self.__images_urls[row])
            self.__images[row].unlink()
            self.__images.remove(self.__images[row])
            
        self.endRemoveRows()
        return True
    
    def addImage(self, image_filename):
        uploaded_file_info = self.__ngw_feature.ngw_resource._res_factory.connection.upload_file(image_filename)
        id = self.__ngw_feature.link_attachment(uploaded_file_info)
        self.insertRow(NGWAttachment(id, self.__ngw_feature))
    
    def insertRow(self, ngw_attachment):
        self.beginInsertRows(QtCore.QModelIndex(), self.rowCount(), self.rowCount())
        self.__images.append(ngw_attachment)
        self.endInsertRows()
        
    def data(self, index, role=QtCore.Qt.DisplayRole):        
        if index.isValid() and role == QtCore.Qt.DecorationRole:
            return None
        
        elif index.isValid() and role == QtCore.Qt.DisplayRole:          
            return self.__images[index.row()]
                
        elif index.isValid() and role == (QtCore.Qt.UserRole + 1):
            return self.__images[index.row()]
        
        else:
            return None