From 911be971bca76a967c61df6562eb88436289ef0d Mon Sep 17 00:00:00 2001 From: Taka Date: Fri, 19 Jul 2019 08:41:35 +0000 Subject: [PATCH] Added the ability to run a search query using a shortcut. --- data/search-queries.json | 7 +++++++ src/ChartImage.cs | 8 ++++++-- src/MainForm.cs | 2 ++ src/Program.cs | 4 ++++ src/Program2.cs | 26 +++++++++++++++++++++++++- src/Shortcut.cs | 24 ++++++++++++++++++++++++ 6 files changed, 68 insertions(+), 3 deletions(-) create mode 100644 data/search-queries.json diff --git a/data/search-queries.json b/data/search-queries.json new file mode 100644 index 0000000..ff09867 --- /dev/null +++ b/data/search-queries.json @@ -0,0 +1,7 @@ +[ + +{ "query": "to", + "shortcut": "Ctrl-T" +} + +] diff --git a/src/ChartImage.cs b/src/ChartImage.cs index d62664d..46d7292 100644 --- a/src/ChartImage.cs +++ b/src/ChartImage.cs @@ -40,8 +40,12 @@ public class ChartImage shortcut = new ChartImageShortcut( keys.Value ) ; Shortcut.registerShortcut( shortcut ) ; } - logger.Info( $"Registering shortcut: {shortcut.ToString()} => {caption()}" ) ; - ((ChartImageShortcut)shortcut).addChartImage( this ) ; + if ( shortcut is ChartImageShortcut ) { + logger.Info( $"Registering image shortcut: {shortcut} => {caption()}" ) ; + ((ChartImageShortcut)shortcut).addChartImage( this ) ; + } + else + Program.logStartupMsg( "bad-shortcut", $"Found duplicate: {shortcut}" ) ; } } diff --git a/src/MainForm.cs b/src/MainForm.cs index 6f4ebb6..d01061e 100644 --- a/src/MainForm.cs +++ b/src/MainForm.cs @@ -201,6 +201,8 @@ public partial class MainForm : Form } } + public void setSearchQuery( string s ) { mSearchQuery.Text = s ; } + private void setChartImagePanelScrollPos( int? hscrollPos, int? vscrollPos ) { // FUDGE! These need to be set twice to take effect on Windows :-/ diff --git a/src/Program.cs b/src/Program.cs index 0686a5e..901d096 100644 --- a/src/Program.cs +++ b/src/Program.cs @@ -65,6 +65,10 @@ public static partial class Program // load the data config mDataConfig = new JsonConfig( "data config", Path.Combine( mDataDir, "config.json" ) ) ; + // load pre-defined search queries + fname = Path.Combine( mDataDir, "search-queries.json" ) ; + loadPredefinedSearchQueries( fname ) ; + // load the debug config mDebugConfig = new JsonConfig( "debug config", Path.Combine( mBaseDir, "debug.json" ) ) ; diff --git a/src/Program2.cs b/src/Program2.cs index c3bed1d..645633c 100644 --- a/src/Program2.cs +++ b/src/Program2.cs @@ -1,5 +1,6 @@ -using System.Security ; +using System ; using System.Text ; +using System.Security ; using System.Windows.Forms ; using System.Collections.Generic ; @@ -19,6 +20,29 @@ public static partial class Program } } + private static void loadPredefinedSearchQueries( string fname ) + { + // load the pre-defined search queries + JsonConfig jsonConfig = new JsonConfig( "pre-defined search queries", fname ) ; + ILog logger = LogManager.GetLogger( "shortcuts" ) ; + foreach( dynamic entry in jsonConfig.data ) { + string queryString = entry["query"] ?? "" ; + if ( queryString == "" ) { + Program.logStartupMsg( "bad-shortcut", $"{entry["shortcut"]}: Missing query string" ) ; + continue ; + } + string shortcutString = entry["shortcut"] ?? "" ; + Keys? keys = Shortcut.parseShortcutString( (string) shortcutString ) ; + if ( keys == null ) { + Program.logStartupMsg( "bad-shortcut", $"{queryString}: {shortcutString}" ) ; + continue ; + } + Shortcut shortcut = new SearchQueryShortcut( keys.Value, queryString ) ; + logger.Info( $"Registering search query shortcut: {shortcut} => {queryString}" ) ; + Shortcut.registerShortcut( shortcut ) ; + } + } + public static void logStartupMsg( string key, string msg ) { // log the startup message diff --git a/src/Shortcut.cs b/src/Shortcut.cs index 511546d..d352b3a 100644 --- a/src/Shortcut.cs +++ b/src/Shortcut.cs @@ -61,6 +61,9 @@ public abstract class Shortcut public static void registerShortcut( Shortcut shortcut ) { // register the shortcut + if ( findRegisteredShortcut( shortcut.mKeys ) != null ) { + Program.logStartupMsg( "bad-shortcut", $"Found duplicate: {shortcut}" ) ; + } mRegisteredShortcuts[ shortcut.mKeys ] = shortcut ; } @@ -110,3 +113,24 @@ public class ChartImageShortcut : Shortcut public void addChartImage( ChartImage ci ) { mChartImages.Add( ci ) ; } } + +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + +public class SearchQueryShortcut : Shortcut +{ + private string mSearchQuery ; + + public SearchQueryShortcut( Keys keys, string searchQuery ) : base( keys ) + { + // initialize + mSearchQuery = searchQuery ; + } + + public override void executeShortcut() + { + // execute the shortcut + Program.mainForm.setSearchQuery( mSearchQuery ) ; + ILog logger = LogManager.GetLogger( "shortcuts" ) ; + logger.Debug( $"- {mSearchQuery}" ) ; + } +}