1#include "VirtualKeyboardSettingsDialog.h"
7#include <QStyleFactory>
9CVirtualKeyboardSettingsDialog::CVirtualKeyboardSettingsDialog(QWidget *parent)
11 , m_settings(new QSettings(
"YourCompany",
"YourApp", this))
13 setWindowTitle(tr(
"虚拟键盘设置"));
21 connect(m_applyButton, &QPushButton::clicked,
this, &CVirtualKeyboardSettingsDialog::applySettings);
22 connect(m_defaultsButton, &QPushButton::clicked,
this, &CVirtualKeyboardSettingsDialog::restoreDefaults);
23 connect(m_okButton, &QPushButton::clicked,
this, &QDialog::accept);
24 connect(m_cancelButton, &QPushButton::clicked,
this, &QDialog::reject);
26 connect(m_languageCombo, QOverload<int>::of(&QComboBox::currentIndexChanged),
27 this, &CVirtualKeyboardSettingsDialog::onLanguageChanged);
28 connect(m_themeCombo, QOverload<int>::of(&QComboBox::currentIndexChanged),
29 this, &CVirtualKeyboardSettingsDialog::onThemeChanged);
32CVirtualKeyboardSettingsDialog::~CVirtualKeyboardSettingsDialog()
36void CVirtualKeyboardSettingsDialog::setupUi()
38 QVBoxLayout *mainLayout =
new QVBoxLayout(
this);
41 QGroupBox *languageGroup =
new QGroupBox(tr(
"输入语言"),
this);
42 QVBoxLayout *languageLayout =
new QVBoxLayout(languageGroup);
44 m_languageCombo =
new QComboBox(
this);
45 m_localeList = getAvailableLocales();
48 for (
const QString &locale : m_localeList) {
50 QString displayName = QString(
"%1 (%2)")
51 .arg(loc.nativeLanguageName())
52 .arg(loc.nativeCountryName());
53 m_languageCombo->addItem(displayName, locale);
56 languageLayout->addWidget(
new QLabel(tr(
"选择输入语言:"),
this));
57 languageLayout->addWidget(m_languageCombo);
58 languageLayout->addStretch();
61 QGroupBox *appearanceGroup =
new QGroupBox(tr(
"外观设置"),
this);
62 QVBoxLayout *appearanceLayout =
new QVBoxLayout(appearanceGroup);
65 QHBoxLayout *themeLayout =
new QHBoxLayout();
66 themeLayout->addWidget(
new QLabel(tr(
"键盘主题:"),
this));
67 m_themeCombo =
new QComboBox(
this);
68 QStringList themes = getAvailableStyles();
70 m_themeCombo->addItems(themes);
71 themeLayout->addWidget(m_themeCombo);
72 themeLayout->addStretch();
73 appearanceLayout->addLayout(themeLayout);
76 QHBoxLayout *keySizeLayout =
new QHBoxLayout();
77 keySizeLayout->addWidget(
new QLabel(tr(
"按键大小:"),
this));
78 m_keySizeSlider =
new QSlider(Qt::Horizontal,
this);
79 m_keySizeSlider->setRange(50, 150);
80 m_keySizeSlider->setValue(100);
81 m_keySizeSlider->setTickPosition(QSlider::TicksBelow);
82 m_keySizeSlider->setTickInterval(10);
83 keySizeLayout->addWidget(m_keySizeSlider, 1);
85 m_keySizeLabel =
new QLabel(
"100%",
this);
86 keySizeLayout->addWidget(m_keySizeLabel);
87 appearanceLayout->addLayout(keySizeLayout);
90 connect(m_keySizeSlider, &QSlider::valueChanged, [
this](
int value) {
91 m_keySizeLabel->setText(QString(
"%1%").arg(value));
95 m_smallTextCheck =
new QCheckBox(tr(
"显示按键辅助小文本"),
this);
96 m_autoCapsCheck =
new QCheckBox(tr(
"自动大写"),
this);
97 appearanceLayout->addWidget(m_smallTextCheck);
98 appearanceLayout->addWidget(m_autoCapsCheck);
101 QGroupBox *feedbackGroup =
new QGroupBox(tr(
"按键反馈"),
this);
102 QVBoxLayout *feedbackLayout =
new QVBoxLayout(feedbackGroup);
104 m_keySoundCheck =
new QCheckBox(tr(
"启用按键音"),
this);
105 m_hapticCheck =
new QCheckBox(tr(
"启用震动反馈"),
this);
107 feedbackLayout->addWidget(m_keySoundCheck);
108 feedbackLayout->addWidget(m_hapticCheck);
111 QHBoxLayout *buttonLayout =
new QHBoxLayout();
112 m_defaultsButton =
new QPushButton(tr(
"恢复默认"),
this);
113 m_applyButton =
new QPushButton(tr(
"应用"),
this);
114 m_okButton =
new QPushButton(tr(
"确定"),
this);
115 m_cancelButton =
new QPushButton(tr(
"取消"),
this);
117 buttonLayout->addWidget(m_defaultsButton);
118 buttonLayout->addStretch();
119 buttonLayout->addWidget(m_applyButton);
120 buttonLayout->addWidget(m_okButton);
121 buttonLayout->addWidget(m_cancelButton);
124 mainLayout->addWidget(languageGroup);
125 mainLayout->addWidget(appearanceGroup);
126 mainLayout->addWidget(feedbackGroup);
127 mainLayout->addStretch();
128 mainLayout->addLayout(buttonLayout);
131void CVirtualKeyboardSettingsDialog::loadSettings()
134 QString savedLocale = m_settings->value(
"virtualkeyboard/locale",
"en_US").toString();
135 int localeIndex = m_localeList.indexOf(savedLocale);
136 if (localeIndex >= 0) {
137 m_languageCombo->setCurrentIndex(localeIndex);
141 QString savedTheme = m_settings->value(
"virtualkeyboard/theme",
"默认").toString();
142 int themeIndex = m_themeCombo->findText(savedTheme);
143 if (themeIndex >= 0) {
144 m_themeCombo->setCurrentIndex(themeIndex);
148 m_keySoundCheck->setChecked(m_settings->value(
"virtualkeyboard/keySound",
true).toBool());
149 m_hapticCheck->setChecked(m_settings->value(
"virtualkeyboard/haptic",
false).toBool());
150 m_keySizeSlider->setValue(m_settings->value(
"virtualkeyboard/keySize", 100).toInt());
151 m_smallTextCheck->setChecked(m_settings->value(
"virtualkeyboard/smallText",
true).toBool());
152 m_autoCapsCheck->setChecked(m_settings->value(
"virtualkeyboard/autoCaps",
true).toBool());
155void CVirtualKeyboardSettingsDialog::saveSettings()
157 m_settings->setValue(
"virtualkeyboard/locale",
158 m_languageCombo->currentData().toString());
159 m_settings->setValue(
"virtualkeyboard/theme", m_themeCombo->currentText());
160 m_settings->setValue(
"virtualkeyboard/keySound", m_keySoundCheck->isChecked());
161 m_settings->setValue(
"virtualkeyboard/haptic", m_hapticCheck->isChecked());
162 m_settings->setValue(
"virtualkeyboard/keySize", m_keySizeSlider->value());
163 m_settings->setValue(
"virtualkeyboard/smallText", m_smallTextCheck->isChecked());
164 m_settings->setValue(
"virtualkeyboard/autoCaps", m_autoCapsCheck->isChecked());
169void CVirtualKeyboardSettingsDialog::applySettings()
172 QString selectedLocale = m_languageCombo->currentData().toString();
173 applyLanguage(selectedLocale);
176 applyTheme(m_themeCombo->currentText());
179 applyKeySound(m_keySoundCheck->isChecked());
180 applyHapticFeedback(m_hapticCheck->isChecked());
181 applyKeySize(m_keySizeSlider->value());
193 qDebug() <<
"Virtual keyboard settings applied";
196void CVirtualKeyboardSettingsDialog::restoreDefaults()
198 m_languageCombo->setCurrentIndex(m_localeList.indexOf(
"en_US"));
199 m_themeCombo->setCurrentIndex(0);
200 m_keySoundCheck->setChecked(
true);
201 m_hapticCheck->setChecked(
false);
202 m_keySizeSlider->setValue(100);
203 m_smallTextCheck->setChecked(
true);
204 m_autoCapsCheck->setChecked(
true);
207void CVirtualKeyboardSettingsDialog::onLanguageChanged(
int index)
209 if (index >= 0 && index < m_localeList.size()) {
211 QString locale = m_localeList[index];
212 qDebug() <<
"Language preview:" << locale;
216void CVirtualKeyboardSettingsDialog::onThemeChanged(
int index)
222QStringList CVirtualKeyboardSettingsDialog::getAvailableLocales()
240QStringList CVirtualKeyboardSettingsDialog::getAvailableStyles()
243 return QStyleFactory::keys();
246void CVirtualKeyboardSettingsDialog::applyLanguage(
const QString &locale)
254void CVirtualKeyboardSettingsDialog::applyTheme(
const QString &theme)
258 qApp->setStyle(theme);
261 qApp->setStyle(QStyleFactory::create(
"Fusion"));
265void CVirtualKeyboardSettingsDialog::applyKeySound(
bool enabled)
269 qDebug() <<
"Key sound:" << enabled;
274void CVirtualKeyboardSettingsDialog::applyHapticFeedback(
bool enabled)
277 qDebug() <<
"Haptic feedback:" << enabled;
280void CVirtualKeyboardSettingsDialog::applyKeySize(
int size)
284 qDebug() <<
"Key size:" << size <<
"%";
287 qputenv(
"QT_VIRTUALKEYBOARD_KEY_SIZE", QByteArray::number(size));