diff options
author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2022-04-28 09:59:49 +0200 |
---|---|---|
committer | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2022-04-29 11:06:04 +0200 |
commit | e64a515c2195eb945264d5d4321a1aaf3bf09a3f (patch) | |
tree | 4b588422358ce695c7a2b1f40b79e1dad379b1cd /examples/datavisualization | |
parent | 6e26532b6b8430ba4d8a56aebeea5adf73501faf (diff) |
Refactor the surface examples
- Introduce a Window class to unclutter main.py
- Port to snake case naming
Task-number: PYSIDE-1880
Pick-to: 6.2 6.3
Change-Id: I433c48904ccc9adbafceb6d51c86f551e405a10e
Reviewed-by: Christian Tismer <tismer@stackless.com>
Diffstat (limited to 'examples/datavisualization')
-rw-r--r-- | examples/datavisualization/surface/main.py | 320 | ||||
-rw-r--r-- | examples/datavisualization/surface/surfacegraph.py | 304 | ||||
-rw-r--r-- | examples/datavisualization/surface_numpy/main.py | 322 | ||||
-rw-r--r-- | examples/datavisualization/surface_numpy/surfacegraph.py | 146 |
4 files changed, 552 insertions, 540 deletions
diff --git a/examples/datavisualization/surface/main.py b/examples/datavisualization/surface/main.py index 6ec717238..676e9942f 100644 --- a/examples/datavisualization/surface/main.py +++ b/examples/datavisualization/surface/main.py @@ -53,168 +53,174 @@ THEMES = ["Qt", "Primary Colors", "Digia", "Stone Moss", "Army Blue", "Retro", "Ebony", "Isabelle"] +class Window(QWidget): + def __init__(self, graph, parent=None): + super().__init__(parent) + self._graph = graph + self._container = QWidget.createWindowContainer(self._graph, self, + Qt.Widget) + + screen_size = self._graph.screen().size() + self._container.setMinimumSize(QSize(screen_size.width() / 2, + screen_size.height() / 1.6)) + self._container.setMaximumSize(screen_size) + self._container.setSizePolicy(QSizePolicy.Expanding, + QSizePolicy.Expanding) + self._container.setFocusPolicy(Qt.StrongFocus) + + h_layout = QHBoxLayout(self) + v_layout = QVBoxLayout() + h_layout.addWidget(self._container, 1) + h_layout.addLayout(v_layout) + v_layout.setAlignment(Qt.AlignTop) + + model_group_box = QGroupBox("Model") + + sqrt_sin_model_rb = QRadioButton(self) + sqrt_sin_model_rb.setText("Sqrt& Sin") + sqrt_sin_model_rb.setChecked(False) + + height_map_model_rb = QRadioButton(self) + height_map_model_rb.setText("Height Map") + height_map_model_rb.setChecked(False) + + model_vbox = QVBoxLayout() + model_vbox.addWidget(sqrt_sin_model_rb) + model_vbox.addWidget(height_map_model_rb) + model_group_box.setLayout(model_vbox) + + selection_group_box = QGroupBox("Selection Mode") + + mode_none_rb = QRadioButton(self) + mode_none_rb.setText("No selection") + mode_none_rb.setChecked(False) + + mode_item_rb = QRadioButton(self) + mode_item_rb.setText("Item") + mode_item_rb.setChecked(False) + + mode_slice_row_rb = QRadioButton(self) + mode_slice_row_rb.setText("Row Slice") + mode_slice_row_rb.setChecked(False) + + mode_slice_column_rb = QRadioButton(self) + mode_slice_column_rb.setText("Column Slice") + mode_slice_column_rb.setChecked(False) + + selection_vbox = QVBoxLayout() + selection_vbox.addWidget(mode_none_rb) + selection_vbox.addWidget(mode_item_rb) + selection_vbox.addWidget(mode_slice_row_rb) + selection_vbox.addWidget(mode_slice_column_rb) + selection_group_box.setLayout(selection_vbox) + + axis_min_slider_x = QSlider(Qt.Horizontal, self) + axis_min_slider_x.setMinimum(0) + axis_min_slider_x.setTickInterval(1) + axis_min_slider_x.setEnabled(True) + axis_max_slider_x = QSlider(Qt.Horizontal, self) + axis_max_slider_x.setMinimum(1) + axis_max_slider_x.setTickInterval(1) + axis_max_slider_x.setEnabled(True) + axis_min_slider_z = QSlider(Qt.Horizontal, self) + axis_min_slider_z.setMinimum(0) + axis_min_slider_z.setTickInterval(1) + axis_min_slider_z.setEnabled(True) + axis_max_slider_z = QSlider(Qt.Horizontal, self) + axis_max_slider_z.setMinimum(1) + axis_max_slider_z.setTickInterval(1) + axis_max_slider_z.setEnabled(True) + + theme_list = QComboBox(self) + theme_list.addItems(THEMES) + + color_group_box = QGroupBox("Custom gradient") + + gr_bto_y = QLinearGradient(0, 0, 1, 100) + gr_bto_y.setColorAt(1.0, Qt.black) + gr_bto_y.setColorAt(0.67, Qt.blue) + gr_bto_y.setColorAt(0.33, Qt.red) + gr_bto_y.setColorAt(0.0, Qt.yellow) + + pm = QPixmap(24, 100) + pmp = QPainter(pm) + pmp.setBrush(QBrush(gr_bto_y)) + pmp.setPen(Qt.NoPen) + pmp.drawRect(0, 0, 24, 100) + pmp.end() + + gradient_bto_ypb = QPushButton(self) + gradient_bto_ypb.setIcon(QIcon(pm)) + gradient_bto_ypb.setIconSize(QSize(24, 100)) + + gr_gto_r = QLinearGradient(0, 0, 1, 100) + gr_gto_r.setColorAt(1.0, Qt.darkGreen) + gr_gto_r.setColorAt(0.5, Qt.yellow) + gr_gto_r.setColorAt(0.2, Qt.red) + gr_gto_r.setColorAt(0.0, Qt.darkRed) + pmp.begin(pm) + pmp.setBrush(QBrush(gr_gto_r)) + pmp.drawRect(0, 0, 24, 100) + pmp.end() + + gradient_gto_rpb = QPushButton(self) + gradient_gto_rpb.setIcon(QIcon(pm)) + gradient_gto_rpb.setIconSize(QSize(24, 100)) + + color_hbox = QHBoxLayout() + color_hbox.addWidget(gradient_bto_ypb) + color_hbox.addWidget(gradient_gto_rpb) + color_group_box.setLayout(color_hbox) + + v_layout.addWidget(model_group_box) + v_layout.addWidget(selection_group_box) + v_layout.addWidget(QLabel("Column range")) + v_layout.addWidget(axis_min_slider_x) + v_layout.addWidget(axis_max_slider_x) + v_layout.addWidget(QLabel("Row range")) + v_layout.addWidget(axis_min_slider_z) + v_layout.addWidget(axis_max_slider_z) + v_layout.addWidget(QLabel("Theme")) + v_layout.addWidget(theme_list) + v_layout.addWidget(color_group_box) + + self._modifier = SurfaceGraph(self._graph) + + height_map_model_rb.toggled.connect(self._modifier.enable_height_map_model) + sqrt_sin_model_rb.toggled.connect(self._modifier.enable_sqrt_sin_model) + mode_none_rb.toggled.connect(self._modifier.toggle_mode_none) + mode_item_rb.toggled.connect(self._modifier.toggle_mode_item) + mode_slice_row_rb.toggled.connect(self._modifier.toggle_mode_slice_row) + mode_slice_column_rb.toggled.connect(self._modifier.toggle_mode_slice_column) + axis_min_slider_x.valueChanged.connect(self._modifier.adjust_xmin) + axis_max_slider_x.valueChanged.connect(self._modifier.adjust_xmax) + axis_min_slider_z.valueChanged.connect(self._modifier.adjust_zmin) + axis_max_slider_z.valueChanged.connect(self._modifier.adjust_zmax) + theme_list.currentIndexChanged[int].connect(self._modifier.change_theme) + gradient_bto_ypb.pressed.connect(self._modifier.set_black_to_yellow_gradient) + gradient_gto_rpb.pressed.connect(self._modifier.set_green_to_red_gradient) + + self._modifier.set_axis_min_slider_x(axis_min_slider_x) + self._modifier.set_axis_max_slider_x(axis_max_slider_x) + self._modifier.set_axis_min_slider_z(axis_min_slider_z) + self._modifier.set_axis_max_slider_z(axis_max_slider_z) + + sqrt_sin_model_rb.setChecked(True) + mode_item_rb.setChecked(True) + theme_list.setCurrentIndex(2) + + if __name__ == "__main__": app = QApplication(sys.argv) graph = Q3DSurface() - container = QWidget.createWindowContainer(graph) - if not graph.hasContext(): - msgBox = QMessageBox() - msgBox.setText("Couldn't initialize the OpenGL context.") - msgBox.exec() + msg_box = QMessageBox() + msg_box.setText("Couldn't initialize the OpenGL context.") + msg_box.exec() sys.exit(-1) - screenSize = graph.screen().size() - container.setMinimumSize(QSize(screenSize.width() / 2, - screenSize.height() / 1.6)) - container.setMaximumSize(screenSize) - container.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) - container.setFocusPolicy(Qt.StrongFocus) - - widget = QWidget() - hLayout = QHBoxLayout(widget) - vLayout = QVBoxLayout() - hLayout.addWidget(container, 1) - hLayout.addLayout(vLayout) - vLayout.setAlignment(Qt.AlignTop) - - widget.setWindowTitle("Surface example") - - modelGroupBox = QGroupBox("Model") - - sqrtSinModelRB = QRadioButton(widget) - sqrtSinModelRB.setText("Sqrt& Sin") - sqrtSinModelRB.setChecked(False) - - heightMapModelRB = QRadioButton(widget) - heightMapModelRB.setText("Height Map") - heightMapModelRB.setChecked(False) - - modelVBox = QVBoxLayout() - modelVBox.addWidget(sqrtSinModelRB) - modelVBox.addWidget(heightMapModelRB) - modelGroupBox.setLayout(modelVBox) - - selectionGroupBox = QGroupBox("Selection Mode") - - modeNoneRB = QRadioButton(widget) - modeNoneRB.setText("No selection") - modeNoneRB.setChecked(False) - - modeItemRB = QRadioButton(widget) - modeItemRB.setText("Item") - modeItemRB.setChecked(False) - - modeSliceRowRB = QRadioButton(widget) - modeSliceRowRB.setText("Row Slice") - modeSliceRowRB.setChecked(False) - - modeSliceColumnRB = QRadioButton(widget) - modeSliceColumnRB.setText("Column Slice") - modeSliceColumnRB.setChecked(False) - - selectionVBox = QVBoxLayout() - selectionVBox.addWidget(modeNoneRB) - selectionVBox.addWidget(modeItemRB) - selectionVBox.addWidget(modeSliceRowRB) - selectionVBox.addWidget(modeSliceColumnRB) - selectionGroupBox.setLayout(selectionVBox) - - axisMinSliderX = QSlider(Qt.Horizontal, widget) - axisMinSliderX.setMinimum(0) - axisMinSliderX.setTickInterval(1) - axisMinSliderX.setEnabled(True) - axisMaxSliderX = QSlider(Qt.Horizontal, widget) - axisMaxSliderX.setMinimum(1) - axisMaxSliderX.setTickInterval(1) - axisMaxSliderX.setEnabled(True) - axisMinSliderZ = QSlider(Qt.Horizontal, widget) - axisMinSliderZ.setMinimum(0) - axisMinSliderZ.setTickInterval(1) - axisMinSliderZ.setEnabled(True) - axisMaxSliderZ = QSlider(Qt.Horizontal, widget) - axisMaxSliderZ.setMinimum(1) - axisMaxSliderZ.setTickInterval(1) - axisMaxSliderZ.setEnabled(True) - - themeList = QComboBox(widget) - themeList.addItems(THEMES) - - colorGroupBox = QGroupBox("Custom gradient") - - grBtoY = QLinearGradient(0, 0, 1, 100) - grBtoY.setColorAt(1.0, Qt.black) - grBtoY.setColorAt(0.67, Qt.blue) - grBtoY.setColorAt(0.33, Qt.red) - grBtoY.setColorAt(0.0, Qt.yellow) - - pm = QPixmap(24, 100) - pmp = QPainter(pm) - pmp.setBrush(QBrush(grBtoY)) - pmp.setPen(Qt.NoPen) - pmp.drawRect(0, 0, 24, 100) - pmp.end() - - gradientBtoYPB = QPushButton(widget) - gradientBtoYPB.setIcon(QIcon(pm)) - gradientBtoYPB.setIconSize(QSize(24, 100)) - - grGtoR = QLinearGradient(0, 0, 1, 100) - grGtoR.setColorAt(1.0, Qt.darkGreen) - grGtoR.setColorAt(0.5, Qt.yellow) - grGtoR.setColorAt(0.2, Qt.red) - grGtoR.setColorAt(0.0, Qt.darkRed) - pmp.begin(pm) - pmp.setBrush(QBrush(grGtoR)) - pmp.drawRect(0, 0, 24, 100) - pmp.end() - - gradientGtoRPB = QPushButton(widget) - gradientGtoRPB.setIcon(QIcon(pm)) - gradientGtoRPB.setIconSize(QSize(24, 100)) - - colorHBox = QHBoxLayout() - colorHBox.addWidget(gradientBtoYPB) - colorHBox.addWidget(gradientGtoRPB) - colorGroupBox.setLayout(colorHBox) - - vLayout.addWidget(modelGroupBox) - vLayout.addWidget(selectionGroupBox) - vLayout.addWidget(QLabel("Column range")) - vLayout.addWidget(axisMinSliderX) - vLayout.addWidget(axisMaxSliderX) - vLayout.addWidget(QLabel("Row range")) - vLayout.addWidget(axisMinSliderZ) - vLayout.addWidget(axisMaxSliderZ) - vLayout.addWidget(QLabel("Theme")) - vLayout.addWidget(themeList) - vLayout.addWidget(colorGroupBox) - - widget.show() - - modifier = SurfaceGraph(graph) - - heightMapModelRB.toggled.connect(modifier.enableHeightMapModel) - sqrtSinModelRB.toggled.connect(modifier.enableSqrtSinModel) - modeNoneRB.toggled.connect(modifier.toggleModeNone) - modeItemRB.toggled.connect(modifier.toggleModeItem) - modeSliceRowRB.toggled.connect(modifier.toggleModeSliceRow) - modeSliceColumnRB.toggled.connect(modifier.toggleModeSliceColumn) - axisMinSliderX.valueChanged.connect(modifier.adjustXMin) - axisMaxSliderX.valueChanged.connect(modifier.adjustXMax) - axisMinSliderZ.valueChanged.connect(modifier.adjustZMin) - axisMaxSliderZ.valueChanged.connect(modifier.adjustZMax) - themeList.currentIndexChanged[int].connect(modifier.changeTheme) - gradientBtoYPB.pressed.connect(modifier.setBlackToYellowGradient) - gradientGtoRPB.pressed.connect(modifier.setGreenToRedGradient) - - modifier.setAxisMinSliderX(axisMinSliderX) - modifier.setAxisMaxSliderX(axisMaxSliderX) - modifier.setAxisMinSliderZ(axisMinSliderZ) - modifier.setAxisMaxSliderZ(axisMaxSliderZ) - - sqrtSinModelRB.setChecked(True) - modeItemRB.setChecked(True) - themeList.setCurrentIndex(2) + window = Window(graph) + window.setWindowTitle("Surface example") + window.show() sys.exit(app.exec()) diff --git a/examples/datavisualization/surface/surfacegraph.py b/examples/datavisualization/surface/surfacegraph.py index 0a450fbc2..f98c419f9 100644 --- a/examples/datavisualization/surface/surfacegraph.py +++ b/examples/datavisualization/surface/surfacegraph.py @@ -49,230 +49,230 @@ from PySide6.QtDataVisualization import (Q3DTheme, QAbstract3DGraph, from PySide6.QtGui import QImage, QLinearGradient, QVector3D from PySide6.QtWidgets import QSlider -sampleCountX = 50 -sampleCountZ = 50 -heightMapGridStepX = 6 -heightMapGridStepZ = 6 -sampleMin = -8.0 -sampleMax = 8.0 +SAMPLE_COUNT_X = 50 +SAMPLE_COUNT_Z = 50 +HEIGHT_MAP_GRID_STEP_X = 6 +HEIGHT_MAP_GRID_STEP_Z = 6 +SAMPLE_MIN = -8.0 +SAMPLE_MAX = 8.0 class SurfaceGraph(QObject): def __init__(self, surface, parent=None): super().__init__(parent) - self.m_graph = surface - self.m_graph.setAxisX(QValue3DAxis()) - self.m_graph.setAxisY(QValue3DAxis()) - self.m_graph.setAxisZ(QValue3DAxis()) - - self.m_sqrtSinProxy = QSurfaceDataProxy() - self.m_sqrtSinSeries = QSurface3DSeries(self.m_sqrtSinProxy) - self.fillSqrtSinProxy() - - imageFile = Path(__file__).parent / "mountain.png" - heightMapImage = QImage(imageFile) - self.m_heightMapProxy = QHeightMapSurfaceDataProxy(heightMapImage) - self.m_heightMapSeries = QSurface3DSeries(self.m_heightMapProxy) - self.m_heightMapSeries.setItemLabelFormat("(@xLabel, @zLabel): @yLabel") - self.m_heightMapProxy.setValueRanges(34.0, 40.0, 18.0, 24.0) - - self.m_heightMapWidth = heightMapImage.width() - self.m_heightMapHeight = heightMapImage.height() - - self.m_axisMinSliderX = QSlider() - self.m_axisMaxSliderX = QSlider() - self.m_axisMinSliderZ = QSlider() - self.m_axisMaxSliderZ = QSlider() - self.m_rangeMinX = 0.0 - self.m_rangeMinZ = 0.0 - self.m_stepX = 0.0 - self.m_stepZ = 0.0 - - def fillSqrtSinProxy(self): - stepX = (sampleMax - sampleMin) / float(sampleCountX - 1) - stepZ = (sampleMax - sampleMin) / float(sampleCountZ - 1) - - dataArray = [] - for i in range(sampleCountZ): - newRow = [] + self._graph = surface + self._graph.setAxisX(QValue3DAxis()) + self._graph.setAxisY(QValue3DAxis()) + self._graph.setAxisZ(QValue3DAxis()) + + self._sqrtSinProxy = QSurfaceDataProxy() + self._sqrtSinSeries = QSurface3DSeries(self._sqrtSinProxy) + self.fill_sqrt_sin_proxy() + + image_file = Path(__file__).parent / "mountain.png" + height_map_image = QImage(image_file) + self._heightMapProxy = QHeightMapSurfaceDataProxy(height_map_image) + self._heightMapSeries = QSurface3DSeries(self._heightMapProxy) + self._heightMapSeries.setItemLabelFormat("(@xLabel, @zLabel): @yLabel") + self._heightMapProxy.setValueRanges(34.0, 40.0, 18.0, 24.0) + + self._heightMapWidth = height_map_image.width() + self._heightMapHeight = height_map_image.height() + + self._axisMinSliderX = QSlider() + self._axisMaxSliderX = QSlider() + self._axisMinSliderZ = QSlider() + self._axisMaxSliderZ = QSlider() + self._rangeMinX = 0.0 + self._rangeMinZ = 0.0 + self._stepX = 0.0 + self._stepZ = 0.0 + + def fill_sqrt_sin_proxy(self): + step_x = (SAMPLE_MAX - SAMPLE_MIN) / float(SAMPLE_COUNT_X - 1) + step_z = (SAMPLE_MAX - SAMPLE_MIN) / float(SAMPLE_COUNT_Z - 1) + + data_array = [] + for i in range(SAMPLE_COUNT_Z): + new_row = [] # Keep values within range bounds, since just adding step can cause # minor drift due to the rounding errors. - z = min(sampleMax, (i * stepZ + sampleMin)) - for j in range(sampleCountX): - x = min(sampleMax, (j * stepX + sampleMin)) + z = min(SAMPLE_MAX, (i * step_z + SAMPLE_MIN)) + for j in range(SAMPLE_COUNT_X): + x = min(SAMPLE_MAX, (j * step_x + SAMPLE_MIN)) R = math.sqrt(z * z + x * x) + 0.01 y = (math.sin(R) / R + 0.24) * 1.61 - newRow.append(QSurfaceDataItem(QVector3D(x, y, z))) - dataArray.append(newRow) + new_row.append(QSurfaceDataItem(QVector3D(x, y, z))) + data_array.append(new_row) - self.m_sqrtSinProxy.resetArray(dataArray) + self._sqrtSinProxy.resetArray(data_array) - def enableSqrtSinModel(self, enable): + def enable_sqrt_sin_model(self, enable): if enable: - self.m_sqrtSinSeries.setDrawMode(QSurface3DSeries.DrawSurfaceAndWireframe) - self.m_sqrtSinSeries.setFlatShadingEnabled(True) + self._sqrtSinSeries.setDrawMode(QSurface3DSeries.DrawSurfaceAndWireframe) + self._sqrtSinSeries.setFlatShadingEnabled(True) - self.m_graph.axisX().setLabelFormat("%.2f") - self.m_graph.axisZ().setLabelFormat("%.2f") - self.m_graph.axisX().setRange(sampleMin, sampleMax) - self.m_graph.axisY().setRange(0.0, 2.0) - self.m_graph.axisZ().setRange(sampleMin, sampleMax) - self.m_graph.axisX().setLabelAutoRotation(30) - self.m_graph.axisY().setLabelAutoRotation(90) - self.m_graph.axisZ().setLabelAutoRotation(30) + self._graph.axisX().setLabelFormat("%.2f") + self._graph.axisZ().setLabelFormat("%.2f") + self._graph.axisX().setRange(SAMPLE_MIN, SAMPLE_MAX) + self._graph.axisY().setRange(0.0, 2.0) + self._graph.axisZ().setRange(SAMPLE_MIN, SAMPLE_MAX) + self._graph.axisX().setLabelAutoRotation(30) + self._graph.axisY().setLabelAutoRotation(90) + self._graph.axisZ().setLabelAutoRotation(30) - self.m_graph.removeSeries(self.m_heightMapSeries) - self.m_graph.addSeries(self.m_sqrtSinSeries) + self._graph.removeSeries(self._heightMapSeries) + self._graph.addSeries(self._sqrtSinSeries) # Reset range sliders for Sqrt&Sin - self.m_rangeMinX = sampleMin - self.m_rangeMinZ = sampleMin - self.m_stepX = (sampleMax - sampleMin) / float(sampleCountX - 1) - self.m_stepZ = (sampleMax - sampleMin) / float(sampleCountZ - 1) - self.m_axisMinSliderX.setMaximum(sampleCountX - 2) - self.m_axisMinSliderX.setValue(0) - self.m_axisMaxSliderX.setMaximum(sampleCountX - 1) - self.m_axisMaxSliderX.setValue(sampleCountX - 1) - self.m_axisMinSliderZ.setMaximum(sampleCountZ - 2) - self.m_axisMinSliderZ.setValue(0) - self.m_axisMaxSliderZ.setMaximum(sampleCountZ - 1) - self.m_axisMaxSliderZ.setValue(sampleCountZ - 1) - - def enableHeightMapModel(self, enable): + self._rangeMinX = SAMPLE_MIN + self._rangeMinZ = SAMPLE_MIN + self._stepX = (SAMPLE_MAX - SAMPLE_MIN) / float(SAMPLE_COUNT_X - 1) + self._stepZ = (SAMPLE_MAX - SAMPLE_MIN) / float(SAMPLE_COUNT_Z - 1) + self._axisMinSliderX.setMaximum(SAMPLE_COUNT_X - 2) + self._axisMinSliderX.setValue(0) + self._axisMaxSliderX.setMaximum(SAMPLE_COUNT_X - 1) + self._axisMaxSliderX.setValue(SAMPLE_COUNT_X - 1) + self._axisMinSliderZ.setMaximum(SAMPLE_COUNT_Z - 2) + self._axisMinSliderZ.setValue(0) + self._axisMaxSliderZ.setMaximum(SAMPLE_COUNT_Z - 1) + self._axisMaxSliderZ.setValue(SAMPLE_COUNT_Z - 1) + + def enable_height_map_model(self, enable): if enable: - self.m_heightMapSeries.setDrawMode(QSurface3DSeries.DrawSurface) - self.m_heightMapSeries.setFlatShadingEnabled(False) + self._heightMapSeries.setDrawMode(QSurface3DSeries.DrawSurface) + self._heightMapSeries.setFlatShadingEnabled(False) - self.m_graph.axisX().setLabelFormat("%.1f N") - self.m_graph.axisZ().setLabelFormat("%.1f E") - self.m_graph.axisX().setRange(34.0, 40.0) - self.m_graph.axisY().setAutoAdjustRange(True) - self.m_graph.axisZ().setRange(18.0, 24.0) + self._graph.axisX().setLabelFormat("%.1f N") + self._graph.axisZ().setLabelFormat("%.1f E") + self._graph.axisX().setRange(34.0, 40.0) + self._graph.axisY().setAutoAdjustRange(True) + self._graph.axisZ().setRange(18.0, 24.0) - self.m_graph.axisX().setTitle("Latitude") - self.m_graph.axisY().setTitle("Height") - self.m_graph.axisZ().setTitle("Longitude") + self._graph.axisX().setTitle("Latitude") + self._graph.axisY().setTitle("Height") + self._graph.axisZ().setTitle("Longitude") - self.m_graph.removeSeries(self.m_sqrtSinSeries) - self.m_graph.addSeries(self.m_heightMapSeries) + self._graph.removeSeries(self._sqrtSinSeries) + self._graph.addSeries(self._heightMapSeries) # Reset range sliders for height map - mapGridCountX = self.m_heightMapWidth / heightMapGridStepX - mapGridCountZ = self.m_heightMapHeight / heightMapGridStepZ - self.m_rangeMinX = 34.0 - self.m_rangeMinZ = 18.0 - self.m_stepX = 6.0 / float(mapGridCountX - 1) - self.m_stepZ = 6.0 / float(mapGridCountZ - 1) - self.m_axisMinSliderX.setMaximum(mapGridCountX - 2) - self.m_axisMinSliderX.setValue(0) - self.m_axisMaxSliderX.setMaximum(mapGridCountX - 1) - self.m_axisMaxSliderX.setValue(mapGridCountX - 1) - self.m_axisMinSliderZ.setMaximum(mapGridCountZ - 2) - self.m_axisMinSliderZ.setValue(0) - self.m_axisMaxSliderZ.setMaximum(mapGridCountZ - 1) - self.m_axisMaxSliderZ.setValue(mapGridCountZ - 1) - - def adjustXMin(self, minimum): - minX = self.m_stepX * float(minimum) + self.m_rangeMinX - - maximum = self.m_axisMaxSliderX.value() + map_grid_count_x = self._heightMapWidth / HEIGHT_MAP_GRID_STEP_X + map_grid_count_z = self._heightMapHeight / HEIGHT_MAP_GRID_STEP_Z + self._rangeMinX = 34.0 + self._rangeMinZ = 18.0 + self._stepX = 6.0 / float(map_grid_count_x - 1) + self._stepZ = 6.0 / float(map_grid_count_z - 1) + self._axisMinSliderX.setMaximum(map_grid_count_x - 2) + self._axisMinSliderX.setValue(0) + self._axisMaxSliderX.setMaximum(map_grid_count_x - 1) + self._axisMaxSliderX.setValue(map_grid_count_x - 1) + self._axisMinSliderZ.setMaximum(map_grid_count_z - 2) + self._axisMinSliderZ.setValue(0) + self._axisMaxSliderZ.setMaximum(map_grid_count_z - 1) + self._axisMaxSliderZ.setValue(map_grid_count_z - 1) + + def adjust_xmin(self, minimum): + min_x = self._stepX * float(minimum) + self._rangeMinX + + maximum = self._axisMaxSliderX.value() if minimum >= maximum: maximum = minimum + 1 - self.m_axisMaxSliderX.setValue(maximum) - maxX = self.m_stepX * maximum + self.m_rangeMinX + self._axisMaxSliderX.setValue(maximum) + max_x = self._stepX * maximum + self._rangeMinX - self.setAxisXRange(minX, maxX) + self.set_axis_xrange(min_x, max_x) - def adjustXMax(self, maximum): - maxX = self.m_stepX * float(maximum) + self.m_rangeMinX + def adjust_xmax(self, maximum): + max_x = self._stepX * float(maximum) + self._rangeMinX - minimum = self.m_axisMinSliderX.value() + minimum = self._axisMinSliderX.value() if maximum <= minimum: minimum = maximum - 1 - self.m_axisMinSliderX.setValue(minimum) - minX = self.m_stepX * minimum + self.m_rangeMinX + self._axisMinSliderX.setValue(minimum) + min_x = self._stepX * minimum + self._rangeMinX - self.setAxisXRange(minX, maxX) + self.set_axis_xrange(min_x, max_x) - def adjustZMin(self, minimum): - minZ = self.m_stepZ * float(minimum) + self.m_rangeMinZ + def adjust_zmin(self, minimum): + min_z = self._stepZ * float(minimum) + self._rangeMinZ - maximum = self.m_axisMaxSliderZ.value() + maximum = self._axisMaxSliderZ.value() if minimum >= maximum: maximum = minimum + 1 - self.m_axisMaxSliderZ.setValue(maximum) - maxZ = self.m_stepZ * maximum + self.m_rangeMinZ + self._axisMaxSliderZ.setValue(maximum) + max_z = self._stepZ * maximum + self._rangeMinZ - self.setAxisZRange(minZ, maxZ) + self.set_axis_zrange(min_z, max_z) - def adjustZMax(self, maximum): - maxX = self.m_stepZ * float(maximum) + self.m_rangeMinZ + def adjust_zmax(self, maximum): + max_x = self._stepZ * float(maximum) + self._rangeMinZ - minimum = self.m_axisMinSliderZ.value() + minimum = self._axisMinSliderZ.value() if maximum <= minimum: minimum = maximum - 1 - self.m_axisMinSliderZ.setValue(minimum) - minX = self.m_stepZ * minimum + self.m_rangeMinZ + self._axisMinSliderZ.setValue(minimum) + min_x = self._stepZ * minimum + self._rangeMinZ - self.setAxisZRange(minX, maxX) + self.set_axis_zrange(min_x, max_x) - def setAxisXRange(self, minimum, maximum): - self.m_graph.axisX().setRange(minimum, maximum) + def set_axis_xrange(self, minimum, maximum): + self._graph.axisX().setRange(minimum, maximum) - def setAxisZRange(self, minimum, maximum): - self.m_graph.axisZ().setRange(minimum, maximum) + def set_axis_zrange(self, minimum, maximum): + self._graph.axisZ().setRange(minimum, maximum) @Slot() - def changeTheme(self, theme): - self.m_graph.activeTheme().setType(Q3DTheme.Theme(theme)) + def change_theme(self, theme): + self._graph.activeTheme().setType(Q3DTheme.Theme(theme)) - def setBlackToYellowGradient(self): + def set_black_to_yellow_gradient(self): gr = QLinearGradient() gr.setColorAt(0.0, Qt.black) gr.setColorAt(0.33, Qt.blue) gr.setColorAt(0.67, Qt.red) gr.setColorAt(1.0, Qt.yellow) - series = self.m_graph.seriesList()[0] + series = self._graph.seriesList()[0] series.setBaseGradient(gr) series.setColorStyle(Q3DTheme.ColorStyleRangeGradient) - def setGreenToRedGradient(self): + def set_green_to_red_gradient(self): gr = QLinearGradient() gr.setColorAt(0.0, Qt.darkGreen) gr.setColorAt(0.5, Qt.yellow) gr.setColorAt(0.8, Qt.red) gr.setColorAt(1.0, Qt.darkRed) - series = self.m_graph.seriesList()[0] + series = self._graph.seriesList()[0] series.setBaseGradient(gr) series.setColorStyle(Q3DTheme.ColorStyleRangeGradient) - def toggleModeNone(self): - self.m_graph.setSelectionMode(QAbstract3DGraph.SelectionNone) + def toggle_mode_none(self): + self._graph.setSelectionMode(QAbstract3DGraph.SelectionNone) - def toggleModeItem(self): - self.m_graph.setSelectionMode(QAbstract3DGraph.SelectionItem) + def toggle_mode_item(self): + self._graph.setSelectionMode(QAbstract3DGraph.SelectionItem) - def toggleModeSliceRow(self): - self.m_graph.setSelectionMode( + def toggle_mode_slice_row(self): + self._graph.setSelectionMode( QAbstract3DGraph.SelectionItemAndRow | QAbstract3DGraph.SelectionSlice ) - def toggleModeSliceColumn(self): - self.m_graph.setSelectionMode( + def toggle_mode_slice_column(self): + self._graph.setSelectionMode( QAbstract3DGraph.SelectionItemAndColumn | QAbstract3DGraph.SelectionSlice ) - def setAxisMinSliderX(self, slider): - self.m_axisMinSliderX = slider + def set_axis_min_slider_x(self, slider): + self._axisMinSliderX = slider - def setAxisMaxSliderX(self, slider): - self.m_axisMaxSliderX = slider + def set_axis_max_slider_x(self, slider): + self._axisMaxSliderX = slider - def setAxisMinSliderZ(self, slider): - self.m_axisMinSliderZ = slider + def set_axis_min_slider_z(self, slider): + self._axisMinSliderZ = slider - def setAxisMaxSliderZ(self, slider): - self.m_axisMaxSliderZ = slider + def set_axis_max_slider_z(self, slider): + self._axisMaxSliderZ = slider diff --git a/examples/datavisualization/surface_numpy/main.py b/examples/datavisualization/surface_numpy/main.py index 17e23a795..676e9942f 100644 --- a/examples/datavisualization/surface_numpy/main.py +++ b/examples/datavisualization/surface_numpy/main.py @@ -1,6 +1,6 @@ ############################################################################# ## -## Copyright (C) 2022 The Qt Company Ltd. +## Copyright (C) 2021 The Qt Company Ltd. ## Contact: https://www.qt.io/licensing/ ## ## This file is part of the Qt for Python examples of the Qt Toolkit. @@ -53,168 +53,174 @@ THEMES = ["Qt", "Primary Colors", "Digia", "Stone Moss", "Army Blue", "Retro", "Ebony", "Isabelle"] +class Window(QWidget): + def __init__(self, graph, parent=None): + super().__init__(parent) + self._graph = graph + self._container = QWidget.createWindowContainer(self._graph, self, + Qt.Widget) + + screen_size = self._graph.screen().size() + self._container.setMinimumSize(QSize(screen_size.width() / 2, + screen_size.height() / 1.6)) + self._container.setMaximumSize(screen_size) + self._container.setSizePolicy(QSizePolicy.Expanding, + QSizePolicy.Expanding) + self._container.setFocusPolicy(Qt.StrongFocus) + + h_layout = QHBoxLayout(self) + v_layout = QVBoxLayout() + h_layout.addWidget(self._container, 1) + h_layout.addLayout(v_layout) + v_layout.setAlignment(Qt.AlignTop) + + model_group_box = QGroupBox("Model") + + sqrt_sin_model_rb = QRadioButton(self) + sqrt_sin_model_rb.setText("Sqrt& Sin") + sqrt_sin_model_rb.setChecked(False) + + height_map_model_rb = QRadioButton(self) + height_map_model_rb.setText("Height Map") + height_map_model_rb.setChecked(False) + + model_vbox = QVBoxLayout() + model_vbox.addWidget(sqrt_sin_model_rb) + model_vbox.addWidget(height_map_model_rb) + model_group_box.setLayout(model_vbox) + + selection_group_box = QGroupBox("Selection Mode") + + mode_none_rb = QRadioButton(self) + mode_none_rb.setText("No selection") + mode_none_rb.setChecked(False) + + mode_item_rb = QRadioButton(self) + mode_item_rb.setText("Item") + mode_item_rb.setChecked(False) + + mode_slice_row_rb = QRadioButton(self) + mode_slice_row_rb.setText("Row Slice") + mode_slice_row_rb.setChecked(False) + + mode_slice_column_rb = QRadioButton(self) + mode_slice_column_rb.setText("Column Slice") + mode_slice_column_rb.setChecked(False) + + selection_vbox = QVBoxLayout() + selection_vbox.addWidget(mode_none_rb) + selection_vbox.addWidget(mode_item_rb) + selection_vbox.addWidget(mode_slice_row_rb) + selection_vbox.addWidget(mode_slice_column_rb) + selection_group_box.setLayout(selection_vbox) + + axis_min_slider_x = QSlider(Qt.Horizontal, self) + axis_min_slider_x.setMinimum(0) + axis_min_slider_x.setTickInterval(1) + axis_min_slider_x.setEnabled(True) + axis_max_slider_x = QSlider(Qt.Horizontal, self) + axis_max_slider_x.setMinimum(1) + axis_max_slider_x.setTickInterval(1) + axis_max_slider_x.setEnabled(True) + axis_min_slider_z = QSlider(Qt.Horizontal, self) + axis_min_slider_z.setMinimum(0) + axis_min_slider_z.setTickInterval(1) + axis_min_slider_z.setEnabled(True) + axis_max_slider_z = QSlider(Qt.Horizontal, self) + axis_max_slider_z.setMinimum(1) + axis_max_slider_z.setTickInterval(1) + axis_max_slider_z.setEnabled(True) + + theme_list = QComboBox(self) + theme_list.addItems(THEMES) + + color_group_box = QGroupBox("Custom gradient") + + gr_bto_y = QLinearGradient(0, 0, 1, 100) + gr_bto_y.setColorAt(1.0, Qt.black) + gr_bto_y.setColorAt(0.67, Qt.blue) + gr_bto_y.setColorAt(0.33, Qt.red) + gr_bto_y.setColorAt(0.0, Qt.yellow) + + pm = QPixmap(24, 100) + pmp = QPainter(pm) + pmp.setBrush(QBrush(gr_bto_y)) + pmp.setPen(Qt.NoPen) + pmp.drawRect(0, 0, 24, 100) + pmp.end() + + gradient_bto_ypb = QPushButton(self) + gradient_bto_ypb.setIcon(QIcon(pm)) + gradient_bto_ypb.setIconSize(QSize(24, 100)) + + gr_gto_r = QLinearGradient(0, 0, 1, 100) + gr_gto_r.setColorAt(1.0, Qt.darkGreen) + gr_gto_r.setColorAt(0.5, Qt.yellow) + gr_gto_r.setColorAt(0.2, Qt.red) + gr_gto_r.setColorAt(0.0, Qt.darkRed) + pmp.begin(pm) + pmp.setBrush(QBrush(gr_gto_r)) + pmp.drawRect(0, 0, 24, 100) + pmp.end() + + gradient_gto_rpb = QPushButton(self) + gradient_gto_rpb.setIcon(QIcon(pm)) + gradient_gto_rpb.setIconSize(QSize(24, 100)) + + color_hbox = QHBoxLayout() + color_hbox.addWidget(gradient_bto_ypb) + color_hbox.addWidget(gradient_gto_rpb) + color_group_box.setLayout(color_hbox) + + v_layout.addWidget(model_group_box) + v_layout.addWidget(selection_group_box) + v_layout.addWidget(QLabel("Column range")) + v_layout.addWidget(axis_min_slider_x) + v_layout.addWidget(axis_max_slider_x) + v_layout.addWidget(QLabel("Row range")) + v_layout.addWidget(axis_min_slider_z) + v_layout.addWidget(axis_max_slider_z) + v_layout.addWidget(QLabel("Theme")) + v_layout.addWidget(theme_list) + v_layout.addWidget(color_group_box) + + self._modifier = SurfaceGraph(self._graph) + + height_map_model_rb.toggled.connect(self._modifier.enable_height_map_model) + sqrt_sin_model_rb.toggled.connect(self._modifier.enable_sqrt_sin_model) + mode_none_rb.toggled.connect(self._modifier.toggle_mode_none) + mode_item_rb.toggled.connect(self._modifier.toggle_mode_item) + mode_slice_row_rb.toggled.connect(self._modifier.toggle_mode_slice_row) + mode_slice_column_rb.toggled.connect(self._modifier.toggle_mode_slice_column) + axis_min_slider_x.valueChanged.connect(self._modifier.adjust_xmin) + axis_max_slider_x.valueChanged.connect(self._modifier.adjust_xmax) + axis_min_slider_z.valueChanged.connect(self._modifier.adjust_zmin) + axis_max_slider_z.valueChanged.connect(self._modifier.adjust_zmax) + theme_list.currentIndexChanged[int].connect(self._modifier.change_theme) + gradient_bto_ypb.pressed.connect(self._modifier.set_black_to_yellow_gradient) + gradient_gto_rpb.pressed.connect(self._modifier.set_green_to_red_gradient) + + self._modifier.set_axis_min_slider_x(axis_min_slider_x) + self._modifier.set_axis_max_slider_x(axis_max_slider_x) + self._modifier.set_axis_min_slider_z(axis_min_slider_z) + self._modifier.set_axis_max_slider_z(axis_max_slider_z) + + sqrt_sin_model_rb.setChecked(True) + mode_item_rb.setChecked(True) + theme_list.setCurrentIndex(2) + + if __name__ == "__main__": app = QApplication(sys.argv) graph = Q3DSurface() - container = QWidget.createWindowContainer(graph) - if not graph.hasContext(): - msgBox = QMessageBox() - msgBox.setText("Couldn't initialize the OpenGL context.") - msgBox.exec() + msg_box = QMessageBox() + msg_box.setText("Couldn't initialize the OpenGL context.") + msg_box.exec() sys.exit(-1) - screenSize = graph.screen().size() - container.setMinimumSize(QSize(screenSize.width() / 2, - screenSize.height() / 1.6)) - container.setMaximumSize(screenSize) - container.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) - container.setFocusPolicy(Qt.StrongFocus) - - widget = QWidget() - hLayout = QHBoxLayout(widget) - vLayout = QVBoxLayout() - hLayout.addWidget(container, 1) - hLayout.addLayout(vLayout) - vLayout.setAlignment(Qt.AlignTop) - - widget.setWindowTitle("Surface example") - - modelGroupBox = QGroupBox("Model") - - sqrtSinModelRB = QRadioButton(widget) - sqrtSinModelRB.setText("Sqrt& Sin") - sqrtSinModelRB.setChecked(False) - - heightMapModelRB = QRadioButton(widget) - heightMapModelRB.setText("Height Map") - heightMapModelRB.setChecked(False) - - modelVBox = QVBoxLayout() - modelVBox.addWidget(sqrtSinModelRB) - modelVBox.addWidget(heightMapModelRB) - modelGroupBox.setLayout(modelVBox) - - selectionGroupBox = QGroupBox("Selection Mode") - - modeNoneRB = QRadioButton(widget) - modeNoneRB.setText("No selection") - modeNoneRB.setChecked(False) - - modeItemRB = QRadioButton(widget) - modeItemRB.setText("Item") - modeItemRB.setChecked(False) - - modeSliceRowRB = QRadioButton(widget) - modeSliceRowRB.setText("Row Slice") - modeSliceRowRB.setChecked(False) - - modeSliceColumnRB = QRadioButton(widget) - modeSliceColumnRB.setText("Column Slice") - modeSliceColumnRB.setChecked(False) - - selectionVBox = QVBoxLayout() - selectionVBox.addWidget(modeNoneRB) - selectionVBox.addWidget(modeItemRB) - selectionVBox.addWidget(modeSliceRowRB) - selectionVBox.addWidget(modeSliceColumnRB) - selectionGroupBox.setLayout(selectionVBox) - - axisMinSliderX = QSlider(Qt.Horizontal, widget) - axisMinSliderX.setMinimum(0) - axisMinSliderX.setTickInterval(1) - axisMinSliderX.setEnabled(True) - axisMaxSliderX = QSlider(Qt.Horizontal, widget) - axisMaxSliderX.setMinimum(1) - axisMaxSliderX.setTickInterval(1) - axisMaxSliderX.setEnabled(True) - axisMinSliderZ = QSlider(Qt.Horizontal, widget) - axisMinSliderZ.setMinimum(0) - axisMinSliderZ.setTickInterval(1) - axisMinSliderZ.setEnabled(True) - axisMaxSliderZ = QSlider(Qt.Horizontal, widget) - axisMaxSliderZ.setMinimum(1) - axisMaxSliderZ.setTickInterval(1) - axisMaxSliderZ.setEnabled(True) - - themeList = QComboBox(widget) - themeList.addItems(THEMES) - - colorGroupBox = QGroupBox("Custom gradient") - - grBtoY = QLinearGradient(0, 0, 1, 100) - grBtoY.setColorAt(1.0, Qt.black) - grBtoY.setColorAt(0.67, Qt.blue) - grBtoY.setColorAt(0.33, Qt.red) - grBtoY.setColorAt(0.0, Qt.yellow) - - pm = QPixmap(24, 100) - pmp = QPainter(pm) - pmp.setBrush(QBrush(grBtoY)) - pmp.setPen(Qt.NoPen) - pmp.drawRect(0, 0, 24, 100) - pmp.end() - - gradientBtoYPB = QPushButton(widget) - gradientBtoYPB.setIcon(QIcon(pm)) - gradientBtoYPB.setIconSize(QSize(24, 100)) - - grGtoR = QLinearGradient(0, 0, 1, 100) - grGtoR.setColorAt(1.0, Qt.darkGreen) - grGtoR.setColorAt(0.5, Qt.yellow) - grGtoR.setColorAt(0.2, Qt.red) - grGtoR.setColorAt(0.0, Qt.darkRed) - pmp.begin(pm) - pmp.setBrush(QBrush(grGtoR)) - pmp.drawRect(0, 0, 24, 100) - pmp.end() - - gradientGtoRPB = QPushButton(widget) - gradientGtoRPB.setIcon(QIcon(pm)) - gradientGtoRPB.setIconSize(QSize(24, 100)) - - colorHBox = QHBoxLayout() - colorHBox.addWidget(gradientBtoYPB) - colorHBox.addWidget(gradientGtoRPB) - colorGroupBox.setLayout(colorHBox) - - vLayout.addWidget(modelGroupBox) - vLayout.addWidget(selectionGroupBox) - vLayout.addWidget(QLabel("Column range")) - vLayout.addWidget(axisMinSliderX) - vLayout.addWidget(axisMaxSliderX) - vLayout.addWidget(QLabel("Row range")) - vLayout.addWidget(axisMinSliderZ) - vLayout.addWidget(axisMaxSliderZ) - vLayout.addWidget(QLabel("Theme")) - vLayout.addWidget(themeList) - vLayout.addWidget(colorGroupBox) - - widget.show() - - modifier = SurfaceGraph(graph) - - heightMapModelRB.toggled.connect(modifier.enableHeightMapModel) - sqrtSinModelRB.toggled.connect(modifier.enableSqrtSinModel) - modeNoneRB.toggled.connect(modifier.toggleModeNone) - modeItemRB.toggled.connect(modifier.toggleModeItem) - modeSliceRowRB.toggled.connect(modifier.toggleModeSliceRow) - modeSliceColumnRB.toggled.connect(modifier.toggleModeSliceColumn) - axisMinSliderX.valueChanged.connect(modifier.adjustXMin) - axisMaxSliderX.valueChanged.connect(modifier.adjustXMax) - axisMinSliderZ.valueChanged.connect(modifier.adjustZMin) - axisMaxSliderZ.valueChanged.connect(modifier.adjustZMax) - themeList.currentIndexChanged[int].connect(modifier.changeTheme) - gradientBtoYPB.pressed.connect(modifier.setBlackToYellowGradient) - gradientGtoRPB.pressed.connect(modifier.setGreenToRedGradient) - - modifier.setAxisMinSliderX(axisMinSliderX) - modifier.setAxisMaxSliderX(axisMaxSliderX) - modifier.setAxisMinSliderZ(axisMinSliderZ) - modifier.setAxisMaxSliderZ(axisMaxSliderZ) - - sqrtSinModelRB.setChecked(True) - modeItemRB.setChecked(True) - themeList.setCurrentIndex(2) + window = Window(graph) + window.setWindowTitle("Surface example") + window.show() sys.exit(app.exec()) diff --git a/examples/datavisualization/surface_numpy/surfacegraph.py b/examples/datavisualization/surface_numpy/surfacegraph.py index 88ef7e36a..c95cec2a0 100644 --- a/examples/datavisualization/surface_numpy/surfacegraph.py +++ b/examples/datavisualization/surface_numpy/surfacegraph.py @@ -52,12 +52,12 @@ from PySide6.QtDataVisualization import (Q3DTheme, QAbstract3DGraph, from PySide6.QtGui import QImage, QLinearGradient from PySide6.QtWidgets import QSlider -sampleCountX = 50 -sampleCountZ = 50 -heightMapGridStepX = 6 -heightMapGridStepZ = 6 -sampleMin = -8.0 -sampleMax = 8.0 +SAMPLE_COUNT_X = 50 +SAMPLE_COUNT_Z = 50 +HEIGHT_MAP_GRID_STEP_X = 6 +HEIGHT_MAP_GRID_STEP_Z = 6 +SAMPLE_MIN = -8.0 +SAMPLE_MAX = 8.0 X_ROLE = Qt.UserRole + 1 @@ -69,20 +69,20 @@ class SqrtSinModel(QAbstractTableModel): def __init__(self, parent=None): super().__init__(parent) - self._x = np.zeros(sampleCountX) - self._z = np.zeros(sampleCountZ) - self._data = np.zeros((sampleCountZ, sampleCountX)) + self._x = np.zeros(SAMPLE_COUNT_X) + self._z = np.zeros(SAMPLE_COUNT_Z) + self._data = np.zeros((SAMPLE_COUNT_Z, SAMPLE_COUNT_X)) - stepX = (sampleMax - sampleMin) / float(sampleCountX - 1) - stepZ = (sampleMax - sampleMin) / float(sampleCountZ - 1) + step_x = (SAMPLE_MAX - SAMPLE_MIN) / float(SAMPLE_COUNT_X - 1) + step_z = (SAMPLE_MAX - SAMPLE_MIN) / float(SAMPLE_COUNT_Z - 1) - for i in range(sampleCountZ): + for i in range(SAMPLE_COUNT_Z): # Keep values within range bounds, since just adding step can cause # minor drift due to the rounding errors. - z = min(sampleMax, (i * stepZ + sampleMin)) + z = min(SAMPLE_MAX, (i * step_z + SAMPLE_MIN)) self._z[i] = z - for j in range(sampleCountX): - x = min(sampleMax, (j * stepX + sampleMin)) + for j in range(SAMPLE_COUNT_X): + x = min(SAMPLE_MAX, (j * step_x + SAMPLE_MIN)) self._x[j] = x R = math.sqrt(z * z + x * x) + 0.01 y = (math.sin(R) / R + 0.24) * 1.61 @@ -131,15 +131,15 @@ class SurfaceGraph(QObject): self.m_sqrtSinSeries = QSurface3DSeries(self.m_sqrtSinProxy) - imageFile = Path(__file__).parent.parent / "surface" / "mountain.png" - heightMapImage = QImage(imageFile) - self.m_heightMapProxy = QHeightMapSurfaceDataProxy(heightMapImage) + image_file = Path(__file__).parent.parent / "surface" / "mountain.png" + height_map_image = QImage(image_file) + self.m_heightMapProxy = QHeightMapSurfaceDataProxy(height_map_image) self.m_heightMapSeries = QSurface3DSeries(self.m_heightMapProxy) self.m_heightMapSeries.setItemLabelFormat("(@xLabel, @zLabel): @yLabel") self.m_heightMapProxy.setValueRanges(34.0, 40.0, 18.0, 24.0) - self.m_heightMapWidth = heightMapImage.width() - self.m_heightMapHeight = heightMapImage.height() + self.m_heightMapWidth = height_map_image.width() + self.m_heightMapHeight = height_map_image.height() self.m_axisMinSliderX = QSlider() self.m_axisMaxSliderX = QSlider() @@ -150,16 +150,16 @@ class SurfaceGraph(QObject): self.m_stepX = 0.0 self.m_stepZ = 0.0 - def enableSqrtSinModel(self, enable): + def enable_sqrt_sin_model(self, enable): if enable: self.m_sqrtSinSeries.setDrawMode(QSurface3DSeries.DrawSurfaceAndWireframe) self.m_sqrtSinSeries.setFlatShadingEnabled(True) self.m_graph.axisX().setLabelFormat("%.2f") self.m_graph.axisZ().setLabelFormat("%.2f") - self.m_graph.axisX().setRange(sampleMin, sampleMax) + self.m_graph.axisX().setRange(SAMPLE_MIN, SAMPLE_MAX) self.m_graph.axisY().setRange(0.0, 2.0) - self.m_graph.axisZ().setRange(sampleMin, sampleMax) + self.m_graph.axisZ().setRange(SAMPLE_MIN, SAMPLE_MAX) self.m_graph.axisX().setLabelAutoRotation(30) self.m_graph.axisY().setLabelAutoRotation(90) self.m_graph.axisZ().setLabelAutoRotation(30) @@ -168,20 +168,20 @@ class SurfaceGraph(QObject): self.m_graph.addSeries(self.m_sqrtSinSeries) # Reset range sliders for Sqrt&Sin - self.m_rangeMinX = sampleMin - self.m_rangeMinZ = sampleMin - self.m_stepX = (sampleMax - sampleMin) / float(sampleCountX - 1) - self.m_stepZ = (sampleMax - sampleMin) / float(sampleCountZ - 1) - self.m_axisMinSliderX.setMaximum(sampleCountX - 2) + self.m_rangeMinX = SAMPLE_MIN + self.m_rangeMinZ = SAMPLE_MIN + self.m_stepX = (SAMPLE_MAX - SAMPLE_MIN) / float(SAMPLE_COUNT_X - 1) + self.m_stepZ = (SAMPLE_MAX - SAMPLE_MIN) / float(SAMPLE_COUNT_Z - 1) + self.m_axisMinSliderX.setMaximum(SAMPLE_COUNT_X - 2) self.m_axisMinSliderX.setValue(0) - self.m_axisMaxSliderX.setMaximum(sampleCountX - 1) - self.m_axisMaxSliderX.setValue(sampleCountX - 1) - self.m_axisMinSliderZ.setMaximum(sampleCountZ - 2) + self.m_axisMaxSliderX.setMaximum(SAMPLE_COUNT_X - 1) + self.m_axisMaxSliderX.setValue(SAMPLE_COUNT_X - 1) + self.m_axisMinSliderZ.setMaximum(SAMPLE_COUNT_Z - 2) self.m_axisMinSliderZ.setValue(0) - self.m_axisMaxSliderZ.setMaximum(sampleCountZ - 1) - self.m_axisMaxSliderZ.setValue(sampleCountZ - 1) + self.m_axisMaxSliderZ.setMaximum(SAMPLE_COUNT_Z - 1) + self.m_axisMaxSliderZ.setValue(SAMPLE_COUNT_Z - 1) - def enableHeightMapModel(self, enable): + def enable_height_map_model(self, enable): if enable: self.m_heightMapSeries.setDrawMode(QSurface3DSeries.DrawSurface) self.m_heightMapSeries.setFlatShadingEnabled(False) @@ -200,76 +200,76 @@ class SurfaceGraph(QObject): self.m_graph.addSeries(self.m_heightMapSeries) # Reset range sliders for height map - mapGridCountX = self.m_heightMapWidth / heightMapGridStepX - mapGridCountZ = self.m_heightMapHeight / heightMapGridStepZ + map_grid_count_x = self.m_heightMapWidth / HEIGHT_MAP_GRID_STEP_X + map_grid_count_z = self.m_heightMapHeight / HEIGHT_MAP_GRID_STEP_Z self.m_rangeMinX = 34.0 self.m_rangeMinZ = 18.0 - self.m_stepX = 6.0 / float(mapGridCountX - 1) - self.m_stepZ = 6.0 / float(mapGridCountZ - 1) - self.m_axisMinSliderX.setMaximum(mapGridCountX - 2) + self.m_stepX = 6.0 / float(map_grid_count_x - 1) + self.m_stepZ = 6.0 / float(map_grid_count_z - 1) + self.m_axisMinSliderX.setMaximum(map_grid_count_x - 2) self.m_axisMinSliderX.setValue(0) - self.m_axisMaxSliderX.setMaximum(mapGridCountX - 1) - self.m_axisMaxSliderX.setValue(mapGridCountX - 1) - self.m_axisMinSliderZ.setMaximum(mapGridCountZ - 2) + self.m_axisMaxSliderX.setMaximum(map_grid_count_x - 1) + self.m_axisMaxSliderX.setValue(map_grid_count_x - 1) + self.m_axisMinSliderZ.setMaximum(map_grid_count_z - 2) self.m_axisMinSliderZ.setValue(0) - self.m_axisMaxSliderZ.setMaximum(mapGridCountZ - 1) - self.m_axisMaxSliderZ.setValue(mapGridCountZ - 1) + self.m_axisMaxSliderZ.setMaximum(map_grid_count_z - 1) + self.m_axisMaxSliderZ.setValue(map_grid_count_z - 1) - def adjustXMin(self, minimum): - minX = self.m_stepX * float(minimum) + self.m_rangeMinX + def adjust_xmin(self, minimum): + min_x = self.m_stepX * float(minimum) + self.m_rangeMinX maximum = self.m_axisMaxSliderX.value() if minimum >= maximum: maximum = minimum + 1 self.m_axisMaxSliderX.setValue(maximum) - maxX = self.m_stepX * maximum + self.m_rangeMinX + max_x = self.m_stepX * maximum + self.m_rangeMinX - self.setAxisXRange(minX, maxX) + self.set_axis_xrange(min_x, max_x) - def adjustXMax(self, maximum): - maxX = self.m_stepX * float(maximum) + self.m_rangeMinX + def adjust_xmax(self, maximum): + max_x = self.m_stepX * float(maximum) + self.m_rangeMinX minimum = self.m_axisMinSliderX.value() if maximum <= minimum: minimum = maximum - 1 self.m_axisMinSliderX.setValue(minimum) - minX = self.m_stepX * minimum + self.m_rangeMinX + min_x = self.m_stepX * minimum + self.m_rangeMinX - self.setAxisXRange(minX, maxX) + self.set_axis_xrange(min_x, max_x) - def adjustZMin(self, minimum): - minZ = self.m_stepZ * float(minimum) + self.m_rangeMinZ + def adjust_zmin(self, minimum): + min_z = self.m_stepZ * float(minimum) + self.m_rangeMinZ maximum = self.m_axisMaxSliderZ.value() if minimum >= maximum: maximum = minimum + 1 self.m_axisMaxSliderZ.setValue(maximum) - maxZ = self.m_stepZ * maximum + self.m_rangeMinZ + max_z = self.m_stepZ * maximum + self.m_rangeMinZ - self.setAxisZRange(minZ, maxZ) + self.set_axis_zrange(min_z, max_z) - def adjustZMax(self, maximum): - maxX = self.m_stepZ * float(maximum) + self.m_rangeMinZ + def adjust_zmax(self, maximum): + max_x = self.m_stepZ * float(maximum) + self.m_rangeMinZ minimum = self.m_axisMinSliderZ.value() if maximum <= minimum: minimum = maximum - 1 self.m_axisMinSliderZ.setValue(minimum) - minX = self.m_stepZ * minimum + self.m_rangeMinZ + min_x = self.m_stepZ * minimum + self.m_rangeMinZ - self.setAxisZRange(minX, maxX) + self.set_axis_zrange(min_x, max_x) - def setAxisXRange(self, minimum, maximum): + def set_axis_xrange(self, minimum, maximum): self.m_graph.axisX().setRange(minimum, maximum) - def setAxisZRange(self, minimum, maximum): + def set_axis_zrange(self, minimum, maximum): self.m_graph.axisZ().setRange(minimum, maximum) @Slot() - def changeTheme(self, theme): + def change_theme(self, theme): self.m_graph.activeTheme().setType(Q3DTheme.Theme(theme)) - def setBlackToYellowGradient(self): + def set_black_to_yellow_gradient(self): gr = QLinearGradient() gr.setColorAt(0.0, Qt.black) gr.setColorAt(0.33, Qt.blue) @@ -280,7 +280,7 @@ class SurfaceGraph(QObject): series.setBaseGradient(gr) series.setColorStyle(Q3DTheme.ColorStyleRangeGradient) - def setGreenToRedGradient(self): + def set_green_to_red_gradient(self): gr = QLinearGradient() gr.setColorAt(0.0, Qt.darkGreen) gr.setColorAt(0.5, Qt.yellow) @@ -291,30 +291,30 @@ class SurfaceGraph(QObject): series.setBaseGradient(gr) series.setColorStyle(Q3DTheme.ColorStyleRangeGradient) - def toggleModeNone(self): + def toggle_mode_none(self): self.m_graph.setSelectionMode(QAbstract3DGraph.SelectionNone) - def toggleModeItem(self): + def toggle_mode_item(self): self.m_graph.setSelectionMode(QAbstract3DGraph.SelectionItem) - def toggleModeSliceRow(self): + def toggle_mode_slice_row(self): self.m_graph.setSelectionMode( QAbstract3DGraph.SelectionItemAndRow | QAbstract3DGraph.SelectionSlice ) - def toggleModeSliceColumn(self): + def toggle_mode_slice_column(self): self.m_graph.setSelectionMode( QAbstract3DGraph.SelectionItemAndColumn | QAbstract3DGraph.SelectionSlice ) - def setAxisMinSliderX(self, slider): + def set_axis_min_slider_x(self, slider): self.m_axisMinSliderX = slider - def setAxisMaxSliderX(self, slider): + def set_axis_max_slider_x(self, slider): self.m_axisMaxSliderX = slider - def setAxisMinSliderZ(self, slider): + def set_axis_min_slider_z(self, slider): self.m_axisMinSliderZ = slider - def setAxisMaxSliderZ(self, slider): + def set_axis_max_slider_z(self, slider): self.m_axisMaxSliderZ = slider |