Skip to content

Commit

Permalink
fix: 修复输入法分类不正确
Browse files Browse the repository at this point in the history
* 不自己生成 map,直接从 xkeyboard 读取键盘排列数据
* 当不是键盘排列时,通过 language code 生成分类

Fixes linuxdeepin/developer-center#3685
  • Loading branch information
zsien committed Feb 24, 2023
1 parent a0419c0 commit e79247a
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 843 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ project(deepin-fcitx5configtool-plugin VERSION 5.0.5)


set(CMAKE_MODULE_PATH ${ECM_MODULE_PATH} ${ECM_KDE_MODULE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
set(DEFAULT_XKB_RULES "evdev" CACHE STRING "Xkb rules name")

find_package(XKeyboardConfig REQUIRED)
find_package(IsoCodes REQUIRED)
configure_file(config.h.in config.h)
Expand Down
3 changes: 3 additions & 0 deletions config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
#cmakedefine ISOCODES_ISO639_3_JSON "@ISOCODES_ISO639_3_JSON@"
#cmakedefine ISOCODES_ISO639_5_JSON "@ISOCODES_ISO639_5_JSON@"

#define XKEYBOARDCONFIG_XKBBASE "@XKEYBOARDCONFIG_XKBBASE@"
#define DEFAULT_XKB_RULES "@DEFAULT_XKB_RULES@"

#define FCITX5_QT5_GUI_WRAPPER "@FCITX5_QT5_GUI_WRAPPER@"

#endif // _KCM_FCITX5_CONFIG_H_
80 changes: 42 additions & 38 deletions src/addim/configlib/addimmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "addimmodel.h"

#include "glo.h"
#include "xkbrules.h"

#include <fcitx-utils/i18n.h>

Expand Down Expand Up @@ -143,6 +144,28 @@ static QString languageName(const QString &langCode) {
}
}

static QString getEnglishLanguageName(const QString &uniqueName, const QString &languageCode)
{
auto &xkbrules = XkbRules::instance();
QString englishName;

if (uniqueName.startsWith("keyboard-")) {
auto layout = xkbrules.layout(uniqueName);
englishName = layout.description;
}

if (englishName.isEmpty()) {
englishName = QLocale::languageToString(QLocale(languageCode).language());
}

if (englishName.isEmpty()) {
englishName = "unknown";
qInfo("NOTICE: uniqueName [%s] not found english name. unknown.", uniqueName.toStdString().c_str());
}

return englishName;
}

AvailIMModel::AvailIMModel(QObject *parent) : CategorizedItemModel(parent) {}

QVariant AvailIMModel::dataForCategory(const QModelIndex &index, int role) const {
Expand All @@ -152,13 +175,13 @@ QVariant AvailIMModel::dataForCategory(const QModelIndex &index, int role) const
switch (role) {
case Qt::DisplayRole:
language = languageName(m_filteredIMEntryList[index.row()].first);
englishName = getEnglishLanguageName(m_filteredIMEntryList[index.row()].second.at(0).uniqueName());
englishName = getEnglishLanguageName(m_filteredIMEntryList[index.row()].second.at(0).uniqueName(), m_filteredIMEntryList[index.row()].first);

categoryLanguageName = language + " - " + englishName;
return categoryLanguageName;

case FcitxEnglishNameRole:
englishName = getEnglishLanguageName(m_filteredIMEntryList[index.row()].second.at(0).uniqueName());
englishName = getEnglishLanguageName(m_filteredIMEntryList[index.row()].second.at(0).uniqueName(), m_filteredIMEntryList[index.row()].first);
return englishName;

case FcitxLanguageRole:
Expand Down Expand Up @@ -212,41 +235,22 @@ QVariant AvailIMModel::dataForItem(const QModelIndex &index, int role) const {
return QVariant();
}

QString buildKeyByIMNameAndLanguageCode(const QString &name, const QString& languageCode)
{
QString key;
int start_loc;
start_loc = name.indexOf(" - ");
if (start_loc != -1) {
start_loc += 3;
int end_loc = name.indexOf(" - ", start_loc);
if (end_loc != -1) {
key = name.mid(start_loc, end_loc - start_loc);
QString buildKeyByIMNameAndLanguageCode(const QString &uniqueName, const QString &languageCode) {
auto &xkbrules = XkbRules::instance();
if (uniqueName.startsWith("keyboard-")) {
QString description = xkbrules.layout(uniqueName).description;
if (!description.isEmpty()) {
return XkbRules::tr(description);
}
else {
key = name.mid(start_loc, -1);
}
}
else {
key = languageName(languageCode);
}

start_loc = key.indexOf("(");
if (start_loc != -1) {
key = key.mid(0, start_loc);
}

start_loc = key.indexOf("");
if (start_loc != -1) {
key = key.mid(0, start_loc);
}
key = key.trimmed();
return key;
return QLocale(languageCode).name();
}

void AvailIMModel::filterIMEntryList(
const FcitxQtInputMethodEntryList &imEntryList,
const FcitxQtStringKeyValueList &enabledIMList) {

beginResetModel();

FcitxQtStringKeyValueList useIMList = getUseIMList();
Expand All @@ -265,7 +269,10 @@ void AvailIMModel::filterIMEntryList(
QString englishName;
QString loc_name = QLocale().name();
for (const FcitxQtInputMethodEntry &im : imEntryList) {
key = buildKeyByIMNameAndLanguageCode(im.name(), im.languageCode());
uniqueName = im.uniqueName();
qInfo("uniqueName [%s]", uniqueName.toStdString().c_str());

key = buildKeyByIMNameAndLanguageCode(uniqueName, im.languageCode());

if (!languageMap.contains(key)) {
idx = m_filteredIMEntryList.count();
Expand All @@ -278,10 +285,7 @@ void AvailIMModel::filterIMEntryList(
}
m_filteredIMEntryList[idx].second.append(im);

uniqueName = im.uniqueName();
qInfo("uniqueName [%s]", uniqueName.toStdString().c_str());

englishName = getEnglishLanguageName(uniqueName);
englishName = getEnglishLanguageName(uniqueName, im.languageCode());
if (loc_name.startsWith("zh") && englishName == "Chinese") {
m_filteredUseIMLanguageList[idx].second = true;
continue;
Expand Down Expand Up @@ -389,8 +393,8 @@ void AvailIMModel::getInputMethodEntryList(int row, FcitxQtStringKeyValueList& c
}
}

QString key = buildKeyByIMNameAndLanguageCode(im.name(), im.languageCode());
QString englishName = getEnglishLanguageName(uniqueName);
QString key = buildKeyByIMNameAndLanguageCode(im.uniqueName(), im.languageCode());
QString englishName = getEnglishLanguageName(uniqueName, im.languageCode());

if (iter != useIMList.end()) {
if (entry_name.contains(matchStr, Qt::CaseInsensitive) ||
Expand Down Expand Up @@ -495,8 +499,8 @@ bool IMProxyModel::filterIM(const QModelIndex &index) const {
return true;
}

QString key = buildKeyByIMNameAndLanguageCode(name, languageCode);
QString englishName = getEnglishLanguageName(uniqueName);
QString key = buildKeyByIMNameAndLanguageCode(uniqueName, languageCode);
QString englishName = getEnglishLanguageName(uniqueName, languageCode);

bool flag = true;
if (!m_filterText.isEmpty()) {
Expand Down
Loading

0 comments on commit e79247a

Please sign in to comment.