VirtualBox

source: vbox/trunk/src/VBox/Frontends/VirtualBox/src/settings/global/UIGlobalSettingsInput.cpp

Last change on this file was 104313, checked in by vboxsync, 2 months ago

FE/Qt. bugref:10622. Using new UITranslationEventListener in the settings related GUI classes.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.3 KB
Line 
1/* $Id: UIGlobalSettingsInput.cpp 104313 2024-04-12 13:10:30Z 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/** Global settings: Input page data structure. */
41struct UIDataSettingsGlobalInput
42{
43 /** Constructs cache. */
44 UIDataSettingsGlobalInput()
45 : m_fAutoCapture(false)
46 {}
47
48 /** Returns whether the @a other passed data is equal to this one. */
49 bool equal(const UIDataSettingsGlobalInput &other) const
50 {
51 return true
52 && (m_shortcuts == other.m_shortcuts)
53 && (m_fAutoCapture == other.m_fAutoCapture)
54 ;
55 }
56
57 /** Returns whether the @a other passed data is equal to this one. */
58 bool operator==(const UIDataSettingsGlobalInput &other) const { return equal(other); }
59 /** Returns whether the @a other passed data is different from this one. */
60 bool operator!=(const UIDataSettingsGlobalInput &other) const { return !equal(other); }
61
62 /** Holds the shortcut configuration list. */
63 UIShortcutConfigurationList m_shortcuts;
64 /** Holds whether the keyboard auto-capture is enabled. */
65 bool m_fAutoCapture;
66};
67
68
69/*********************************************************************************************************************************
70* Class UIGlobalSettingsInput implementation. *
71*********************************************************************************************************************************/
72
73UIGlobalSettingsInput::UIGlobalSettingsInput()
74 : m_pCache(0)
75 , m_pEditorShortcutConfiguration(0)
76 , m_pEditorAutoCaptureKeyboard(0)
77{
78 prepare();
79}
80
81UIGlobalSettingsInput::~UIGlobalSettingsInput()
82{
83 cleanup();
84}
85
86bool UIGlobalSettingsInput::changed() const
87{
88 return m_pCache ? m_pCache->wasChanged() : false;
89}
90
91void UIGlobalSettingsInput::loadToCacheFrom(QVariant &data)
92{
93 /* Sanity check: */
94 if (!m_pCache)
95 return;
96
97 /* Fetch data to properties: */
98 UISettingsPageGlobal::fetchData(data);
99
100 /* Clear cache initially: */
101 m_pCache->clear();
102
103 /* Cache old data: */
104 UIDataSettingsGlobalInput oldData;
105 UIShortcutConfigurationList list;
106 list << UIShortcutConfigurationItem(UIHostCombo::hostComboCacheKey(),
107 QString(),
108 tr("Host Key Combination"),
109 gEDataManager->hostKeyCombination(),
110 QString());
111 const QMap<QString, UIShortcut> &shortcuts = gShortcutPool->shortcuts();
112 const QList<QString> shortcutKeys = shortcuts.keys();
113 foreach (const QString &strShortcutKey, shortcutKeys)
114 {
115 const UIShortcut &shortcut = shortcuts.value(strShortcutKey);
116 list << UIShortcutConfigurationItem(strShortcutKey,
117 shortcut.scope(),
118 UITranslator::removeAccelMark(shortcut.description()),
119 shortcut.primaryToNativeText(),
120 shortcut.defaultSequence().toString(QKeySequence::NativeText));
121 }
122 oldData.m_shortcuts = list;
123 oldData.m_fAutoCapture = gEDataManager->autoCaptureEnabled();
124 m_pCache->cacheInitialData(oldData);
125
126 /* Upload properties to data: */
127 UISettingsPageGlobal::uploadData(data);
128}
129
130void UIGlobalSettingsInput::getFromCache()
131{
132 /* Sanity check: */
133 if (!m_pCache)
134 return;
135
136 /* Load old data from cache: */
137 const UIDataSettingsGlobalInput &oldData = m_pCache->base();
138 if (m_pEditorShortcutConfiguration)
139 m_pEditorShortcutConfiguration->load(oldData.m_shortcuts);
140 if (m_pEditorAutoCaptureKeyboard)
141 m_pEditorAutoCaptureKeyboard->setValue(oldData.m_fAutoCapture);
142
143 /* Revalidate: */
144 revalidate();
145}
146
147void UIGlobalSettingsInput::putToCache()
148{
149 /* Sanity check: */
150 if (!m_pCache)
151 return;
152
153 /* Prepare new data: */
154 UIDataSettingsGlobalInput newData = m_pCache->base();
155
156 /* Cache new data: */
157 if (m_pEditorShortcutConfiguration)
158 m_pEditorShortcutConfiguration->save(newData.m_shortcuts);
159 if (m_pEditorAutoCaptureKeyboard)
160 newData.m_fAutoCapture = m_pEditorAutoCaptureKeyboard->value();
161 m_pCache->cacheCurrentData(newData);
162}
163
164void UIGlobalSettingsInput::saveFromCacheTo(QVariant &data)
165{
166 /* Fetch data to properties: */
167 UISettingsPageGlobal::fetchData(data);
168
169 /* Update data and failing state: */
170 setFailed(!saveData());
171
172 /* Upload properties to data: */
173 UISettingsPageGlobal::uploadData(data);
174}
175
176bool UIGlobalSettingsInput::validate(QList<UIValidationMessage> &messages)
177{
178 /* Pass by default: */
179 bool fPass = true;
180
181 /* Check VirtualBox Manager page for unique shortcuts: */
182 if (!m_pEditorShortcutConfiguration->isShortcutsUniqueManager())
183 {
184 UIValidationMessage message;
185 message.first = UITranslator::removeAccelMark(m_pEditorShortcutConfiguration->tabNameManager());
186 message.second << tr("Some items have the same shortcuts assigned.");
187 messages << message;
188 fPass = false;
189 }
190
191 /* Check Virtual Runtime page for unique shortcuts: */
192 if (!m_pEditorShortcutConfiguration->isShortcutsUniqueRuntime())
193 {
194 UIValidationMessage message;
195 message.first = UITranslator::removeAccelMark(m_pEditorShortcutConfiguration->tabNameRuntime());
196 message.second << tr("Some items have the same shortcuts assigned.");
197 messages << message;
198 fPass = false;
199 }
200
201 /* Return result: */
202 return fPass;
203}
204
205void UIGlobalSettingsInput::sltRetranslateUI()
206{
207}
208
209void UIGlobalSettingsInput::prepare()
210{
211 /* Prepare cache: */
212 m_pCache = new UISettingsCacheGlobalInput;
213 AssertPtrReturnVoid(m_pCache);
214
215 /* Prepare everything: */
216 prepareWidgets();
217
218 /* Apply language settings: */
219 sltRetranslateUI();
220}
221
222void UIGlobalSettingsInput::prepareWidgets()
223{
224 /* Prepare main layout: */
225 QVBoxLayout *pLayout = new QVBoxLayout(this);
226 if (pLayout)
227 {
228 /* Prepare 'shortcut configuration' editor: */
229 m_pEditorShortcutConfiguration = new UIShortcutConfigurationEditor(this);
230 if (m_pEditorShortcutConfiguration)
231 {
232 connect(m_pEditorShortcutConfiguration, &UIShortcutConfigurationEditor::sigValueChanged,
233 this, &UIGlobalSettingsInput::revalidate);
234 addEditor(m_pEditorShortcutConfiguration);
235 pLayout->addWidget(m_pEditorShortcutConfiguration);
236 }
237
238 /* Prepare 'auto capture keyboard' editor: */
239 m_pEditorAutoCaptureKeyboard = new UIAutoCaptureKeyboardEditor(this);
240 if (m_pEditorAutoCaptureKeyboard)
241 {
242 addEditor(m_pEditorAutoCaptureKeyboard);
243 pLayout->addWidget(m_pEditorAutoCaptureKeyboard);
244 }
245 }
246}
247
248void UIGlobalSettingsInput::cleanup()
249{
250 /* Cleanup cache: */
251 delete m_pCache;
252 m_pCache = 0;
253}
254
255bool UIGlobalSettingsInput::saveData()
256{
257 /* Sanity check: */
258 if (!m_pCache)
259 return false;
260
261 /* Prepare result: */
262 bool fSuccess = true;
263 /* Save settings from cache: */
264 if ( fSuccess
265 && m_pCache->wasChanged())
266 {
267 /* Get old data from cache: */
268 const UIDataSettingsGlobalInput &oldData = m_pCache->base();
269 /* Get new data from cache: */
270 const UIDataSettingsGlobalInput &newData = m_pCache->data();
271
272 /* Save new host-combo shortcut from cache: */
273 if (fSuccess)
274 {
275 const UIShortcutConfigurationItem fakeHostComboItem(UIHostCombo::hostComboCacheKey(), QString(), QString(), QString(), QString());
276 const int iHostComboItemBase = UIShortcutSearchFunctor<UIShortcutConfigurationItem>()(oldData.m_shortcuts, fakeHostComboItem);
277 const int iHostComboItemData = UIShortcutSearchFunctor<UIShortcutConfigurationItem>()(newData.m_shortcuts, fakeHostComboItem);
278 const QString strHostComboBase = iHostComboItemBase != -1 ? oldData.m_shortcuts.at(iHostComboItemBase).currentSequence() : QString();
279 const QString strHostComboData = iHostComboItemData != -1 ? newData.m_shortcuts.at(iHostComboItemData).currentSequence() : QString();
280 if (strHostComboData != strHostComboBase)
281 /* fSuccess = */ gEDataManager->setHostKeyCombination(strHostComboData);
282 }
283
284 /* Save other new shortcuts from cache: */
285 if (fSuccess)
286 {
287 QMap<QString, QString> sequencesBase;
288 QMap<QString, QString> sequencesData;
289 foreach (const UIShortcutConfigurationItem &item, oldData.m_shortcuts)
290 sequencesBase.insert(item.key(), item.currentSequence());
291 foreach (const UIShortcutConfigurationItem &item, newData.m_shortcuts)
292 sequencesData.insert(item.key(), item.currentSequence());
293 if (sequencesData != sequencesBase)
294 /* fSuccess = */ gShortcutPool->setOverrides(sequencesData);
295 }
296
297 /* Save other new things from cache: */
298 if ( fSuccess
299 && newData.m_fAutoCapture != oldData.m_fAutoCapture)
300 /* fSuccess = */ gEDataManager->setAutoCaptureEnabled(newData.m_fAutoCapture);
301 }
302 /* Return result: */
303 return fSuccess;
304}
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use