|
|
|
@ -2,6 +2,7 @@ using System ; |
|
|
|
|
using System.Text ; |
|
|
|
|
using System.IO ; |
|
|
|
|
using System.Collections.Generic ; |
|
|
|
|
using System.Windows.Forms ; |
|
|
|
|
using System.Drawing ; |
|
|
|
|
|
|
|
|
|
using Manina.Windows.Forms ; |
|
|
|
@ -12,6 +13,9 @@ using log4net ; |
|
|
|
|
|
|
|
|
|
public class ChartImage |
|
|
|
|
{ |
|
|
|
|
// NOTE: This table maps shortcuts (e.g. Ctrl-Shift-C) to ChartImage's. |
|
|
|
|
private static Dictionary< Tuple<Keys,Keys>, List<ChartImage> > mShortcuts = new Dictionary< Tuple<Keys,Keys>, List<ChartImage> >() ; |
|
|
|
|
|
|
|
|
|
private string mFullPath ; |
|
|
|
|
private dynamic mConfig ; |
|
|
|
|
private HashSet<string> mKeywords = new HashSet<string>() ; |
|
|
|
@ -27,6 +31,21 @@ public class ChartImage |
|
|
|
|
mConfig = new JObject() ; |
|
|
|
|
mImage = Image.FromFile( fullPath ) ; |
|
|
|
|
|
|
|
|
|
// parse the shortcut |
|
|
|
|
ILog logger = LogManager.GetLogger( "shortcuts" ) ; |
|
|
|
|
string val = mConfig[ "shortcut" ] ; |
|
|
|
|
if ( val != null ) { |
|
|
|
|
Tuple<Keys,Keys> shortcut = parseShortcut( val ) ; |
|
|
|
|
if ( shortcut == null ) |
|
|
|
|
logger.Warn( $"Can't parse shortcut: {val}" ) ; |
|
|
|
|
else { |
|
|
|
|
if ( ! mShortcuts.ContainsKey( shortcut ) ) |
|
|
|
|
mShortcuts[ shortcut ] = new List<ChartImage>() ; |
|
|
|
|
logger.Info( $"Registering shortcut: {shortcutString(shortcut.Item1,shortcut.Item2)} => ${caption()}" ) ; |
|
|
|
|
mShortcuts[ shortcut ].Add( this ) ; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// prepare for search scoring |
|
|
|
|
if ( mConfig["keywords"] != null ) { |
|
|
|
|
foreach( string kywd in mConfig["keywords"] ) |
|
|
|
@ -124,6 +143,51 @@ public class ChartImage |
|
|
|
|
return totalScore ; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private Tuple<Keys,Keys> parseShortcut( string val ) |
|
|
|
|
{ |
|
|
|
|
// parse the shortcut |
|
|
|
|
string[] parts = val.ToLower().Split( "-" ) ; |
|
|
|
|
Keys modifiers = 0 ; |
|
|
|
|
for ( int i=0 ; i < parts.Length-1 ; ++i ) { |
|
|
|
|
if ( parts[i] == "ctrl" ) |
|
|
|
|
modifiers |= Keys.Control ; |
|
|
|
|
else if ( parts[i] == "alt" ) |
|
|
|
|
modifiers |= Keys.Alt ; |
|
|
|
|
else if ( parts[i] == "shift" ) |
|
|
|
|
modifiers |= Keys.Shift ; |
|
|
|
|
else |
|
|
|
|
return null ; |
|
|
|
|
} |
|
|
|
|
Keys key ; |
|
|
|
|
bool rc = Enum.TryParse( parts[parts.Length-1], true, out key ) ; |
|
|
|
|
if ( ! rc ) |
|
|
|
|
return null ; |
|
|
|
|
return new Tuple<Keys,Keys>( modifiers, key ) ; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static List<ChartImage> checkShortcut( Keys modifiers, Keys key ) |
|
|
|
|
{ |
|
|
|
|
// check if there are any ChartImage's associated with the specified shortcut |
|
|
|
|
List<ChartImage> chartImages ; |
|
|
|
|
if ( ! mShortcuts.TryGetValue( new Tuple<Keys,Keys>( modifiers, key ), out chartImages ) ) |
|
|
|
|
return null ; |
|
|
|
|
return chartImages ; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static string shortcutString( Keys modifiers, Keys key ) |
|
|
|
|
{ |
|
|
|
|
// return the shortcut as a string |
|
|
|
|
StringBuilder buf = new StringBuilder() ; |
|
|
|
|
if ( (modifiers & Keys.Control) != 0 ) |
|
|
|
|
buf.Append( "Ctrl-" ) ; |
|
|
|
|
if ( (modifiers & Keys.Shift) != 0 ) |
|
|
|
|
buf.Append( "Shift-" ) ; |
|
|
|
|
if ( (modifiers & Keys.Alt) != 0 ) |
|
|
|
|
buf.Append( "Alt-" ) ; |
|
|
|
|
buf.Append( key ) ; |
|
|
|
|
return buf.ToString() ; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public string caption() |
|
|
|
|
{ |
|
|
|
|
string caption = (mConfig != null) ? mConfig["caption"] : null ; |
|
|
|
|