def saveState(self): """ Save state of the colomap into a QDataStream. :rtype: qt.QByteArray """ data = qt.QByteArray() stream = qt.QDataStream(data, qt.QIODevice.WriteOnly) stream.writeQString(self.__class__.__name__) stream.writeUInt32(self._SERIAL_VERSION) stream.writeQString(self.getName()) stream.writeBool(self.getVMin() is None) if self.getVMin() is not None: stream.writeQVariant(self.getVMin()) stream.writeBool(self.getVMax() is None) if self.getVMax() is not None: stream.writeQVariant(self.getVMax()) stream.writeQString(self.getNormalization()) if self.getNormalization() == Colormap.GAMMA: stream.writeFloat(self.getGammaNormalizationParameter()) stream.writeQString(self.getAutoscaleMode()) nanColor = self.getNaNColor() stream.writeInt32(nanColor.red()) stream.writeInt32(nanColor.green()) stream.writeInt32(nanColor.blue()) stream.writeInt32(nanColor.alpha()) return data
def restoreState(self, byteArray): """ Read the colormap state from a QByteArray. :param qt.QByteArray byteArray: Stream containing the state :return: True if the restoration sussseed :rtype: bool """ if self.isEditable() is False: raise NotEditableError('Colormap is not editable') stream = qt.QDataStream(byteArray, qt.QIODevice.ReadOnly) className = stream.readQString() if className != self.__class__.__name__: _logger.warning("Classname mismatch. Found %s." % className) return False version = stream.readUInt32() if version not in (1, self._SERIAL_VERSION): _logger.warning("Serial version mismatch. Found %d." % version) return False name = stream.readQString() isNull = stream.readBool() if not isNull: vmin = stream.readQVariant() else: vmin = None isNull = stream.readBool() if not isNull: vmax = stream.readQVariant() else: vmax = None normalization = stream.readQString() if version == 1: autoscaleMode = Colormap.MINMAX else: autoscaleMode = stream.readQString() # emit change event only once old = self.blockSignals(True) try: self.setName(name) self.setNormalization(normalization) self.setAutoscaleMode(autoscaleMode) self.setVRange(vmin, vmax) finally: self.blockSignals(old) self.sigChanged.emit() return True
def dropEvent(self, event): mimeData = event.mimeData() if not mimeData.hasFormat('application/FitModel'): return super(DropPlotWidget, self).dropEvent(event) qByteArray = mimeData.data('application/FitModel') stream = Qt.QDataStream(qByteArray, Qt.QIODevice.ReadOnly) h5File = stream.readQString() entry = stream.readQString() q_axis = stream.readInt() type = stream.readQString() if type == 'result': process = stream.readQString() result = stream.readQString() self.plotFitResult(h5File, entry, process, result, q_axis) elif type == 'status': self.plotFitStatus(h5File, entry, q_axis)
def saveState(self): """ Save state of the colomap into a QDataStream. :rtype: qt.QByteArray """ data = qt.QByteArray() stream = qt.QDataStream(data, qt.QIODevice.WriteOnly) stream.writeQString(self.__class__.__name__) stream.writeUInt32(self._SERIAL_VERSION) stream.writeQString(self.getName()) stream.writeBool(self.getVMin() is None) if self.getVMin() is not None: stream.writeQVariant(self.getVMin()) stream.writeBool(self.getVMax() is None) if self.getVMax() is not None: stream.writeQVariant(self.getVMax()) stream.writeQString(self.getNormalization()) return data
def saveConfig(self, ioDevice): """ Saves this view state. Only isosurfaces at the moment. Does not save the isosurface's function. :param qt.QIODevice ioDevice: A `qt.QIODevice`. """ stream = qt.QDataStream(ioDevice) stream.writeString('<ScalarFieldView>') isoSurfaces = self.getIsosurfaces() nIsoSurfaces = len(isoSurfaces) # TODO : delegate the serialization to the serialized items # isosurfaces if nIsoSurfaces: tagIn = '<IsoSurfaces nIso={0}>'.format(nIsoSurfaces) stream.writeString(tagIn) for surface in isoSurfaces: color = surface.getColor() level = surface.getLevel() visible = surface.isVisible() stream << color stream.writeDouble(level) stream.writeBool(visible) stream.writeString('</IsoSurfaces>') stream.writeString('<Style>') background = self.getBackgroundColor() foreground = self.getForegroundColor() highlight = self.getHighlightColor() stream << background << foreground << highlight stream.writeString('</Style>') stream.writeString('</ScalarFieldView>')
def loadConfig(self, ioDevice): """ Loads this view state. See ScalarFieldView.saveView to know what is supported at the moment. :param qt.QIODevice ioDevice: A `qt.QIODevice`. """ tagStack = deque() tagInRegex = re.compile('<(?P<itemId>[^ /]*) *' '(?P<args>.*)>') tagOutRegex = re.compile('</(?P<itemId>[^ ]*)>') tagRootInRegex = re.compile('<ScalarFieldView>') isoSurfaceArgsRegex = re.compile('nIso=(?P<nIso>[0-9]*)') stream = qt.QDataStream(ioDevice) tag = stream.readString() tagMatch = tagRootInRegex.match(tag) if tagMatch is None: # TODO : explicit error raise ValueError('Unknown data.') itemId = 'ScalarFieldView' tagStack.append(itemId) while True: tag = stream.readString() tagMatch = tagOutRegex.match(tag) if tagMatch: closeId = tagMatch.groupdict()['itemId'] if closeId != itemId: # TODO : explicit error raise ValueError('Unexpected closing tag {0} ' '(expected {1})' ''.format(closeId, itemId)) if itemId == 'ScalarFieldView': # reached end break else: itemId = tagStack.pop() # fetching next tag continue tagMatch = tagInRegex.match(tag) if tagMatch is None: # TODO : explicit error raise ValueError('Unknown data.') tagStack.append(itemId) matchDict = tagMatch.groupdict() itemId = matchDict['itemId'] # TODO : delegate the deserialization to the serialized items if itemId == 'IsoSurfaces': argsMatch = isoSurfaceArgsRegex.match(matchDict['args']) if not argsMatch: # TODO : explicit error raise ValueError('Failed to parse args "{0}".' ''.format(matchDict['args'])) argsDict = argsMatch.groupdict() nIso = int(argsDict['nIso']) if nIso: for surface in self.getIsosurfaces(): self.removeIsosurface(surface) for isoIdx in range(nIso): color = qt.QColor() stream >> color level = stream.readDouble() visible = stream.readBool() surface = self.addIsosurface(level, color=color) surface.setVisible(visible) elif itemId == 'Style': background = qt.QColor() foreground = qt.QColor() highlight = qt.QColor() stream >> background >> foreground >> highlight self.setBackgroundColor(background) self.setForegroundColor(foreground) self.setHighlightColor(highlight) else: raise ValueError('Unknown entry tag {0}.' ''.format(itemId))