Show the scenario name in the window title.

master
Pacman Ghost 6 years ago
parent e9bea3a165
commit 2ae8dd1589
  1. 49
      vasl_templates/main_window.py
  2. 17
      vasl_templates/webapp/static/main.js
  3. 14
      vasl_templates/webapp/static/snippets.js
  4. 1
      vasl_templates/webapp/templates/index.html
  5. 12
      vasl_templates/webapp/tests/test_scenario_persistence.py

@ -4,14 +4,32 @@ import os
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QLabel, QFileDialog, QMessageBox
from PyQt5.QtWebEngineWidgets import QWebEngineView, QWebEngineProfile, QWebEnginePage
from PyQt5.QtWebChannel import QWebChannel
from PyQt5.QtGui import QDesktopServices
from PyQt5.QtCore import QUrl
from PyQt5.QtCore import QObject, QUrl, pyqtSlot
from vasl_templates.webapp.config.constants import APP_NAME
from vasl_templates.webapp import app as webapp
# ---------------------------------------------------------------------
class WebChannelHandler( QObject ):
"""Handle web channel requests."""
def __init__( self, window ):
# initialize
super().__init__()
self.window = window
@pyqtSlot( str )
def on_scenario_name_change( self, val ):
"""Update the main window title to show the scenario name."""
self.window.setWindowTitle(
"{} - {}".format( APP_NAME, val ) if val else APP_NAME
)
# ---------------------------------------------------------------------
class MainWindow( QWidget ):
"""Main application window."""
@ -38,19 +56,39 @@ class MainWindow( QWidget ):
# that way. Sigh...
layout = QVBoxLayout( self )
if not webapp.config.get( "DISABLE_WEBENGINEVIEW" ):
# load the webapp
# NOTE: We create an off-the-record profile to stop the view from using cached JS files :-/
# initialize the web view
self.view = QWebEngineView()
layout.addWidget( self.view )
# initialize the web page
# nb: we create an off-the-record profile to stop the view from using cached JS files :-/
profile = QWebEngineProfile( None, self.view )
profile.downloadRequested.connect( self.onDownloadRequested )
page = QWebEnginePage( profile, self.view )
self.view.setPage( page )
# create a web channel to communicate with the front-end
web_channel = QWebChannel( page )
# FUDGE! We would like to register a WebChannelHandler instance as the handler, but this crashes PyQt :-/
# Instead, we register ourself as the handler, and delegate processing to a WebChannelHandler.
# The downside is that PyQt emits lots of warnings about our member variables not being properties :-/
self.web_channel_handler = WebChannelHandler( self )
web_channel.registerObject( "handler", self )
page.setWebChannel( web_channel )
# load the webapp
url += "?pyqt=1"
self.view.load( QUrl(url) )
else:
# show a minimal UI
label = QLabel()
label.setText( "Running the {} application.\n\nClose this window when you're done.".format( APP_NAME ) )
layout.addWidget( label )
# open the webapp in an external browser
QDesktopServices.openUrl( QUrl(url) )
def closeEvent( self, evt ) :
@ -106,3 +144,8 @@ class MainWindow( QWidget ):
item.setPath( fname )
item.accept()
MainWindow._curr_scenario_fname = fname
@pyqtSlot( str )
def on_scenario_name_change( self, val ):
"""Update the main window title to show the scenario name."""
self.web_channel_handler.on_scenario_name_change( val )

