Form1.cs 10.3 KB
using DirectShowLib;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//using WebCam;
//using ImageFilters;
using System.IO;
using MoyaAdminLib;
using System.Diagnostics;
using ThermoPrinterLibrary;
using Emgu.CV;
using Emgu.CV.Structure;
using Emgu.CV.CvEnum;
using Emgu.CV.UI;
using MoyaSignup.Dataclasses;

namespace MoyaSignup
{
    public partial class Form1 : Form
    {
        ThermoPrinter thermoPrinter;

        List<DsDeviceContainer> webCams;

        //public delegate void ThermoPrinterEvent(ThermoPrinter.Models model);
        //public event ThermoPrinterEvent SetPrinter;

        public Form1()
        {
            RestClient.ApiApplicationKey = Properties.Settings.Default.ApiApplicationKey;
            RestClient.ApiPass = Properties.Settings.Default.ApiPass;
            RestClient.ApiUser = Properties.Settings.Default.ApiUser;
            RestClient.ApiURL = Properties.Settings.Default.ApiURL;
            InitializeComponent();
        }


        enum Language
        {
            Finnish = 0,
            English = 1,
        }
//        Language currentLanguage = Language.English;

        /*
        void changed()
        {
            if (!pictureTaken )
            {

                    this.lblMessage.Text = "Ota kuva ensin painamalla 'Käytä tätä kuvaa' nappia";
                
                this.lblMessage.ForeColor = Color.Red;
                this.btnSave.Enabled = false;
                return;
            }
           
            this.lblMessage.ForeColor = Color.Green;

                this.lblMessage.Text = "OK, varmista tietojen oikeellisuus ja tallenna";
            
            this.btnSave.Enabled = true;
        }*/


        public void SetPrinter(ThermoPrinter.Models model)
        {
            thermoPrinter = new ThermoPrinter(model);
            
            try
            {
                thermoPrinter.Start(Properties.Settings.Default.printerPort);
                thermoPrinter.SetBarcodeWidth(ThermoPrinter.BarcodeWidths.Small);
                thermoPrinter.SetCharacterAlignment(ThermoPrinter.CharacterAlignments.Center);
                thermoPrinter.SetPositionHRI(ThermoPrinter.HRIPrintingPositions.Below);
                thermoPrinter.SetCharacterTable(ThermoPrinter.CharacterCodeTables.Nordic);
            }
            catch
            {
                thermoPrinter = null;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            /*if (AutoUpdateLib.AutoUpdateCore.CheckForUpdates(null))
            {
                Application.ExitThread();		//program is required to close by autoupdate (probably updated)
                return;
            }*/

            if (Debugger.IsAttached)
                this.WindowState = FormWindowState.Normal;

            ApiSettings form = new ApiSettings();
            form.ShowDialog();

            webCams = new List<DsDeviceContainer>();
            if(form.Webcams != null && form.Webcams.Count > 0)
            {
                webCams = form.Webcams;
            }
            Program.CaptureDevices = webCams;

            if(Properties.Settings.Default.PrinterTypeSelected == 1 && Properties.Settings.Default.printerPort != null && Properties.Settings.Default.ThermoPrinter > 0)
            {
                ThermoPrinter.Models model = (ThermoPrinter.Models)Properties.Settings.Default.ThermoPrinter;
                SetPrinter(model);

                userLoginControl1.SetPrinter += SetPrinter;
            }

            

        }

        bool pictureTaken;

        private void btnFinnish_Click(object sender, EventArgs e)
        {

        }

        private void btnEnglish_Click(object sender, EventArgs e)
        {

        }


        
        bool checkSaveSafety()
        {
            return true;
        }
         
        /*
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!checkSaveSafety())
            {
                this.btnSave.Enabled = false;
                return;
            }

            this.Cursor = Cursors.WaitCursor;
            this.btnSave.Enabled = false;
            this.Enabled = false;
            this.btnSave.Refresh();

            
            //savePicture(pid);


            MessageBox.Show("Tallennus onnistui.\r\nMene infotiskille, ja kerro heille tämä numero, niin he opastavat sinut eteenpäin.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
            
            
            this.Enabled = true;
            this.Cursor = Cursors.Default;
        }*/


        bool allowShutdown;
        public bool AllowShutdown {
            get { return allowShutdown; }
            set { allowShutdown = value; }
        }
        /*
        private void txtName_TextChanged(object sender, EventArgs e)
        {
            changed();
        }*/

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!allowShutdown)
            {
                e.Cancel = true;
            }
            else
            {
                if (thermoPrinter != null)
                    thermoPrinter.Stop();
            }

        }

