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

136 lines
5.0 KiB
C#
Raw Permalink 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
{
2023-03-28 10:36:36 +03:00
private static IRibbonUI ribbonUi;
2023-05-27 08:50:30 +03:00
private static bool _workbookIsValid;
2023-03-28 10:36:36 +03:00
public override string GetCustomUI(string RibbonID)
{
return @"
2023-12-09 23:59:57 +03:00
<customUI onLoad='RibbonLoad' xmlns='http://schemas.microsoft.com/office/2006/01/customui' loadImage='LoadImage'>
2021-12-05 16:55:36 +03:00
<ribbon>
<tabs>
2022-12-19 08:44:32 +03:00
<tab id='rau' label='RhSolutions'>
<group id='priceList' label='Прайс-лист'>
2023-12-09 23:59:57 +03:00
<button id='export' getEnabled='GetExportEnabled' label='Экспорт в новый файл' size='normal' image='RhSolutions' onAction='OnToolPressed'/>
2022-02-12 16:57:29 +03:00
<button id='convert' getEnabled='GetConvertEnabled' label='Актуализировать' size='normal' imageMso='FileUpdate' onAction='OnToolPressed'/>
2023-04-06 08:29:39 +03:00
<button id='merge' label='Объединить' size='normal' imageMso='Copy' onAction='OnToolPressed'/>
2023-12-12 23:43:10 +03:00
<button id='guess' getEnabled='GetGuessEnabled' label='Найти и экспортировать' size='large' imageMso='ControlWizards' onAction='OnToolPressed'/>
</group>
2023-12-12 23:43:10 +03:00
<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='settings' getLabel='GetVersionLabel'>
2023-12-09 23:59:57 +03:00
<button id='setPriceList' getLabel='GetPriceListPathLabel' size='large' image='RhSolutions' onAction='OnSetPricePressed'/>
</group>
2021-12-05 16:55:36 +03:00
</tab>
</tabs>
</ribbon>
</customUI>";
2023-03-28 10:36:36 +03:00
}
2023-03-28 10:36:36 +03:00
public void RibbonLoad(IRibbonUI sender)
{
ribbonUi = sender;
}
2023-03-28 10:36:36 +03:00
public static void RefreshControl(string id)
{
2023-04-06 08:29:39 +03:00
ribbonUi?.InvalidateControl(id);
2023-03-28 10:36:36 +03:00
}
2023-03-28 07:25:10 +03:00
2023-03-28 10:36:36 +03:00
public void OnSetPricePressed(IRibbonControl control)
{
2023-04-06 08:29:39 +03:00
IFileDialog dialog = RhSolutionsAddIn.ServiceProvider.GetService<IFileDialog>();
string file = dialog.GetFile();
2023-04-06 08:29:39 +03:00
if (!string.IsNullOrEmpty(file))
{
2023-04-06 08:29:39 +03:00
RhSolutionsAddIn.Configuration.SetPriceListPath(file);
2023-03-28 10:36:36 +03:00
RhSolutionsAddIn.Configuration.SaveSettings();
2021-12-24 16:22:03 +03:00
}
2023-03-28 10:36:36 +03:00
}
2021-12-24 16:22:03 +03:00
2023-03-28 10:36:36 +03:00
public void OnToolPressed(IRibbonControl control)
{
try
{
2023-04-14 08:08:46 +03:00
var toolFactory = RhSolutionsAddIn.ServiceProvider.GetService<ToolFactory>();
using Tool tool = toolFactory.GetTool(control.Id);
2023-04-06 08:29:39 +03:00
tool.Execute();
}
2023-03-28 10:36:36 +03:00
catch (Exception exception)
{
2023-03-28 10:36:36 +03:00
MessageBox.Show(exception.Message,
"Ошибка",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
RhSolutionsAddIn.Excel.StatusBar = false;
return;
}
2023-03-28 10:36:36 +03:00
}
2023-05-27 08:50:30 +03:00
public bool GetConvertEnabled(IRibbonControl control) => _workbookIsValid;
public bool GetDxfEnabled(IRibbonControl control) => _workbookIsValid;
2023-11-09 23:34:52 +03:00
public bool GetFittingsCalcEnabled(IRibbonControl control) => _workbookIsValid;
2023-05-27 08:58:23 +03:00
public bool GetGuessEnabled(IRibbonControl control) => RhSolutionsAddIn.Excel.ActiveWorkbook != null && !_workbookIsValid;
2023-03-28 10:36:36 +03:00
public bool GetExportEnabled(IRibbonControl control)
{
if (RhSolutionsAddIn.Excel.ActiveWorkbook == null)
return false;
2023-03-28 10:36:36 +03:00
else
{
2023-03-28 10:36:36 +03:00
Range selection = RhSolutionsAddIn.Excel.Selection;
return selection.Columns.Count == 2;
}
2021-12-05 16:55:36 +03:00
}
2023-03-28 10:36:36 +03:00
2023-05-27 08:50:30 +03:00
public string GetVersionLabel(IRibbonControl control)
2023-04-07 08:10:51 +03:00
{
2023-05-27 08:50:30 +03:00
string version = Assembly.GetExecutingAssembly().GetName().Version.ToString();
return $"v{version}";
}
2023-04-07 08:10:51 +03:00
2023-05-27 08:50:30 +03:00
public string GetPriceListPathLabel(IRibbonControl control)
{
string name = RhSolutionsAddIn.Configuration.GetPriceListFileName();
2024-02-22 18:09:27 +03:00
return string.IsNullOrEmpty(name) ? "Указать шаблонный файл" : name;
2023-04-07 08:10:51 +03:00
}
2023-05-27 08:50:30 +03:00
public static void UpdateWorkbookValidation()
{
if (RhSolutionsAddIn.Excel.ActiveWorkbook == null)
2023-05-27 08:50:30 +03:00
_workbookIsValid = false;
else
{
Worksheet worksheet = RhSolutionsAddIn.Excel.ActiveWorkbook.ActiveSheet;
_workbookIsValid = worksheet.IsValidSource();
}
}
2024-02-22 18:09:27 +03:00
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
}