def onData(self, value): """ Executed when selected item data is modified. """ plt = Plot.getPlot() if not plt: self.updateUI() return if not self.skip: self.skip = True name = self.names[self.item] obj = self.objs[self.item] x = self.form.x.value() y = self.form.y.value() s = self.form.s.value() # x/y labels only have one position control if name.find('x label') >= 0: self.form.y.setValue(x) elif name.find('y label') >= 0: self.form.x.setValue(y) # title and labels only have one size control if name.find('title') >= 0 or name.find('label') >= 0: obj.set_position((x, y)) obj.set_size(s) # legend have all controls else: Plot.legend(plt.legend, (x, y), s) plt.update() self.skip = False
def onOffset(self, value): """Executed when axes offsets have been modified.""" # Ensure that we can work plt = Plot.getPlot() if not plt: self.updateUI() return axesList = [plt.axes] if self.form.all.isChecked(): axesList = plt.axesList # Set new offset for axes in axesList: # For some reason, modify spines offset erase axes labels, so we # need store it in order to regenerate later x = axes.get_xlabel() y = axes.get_ylabel() for loc, spine in axes.spines.iteritems(): if loc in ['bottom', 'top']: spine.set_position(('outward', self.form.xOffset.value())) if loc in ['left', 'right']: spine.set_position(('outward', self.form.yOffset.value())) # Now we can restore axes labels Plot.xlabel(unicode(x)) Plot.ylabel(unicode(y)) plt.update()
def onData(self): """Executed when the selected item data is modified.""" if not self.skip: self.skip = True plt = Plot.getPlot() if not plt: self.updateUI() return # Ensure that selected serie exist if self.item >= len(Plot.series()): self.updateUI() return # Set label serie = Plot.series()[self.item] if (self.form.isLabel.isChecked()): serie.name = None self.form.label.setEnabled(False) else: serie.name = self.form.label.text() self.form.label.setEnabled(True) # Set line style and marker style = self.form.style.currentIndex() linestyles = list(Line2D.lineStyles.keys()) serie.line.set_linestyle(linestyles[style]) marker = self.form.marker.currentIndex() markers = list(Line2D.markers.keys()) serie.line.set_marker(markers[marker]) # Set line width and marker size serie.line.set_linewidth(self.form.width.value()) serie.line.set_markersize(self.form.size.value()) plt.update() # Regenerate series labels self.setList() self.skip = False
def updateUI(self): """ Setup UI controls values if possible """ plt = Plot.getPlot() self.form.axId.setEnabled(bool(plt)) self.form.title.setEnabled(bool(plt)) self.form.titleSize.setEnabled(bool(plt)) self.form.xLabel.setEnabled(bool(plt)) self.form.xSize.setEnabled(bool(plt)) self.form.yLabel.setEnabled(bool(plt)) self.form.ySize.setEnabled(bool(plt)) if not plt: return # Ensure that active axes is correct index = min(self.form.axId.value(), len(plt.axesList) - 1) self.form.axId.setValue(index) # Store data before starting changing it. ax = plt.axes t = ax.get_title() x = ax.get_xlabel() y = ax.get_ylabel() tt = ax.title.get_fontsize() xx = ax.xaxis.label.get_fontsize() yy = ax.yaxis.label.get_fontsize() # Set labels self.form.title.setText(t) self.form.xLabel.setText(x) self.form.yLabel.setText(y) # Set font sizes self.form.titleSize.setValue(tt) self.form.xSize.setValue(xx) self.form.ySize.setValue(yy)
def create(self, title, function, xlength, xname, xunit, xscale, yname, yunit, yscale, numxpoints): # Initialize from FreeCAD.Plot import Plot self.title = title self.function = function # This is assumed to be always a SegmentFunction self.xlength = xlength self.xname = xname self.xunit = xunit self.xscale = xscale self.yname = yname self.yunit = yunit self.yscale = yscale self.numxpoints = numxpoints # Create a plot window self.win = Plot.figure(title) # Get the plot object from the window self.thePlot = Plot.getPlot() # Format the plot object Plot.xlabel("$%s$ [%s]" % (xname, xunit)) Plot.ylabel("$%s$ [%s]" % (yname, yunit)) Plot.grid(True) # Calculate points (self.xpoints, self.ypoints) = self.function.evaluate(self.xlength, self.numxpoints) # Create plot self.plot()
def updateUI(self): """ Setup UI controls values if possible """ plt = Plot.getPlot() self.form.items.setEnabled(bool(plt)) self.form.label.setEnabled(bool(plt)) self.form.isLabel.setEnabled(bool(plt)) self.form.style.setEnabled(bool(plt)) self.form.marker.setEnabled(bool(plt)) self.form.width.setEnabled(bool(plt)) self.form.size.setEnabled(bool(plt)) self.form.color.setEnabled(bool(plt)) self.form.remove.setEnabled(bool(plt)) if not plt: self.plt = plt self.form.items.clear() return self.skip = True # Refill list if self.plt != plt or len(Plot.series()) != self.form.items.count(): self.plt = plt self.setList() # Ensure that have series if not len(Plot.series()): self.form.label.setEnabled(False) self.form.isLabel.setEnabled(False) self.form.style.setEnabled(False) self.form.marker.setEnabled(False) self.form.width.setEnabled(False) self.form.size.setEnabled(False) self.form.color.setEnabled(False) self.form.remove.setEnabled(False) return # Set label serie = Plot.series()[self.item] if serie.name is None: self.form.isLabel.setChecked(True) self.form.label.setEnabled(False) self.form.label.setText("") else: self.form.isLabel.setChecked(False) self.form.label.setText(serie.name) # Set line style and marker self.form.style.setCurrentIndex(0) for i, style in enumerate(Line2D.lineStyles.keys()): if style == serie.line.get_linestyle(): self.form.style.setCurrentIndex(i) self.form.marker.setCurrentIndex(0) for i, marker in enumerate(Line2D.markers.keys()): if marker == serie.line.get_marker(): self.form.marker.setCurrentIndex(i) # Set line width and marker size self.form.width.setValue(serie.line.get_linewidth()) self.form.size.setValue(serie.line.get_markersize()) # Set color color = Colors.colorConverter.to_rgb(serie.line.get_color()) self.form.color.setStyleSheet( "background-color: rgb({}, {}, {});".format( int(color[0] * 255), int(color[1] * 255), int(color[2] * 255))) self.skip = False
def onMdiArea(self, subWin): """Executed when a new window is selected on the mdi area. Keyword arguments: subWin -- Selected window. """ plt = Plot.getPlot() if plt != subWin: self.updateUI()
def onNew(self): """Executed when new axes must be created.""" # Ensure that we can work plt = Plot.getPlot() if not plt: self.updateUI() return Plot.addNewAxes() self.form.axId.setValue(len(plt.axesList) - 1) plt.update()
def onLabels(self): """ Executed when labels have been modified. """ plt = Plot.getPlot() if not plt: self.updateUI() return Plot.title(unicode(self.form.title.text())) Plot.xlabel(unicode(self.form.xLabel.text())) Plot.ylabel(unicode(self.form.yLabel.text())) plt.update()
def Activated(self): from FreeCAD.Plot import Plot plt = Plot.getPlot() if not plt: msg = QtGui.QApplication.translate( "plot_console", "The grid must be activated on top of a plot document", None) FreeCAD.Console.PrintError(msg + "\n") return flag = plt.isGrid() Plot.grid(not flag)
def accept(self): plt = Plot.getPlot() if not plt: msg = QtGui.QApplication.translate( "plot_console", "Plot document must be selected in order to save it", None) App.Console.PrintError(msg + "\n") return False path = self.form.path.text() size = (self.form.sizeX.value(), self.form.sizeY.value()) dpi = self.form.dpi.value() Plot.save(path, size, dpi) return True
def onFontSizes(self, value): """ Executed when font sizes have been modified. """ # Get apply environment plt = Plot.getPlot() if not plt: self.updateUI() return ax = plt.axes ax.title.set_fontsize(self.form.titleSize.value()) ax.xaxis.label.set_fontsize(self.form.xSize.value()) ax.yaxis.label.set_fontsize(self.form.ySize.value()) plt.update()
def onRemove(self): """Executed when the data serie must be removed.""" plt = Plot.getPlot() if not plt: self.updateUI() return # Ensure that selected serie exist if self.item >= len(Plot.series()): self.updateUI() return # Remove serie Plot.removeSerie(self.item) self.setList() self.updateUI() plt.update()
def updateUI(self): """ Setup UI controls values if possible """ plt = Plot.getPlot() self.form.path.setEnabled(bool(plt)) self.form.pathButton.setEnabled(bool(plt)) self.form.sizeX.setEnabled(bool(plt)) self.form.sizeY.setEnabled(bool(plt)) self.form.dpi.setEnabled(bool(plt)) if not plt: return fig = plt.fig size = fig.get_size_inches() dpi = fig.get_dpi() self.form.sizeX.setValue(size[0]) self.form.sizeY.setValue(size[1]) self.form.dpi.setValue(dpi)
def onAxesId(self, value): """Executed when axes index is modified.""" if not self.skip: self.skip = True # No active plot case plt = Plot.getPlot() if not plt: self.updateUI() self.skip = False return self.form.axId.setMaximum(len(plt.axesList)) if self.form.axId.value() >= len(plt.axesList): self.form.axId.setValue(len(plt.axesList) - 1) # Send new control to Plot instance plt.setActiveAxes(self.form.axId.value()) self.updateUI() self.skip = False
def setupUi(self): self.form.axId = self.widget(QtGui.QSpinBox, "axesIndex") self.form.title = self.widget(QtGui.QLineEdit, "title") self.form.titleSize = self.widget(QtGui.QSpinBox, "titleSize") self.form.xLabel = self.widget(QtGui.QLineEdit, "titleX") self.form.xSize = self.widget(QtGui.QSpinBox, "xSize") self.form.yLabel = self.widget(QtGui.QLineEdit, "titleY") self.form.ySize = self.widget(QtGui.QSpinBox, "ySize") self.retranslateUi() # Look for active axes if can axId = 0 plt = Plot.getPlot() if plt: while plt.axes != plt.axesList[axId]: axId = axId + 1 self.form.axId.setValue(axId) self.updateUI() QtCore.QObject.connect(self.form.axId, QtCore.SIGNAL('valueChanged(int)'), self.onAxesId) QtCore.QObject.connect(self.form.title, QtCore.SIGNAL("editingFinished()"), self.onLabels) QtCore.QObject.connect(self.form.xLabel, QtCore.SIGNAL("editingFinished()"), self.onLabels) QtCore.QObject.connect(self.form.yLabel, QtCore.SIGNAL("editingFinished()"), self.onLabels) QtCore.QObject.connect(self.form.titleSize, QtCore.SIGNAL("valueChanged(int)"), self.onFontSizes) QtCore.QObject.connect(self.form.xSize, QtCore.SIGNAL("valueChanged(int)"), self.onFontSizes) QtCore.QObject.connect(self.form.ySize, QtCore.SIGNAL("valueChanged(int)"), self.onFontSizes) QtCore.QObject.connect( Plot.getMdiArea(), QtCore.SIGNAL("subWindowActivated(QMdiSubWindow*)"), self.onMdiArea) return False
def onDims(self, value): """Executed when axes dims have been modified.""" # Ensure that we can work plt = Plot.getPlot() if not plt: self.updateUI() return axesList = [plt.axes] if self.form.all.isChecked(): axesList = plt.axesList # Set new dimensions xmin = self.form.xMin.value() / 100.0 xmax = self.form.xMax.value() / 100.0 ymin = self.form.yMin.value() / 100.0 ymax = self.form.yMax.value() / 100.0 for axes in axesList: axes.set_position([xmin, ymin, xmax - xmin, ymax - ymin]) plt.update()
def onColor(self): """ Executed when color palette is requested. """ plt = Plot.getPlot() if not plt: self.updateUI() return # Ensure that selected serie exist if self.item >= len(Plot.series()): self.updateUI() return # Show widget to select color col = QtGui.QColorDialog.getColor() # Send color to widget and serie if col.isValid(): serie = plt.series[self.item] self.form.color.setStyleSheet( "background-color: rgb({}, {}, {});".format( col.red(), col.green(), col.blue())) serie.line.set_color((col.redF(), col.greenF(), col.blueF())) plt.update()
def updateUI(self): """Setup the UI control values if it is possible.""" plt = Plot.getPlot() self.form.items.setEnabled(bool(plt)) self.form.x.setEnabled(bool(plt)) self.form.y.setEnabled(bool(plt)) self.form.s.setEnabled(bool(plt)) if not plt: self.plt = plt self.form.items.clear() return # Refill items list only if Plot instance have been changed if self.plt != plt: self.plt = plt self.plt.update() self.setList() # Get data for controls name = self.names[self.item] obj = self.objs[self.item] if name.find('title') >= 0 or name.find('label') >= 0: p = obj.get_position() x = p[0] y = p[1] s = obj.get_size() if name.find('x label') >= 0: self.form.y.setEnabled(False) self.form.y.setValue(x) elif name.find('y label') >= 0: self.form.x.setEnabled(False) self.form.x.setValue(y) else: x = plt.legPos[0] y = plt.legPos[1] s = obj.get_texts()[-1].get_fontsize() # Send it to controls self.form.x.setValue(x) self.form.y.setValue(y) self.form.s.setValue(s)
def onAlign(self, value): """Executed when axes align have been modified.""" # Ensure that we can work plt = Plot.getPlot() if not plt: self.updateUI() return axesList = [plt.axes] if self.form.all.isChecked(): axesList = plt.axesList # Set new alignment for axes in axesList: if self.form.xAlign.currentIndex() == 0: axes.xaxis.tick_bottom() axes.spines['bottom'].set_color((0.0, 0.0, 0.0)) axes.spines['top'].set_color('none') axes.xaxis.set_ticks_position('bottom') axes.xaxis.set_label_position('bottom') else: axes.xaxis.tick_top() axes.spines['top'].set_color((0.0, 0.0, 0.0)) axes.spines['bottom'].set_color('none') axes.xaxis.set_ticks_position('top') axes.xaxis.set_label_position('top') if self.form.yAlign.currentIndex() == 0: axes.yaxis.tick_left() axes.spines['left'].set_color((0.0, 0.0, 0.0)) axes.spines['right'].set_color('none') axes.yaxis.set_ticks_position('left') axes.yaxis.set_label_position('left') else: axes.yaxis.tick_right() axes.spines['right'].set_color((0.0, 0.0, 0.0)) axes.spines['left'].set_color('none') axes.yaxis.set_ticks_position('right') axes.yaxis.set_label_position('right') plt.update()
def onRemove(self): """Executed when axes must be deleted.""" # Ensure that we can work plt = Plot.getPlot() if not plt: self.updateUI() return # Don't remove first axes if not self.form.axId.value(): msg = QtGui.QApplication.translate("plot_console", "Axes 0 can not be deleted", None) App.Console.PrintError(msg + "\n") return # Remove axes ax = plt.axes ax.set_axis_off() plt.axesList.pop(self.form.axId.value()) # Ensure that active axes is correct index = min(self.form.axId.value(), len(plt.axesList) - 1) self.form.axId.setValue(index) plt.update()
def onScales(self): """Executed when axes scales have been modified.""" # Ensure that we can work plt = Plot.getPlot() if not plt: self.updateUI() return axesList = [plt.axes] if self.form.all.isChecked(): axesList = plt.axesList if not self.skip: self.skip = True # X axis if self.form.xAuto.isChecked(): for ax in axesList: ax.set_autoscalex_on(True) self.form.xSMin.setEnabled(False) self.form.xSMax.setEnabled(False) lim = plt.axes.get_xlim() self.form.xSMin.setText(str(lim[0])) self.form.xSMax.setText(str(lim[1])) else: self.form.xSMin.setEnabled(True) self.form.xSMax.setEnabled(True) try: xMin = float(self.form.xSMin.text()) except: xMin = plt.axes.get_xlim()[0] self.form.xSMin.setText(str(xMin)) try: xMax = float(self.form.xSMax.text()) except: xMax = plt.axes.get_xlim()[1] self.form.xSMax.setText(str(xMax)) for ax in axesList: ax.set_xlim((xMin, xMax)) # Y axis if self.form.yAuto.isChecked(): for ax in axesList: ax.set_autoscaley_on(True) self.form.ySMin.setEnabled(False) self.form.ySMax.setEnabled(False) lim = plt.axes.get_ylim() self.form.ySMin.setText(str(lim[0])) self.form.ySMax.setText(str(lim[1])) else: self.form.ySMin.setEnabled(True) self.form.ySMax.setEnabled(True) try: yMin = float(self.form.ySMin.text()) except: yMin = plt.axes.get_ylim()[0] self.form.ySMin.setText(str(yMin)) try: yMax = float(self.form.ySMax.text()) except: yMax = plt.axes.get_ylim()[1] self.form.ySMax.setText(str(yMax)) for ax in axesList: ax.set_ylim((yMin, yMax)) plt.update() self.skip = False
def updateUI(self): """Setup UI controls values if possible""" plt = Plot.getPlot() # Enable/disable them self.form.axId.setEnabled(bool(plt)) self.form.new.setEnabled(bool(plt)) self.form.remove.setEnabled(bool(plt)) self.form.all.setEnabled(bool(plt)) self.form.xMin.setEnabled(bool(plt)) self.form.xMax.setEnabled(bool(plt)) self.form.yMin.setEnabled(bool(plt)) self.form.yMax.setEnabled(bool(plt)) self.form.xAlign.setEnabled(bool(plt)) self.form.yAlign.setEnabled(bool(plt)) self.form.xOffset.setEnabled(bool(plt)) self.form.yOffset.setEnabled(bool(plt)) self.form.xAuto.setEnabled(bool(plt)) self.form.yAuto.setEnabled(bool(plt)) self.form.xSMin.setEnabled(bool(plt)) self.form.xSMax.setEnabled(bool(plt)) self.form.ySMin.setEnabled(bool(plt)) self.form.ySMax.setEnabled(bool(plt)) if not plt: self.form.axId.setValue(0) return # Ensure that active axes is correct index = min(self.form.axId.value(), len(plt.axesList) - 1) self.form.axId.setValue(index) # Set dimensions ax = plt.axes bb = ax.get_position() self.form.xMin.setValue(int(100 * bb.min[0])) self.form.xMax.setValue(int(100 * bb.max[0])) self.form.yMin.setValue(int(100 * bb.min[1])) self.form.yMax.setValue(int(100 * bb.max[1])) # Set alignment and offset xPos = ax.xaxis.get_ticks_position() yPos = ax.yaxis.get_ticks_position() xOffset = ax.spines['bottom'].get_position()[1] yOffset = ax.spines['left'].get_position()[1] if xPos == 'bottom' or xPos == 'default': self.form.xAlign.setCurrentIndex(0) else: self.form.xAlign.setCurrentIndex(1) self.form.xOffset.setValue(xOffset) if yPos == 'left' or yPos == 'default': self.form.yAlign.setCurrentIndex(0) else: self.form.yAlign.setCurrentIndex(1) self.form.yOffset.setValue(yOffset) # Set scales if ax.get_autoscalex_on(): self.form.xAuto.setChecked(True) self.form.xSMin.setEnabled(False) self.form.xSMax.setEnabled(False) else: self.form.xAuto.setChecked(False) self.form.xSMin.setEnabled(True) self.form.xSMax.setEnabled(True) lim = ax.get_xlim() self.form.xSMin.setText(str(lim[0])) self.form.xSMax.setText(str(lim[1])) if ax.get_autoscaley_on(): self.form.yAuto.setChecked(True) self.form.ySMin.setEnabled(False) self.form.ySMax.setEnabled(False) else: self.form.yAuto.setChecked(False) self.form.ySMin.setEnabled(True) self.form.ySMax.setEnabled(True) lim = ax.get_ylim() self.form.ySMin.setText(str(lim[0])) self.form.ySMax.setText(str(lim[1]))
def setupUi(self): self.form.axId = self.widget(QtGui.QSpinBox, "axesIndex") self.form.new = self.widget(QtGui.QPushButton, "newAxesButton") self.form.remove = self.widget(QtGui.QPushButton, "delAxesButton") self.form.all = self.widget(QtGui.QCheckBox, "allAxes") self.form.xMin = self.widget(QtGui.QSlider, "posXMin") self.form.xMax = self.widget(QtGui.QSlider, "posXMax") self.form.yMin = self.widget(QtGui.QSlider, "posYMin") self.form.yMax = self.widget(QtGui.QSlider, "posYMax") self.form.xAlign = self.widget(QtGui.QComboBox, "xAlign") self.form.yAlign = self.widget(QtGui.QComboBox, "yAlign") self.form.xOffset = self.widget(QtGui.QSpinBox, "xOffset") self.form.yOffset = self.widget(QtGui.QSpinBox, "yOffset") self.form.xAuto = self.widget(QtGui.QCheckBox, "xAuto") self.form.yAuto = self.widget(QtGui.QCheckBox, "yAuto") self.form.xSMin = self.widget(QtGui.QLineEdit, "xMin") self.form.xSMax = self.widget(QtGui.QLineEdit, "xMax") self.form.ySMin = self.widget(QtGui.QLineEdit, "yMin") self.form.ySMax = self.widget(QtGui.QLineEdit, "yMax") self.retranslateUi() # Look for active axes if can axId = 0 plt = Plot.getPlot() if plt: while plt.axes != plt.axesList[axId]: axId = axId + 1 self.form.axId.setValue(axId) self.updateUI() QtCore.QObject.connect(self.form.axId, QtCore.SIGNAL('valueChanged(int)'), self.onAxesId) QtCore.QObject.connect(self.form.new, QtCore.SIGNAL("pressed()"), self.onNew) QtCore.QObject.connect(self.form.remove, QtCore.SIGNAL("pressed()"), self.onRemove) QtCore.QObject.connect(self.form.xMin, QtCore.SIGNAL("valueChanged(int)"), self.onDims) QtCore.QObject.connect(self.form.xMax, QtCore.SIGNAL("valueChanged(int)"), self.onDims) QtCore.QObject.connect(self.form.yMin, QtCore.SIGNAL("valueChanged(int)"), self.onDims) QtCore.QObject.connect(self.form.yMax, QtCore.SIGNAL("valueChanged(int)"), self.onDims) QtCore.QObject.connect(self.form.xAlign, QtCore.SIGNAL("currentIndexChanged(int)"), self.onAlign) QtCore.QObject.connect(self.form.yAlign, QtCore.SIGNAL("currentIndexChanged(int)"), self.onAlign) QtCore.QObject.connect(self.form.xOffset, QtCore.SIGNAL("valueChanged(int)"), self.onOffset) QtCore.QObject.connect(self.form.yOffset, QtCore.SIGNAL("valueChanged(int)"), self.onOffset) QtCore.QObject.connect(self.form.xAuto, QtCore.SIGNAL("stateChanged(int)"), self.onScales) QtCore.QObject.connect(self.form.yAuto, QtCore.SIGNAL("stateChanged(int)"), self.onScales) QtCore.QObject.connect(self.form.xSMin, QtCore.SIGNAL("editingFinished()"), self.onScales) QtCore.QObject.connect(self.form.xSMax, QtCore.SIGNAL("editingFinished()"), self.onScales) QtCore.QObject.connect(self.form.ySMin, QtCore.SIGNAL("editingFinished()"), self.onScales) QtCore.QObject.connect(self.form.ySMax, QtCore.SIGNAL("editingFinished()"), self.onScales) QtCore.QObject.connect( Plot.getMdiArea(), QtCore.SIGNAL("subWindowActivated(QMdiSubWindow*)"), self.onMdiArea) return False