RhSolutions-AddIn/RhSolutions.AddIn/Controllers/RibbonController.cs

139 lines
4.5 KiB
C#
Raw Normal View History

using ExcelDna.Integration.CustomUI;
2024-02-22 18:09:27 +03:00
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
2024-02-22 18:10:53 +03:00
using System.Windows.Forms;
2021-12-05 16:55:36 +03:00
2023-03-28 10:36:36 +03:00
namespace RhSolutions.Controllers;
[ComVisible(true)]
public class RibbonController : ExcelRibbon
2021-12-05 16:55:36 +03:00
{
2024-11-06 23:43:00 +03:00
private static IRibbonUI ribbonUi;
private static bool _workbookIsValid;
public override string GetCustomUI(string RibbonID)
{
return @"
<customUI onLoad='RibbonLoad' xmlns='http://schemas.microsoft.com/office/2006/01/customui' loadImage='LoadImage'>
<ribbon>
<tabs>
<tab id='rau' label='RhSolutions'>
<group id='priceList' label='Прайс-лист'>
<button id='export' getEnabled='GetExportEnabled' label='Экспорт в новый файл' size='normal' image='RhSolutions' onAction='OnToolPressed'/>
<button id='convert' getEnabled='GetConvertEnabled' label='Актуализировать' size='normal' imageMso='FileUpdate' onAction='OnToolPressed'/>
<button id='merge' label='Объединить' size='normal' imageMso='Copy' onAction='OnToolPressed'/>
<button id='guess' getEnabled='GetGuessEnabled' label='Найти и экспортировать' size='large' imageMso='ControlWizards' onAction='OnToolPressed'/>
</group>
<group id='fittingsCalc' label='Расчет фитингов'>
<button id='fillsleeves' getEnabled='GetFittingsCalcEnabled' label='Гильзы' size='large' image='Sleeve' onAction='OnToolPressed'/>
<button id='fillcouplings' getEnabled='GetFittingsCalcEnabled' label='Муфты' size='large' image='Coupling' onAction='OnToolPressed'/>
</group>
<group id='exportTab' label='Экспорт'>
<button id='dxfexport' getEnabled='GetDxfEnabled' label='DXF' size='large' image='DXF' onAction='OnToolPressed'/>
</group>
<group id='importTab' label='OCR'>
<button id='ocr' label='Распознать таблицу' size='large' imageMso='TableInsert' onAction='OnToolPressed'/>
</group>
<group id='settings' getLabel='GetVersionLabel'>
<button id='setPriceList' getLabel='GetPriceListPathLabel' size='large' image='RhSolutions' onAction='OnSetPricePressed'/>
</group>
</tab>
</tabs>
</ribbon>
</customUI>";
}
public void RibbonLoad(IRibbonUI sender)
{
ribbonUi = sender;
}
public static void RefreshControl(string id)
{
ribbonUi?.InvalidateControl(id);
}
public void OnSetPricePressed(IRibbonControl control)
{
IFileDialog dialog = RhSolutionsAddIn.ServiceProvider.GetService<IFileDialog>();
string file = dialog.GetFile();
if (!string.IsNullOrEmpty(file))
{
RhSolutionsAddIn.Configuration.SetPriceListPath(file);
RhSolutionsAddIn.Configuration.SaveSettings();
}
}
public void OnToolPressed(IRibbonControl control)
{
try
{
var toolFactory = RhSolutionsAddIn.ServiceProvider.GetService<ToolFactory>();
using Tool tool = toolFactory.GetTool(control.Id);
tool.Execute();
}
catch (Exception exception)
{
MessageBox.Show(exception.Message,
"Ошибка",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
RhSolutionsAddIn.Excel.StatusBar = false;
return;
}
}
public bool GetConvertEnabled(IRibbonControl control) => _workbookIsValid;
public bool GetDxfEnabled(IRibbonControl control) => _workbookIsValid;
public bool GetFittingsCalcEnabled(IRibbonControl control) => _workbookIsValid;
public bool GetGuessEnabled(IRibbonControl control) => RhSolutionsAddIn.Excel.ActiveWorkbook != null && !_workbookIsValid;
public bool GetExportEnabled(IRibbonControl control)
{
if (RhSolutionsAddIn.Excel.ActiveWorkbook == null)
return false;
else
{
Range selection = RhSolutionsAddIn.Excel.Selection;
return selection.Columns.Count == 2;
}
}
public string GetVersionLabel(IRibbonControl control)
{
string version = Assembly.GetExecutingAssembly().GetName().Version.ToString();
return $"v{version}";
}
public string GetPriceListPathLabel(IRibbonControl control)
{
string name = RhSolutionsAddIn.Configuration.GetPriceListFileName();
return string.IsNullOrEmpty(name) ? "Указать шаблонный файл" : name;
}
public static void UpdateWorkbookValidation()
{
if (RhSolutionsAddIn.Excel.ActiveWorkbook == null)
_workbookIsValid = false;
else
{
Worksheet worksheet = RhSolutionsAddIn.Excel.ActiveWorkbook.ActiveSheet;
_workbookIsValid = worksheet.IsValidSource();
}
}
public static void EnsurePriseListExists()
{
string pricelistPath = RhSolutionsAddIn.Configuration.GetPriceListPath();
if (!File.Exists(pricelistPath))
{
RhSolutionsAddIn.Configuration.SetPriceListPath(string.Empty);
}
}
2021-12-05 16:55:36 +03:00
}