aboutsummaryrefslogtreecommitdiff
path: root/RhSolutions.AddIn/Controllers/RibbonController.cs
blob: c815229ebd8d983d0735360c3f04792ab598a06e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
using ExcelDna.Integration.CustomUI;
using System.Reflection;
using System.Runtime.InteropServices;
#if! NET472
using System.Runtime.Versioning;
#endif
using System.Windows.Forms;

namespace RhSolutions.Controllers;

#if !NET472
[SupportedOSPlatform("windows")]
#endif
[ComVisible(true)]
public class RibbonController : ExcelRibbon
{
    private static IRibbonUI ribbonUi;

    public override string GetCustomUI(string RibbonID)
    {
        return @"
      <customUI onLoad='RibbonLoad' xmlns='http://schemas.microsoft.com/office/2006/01/customui'>
      <ribbon>
        <tabs>
          <tab id='rau' label='RhSolutions'>
            <group id='priceList' label='Прайс-лист'>
                <button id='export' getEnabled='GetExportEnabled' label='Экспорт в новый файл' size='normal' imageMso='PivotExportToExcel' 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='dxfexport' getEnabled='GetDxfEnabled' label='Экспортировать в DXF' size='normal' imageMso='ExportExcel' onAction='OnToolPressed'/>
            </group>
            <group id='rausettings' getLabel='GetVersionLabel'>
                <button id='setPriceList' getLabel='GetPriceListPathLabel' size='large' imageMso='TableExcelSpreadsheetInsert' 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
        {
            using ToolBase tool = control.Id switch
            {
                "export" => new ExportTool(),
                "convert" => new ConvertTool(),
                "merge" => new MergeTool(),
                "dxfexport" => new DxfTool(),
                _ => throw new Exception("Неизвестный инструмент"),
            };
            tool.Execute();
        }

        catch (Exception exception)
        {
            MessageBox.Show(exception.Message,
                "Ошибка",
                MessageBoxButtons.OK,
                MessageBoxIcon.Information);
            RhSolutionsAddIn.Excel.StatusBar = false;
            return;
        }
    }

    public bool GetConvertEnabled(IRibbonControl control)
    {
        if (RhSolutionsAddIn.Excel.ActiveWorkbook == null)
            return false;

        else
        {
            Worksheet worksheet = RhSolutionsAddIn.Excel.ActiveWorkbook.ActiveSheet;
            return worksheet.IsValidSource();
        }
    }

    public bool GetExportEnabled(IRibbonControl control)
    {
        if (RhSolutionsAddIn.Excel.ActiveWorkbook == null)
            return false;

        else
        {
            Range selection = RhSolutionsAddIn.Excel.Selection;
            return selection.Columns.Count == 2;
        }
    }

    public bool GetDxfEnabled(IRibbonControl control)
    {
        if (RhSolutionsAddIn.Excel.ActiveWorkbook == null)
            return false;

        else
        {
            Worksheet worksheet = RhSolutionsAddIn.Excel.ActiveWorkbook.ActiveSheet;
            return worksheet.IsValidSource();
        }
    }

    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;
    }
}