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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
// Copyright (C) 2025 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
using System;
using System.Collections.Generic;
using System.ComponentModel.Design;
using System.IO;
using Microsoft.VisualStudio.Shell;
namespace QtVsTools
{
using Core;
using Core.MsBuild;
using QtVsTools.Core.Common;
using VisualStudio;
/// <summary>
/// Command handler
/// </summary>
internal sealed class QtProjectContextMenu
{
/// <summary>
/// Gets the instance of the command.
/// </summary>
private static QtProjectContextMenu Instance
{
get;
set;
}
/// <summary>
/// Initializes the singleton instance of the command.
/// </summary>
public static void Initialize()
{
Instance = new QtProjectContextMenu();
}
/// <summary>
/// Command ID.
/// TODO: Remove, take form QtMenus.Package
/// </summary>
private enum CommandId
{
ImportPriFileProject = QtMenus.Package.ImportPriFileProject,
lUpdateOnProject = QtMenus.Package.lUpdateOnProject,
lReleaseOnProject = QtMenus.Package.lReleaseOnProject,
ProjectConvertToQtMsBuild = QtMenus.Package.ProjectConvertToQtMsBuild,
ProjectRefreshIntelliSense = QtMenus.Package.ProjectRefreshIntelliSense,
QtProjectSettingsProject = QtMenus.Package.QtProjectSettingsProject
}
/// <summary>
/// Initializes a new instance of the <see cref="QtMainMenu"/> class.
/// Adds our command handlers for menu (commands must exist in the command table file)
/// </summary>
private QtProjectContextMenu()
{
var commandService = VsServiceProvider
.GetService<IMenuCommandService, OleMenuCommandService>();
if (commandService == null)
return;
foreach (int id in Enum.GetValues(typeof(CommandId))) {
var command = new OleMenuCommand(ExecHandler,
new CommandID(QtMenus.Package.Guid, id));
command.BeforeQueryStatus += BeforeQueryStatus;
commandService.AddCommand(command);
}
}
private static void ExecHandler(object sender, EventArgs e)
{
ThreadHelper.ThrowIfNotOnUIThread();
if (sender is not OleMenuCommand command)
return;
var properties = new Dictionary<string, string>
{
{"Command", Enum.GetName(typeof(CommandId), command.CommandID.ID)}
};
Telemetry.TrackEvent(typeof(QtProjectContextMenu) + ".ExecHandler", properties);
var dte = QtVsToolsPackage.Instance.Dte;
switch (command.CommandID.ID) {
case QtMenus.Package.ImportPriFileProject:
ProjectImporter.ImportPriFile(dte, Utils.PackageInstallPath);
break;
case QtMenus.Package.lUpdateOnProject:
Translation.RunLUpdate(HelperFunctions.GetSelectedQtProject(dte));
break;
case QtMenus.Package.lReleaseOnProject:
Translation.RunLRelease(HelperFunctions.GetSelectedQtProject(dte));
break;
case QtMenus.Package.QtProjectSettingsProject:
QtVsToolsPackage.Instance.Dte.ExecuteCommand("Project.Properties");
break;
case QtMenus.Package.ProjectConvertToQtMsBuild:
if (HelperFunctions.GetSelectedProject(Instances.Package.Dte) is not { } vcProject)
break;
switch (MsBuildProjectFormat.GetVersion(vcProject)) {
case MsBuildProjectFormat.Version.Latest:
VsEditor.Open(Path.ChangeExtension(vcProject.ProjectFile, ".qtvscr"));
break;
default:
MsBuildProjectConverter.ProjectToQtMsBuild();
break;
}
break;
case QtMenus.Package.ProjectRefreshIntelliSense:
if (HelperFunctions.GetSelectedQtProject(dte) is not { } project)
break;
project.Refresh();
break;
}
}
private static void BeforeQueryStatus(object sender, EventArgs e)
{
if (sender is not OleMenuCommand command)
return;
command.Visible = command.Enabled = false;
var vcProject = HelperFunctions.GetSelectedProject(QtVsToolsPackage.Instance.Dte);
// Filter out non-VC projects
if (vcProject is null)
return;
switch (command.CommandID.ID) {
case QtMenus.Package.ImportPriFileProject:
case QtMenus.Package.QtProjectSettingsProject:
case QtMenus.Package.ProjectRefreshIntelliSense:
command.Visible = command.Enabled = MsBuildProject.GetOrAdd(vcProject) is { };
break;
case QtMenus.Package.lUpdateOnProject:
case QtMenus.Package.lReleaseOnProject:
if (MsBuildProject.GetOrAdd(vcProject) is not { } project)
break;
command.Visible = true;
command.Enabled = Translation.ToolsAvailable(project);
break;
case QtMenus.Package.ProjectConvertToQtMsBuild:
switch (MsBuildProjectFormat.GetVersion(vcProject)) {
case MsBuildProjectFormat.Version.V1:
case MsBuildProjectFormat.Version.V2:
command.Visible = command.Enabled = true;
return;
case >= MsBuildProjectFormat.Version.V3 and < MsBuildProjectFormat.Version.Latest:
command.Visible = command.Enabled = true;
command.Text = "Upgrade project to latest Qt project format version";
return;
case MsBuildProjectFormat.Version.Unknown:
command.Visible = command.Enabled = true;
command.Text = "Convert to Qt/MSBuild project";
return;
case MsBuildProjectFormat.Version.Latest:
if (File.Exists(Path.ChangeExtension(vcProject.ProjectFile, ".qtvscr"))) {
command.Visible = command.Enabled = true;
command.Text = "View conversion report";
}
return;
}
break;
}
}
}
}
|