@ -3,6 +3,8 @@ gDefaultNationalities = {} ;
gValidTemplateIds = [] ;
gVehicleOrdnanceListings = {} ;
gWebChannelHandler = null ;
var _NATIONALITY_SPECIFIC_BUTTONS = {
"russian": [ "mol", "mol-p" ],
"german": [ "pf", "psk", "atmm" ],
@ -14,6 +16,16 @@ var _NATIONALITY_SPECIFIC_BUTTONS = {
$(document).ready( function () {
// initialize the PyQt web channel
if ( getUrlParam( "pyqt" ) ) {
$.getScript( "qrc:///qtwebchannel/qwebchannel.js", function() {
// connect to the web channel
new QWebChannel( qt.webChannelTransport, function(channel) {
gWebChannelHandler = channel.objects.handler ;
} ) ;
} ) ;
}
// initialize the menu
var $menu = $("#menu input") ;
$menu.popmenu( {
@ -288,6 +300,11 @@ $(document).ready( function () {
// enable Ctrl-Enter when editing simple notes
enable_ctrl_enter( $("#edit-simple_note"), "OK" ) ;
// watch for changes to the scenario name
$("input[name='SCENARIO_NAME']").on( "input propertychange paste", function() {
on_scenario_name_change() ;
} ) ;
// initialize hotkeys
init_hotkeys() ;

@ -593,6 +593,7 @@ function do_load_scenario( params )
// update the UI
$("#tabs").tabs( "option", "active", 0 ) ;
on_scenario_name_change() ;
on_scenario_date_change() ;
}
@ -921,3 +922,16 @@ function on_scenario_date_change()
update_ui( "baz", is_baz_available() ) ;
update_ui( "atmm", is_atmm_available() ) ;
}
// --------------------------------------------------------------------
function on_scenario_name_change()
{
// update the document title to include the scenario name
var val = $("input[name='SCENARIO_NAME']").val().trim() ;
document.title = (val.length > 0) ? gAppName+" - "+val : gAppName ;
// notify the PyQt wrapper application
if ( gWebChannelHandler )
gWebChannelHandler.on_scenario_name_change( val ) ;
}

@ -269,6 +269,7 @@
<script src="{{url_for('static',filename='download/download.min.js')}}"></script>
<script src="{{url_for('static',filename='jszip/jszip.min.js')}}"></script>
<script>
gAppName = "{{APP_NAME}}" ;
gImagesBaseUrl = "{{url_for('static',filename='images')}}" ;
gGetTemplatePackUrl = "{{url_for('get_template_pack')}}" ;
gGetDefaultScenarioUrl = "{{url_for('get_default_scenario')}}" ;

@ -5,6 +5,7 @@ import itertools
from selenium.webdriver.support.ui import Select
from vasl_templates.webapp.config.constants import APP_NAME
from vasl_templates.webapp.tests.utils import \
init_webapp, get_nationalities, set_template_params, select_tab, select_menu_option, \
get_sortable_entry_text, get_stored_msg, set_stored_msg, set_stored_msg_marker, find_child, find_children, wait_for
@ -47,6 +48,14 @@ def test_scenario_persistence( webapp, webdriver ): #pylint: disable=too-many-st
nat = args[ player_no-1 ]
assert elem.text.strip() == "{} OB".format( nationalities[nat]["display_name"] )
def check_window_title( expected ):
"""Check the window title."""
if expected:
expected = "{} - {}".format( APP_NAME, expected )
else:
expected = APP_NAME
assert webdriver.title == expected
# load the scenario fields
SCENARIO_PARAMS = {
"scenario": {
@ -91,6 +100,7 @@ def test_scenario_persistence( webapp, webdriver ): #pylint: disable=too-many-st
for tab_id,fields in SCENARIO_PARAMS.items():
select_tab( tab_id )
set_template_params( fields )
check_window_title( "my test scenario" )
check_ob_tabs( "russian", "german" )
# make sure that our test scenario includes everything
@ -115,6 +125,7 @@ def test_scenario_persistence( webapp, webdriver ): #pylint: disable=too-many-st
_ = set_stored_msg_marker( "_last-info_" )
select_menu_option( "new_scenario" )
wait_for( 2, lambda: get_stored_msg("_last-info_") == "The scenario was reset." )
check_window_title( "" )
check_ob_tabs( "german", "russian" )
data = _save_scenario()
data2 = { k: v for k,v in data.items() if v }
@ -137,6 +148,7 @@ def test_scenario_persistence( webapp, webdriver ): #pylint: disable=too-many-st
# load a scenario and make sure it was loaded into the UI correctly
# nb: we just reset the scenario, so we shouldn't get asked to confirm the "load scenario" operation
_load_scenario( saved_scenario )
check_window_title( "my test scenario" )
check_ob_tabs( "russian", "german" )
for tab_id in SCENARIO_PARAMS:
select_tab( tab_id )

Loading…
Cancel
Save