using System ; using System.IO ; using System.Drawing ; using System.Collections.Generic ; using System.Windows.Forms ; using Manina.Windows.Forms ; using log4net ; // -------------------------------------------------------------------- public partial class MainForm : Form { private readonly HashSet mValidImageExtensions = new HashSet{ ".png", ".jpg", ".gif" } ; private Dictionary mChartImages = new Dictionary() ; private SplitContainer mSplitter = new SplitContainer() ; private UserControl mSearchUserControl = new UserControl() ; private Label mSearchLabel = new Label() ; private TextBox mSearchQuery = new TextBox() ; private ImageListView mSearchResults = new ImageListView() ; private Panel mChartImagePanel = new Panel() ; private PictureBox mChartImagePictureBox = new PictureBox() ; private string mSearchResultsKey = null ; private Point? mMouseDragAnchor = null ; private Tuple mScrollDragAnchor ; private double mUserZoom = 1.0 ; private DateTime mLastKeyPressTimeStamp = DateTime.Now ; public MainForm() { // initialize the form InitializeComponent() ; } private void loadChartImages() { // initialize ILog logger = LogManager.GetLogger( "startup" ) ; // locate the chart images string dataDir = Path.GetFullPath( Program.dataDir ) ; IEnumerable files = Directory.EnumerateFiles( dataDir, "*.*", SearchOption.AllDirectories ) ; foreach( string fname in files ) { string extn = Path.GetExtension( fname ).ToLower() ; if ( ! mValidImageExtensions.Contains( extn ) ) continue ; string key ; string fullPath = Path.GetFullPath( fname ) ; if ( fullPath.StartsWith( dataDir ) ) { key = fname.Substring( dataDir.Length ) ; if ( key.StartsWith( "/" ) || key.StartsWith( "\\" ) ) key = key.Substring( 1 ) ; } else { // NOTE: I don't think we should ever get here :-/ If we do, the user will have to manage // their configuration using full paths for the image files, but at least we will still run... key = fullPath ; } logger.Debug( $"Loading image: {key}" ) ; mChartImages[ key ] = new ChartImage( key, fullPath ) ; } } private void updateSearchResults( string searchQuery ) { // initialize searchQuery = searchQuery.Trim() ; ILog logger = LogManager.GetLogger( "search" ) ; logger.Info( $"Updating search results: query=\"{searchQuery}\"" ) ; // search for matching chart images List< Tuple > results = new List>() ; foreach( ChartImage chartImage in mChartImages.Values ) { float score ; if ( searchQuery == "" ) score = 0 ; else { score = chartImage.getSearchScore( searchQuery ) ; if ( score <= 0 ) continue ; } results.Add( new Tuple( chartImage, score ) ) ; } // sort the search results results.Sort( (lhs, rhs) => { return (lhs.Item2 == rhs.Item2) ? 0 : (lhs.Item2 > rhs.Item2) ? -1 : +1 ; } ) ; if ( searchQuery != "" && logger.IsInfoEnabled ) { if ( results.Count > 0 ) { logger.Info( "- Sorted results:" ) ; foreach ( var val in results ) logger.Info( $" - \"{val.Item1.caption()}\" = {val.Item2:F1}" ) ; } else logger.Info( "- No results." ) ; } else logger.Info( "- No search query, showing all chart images." ) ; // show the search results List results2 = new List() ; foreach ( var r in results ) results2.Add( r.Item1 ) ; loadSearchResults( results2, searchQuery ) ; } private void loadSearchResults( IEnumerable chartImages, string key ) { // check if we need to reload the search results if ( key == mSearchResultsKey ) return ; // clear the search results mSearchResults.SuspendLayout() ; mSearchResults.Items.Clear() ; mSearchResults.ResumeLayout( true ) ; // load the search results mSearchResults.SuspendLayout() ; int nItems = 0 ; foreach( ChartImage chartImage in chartImages ) { ImageListViewItem item = chartImage.imageListViewItem ; mSearchResults.Items.Add( item ) ; if ( nItems++ == 0 ) item.Selected = true ; } mSearchResults.ResumeLayout( true ) ; mSearchResultsKey = key ; } }