123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231 |
- #include "PinYinHelper.h"
- #include "DialogSoftKeyboard.h"
- PinYinHelper::PinYinHelper(QObject *parent) : QObject(parent)
- {
- input = 0;
- pinyinCount = 0;
- pinyinIndex = 0;
- }
- void PinYinHelper::setInput(DialogSoftKeyboard *input)
- {
- this->input = input;
- }
- void PinYinHelper::setLabs(QList<QLabel *> labCh, QList<QLabel *> labCn, QList<QLabel *> labMore)
- {
- this->labCh = labCh;
- this->labCn = labCn;
- this->labMore = labMore;
- pinyinCount = labCh.count();
- }
- //输入法的位置,可以自行调整或者增加设置
- void PinYinHelper::movePosition()
- {
- //根据用户选择的输入法位置设置-居中显示-底部填充-显示在输入框正下方
- static int deskWidth = qApp->desktop()->availableGeometry().width();
- static int deskHeight = qApp->desktop()->availableGeometry().height();
- int width = input->width();
- int height = input->height();
- if (input->position == "center") {
- QPoint pos = QPoint(deskWidth / 2 - width / 2, deskHeight / 2 - height / 2);
- input->setGeometry(pos.x(), pos.y(), width, height);
- } else if (input->position == "bottom") {
- input->setGeometry(0, deskHeight - height, deskWidth, height);
- } else if (input->position == "control") {
- QRect rect = input->currentWidget->rect();
- QPoint pos = QPoint(rect.left(), rect.bottom() + 2);
- pos = input->currentWidget->mapToGlobal(pos);
- //如果输入控件的X坐标+输入法的宽度大于桌面宽度说明右侧部分被遮住,需要X坐标往左平移
- int x = pos.x();
- if (x + width > deskWidth) {
- x = deskWidth - width;
- }
- //如果输入控件的Y坐标+输入法的高度大于桌面高度说明下侧部分被遮住,需要显示在控件上方
- int y = pos.y();
- if (y + height > deskHeight) {
- y = y - height - rect.height() - 2;
- }
- //如果XY为负数说明被遮住了部分则居中显示否则按照自动计算的XY值显示
- if (x < 0 || y < 0) {
- QPoint pos = QPoint(deskWidth / 2 - width / 2, deskHeight / 2 - height / 2);
- input->setGeometry(pos.x(), pos.y(), width, height);
- } else {
- input->setGeometry(x, y, width, height);
- }
- }
- //立即刷新界面防止残影,部分嵌入式linux硬件性能差需要刷新
- input->update();
- }
- void PinYinHelper::selectChinese(const QString &pinyin)
- {
- //清空汉字
- pinyinAll.clear();
- pinyinIndex = 0;
- //对当前的拼音查询汉字,如果超过最大数量的汉字则取前面
- //限制最大的允许输入的拼音字母数量,不限制可能出问题,谷歌拼音输入法的BUG
- int len = pinyin.length();
- if (len > 12) {
- qDebug() << "input too long";
- return;
- }
- //插入用户造词汉字,优先级最高,插入在最前面
- int indexUser = input->userKey.indexOf(pinyin);
- if (indexUser >= 0) {
- QString chineses = input->userValue.at(indexUser);
- QStringList list = chineses.split("|");
- for (int i = list.count() - 1; i >= 0; i--) {
- QString chinese = list.at(i);
- if (!chinese.isEmpty()) {
- pinyinAll.insert(0, chinese);
- }
- }
- }
- //插入用户选定的词,优先级中间,紧随用户造词其后
- int indexSelect = input->selectKey.indexOf(pinyin);
- if (indexSelect >= 0) {
- QString chineses = input->selectValue.at(indexSelect);
- QStringList list = chineses.split("|");
- for (int i = 0; i < list.count(); i++) {
- QString chinese = list.at(i);
- if (!chinese.isEmpty() && !pinyinAll.contains(chinese)) {
- pinyinAll << chinese;
- }
- }
- }
- //显示汉字
- this->pinyin = pinyin;
- showChinese();
- }
- void PinYinHelper::showChinese()
- {
- //隐藏汉字标签
- hideChinese();
- int pyCount = pinyinAll.count();
- //设置实体键盘同步模式下的汉字
- if (input->useHardKeyBoard) {
- //自动计算汉字长度隐藏多余标签 注释掉以后永远按照 chineseCount 个标签的数量显示
- checkChinese(pinyin);
- int count = 0;
- for (int i = pinyinIndex; i < pyCount; i++) {
- QString txt = QString("%1. %2").arg(count + 1).arg(pinyinAll.at(pinyinIndex));
- labCh.at(count)->setText(txt);
- count++;
- pinyinIndex++;
- if (count == pinyinCount) {
- break;
- }
- }
- } else {
- //设置当前拼音对应的汉字 横向汉字框和候选汉字框都设置
- for (int i = 0; i < pyCount; i++) {
- QString text = pinyinAll.at(i);
- labCn.at(i)->setText(text);
- labMore.at(i)->setText(text);
- if (input->autoHide) {
- labCn.at(i)->setVisible(true);
- labMore.at(i)->setVisible(true);
- }
- }
- }
- }
- void PinYinHelper::showPrevious()
- {
- int pyCount = pinyinAll.count();
- if (pinyinIndex >= (pinyinCount * 2)) {
- //每次最多显示汉字数,所以每次向前的时候索引要减 数量 * 2
- if (pinyinIndex % pinyinCount == 0) {
- pinyinIndex -= (pinyinCount * 2);
- } else {
- pinyinIndex = pyCount - (pyCount % pinyinCount) - pinyinCount;
- }
- } else {
- pinyinIndex = 0;
- }
- showChinese();
- }
- void PinYinHelper::showNext()
- {
- if (pinyinIndex < pinyinAll.count() - 1) {
- showChinese();
- }
- }
- void PinYinHelper::checkChinese(const QString &pinyin)
- {
- if (!input->useHardKeyBoard) {
- return;
- }
- //根据当前拼音长度自动隐藏标签
- int len = pinyin.length();
- if (len > 8) {
- setChVisible(input->maxCountCh - 4);
- } else if (len > 6) {
- setChVisible(input->maxCountCh - 3);
- } else if (len > 4) {
- setChVisible(input->maxCountCh - 2);
- } else if (len > 2) {
- setChVisible(input->maxCountCh - 1);
- } else {
- setChVisible(input->maxCountCh);
- }
- }
- void PinYinHelper::setChVisible(int index)
- {
- pinyinCount = index;
- for (int i = 0; i < input->maxCountCh; i++) {
- labCh.at(i)->setVisible(i < index);
- }
- }
- void PinYinHelper::clearChinese()
- {
- pinyinAll.clear();
- pinyinIndex = 0;
- hideChinese();
- }
- void PinYinHelper::hideChinese()
- {
- if (input == 0) {
- return;
- }
- //清空汉字标签
- if (input->useHardKeyBoard) {
- for (int i = 0; i < input->maxCountCh; i++) {
- labCh.at(i)->clear();
- labCh.at(i)->setVisible(false);
- }
- } else {
- for (int i = 0; i < input->maxCountCn; i++) {
- labCn.at(i)->clear();
- labMore.at(i)->clear();
- if (input->autoHide) {
- labCn.at(i)->setVisible(false);
- labMore.at(i)->setVisible(false);
- }
- }
- }
- }
|