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 undo unintuitive behavior #214

Open
wants to merge 1 commit 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
19 changes: 13 additions & 6 deletions src/model/UserphraseModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ void UserphraseModel::remove(QModelIndexList indexList)

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

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

Expand All @@ -106,13 +107,15 @@ void UserphraseModel::remove(QModelIndexList indexList)
userphrase_[index].bopomofo_.toUtf8().constData());
if (ret > 0) {
removerecord_.push_back(Userphrase{userphrase_[index]});
_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.
}
removecount_.push_back(_cnt);

emit endRemoveRows();

Expand Down Expand Up @@ -232,11 +235,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);
if (!removecount_.empty() && !removerecord_.empty()) {
unsigned int remove_n = *(removecount_.end()-1) ;
removecount_.pop_back();
for(unsigned int i = 0 ;i < remove_n ; i ++){
auto last = removerecord_.end() -1;
const QString phrase = last->display_;
add(last->phrase_, last->bopomofo_);
removerecord_.erase(last);
emit undoCompleted(phrase);
}
}
}
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<unsigned int> removecount_;
int addresult_;
};