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

Let undo can restore multiple pharses at one time #233

Open
wants to merge 2 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
23 changes: 14 additions & 9 deletions src/model/UserphraseModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ void UserphraseModel::remove(QModelIndexList indexList)
auto last = indexList.first().row();

emit beginRemoveRows(indexList.first().parent(), first, last);

int remove_cnt = 0;
foreach(auto item, indexList) {
auto index = item.row();

Expand All @@ -106,14 +106,15 @@ void UserphraseModel::remove(QModelIndexList indexList)
userphrase_[index].bopomofo_.toUtf8().constData());
if (ret > 0) {
removerecord_.push_back(Userphrase{userphrase_[index]});
// FIXME: std::vector::erase is an inefficient operation.
remove_cnt++;
// FIXME: std::vector::erase is an inefficient operation.
userphrase_.erase(userphrase_.begin() + index);
} else {
qWarning() << "chewing_userphrase_remove() returns" << ret;
}
// FIXME: Handle chewing_userphrase_remove fails.
}

maxundocnt.push_back(remove_cnt);
emit endRemoveRows();

emit removePhraseCompleted(indexList.size());
Expand Down Expand Up @@ -232,11 +233,15 @@ const Userphrase *UserphraseModel::getUserphrase(const QModelIndex& idx)

void UserphraseModel::undo()
{
if (!removerecord_.empty()) {
auto last = removerecord_.end() - 1;
const QString phrase = last->display_;
add(last->phrase_, last->bopomofo_);
removerecord_.erase(last);
emit undoCompleted(phrase);
int cnt = *(maxundocnt.end() -1);
if (!removerecord_.empty() && cnt>0) {
while (cnt--) {
auto last = removerecord_.end() -1;
const QString phrase = last->display_;
add(last->phrase_, last->bopomofo_);
removerecord_.erase(last);
emit undoCompleted(phrase);
}
maxundocnt.pop_back();
}
}
1 change: 1 addition & 0 deletions src/model/UserphraseModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,6 @@ public slots:
std::unique_ptr<ChewingContext, void (*)(ChewingContext*)> ctx_;
UserphraseSet userphrase_;
std::vector<Userphrase> removerecord_;
std::vector<int> maxundocnt;
int addresult_;
};