Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #136 #232

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 36 additions & 23 deletions src/model/UserphraseModel.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*

* chewing-editor: Chewing userphrase editor
* Copyright (C) 2014 Chewing Development Team

Expand All @@ -18,7 +19,7 @@
*/

#include "UserphraseModel.h"

#include <string>
#include <QDebug>

static void logger(void *data, int level, const char *fmt, ...)
Expand Down Expand Up @@ -194,35 +195,47 @@ void UserphraseModel::exportUserphrase(std::shared_ptr<UserphraseExporter> expor
QString UserphraseModel::checkBopomofo(const QString &bopomofo) const
{
QString replaceBopomofo = bopomofo;

replaceBopomofo.replace(QString::fromUtf8("一"),QString::fromUtf8("ㄧ"));
replaceBopomofo.replace(QString::fromUtf8("丫"),QString::fromUtf8("ㄚ"));
replaceBopomofo= replaceBopomofo.simplified();

return replaceBopomofo;
}

void UserphraseModel::add(const QString &phrase, const QString &bopomofo)

QStringList UserphraseModel::splitPhrases(const QString &phrases)
{
QString replaceBopomofo = checkBopomofo(bopomofo);
auto ret = chewing_userphrase_add(
ctx_.get(),
phrase.toUtf8().constData(),
replaceBopomofo.toUtf8().constData());

addresult_ = ret;

if (ret > 0) {
emit beginResetModel();
userphrase_.insert(Userphrase{
phrase,
bopomofo
});
emit endResetModel();
emit addNewPhraseCompleted(userphrase_[userphrase_.size()-1]);
} else {
qWarning() << "chewing_userphrase_add() returns" << ret;
refresh();
emit addNewPhraseFailed();
}
QStringList sections = phrases.split(QRegExp("[,/^]"));
return sections;
}

void UserphraseModel::add(const QString &phrases, const QString &bopomofo)
{
QStringList phraseList = splitPhrases(phrases);
for(int i = 0 ; i < phraseList.size() ; i++)
{
QString phrase = QString(phraseList.at(i).toLocal8Bit().constData());
QString replaceBopomofo = checkBopomofo(bopomofo);
auto ret = chewing_userphrase_add(
ctx_.get(),
phrase.toUtf8().constData(),
replaceBopomofo.toUtf8().constData());
addresult_ = ret;
if (ret > 0) {
emit beginResetModel();
userphrase_.insert(Userphrase{
phrase,
bopomofo
});
emit endResetModel();
emit addNewPhraseCompleted(userphrase_[userphrase_.size()-1]);
} else {
qWarning() << "chewing_userphrase_add() returns" << ret;
refresh();
emit addNewPhraseFailed();
}
}
}

const Userphrase *UserphraseModel::getUserphrase(const QModelIndex& idx)
Expand Down
3 changes: 2 additions & 1 deletion src/model/UserphraseModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class UserphraseModel final: public QAbstractListModel {
UserphraseModel(const UserphraseModel& x) = delete;
UserphraseModel& operator=(const UserphraseModel& x) = delete;
virtual ~UserphraseModel() = default;

virtual int rowCount(const QModelIndex &parent = QModelIndex()) const override;
virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;

Expand All @@ -65,6 +65,7 @@ class UserphraseModel final: public QAbstractListModel {
public slots:
void refresh();
void add(const QString &phrase, const QString &bopomofo);
QStringList splitPhrases(const QString &phrases);
void importUserphrase(std::shared_ptr<UserphraseImporter> importer);
void exportUserphrase(std::shared_ptr<UserphraseExporter> exporter);
void remove(QModelIndexList indexList);
Expand Down