From f029b8c0b669036b01ad844d1d107914b5e768de Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Sylvain=20Th=C3=A9ry?= <sylvain.thery@cnrs.fr>
Date: Tue, 12 Jul 2022 15:39:47 +0200
Subject: [PATCH] improved push message and execptions

---
 active_channel_evolution.py | 146 ++++++++++++++++++++----------------
 1 file changed, 80 insertions(+), 66 deletions(-)

diff --git a/active_channel_evolution.py b/active_channel_evolution.py
index 933b2e9..c38e8b6 100644
--- a/active_channel_evolution.py
+++ b/active_channel_evolution.py
@@ -283,72 +283,86 @@ class active_channel_evolution:
 
         # When OK is pressed
         if result:
-            # we grab the value to use for the width of the cross-sections
-            cross_section_width = self.dlg.spinBoxWidthTransects.value()
-            # we grav the value to use as distance between two transects
-            cross_section_distance = self.dlg.spinBoxDistTransects.value()
-            # we grab the path where to export the layer containing the blocks of the valley bottom
-            export_vb = self.dlg.lineEditExportValleyFloorBlocks.text()
-            # we grab the path where to export the csv containing the blocks of the valley bottom
-            export_csv_vb = self.dlg.lineEditExportTabValleyFloorBlocks.text()
-            # we grab if we have to reverse the centerline of the valley floor
-            flag_reverse_vb = self.dlg.checkBoxReverseValleyFloor.isChecked()
-
-            # we grab the layer of the blocks of the valley bottom to use to process the active channel
-            input_blocks_vb = self.dlg.lineEditInBlocksValleyFloor.text()
-            # we grab the path where to export the layer containing the blocks of the active channel
-            export_ac = self.dlg.lineEditExportActiveBandBlocks.text()
-            export_csv_ac = self.dlg.lineEditExportTabActiveBandBlocks.text()
-
-            # we grab the layer of the valley bottom
-            selected_layer_vb = self.dlg.comboBoxValleyFloor.currentLayer()
-
-            # we grab the layer of the active channel
-            selected_layer_ac = self.dlg.comboBoxActiveBand.currentLayer()
-
-            outputs = {}
-
-            # if the user wants to detect the starting point of the valley bottom
-            if flag_detect_start_point == True:
-                detect_starting_point(QgsProject, outputs, QgsProcessing, processing, selected_layer_vb, name_starting_point)
-
-            # if the user wants to run only the processing of the valley bottom
-            if (flag_run_vb == True and flag_run_ac == False) and flag_detect_start_point == False:
-                valley_bottom_processing(QgsProject, QgsVectorLayer, outputs, QgsProcessing, processing,
-                                         selected_layer_vb, flag_reverse_vb, cross_section_width, cross_section_distance, export_vb,
-                                         export_csv_vb, flag_load_temp_vb, name_valley_bottom_blocks, name_valley_bottom_centerline,
-                                         name_valley_bottom_cross_sections, name_valley_bottom_rk)
-
-            # if the user wants to run only the processing of the active channel
-            if flag_run_ac == True and flag_run_vb == False:
-                active_channel_processing(QgsProject, QgsVectorLayer, outputs, QgsProcessing, processing, selected_layer_ac,
-                                          input_blocks_vb, export_ac, export_csv_ac, flag_load_temp_ac,
-                                          name_active_channel_blocks, name_active_channel_centerline)
-            # if the user wants to run in one time the processing of the valley bottom followed by the processing
-            # of the active channel
-            if flag_run_vb == True and flag_run_ac == True:
-                self.iface.messageBar().pushMessage(self.tr("Step 1 - Valley bottom processing"), level=Qgis.Info, duration=5)
+            try:
+                # we grab the value to use for the width of the cross-sections
+                cross_section_width = self.dlg.spinBoxWidthTransects.value()
+                # we grav the value to use as distance between two transects
+                cross_section_distance = self.dlg.spinBoxDistTransects.value()
+                # we grab the path where to export the layer containing the blocks of the valley bottom
+                export_vb = self.dlg.lineEditExportValleyFloorBlocks.text()
+                # we grab the path where to export the csv containing the blocks of the valley bottom
+                export_csv_vb = self.dlg.lineEditExportTabValleyFloorBlocks.text()
+                # we grab if we have to reverse the centerline of the valley floor
+                flag_reverse_vb = self.dlg.checkBoxReverseValleyFloor.isChecked()
+
+                # we grab the layer of the blocks of the valley bottom to use to process the active channel
+                input_blocks_vb = self.dlg.lineEditInBlocksValleyFloor.text()
+                # we grab the path where to export the layer containing the blocks of the active channel
+                export_ac = self.dlg.lineEditExportActiveBandBlocks.text()
+                export_csv_ac = self.dlg.lineEditExportTabActiveBandBlocks.text()
+
+                # we grab the layer of the valley bottom
+                selected_layer_vb = self.dlg.comboBoxValleyFloor.currentLayer()
+
+                # we grab the layer of the active channel
+                selected_layer_ac = self.dlg.comboBoxActiveBand.currentLayer()
+
+                outputs = {}
+
+                # if the user wants to detect the starting point of the valley bottom
+                if flag_detect_start_point == True:
+                    detect_starting_point(QgsProject, outputs, QgsProcessing, processing, selected_layer_vb, name_starting_point)
+
+                # if the user wants to run only the processing of the valley bottom
+                if (flag_run_vb == True and flag_run_ac == False) and flag_detect_start_point == False:
+                    valley_bottom_processing(QgsProject, QgsVectorLayer, outputs, QgsProcessing, processing,
+                                             selected_layer_vb, flag_reverse_vb, cross_section_width, cross_section_distance, export_vb,
+                                             export_csv_vb, flag_load_temp_vb, name_valley_bottom_blocks, name_valley_bottom_centerline,
+                                             name_valley_bottom_cross_sections, name_valley_bottom_rk)
+
+                # if the user wants to run only the processing of the active channel
+                if flag_run_ac == True and flag_run_vb == False:
+                    active_channel_processing(QgsProject, QgsVectorLayer, outputs, QgsProcessing, processing, selected_layer_ac,
+                                              input_blocks_vb, export_ac, export_csv_ac, flag_load_temp_ac,
+                                              name_active_channel_blocks, name_active_channel_centerline)
+                # if the user wants to run in one time the processing of the valley bottom followed by the processing
+                # of the active channel
+                if flag_run_vb == True and flag_run_ac == True:
+                    self.iface.messageBar().pushMessage(self.tr("Step 1 - Valley bottom processing"), level=Qgis.Info)
+                    self.iface.mainWindow().repaint()
+                    input_blocks_vb = valley_bottom_processing(QgsProject, QgsVectorLayer, outputs, QgsProcessing,
+                                                               processing, selected_layer_vb,
+                                                               flag_reverse_vb, cross_section_width, cross_section_distance, export_vb,
+                                                               export_csv_vb, flag_load_temp_vb, name_valley_bottom_blocks, name_valley_bottom_centerline,
+                                                               name_valley_bottom_cross_sections, name_valley_bottom_rk)
+                    self.iface.messageBar().clearWidgets()
+                    self.iface.messageBar().pushMessage(self.tr("Step 2 - Active channel processing"), level=Qgis.Info)
+                    self.iface.mainWindow().repaint()
+                    active_channel_processing(QgsProject, QgsVectorLayer, outputs, QgsProcessing, processing, selected_layer_ac,
+                                              input_blocks_vb, export_ac, export_csv_ac, flag_load_temp_ac,
+                                              name_active_channel_blocks, name_active_channel_centerline)
+                a = 5/0
+                # Show success message when finished
+                self.iface.messageBar().clearWidgets()
+                self.iface.messageBar().pushMessage("♪♪ This is the End, my only friend, the End ♪♪", level=Qgis.Success,
+                                                    duration=5)
+
+                # we disconnect from the different menus
+                self.dlg.pushButtonExportValleyFloorBlocks.clicked.disconnect(self.select_output_blocks_vb)
+                self.dlg.pushButtonExportTabValleyFloorBlocks.clicked.disconnect(self.select_output_tab_blocks_vb)
+                self.dlg.pushButtonExportActiveBandBlocks.clicked.disconnect(self.select_output_blocks_ac)
+                self.dlg.pushButtonExportTabActiveBandBlocks.clicked.disconnect(self.select_output_tab_blocks_ac)
+                self.dlg.pushButtonInBlocksValleyFloor.clicked.disconnect(self.select_input_blocks_vb)
+
+            except Exception as inst:
+                # print info in console
+                print(type(inst))  # the exception instance
+                print(inst.args)  # arguments stored in .args
+                print(inst)
+                # send a message to the user
+                self.iface.messageBar().clearWidgets()
                 self.iface.mainWindow().repaint()
