2022-07-12 18:50:42 +00:00
|
|
|
|
using DataModel.Database;
|
2022-07-12 19:05:50 +00:00
|
|
|
|
using Processor;
|
2022-07-17 10:56:06 +00:00
|
|
|
|
using UI_WinForms.Dialogs;
|
2022-07-17 18:47:17 +00:00
|
|
|
|
using UI_WinForms.Resources;
|
2022-07-12 18:50:42 +00:00
|
|
|
|
|
|
|
|
|
namespace UI_WinForms.Components;
|
2022-07-11 17:52:15 +00:00
|
|
|
|
|
|
|
|
|
public partial class TextElements : UserControl
|
|
|
|
|
{
|
2022-07-12 18:50:42 +00:00
|
|
|
|
private Section? currentSection;
|
2022-07-20 18:51:12 +00:00
|
|
|
|
private TextElement? currentTextElement;
|
2022-07-12 18:50:42 +00:00
|
|
|
|
|
2022-07-11 17:52:15 +00:00
|
|
|
|
public TextElements()
|
|
|
|
|
{
|
|
|
|
|
this.InitializeComponent();
|
2022-07-12 18:50:42 +00:00
|
|
|
|
|
2022-07-12 19:05:50 +00:00
|
|
|
|
// Check if we are in the designer:
|
|
|
|
|
if(Program.SERVICE_PROVIDER is null)
|
|
|
|
|
return;
|
|
|
|
|
|
2022-07-17 18:47:17 +00:00
|
|
|
|
// Create an image list from a resource:
|
|
|
|
|
var imgList = new ImageList();
|
|
|
|
|
imgList.ImageSize = new Size(45, 45);
|
|
|
|
|
imgList.ColorDepth = ColorDepth.Depth32Bit;
|
|
|
|
|
imgList.Images.Add(Icons.icons8_align_text_left_512);
|
|
|
|
|
|
|
|
|
|
// Set the image list to the list box:
|
|
|
|
|
this.listTextElements.SmallImageList = imgList;
|
|
|
|
|
|
2022-07-12 18:50:42 +00:00
|
|
|
|
// When the section is changed, update this component:
|
2022-07-13 18:07:06 +00:00
|
|
|
|
AppEvents.WhenSectionChanged += async (sender, section) =>
|
2022-07-12 18:50:42 +00:00
|
|
|
|
{
|
|
|
|
|
this.currentSection = section;
|
2022-07-20 18:51:12 +00:00
|
|
|
|
this.currentTextElement = null;
|
2022-07-12 18:50:42 +00:00
|
|
|
|
this.buttonAdd.Enabled = this.currentSection is not null;
|
2022-07-20 18:51:12 +00:00
|
|
|
|
this.buttonRename.Enabled = this.buttonRemove.Enabled = false;
|
2022-07-12 19:05:50 +00:00
|
|
|
|
|
2022-07-16 20:40:06 +00:00
|
|
|
|
if (this.currentSection is null)
|
|
|
|
|
return;
|
|
|
|
|
|
2022-07-20 18:51:12 +00:00
|
|
|
|
// Update the path:
|
2022-07-17 13:15:55 +00:00
|
|
|
|
this.labelSectionPath.Text = await SectionProcessor.GetSectionPath(this.currentSection.DataKey);
|
2022-07-16 20:40:06 +00:00
|
|
|
|
this.LoadTextElements();
|
2022-07-12 18:50:42 +00:00
|
|
|
|
};
|
2022-07-20 18:51:12 +00:00
|
|
|
|
|
|
|
|
|
// When the text element is changed, update the button states:
|
|
|
|
|
AppEvents.WhenTextElementChanged += (sender, textElement) =>
|
|
|
|
|
{
|
|
|
|
|
this.currentTextElement = textElement;
|
|
|
|
|
this.buttonRename.Enabled = this.buttonRemove.Enabled = this.currentTextElement is not null;
|
|
|
|
|
};
|
2022-07-11 17:52:15 +00:00
|
|
|
|
}
|
2022-07-16 20:40:06 +00:00
|
|
|
|
|
|
|
|
|
// Loads all the text elements for the current section.
|
|
|
|
|
private async void LoadTextElements()
|
|
|
|
|
{
|
|
|
|
|
if (this.currentSection is null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// Load the text elements:
|
2022-07-17 13:15:55 +00:00
|
|
|
|
var textElements = await TextElementProcessor.GetTextElements(this.currentSection);
|
2022-07-16 20:40:06 +00:00
|
|
|
|
|
|
|
|
|
// Update the list:
|
|
|
|
|
this.listTextElements.Items.Clear();
|
2022-07-17 13:15:55 +00:00
|
|
|
|
foreach (var textElement in textElements)
|
2022-07-17 18:47:17 +00:00
|
|
|
|
this.listTextElements.Items.Add(new ListViewItem(textElement.Name, 0)
|
2022-07-16 20:40:06 +00:00
|
|
|
|
{
|
2022-07-20 18:51:12 +00:00
|
|
|
|
Tag = textElement.Id,
|
2022-07-17 18:47:17 +00:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
this.column.AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
|
2022-07-16 20:40:06 +00:00
|
|
|
|
}
|
2022-07-17 10:56:06 +00:00
|
|
|
|
|
|
|
|
|
private async void buttonAdd_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if(this.DesignMode)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
var result = InputDialog.Show(new InputDialog.Options(
|
|
|
|
|
Message: "Please type the desired text element's name.",
|
|
|
|
|
Title: "Add a text element",
|
|
|
|
|
Placeholder: "My text element",
|
|
|
|
|
ShowQuestionCheckbox: false
|
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
if(result.DialogResult == DialogResult.Cancel)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// Add the text element to the database into the current section:
|
2022-07-17 13:15:55 +00:00
|
|
|
|
var newTextElement = await TextElementProcessor.AddTextElement(this.currentSection?.DataKey, result.Text);
|
2022-07-17 10:56:06 +00:00
|
|
|
|
newTextElement.ProcessError();
|
|
|
|
|
|
|
|
|
|
if(!newTextElement.Successful)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// Add the text element to the list:
|
2022-07-17 18:47:17 +00:00
|
|
|
|
this.listTextElements.Items.Add(new ListViewItem(newTextElement.Result!.Name, 0)
|
2022-07-17 10:56:06 +00:00
|
|
|
|
{
|
2022-07-20 18:51:12 +00:00
|
|
|
|
Tag = newTextElement.Result.Id,
|
2022-07-17 18:47:17 +00:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
this.column.AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
|
2022-07-17 10:56:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void buttonRemove_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void buttonRename_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
2022-07-20 18:51:12 +00:00
|
|
|
|
|
|
|
|
|
private async void listTextElements_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
// Load the text element:
|
|
|
|
|
var selectedTextElementId = (int)e.Item.Tag;
|
|
|
|
|
this.currentTextElement = await TextElementProcessor.LoadTextElement(selectedTextElementId);
|
|
|
|
|
|
|
|
|
|
// Fire the event:
|
|
|
|
|
AppEvents.TextElementChanged(this.currentTextElement);
|
|
|
|
|
}
|
2022-07-11 17:52:15 +00:00
|
|
|
|
}
|