diff --git a/vasl_templates/main_window.py b/vasl_templates/main_window.py index 91547c0..63f5e7d 100644 --- a/vasl_templates/main_window.py +++ b/vasl_templates/main_window.py @@ -245,17 +245,24 @@ class MainWindow( QWidget ): """Called when the user wants to load a scenario.""" self._web_channel_handler.on_new_scenario() - @pyqtSlot( result=str ) + @pyqtSlot( result=QVariant ) @catch_exceptions( caption="SLOT EXCEPTION" ) def load_scenario( self ): """Called when the user wants to load a scenario.""" - return self._web_channel_handler.load_scenario() + fname, data = self._web_channel_handler.load_scenario() + if data is None: + return None + return QVariant( { + "filename": fname, + "data": data + } ) - @pyqtSlot( str, result=bool ) + @pyqtSlot( str, str, result=str ) @catch_exceptions( caption="SLOT EXCEPTION", retval=False ) - def save_scenario( self, data ): + def save_scenario( self, fname, data ): """Called when the user wants to save a scenario.""" - return self._web_channel_handler.save_scenario( data ) + fname = self._web_channel_handler.save_scenario( fname, data ) + return fname @pyqtSlot( result=QVariant ) @catch_exceptions( caption="SLOT EXCEPTION" ) diff --git a/vasl_templates/web_channel.py b/vasl_templates/web_channel.py index 688da36..50c72b9 100644 --- a/vasl_templates/web_channel.py +++ b/vasl_templates/web_channel.py @@ -20,7 +20,7 @@ class WebChannelHandler: self.parent, "scenario", ".json", "Scenario files (*.json);;All files (*)", - "scenario.json" + None ) self.updated_vsav_file_dialog = FileDialog( self.parent, @@ -35,11 +35,23 @@ class WebChannelHandler: def load_scenario( self ): """Called when the user wants to load a scenario.""" - return self.scenario_file_dialog.load_file( False ) + data = self.scenario_file_dialog.load_file( False ) + if data is None: + return None, None + return self.scenario_file_dialog.curr_fname, data - def save_scenario( self, data ): + def save_scenario( self, fname, data ): """Called when the user wants to save a scenario.""" - return self.scenario_file_dialog.save_file( data ) + prev_curr_fname = self.scenario_file_dialog.curr_fname + if not self.scenario_file_dialog.curr_fname: + # NOTE: We are tracking the current scenario filename ourself, so we only use the filename + # passed to us by the web page if a new scenario is being saved for the first time. + self.scenario_file_dialog.curr_fname = fname + rc = self.scenario_file_dialog.save_file( data ) + if not rc: + self.scenario_file_dialog.curr_fname = prev_curr_fname + return None + return self.scenario_file_dialog.curr_fname def on_scenario_name_change( self, val ): """Update the main window title to show the scenario name.""" diff --git a/vasl_templates/webapp/static/snippets.js b/vasl_templates/webapp/static/snippets.js index 9bf08cf..65c61ef 100644 --- a/vasl_templates/webapp/static/snippets.js +++ b/vasl_templates/webapp/static/snippets.js @@ -1123,10 +1123,10 @@ function on_load_scenario() // if we are running inside the PyQt wrapper, let it handle everything if ( gWebChannelHandler ) { - gWebChannelHandler.load_scenario( function( data ) { - if ( ! data ) + gWebChannelHandler.load_scenario( function( resp ) { + if ( ! resp ) return ; - do_load_scenario( data, null ) ; + do_load_scenario( resp.data, resp.filename ) ; } ) ; return ; } @@ -1399,28 +1399,44 @@ function on_save_scenario() return ; } + // generate the save filename + var save_fname = gLastSavedScenarioFilename ; + if ( ! save_fname ) { + var scenario_name = params.SCENARIO_NAME.trim() ; + var scenario_id = params.SCENARIO_ID.trim() ; + if ( scenario_name && scenario_id ) + save_fname = scenario_name + " (" + scenario_id + ").json" ; + else if ( scenario_name ) + save_fname = scenario_name + ".json" ; + else if ( scenario_id ) + save_fname = scenario_id + ".json" ; + else + save_fname = "scenario.json" ; + } + // if we are running inside the PyQt wrapper, let it handle everything if ( gWebChannelHandler ) { - gWebChannelHandler.save_scenario( data, function( result ) { - if ( ! result ) + gWebChannelHandler.save_scenario( save_fname, data, function( save_fname ) { + if ( ! save_fname ) return ; gLastSavedScenario = params ; + gLastSavedScenarioFilename = save_fname ; showInfoMsg( "The scenario was saved." ) ; } ) ; return ; } // return the parameters to the user as a downloadable file - download( data, - gLastSavedScenarioFilename ? gLastSavedScenarioFilename : "scenario.json", - "application/json" - ) ; + download( data, save_fname, "application/json" ) ; + // NOTE: We get no indication if the download was successful, so we can't show feedback :-/ // Also, if the download didn't actually happen (e.g. because it was cancelled), then setting // the last saved scenario here is not quite the right thing to do, since subsequent checks // for a dirty scenario will return the wrong result, since they assume that the scenario // was saved properly here :-/ gLastSavedScenario = params ; + // NOTE: It would be nice to set gLastSavedScenarioFilename here, but this will give the wrong behaviour + // if the user loads a scenario from a file that is named using a non-standard convention. } function unload_params_for_save( user_requested )