def dropEvent(self, event: QDropEvent): mimedata = event.mimeData() data = QByteArray(mimedata.data("application/x_nodepath.list")) stream = QDataStream(data, QIODevice.ReadOnly) while not stream.atEnd(): path = stream.readQString() self.parent().add_plot(path) event.acceptProposedAction()
def fromfile(cls, path): """Read a bloom filter from file-object `f' serialized with ``BloomFilter.tofile''. """ f = QFile(path) if not f.open(QIODevice.ReadOnly): raise ValueError("unable to open file " + path) data = QDataStream(f) file_fmt = data.readBytes() if file_fmt != cls.FILE_FMT: raise ValueError('unexpected file format') error_rate = data.readFloat() num_slices = data.readInt() bits_per_slice = data.readInt() capacity = data.readInt() count = data.readInt() bitarray = QBitArray() filter = cls(1) # Bogus instantiation, we will `_setup'. filter._setup(error_rate, num_slices, bits_per_slice, capacity, count) filter.bitarray = QBitArray() data >> filter.bitarray return filter
def mimeData(self, indexes): def path(x): return "/".join(x.path) def node(x): return self.node_from_index(x) mimedata = QMimeData() data = QByteArray() stream = QDataStream(data, QIODevice.WriteOnly) for path in set(path(node(index)) for index in indexes if index.isValid()): stream.writeQString(path) mimedata.setData("application/x_nodepath.list", data) return mimedata
def tofile(self, path): """Write the bloom filter to file object `f'. Underlying bits are written as machine values. This is much more space efficient than pickling the object.""" # f.write(pack(self.FILE_FMT, self.error_rate, self.num_slices, # self.bits_per_slice, self.capacity, self.count)) # f.write(self.bitarray.bits) f = QFile(path) if f.open(QIODevice.WriteOnly): out = QDataStream(f) out.writeBytes(self.FILE_FMT) out.writeFloat(self.error_rate) out.writeInt(self.num_slices) out.writeInt(self.bits_per_slice) out.writeInt(self.capacity) out.writeInt(self.count) out << self.bitarray f.flush() f.close()