199 lines
4.4 KiB
C++
199 lines
4.4 KiB
C++
#include "InputMethod.h"
|
|
#include <QDebug>
|
|
|
|
namespace
|
|
{
|
|
const QString DICT_PATH = "./dict/dict_pinyin.dat";
|
|
const QString DICT_USER_PATH = "./dict/dict_pinyin_user.dat";
|
|
}
|
|
|
|
InputMethod::InputMethod(QObject* aParent)
|
|
: QObject(aParent)
|
|
, mIsEnable(false)
|
|
, mSpellLen(64)
|
|
, mOutLen(64)
|
|
, mWholeSpellString()
|
|
, mUsingSpellString()
|
|
{
|
|
|
|
}
|
|
|
|
InputMethod::~InputMethod()
|
|
{
|
|
|
|
}
|
|
|
|
QString InputMethod::getUsingSpellString()
|
|
{
|
|
return mUsingSpellString;
|
|
}
|
|
|
|
bool InputMethod::getEnable()
|
|
{
|
|
return mIsEnable;
|
|
}
|
|
|
|
bool InputMethod::init(int aMaxSpellLen, int aMaxOutLen)
|
|
{
|
|
QString path = QDir::currentPath();
|
|
|
|
QFile filePinyin(DICT_PATH);
|
|
if (!filePinyin.exists())
|
|
{
|
|
QFile::copy(":/dict/dict_pinyin.dat", path + "/" + DICT_PATH);
|
|
QFile::setPermissions(path, QFileDevice::ReadOther | QFileDevice::WriteOther);
|
|
}
|
|
|
|
QFile fileUser(DICT_USER_PATH);
|
|
if (!fileUser.exists())
|
|
{
|
|
QFile::copy(":/dict/dict_pinyin_user.dat", path + "/" + DICT_USER_PATH);
|
|
QFile::setPermissions(path, QFileDevice::ReadOther | QFileDevice::WriteOther);
|
|
}
|
|
|
|
mSpellLen = aMaxSpellLen;
|
|
mOutLen = aMaxOutLen;
|
|
|
|
mIsEnable = im_open_decoder(QString("%1/" + DICT_PATH).arg(path).toLocal8Bit().data(),
|
|
QString("%1/" + DICT_USER_PATH).arg(path).toLocal8Bit().data());
|
|
if (!mIsEnable)
|
|
{
|
|
return mIsEnable;
|
|
}
|
|
|
|
im_set_max_lens(static_cast<size_t>(mSpellLen), static_cast<size_t>(mOutLen));
|
|
resetSearch();
|
|
|
|
return mIsEnable;
|
|
}
|
|
|
|
void InputMethod::deinit()
|
|
{
|
|
im_close_decoder();
|
|
}
|
|
|
|
void InputMethod::resetSearch()
|
|
{
|
|
if (mIsEnable)
|
|
{
|
|
im_reset_search();
|
|
|
|
}
|
|
}
|
|
|
|
unsigned int InputMethod::search(const QString& aSpellString)
|
|
{
|
|
if (!mIsEnable)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
QByteArray bytearray;
|
|
char* pinyin;
|
|
|
|
bytearray = aSpellString.toUtf8();
|
|
pinyin = bytearray.data();
|
|
|
|
size_t candnum = im_search(pinyin, static_cast<size_t>(bytearray.size()));
|
|
|
|
mWholeSpellString = aSpellString;
|
|
mUsingSpellString = aSpellString;
|
|
|
|
if (static_cast<int>(candnum) < mOutLen)
|
|
{
|
|
return static_cast<unsigned int>(candnum);
|
|
}
|
|
return static_cast<unsigned int>(mOutLen);
|
|
}
|
|
|
|
QStringList InputMethod::getCandidate(unsigned int aCnadidateNum)
|
|
{
|
|
QStringList textList;
|
|
if (aCnadidateNum == 0)
|
|
{
|
|
return textList;
|
|
}
|
|
|
|
char16* candidateBuffer = new char16[mOutLen];
|
|
for (unsigned int i = 0; i < aCnadidateNum; ++i)
|
|
{
|
|
char16* cand;
|
|
cand = im_get_candidate(i, candidateBuffer, static_cast<unsigned int>(mOutLen));
|
|
if (cand)
|
|
{
|
|
textList.append(QString::fromUtf16(cand));
|
|
}
|
|
else
|
|
{
|
|
continue;
|
|
}
|
|
}
|
|
delete[] candidateBuffer;
|
|
return textList;
|
|
}
|
|
|
|
int InputMethod::getSelectedChineseCount()
|
|
{
|
|
return static_cast<int>(im_get_fixed_len());
|
|
}
|
|
|
|
QStringList InputMethod::cancelLastChoice()
|
|
{
|
|
size_t candidateCount = im_cancel_last_choice();
|
|
const uint16* spellStringIndexArray;
|
|
size_t size = im_get_spl_start_pos(spellStringIndexArray) + 1;
|
|
size_t chooseNum = im_get_fixed_len();
|
|
if (chooseNum <= size)
|
|
{
|
|
mUsingSpellString = mWholeSpellString.mid(spellStringIndexArray[chooseNum]);
|
|
}
|
|
|
|
if (0 == candidateCount)
|
|
{
|
|
resetSearch();
|
|
candidateCount = search(mUsingSpellString);
|
|
}
|
|
return getCandidate(static_cast<unsigned int>(candidateCount));
|
|
}
|
|
|
|
QStringList InputMethod::chooseCandidate(int aIndex)
|
|
{
|
|
size_t candidateCount = im_choose(size_t(aIndex));
|
|
|
|
const uint16* spellStringIndexArray;
|
|
size_t size = im_get_spl_start_pos(spellStringIndexArray) + 1;
|
|
size_t chooseNum = im_get_fixed_len();
|
|
if (chooseNum <= size)
|
|
{
|
|
mUsingSpellString = mWholeSpellString.mid(spellStringIndexArray[chooseNum]);
|
|
}
|
|
|
|
if (1 >= candidateCount)
|
|
{
|
|
mWholeSpellString = mUsingSpellString;
|
|
return QStringList();
|
|
}
|
|
|
|
return getCandidate(static_cast<unsigned int>(candidateCount));
|
|
//delete spellStringIndexArray;
|
|
//decoder saved pinyin str
|
|
// size_t* aa = new size_t;
|
|
// const char* bb = im_get_sps_str(aa);
|
|
// qDebug()<< *aa;
|
|
// qDebug()<< bb;
|
|
|
|
//choosed hanzi length
|
|
// qDebug()<< im_get_fixed_len();
|
|
|
|
//nothing to do
|
|
//im_cancel_input();
|
|
|
|
//pinyin split to hanzi index
|
|
// const uint16* aa = new uint16;
|
|
// size_t bb = im_get_spl_start_pos(aa);
|
|
// for(int i=0;i<=bb;++i)
|
|
// {
|
|
// qDebug()<<aa[i];
|
|
// }
|
|
}
|