1 | /* $Id: UIGlobalSettingsInput.cpp 101241 2023-09-22 15:40:32Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Qt GUI - UIGlobalSettingsInput class implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
26 | */
|
---|
27 |
|
---|
28 | /* Qt includes: */
|
---|
29 | #include <QVBoxLayout>
|
---|
30 |
|
---|
31 | /* GUI includes: */
|
---|
32 | #include "UIAutoCaptureKeyboardEditor.h"
|
---|
33 | #include "UIExtraDataManager.h"
|
---|
34 | #include "UIGlobalSettingsInput.h"
|
---|
35 | #include "UIHostComboEditor.h"
|
---|
36 | #include "UIShortcutConfigurationEditor.h"
|
---|
37 | #include "UIShortcutPool.h"
|
---|
38 | #include "UITranslator.h"
|
---|
39 |
|
---|
40 |
|
---|
41 | /** Global settings: Input page data structure. */
|
---|
42 | struct UIDataSettingsGlobalInput
|
---|
43 | {
|
---|
44 | /** Constructs cache. */
|
---|
45 | UIDataSettingsGlobalInput()
|
---|
46 | : m_fAutoCapture(false)
|
---|
47 | {}
|
---|
48 |
|
---|
49 | /** Returns whether the @a other passed data is equal to this one. */
|
---|
50 | bool equal(const UIDataSettingsGlobalInput &other) const
|
---|
51 | {
|
---|
52 | return true
|
---|
53 | && (m_shortcuts == other.m_shortcuts)
|
---|
54 | && (m_fAutoCapture == other.m_fAutoCapture)
|
---|
55 | ;
|
---|
56 | }
|
---|
57 |
|
---|
58 | /** Returns whether the @a other passed data is equal to this one. */
|
---|
59 | bool operator==(const UIDataSettingsGlobalInput &other) const { return equal(other); }
|
---|
60 | /** Returns whether the @a other passed data is different from this one. */
|
---|
61 | bool operator!=(const UIDataSettingsGlobalInput &other) const { return !equal(other); }
|
---|
62 |
|
---|
63 | /** Holds the shortcut configuration list. */
|
---|
64 | UIShortcutConfigurationList m_shortcuts;
|
---|
65 | /** Holds whether the keyboard auto-capture is enabled. */
|
---|
66 | bool m_fAutoCapture;
|
---|
67 | };
|
---|
68 |
|
---|
69 |
|
---|
70 | /*********************************************************************************************************************************
|
---|
71 | * Class UIGlobalSettingsInput implementation. *
|
---|
72 | *********************************************************************************************************************************/
|
---|
73 |
|
---|
74 | UIGlobalSettingsInput::UIGlobalSettingsInput()
|
---|
75 | : m_pCache(0)
|
---|
76 | , m_pEditorShortcutConfiguration(0)
|
---|
77 | , m_pEditorAutoCaptureKeyboard(0)
|
---|
78 | {
|
---|
79 | prepare();
|
---|
80 | }
|
---|
81 |
|
---|
82 | UIGlobalSettingsInput::~UIGlobalSettingsInput()
|
---|
83 | {
|
---|
84 | cleanup();
|
---|
85 | }
|
---|
86 |
|
---|
87 | bool UIGlobalSettingsInput::changed() const
|
---|
88 | {
|
---|
89 | return m_pCache ? m_pCache->wasChanged() : false;
|
---|
90 | }
|
---|
91 |
|
---|
92 | void UIGlobalSettingsInput::loadToCacheFrom(QVariant &data)
|
---|
93 | {
|
---|
94 | /* Sanity check: */
|
---|
95 | if (!m_pCache)
|
---|
96 | return;
|
---|
97 |
|
---|
98 | /* Fetch data to properties: */
|
---|
99 | UISettingsPageGlobal::fetchData(data);
|
---|
100 |
|
---|
101 | /* Clear cache initially: */
|
---|
102 | m_pCache->clear();
|
---|
103 |
|
---|
104 | /* Cache old data: */
|
---|
105 | UIDataSettingsGlobalInput oldData;
|
---|
106 | UIShortcutConfigurationList list;
|
---|
107 | list << UIShortcutConfigurationItem(UIHostCombo::hostComboCacheKey(),
|
---|
108 | QString(),
|
---|
109 | tr("Host Key Combination"),
|
---|
110 | gEDataManager->hostKeyCombination(),
|
---|
111 | QString());
|
---|
112 | const QMap<QString, UIShortcut> &shortcuts = gShortcutPool->shortcuts();
|
---|
113 | const QList<QString> shortcutKeys = shortcuts.keys();
|
---|
114 | foreach (const QString &strShortcutKey, shortcutKeys)
|
---|
115 | {
|
---|
116 | const UIShortcut &shortcut = shortcuts.value(strShortcutKey);
|
---|
117 | list << UIShortcutConfigurationItem(strShortcutKey,
|
---|
118 | shortcut.scope(),
|
---|
119 | UITranslator::removeAccelMark(shortcut.description()),
|
---|
120 | shortcut.primaryToNativeText(),
|
---|
121 | shortcut.defaultSequence().toString(QKeySequence::NativeText));
|
---|
122 | }
|
---|
123 | oldData.m_shortcuts = list;
|
---|
124 | oldData.m_fAutoCapture = gEDataManager->autoCaptureEnabled();
|
---|
125 | m_pCache->cacheInitialData(oldData);
|
---|
126 |
|
---|
127 | /* Upload properties to data: */
|
---|
128 | UISettingsPageGlobal::uploadData(data);
|
---|
129 | }
|
---|
130 |
|
---|
131 | void UIGlobalSettingsInput::getFromCache()
|
---|
132 | {
|
---|
133 | /* Sanity check: */
|
---|
134 | if (!m_pCache)
|
---|
135 | return;
|
---|
136 |
|
---|
137 | /* Load old data from cache: */
|
---|
138 | const UIDataSettingsGlobalInput &oldData = m_pCache->base();
|
---|
139 | if (m_pEditorShortcutConfiguration)
|
---|
140 | m_pEditorShortcutConfiguration->load(oldData.m_shortcuts);
|
---|
141 | if (m_pEditorAutoCaptureKeyboard)
|
---|
142 | m_pEditorAutoCaptureKeyboard->setValue(oldData.m_fAutoCapture);
|
---|
143 |
|
---|
144 | /* Revalidate: */
|
---|
145 | revalidate();
|
---|
146 | }
|
---|
147 |
|
---|
148 | void UIGlobalSettingsInput::putToCache()
|
---|
149 | {
|
---|
150 | /* Sanity check: */
|
---|
151 | if (!m_pCache)
|
---|
152 | return;
|
---|
153 |
|
---|
154 | /* Prepare new data: */
|
---|
155 | UIDataSettingsGlobalInput newData = m_pCache->base();
|
---|
156 |
|
---|
157 | /* Cache new data: */
|
---|
158 | if (m_pEditorShortcutConfiguration)
|
---|
159 | m_pEditorShortcutConfiguration->save(newData.m_shortcuts);
|
---|
160 | if (m_pEditorAutoCaptureKeyboard)
|
---|
161 | newData.m_fAutoCapture = m_pEditorAutoCaptureKeyboard->value();
|
---|
162 | m_pCache->cacheCurrentData(newData);
|
---|
163 | }
|
---|
164 |
|
---|
165 | void UIGlobalSettingsInput::saveFromCacheTo(QVariant &data)
|
---|
166 | {
|
---|
167 | /* Fetch data to properties: */
|
---|
168 | UISettingsPageGlobal::fetchData(data);
|
---|
169 |
|
---|
170 | /* Update data and failing state: */
|
---|
171 | setFailed(!saveData());
|
---|
172 |
|
---|
173 | /* Upload properties to data: */
|
---|
174 | UISettingsPageGlobal::uploadData(data);
|
---|
175 | }
|
---|
176 |
|
---|
177 | bool UIGlobalSettingsInput::validate(QList<UIValidationMessage> &messages)
|
---|
178 | {
|
---|
179 | /* Pass by default: */
|
---|
180 | bool fPass = true;
|
---|
181 |
|
---|
182 | /* Check VirtualBox Manager page for unique shortcuts: */
|
---|
183 | if (!m_pEditorShortcutConfiguration->isShortcutsUniqueManager())
|
---|
184 | {
|
---|
185 | UIValidationMessage message;
|
---|
186 | message.first = UITranslator::removeAccelMark(m_pEditorShortcutConfiguration->tabNameManager());
|
---|
187 | message.second << tr("Some items have the same shortcuts assigned.");
|
---|
188 | messages << message;
|
---|
189 | fPass = false;
|
---|
190 | }
|
---|
191 |
|
---|
192 | /* Check Virtual Runtime page for unique shortcuts: */
|
---|
193 | if (!m_pEditorShortcutConfiguration->isShortcutsUniqueRuntime())
|
---|
194 | {
|
---|
195 | UIValidationMessage message;
|
---|
196 | message.first = UITranslator::removeAccelMark(m_pEditorShortcutConfiguration->tabNameRuntime());
|
---|
197 | message.second << tr("Some items have the same shortcuts assigned.");
|
---|
198 | messages << message;
|
---|
199 | fPass = false;
|
---|
200 | }
|
---|
201 |
|
---|
202 | /* Return result: */
|
---|
203 | return fPass;
|
---|
204 | }
|
---|
205 |
|
---|
206 | void UIGlobalSettingsInput::retranslateUi()
|
---|
207 | {
|
---|
208 | }
|
---|
209 |
|
---|
210 | void UIGlobalSettingsInput::prepare()
|
---|
211 | {
|
---|
212 | /* Prepare cache: */
|
---|
213 | m_pCache = new UISettingsCacheGlobalInput;
|
---|
214 | AssertPtrReturnVoid(m_pCache);
|
---|
215 |
|
---|
216 | /* Prepare everything: */
|
---|
217 | prepareWidgets();
|
---|
218 |
|
---|
219 | /* Apply language settings: */
|
---|
220 | retranslateUi();
|
---|
221 | }
|
---|
222 |
|
---|
223 | void UIGlobalSettingsInput::prepareWidgets()
|
---|
224 | {
|
---|
225 | /* Prepare main layout: */
|
---|
226 | QVBoxLayout *pLayout = new QVBoxLayout(this);
|
---|
227 | if (pLayout)
|
---|
228 | {
|
---|
229 | /* Prepare 'shortcut configuration' editor: */
|
---|
230 | m_pEditorShortcutConfiguration = new UIShortcutConfigurationEditor(this);
|
---|
231 | if (m_pEditorShortcutConfiguration)
|
---|
232 | {
|
---|
233 | connect(m_pEditorShortcutConfiguration, &UIShortcutConfigurationEditor::sigValueChanged,
|
---|
234 | this, &UIGlobalSettingsInput::revalidate);
|
---|
235 | addEditor(m_pEditorShortcutConfiguration);
|
---|
236 | pLayout->addWidget(m_pEditorShortcutConfiguration);
|
---|
237 | }
|
---|
238 |
|
---|
239 | /* Prepare 'auto capture keyboard' editor: */
|
---|
240 | m_pEditorAutoCaptureKeyboard = new UIAutoCaptureKeyboardEditor(this);
|
---|
241 | if (m_pEditorAutoCaptureKeyboard)
|
---|
242 | {
|
---|
243 | addEditor(m_pEditorAutoCaptureKeyboard);
|
---|
244 | pLayout->addWidget(m_pEditorAutoCaptureKeyboard);
|
---|
245 | }
|
---|
246 | }
|
---|
247 | }
|
---|
248 |
|
---|
249 | void UIGlobalSettingsInput::cleanup()
|
---|
250 | {
|
---|
251 | /* Cleanup cache: */
|
---|
252 | delete m_pCache;
|
---|
253 | m_pCache = 0;
|
---|
254 | }
|
---|
255 |
|
---|
256 | bool UIGlobalSettingsInput::saveData()
|
---|
257 | {
|
---|
258 | /* Sanity check: */
|
---|
259 | if (!m_pCache)
|
---|
260 | return false;
|
---|
261 |
|
---|
262 | /* Prepare result: */
|
---|
263 | bool fSuccess = true;
|
---|
264 | /* Save settings from cache: */
|
---|
265 | if ( fSuccess
|
---|
266 | && m_pCache->wasChanged())
|
---|
267 | {
|
---|
268 | /* Get old data from cache: */
|
---|
269 | const UIDataSettingsGlobalInput &oldData = m_pCache->base();
|
---|
270 | /* Get new data from cache: */
|
---|
271 | const UIDataSettingsGlobalInput &newData = m_pCache->data();
|
---|
272 |
|
---|
273 | /* Save new host-combo shortcut from cache: */
|
---|
274 | if (fSuccess)
|
---|
275 | {
|
---|
276 | const UIShortcutConfigurationItem fakeHostComboItem(UIHostCombo::hostComboCacheKey(), QString(), QString(), QString(), QString());
|
---|
277 | const int iHostComboItemBase = UIShortcutSearchFunctor<UIShortcutConfigurationItem>()(oldData.m_shortcuts, fakeHostComboItem);
|
---|
278 | const int iHostComboItemData = UIShortcutSearchFunctor<UIShortcutConfigurationItem>()(newData.m_shortcuts, fakeHostComboItem);
|
---|
279 | const QString strHostComboBase = iHostComboItemBase != -1 ? oldData.m_shortcuts.at(iHostComboItemBase).currentSequence() : QString();
|
---|
280 | const QString strHostComboData = iHostComboItemData != -1 ? newData.m_shortcuts.at(iHostComboItemData).currentSequence() : QString();
|
---|
281 | if (strHostComboData != strHostComboBase)
|
---|
282 | /* fSuccess = */ gEDataManager->setHostKeyCombination(strHostComboData);
|
---|
283 | }
|
---|
284 |
|
---|
285 | /* Save other new shortcuts from cache: */
|
---|
286 | if (fSuccess)
|
---|
287 | {
|
---|
288 | QMap<QString, QString> sequencesBase;
|
---|
289 | QMap<QString, QString> sequencesData;
|
---|
290 | foreach (const UIShortcutConfigurationItem &item, oldData.m_shortcuts)
|
---|
291 | sequencesBase.insert(item.key(), item.currentSequence());
|
---|
292 | foreach (const UIShortcutConfigurationItem &item, newData.m_shortcuts)
|
---|
293 | sequencesData.insert(item.key(), item.currentSequence());
|
---|
294 | if (sequencesData != sequencesBase)
|
---|
295 | /* fSuccess = */ gShortcutPool->setOverrides(sequencesData);
|
---|
296 | }
|
---|
297 |
|
---|
298 | /* Save other new things from cache: */
|
---|
299 | if ( fSuccess
|
---|
300 | && newData.m_fAutoCapture != oldData.m_fAutoCapture)
|
---|
301 | /* fSuccess = */ gEDataManager->setAutoCaptureEnabled(newData.m_fAutoCapture);
|
---|
302 | }
|
---|
303 | /* Return result: */
|
---|
304 | return fSuccess;
|
---|
305 | }
|
---|