459 lines
18 KiB
C#
459 lines
18 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Windows.Forms;
|
|
using NHibernate;
|
|
using Results.Entities;
|
|
using TBF.BenchControl;
|
|
using TBF.BenchControl.Generic;
|
|
using ResultsBrowser.Filters;
|
|
using ResultsBrowser.Resources;
|
|
|
|
namespace ResultsBrowser
|
|
{
|
|
public partial class ResultsBrowserWnd : Form
|
|
{
|
|
const string OpenFileDlg_SaveFileDlg_Filter = "{0} (*.qry)|*.qry|{1} (*.*)|*.*";
|
|
|
|
string[] args; /// Comand line argument
|
|
|
|
IList<Filters.IFilter> filters; /// Filters and the UI of filters
|
|
FiltersConfig filtersConfig; /// Serializable configuration of filters and displayed results
|
|
|
|
IList<WaterMeter> waterMeters;
|
|
|
|
DateTime timePeriodStart;
|
|
DateTime timePeriodEnd;
|
|
|
|
public ResultsBrowserWnd(string[] args)
|
|
{
|
|
InitializeComponent();
|
|
|
|
filters = new List<Filters.IFilter>();
|
|
filtersConfig = new FiltersConfig();
|
|
this.args = args;
|
|
|
|
timePeriodStart = new DateTime(0);
|
|
timePeriodEnd = new DateTime(0);
|
|
}
|
|
|
|
private void ResultsBrowserWnd_Load(object sender, EventArgs e)
|
|
{
|
|
ResultsBrowser.LocalSettings ls = Program.LocalSettings;
|
|
Width = (ls.MainWndWidth > 0) ? ls.MainWndWidth : 1000;
|
|
Height = (ls.MainWndHeight > 0) ? ls.MainWndHeight : 750;
|
|
Left = (ls.MainWndLeft != 0) ? ls.MainWndLeft : 50;
|
|
Top = (ls.MainWndTop != 0) ? ls.MainWndTop : 50;
|
|
hSplitContainer.SplitterDistance = (ls.SplitterDistance != 0) ? ls.SplitterDistance : 280;
|
|
|
|
Localize();
|
|
|
|
RedrawTestBenches();
|
|
|
|
checkBox1.Checked = Program.LocalSettings.Bench1Selected;
|
|
checkBox2.Checked = Program.LocalSettings.Bench2Selected;
|
|
checkBox3.Checked = Program.LocalSettings.Bench3Selected;
|
|
checkBox4.Checked = Program.LocalSettings.Bench4Selected;
|
|
checkBox5.Checked = Program.LocalSettings.Bench5Selected;
|
|
|
|
if (args.Length == 1 && args[0].IndexOf(".qry") == args[0].Length - 4)
|
|
{
|
|
LoadConfigFromFile(args[0]);
|
|
}
|
|
else if (!string.IsNullOrEmpty(Program.LocalSettings.LastConfigFileName))
|
|
{
|
|
LoadConfigFromFile(Program.LocalSettings.LastConfigFileName);
|
|
}
|
|
else
|
|
{
|
|
Text = string.Format("{0} ver.{1}", Strings.Results_Browser, Program.Version);
|
|
}
|
|
}
|
|
|
|
void RedrawTestBenches()
|
|
{
|
|
checkBox1.Text = Program.LocalSettings.Bench1;
|
|
checkBox2.Text = Program.LocalSettings.Bench2;
|
|
checkBox3.Text = Program.LocalSettings.Bench3;
|
|
checkBox4.Text = Program.LocalSettings.Bench4;
|
|
checkBox5.Text = Program.LocalSettings.Bench5;
|
|
|
|
checkBox1.Visible = !string.IsNullOrEmpty(checkBox1.Text);
|
|
checkBox2.Visible = !string.IsNullOrEmpty(checkBox2.Text);
|
|
checkBox3.Visible = !string.IsNullOrEmpty(checkBox3.Text);
|
|
checkBox4.Visible = !string.IsNullOrEmpty(checkBox4.Text);
|
|
checkBox5.Visible = !string.IsNullOrEmpty(checkBox5.Text);
|
|
}
|
|
|
|
string BenchName()
|
|
{
|
|
return checkBox1.Checked ? Program.LocalSettings.Bench1
|
|
: checkBox2.Checked ? Program.LocalSettings.Bench2
|
|
: Program.LocalSettings.Bench3;
|
|
}
|
|
|
|
void Localize()
|
|
{
|
|
filtersGroupBox.Text = Strings.Filters;
|
|
resultsGroupBox.Text = Strings.Query_results;
|
|
settingsBtn.Text = Strings.Settings;
|
|
testBenchesGroupBox.Text = Strings.Test_benches;
|
|
|
|
clearFiltersBtn.Text = Strings.Clear_filters;
|
|
addFilterBtn.Text = Strings.Add_filter;
|
|
formatOfResultsBtn.Text = Strings.Format_of_results;
|
|
printerConfigBtn.Text = Strings.Configure_printer;
|
|
loadConfigBtn.Text = Strings.Load_filters;
|
|
saveConfigBtn.Text = Strings.Save_filters;
|
|
|
|
executeQueryBtn.Text = Strings.Execute_query;
|
|
exportQueryResultsBtn.Text = Strings.Export_query_results;
|
|
printQueryResultsBtn.Text = Strings.Print_query_results;
|
|
|
|
statisticsBtn.Text = Strings.Statistics;
|
|
}
|
|
|
|
private void settingsBtn_Click(object sender, EventArgs e)
|
|
{
|
|
if (new Forms.DatabaseSettingsDlg().ShowDialog() == DialogResult.OK)
|
|
{
|
|
RedrawTestBenches();
|
|
}
|
|
}
|
|
|
|
private void checkBox1_CheckedChanged(object sender, EventArgs e) { SaveCheckBoxes(); }
|
|
private void checkBox2_CheckedChanged(object sender, EventArgs e) { SaveCheckBoxes(); }
|
|
private void checkBox3_CheckedChanged(object sender, EventArgs e) { SaveCheckBoxes(); }
|
|
private void checkBox4_CheckedChanged(object sender, EventArgs e) { SaveCheckBoxes(); }
|
|
private void checkBox5_CheckedChanged(object sender, EventArgs e) { SaveCheckBoxes(); }
|
|
|
|
void SaveCheckBoxes()
|
|
{
|
|
Program.LocalSettings.Bench1Selected = checkBox1.Checked;
|
|
Program.LocalSettings.Bench2Selected = checkBox2.Checked;
|
|
Program.LocalSettings.Bench3Selected = checkBox3.Checked;
|
|
Program.LocalSettings.Bench4Selected = checkBox4.Checked;
|
|
Program.LocalSettings.Bench5Selected = checkBox5.Checked;
|
|
Program.LocalSettings.Save();
|
|
}
|
|
|
|
private void clearFiltersBtn_Click(object sender, EventArgs e)
|
|
{
|
|
filters.Clear();
|
|
filtersFlowLayoutPanel.Controls.Clear();
|
|
}
|
|
|
|
private void addFilterBtn_Click(object sender, EventArgs e)
|
|
{
|
|
Forms.AddFilterDlg dlg = new Forms.AddFilterDlg();
|
|
if (dlg.ShowDialog() == DialogResult.OK && dlg.SelectedFilterID != null)
|
|
{
|
|
Filters.IFilter filter = Factory.GetFilter(dlg.SelectedFilterID);
|
|
filters.Add(filter);
|
|
filtersFlowLayoutPanel.Controls.Add(filter.GetFilterUI());
|
|
}
|
|
}
|
|
|
|
private void formatOfResultsBtn_Click(object sender, EventArgs e)
|
|
{
|
|
Results.Forms.ResultsConfigDlg dlg = new Results.Forms.ResultsConfigDlg();
|
|
dlg.MetersKind = Config.Entities.MetersKind.Single;
|
|
dlg.SelectedItems = Results.WMeterRsltItemSpec.FromStrArray(filtersConfig.ResultItems);
|
|
|
|
if (dlg.ShowDialog() == DialogResult.OK)
|
|
{
|
|
filtersConfig.ResultItems = Results.WMeterRsltItemSpec.ToStrArray(dlg.SelectedItems);
|
|
RedrawResults();
|
|
}
|
|
}
|
|
|
|
private void printerConfigBtn_Click(object sender, EventArgs e)
|
|
{
|
|
Forms.PrinterConfigDlg dlg = new Forms.PrinterConfigDlg
|
|
{
|
|
PrinterClass = filtersConfig.PrinterClass,
|
|
PrinterCfg = filtersConfig.PrinterCfg
|
|
};
|
|
|
|
if (dlg.ShowDialog() == DialogResult.OK)
|
|
{
|
|
filtersConfig.PrinterClass = dlg.PrinterClass;
|
|
filtersConfig.PrinterCfg = dlg.PrinterCfg;
|
|
}
|
|
}
|
|
|
|
private void loadConfigBtn_Click(object sender, EventArgs e)
|
|
{
|
|
OpenFileDialog dlg = new OpenFileDialog();
|
|
dlg.Filter = string.Format("{0} (*.qry)|*.qry|{1} (*.*)|*.*", Strings.Query_files, Strings.All_files);
|
|
if (dlg.ShowDialog() == DialogResult.OK)
|
|
{
|
|
LoadConfigFromFile(dlg.FileName);
|
|
}
|
|
}
|
|
|
|
void LoadConfigFromFile(string fileName)
|
|
{
|
|
FiltersConfig localFiltersConfig = FiltersConfig.Load(fileName);
|
|
if (localFiltersConfig != null)
|
|
{
|
|
filtersConfig = localFiltersConfig;
|
|
|
|
filters.Clear();
|
|
filtersFlowLayoutPanel.Controls.Clear();
|
|
///
|
|
for (int i = 0; i < filtersConfig.FiltersCount; i++)
|
|
{
|
|
Filters.IFilter filter = Factory.LoadFilter(filtersConfig.FilterID[i], filtersConfig.FilterData[i]);
|
|
filters.Add(filter);
|
|
filtersFlowLayoutPanel.Controls.Add(filter.GetFilterUI());
|
|
}
|
|
}
|
|
|
|
|
|
Program.LocalSettings.LastConfigFileName = fileName;
|
|
Program.LocalSettings.Save();
|
|
Text = string.Format("{0} ver.{1} ({2})", Strings.Results_Browser, Program.Version, Program.LocalSettings.LastConfigFileName);
|
|
}
|
|
|
|
private void saveConfigBtn_Click(object sender, EventArgs e)
|
|
{
|
|
if (filtersConfig.UpdateFromFilters(filters)) /// filtersConfig members related to filters
|
|
{
|
|
/// Save column widths of the ListView into filtersConfig members
|
|
if (resultsListView.Columns.Count > 0)
|
|
{
|
|
filtersConfig.ResultsClmnWidths = new int[resultsListView.Columns.Count];
|
|
for (int i = 0; i < resultsListView.Columns.Count; i++)
|
|
{
|
|
filtersConfig.ResultsClmnWidths[i] = resultsListView.Columns[i].Width;
|
|
}
|
|
}
|
|
|
|
/// Save the file
|
|
SaveFileDialog dlg = new SaveFileDialog();
|
|
dlg.Filter = string.Format(OpenFileDlg_SaveFileDlg_Filter, Strings.Query_files, Strings.All_files);
|
|
if (dlg.ShowDialog() == DialogResult.OK)
|
|
{
|
|
filtersConfig.Save(dlg.FileName);
|
|
|
|
|
|
Program.LocalSettings.LastConfigFileName = dlg.FileName;
|
|
Program.LocalSettings.Save();
|
|
Text = string.Format("{0} ver.{1} ({2})", Strings.Results_Browser, Program.Version, Program.LocalSettings.LastConfigFileName);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void executeQueryBtn_Click(object sender, EventArgs e)
|
|
{
|
|
timePeriodStart = new DateTime(0);
|
|
timePeriodEnd = new DateTime(0);
|
|
|
|
try
|
|
{
|
|
foreach (var filter in filters) filter.UI2Data(); /// Update filter data
|
|
|
|
waterMeters = null;
|
|
IList<WaterMeter> waterMeters2;
|
|
|
|
Results.DB.ConnectionString = checkBox1.Checked ? Program.LocalSettings.ConnectionString1 :
|
|
(checkBox2.Checked ? Program.LocalSettings.ConnectionString2 :
|
|
(checkBox3.Checked ? Program.LocalSettings.ConnectionString3 :
|
|
(checkBox4.Checked ? Program.LocalSettings.ConnectionString4 :
|
|
(checkBox5.Checked ? Program.LocalSettings.ConnectionString5 :
|
|
Program.LocalSettings.ConnectionString1))));
|
|
|
|
ISession[] sessions = new ISession[] { Results.DB.CreateSession() };
|
|
|
|
foreach (var filter in filters)
|
|
{
|
|
if (filter is DateFilter)
|
|
{
|
|
timePeriodStart = (filter as DateFilter).From;
|
|
timePeriodEnd = (filter as DateFilter).To;
|
|
}
|
|
waterMeters2 = filter.ExecuteQuery(sessions, waterMeters);
|
|
waterMeters = waterMeters2;
|
|
}
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
string message = exc.Message;
|
|
if (exc.InnerException != null)
|
|
message += string.Format("{0}Inner exception:{0}{1}", Environment.NewLine, exc.InnerException.Message);
|
|
|
|
MessageBox.Show(message, Strings.Error, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
|
|
return;
|
|
}
|
|
|
|
RedrawResults();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Displays obtained results as configured by result items in the ListView
|
|
/// </summary>
|
|
void RedrawResults()
|
|
{
|
|
IList<Results.WMeterRsltItemSpec> items = Results.WMeterRsltItemSpec.FromStrArray(filtersConfig.ResultItems);
|
|
|
|
resultsListView.Clear();
|
|
if (waterMeters == null || waterMeters.Count == 0 || items.Count == 0) return;
|
|
|
|
try
|
|
{
|
|
for (int i = 0; i < items.Count; i++)
|
|
{
|
|
resultsListView.Columns.Add(items[i].Caption, (filtersConfig.ResultsClmnCount > i) ? filtersConfig.ResultsClmnWidths[i] : 70);
|
|
}
|
|
foreach (var wm in waterMeters)
|
|
{
|
|
ListViewItem lvi = null;
|
|
foreach (var item in items)
|
|
{
|
|
/// Get text and strip color information
|
|
string itemText = item.Print(wm);
|
|
string[] texts = itemText.Split(new char[] { '|' });
|
|
if (texts.Length == 2) { itemText = texts[0]; }
|
|
|
|
if (lvi == null)
|
|
lvi = new ListViewItem(itemText);
|
|
else
|
|
lvi.SubItems.Add(itemText);
|
|
}
|
|
if (lvi != null) resultsListView.Items.Add(lvi);
|
|
}
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
MessageBox.Show(exc.Message, Strings.Error_displaying_results, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
|
|
}
|
|
}
|
|
|
|
private void exportQueryResultsBtn_Click(object sender, EventArgs e)
|
|
{
|
|
IList<Results.WMeterRsltItemSpec> items = Results.WMeterRsltItemSpec.FromStrArray(filtersConfig.ResultItems);
|
|
|
|
if (waterMeters == null || waterMeters.Count == 0 || items.Count == 0) return;
|
|
|
|
SaveFileDialog dlg = new SaveFileDialog();
|
|
|
|
if (dlg.ShowDialog() == DialogResult.OK)
|
|
{
|
|
using (TextWriter writer = new StreamWriter(dlg.FileName))
|
|
{
|
|
try
|
|
{
|
|
for (int i = 0; i < items.Count; i++)
|
|
{
|
|
writer.Write(items[i].Caption);
|
|
|
|
if (i == items.Count - 1)
|
|
writer.WriteLine(); /// New line after the last item
|
|
else
|
|
writer.Write(";"); /// Semicolon between two items
|
|
}
|
|
|
|
foreach (var wm in waterMeters)
|
|
{
|
|
for (int i = 0; i < items.Count; i++)
|
|
{
|
|
/// Get text and strip color information
|
|
string itemText = items[i].Print(wm);
|
|
string[] texts = itemText.Split(new char[] { '|' });
|
|
if (texts.Length == 2) { itemText = texts[0]; }
|
|
|
|
writer.Write(itemText);
|
|
|
|
if (i == items.Count - 1)
|
|
writer.WriteLine(); /// New line after the last item
|
|
else
|
|
writer.Write(";"); /// Semicolon between two items
|
|
}
|
|
}
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
MessageBox.Show(exc.Message, Strings.Error_displaying_results, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private void printQueryResultsBtn_Click(object sender, EventArgs e)
|
|
{
|
|
if (waterMeters == null || waterMeters.Count == 0) return;
|
|
|
|
#if MUNICH
|
|
foreach (var wm in waterMeters)
|
|
{
|
|
new Results.Output.Printers.Munich.MunichPrintDocument(string.Format("4/13 - {0}", BenchName()), wm, Config.Entities.PageOrientation.Portrait).Print();
|
|
}
|
|
#elif CEVAK_PT40_272
|
|
IList<Results.Entities.Batch> batches = new List<Results.Entities.Batch>();
|
|
foreach (var wm in waterMeters)
|
|
{
|
|
if (!batches.Contains(wm.Batch)) batches.Add(wm.Batch);
|
|
}
|
|
|
|
foreach (var batch in batches)
|
|
{
|
|
new Results.Output.Printers.Cevak.PrintDocumentCevak(batch,
|
|
Config.Entities.PageOrientation.Portrait,
|
|
60, 60, 60,
|
|
"PT40 Z/E-35-A",
|
|
"0111-OOP-C035-13").Print();
|
|
}
|
|
#else
|
|
foreach (var wm in waterMeters)
|
|
{
|
|
new Results.Output.Printers.Munich.MunichPrintDocument(string.Format("4/13 - {0}", BenchName()), wm, Config.Entities.PageOrientation.Portrait).Print();
|
|
}
|
|
#endif
|
|
}
|
|
|
|
private void statisticsBtn_Click(object sender, EventArgs e)
|
|
{
|
|
if (waterMeters == null || waterMeters.Count == 0) return;
|
|
|
|
IList<Results.WMeterRsltItemSpec> items = Results.WMeterRsltItemSpec.FromStrArray(filtersConfig.ResultItems);
|
|
|
|
///
|
|
/// Enter options
|
|
///
|
|
Forms.StatisticsOptionsDlg optionsDlg = new Forms.StatisticsOptionsDlg(items);
|
|
if (optionsDlg.ShowDialog() != DialogResult.OK) return;
|
|
|
|
///
|
|
/// Calculate statistics
|
|
///
|
|
Forms.StatisticsDlg statisticsDlg = new Forms.StatisticsDlg(waterMeters, optionsDlg.TwoDim,
|
|
optionsDlg.RowItem, optionsDlg.RowItem2, optionsDlg.RowItem3,
|
|
optionsDlg.ColumnItem, optionsDlg.ColumnItem2, optionsDlg.ColumnItem3,
|
|
optionsDlg.RowTestSpecifier, optionsDlg.RowTestSpecifier2, optionsDlg.RowTestSpecifier3,
|
|
optionsDlg.ColumnTestSpecifier, optionsDlg.ColumnTestSpecifier2, optionsDlg.ColumnTestSpecifier3,
|
|
timePeriodStart, timePeriodEnd);
|
|
statisticsDlg.ShowDialog();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Updates component factory
|
|
/// </summary>
|
|
/// <param name="className">Component class name</param>
|
|
/// <param name="factory">Reference to a factory</param>
|
|
/// <returns>true when factory changed</returns>
|
|
IComponentFactory GetFactory(string className)
|
|
{
|
|
if (!string.IsNullOrEmpty(className))
|
|
{
|
|
foreach (var fac in TbfComponents.Factories)
|
|
{
|
|
if (fac.ClassName == className) return fac;
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|
|
}
|