        private void Form1_Deactivate(object sender, EventArgs e)
        {
            if (!allowShutdown)
                this.Activate();
        }

        public void HandleKeyUpEvent(object sender, KeyEventArgs e)
        {
            if (e.Alt && e.Control && e.KeyCode == Keys.S)
            {
                Debug.WriteLine("[form1] Catched key event alt + ctrl + S: Shutting down the program. Sender: " + sender.ToString());
                allowShutdown = true;
                Application.Exit();
                return;
            } else if(e.Alt && e.Control && e.KeyCode == Keys.C)
            {
                if (Program.WebCams != null)
                {
                    WebCamViewForm frm = new WebCamViewForm(Program.WebCams);
                    frm.Show();
                } else if(Program.CaptureDevices != null && Program.CaptureDevices.Count > 0)
                {
                    List<Capture> cams = new List<Emgu.CV.Capture>();
                    foreach (DsDeviceContainer device in Program.CaptureDevices)
                    {
                        Capture capture = new Capture(device.Idx);
                        capture.SetCaptureProperty(CAP_PROP.CV_CAP_PROP_FRAME_HEIGHT, 720);
                        capture.SetCaptureProperty(CAP_PROP.CV_CAP_PROP_FRAME_WIDTH, 1280);
                        capture.QueryFrame();

                        Debug.WriteLine("[takepicturecontrol] adding webcam " + device.Device.Name + "' to webcams");

                        cams.Add(capture);
                    }

                    Program.WebCams = cams;

                    WebCamViewForm frm = new WebCamViewForm(Program.WebCams);
                    frm.Show();
                }
            }
            
            else if (e.Alt && e.Control && e.KeyCode == Keys.D)
            {
                if (Program.LogFrm == null)
                {
                    Program.LogFrm = new LogForm();
                    Program.LogFrm.Show();
                }
            }
            else if (e.Alt && e.KeyCode == Keys.F5)
            {
                //userDetailsEditor1.SetPrinter(ThermoPrinterLibrary.ThermoPrinter.Models.IDP3210);
                //MessageBox.Show("Printteri: IDP3210");

            }
        }

        private void Form1_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Alt && e.Control && e.KeyCode == Keys.S)
            {
                allowShutdown = true;
                Application.Exit();
                return;
            } /*else if(e.Alt && e.Control && e.KeyCode == Keys.C)
            {
                WebCamViewForm frm = new WebCamViewForm(webCams);
                frm.Show();
            }*/
            else if (e.Alt && e.KeyCode == Keys.F5)
            {
                //userDetailsEditor1.SetPrinter(ThermoPrinterLibrary.ThermoPrinter.Models.IDP3210);
                //MessageBox.Show("Printteri: IDP3210");
                
            }
        }
        
        
        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Alt && e.Control && e.KeyCode == Keys.S)
            {
                allowShutdown = true;
                this.Close();
                return;
            }
            /*else if (e.Alt && e.Control && e.KeyCode == Keys.C)
            {
                WebCamViewForm frm = new WebCamViewForm(webCams);
                frm.Show();
            }*/
        }

        
        
        private void Form1_MouseClick(object sender, MouseEventArgs e)
        {/*
            if (e.Button == MouseButtons.Right)
            {
                allowShutdown = true;
                Application.Exit();
                return;
            }*/
        }
        
        
        
        private void button1_Click(object sender, EventArgs e)
        {
            allowShutdown = true;
            Application.Exit();
        }

        private void userLoginControl1_LoginOk(object sender, EventArgs e)
        {
            userLoginControl1.Visible = false;
            userDetailsEditor1.LoadUser(userLoginControl1.CurrentUser);

            if (Properties.Settings.Default.PrinterTypeSelected == 1)
                userDetailsEditor1.ThermoPrinter = thermoPrinter;
            else if (Properties.Settings.Default.PrinterTypeSelected == 0)
                userDetailsEditor1.ReceiptPrinterName = Properties.Settings.Default.ReceiptPrinterName;
            userLoginControl1.Clear();
            userDetailsEditor1.Visible = true;
            //userLoginControl1.CurrentUser;
        }

        private void userDetailsEditor1_Load(object sender, EventArgs e)
        {

        }

        private void userDetailsEditor1_CloseView(object sender, EventArgs e)
        {
            userDetailsEditor1.Visible = false;
            userLoginControl1.Visible = true;
        }

        public void SetUsersPicture(Bitmap image)
        {
            userDetailsEditor1.SetUsersImage(image);
            userDetailsEditor1.LatestFrame = image;
            userDetailsEditor1.ShowSaveButton();
        }

        private void btnFinnish_Click_1(object sender, EventArgs e)
        {

        }
    }
}