-                input_blocks_vb = valley_bottom_processing(QgsProject, QgsVectorLayer, outputs, QgsProcessing,
-                                                           processing, selected_layer_vb,
-                                                           flag_reverse_vb, cross_section_width, cross_section_distance, export_vb,
-                                                           export_csv_vb, flag_load_temp_vb, name_valley_bottom_blocks, name_valley_bottom_centerline,
-                                                           name_valley_bottom_cross_sections, name_valley_bottom_rk)
+                self.iface.messageBar().pushMessage(self.tr("Something went wrong. See Python console for details :/"),
+                                                    level=Qgis.Critical)
 
-                self.iface.messageBar().pushMessage(self.tr("Step 2 - Active channel processing"), level=Qgis.Info, duration=5)
-                self.iface.mainWindow().repaint()
-                active_channel_processing(QgsProject, QgsVectorLayer, outputs, QgsProcessing, processing, selected_layer_ac,
-                                          input_blocks_vb, export_ac, export_csv_ac, flag_load_temp_ac,
-                                          name_active_channel_blocks, name_active_channel_centerline)
-
-            # Show success message when finished
-            self.iface.messageBar().pushMessage("♪♪ This is the End, my only friend, the End ♪♪", level=Qgis.Success,
-                                                duration=3)
-
-            # we deconnect from the different menus
-            self.dlg.pushButtonExportValleyFloorBlocks.clicked.disconnect(self.select_output_blocks_vb)
-            self.dlg.pushButtonExportTabValleyFloorBlocks.clicked.disconnect(self.select_output_tab_blocks_vb)
 
-            self.dlg.pushButtonExportActiveBandBlocks.clicked.disconnect(self.select_output_blocks_ac)
-            self.dlg.pushButtonExportTabActiveBandBlocks.clicked.disconnect(self.select_output_tab_blocks_ac)
-            self.dlg.pushButtonInBlocksValleyFloor.clicked.disconnect(self.select_input_blocks_vb)
\ No newline at end of file
-- 
GitLab