SafeDispatch/Safedispatch_4_0/maptab/Radio/RecordingGridCellElement.cs
2024-02-22 18:43:59 +02:00

328 lines
14 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Dispatcher.maptab.UIClasses;
using SafeMobileLib;
using Telerik.WinControls.Layouts;
using Telerik.WinControls.UI;
namespace Safedispatch_4_0.Radio
{
public class RecordingGridCellElement : GridDataCellElement
{
public static bool REPEATER_LAYOUT = false;
public LightVisualElement topRightElement;
private DockLayoutPanel dockLayout;
private LightVisualElement callTypeIcon;
private StackLayoutElement callInformationStack;
private LightVisualElement username;
private LightVisualElement duration;
private LightVisualElement filename;
private LightVisualElement dateUnix;
private LightVisualElement buttonPlay;
public RecordingGridCellElement(GridViewColumn column, GridRowElement row)
: base(column, row)
{
}
protected override void CreateChildElements()
{
base.CreateChildElements();
/*
this.dockLayout = new DockLayoutPanel();
this.dockLayout.StretchHorizontally = true;
this.dockLayout.ShouldHandleMouseInput = false;
this.dockLayout.Padding = new Padding(2, 2, 10, 2);
this.dockLayout.Margin = new Padding(2, 2, 10, 2);
*/
this.dockLayout = new DockLayoutPanel();
this.dockLayout.StretchHorizontally = true;
this.dockLayout.StretchVertically = true;
this.dockLayout.ShouldHandleMouseInput = false;
this.dockLayout.Margin = new Padding(0, 0, 0, 0);
this.dockLayout.Padding = new Padding(0, 0, 0, 0);
this.dockLayout.AutoToolTip = true;
this.dockLayout.AutoSize = true;
this.dockLayout.AutoSizeMode = Telerik.WinControls.RadAutoSizeMode.FitToAvailableSize;
this.callTypeIcon = new LightVisualElement();
this.callTypeIcon.AutoSizeMode = Telerik.WinControls.RadAutoSizeMode.WrapAroundChildren;
this.callTypeIcon.AutoSize = true;
this.callTypeIcon.Padding = new Padding(0, 10, 0, 0);
this.callTypeIcon.Alignment = ContentAlignment.MiddleCenter;
//this.callTypeIcon.ImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.callTypeIcon.ShouldHandleMouseInput = false;
this.dockLayout.Children.Add(this.callTypeIcon);
DockLayoutPanel.SetDock(callTypeIcon, Telerik.WinControls.Layouts.Dock.Left);
this.buttonPlay = new LightVisualElement();
this.buttonPlay.Tag = "stopped";
//this.buttonPlay.Margin = new Padding(0,12,3,12);
this.buttonPlay.Image = ScaleImage(Dispatcher.Properties.Resources.r_play, 32, 32);
/*
this.buttonPlay.MouseEnter += delegate(object sender, EventArgs e)
{
this.buttonPlay.Image = ScaleImage(Dispatcher.Properties.Resources.r_play,32,32);
};
this.buttonPlay.MouseLeave += delegate(object sender, EventArgs e)
{
this.buttonPlay.Image = ScaleImage(Dispatcher.Properties.Resources.r_play_blue, 32, 32);
};*/
this.buttonPlay.Click += delegate(object sender, EventArgs e)
{
if (MainForm2.PlayingFile)
return;
if (this.buttonPlay.Tag.Equals("stopped"))
{
// get row
GridViewDataRowInfo row = this.RowInfo as GridViewDataRowInfo;
if (row != null)
{
// get row data
Recording gridData = row.DataBoundItem as Recording;
// check if data exists
if (gridData != null)
{
this.buttonPlay.Image = Utils.ScaleImage(Dispatcher.Properties.Resources.r_play_blue, 32, 32);
this.buttonPlay.Tag = "playing";
// trigger event for when click is done
if (_onPlay_Click != null)
_onPlay_Click(buttonPlay, new ToggleClickEventArgs()
{
recording = gridData,
isActive = !buttonPlay.Tag.Equals("playing")
});
Thread t = new Thread(new ThreadStart(delegate
{
Thread.Sleep(gridData.duration * 1000);
this.buttonPlay.Image = Utils.ScaleImage(Dispatcher.Properties.Resources.r_play, 32, 32);
this.buttonPlay.Tag = "stopped";
}));
t.Start();
}
}
}
else
{
this.buttonPlay.Image = Utils.ScaleImage(Dispatcher.Properties.Resources.r_play, 32, 32);
this.buttonPlay.Tag = "stopped";
}
};
this.buttonPlay.ShouldHandleMouseInput = true;
this.buttonPlay.Visibility = Telerik.WinControls.ElementVisibility.Hidden;
//this.buttonPlay.Image = ScaleImage(Dispatcher.Properties.Resources.r_play_blue, 32, 32);
//this.buttonPlay.ImageAlignment = ContentAlignment.MiddleCenter;
this.dockLayout.Children.Add(this.buttonPlay);
DockLayoutPanel.SetDock(buttonPlay, Telerik.WinControls.Layouts.Dock.Right);
this.callInformationStack = new StackLayoutElement();
this.callInformationStack.Padding = new Padding(0);
this.callInformationStack.Margin = new Padding(0);
this.callInformationStack.ShouldHandleMouseInput = false;
this.callInformationStack.Orientation = REPEATER_LAYOUT ? Orientation.Horizontal : Orientation.Vertical;
this.callInformationStack.StretchHorizontally = true;
this.callInformationStack.AutoSize = true;
this.callInformationStack.AutoSizeMode = Telerik.WinControls.RadAutoSizeMode.FitToAvailableSize;
//this.callInformationStack.Padding = new Padding(0, 0, 40, 0);
this.dockLayout.Children.Add(this.callInformationStack);
DockLayoutPanel.SetDock(this.callInformationStack, Telerik.WinControls.Layouts.Dock.Top);
int usernameSize = REPEATER_LAYOUT ? 250 : 150;
this.username = new LightVisualElement();
this.username.ShouldHandleMouseInput = false;
this.username.StretchHorizontally = false;
this.username.StretchVertically = true;
this.username.MaxSize = new Size(usernameSize, 35);
this.username.MinSize = new Size(usernameSize, 35);
this.username.Padding = new Padding(0, 14, 0, 0);
this.username.Margin = new Padding(0, 0, 0, 0);
this.username.Font = new System.Drawing.Font("Segoe UI", 9, FontStyle.Bold, GraphicsUnit.Point);
this.username.TextAlignment = ContentAlignment.MiddleLeft;
this.username.TextWrap = true;
this.callInformationStack.Children.Add(this.username);
this.duration = new LightVisualElement();
this.duration.ShouldHandleMouseInput = false;
this.duration.StretchHorizontally = false;
this.duration.MaxSize = new Size(usernameSize, 35);
this.duration.MinSize = new Size(usernameSize, 35);
this.duration.Padding = new Padding(0, 14, 0, 0);
this.duration.Margin = new Padding(0, 0, 0, 0);
this.duration.TextAlignment = ContentAlignment.TopLeft;
this.duration.ForeColor = Color.FromArgb(0x66,0x66,0x66);
this.callInformationStack.Children.Add(this.duration);
if (REPEATER_LAYOUT)
{
this.filename = new LightVisualElement();
this.filename.ShouldHandleMouseInput = false;
this.filename.StretchHorizontally = true;
this.filename.StretchVertically = true;
this.filename.Padding = new Padding(0, 14, 40, 0);
this.filename.Margin = new Padding(0, 14, 40, 0);
this.filename.TextAlignment = ContentAlignment.TopLeft;
this.filename.ForeColor = Color.FromArgb(0x66, 0x66, 0x66);
this.filename.TextWrap = true;
this.filename.AutoSize = true;
this.filename.AutoSizeMode = Telerik.WinControls.RadAutoSizeMode.FitToAvailableSize;
this.callInformationStack.Children.Add(this.filename);
}
this.dateUnix = new LightVisualElement();
this.dateUnix.ShouldHandleMouseInput = false;
this.dateUnix.StretchHorizontally = false;
this.callInformationStack.Children.Add(this.dateUnix);
this.topRightElement = new LightVisualElement();
this.topRightElement.StretchHorizontally = false;
this.topRightElement.StretchVertically = false;
this.topRightElement.DrawFill = true;
this.topRightElement.NumberOfColors = 1;
this.topRightElement.BackColor = Color.FromArgb(171, 171, 171);
this.topRightElement.ForeColor = Color.White;
this.topRightElement.Font = new Font("Segoe UI Semibold", 8f);
this.topRightElement.Alignment = ContentAlignment.TopRight;
this.topRightElement.Padding = new Padding(2);
//this.topRightElement.Text = "This is a demo";
this.Children.Add(topRightElement);
this.Children.Add(this.dockLayout);
}
protected override Type ThemeEffectiveType
{
get
{
return typeof(GridDataCellElement);
}
}
public override bool IsCompatible(GridViewColumn data, object context)
{
return data is RecordingGridColumn && context is GridDataRowElement;
}
public override void SetContent()
{
GridViewDataRowInfo row = this.RowInfo as GridViewDataRowInfo;
if (row != null)
{
Recording gridData = row.DataBoundItem as Recording;
// update the UI elements
UpdateUIElements(gridData);
}
this.Text = string.Empty;
}
/// <summary>
/// Update all UI elements with the ones received in gridData object
/// </summary>
/// <param name="gridData">Object containing all infos for the UI elements</param>
private void UpdateUIElements(Recording gridData)
{
if (gridData != null)
{
this.Text = "";
this.callTypeIcon.Image = ScaleImage(gridData.callTypeImage, 36, 36);
this.username.Text = Convert.ToString(gridData.username);// rec.username;
this.topRightElement.Text = gridData.GatewayName.Length > 0 ? gridData.GatewayName : "N/A";
this.duration.Text = String.Format("{0:HH:mm:ss}", gridData.DateTimeStart) + " [" + Convert.ToString(gridData.duration) + " sec]";
if (REPEATER_LAYOUT)
this.filename.Text = gridData.File_Name;
if (this.IsCurrentRow)
this.buttonPlay.Visibility = Telerik.WinControls.ElementVisibility.Visible;
else
this.buttonPlay.Visibility = Telerik.WinControls.ElementVisibility.Hidden;
}
}
/// <summary>
/// Resize an image keeping the aspect ratio of the image
/// </summary>
/// <param name="image">Image which needs to be resized</param>
/// <param name="maxWidth">Maximum allowed width for this image</param>
/// <param name="maxHeight">Maximum allowed height for this image</param>
/// <returns>The resized image</returns>
public static Image ScaleImage(Image image, int maxWidth, int maxHeight)
{
var ratioX = (double)maxWidth / image.Width;
var ratioY = (double)maxHeight / image.Height;
var ratio = Math.Min(ratioX, ratioY);
var newWidth = (int)(image.Width * ratio);
var newHeight = (int)(image.Height * ratio);
var newImage = new Bitmap(newWidth, newHeight);
Graphics.FromImage(newImage).DrawImage(image, 0, 0, newWidth, newHeight);
return newImage;
}
#region CELL CLICK EVENTS
public class ToggleClickEventArgs : EventArgs
{
public Recording recording { get; set; }
public Boolean isActive { get; set; }
}
private object _lockPlayClick = new object();
private event EventHandler<ToggleClickEventArgs> _onPlay_Click;
public event EventHandler<ToggleClickEventArgs> OnPlay_Click
{
add { lock (_lockPlayClick) { _onPlay_Click += value; } }
remove { lock (_lockPlayClick) { _onPlay_Click -= value; } }
}
/*
private object _lockRemoteClick = new object();
private event EventHandler<ToggleClickEventArgs> _onRemote_Click;
public event EventHandler<ToggleClickEventArgs> OnRemote_Click
{
add { lock (_lockRemoteClick) { _onRemote_Click += value; } }
remove { lock (_lockRemoteClick) { _onRemote_Click -= value; } }
}*/
#endregion
}
}