def startDrag(self, event): """ operations done when we start a drag event """ drag = Qt.QDrag(self) mimedata = Qt.QMimeData() passed_string = 'copyColorRange' mimedata.setText(passed_string) drag.setMimeData(mimedata) drag.exec_() event.accept()
def create_layout_stuff(self): """ create grid layouts into which plotter widgets are inserted """ if self.layout_parent is None or not self.layout_created: self.layout_parent = Qt.QWidget(self.wparent()) self.layout = Qt.QGridLayout(self.layout_parent) self.set_widgets(self.layout_parent, self.dataitem.caption, icon=self.icon()) self.layout_created = True self._wtop = self.layout_parent
def initScale(self, align): self.__data = QwtScaleWidget_PrivateData() self.__data.layoutFlags = 0 if align == QwtScaleDraw.RightScale: self.__data.layoutFlags |= self.TitleInverted self.__data.borderDist = [0, 0] self.__data.minBorderDist = [0, 0] self.__data.margin = 4 self.__data.titleOffset = 0 self.__data.spacing = 2 self.__data.scaleDraw = QwtScaleDraw() self.__data.scaleDraw.setAlignment(align) self.__data.scaleDraw.setLength(10) self.__data.scaleDraw.setScaleDiv(QwtLinearScaleEngine().divideScale( 0.0, 100.0, 10, 5)) self.__data.colorBar.colorMap = QwtLinearColorMap() self.__data.colorBar.isEnabled = False self.__data.colorBar.width = 10 flags = Qt.AlignmentFlag(Qt.AlignHCenter | Qt.TextExpandTabs | Qt.TextWordWrap) self.__data.title.setRenderFlags(flags) self.__data.title.setFont(self.font()) policy = QSizePolicy(QSizePolicy.MinimumExpanding, QSizePolicy.Fixed) if self.__data.scaleDraw.orientation() == Qt.Vertical: policy.transpose() self.setSizePolicy(policy) self.setAttribute(Qt.WA_WState_OwnSizePolicy, False)
def __init__(self): self.isDirty = True self.maxColumns = 0 self.numRows = 0 self.numColumns = 0 self.expanding = Qt.Orientations() self.itemSizeHints = [] self.itemList = []
def mightRender(self, text): """ Test if a string can be rendered by this text engine :param str text: Text to be tested :return: True, if it can be rendered """ return Qt.mightBeRichText(text)
def newToQImage(array): """Converts a numpy array to a QImage A Python version of PyQt4.Qwt5.toQImage(array) in PyQwt >= 5.2. Function written by Gerard Vermeulen """ if array.ndim != 2: raise RuntimeError('array must be 2-D') height, width = array.shape if array.dtype == numpy.uint8: # image = QImage(array, width, height, QImage.Format_Indexed8) # The next statement shows that QImage does not increase the # reference count of the buffer object to keep the data valid. image = Qt.QImage( array.tostring(), width, height, Qt.QImage.Format_Indexed8) image.setNumColors(256) for i in range(256): image.setColor(i, qRgb(i, i, i)) return image elif array.dtype == numpy.uint32: image = Qt.QImage( array.tostring(), width, height, Qt.QImage.Format_ARGB32) return image else: raise RuntimeError('array.dtype must be uint8 or uint32')
def plot_3D_array(self, display_flag_3D): # first figure out the actual rank of the array we are plotting self.actual_rank = 0 self.array_shape = self.dataitem.data.shape self.array_rank = self.dataitem.data.ndim for i in range(len(self.array_shape)): if self.array_shape[i] > 1: self.actual_rank = self.actual_rank + 1 if display_flag_3D and self.actual_rank > 2: self.set_ND_controls(None, None, num_axes=3) self.array_selector = None else: if not self.ND_Controls is None: self.ND_Controls.setParent(Qt.QWidget()) self.ND_Controls = None # pass initial array to the plotter if self.array_rank > 2: self.data = self.dataitem.data if self.array_selector is None: third_axis = None second_axis = None first_axis = None for i in range(self.array_rank - 1, -1, -1): if self.data.shape[i] > 1: if third_axis is None: third_axis = i elif second_axis is None: second_axis = i else: if first_axis is None: first_axis = i if not first_axis is None and not second_axis is None and not third_axis is None: self.array_selector = plot_func.create_array_selector( None, self.array_rank, self.data.shape, first_axis, second_axis, third_axis) self.array_tuple = tuple(self.array_selector) plot_array = self.data[self.array_tuple] else: plot_array = self.data if plot_array.min() == plot_array.max(): return else: self.ND_plotter.array_plot(plot_array, data_label='data ') # enable & highlight the cell self.enable() self.flash_refresh()
def setRenderFlags(self, renderFlags): """ Change the render flags The default setting is `Qt.AlignCenter` :param int renderFlags: Bitwise OR of the flags used like in `QPainter.drawText()` .. seealso:: :py:meth:`renderFlags()`, :py:meth:`qwt.text.QwtTextEngine.draw()` """ renderFlags = Qt.AlignmentFlag(renderFlags) if renderFlags != self.__data.renderFlags: self.__data.renderFlags = renderFlags self.__layoutCache.invalidate()
def show_3D_Display(self, display_flag_3D): if not has_vtk: return self.twoD_plotter = delete_2D_Plotters(self.colorbar, self.twoD_plotter) self.status_label.setParent(Qt.QWidget()) self.status_label = None if self.ND_plotter is None: self.ND_plotter = plot_func.create_ND_Plotter( self.layout, self.layout_parent) self.ND_plotter.show_2D_Display.connect(self.show_2D_Display) self.ND_plotter.show_ND_Controller.connect( self.ND_controller_showDisplay) else: self.ND_plotter.delete_vtk_renderer() self.ND_plotter.show_vtk_controls() # create 3-D Controller self.plot_3D_array(display_flag_3D)
def create_2D_plotter(self): if not self.ND_plotter is None: self.ND_plotter.close() self.ND_plotter = None self.twoD_plotter, self.plotPrinter = plot_func.create_2D_Plotters( self.layout, self.layout_parent) self.twoD_plotter.colorbar_needed.connect(self.set_ColorBar) self.twoD_plotter.show_ND_Controller.connect( self.ND_controller_showDisplay) self.twoD_plotter.show_3D_Display.connect(self.show_3D_Display) self.twoD_plotter.do_print.connect(self.plotPrinter.do_print) self.twoD_plotter.save_display.connect(self.grab_display) # create status label display self.status_label = Qt.QLabel(self.layout_parent) sansFont = QFont("Helvetica [Cronyx]", 8) self.status_label.setFont(sansFont) # self.layout.addMultiCellWidget(self.status_label,1,1,0,2) self.layout.addWidget(self.status_label, 1, 0, 1, 2) self.status_label.setText("Move the mouse within the plot canvas" " to show the cursor position.") self.status_label.show() self.twoD_plotter.status_update.connect(self.update_status)
def oldToQImage(array): """Converts a numpy array to a QImage A Python version of PyQt4.Qwt5.toQImage(array) in PyQwt < 5.2. Function written by Gerard Vermeulen """ if array.ndim != 2: raise RuntimeError('array must be 2-D') nx, ny = array.shape # width, height xstride, ystride = array.strides if array.dtype == numpy.uint8: image = QImage(nx, ny, QImage.Format_Indexed8) f_array = numpy.reshape(array,(nx*ny,),order='F') for j in range(ny): pointer = image.scanLine(j) pointer.setsize(nx*array.itemsize) memory = numpy.frombuffer(pointer, numpy.uint8) first_value = j*nx last_value = (j+1)*nx memory[:] = f_array[first_value:last_value] image.setColorCount(256) for i in range(256): image.setColor(i, qRgb(i, i, i)) return image elif array.dtype == numpy.uint32: image = Qt.QImage( array.tostring(), width, height, Qt.QImage.Format_ARGB32) f_array = numpy.reshape(array,(nx*ny,),order='F') for j in xrange(ny): pointer = image.scanLine(j) pointer.setsize(nx*array.itemsize) memory = numpy.frombuffer(pointer, numpy.uint32) first_value = j*nx last_value = (j+1)*nx memory[:] = f_array[first_value:last_value] return image else: raise RuntimeError('array.dtype must be uint8 or uint32')
def drawLabel(self, painter, canvasRect, pos): if self.__data.label.isEmpty(): return align = Qt.Alignment(self.__data.labelAlignment) alignPos = QPointF(pos) symbolOff = QSizeF(0, 0) if self.__data.style == QwtPlotMarker.VLine: if bool(self.__data.labelAlignment & Qt.AlignTop): alignPos.setY(canvasRect.top()) align &= ~Qt.AlignTop align |= Qt.AlignBottom elif bool(self.__data.labelAlignment & Qt.AlignBottom): alignPos.setY(canvasRect.bottom() - 1) align &= ~Qt.AlignBottom align |= Qt.AlignTop else: alignPos.setY(canvasRect.center().y()) elif self.__data.style == QwtPlotMarker.HLine: if bool(self.__data.labelAlignment & Qt.AlignLeft): alignPos.setX(canvasRect.left()) align &= ~Qt.AlignLeft align |= Qt.AlignRight elif bool(self.__data.labelAlignment & Qt.AlignRight): alignPos.setX(canvasRect.right() - 1) align &= ~Qt.AlignRight align |= Qt.AlignLeft else: alignPos.setX(canvasRect.center().x()) else: if self.__data.symbol and\ self.__data.symbol.style() != QwtSymbol.NoSymbol: symbolOff = self.__data.symbol.size() + QSizeF(1, 1) symbolOff /= 2 pw2 = self.__data.pen.widthF() / 2. if pw2 == 0.: pw2 = .5 spacing = self.__data.spacing xOff = max([pw2, symbolOff.width()]) yOff = max([pw2, symbolOff.height()]) textSize = self.__data.label.textSize(painter.font()) if align & Qt.AlignLeft: alignPos.setX(alignPos.x() - (xOff + spacing)) if self.__data.labelOrientation == Qt.Vertical: alignPos.setX(alignPos.x() - textSize.height()) else: alignPos.setX(alignPos.x() - textSize.width()) elif align & Qt.AlignRight: alignPos.setX(alignPos.x() + xOff + spacing) else: if self.__data.labelOrientation == Qt.Vertical: alignPos.setX(alignPos.x() - textSize.height() / 2) else: alignPos.setX(alignPos.x() - textSize.width() / 2) if align & Qt.AlignTop: alignPos.setY(alignPos.y() - (yOff + spacing)) if self.__data.labelOrientation != Qt.Vertical: alignPos.setY(alignPos.y() - textSize.height()) elif align & Qt.AlignBottom: alignPos.setY(alignPos.y() + yOff + spacing) if self.__data.labelOrientation == Qt.Vertical: alignPos.setY(alignPos.y() + textSize.width()) else: if self.__data.labelOrientation == Qt.Vertical: alignPos.setY(alignPos.y() + textSize.width() / 2) else: alignPos.setY(alignPos.y() - textSize.height() / 2) painter.translate(alignPos.x(), alignPos.y()) if self.__data.labelOrientation == Qt.Vertical: painter.rotate(-90.) textRect = QRectF(0, 0, textSize.width(), textSize.height()) self.__data.label.draw(painter, textRect)
def setRenderFlags(self, renderFlags): renderFlags = Qt.AlignmentFlag(renderFlags) if renderFlags != self.__data.renderFlags: self.__data.renderFlags = renderFlags self.__layoutCache.invalidate()
def mightRender(self, text): return Qt.mightBeRichText(text)
def drawLabel(self, painter, canvasRect, pos): """ Align and draw the text label of the marker :param QPainter painter: Painter :param QRectF canvasRect: Contents rectangle of the canvas in painter coordinates :param QPointF pos: Position of the marker, translated into widget coordinates .. seealso:: :py:meth:`drawLabel()`, :py:meth:`qwt.symbol.QwtSymbol.drawSymbol()` """ if self.__data.label.isEmpty(): return align = Qt.Alignment(self.__data.labelAlignment) alignPos = QPointF(pos) symbolOff = QSizeF(0, 0) if self.__data.style == QwtPlotMarker.VLine: # In VLine-style the y-position is pointless and # the alignment flags are relative to the canvas if bool(self.__data.labelAlignment & Qt.AlignTop): alignPos.setY(canvasRect.top()) align &= ~Qt.AlignTop align |= Qt.AlignBottom elif bool(self.__data.labelAlignment & Qt.AlignBottom): # In HLine-style the x-position is pointless and # the alignment flags are relative to the canvas alignPos.setY(canvasRect.bottom() - 1) align &= ~Qt.AlignBottom align |= Qt.AlignTop else: alignPos.setY(canvasRect.center().y()) elif self.__data.style == QwtPlotMarker.HLine: if bool(self.__data.labelAlignment & Qt.AlignLeft): alignPos.setX(canvasRect.left()) align &= ~Qt.AlignLeft align |= Qt.AlignRight elif bool(self.__data.labelAlignment & Qt.AlignRight): alignPos.setX(canvasRect.right() - 1) align &= ~Qt.AlignRight align |= Qt.AlignLeft else: alignPos.setX(canvasRect.center().x()) else: if self.__data.symbol and\ self.__data.symbol.style() != QwtSymbol.NoSymbol: symbolOff = self.__data.symbol.size() + QSizeF(1, 1) symbolOff /= 2 pw2 = self.__data.pen.widthF() / 2. if pw2 == 0.: pw2 = .5 spacing = self.__data.spacing xOff = max([pw2, symbolOff.width()]) yOff = max([pw2, symbolOff.height()]) textSize = self.__data.label.textSize(painter.font()) if align & Qt.AlignLeft: alignPos.setX(alignPos.x() - (xOff + spacing)) if self.__data.labelOrientation == Qt.Vertical: alignPos.setX(alignPos.x() - textSize.height()) else: alignPos.setX(alignPos.x() - textSize.width()) elif align & Qt.AlignRight: alignPos.setX(alignPos.x() + xOff + spacing) else: if self.__data.labelOrientation == Qt.Vertical: alignPos.setX(alignPos.x() - textSize.height() / 2) else: alignPos.setX(alignPos.x() - textSize.width() / 2) if align & Qt.AlignTop: alignPos.setY(alignPos.y() - (yOff + spacing)) if self.__data.labelOrientation != Qt.Vertical: alignPos.setY(alignPos.y() - textSize.height()) elif align & Qt.AlignBottom: alignPos.setY(alignPos.y() + yOff + spacing) if self.__data.labelOrientation == Qt.Vertical: alignPos.setY(alignPos.y() + textSize.width()) else: if self.__data.labelOrientation == Qt.Vertical: alignPos.setY(alignPos.y() + textSize.width() / 2) else: alignPos.setY(alignPos.y() - textSize.height() / 2) painter.translate(alignPos.x(), alignPos.y()) if self.__data.labelOrientation == Qt.Vertical: painter.rotate(-90.) textRect = QRectF(0, 0, textSize.width(), textSize.height()) self.__data.label.draw(painter, textRect)