aboutsummaryrefslogtreecommitdiffstats
path: root/QtVsTools.Package/Options/QtOptionsPage.cs
blob: 147b8d42bf7729e2610f444f0061aac9f3248503 (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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/****************************************************************************
**
** Copyright (C) 2022 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt VS Tools.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

using System;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using Microsoft.Build.Framework;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.Win32;
using EnvDTE;

namespace QtVsTools.Options
{
    using Core;
    using VisualStudio;

    using static Common.EnumExt;

    public class QtOptionsPage : DialogPage, IQtVsToolsOptions
    {
        public enum QtMsBuild
        {
            [String("QtMsBuild_Path")] Path
        }

        public enum QmlDebug
        {
            [String("QMLDebug_Enable")] Enable,
            [String("QMLDebug_Timeout")] Timeout
        }

        public enum Help
        {
            [String("Help_Preference")] Preference,
            [String("Help_TryOnF1Pressed")] TryOnF1Pressed
        }

        public enum Designer
        {
            [String("Designer_Detached")] Detached,
        }

        public enum Linguist
        {
            [String("Linguist_Detached")] Detached,
        }

        public enum ResEditor
        {
            [String("ResourceEditor_Detached")] Detached,
        }

        public enum BkgBuild
        {
            [String("BkgBuild_ProjectTracking")] ProjectTracking,
            [String("BkgBuild_RunQtTools")] RunQtTools,
            [String("BkgBuild_DebugInfo")] DebugInfo,
            [String("BkgBuild_LoggerVerbosity")] LoggerVerbosity,
        }

        public enum Notifications
        {
            [String("Notifications_Installed")] Installed,
            [String("Notifications_UpdateProjectFormat")] UpdateProjectFormat
        }

        public enum Natvis
        {
            [String("LinkNatvis")] Link,
        }

        public enum Timeout : uint { Disabled = 0 }

        class TimeoutConverter : EnumConverter
        {
            public TimeoutConverter(Type t) : base(t)
            { }

            public override bool GetStandardValuesSupported(ITypeDescriptorContext c)
                => true;

            public override bool GetStandardValuesExclusive(ITypeDescriptorContext c)
                => false;

            public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext c)
                => new StandardValuesCollection(new[] { Timeout.Disabled });

            public override object ConvertFrom(
                ITypeDescriptorContext context,
                CultureInfo culture,
                object value)
            {
                uint n = 0;
                try {
                    n = Convert.ToUInt32(value);
                } catch { }
                return (Timeout)n;
            }

            public override object ConvertTo(
                ITypeDescriptorContext context,
                CultureInfo culture,
                object value,
                Type destinationType)
            {
                if (destinationType == typeof(string))
                    return value.ToString();
                return base.ConvertTo(context, culture, value, destinationType);
            }
        }

        class EnableDisableConverter : BooleanConverter
        {
            public override object ConvertFrom(
                ITypeDescriptorContext context,
                CultureInfo culture,
                object value)
            {
                return string
                    .Equals(value as string, "Enable", StringComparison.InvariantCultureIgnoreCase);
            }

            public override object ConvertTo(
                ITypeDescriptorContext context,
                CultureInfo culture,
                object value,
                Type destinationType)
            {
                if (value is bool b && destinationType == typeof(string))
                    return b ? "Enable" : "Disable";
                return base.ConvertTo(context, culture, value, destinationType);
            }
        }

        [Category("Qt/MSBuild")]
        [DisplayName("Path to Qt/MSBuild files")]
        [Description("Corresponds to the QTMSBUILD environment variable")]
        public string QtMsBuildPath { get; set; }

        [Category("QML Debugging")]
        [DisplayName("Process debug events")]
        [Description("Set to false to turn off processing of all debug events by the QML debug engine, effectively excluding it from the debugging environment. Disabling the QML debug engine will skip debugging of QML code for all projects.")]
        public bool QmlDebuggerEnabled { get; set; }

        [Category("QML Debugging")]
        [DisplayName("Runtime connection timeout (msecs)")]
        [TypeConverter(typeof(TimeoutConverter))]
        public Timeout QmlDebuggerTimeout { get; set; }
        int IQtVsToolsOptions.QmlDebuggerTimeout => (int)QmlDebuggerTimeout;

        [Category("Help")]
        [DisplayName("Keyboard shortcut")]
        [Description("To change keyboard mapping, go to: Tools > Options > Keyboard")]
        [ReadOnly(true)]
        private string QtHelpKeyBinding { get; set; }

        [Category("Help")]
        [DisplayName("Preferred source")]
        public QtHelp.SourcePreference HelpPreference { get; set; }
        bool IQtVsToolsOptions.HelpPreferenceOnline
            => (HelpPreference == QtHelp.SourcePreference.Online);

        [Category("Help")]
        [DisplayName("Try Qt documentation when F1 is pressed")]
        public bool TryQtHelpOnF1Pressed { get; set; }

        [Category("Qt Designer")]
        [DisplayName("Run in detached window")]
        public bool DesignerDetached { get; set; }

        [Category("Qt Linguist")]
        [DisplayName("Run in detached window")]
        public bool LinguistDetached { get; set; }

        [Category("Qt Resource Editor")]
        [DisplayName("Run in detached window")]
        public bool ResourceEditorDetached { get; set; }

        [Category("IntelliSense")]
        [DisplayName("Auto project tracking")]
        [Description(
            "Enable this option to automatically keep track of project changes and trigger a"
            + " background build of Qt targets if required to keep IntelliSense updated.")]
        [TypeConverter(typeof(EnableDisableConverter))]
        public bool ProjectTracking { get; set; }

        [Category("IntelliSense")]
        [DisplayName("Run Qt tools in background build")]
        [Description(
            "Enable this option to allow all Qt tools (e.g. moc, uic) to be invoked during a"
            + " background update of IntelliSense information. If disabled, only qmake will be"
            + " invoked during background builds, to update a minimal set of Qt build properties.")]
        [TypeConverter(typeof(EnableDisableConverter))]
        public bool BuildRunQtTools { get; set; }

        [Category("IntelliSense")]
        [DisplayName("Show debug information")]
        [Description("Enable this option to display debug information about IntelliSense updates.")]
        [TypeConverter(typeof(EnableDisableConverter))]
        public bool BuildDebugInformation { get; set; }

        [Category("IntelliSense")]
        [DisplayName("Verbosity of background build log")]
        [Description("Configure verbosity level of background build log.")]
        public LoggerVerbosity BuildLoggerVerbosity { get; set; }

        [Category("Notifications")]
        [DisplayName("New version installed")]
        [Description("Show notification when a new version was recently installed.")]
        [TypeConverter(typeof(EnableDisableConverter))]
        public bool NotifyInstalled { get; set; }

        [Category("Notifications")]
        [DisplayName("Update project format")]
        [Description("Show notification when a project uses some legacy code path of the Qt "
            + "Visual Studio Tools.")]
        [TypeConverter(typeof(EnableDisableConverter))]
        public bool UpdateProjectFormat
        {
#if DISABLE_UPDATEPROJECTFORMAT
            get => false; set { }
#else
            get; set;
#endif
        }
        [Category("Natvis")]
        [DisplayName("Embed .natvis file into PDB")]
        [Description("Embeds the debugger visualizations (.natvis file) into the PDB file"
            + "generated by LINK. While setting this option, the embedded Natvis file will"
            + "take precedence over user-specific Natvis files(for example the files"
            + "located in %USERPROFILE%\\Documents\\Visual Studio 2022\\Visualizers).")]
        [TypeConverter(typeof(EnableDisableConverter))]
        public bool LinkNatvis { get; set; }

        public override void ResetSettings()
        {
            ThreadHelper.ThrowIfNotOnUIThread();

            QtMsBuildPath = "";
            QmlDebuggerEnabled = true;
            QmlDebuggerTimeout = (Timeout)60000;
            HelpPreference = QtHelp.SourcePreference.Online;
            TryQtHelpOnF1Pressed = true;
            DesignerDetached = LinguistDetached = ResourceEditorDetached = false;

            BuildRunQtTools = ProjectTracking = true;
            BuildDebugInformation = false;
            BuildLoggerVerbosity = LoggerVerbosity.Quiet;
            NotifyInstalled = true;
            UpdateProjectFormat = true;
            LinkNatvis = true;

            ////////
            // Get Qt Help keyboard shortcut
            //
            var dte = VsServiceProvider.GetService<SDTE, DTE>();
            var f1QtHelpBindings = dte.Commands.Item("QtVSTools.F1QtHelp")?.Bindings as Array;
            var binding = f1QtHelpBindings.Cast<string>()
                .Select(x => x.Split(new[] { "::" }, StringSplitOptions.None))
                .Select(x => new { Scope = x.FirstOrDefault(), Shortcut = x.LastOrDefault() })
                .FirstOrDefault();
            if (binding != null)
                QtHelpKeyBinding = string.Format("[{0}] {1}", binding.Scope, binding.Shortcut);
            else
                QtHelpKeyBinding = "";
        }

        public override void LoadSettingsFromStorage()
        {
            ThreadHelper.ThrowIfNotOnUIThread();

            ResetSettings();
            try {
                QtMsBuildPath = Environment.GetEnvironmentVariable("QTMSBUILD");

                using (var key = Registry.CurrentUser
                    .OpenSubKey(@"SOFTWARE\" + Resources.registryPackagePath, writable: false)) {
                    if (key == null)
                        return;
                    Load(() => QmlDebuggerEnabled, key, QmlDebug.Enable);
                    Load(() => QmlDebuggerTimeout, key, QmlDebug.Timeout);
                    Load(() => HelpPreference, key, Help.Preference);
                    Load(() => TryQtHelpOnF1Pressed, key, Help.TryOnF1Pressed);
                    Load(() => DesignerDetached, key, Designer.Detached);
                    Load(() => LinguistDetached, key, Linguist.Detached);
                    Load(() => ResourceEditorDetached, key, ResEditor.Detached);
                    Load(() => ProjectTracking, key, BkgBuild.ProjectTracking);
                    Load(() => BuildRunQtTools, key, BkgBuild.RunQtTools);
                    Load(() => BuildDebugInformation, key, BkgBuild.DebugInfo);
                    Load(() => BuildLoggerVerbosity, key, BkgBuild.LoggerVerbosity);
                    Load(() => NotifyInstalled, key, Notifications.Installed);
                    Load(() => UpdateProjectFormat, key, Notifications.UpdateProjectFormat);
                    Load(() => LinkNatvis, key, Natvis.Link);
                }
            } catch (Exception exception) {
                exception.Log();
            }
        }

        public override void SaveSettingsToStorage()
        {
            try {
                if (!string.IsNullOrEmpty(QtMsBuildPath)
                    && QtMsBuildPath != Environment.GetEnvironmentVariable("QTMSBUILD")) {
                    Environment.SetEnvironmentVariable(
                        "QTMSBUILD", QtMsBuildPath, EnvironmentVariableTarget.User);
                    Environment.SetEnvironmentVariable(
                        "QTMSBUILD", QtMsBuildPath, EnvironmentVariableTarget.Process);
                }

                using (var key = Registry.CurrentUser
                    .CreateSubKey(@"SOFTWARE\" + Resources.registryPackagePath)) {
                    if (key == null)
                        return;
                    Save(QmlDebuggerEnabled, key, QmlDebug.Enable);
                    Save(QmlDebuggerTimeout, key, QmlDebug.Timeout);
                    Save(HelpPreference, key, Help.Preference);
                    Save(TryQtHelpOnF1Pressed, key, Help.TryOnF1Pressed);
                    Save(DesignerDetached, key, Designer.Detached);
                    Save(LinguistDetached, key, Linguist.Detached);
                    Save(ResourceEditorDetached, key, ResEditor.Detached);
                    Save(ProjectTracking, key, BkgBuild.ProjectTracking);
                    Save(BuildRunQtTools, key, BkgBuild.RunQtTools);
                    Save(BuildDebugInformation, key, BkgBuild.DebugInfo);
                    Save(BuildLoggerVerbosity, key, BkgBuild.LoggerVerbosity);
                    Save(NotifyInstalled, key, Notifications.Installed);
                    Save(UpdateProjectFormat, key, Notifications.UpdateProjectFormat);
                    Save(LinkNatvis, key, Natvis.Link);
                }
            } catch (Exception exception) {
                exception.Log();
            }
        }

        void Save<T>(T property, RegistryKey key, Enum name)
        {
            object value = property;
            if (Equals<T, bool>())
                value = ((bool)(object)property) ? 1 : 0;
            else if (Equals<T, Timeout>())
                value = Convert.ToInt32(property);
            else if (typeof(T).IsEnum)
                value = Enum.GetName(typeof(T), property);
            key.SetValue(name.Cast<string>(), value);
        }

        void Load<T>(Expression<Func<T>> propertyByRef, RegistryKey key, Enum name)
        {
            var propertyExpr = (MemberExpression)propertyByRef.Body;
            var property = (PropertyInfo)propertyExpr.Member;
            var regValue = key.GetValue(name.Cast<string>());
            if (Equals<T, bool>() && regValue is int numValue)
                property.SetValue(this, numValue == 1);
            else if (Equals<T, Timeout>() && regValue is int timeout)
                property.SetValue(this, (Timeout)timeout);
            else if (typeof(T).IsEnum && regValue is string enumValue)
                property.SetValue(this, Enum.Parse(typeof(T), enumValue));
            else if (regValue is T value)
                property.SetValue(this, value);
        }

        bool Equals<T1, T2>()
        {
            return typeof(T1) == typeof(T2);
        }
    }
}